-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
4817 lines (4443 loc) · 139 KB
/
functions.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
/**
* This file demonstrates the central functions.
*
* @file-name :functions.php
* @author :Deepika Patel <deepika.patel@ucertify.com>
* @create_on :30-March-2018
* @last_update_on :12 July 2020
* @package :uclib.
*/
include_once UCLIB.'lists/ENUM.php';
/**
*Check if an email address is valid.
*@param string $email The email address to check.
*@param bool $ignore_number Optional. Whether to ignore numbers or not. Default is false.
*@return mixed Returns the email address if valid, false otherwise.
*/
function isValidEmail($email, $ignore_number = false)
{
$is_valid = false;
// Check if ignoring numbers
if ($ignore_number) {
// Split the email into chunks separated by '||'
$email_chunk = explode('||', $email);
// If the second chunk exists and has a length of 5, it is valid
if ($email_chunk[1] && strlen($email_chunk[1]) == 5) {
$is_valid = $email;
// If it matches the format of a number (with optional plus sign) up to 20 digits, it is valid
} elseif (preg_match('/^[+][1-9][0-9]{0,20}$/', $email)) { // Validate as number too (max 20 digit).
$is_valid = $email;
} else {
// Otherwise, validate it as an email address using filter_var()
$is_valid = filter_var($email, FILTER_VALIDATE_EMAIL);
}
} else {
$is_valid = filter_var($email, FILTER_VALIDATE_EMAIL);
}
return $is_valid;
}
/**
* Check if the given string is a valid phone number.
*@param string $str The phone number to be checked.
*@return int Returns 1 if the phone number is valid, otherwise returns 0.
*/
function checkPhoneNo($str)
{
$check_plus = substr($str, 0, 1);
$trim_mbl = removeStrSpace($str);
$check_num = is_numeric($trim_mbl);
return ($check_plus == '+' && $check_num && strlen($str) < 17 && strlen($str) > 10 && !isValidEmail($str)) ? 1 : 0;
}
/**
*Sanitize an email address.
*@param string $email The email address to be sanitized.
*@return string The sanitized email address.
*/
function sanitizeEmail($email)
{
// Use PHP's filter_var function with FILTER_SANITIZE_EMAIL flag to sanitize email.
return filter_var($email, FILTER_SANITIZE_EMAIL);
}
/**
* Sanitizes a string by removing multiple spaces, stripping tags, and escaping special characters.
*
* @param string $str The string to be sanitized.
* @return string The sanitized string.
*/
function sanitizeString($str)
{
// Remove multiple spaces
$str = preg_replace('/\s+/', ' ', $str); //remove multple space;
// Strip tags from the string
// Replace single quotes with backticks and escape special characters
$str = trim(filter_var(strip_tags(str_replace("'", '`', $str)), FILTER_SANITIZE_ADD_SLASHES));
return $str;
}
/**
* Generates a UUID / unique ID of specified length
*
* @param int $len The desired length of the ID (defaults to 8)
* @param string $salt The salt to use for generating the ID (defaults to 'ucertify')
* @return string The generated unique ID
*/
function gen_uuid($len = 8, $salt = 'ucertify')
{
$hex = md5($salt . uniqid('', true));
$pack = pack('H*', $hex);
$tmp = base64_encode($pack);
$uid = preg_replace('/[^A-Za-z0-9]/', '', $tmp);
$len = max(4, min(128, $len));
$uid_length = strlen($uid);
while ($uid_length < $len) {
$uid .= gen_uuid(22);
}
return substr($uid, 0, $len);
}
/**
* Retrieves user request data from $_GET and $_POST.
*
* @param array $data The data array to populate with request values.
* @param bool $isStrip Whether to strip tags from request values or not (defaults to true).
* @return array The populated data array.
*/
function getUserRequest($data = array(), $isStrip = true)
{
// Retrieve values from $_GET
if (isset($_GET)) {
foreach ($_GET as $k => $v) {
if (is_array($v)) {
$data[$k] = $v;
} else {
if ($isStrip) {
$data[$k] = trim(strip_all_tags_uc($v));
} else {
$data[$k] = trim($v);
}
}
}
}
// Retrieve values from $_POST
if (isset($_POST)) {
foreach ($_POST as $k => $v) {
if (is_array($v)) {
$data[$k] = $v;
} else {
$data[$k] = trim($v);
}
}
}
return $data;
}
/**
* It is the function that has been to find difference between two dates in diffrent units like month,year and days.
*
* @param string $interval denotes interval.
* @param string $datefrom denotes datefrom.
* @param string $dateto denotes dateto.
* @param string $using_timestamps denotes using_timestamps.
* @return string as whole response of function.
*/
function datediff($interval, $datefrom, $dateto, $using_timestamps = false)
{
/*
$interval can be:
yyyy - Number of full years.
q - Number of full quarters.
m - Number of full months.
y - Difference between day numbers.
(eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".).
d - Number of full days.
w - Number of full weekdays.
ww - Number of full weeks.
h - Number of full hours.
n - Number of full minutes.
s - Number of full seconds (default).
*/
if (!$using_timestamps) {
$datefrom = strtotime($datefrom, 0);
$dateto = strtotime($dateto, 0);
}
$difference = $dateto - $datefrom; // Difference in seconds.
switch ($interval) {
case 'yyyy':
$years_difference = floor($difference / 31536000);
if (mktime(date('H', $datefrom), date('i', $datefrom), date('s', $datefrom), date('n', $datefrom), date('j', $datefrom), date('Y', $datefrom) + $years_difference) > $dateto) {
$years_difference--;
}
if (mktime(date('H', $dateto), date('i', $dateto), date('s', $dateto), date('n', $dateto), date('j', $dateto), date('Y', $dateto) - ($years_difference + 1)) > $datefrom) {
$years_difference++;
}
$datediff = $years_difference;
break;
case 'q': // Number of full quarters.
$months_difference = 0;
$quarters_difference = floor($difference / 8035200);
while (mktime(date('H', $datefrom), date('i', $datefrom), date('s', $datefrom), date('n', $datefrom) + ($quarters_difference * 3), date('j', $dateto), date('Y', $datefrom)) < $dateto) {
$months_difference++;
}
$quarters_difference--;
$datediff = $quarters_difference;
break;
case 'm':
$months_difference = floor($difference / 2678400);
while (mktime(date('H', $datefrom), date('i', $datefrom), date('s', $datefrom), date('n', $datefrom) + ($months_difference), date('j', $dateto), date('Y', $datefrom)) < $dateto) {
$months_difference++;
}
$months_difference--;
$datediff = $months_difference;
break;
case 'y':
$datediff = date('z', $dateto) - date('z', $datefrom);
break;
case 'd':
$datediff = round($difference / 86400);
break;
case 'w':
$days_difference = floor($difference / 86400);
$weeks_difference = floor($days_difference / 7); // Complete weeks.
$first_day = date('w', $datefrom);
$days_remainder = floor($days_difference % 7);
$odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder.
if ($odd_days > 7) { // Sunday.
$days_remainder--;
}
if ($odd_days > 6) { // Saturday.
$days_remainder--;
}
$datediff = ($weeks_difference * 5) + $days_remainder;
break;
case 'ww':
$datediff = floor($difference / 604800);
break;
case 'h':
$datediff = floor($difference / 3600);
break;
case 'n':
$datediff = floor($difference / 60);
break;
default:
$datediff = $difference;
break;
}
return $datediff;
}
/**
* Converts a string into a comma-separated list of values, removing extra spaces and newlines.
*
* @param string $str The string to convert.
* @return string The comma-separated list of values.
*/
function strIntoComma($str = '')
{
$f = preg_replace('/(\r\n|\r|\n|\t)+/', ',', $str); // replace tab and new line into comma.
$f = preg_replace('/\s+/', ' ', $f); // replace extra space into single space.
$f = trim(implode(',', array_filter(explode(',', $f)))); // replace extra comma (blank array) in single comma.
return preg_replace('/\s*,\s*/', ',', $f); // trim before and after spacing with string.
}
/**
*Generates a list of dates between a given start and end date.
*@param string $start Start date in 'Y-m-d' format
*@param string $end End date in 'Y-m-d' format
*@param string $format Date format for the output list, defaults to 'Y-m-d'
*@return array List of dates between the start and end date
*/
function getDatesListFromRange($start, $end, $format = 'Y-m-d')
{
$array = array();
$interval = new DateInterval('P1D');
$realEnd = new DateTime($end);
$realEnd->add($interval);
$period = new DatePeriod(new DateTime($start), $interval, $realEnd);
foreach ($period as $date) {
$date_list[] = $date->format($format);
}
return $date_list;
}
/**
* Convert an XML string to a nested array.
* @param string $xml The XML string to convert.
* @return array The resulting nested array.
*/
function xml2array($xml)
{
$xmlary = array();
if ((strlen($xml) < 256) && is_file($xml)) {
$xml = file_get_contents_uc($xml);
}
$ReElements = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*?)<(\/\s*\1\s*)>)/s';
$ReAttributes = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';
preg_match_all($ReElements, $xml, $elements);
foreach ($elements[1] as $ie => $xx) {
$xmlary[$ie]['name'] = $elements[1][$ie];
$attributes = trim($elements[2][$ie]);
if ($attributes) {
preg_match_all($ReAttributes, $attributes, $att);
foreach ($att[1] as $ia => $xx) {
// all the attributes for current element are added here.
$xmlary[$ie]['attributes'][$att[1][$ia]] = $att[2][$ia];
}
}
// get text if it's combined with sub elements.
$cdend = strpos($elements[3][$ie], '<');
if ($cdend > 0) {
$xmlary[$ie]['text'] = substr($elements[3][$ie], 0, $cdend - 1);
}
if (preg_match($ReElements, $elements[3][$ie])) {
$xmlary[$ie]['elements'] = xml2array($elements[3][$ie]);
} elseif (isset($elements[3][$ie])) {
$xmlary[$ie]['text'] = $elements[3][$ie];
}
$xmlary[$ie]['closetag'] = $elements[4][$ie];
}
return $xmlary;
}
/**
* This function that is used to convert xml into array.
*
* @param string $xml denotes xml string.
* @return array as response of the function.
*/
function xmltoArray($xml)
{
$objXML = new clsXml_Array();
return $objXML->parse($xml);
}
/**
* This is the function that has been used for xml conversion to array.
*/
class clsXml_Array
{
/**
* It is the output.
*
* @var $arrOutput as arrOutput.
*/
public $arrOutput = array();
/**
* It is for the parser.
*
* @var $resParser as resParser.
*/
public $resParser;
/**
* It is for the string xml data.
*
* @var $strXmlData as strXmlData.
*/
public $strXmlData;
/**
* This function is used to parse str into xml.
*
* @param string $strInputXML is the strInputXML.
* @return array as response of the function.
*/
public function parse($strInputXML)
{
$this->resParser = xml_parser_create();
xml_set_object($this->resParser, $this);
xml_set_element_handler($this->resParser, 'tagOpen', 'tagClosed');
xml_set_character_data_handler($this->resParser, 'tagData');
$this->strXmlData = xml_parse($this->resParser, $strInputXML);
if (!$this->strXmlData) {
return array();
/*
die(sprintf(
'XML error: %s at line %d',
xml_error_string(xml_get_error_code($this->resParser)),
xml_get_current_line_number($this->resParser)
));
*/
}
xml_parser_free($this->resParser);
return $this->arrOutput;
}
/**
* This function has been used to open tag.
*
* @param array $parser denotes parser.
* @param array $name is the name.
* @param array $attrs is the attrs.
*/
public function tagOpen($parser, $name, $attrs)
{
foreach ($attrs as $key => $val) {
$new_attrs[strtolower($key)] = $val;
}
$tag = array(
'name' => strtolower($name),
'attributes' => $new_attrs,
);
array_push($this->arrOutput, $tag);
}
/**
* This function has been used for tag data.
*
* @param array $parser denotes parser.
* @param array $tagData is the tagData.
*/
public function tagData($parser, $tagData)
{
if (trim($tagData)) {
if (isset($this->arrOutput[count_uc($this->arrOutput) - 1]['text'])) {
$this->arrOutput[count_uc($this->arrOutput) - 1]['text'] .= $tagData;
} else {
$this->arrOutput[count_uc($this->arrOutput) - 1]['text'] = $tagData;
}
}
}
/**
* This function has been used for tag closed.
*
* @param array $parser denotes parser.
* @param array $name is the name.
*/
public function tagClosed($parser, $name)
{
$this->arrOutput[count_uc($this->arrOutput) - 2]['elements'][] = $this->arrOutput[count_uc($this->arrOutput) - 1];
array_pop($this->arrOutput);
}
}
/**
* This function has been used to convert array to xml.
*
* @param string $mainTag denotes mainTag.
* @param string $ary denotes ary.
* @param string $id is the id.
* @return string as overall response of the function.
*/
function array2xml($mainTag, $ary, $id = '')
{
if (is_numeric($mainTag)) {
$mainTag = 'child';
}
if (!is_Array($id)) {
$ids[] = $id;
} else {
$ids = $id;
}
$xml = "<$mainTag ";
foreach ($ids as $i) {
if ($i <> '') {
if (isset($ary[$i])) {
$xml .= ' ' . $i . '="' . $ary[$i] . '" ';
} else {
$xml .= ' ' . $i . '="" ';
}
}
}
$xml .= ">\n";
foreach ($ary as $k => $v) {
if (is_array($v)) {
$xml .= array2xml($k, $v);
} else {
if (!strpos('<[@#$%&=:;\\/,>"$/' . "'", $v)) {
$xml .= "<$k>$v</$k>\n";
} else {
$xml .= "<$k><![CDATA[$v]]></$k>\n";
}
}
}
$xml .= "</$mainTag>";
return $xml;
}
/**
* Shorten a string to a specified length.
*
* @param string $str The string to shorten.
* @param int $length The maximum length of the shortened string.
* @param int $direction The direction from which to shorten the string. 1 for left, 2 for right, 0 for both sides.
* @param bool $remove_single_quote Whether or not to remove single quotes from the string.
* @return string The shortened string.
*/
function short_string($str, $length = 80, $direction = 0, $remove_single_quote = true)
{
// Remove single quotes from string if specified
if ($remove_single_quote) {
// Replace double quotes with spaces
$str = str_replace("'", ' ', strip_all_tags_uc($str));
}
$str = str_replace('"', ' ', $str);
// Shorten string if longer than specified length
if (strlen($str) > $length) {
if ($direction == 1) {
$str = substr($str, 0, $length - 3) . '...';
} elseif ($direction == 2) {
$str = '...' . substr($str, ($length - 3) * -1);
} else {
if (strpos($str, '"') !== false) {
$str = substr($str, 0, $length - 3) . '...';
} else {
$str = substr($str, 0, ($length / 2) - 2) . '...' . substr($str, (($length / 2) - 2) * -1);
}
}
}
return $str;
}
/**
* Find a substring within a string between two given substrings and update the starting index.
*
* @param string $str denotes str.
* @param string $fstr denotes fstr.
* @param string $lstr is the lstr.
* @param string $i is the i.
* @return string as overall response of the function.
*/
function findtext($str, $fstr, $lstr, $i = 0)
{
return findtextnew($str, $fstr, $lstr, $i);
}
/**
* This function has been used to find text (new function : to find between substrings).
*
* @param string $str denotes str.
* @param string $fstr denotes fstr.
* @param string $lstr is the lstr.
* @param string $i is the i.
* @return string as overall response of the function.
*/
function findtextnew($str, $fstr, $lstr, &$i)
{
$namepos = 0;
$endpos = 0;
$lenstr = 0;
$retstr = '';
$lenstr = strlen($fstr);
if (strlen($str) <= 0) {
$i = 0;
return false;
}
$namepos = false;
if ($str && $i <= strlen($str)) {
$namepos = strpos(strtolower($str), strtolower($fstr), $i);
}
if ($namepos === false) {
$retstr = '';
} else {
$endpos = strpos(strtolower($str), strtolower($lstr), $namepos + $lenstr);
if ($endpos === false) {
$endpos = strlen($str) + 1;
}
$retstr = trim(substr($str, $namepos + $lenstr, $endpos - $lenstr - $namepos));
}
$i = $namepos + $lenstr;
return $retstr;
}
/**
* Extracts the value of the specified attribute from an HTML/XML tag.
*
* @param string $strmle The input string containing the tag.
* @param string $tag The name of the tag to extract the attribute from.
* @param string $attribute The name of the attribute to extract.
* @param int $i The index to start searching for the tag.
* @return string The value of the specified attribute, or an empty string if the attribute is not found.
*/
function getAttribute($strmle, $tag, $attribute, &$i)
{
$z = 0;
$startstr = '<' . $tag;
$endstr = '>';
// Ensure attribute is surrounded by spaces for more accurate matching.
$attribute = ' ' . $attribute;
// Find the tag and extract the attribute from it.
$str = ' ' . findtextnew($strmle, $startstr, $endstr, $i);
// Fix potential JSON encoding issues.
$str = str_replace('"{"', '\'{"', $str);
$str = str_replace('"}"', '"}\'', $str);
// Check if the attribute value is surrounded by quotes.
if ($str <> '') {
$str = findtextnew($str . '>', $attribute, $endstr, $z);
if (substr($str, 0, 1) == '=') {
$str = substr($str, 1);
$quote = substr($str, 0, 1);
if (($quote == "'") || ($quote == '"')) {
$z = 0;
$str = findtextnew($str, $quote, $quote, $z);
} else {
$stpos = strpos($str, ' ');
if ($stpos > 0) {
$str = substr($str, 0, $stpos);
}
}
// If the attribute value is not surrounded by quotes, try to extract it again.
} elseif (substr($str, 0, 1) !== '=' && strpos($str, $attribute) !== false) {
$str = findtextnew($str . '>', $attribute, $endstr, $z);
$str = substr($str, 1);
$quote = substr($str, 0, 1);
if (($quote == "'") || ($quote == '"')) {
$z = 0;
$str = findtextnew($str, $quote, $quote, $z);
} else {
$stpos = strpos($str, ' ');
if ($stpos > 0) {
$str = substr($str, 0, $stpos);
}
}
} else {
// Attribute not found, return empty string.
$str = '';
}
}
return $str;
}
/**
* Remove comments and specific tags from a string.
*
* @param string $str The input string.
* @return string The modified string.
*/
function removecomments($str)
{
// Remove HTML comments
$str = removephrase($str, '<!--', '-->');
// Remove PHP tags
$str = removephrase($str, '<?', '>');
// Remove specific custom tags
$str = removephrase($str, '<uc:iref>', '</uc:iref>');
return $str;
}
/**
* Removes a specified phrase from a given string.
*
* @param string $str The string to remove the phrase from.
* @param string $keystart The starting phrase to remove.
* @param string $keyend The ending phrase to remove.
* @return string The resulting string with the phrase removed.
*/
function removephrase($str, $keystart, $keyend)
{
$len = strlen($str);
$curpos = 0;
for ($i = 1; $i < 1000; $i++) {
$curpos = strpos($str, $keystart);
if ($curpos === false) {
$i = 1001;
} else {
$endpos = strpos($str, $keyend, $curpos);
if ($endpos > 0) {
$start = trim(substr($str, 0, $curpos), " \t");
$end = trim(substr($str, $endpos + strlen($keyend)), " \t");
if (strpos(' .?!', substr($end, 0, 1)) === false) {
$end = ' ' . $end;
}
$str = $start . $end;
} else {
$i = 1001;
}
}
$len = strlen($str);
}
return $str;
}
/**
* Find and get the Smarty template or retrieves an instance of the Smarty template engine with appropriate settings and assigns common global variables.
*
* @param string $templatedir denotes templatedir.
* @param string $basedir denotes basedir.
* @param string $usesitename is the usesitename.
* @return object as overall response of the function.
*/
function getSmartyTemplate($templatedir = 'layout/templates', $basedir = false, $usesitename = true)
{
global $website;
global $meta;
global $isSecure;
if (!$basedir && defined('SITE_ABSPATH')) {
$basedir = SITE_ABSPATH;
}
include_once UCLIB . 'templateengines/smarty_3.1/libs/Smarty.class.php';
include_once UCLIB . 'templateengines/smarty_3.1/libs/SmartyBC.class.php';
$smarty = new SmartyBC();
$smarty->left_delimiter = '<{';
$smarty->right_delimiter = '}>';
if (defined('SITE_NAME') && $usesitename) {
$smarty->template_dir = $basedir . SITE_NAME . $templatedir;
} else {
$smarty->template_dir = $basedir . $templatedir;
}
$smarty->compile_dir = $basedir . 'templates_c/';
$smarty->config_dir = $basedir . 'configs/';
$smarty->cache_dir = $basedir . 'cache/';
if (defined('SITE_URL')) {
$smarty->assign('SITE_URL', SITE_URL);
$smarty->assign('site_url', SITE_URL);
}
if (defined('SITE_SECURE_URL')) {
$smarty->assign('SITE_SECURE_URL', SITE_SECURE_URL);
}
if (defined('APP_FOLDER')) {
$smarty->assign('APP_FOLDER', APP_FOLDER);
}
if (defined('SITE_SHORT_URL')) {
$smarty->assign('SITE_SHORT_URL', SITE_SHORT_URL);
}
if (defined('SITE_LINK_URL')) {
$smarty->assign('SITE_LINK_URL', SITE_LINK_URL);
}
if (defined('SITE_THEME_URL')) {
$smarty->assign('SITE_THEME_URL', SITE_THEME_URL);
}
if (defined('THEME_NAME')) {
$smarty->assign('THEME_NAME', THEME_NAME);
}
if (defined('DEVICE')) {
$smarty->assign('DEVICE', DEVICE);
}
if (defined('THEME_SUFFIX')) {
$smarty->assign('THEME_SUFFIX', THEME_SUFFIX);
}
if (defined('SITE_THEME_DIR')) {
$smarty->assign('SITE_THEME_DIR', SITE_THEME_DIR);
}
if (defined('SITE_NAME')) {
$smarty->assign('SITE_NAME', SITE_NAME);
}
if (defined('IS_DATA_DUMP')) {
$smarty->assign('IS_DATA_DUMP', IS_DATA_DUMP);
}
if (isset($website['currencycode'])) {
$smarty->assign('currencycode', $website['currencycode']);
}
$smarty->assign('isSecure', $isSecure);
$smarty->assign('meta', $meta);
return $smarty;
}
/**
* Converts a date string to the format 'mm/dd/yyyy'.
*
* @param string $str The date string to convert.
* @return string The formatted date string
*/
function strtodate($str = '')
{
if (strlen($str) == 0) {
$str = date('YmdHis');
}
// Check if the date string is already in the correct format
if (strpos($str, '/', 0) === false) {
// If not, format the date string as 'yyyy/mm/dd'
$dt = substr($str, 4, 2) . '/' . substr($str, 6, 2) . '/' . substr($str, 0, 4);
} else {
// Otherwise, use the date string as is
$dt = $str;
}
return $dt;
}
/**
* Converts a date string in the format MM/DD/YYYY to a string in the format YYYYMMDD000000.
*
* @param string $date The date to convert. If empty, the current date and time will be used.
* @return string The date string in the format YYYYMMDD000000.
*/
function datetostr($date = '')
{
if ($date == '') {
$dt = date('YmdHis');
} else {
if (strpos($date, '/', 0) > 0) {
$dt = substr($date, 6, 4) . substr($date, 0, 2) . substr($date, 3, 2) . '000000';
} else {
$dt = $date;
}
}
return $dt;
}
/**
* This function has been used to convert date.
* Returns the date that is a certain number of days after a given date.
* If no date is specified, defaults to the current date.
* @param string $date The starting date (format: 'YYYYMMDD').
* @param int $dayafter The number of days after the starting date.
* @return string The resulting date (format: 'YYYYMMDD000000').
*/
function dayafter($date, $dayafter = 0)
{
// If no date is specified, use the current date
if ($date == 0) {
$yr = date('Y');
$mon = date('m');
$day = date('d');
}
// Calculate the resulting date
$day = $day + $dayafter;
$mon = $mon + 0;
$yr = $yr + 0;
if ($day <= 0) {
$day = '1';
$mon--;
}
if ($mon <= 0) {
$mon = '1';
$yr--;
}
if ($day <= 9) {
$day = '0' . $day;
}
if ($mon <= 9) {
$mon = '0' . $mon;
}
$dt = $yr . $mon . $day . '000000';
return $dt;
}
/**
* Generates a random string with specified length and type.
*
* @param int $a_digit The length of the random string.
* @param int $a_rantype The type of random string: 1 for alphanumeric, 2 for alphabetic, 3 for numeric.
* @return string The generated random string.
*/
function f_random($a_digit = 5, $a_rantype = 1)
{
$l_offset = 87; // Ascii offset require to get alpha vlue.
$l_add = 0; // offset to get only alpha value.
$i = 0; // starting for loop.
// Determine the maximum value for random number generation based on rantype.
if ($a_rantype == 1) {
$maxno = 35;
} else {
if ($a_rantype == 2) {
$maxno = 25;
$l_add = 10;
} else {
$maxno = 9;
}
}
$rannum = '';
for ($i == 0; $i < $a_digit; $i++) {
$lnum = rand_uc(1, $maxno);
$lnum = $lnum + $l_add;
if ($lnum < 10) {
$ran = $lnum;
} else {
$ran = chr($lnum + $l_offset);
}
$rannum = $rannum . $ran;
}
return $rannum;
}
/**
* Redirects to a given URL with an optional message and delay time.
*
* @param string $url The URL to redirect to.
* @param int $time The delay time in seconds before redirecting (default is 3 seconds).
* @param string $message The message to display (default is an empty string).
* @return void
*/
function redirect_header($url, $time = 3, $message = '')
{
$url = preg_replace('/&/i', '&', htmlspecialchars($url, ENT_QUOTES));
echo '
<html>
<head>
<title>".SITE_URL."</title>
<meta http-equiv="Content-Type" content="text/html; charset=' . _CHARSET . '" />
<meta http-equiv="Refresh" content="' . $time . '; url=' . $url . '" />
<style type="text/css">
body {background-color : #fcfcfc; font-size: 12px; font-family: Trebuchet MS,Verdana, Arial, Helvetica, sans-serif; margin: 0px;}
.redirect {width: 70%; margin: 110px; text-align: center; padding: 15px; border: #e0e0e0 1px solid; color: #666666; background-color: #f6f6f6;}
.redirect a:link {color: #666666; text-decoration: none; font-weight: bold;}
.redirect a:visited {color: #666666; text-decoration: none; font-weight: bold;}
.redirect a:hover {color: #999999; text-decoration: underline; font-weight: bold;}
</style>
</head>
<body>
<div align="center">
<div class="redirect">
<span style="font-size: 16px; font-weight: bold;">' . $message . '</span>
<hr style="height: 3px; border: 3px #E18A00 solid; width: 95%;" />
<p>' . sprintf('redirecting to %s. Please wait...', $url) . '</p>
</div>
</div>
</body>
</html>';
exit();
}
/**
* Format date in given format
*
* @param string $dt Date string to format
* @param string $fmt Format string (default: 'd-M-y')
* @return string Formatted date string
*/
function f_date($dt, $fmt = 'd-M-y')
{
// Extract date/time components from input string
$yr = substr($dt, 0, 4);
$mon = substr($dt, 4, 2);
$day = substr($dt, 6, 2);
$hrs = substr($dt, 8, 2);
$min = substr($dt, 10, 2);
$sec = substr($dt, 12, 2);
// Format date using the extracted components
return date($fmt, mktime($hrs, $min, $sec, $mon, $day, $yr));
}
/**
* Trims whitespace characters from the beginning and end of a string.
*
* @param string $str The input string to remove CDATA tags from
* @return string The resulting string with CDATA tags removed
*/
function trimWhiteSpace($str)
{
return trim($str);
}
/**
* Removes CDATA tags from a given string
*
* @param string $str The input string to remove CDATA tags from
* @return string The resulting string with CDATA tags removed
*/
function removeCDATA($str)
{
// Check if the string contains CDATA tags
if (strpos($str, 'CDATA[') !== false) {
// Remove different variants of CDATA tags from the string
$str = str_replace('<![CDATA[', '', $str);
$str = str_replace(']]>', '', $str);
$str = str_replace('[CDATA[', '', $str);
$str = str_replace(']]', '', $str);
}
// Return the resulting string
return $str;
}
/**
* Removes invalid characters from XML content.