-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.functions.php
More file actions
2555 lines (2405 loc) · 84.1 KB
/
Copy path7.functions.php
File metadata and controls
2555 lines (2405 loc) · 84.1 KB
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
/**
* Takes off diacritics and empty spaces from a string, if $tofile is <tt>FALSE</tt> (default) the case is changed to lowercase.
*
* @param string $S String to be formatted.
* @param bool $tofile Sets whether it will be used for a filename or not, <tt>FALSE</tt> is the default value.
* @param string $separador Separator used to replace empty spaces.
*
* @return string Formatted string.
*
* @version (2006/01/18)
*/
if (!function_exists('toId')) {
function toId($string, $tofile = false, $separador = '')
{
// Check if there are diacritics before replacing them
if (preg_match('/[^a-zA-Z0-9-\/ _.,]/', $string)) {
$string = preg_replace('/[áàãâäÁÀÃÂĪ]/u', 'a', $string);
$string = preg_replace('/[éèêëÉÈÊË&]/u', 'e', $string);
$string = preg_replace('/[íìîïÍÌÎÏ]/u', 'i', $string);
$string = preg_replace('/[óòõôöÓÒÕÔÖº]/u', 'o', $string);
$string = preg_replace('/[úùûüÚÙÛÜ]/u', 'u', $string);
$string = preg_replace('/[çÇ]/u', 'c', $string);
$string = preg_replace('/[ñÑ]/u', 'n', $string);
}
if ($tofile) {
$string = preg_replace('/[^a-zA-Z0-9_]/u', '_', $string);
} else {
$string = preg_replace('/[^a-zA-Z0-9_]+/u', $separador, $string);
$string = trim(mb_strtolower($string), $separador);
}
if ($separador) {
$string = str_replace('_', $separador, $string);
} else {
$string = preg_replace('/[\/-]/u', '_', $string);
}
return $string;
}
}
/**
* Takes off diacritics from a string and replace special characters and empty spaces by '-'.
*
* @param string $S String to be formatted.
*
* @return string Formatted string.
*
* @author JP
*
* @version (2008/06/12) update by Carlos Rodrigues
*/
function toSeo($string)
{
return toId($string, false, '-');
}
/**
* Alias for toSeo().
*/
function toSlug($string)
{
return toSeo($string);
}
/**
* Generates a SQL WHERE statement with REGEXP for 'decoding' the toSeo() function.
*
* @param string $field Field where the data will be searched, e.g. varchar_key.
* @param string $str String to be formatted and searched.
* @param string $regexp Optional REGEXP string, the default value is '[^\d\w]?'.
*
* @return string Formatted SQL WHERE statement with a REGEXP.
*
* @author Carlos Rodrigues
*
* @version (2008/06/12)
* @deprecated
*/
function toSeoSearch($field, $str, $regexp = '[^[:alnum:]]*')
{
$sql_where = $regexp;
for ($i = 0; $i < mb_strlen($str); $i++) {
$char = $str[$i];
$char = str_replace('a', '[aáàãâäª]', $char);
$char = str_replace('e', '[eéèêë&]', $char);
$char = str_replace('i', '[iíìîï]', $char);
$char = str_replace('o', '[oóòõôöº]', $char);
$char = str_replace('u', '[uúùûü]', $char);
$char = str_replace('c', '[cç]', $char);
$char = str_replace('n', '[nñ]', $char);
$sql_where .= $char.$regexp;
}
return 'REPLACE('.$field.",' ','') REGEXP '^".$sql_where."$'";
}
/**
* @deprecated
*/
function wap_toHTML($S)
{
return Jp7_Deprecated::wap_toHTML($S);
}
/**
* Quotes a string to be sent to the database. e.g. mysql becomes 'mysql'.
*
* @param string $S The input string.
* @param bool $force_magic_quotes_gpc If TRUE the string will be quoted even if 'magic_quotes_gpc' is not active.
*
* @global ADOConnection
*
* @return string Quoted string.
*
* @version (2003/08/25)
* @deprecated
*/
function toBase($S, $force_magic_quotes_gpc = false)
{
global $db;
if (mb_strlen($S)) {
$S = $db->qstr($S, !$force_magic_quotes_gpc); //trata as aspas. Ex.: 'mysql' fica \'mysql\'
$S = trim($S);
} else {
$S = "''";
}
return $S;
}
/**
* Replaces double and single quotes so they can be used inside an HTML element's attribute. e.g. \'test\' becomes 'test'.
*
* @param string $S String to be formatted.
*
* @return string Formatted string.
*
* @version (2004/06/14)
* @deprecated
*/
function toForm($S)
{
$S = str_replace("\'", ''', $S);// Bug LocaWeb e JavaScript
$S = str_replace('\"', '"', $S);// Bug LocaWeb
return stripslashes(str_replace('"', '"', $S));
}
/**
* Formats an string to be used as HTML text, strips slashes and replaces values.
*
* @param string $S String to be formatted.
* @param bool $HTML If <tt>FALSE</tt> (default) the line breaks are replaced by <br />
* @param bool $busca_replace If <tt>TRUE</tt> the function uses the regex string ($busca_varchar or $busca_text, passed by globals) to replace values. <tt>FALSE</tt> is the default value.
*
* @global string
* @global string
*
* @return string Formatted string.
*
* @version (2004/06/14)
* @deprecated
*/
function toHTML($S, $HTML = false, $busca_replace = false)
{
global $busca_varchar, $busca_text;
$busca = ($busca_varchar) ? $busca_varchar : $busca_text;
if (mb_strlen($S)) {
if (!$HTML) {
$S = str_replace(chr(13), ' <br /> ', $S);
}
//elseif(strpos(mb_strtolower($S),"<p>")===false)$S="<p>".$S."</p>";
$S = str_replace("\'", "'", $S);// Bug LocaWeb
$S = str_replace("''", "'", $S);// Bug LocaWeb
$S = str_replace('\"', '"', $S);// Bug LocaWeb
if ($busca_replace && $busca) {
$S = preg_replace("/[^@\.]".$busca."[^@\.]/i", ' <span class="font-search">'.mb_strtoupper($busca).'</span> ', $S);
}
return stripslashes($S);
}
}
/**
* Formats a string to be used inside a javascript. Replaces \" by " and ' by \'.
*
* @param string $S String to be formatted.
*
* @return string Formatted string.
*
* @version (2004/05/31)
* @deprecated
*/
function toScript($S)
{
$S = str_replace("\r", '\r', $S);
$S = str_replace("\n", '\n', $S);
$S = str_replace('"', '"', $S);
$S = str_replace("'", "\'", $S);
return $S;
}
/**
* Formats a string to be used inside a parameter. Replaces \" by " and line breaks by empty spaces (" ").
*
* @param string $S String to be formatted.
*
* @return string Formatted string.
*
* @version (2007/06/25)
*
* @author JP
*/
function toParam($S)
{
$S = str_replace('"', '"', $S);
$S = str_replace("\n", ' ', $S);
$S = str_replace("\r", ' ', $S);
$S = str_replace(chr(13), ' ', $S);
$S = str_replace(chr(11), ' ', $S);
return $S;
}
/**
* Formats a string to be used inside a XML.
*
* @param string $S String to be formatted.
*
* @return string Formatted string.
*
* @version (2008/12/05)
*
* @author JP7
*/
function toXml($S)
{
return str_replace(['&', '"', "'", '<', '>', '’'], ['&', '"', ''', '<', '>', '''], $S);
}
/*
* Converts Hex string into binary string.
*
* @param string $S String to be converted.
* @return string Binary string.
* @version (2007/01/22)
* @author JP
*/
if (!function_exists('hex2bin')) {
function hex2bin($S)
{
return pack('H'.mb_strlen($S), $S);
}
}
/**
* Encrypts a string using a key.
*
* @param string $S String that will be encrypted.
* @param string $key Key with which the data will be encrypted, the key will be required to decrypt it as well, the default value is the md5 hash of $_SERVER["HTTP_HOST"].
* @param string $cipher One of the MCRYPT_ciphername constants of the name of the algorithm, the default value is <tt>MCRYPT_RIJNDAEL_128</tt>.
* @param string $mode One of the MCRYPT_MODE_modename constants, the default value is <tt>MCRYPT_MODE_ECB</tt>.
*
* @return string Encrypted string.
*
* @version (2007/04/19)
*
* @author JP
* @deprecated
*/
function jp7_encrypt($S, $key = '', $cipher = 'AES-256-CBC', $options = false)
{
if (!$key) {
$key = 9415616219865148;
}
$iv = 1986514894156162;
return openssl_encrypt($S, $cipher, $key, $options, $iv);
}
/**
* Decrypts a string using a key.
*
* @param string $S Encrypted string.
* @param string $key Key with which the data was encrypted, the default value is the md5 hash of $_SERVER["HTTP_HOST"].
* @param string $cipher One of the MCRYPT_ciphername constants of the name of the algorithm, the default value is <tt>MCRYPT_RIJNDAEL_128</tt>.
* @param string $mode One of the MCRYPT_MODE_modename constants, the default value is <tt>MCRYPT_MODE_ECB</tt>.
*
* @return string Decrypted string.
*
* @version (2007/04/19)
*
* @author JP
* @deprecated
*/
function jp7_decrypt($S, $key = '', $cipher = 'AES-256-CBC', $options = false)
{
if (!$key) {
$key = 9415616219865148;
}
$iv = 1986514894156162;
return openssl_decrypt($S, $cipher, $key, $options, $iv);
}
function toXHTML($S)
{
return Jp7_Deprecated::toXHTML($S);
}
/**
* Checks if the referer page is the same as it was expected to be.
*
* @param string $S Expected referer page's URL.
* @param string $protocol Protocol used, the default value is "http".
*
* @return bool <tt>TRUE</tt> if the referer is the expected page, <tt>FALSE</tt> if not.
*
* @version (2008/05/19)
*/
function checkReferer($S, $protocol = 'http')
{
/*
while(strpos($S,"../")!==false){
}
*/
if (!dirname($S) || dirname($S) == '.') {
$parent_dirname = dirname(dirname($_SERVER['REQUEST_URI']));
if ($parent_dirname == '/') {
$parent_dirname = '';
}
$dirname = dirname($_SERVER['REQUEST_URI']);
if ($dirname == '/') {
$dirname = '';
}
$S_parent = $protocol.'://'.$_SERVER['HTTP_HOST'].$parent_dirname.'/'.$S;
$S = $protocol.'://'.$_SERVER['HTTP_HOST'].$dirname.'/'.$S;
}
return strpos($_SERVER['HTTP_REFERER'], $S) === 0 ||
strpos($_SERVER['HTTP_REFERER'], $S_parent) === 0 ||
strpos($_SERVER['HTTP_REFERER'], replace_prefix($protocol.'://', 'https://', $S)) === 0 ||
strpos($_SERVER['HTTP_REFERER'], replace_prefix($protocol.'://', 'https://', $S_parent)) === 0;
}
/**
* Shrinks the input string and adds "..." if it is larger than the maximum length, the input string is not changed if its shorter.
*
* @param string $S Input string.
* @param int $length Max. lenght of the output string.
*
* @return string Shrunk string.
*
* @version (2008/07/04)
*
* @global string
* @global string
*/
function jp7_string_left($S, $length)
{
global $s_session, $c_lang;
if ($c_lang) {
foreach ($c_lang as $item) {
if ($item[0] == $s_session['lang'] && $item[2]) {
$length = $length * 8;
} // Check if language uses entities for characters (eg.: japanese)
}
}
return (mb_strlen($S) > $length) ? mb_substr($S, 0, $length).'...' : $S;
}
/**
* Truncates text.
*
* Cuts a string to the length of $length and replaces the last characters
* with the ending if the text is longer than length.
*
* @param string $text String to truncate.
* @param int $length Length of returned string, including ellipsis.
* @param string $ending Ending to be appended to the trimmed string.
* @param bool $exact If false, $text will not be cut mid-word
* @param bool $considerHtml If true, HTML tags would be handled correctly
*
* @return string Trimmed string.
*/
function jp7_truncate($text, $length = 100, $considerHtml = true, $ending = '...', $exact = true)
{
if ($considerHtml) {
// if the plain text is shorter than the maximum length, return the whole text
if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
return $text;
}
// splits all html-tags to scanable lines
preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER);
$total_length = mb_strlen($ending);
$open_tags = [];
$truncate = '';
foreach ($lines as $line_matchings) {
// if there is any html-tag in this line, handle it and add it (uncounted) to the output
if (!empty($line_matchings[1])) {
// if it's an "empty element" with or without xhtml-conform closing slash (f.e. <br/>)
if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) {
// do nothing
// if tag is a closing tag (f.e. </b>)
} elseif (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) {
// delete tag from $open_tags list
$pos = array_search($tag_matchings[1], $open_tags);
if ($pos !== false) {
unset($open_tags[$pos]);
}
// if tag is an opening tag (f.e. <b>)
} elseif (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) {
// add tag to the beginning of $open_tags list
array_unshift($open_tags, mb_strtolower($tag_matchings[1]));
}
// add html-tag to $truncate'd text
$truncate .= $line_matchings[1];
}
// calculate the length of the plain text part of the line; handle entities as one character
$content_length = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $line_matchings[2]));
if ($total_length + $content_length > $length) {
// the number of characters which are left
$left = $length - $total_length;
$entities_length = 0;
// search for html entities
if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) {
// calculate the real length of all entities in the legal range
foreach ($entities[0] as $entity) {
if ($entity[1] + 1 - $entities_length <= $left) {
$left--;
$entities_length += mb_strlen($entity[0]);
} else {
// no more characters left
break;
}
}
}
$truncate .= mb_substr($line_matchings[2], 0, $left + $entities_length);
// maximum lenght is reached, so get off the loop
break;
} else {
$truncate .= $line_matchings[2];
$total_length += $content_length;
}
// if the maximum length is reached, get off the loop
if ($total_length >= $length) {
break;
}
}
} else {
if (mb_strlen($text) <= $length) {
return $text;
} else {
$truncate = mb_substr($text, 0, $length - mb_strlen($ending));
}
}
// if the words shouldn't be cut in the middle...
if (!$exact) {
// ...search the last occurance of a space...
$spacepos = strrpos($truncate, ' ');
if (isset($spacepos)) {
// ...and cut the text in this position
$truncate = mb_substr($truncate, 0, $spacepos);
}
}
// add the defined ending to the text
$truncate .= $ending;
if ($considerHtml) {
// close all unclosed html-tags
foreach ($open_tags as $tag) {
$truncate .= '</'.$tag.'>';
}
}
return $truncate;
}
/**
* Sets global variables using values from superglobals if "register_globals" is OFF, emulating this feature.
*
* @global string
*
* @todo Check if this function could be flagged as "deprecated".
*
* @version (2007/03/03)
* @deprecated
*/
function jp7_register_globals()
{
global $HTTP_HOST;
if (!@ini_get('register_globals') || !$HTTP_HOST) {
if (!empty($_SERVER)) {
foreach ($_SERVER as $key => $value) {
$GLOBALS[$key] = $value;
}
}
if (!empty($_GET)) {
foreach ($_GET as $key => $value) {
$GLOBALS[$key] = $value;
}
}
if (!empty($_POST)) {
foreach ($_POST as $key => $value) {
$GLOBALS[$key] = $value;
}
}
if (!empty($_COOKIE)) {
foreach ($_COOKIE as $key => $value) {
$GLOBALS[$key] = $value;
}
}
if (!empty($_SESSION)) {
foreach ($_SESSION as $key => $value) {
$GLOBALS[$key] = $value;
}
}
}
}
/**
* Creates an alphanumeric password (a-z, 0-9).
*
* @param string $length Length of the created password, the default value is 6.
*
* @return string Created password.
*
* @version (2008/09/25)
*
* @author JP
* @deprecated
*/
function jp7_password($length = 6)
{
$chars = 'abcdefghijkmnopqrstuvwxyz023456789';
$S = '';
for ($i = 0; $i < $length; $i++) {
$S .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $S;
}
/**
* Formats and prints the elements of an array or object, using the print_r() function and adding the "pre" tag around it.
*
* @param mixed $var Array or object that will have its elements printed.
* @param bool $return If <tt>TRUE</tt> the formatted string is returned, otherwise its printed, default value is <tt>FALSE</tt>.
* @param bool $hideProtectedVars If <tt>TRUE</tt> the print_r will not show protected properties of an object. This feature is not recursive.
* @param string @varPrefix If <tt>TRUE</tt> it will only print the keys starting by this prefix. Is is useful when printing large arrays, like $GLOBALS.
*
* @return string|NULL Formatted string or <tt>NULL</tt>.
*
* @version (2008/02/06)
*
* @author JP
*/
function jp7_print_r($var, $return = false, $hideProtectedVars = false, $varPrefix = '')
{
return Jp7_Deprecated::jp7_print_r($var, $return, $hideProtectedVars, $varPrefix);
}
/**
* Splits a time/date into an array.
*
* @param string $date String containing a date/time on the format Y-m-d H:i:s or Y/m/d H:i:s.
*
* @return array Array containing the following keys: Y, m, M, d, H, i, s and y.
*
* @version (2008/05/27)
*/
function jp7_date_split($date)
{
$date = str_replace(' ', ',', $date);
$date = str_replace('/', ',', $date);
$date = str_replace('-', ',', $date);
$date = str_replace(':', ',', $date);
$date = explode(',', $date);
return [
'Y' => $date[0],
'm' => $date[1],
'M' => jp7_date_month($date[1], true),
'F' => jp7_date_month($date[1]),
'd' => $date[2],
'H' => $date[3],
'i' => $date[4],
's' => $date[5],
'y' => mb_substr($date[0], 2),
];
}
/**
* Returns date formatted according to given format.
*
* @param string $date Date/time string.
* @param string $format Format using: "Y", "m", "M", "d", "H", "i", "s" or "y". The default value is "d/m/Y", when english language is active the "d/m" is automatically replaced by "m/d".
*
* @global string
*
* @return string|NULL Returns formatted date or <tt>NULL</tt> if no date is given.
*
* @version (2010/02/08)
*/
function jp7_date_format($date, $format = 'd/m/Y')
{
global $jp7_app;
if ($jp7_app) {
$lang = new jp7_lang('pt-br', true);
} else {
global $lang;
}
if ($date instanceof Jp7_Date) {
$date = $date->format('Y-m-d H:m:i');
}
if ($date) {
if ($lang->lang == 'en') {
$format = str_replace('d/m', 'm/d', $format);
$format = str_replace('d-m', 'm-d', $format);
}
$date = jp7_date_split($date);
$S = '';
for ($i = 0;$i < mb_strlen($format);$i++) {
$x = mb_substr($format, $i, 1);
$S .= isset($date[$x]) ? $date[$x] : $x;
}
return $S;
}
}
/**
* Returns textual representation for the day of the week, such as Sunday or Saturday. Supports english and portuguese.
*
* @param int|string $w A numeric representation of the day of the week (0 for Sunday through 6 for Saturday), or a date/time string.
* @param string $sigla If <tt>TRUE</tt> returns only the first three letters, the default value is <tt>FALSE</tt>.
*
* @global string
*
* @return string Textual representation for the day of the week.
*
* @version (2006/04/27)
*/
function jp7_date_week($w, $sigla = false)
{
global $lang;
switch ($lang->lang) {
case 'en': $W = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; break;
case 'de': $W = ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag']; break;
case 'es': $W = ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado']; break;
default: $W = ['Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado']; break;
}
if (!is_int($w)) {
$w = date('w', strtotime($w));
}
$return = $W[$w];
return ($sigla) ? mb_substr($return, 0, 3) : $return;
}
/**
* Returns textual representation of a month, such as January or March. Supports english and portuguese.
*
* @param int $m Numeric representation of a month, (1 for January through 12 for December).
* @param string $sigla If <tt>TRUE</tt> returns only the first three letters, the default value is <tt>FALSE</tt>.
*
* @global string
*
* @return string Textual representation of a month.
*
* @version (2004/06/14)
*/
function jp7_date_month($m, $sigla = false)
{
global $lang;
switch ($lang->lang) {
case 'en':
$M = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
break;
case 'de':
$M = ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'];
break;
case 'es':
$M = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
break;
default:
$M = ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'];
break;
}
if (isset($M[$m - 1])) {
$return = $M[$m - 1];
} else {
$return = '';
}
return ($sigla) ? mb_substr($return, 0, 3) : $return;
}
/**
* Calculates the number of months from the start date to the end date.
*
* @param string $start Start date, string on the "Y-m-d" format.
* @param string $end End date, string on the "Y-m-d" format.
*
* @return int Number of months.
*
* @author Paulo
*
* @todo Add functionality to return days and years too, and to take off the time aggregated with a date, like: 2008-10-08 00:01:02.
*
* @version (2008/04/15)
*/
function jp7_date_diff($start, $end)
{
$start = explode('-', $start);
$start = mktime(0, 0, 0, $start[1], $start[2], $start[0]); // mes / dia / ano (padrao mktime)
$end = explode('-', $end);
$end = mktime(0, 0, 0, $end[1], $end[2], $end[0]); // mes / dia / ano (padrao mktime)
$diff = ($end - $start);
$diff = explode('-', date('Y-m-d', $diff));
$diff_r['m'] = $diff[1];
return $diff_r['m'];
}
/**
* Splits a telephone number into "ddd", "numero" and "ramal".
*
* @param string $tel String containing a telephone number.
*
* @return array Array containing "ddd", "numero" and "ramal".
*
* @todo Add support for poorly formatted telephones like: "-Ramal:", " R:", " - R:", maybe taking off empty spaces and "-".
*
* @version (2004/08/12)
*/
function jp7_tel_split($tel)
{
$tel = str_replace('(', '', $tel);
$tel = str_replace(')', ',', $tel);
$tel = str_replace(' - Ramal: ', ',', $tel);
$tel = explode(',', $tel);
return [
ddd => trim($tel[0]),
numero => trim($tel[1]),
ramal => trim($tel[2]),
];
}
function jp7_db_select($table, $table_id_name, $table_id_value, $var_prefix = '', $returnValues = false)
{
return Jp7_Deprecated::jp7_db_select($table, $table_id_name, $table_id_value, $var_prefix, $returnValues);
}
function jp7_db_insert($table, $table_id_name, $table_id_value = 0, $var_prefix = '', $var_check = true, $force_magic_quotes_gpc = false)
{
return Jp7_Deprecated::jp7_db_insert($table, $table_id_name, $table_id_value, $var_prefix, $var_check, $force_magic_quotes_gpc);
}
/**
* Creates a checkbox and a hidden field, the hidden field will have a value or not depending on whether the checkbox is checked or not.
*
* @param string $name Name of the hidden field.
* @param string $value Value that the hidden field will have if the checkbox is checked, the default value is "S".
* @param string $var Name of global variable containing the current value for the hidden field, the default value is "".
* @param string $readonly Readonly parameter to be inserted on the checkbox. e.g. readonly="readonly"
* @param string $xtra Additional HTML parameter to be inserted on the checkbox.
*
* @return string If $GLOBALS["interadmin_visualizar"] is set it returns "Sim" or "Não", otherwise it returns the created HTML for checkbox and hidden field.
*
* @todo Make $readonly a boolean, setting if the field is readonly or not. Check if its better to replace $GLOBALS["interadmin_visualizar"] by global $interadmin_visualizar.
*
* @author JP
*
* @version (2007/07/13)
*/
function jp7_db_checkbox($name, $value = 'S', $var = '', $readonly = '', $xtra = '', $var_value = null)
{
if (!$var) {
$var = $name;
}
if (is_null($var_value)) {
$var_value = $GLOBALS[$var];
}
if ($GLOBALS['interadmin_visualizar']) {
return (($var_value) ? 'Sim' : 'Não');
} else {
return ''.
'<input type="checkbox" name="jp7_db_checkbox_'.$name.'" id="jp7_db_checkbox_'.$name.'" value="'.$value.'"'.(($var_value) ? ' checked="checked"' : '').$readonly." onclick=\"form['".$name."'].value=(checked)?value:''\"".(($xtra) ? ' '.$xtra : '').' />'.
'<input type="hidden" name="'.$name.'" value="'.(($var_value) ? $value : '').'" />';
}
}
function jp7_db_update($table, $table_id_name, $table_id_value, $fields)
{
return Jp7_Deprecated::jp7_db_update($table, $table_id_name, $table_id_value, $fields);
}
/**
* Creates an array from a given list of fields using Interadmin's format.
*
* @param string $campos String containing the fields of a type, fields separated by {;}, parameters separated by {,}.
*
* @return array Array of fields with its parameters.
*
* @author JP
*
* @version (2007/03/10)
*/
function interadmin_tipos_campos($campos)
{
$A = [];
$campos_parameters = ['tipo', 'nome', 'ajuda', 'tamanho', 'obrigatorio', 'separador', 'xtra', 'lista', 'orderby', 'combo', 'readonly', 'form', 'label', 'permissoes', 'default', 'nome_id'];
$campos = explode('{;}', $campos);
for ($i = 0; $i < count($campos); $i++) {
$parameters = explode('{,}', $campos[$i]);
if ($parameters[0]) {
$A[$parameters[0]]['ordem'] = ($i + 1);
for ($j = 0; $j < count($parameters); $j++) {
$A[$parameters[0]][$campos_parameters[$j]] = $parameters[$j];
}
}
}
return $A;
}
/**
* Transforma array de campos em string separada por ; e {,} no formato do InterAdmin.
*
* @param array $campos
*
* @return string
*/
if (!function_exists('interadmin_tipos_campos_encode')) {
function interadmin_tipos_campos_encode($campos)
{
$s = '';
foreach ($campos as $key => $value) {
unset($value['ordem']);
$s .= implode('{,}', $value).'{;}';
}
return $s;
}
}
/**
* Gets an array containing "nome" and "xtra" values of a field on Interadmin.
*
* @param string $db_prefix Prefix of the table.
* @param string $id_tipo ID of the type which will be searched (column "id_tipo").
* @param string $var_key Name of the field from this type that will be got. e.g. "varchar_key".
*
* @global ADOConnection
* @global string
* @global int
*
* @return array Array containing "nome" and "xtra" values of the field.
*
* @version (2004/11/03)
*/
function interadmin_tipos_campo($db_prefix, $id_tipo, $var_key)
{
global $db, $tipo_campos, $tipo_model_id_tipo;
$tipo_model_id_tipo = $id_tipo;
while ($tipo_model_id_tipo) {
jp7_db_select($db_prefix.'_tipos', 'id_tipo', $tipo_model_id_tipo, 'tipo_');
}
$tipo_campos = explode('{;}', $tipo_campos);
foreach ($tipo_campos as $campo) {
$campo = explode('{,}', $campo);
if ($campo[0] == $var_key) {
return [
'nome' => $campo[1],
'xtra' => $campo[6],
];
break;
}
}
}
/**
* Alias for interadmin_query().
*
* @deprecated
* @see interadmin_query()
*
* @author JP
*
* @version (2007/04/25)
*/
function interadmin_mysql_query($sql, $sql_db = '', $sql_debug = false)
{
return interadmin_query($sql, $sql_db, $sql_debug);
}
function interadmin_query($sql, $sql_db = '', $sql_debug = false, $numrows = null, $offset = null)
{
return Jp7_Deprecated::interadmin_query($sql, $sql_db, $sql_debug, $numrows, $offset);
}
/**
* Gets the name of a type from its ID.
*
* @param int $id_tipo ID of the type.
* @param bool $nolang If <tt>TRUE</tt> it will return the name regardless of the current language, the default value is <tt>FALSE</tt>.
*
* @return string|bool If $id_tipo is numeric it is returned the name of the type, if it evaluates as <tt>FALSE</tt> it is returned <tt>FALSE</tt>, otherwise it is returned "Tipos".
*
* @author JP
*
* @version (2008/01/09)
*/
function interadmin_tipos_nome($id_tipo, $nolang = false)
{
if (!$id_tipo) {
return false;
} elseif (is_numeric($id_tipo)) {
global $lang;
$sql = 'SELECT nome,nome'.$lang->prefix.' AS nome_lang FROM '.DB::getTablePrefix().'tipos WHERE id_tipo='.$id_tipo;
$rs = DB::select($sql);
$row = $rs[0];
$nome = ($row->nome_lang && !$nolang) ? $row->nome_lang : $row->nome;
return $nome;
} else {
return 'Tipos';
}
}
function interadmin_list($table, $id_tipo, $id, $type = 'list', $order = 'int_key,date_publish,varchar_key', $field = 'varchar_key', $sql_where = '', $seo = false)
{
return Jp7_Deprecated::interadmin_list($table, $id_tipo, $id, $type, $order, $field, $sql_where, $seo);
}
/**
* Alias for jp7_fields_values().
*
* @see jp7_fields_values()
*
* @version (2006/08/24)
*/
function interadmin_fields_values($param_0, $param_1 = '', $param_2 = '', $param_3 = '')
{
return jp7_fields_values($param_0, $param_1, $param_2, $param_3);
}
function jp7_fields_values($table_or_id, $field_or_id = '', $id_value = '', $field_name = '', $OOP = false)
{
return Jp7_Deprecated::jp7_fields_values($table_or_id, $field_or_id, $id_value, $field_name, $OOP);
}
/**
* Gets the ID of a record on the database from its "varchar_key" and "id_tipo" values.
*
* @param string $field_value Value of the field.
* @param int $id_tipo Value of the field "id_tipo" (Optional).
* @param string $field_name Name of the field (Optional).
*
* @global ADOConnection
* @global string
* @global string
*
* @return int Value of the field "id", which is the ID of the record.
*
* @author JP
*
* @version (2008/11/12)
* @deprecated
*/
function jp7_id_value($field_value, $id_tipo = 0, $field_name = 'varchar_key')
{
$tipoObj = new InterAdminTipo($id_tipo);
$record = $tipoObj->records()->where($field_name, $field_value)->first();
return $record->id ?? null;
}
/**
* class jp7_lang.
*
* @author JP
*
* @version (2007/08/08)
* @deprecated Use only with legacy systems
*/
class jp7_lang
{
/**
* Checks the current language.
*
* @param string $lang Current language, the default value is "".
* @param bool $force If <tt>TRUE</tt> it skips the check and $lang becomes the current language, the default value is <tt>FALSE</tt>.
*
* @global string
* @global string
*
* @return jp7_lang Object with the following properties: $this->lang, $this->prefix, $this->path and $this->path_2.