-
Notifications
You must be signed in to change notification settings - Fork 17
/
utils.php
1972 lines (1539 loc) · 58 KB
/
utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// The source code packaged with this file is Free Software, Copyright (C) 2005-2010 by
// Ricardo Galli <gallir at uib dot es>.
// It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
// You can get copies of the licenses here:
// http://www.affero.org/oagpl.html
// AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".
function unaccent($string)
{
return strtr($string, array(
// Decompositions for Latin-1 Supplement
chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
chr(195).chr(143) => 'I',
chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
chr(195).chr(156) => 'U',
chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
chr(195).chr(178) => 'o',
chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
chr(195).chr(188) => 'u',
// Decompositions for Latin Extended-A
chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
));
}
function htmlentities2unicodeentities($input)
{
$input = utf8_for_xml($input);
$table = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
$htmlEntities = array_values($table);
$entitiesDecoded = array_keys($table);
$num = count($entitiesDecoded);
for ($u = 0; $u < $num; $u++) {
$utf8Entities[$u] = '&#'.ord($entitiesDecoded[$u]).';';
}
return str_replace($htmlEntities, $utf8Entities, $input);
}
function utf8_for_xml($string)
{
return preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $string);
}
function url_no_scheme($url)
{
return preg_replace('/^https{0,1}:/', '', $url);
}
function clean_input_url($string)
{
$string = preg_replace('/ /', '+', trim(stripslashes(mb_substr($string, 0, 512))));
$string = preg_replace('/[<>\r\n\t]/', '', $string);
$string = preg_replace('/(utm_\w+?|&feature)=[^&]*/', '', $string); // Delete common variables for Analitycs and Youtube
$string = preg_replace('/&{2,}/', '&', $string); // Delete duplicates &
$string = preg_replace('/&+$/', '', $string); // Delete useless & at the end
$string = preg_replace('/\?&+/', '?', $string); // Delete useless & after ?
$string = preg_replace('/\?&*$/', '', $string); // Delete empty queries
return $string;
}
function clean_input_string($string)
{
return preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', stripslashes($string));
}
function get_hex_color($color, $prefix = '')
{
return $prefix.substr(preg_replace('/[^a-f\d]/i', '', $color), 0, 6);
}
function get_negative_vote($value)
{
global $globals;
return $globals['negative_votes_values'][$value];
}
function user_exists($username, $ignore = 0)
{
global $db;
$res = $db->get_var('SELECT user_id FROM users WHERE user_login = "'.$db->escape($username).'" AND user_id != "'.$ignore.'"');
return $res ? true : false;
}
function email_exists($email, $check_previous_registered = true)
{
global $db;
$parts = explode('@', $email);
$domain = $parts[1];
$subparts = explode('+', $parts[0]); // Because we allow [email protected]
$user = $db->escape($subparts[0]);
$domain = $db->escape($domain);
$res = $db->get_var("SELECT COUNT(*) FROM users WHERE user_email = '$user@$domain' or user_email LIKE '$user+%@$domain'");
if ($res) {
return $res;
}
if (!$check_previous_registered) {
return false;
}
// Check the same email wasn't used recently for another account
$res = $db->get_var("SELECT count(*) FROM users WHERE (user_email_register = '$user@$domain' or user_email_register LIKE '$user+%@$domain') and user_date > date_sub(now(), interval 1 year)");
return $res ?: false;
}
function check_email($email)
{
global $globals;
require_once mnminclude.'ban.php';
if (!preg_match('/^[a-z0-9_\-\.]+(\+[a-z0-9_\-\.]+)*@[a-z0-9_\-\.]+\.[a-z]{2,6}$/i', $email)) {
return false;
}
list($username, $domain) = explode('@', $email);
if ((substr_count($username, '.') > 3) || preg_match('/\.{2,}/', $username)) {
return false; // Doesn't allow "..+" or more than 2 dots
}
// check both, the full address and the domain
if (check_ban($email, 'email') || check_ban($domain, 'email')) {
return false;
}
if (check_domain_disposable($domain)) {
return false;
}
return true;
}
function check_username($name)
{
global $current_user;
$len = mb_strlen($name);
return (
preg_match('/^\p{L}[\._\p{L}\d]+$/ui', $name)
&& ($len > 2)
&& ($len <= 24)
&& ($current_user->admin || !preg_match('/^admin/i', $name))
); // Does not allow nicks begining with "admin"
}
function check_password($password)
{
return preg_match("/^(?=.{6,})(?=(.*[a-z].*))(?=(.*[A-Z0-9].*)).*$/", $password);
}
function txt_time_diff($from, $now = 0)
{
global $globals;
$now = $now ?: $globals['now'];
$diff = $now - $from;
$days = intval($diff / 86400);
$diff = $diff % 86400;
$hours = intval($diff / 3600);
$diff = $diff % 3600;
$minutes = intval($diff / 60);
$secs = $diff % 60;
$txt = '';
if ($days > 1) {
$txt .= ' '.$days.' '._('días');
} elseif ($days === 1) {
$txt .= ' '.$days.' '._('día');
}
if ($hours > 1) {
$txt .= ' '.$hours.' '._('horas');
} elseif ($hours === 1) {
$txt .= ' '.$hours.' '._('hora');
}
if ($minutes > 1) {
$txt .= ' '.$minutes.' '._('minutos');
} elseif ($minutes === 1) {
$txt .= ' '.$minutes.' '._('minuto');
}
if ($txt) {
return $txt;
}
if ($secs < 5) {
return ' '._('nada');
}
return ' '.$secs.' '._('segundos');
}
function txt_shorter($string, $len = 70)
{
if (mb_strlen($string) > $len) {
return mb_substr($string, 0, $len - 3).'...';
}
return $string;
}
// Used to get the text content for stories and comments
function clean_text($string, $wrap = 0, $replace_nl = true, $maxlength = 0)
{
$string = stripslashes(trim($string));
$string = preg_replace('/\r\n/u', "\n", $string); // Change \r\n to \n to show right chars' counter
$string = preg_replace('/\t/s', ' ', $string); //    
$string = clear_whitespace($string);
$string = html_entity_decode($string, ENT_COMPAT, 'UTF-8');
// Replace two "-" by a single longer one, to avoid problems with xhtml comments
//$string = preg_replace('/--/', '–', $string);
if ($wrap > 0) {
$string = wordwrap($string, $wrap, " ", 1);
}
if ($replace_nl) {
$string = preg_replace('/[\n\r]+/su', ' ', $string);
}
if ($maxlength > 0) {
$string = mb_substr($string, 0, $maxlength);
}
$string = @htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
return preg_replace('/(\d+) +(\d{3,})/u', "$1 $2", $string); // Avoid to wrap in the middle of numbers with thousands' space separator
}
function clean_text_with_tags($string, $wrap = 0, $replace_nl = true, $maxlength = 0)
{
$string = add_tags(clean_text($string, $wrap, $replace_nl, $maxlength));
$string = preg_replace_callback('/(?:<|<)(\/{0,1})(\w{1,6})(?:>|>)/', function ($matches) {
global $globals;
static $open_tags = array();
if (!preg_match('/^('.$globals['enabled_tags'].')$/', $matches[2])) {
return $matches[0];
}
if ($matches[1] === '/') {
if (count($open_tags) && $open_tags[count($open_tags) - 1] != $matches[2]) {
return $matches[0];
}
array_pop($open_tags);
return "</$matches[2]>";
}
$open_tags[] = $matches[2];
return "<$matches[2]>";
}, $string);
return preg_replace('/<\/(\w{1,6})>( *)<(\1)>/', "$2", close_tags($string)); // Deletes useless close+open tags
}
function close_tags(&$string)
{
return preg_replace_callback('/(?:<\s*(\/{0,1})\s*([^>]+)>|$)/', function ($matches) {
static $open_tags = array();
if (empty($matches[0])) {
// End of text, close open tags
$end = '';
while (($t = array_pop($open_tags))) {
$end .= "</$t>";
}
return $end ? ("\n$end\n") : '';
}
if ($matches[1] && ($matches[1][0] === '/')) {
if (count($open_tags) && $open_tags[count($open_tags) - 1] == $matches[2]) {
array_pop($open_tags);
} else {
return ' '; // Don't allow misplaced or wrong tags
}
} else {
$open_tags[] = $matches[2];
}
return $matches[0];
}, $string);
}
function clean_lines($string)
{
return preg_replace('/[\n\r]{6,}/', "\n\n", $string);
}
function getDomFromHtml($html)
{
libxml_use_internal_errors(true);
$DOM = new DOMDocument;
$DOM->recover = true;
$DOM->preserveWhiteSpace = false;
$DOM->substituteEntities = false;
$DOM->loadHtml('<?xml encoding="UTF-8">'.$html, LIBXML_NOBLANKS | LIBXML_ERR_NONE);
libxml_use_internal_errors(false);
return $DOM;
}
function html_fix($html)
{
return html_remove_headers(getDomFromHtml($html)->saveHTML());
}
function html_xpath_clean($html, $attributes = array('src', 'href'))
{
global $globals;
if (empty($html)) {
return '';
}
$DOM = getDomFromHtml($html);
$xpath = new DOMXPath($DOM);
$query = '//@*';
if ($attributes) {
$query .= '[local-name() != "'.implode('" and local-name() != "', $attributes).'"]';
}
foreach ($xpath->query($query) as $node) {
$node->parentNode->removeAttribute($node->nodeName);
}
foreach ($xpath->query('//img') as $node) {
$src = $node->getAttribute('src');
$local = ($globals['server_name'] === parse_url($src, PHP_URL_HOST));
if (($local === false) && !preg_match('#^https://.*\.(png|jpg|jpeg|gif)$#i', $src)) {
$node->parentNode->removeChild($node);
}
}
foreach ($xpath->query('//iframe') as $node) {
$src = $node->getAttribute('src');
if (!preg_match('#^https://(www\.youtube\.com/embed|player\.vimeo\.com/video)/#', $src)) {
$node->parentNode->removeChild($node);
}
}
return html_remove_headers($DOM->saveHTML());
}
function html_remove_headers($html)
{
return preg_replace('#<(?:!DOCTYPE|/?(?:\?xml|html|head|body))[^>]*>\s*#i', '', $html);
}
function clean_html_with_tags($string)
{
$string = html_fix(strip_tags($string, '<p><strong><b><i><em><u><a><s><h2><h3><ul><ol><li><img><iframe><blockquote>'));
return html_xpath_clean($string);
}
function text_to_summary($string, $length = 50)
{
$string = strip_tags(str_replace('<p>', ' <p>', $string));
// Remove references to comments and number in notes referemces
$string = preg_replace('/(?:#\d+|[\r\n\t]+|,\d+|http\S+|{.+?})\s/u', ' ', $string);
$len = mb_strlen($string);
$string = mb_substr($string, 0, $length);
if (mb_strlen($string) < $len) {
$string = preg_replace('/ *[\w&;]*$/', '', $string);
$string = preg_replace('/\s\S{1,20}$/', '', $string).'...';
}
return $string;
}
function add_tags($string)
{
// Convert to em, strong and strike tags
$regexp = '_[^\s<>_]+_\b|\*[^\s<>]+\*|\-([^\s\-<>]+)\-';
return preg_replace_callback('/([ \t\r\n\(\[{¿]|^)('.$regexp.')/u', function ($matches) {
global $globals;
switch ($matches[2][0]) {
case '_':
return $matches[1].'<em>'.substr($matches[2], 1, -1).'</em>';
case '*':
return $matches[1].'<strong>'.substr($matches[2], 1, -1).'</strong>';
case '-':
return $matches[1].'<del>'.substr($matches[2], 1, -1).'</del>';
}
return $matches[1].$matches[2];
}, $string);
}
function text_to_html(&$string)
{
$regexp = '/([\s\(\[{¡;,:¿]|^)((https{0,1}:\/\/)([^\s<>]{5,500}))/Smu';
return preg_replace_callback($regexp, 'text_to_html_callback', $string);
}
function text_to_html_callback(&$matches)
{
if ($matches[2][0] !== 'h') {
return $matches[1].$matches[2];
}
if (substr($matches[4], -1) === ')' && strrchr($matches[4], '(') === false) {
$matches[4] = substr($matches[4], 0, -1);
$suffix = ')';
} else {
$suffix = '';
}
$url = rawurldecode($matches[4]);
return $matches[1].'<a href="'.$matches[3].$url.'" title="'.$url.'" rel="nofollow">'.substr($url, 0, 70).'</a>'.$suffix;
}
function check_integer($which)
{
if (isset($_REQUEST[$which]) && is_numeric($_REQUEST[$which])) {
return intval($_REQUEST[$which]);
}
}
function get_comment_page_suffix($page_size, $order, $total = 0)
{
if (empty($page_size) || ($total && $total < $page_size)) {
return '';
}
return '/'.ceil($order / $page_size);
}
function get_current_page()
{
if (($var = check_integer('page')) && $var > 0) {
return $var;
}
return 1;
}
function get_date($time)
{
return date('d-m-Y', $time);
}
function get_date_time($time)
{
global $globals;
// Difference is less than 20 hours
if (abs($globals['now'] - $time) < 72000) {
return date('H:i T', $time);
}
return date('d-m-Y H:i T', $time);
}
function get_human_number($number)
{
if ($number < 100) {
if (strstr($number, '.')) {
return number_format($number, 2, ',', '.');
}
return $number;
}
$number = round($number);
if ($number < 1000) {
return $number;
}
if ($number < 10000) {
return number_format($number, 0, ',', '.');
}
return number_format(round($number / 1000), 0, ',', '.').'K';
}
function get_human_date($date, $format, $locale = 'es_ES.UTF-8')
{
$old = setlocale(LC_TIME, 0);
setlocale(LC_TIME, $locale);
$date = strftime($format, is_numeric($date) ? $date : strtotime($date));
setlocale(LC_TIME, $old);
return $date;
}
function get_server_name()
{
global $globals;
return empty($globals['server_name']) ? $_SERVER['SERVER_NAME'] : $globals['server_name'];
}
function get_static_server_name()
{
global $globals;
if (!empty($globals['static_server'])) {
return preg_replace('/^.*?\/\//', '', $globals['static_server']);
}
return get_server_name();
}
function get_auth_link()
{
global $globals;
if (!$globals['ssl_server']) {
return $globals['base_url_general'];
}
return 'https://'.$globals['ssl_server'].$globals['base_url_general'];
}
function check_auth_page()
{
global $globals;
if ($globals['https'] || !$globals['ssl_server'] || !$globals['secure_page']) {
return;
}
setcookie('return_site', $global['scheme'].'//'.get_server_name(), 0, $globals['base_url_general'], UserAuth::domain());
header('HTTP/1.1 302 Moved');
die(header('Location: https://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]));
}
function get_form_auth_ip()
{
global $globals, $site_key;
if (check_form_auth_ip()) {
// We reuse the values
$ip = $_REQUEST['userip'];
$scheme = $_REQUEST['userscheme'];
$control = $_REQUEST['useripcontrol'];
} else {
$ip = $globals['user_ip'];
$scheme = $globals['scheme'];
$control = sha1($ip.$site_key.base64_encode($ip.$site_key));
}
echo '<input type="hidden" name="userscheme" value="'.$scheme.'"/>';
echo '<input type="hidden" name="userip" value="'.$ip.'"/>';
echo '<input type="hidden" name="useripcontrol" value="'.$control.'"/>';
}
function check_form_auth_ip()
{
global $globals, $site_key;
if ($_REQUEST['userip'] && $_REQUEST['useripcontrol'] && sha1($_REQUEST['userip'].$site_key.base64_encode($_REQUEST['userip'].$site_key)) == $_REQUEST['useripcontrol']) {
$globals['form_scheme'] = $_REQUEST['userscheme'];
$globals['form_user_ip'] = $_REQUEST['userip'];
$globals['form_user_ip_int'] = inet_ptod($_REQUEST['userip']);
return true;
}
$globals['form_user_ip'] = $globals['user_ip'];
$globals['form_user_ip_int'] = $globals['user_ip_int'];
$globals['form_scheme'] = $globals['scheme'];
return false;
}
function get_user_uri($user, $view = '')
{
global $globals;
$uri = $globals['base_url_general'].'user/'.htmlspecialchars($user);
if ($view) {
$uri .= "/$view";
}
return $uri;
}
function get_user_uri_by_uid($user, $view = '')
{
$uid = guess_user_id($user);
// User does not exist, ensure it will give error later
if ($uid == 0) {
$uid = -1;
}
return get_user_uri($user, $view)."/$uid";
}
function post_get_base_url($option = '', $give_base = true)
{
global $globals;
return ($give_base ? $globals['base_url_general'] : '').'notame/'.$option;
}
function get_avatar_url($user, $avatar, $size, $fullurl = true)
{
global $globals, $db;
// If it does not get avatar status, check the database
if ($user > 0 && $avatar < 0) {
$avatar = (int) $db->get_var("select user_avatar from users where user_id = $user");
}
if ($avatar <= 0) {
return get_no_avatar_url($size, $fullurl);
}
if ($globals['Amazon_S3_media_url'] && !$globals['Amazon_S3_local_cache']) {
return $globals['Amazon_S3_media_url']."/avatars/$user-$avatar-$size.jpg";
}
if (!$globals['cache_dir']) {
return get_no_avatar_url($size, $fullurl);
}
$base = $fullurl ? $globals['base_static_noversion'] : $globals['base_url_general'];
$file = Upload::get_cache_relative_dir($user)."/$user-$avatar-$size.jpg";
if ($globals['cache_redirector']) {
return $base.$file;
}
if (is_readable(mnmpath.'/'.$file)) {
return $base.$file;
}
return $globals['base_url_general']."backend/get_avatar.php?id=$user&size=$size&time=$avatar";
}
function get_no_avatar_url($size, $fullurl = true)
{
global $globals;
$url = $globals['base_static'].'img/mnm/no-gravatar-2-'.$size.'.png';
return $fullurl ? $url : url_no_scheme($url);
}
function utf8_substr($str, $start)
{
preg_match_all("/./su", $str, $ar);
if (func_num_args() >= 3) {
return implode('', array_slice($ar[0], $start, func_get_arg(2)));
}
return implode('', array_slice($ar[0], $start));
}
// Simple unified key generator for use in GET requests
function get_security_key($time = false)
{
global $globals, $current_user, $site_key;
$time = $time ?: $globals['now'];
if ($current_user->user_id > 0) {
// For users of balanced connections and 3G we avoid using the IP
return $time.'-'.sha1($time.$current_user->user_id.$current_user->user_date.$site_key);
}
// We shift 8 bits to avoid key errors with mobiles/3G that change IP frequently
$ip_key = $globals['user_ip_int'] >> 8;
return $time.'-'.base64_encode($time.$ip_key); // Faster, not needed more complex for anoymous users
}
function check_security_key($key)
{
if (empty($key)) {
return false;
}
$time_key = explode('-', $key);
if (count($time_key) !== 2) {
return false;
}
global $globals;
if ($globals['now'] - intval($time_key[0]) > 14400) {
return false;
}
return ($key === get_security_key($time_key[0]));
}
function do_error($mess = false, $error = false, $send_status = 'Error')
{
global $globals;
$globals['ads'] = false;
if (headers_sent($file, $line)) {
syslog(LOG_INFO, "Headers already sent, file $file line $line, uri: ".$_SERVER["DOCUMENT_URI"]." mess: $mess");
}
$mess = $mess ?: _('algún error nos ha petado');
if ($error) {
@header("HTTP/1.0 $error $mess");
@header("Status: $error $mess");
}
Haanga::Load('error.html', compact('mess', 'error'));
die;
}
function not_found($mess = '')
{
do_error($mess, 404, 'Not found');
}
function get_uppercase_ratio($str)
{
$str = trim(htmlspecialchars_decode($str));
$len = mb_strlen($str);
$uppers = preg_match_all('/[A-Z]/', $str, $matches);
if ($uppers > 0 && $len > 0) {
return $uppers / $len;
}
return 0;
}
function do_modified_headers($time, $tag)
{
header('Last-Modified: '.date('r', $time));
header('ETag: "'.$tag.'"');
}
// Use this function to normalize headers to capital first letter
// Apache converts X-Something to x-something
function request_headers()
{
$headers = array();
foreach ($_SERVER as $key => $value) {
if (substr($key, 0, 5) !== 'HTTP_') {
continue;
}
$headername = strtr(ucwords(strtolower(strtr(substr($key, 5), '_', ' '))), ' ', '-');
$headers[$headername] = $value;
}
return $headers;
}
if (!function_exists('apache_request_headers')) {
function apache_request_headers()
{
return request_headers();
}
}
function get_if_modified()
{
// Get client headers - Apache only
$request = apache_request_headers();
if (empty($request['If-Modified-Since'])) {
return 0;
}
// Split the If-Modified-Since (Netscape < v6 gets this wrong)
$modifiedSince = explode(';', $request['If-Modified-Since']);
return strtotime($modifiedSince[0]);
}
function guess_user_id($str)
{
global $db;
if (preg_match('/^[0-9]+$/', $str)) {
// It's a number, return it as id
return intval($str);
}
$str = $db->escape(mb_substr($str, 0, 64));
return (int) $db->get_var('SELECT user_id FROM users WHERE user_login = "'.$str.'" LIMIT 1;');
}
function put_smileys($str)
{
global $globals;
if ($globals['bot']) {
return $str;
}
return preg_replace_callback('/\{(\S{3,14})\}/', 'put_emojis_callback', $str);
}
function put_emojis_callback($matches)
{
global $globals;
static $translations = false;
if ($translations === false) {
$translations = array(
'angry' => 'angry.png" alt=">:-(" title=">:-(" width="18" height="18"',
'blank' => 'blank.png" alt=":-|" title=":-| :|" width="18" height="18"',
'cheesy' => 'cheesy.png" alt=":->" title=":->" width="18" height="18"',
'confused' => 'confused.png" alt=":-S" title=":-S :S" width="18" height="18"',
'cool' => 'cool.png" alt="8-D" title=":cool: 8-D" width="18" height="18"',
'cry' => 'cry.gif" alt=":\'(" title=":cry: :\'(" width="18" height="18"',
'ffu' => 'ffu.png" alt=":ffu:" title=":ffu:" width="23" height="18"',
'goatse' => 'goatse.png" alt=":goatse:" title=":goatse:" width="18" height="18"',
'grin' => 'grin.png" alt=":-D" title=":-D" width="18" height="18"',
'hug' => 'hug.png" alt=":hug:" title=":hug:" width="35" height="18"',
'huh' => 'huh.png" alt="?(" title="?(" width="16" height="21"',
'kiss' => 'kiss.gif" alt=":-*" title=":-* :*" width="18" height="18"',
'lipssealed' => 'lipssealed.png" alt=":-x" title=":-x" width="18" height="18"',
'lol' => 'lol.gif" alt="xD" title=":lol: xD" width="18" height="18"',
'oops' => 'oops.png" alt="<:(" title=":oops: <:(" width="18" height="18"',
'palm' => 'palm.png" alt=":palm:" title=":palm:" width="18" height="18"',
'roll' => 'roll.gif" alt=":roll:" title=":roll:" width="18" height="18"',
'sad' => 'sad.png" alt=":-(" title=":-(" width="18" height="18"',
'shame' => 'shame.png" alt="¬¬" title="¬¬ :shame:" width="18" height="18"',
'shit' => 'shit.png" alt=":shit:" title=":shit:" width="18" height="18"',
'shocked' => 'shocked.gif" alt=":-O" title=":-O" width="18" height="18"',
'smiley' => 'smiley.png" alt=":-)" title=":-)" width="18" height="18"',
'tongue' => 'tongue.png" alt=":-P" title=":-P" width="18" height="18"',
'troll' => 'troll.png" alt=":troll:" title=":troll:" width="18" height="18"',
'undecided' => 'undecided.png" alt=":-/" title=":-/ :/" width="18" height="18"',
'wall' => 'wall.gif" alt=":wall:" title=":wall:" width="24" height="18"',
'wink' => 'wink.png" alt=";)" title=";)" width="18" height="18"',
'wow' => 'wow.png" alt="o_o" title="o_o :wow:" width="18" height="18"',
'coletas' => 'coletas.png" alt=":coletas:" title=":coletas:" width="18" height="18"',
'eli' => 'eli.png" alt=":eli:" title=":eli:" width="18" height="18"',
'foreveralone' => 'foreveralone.png" alt=":foreveralone:" title=":foreveralone:" width="20" height="18"',
'pagafantas' => 'pagafantas.png" alt=":pagafantas:" title=":pagafantas:" width="25" height="18"',
'popcorn' => 'popcorn.gif" alt=":popcorn:" title=":popcorn:" width="29" height="18"',
'take' => 'takemymoney.png" alt=":take:" title=":take:" width="29" height="18"',
'professor' => 'professor.png" alt=":professor:" title=":professor:" width="18" height="24"',
'peineta' => 'peineta.png" alt=":peineta:" title=":peineta:" width="23" height="18"',
'ferrari' => 'ferrari.png" alt=":ferrari:" title=":ferrari:" width="36" height="18"',
'calzador' => 'calzador.png" alt=":calzador:" title=":calzador:" width="18" height="18"',
'tinfoil' => 'tinfoil.gif" alt=":tinfoil:" title=":tinfoil:" width="18" height="26"',
'clap' => 'clap.gif" alt=":clap:" title=":clap:" width="32" height="18"',
);
}
if (substr($matches[1], 0, 2) === '0x') {
// Twemoji
$image = substr($matches[1], 2).'.png';
return '<img data-src="'.$globals['base_static'].'img/twemojis/36/'.$image.'" alt="{'.$matches[1].'}" title="{'.$matches[1].'}" width="18" height="18" src="'.$globals['base_static'].'img/g.gif" class="emoji lazy" />';
}
if (isset($translations[$matches[1]])) {
return '<img data-src="'.$globals['base_static'].'img/menemojis/36/'.$translations[$matches[1]].' src="'.$globals['base_static'].'img/g.gif" class="emoji lazy" />';
}
return $matches[0];
}
function normalize_smileys($str)
{
global $globals;
require_once mnminclude.'twemojis.php';
$str = Twemojis::normalize($str);
$str = preg_replace('/(\s|^):wall:/i', '$1{wall}', $str);
$str = preg_replace('/(\s|^):troll:/i', '$1{troll}', $str);
$str = preg_replace('/(\s|^):ffu:/i', '$1{ffu}', $str);
$str = preg_replace('/(\s|^):palm:/i', '$1{palm}', $str);
$str = preg_replace('/(\s|^):goatse:/i', '$1{goatse}', $str);
$str = preg_replace('/(\s|^)o_o|:wow:/i', '$1{wow}', $str);
$str = preg_replace('/(\s|^)¬¬|:shame:/i', '$1{shame}', $str);
$str = preg_replace('/(\s|^):-{0,1}\)(\s|$)/i', '$1{smiley}$2', $str);
$str = preg_replace('/(\s|^);-{0,1}\)(\s|$)/i', '$1{wink}$2', $str);
$str = preg_replace('/(\s|^):-{0,1}>/i', '$1{cheesy}', $str);
$str = preg_replace('/(\s|^)(:-{0,1}D|:grin:)/i', '$1{grin}', $str);
$str = preg_replace('/(\s|^)(:oops:|<:\()/i', '$1{oops}', $str);
$str = preg_replace('/(\s|^)>:-{0,1}\((\s|$)/i', '$1{angry}$2', $str);
$str = preg_replace('/(\s|^)\?(:-){0,1}\((\s|$)/i', '$1{huh}$2', $str);
$str = preg_replace('/(\s|^):-{0,1}\((\s|$)/i', '$1{sad}$2', $str);
$str = preg_replace('/(\s|^):-{0,1}O/', '$1{shocked}', $str);
$str = preg_replace('/(\s|^)(8-{0,1}[D\)]|:cool:)/', '$1{cool}', $str);
$str = preg_replace('/(\s|^):roll:/i', '$1{roll}', $str);
$str = preg_replace('/(\s|^):-{0,1}P(\s|$)/i', '$1{tongue}$2', $str);
$str = preg_replace('/(\s|^):-{0,1}x/i', '$1{lipssealed}', $str);
$str = preg_replace('/(\s|^):-{0,1}\//i', '$1{undecided}', $str);
$str = preg_replace('/(\s|^)(:\'\(|:cry:)/i', '$1{cry}', $str);
$str = preg_replace('/(\s|^)(x-{0,1}D+|:lol:)/i', '$1{lol}', $str);
$str = preg_replace('/(\s|^):-{0,1}S(\s|$)/i', '$1{confused}$2', $str);
$str = preg_replace('/(\s|^):-{0,1}\|/i', '$1{blank}', $str);
$str = preg_replace('/(\s|^):-{0,1}\*/i', '$1{kiss}', $str);
$str = preg_replace('/(\s|^):hug:/i', '$1{hug}', $str);
$str = preg_replace('/(\s|^):shit:/i', '$1{shit}', $str);