/
helper_library.php
1354 lines (1051 loc) · 35.6 KB
/
helper_library.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
/*=========================================================================================================
Copyright © Emprivo, Inc. All rights reserved.
Purpose: contains helpful functions for general use
=========================================================================================================*/
/*==================================
Checks return values from classes and functions for false
@param all required data
@return bool
==================================*/
function return_validation( $data )
{
if ( !is_bool($data) || !$data === false ) # Note the ===
return true;
else
return false;
}
/*==================================
Cleans data
==================================*/
function basic_clean( &$data )
{
if ( !is_array($data) )
$data = nvl( trim( $data ) );
}
/*==================================
Changes <br> to hard returns
==================================*/
function br2nl( $str )
{
return preg_replace( "=<br */?>=i", "\n", $str );
}
/*==================================
Changes double hard returns to <p> tags
==================================*/
function nl2p( $str )
{
return str_replace('<p></p>', '', '<p>' . preg_replace('#([\r\n]\s*?[\r\n]){2,}#', '</p>$0<p>', $str) . '</p>');
}
/*==================================
If data is not set, returns "NULL" (used for db insert queries to set NULL values)
@param string required str
@param bool optional add_quotes -- if true, add quotes and sends 'NULL'
==================================*/
function data_or_null( $str, $add_quotes = false )
{
if ( trim($str) == "" || !isset($str) )
{
return "NULL";
}
else
{
if ( $add_quotes )
$str = "'" . escape_quotes( trim( $str ) ) . "'";
return $str;
}
}
/*==================================
Similar to addslashes() in PHP, but prevents multiple slashes in db
==================================*/
function escape_quotes( $str )
{
return addslashes( stripslashes( stripslashes( stripslashes( $str ) ) ) );
}
/*==================================
Checks if user's browser supports gzip compression (safari is excluded due to issues with it parsing gzip files)
==================================*/
function is_browser_gzip()
{
if ( substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') && !substr_count($_SERVER['HTTP_USER_AGENT'], 'Safari') )
return true;
else
return false;
}
/*==================================
Returns user's ip address (used to get forwarded ip from proxy server else uses normal server variable)
==================================*/
function remote_addr()
{
if ( $_SERVER['HTTP_X_FORWARDED_FOR'] != "" )
return $_SERVER['HTTP_X_FORWARDED_FOR'];
else
return $_SERVER['REMOTE_ADDR'];
}
/*==================================
Callback function for array_filter()
==================================*/
function remove_empty( $value )
{
if ( trim($value) != "" )
return true;
}
/*==================================
Removes certain number of characters from the end
==================================*/
function delete_tail( $str, $num_chars )
{
return substr( $str, 0, strlen($str) - $num_chars );
}
/*==================================
Get first few words from a string based on maximum character length allowed
@param string required str - the string to concat
@param int required max_chars - maximum number of characters allowed
@param bool optional ellipsis - if false doesn't add "..." at end of string
==================================*/
function limit_length( $str, $max_chars, $ellipsis = true )
{
if ( strlen($str) > $max_chars )
{
$words = array_filter( explode( " ", $str ), "remove_empty" );
# take first few words
for ( $i = 0; strlen($final_str) < $max_chars; $i++ )
{
if( strlen( $final_str . $words[$i] ) > $max_chars )
continue;
else
$final_str .= $words[$i] . " ";
}
# add ...
if ( $ellipsis )
$final_str = trim( $final_str ) . "...";
return trim( $final_str );
}
else
return trim( $str );
}
/*==================================
Converts an array of words into a string
==================================*/
function array_to_string( &$array )
{
$str = "";
foreach ( $array as $word )
{
$str .= $word . " ";
}
return trim($str);
}
/*==================================
Replace certain characters with hyphen (-) so that the url is valid and clean
@param string required str
@param string optional separator - the character used in place of the illegal characters
==================================*/
function make_url_friendly( $str, $separator = "-" )
{
$illegal = array( "—",
""",
"&",
"©",
"«",
"»",
"–",
"·",
"‘",
"’",
"“",
"”",
" ",
"`",
"~",
"!",
"@",
"#",
"$",
"%",
"^",
"&",
"*",
"(",
")",
"--",
"-",
"-",
"_",
"__",
"+",
"=",
"{",
"}",
"[",
"]",
";",
":",
"'",
"´",
"\"",
",",
".",
"<",
">",
"/",
"?",
"\\",
"|");
$str = str_replace( $illegal, $separator, trim($str) );
$str = preg_replace('/[^a-zA-Z0-9\s\-]/', '', $str);
if( strpos( $str, "--" ) !== false )
$str = make_url_friendly( $str, $separator );
while ( substr( $str, strlen($str)-1, 1 ) == "-" )
$str = substr( $str, 0, strlen($str)-1 );
return $str;
}
/*==================================
Removes special characters like trademark and copyright symbols
==================================*/
function remove_special_characters( $str )
{
$str = ereg_replace( 8226, " ", $str ); # bullet
$str = ereg_replace( 149, " ", $str ); # bullet
$str = ereg_replace( 8211, " ", $str ); # en dash
$str = ereg_replace( 150, " ", $str ); # en dash
$str = ereg_replace( 8212, " ", $str ); # em dash
$str = ereg_replace( 151, " ", $str ); # em dash
$str = ereg_replace( 8482, " ", $str ); # trademark
$str = ereg_replace( 153, " ", $str ); # trademark
$str = ereg_replace( 169, " ", $str ); # copyright mark
$str = ereg_replace( 174, " ", $str ); # registration mark
return trim( collapse_spaces( $str ) );
}
/*==================================
Replaces special characters with non-special equivalents
==================================*/
function normalize_special_characters( $str )
{
# Quotes cleanup
$str = ereg_replace( chr(ord("`")), "'", $str ); # `
$str = ereg_replace( chr(ord("´")), "'", $str ); # ´
$str = ereg_replace( chr(ord("„")), ",", $str ); # „
$str = ereg_replace( chr(ord("`")), "'", $str ); # `
$str = ereg_replace( chr(ord("´")), "'", $str ); # ´
$str = ereg_replace( chr(ord("“")), "\"", $str ); # “
$str = ereg_replace( chr(ord("”")), "\"", $str ); # ”
$str = ereg_replace( chr(ord("´")), "'", $str ); # ´
$str = ereg_replace( chr(ord("’")), "'", $str ); # ’
$unwanted_array = array( 'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' );
$str = strtr( $str, $unwanted_array );
# Bullets, dashes, and trademarks
$str = ereg_replace( chr(149), "•", $str ); # bullet •
$str = ereg_replace( chr(150), "–", $str ); # en dash
$str = ereg_replace( chr(151), "—", $str ); # em dash
$str = ereg_replace( chr(153), "™", $str ); # trademark
$str = ereg_replace( chr(169), "©", $str ); # copyright mark
$str = ereg_replace( chr(174), "®", $str ); # registration mark
return $str;
}
/*==================================
301 redirect
==================================*/
function permanent_redirect( $url )
{
header("HTTP/1.1 301 Moved Permanently");
header("Location: " . $url);
exit();
}
/*==================================
Checks if url already has http:// part. If not, appends to homepage_url
@param string required url
@return string
==================================*/
function absolute_or_relative_url( $url )
{
if( strpos( $url, "http://" ) !== false )
return $url;
else
return $_SESSION['homepage_url'] . $url;
}
/*==================================
If the ending decimal is ".00" or ".0", then return string without decimal point.
For example: "98.00" or "98.0" will get converted to "98".
==================================*/
function format_double( $str )
{
if ( substr( $str, strlen($str)-3, 3 ) == ".00" )
return delete_tail( $str, 3 );
else if ( substr( $str, strlen($str)-2, 2 ) == ".0" )
return delete_tail( $str, 2 );
else
return $str;
}
/*==================================
Converts a date string (default format: 10/31/2006)
==================================*/
function format_date( $str, $format = "n/j/Y" )
{
$str = strtotime( $str );
if ( $str != "" )
return date( $format, $str );
else
return false;
}
/*==================================
Returns a timestamp (optionally takes a date and formats it as a timestamp)
==================================*/
function timestamp( $date = "" )
{
if ( $date != "" )
return date( "Y-m-d H:i:s", strtotime($date) );
else
return date( "Y-m-d H:i:s" );
}
/*==================================
Place contents of an array into a variable
==================================*/
function print_r_to_var( $array )
{
ob_start();
print_r($array);
$b = ob_get_contents();
ob_end_clean();
return $b;
}
/*==================================
Used for error handling (database errors mostly).
Note: the verbal error handling sends the user to an error message page.
==================================*/
function handle_verbal_error( $error_str, &$query = "" )
{
$debug = debug_backtrace();
$error_str = "Error:\n" . $error_str .
"\n\nBacktrace:\n" . print_r_to_var( $debug ) .
"\n\nSQL Query:\n" . $query .
"\n\nURL:\n" . "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}?{$_SERVER['QUERY_STRING']}" .
"\n\nVisitor's IP Address:\n" . remote_addr() .
"\n\nTime Stamp:\n" . timestamp() .
"\n\nSession Data:\n" . print_r_to_var( $_SESSION ) .
"\n\nGET Data:\n" . print_r_to_var( $_GET ) .
"\n\nPOST Data:\n" . print_r_to_var( $_POST );
if ( stristr( $_SESSION['homepage_url'], 'http://localhost' ) === false )
{ # live server
mail( "error@" . $_SESSION['site_domain_name'], "URGENT: " . $_SESSION['site_name'] . " VERBAL Error", $error_str, "From: error@" . $_SESSION['site_domain_name'] . "\r\n" );
# redirect user to friendly error page
echo "<script type=\"text/javascript\">";
echo "document.location='/oops';";
echo "</script>";
}
else
{ # dev server
echo "<pre>";
print_r ( $error_str );
echo "</pre>";
}
exit();
}
/*==================================
Used for error handling
Note: the silent error handling doesn't let the user know about any problems
==================================*/
function handle_silent_error( $error_str, &$query = "" )
{
$debug = debug_backtrace();
$error_str = "Error:\n" . $error_str .
"\n\nBacktrace:\n" . print_r_to_var( $debug ) .
"\n\nSQL Query:\n" . $query .
"\n\nURL:\n" . "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}?{$_SERVER['QUERY_STRING']}" .
"\n\nVisitor's IP Address:\n" . remote_addr() .
"\n\nTime Stamp:\n" . timestamp() .
"\n\nSession Data:\n" . print_r_to_var( $_SESSION ) .
"\n\nGET Data:\n" . print_r_to_var( $_GET ) .
"\n\nPOST Data:\n" . print_r_to_var( $_POST );
if ( stristr( $_SESSION['homepage_url'], 'http://localhost' ) === false )
{ # live server
mail( "error@" . $_SESSION['site_domain_name'], $_SESSION['site_name'] . " Silent Error", $error_str, "From: error@" . $_SESSION['site_domain_name'] . "\r\n" );
}
else
{ # dev server
echo "<pre>";
print_r ( $error_str );
echo "</pre>";
}
}
/*==================================
Returns numbers of days left to given date
@param date required date
@return int
==================================*/
function get_days_to_date( $date )
{
return abs( round( ( strtotime($date) - strtotime(date("Y-m-d")) ) / (60 * 60 * 24) ) );
}
/*==================================
Removed multiple spaces with a single space
==================================*/
function collapse_spaces( $str )
{
return eregi_replace("[[:space:]]+", " ", $str);
}
/*==================================
If input is undefined, set default value
==================================*/
function set_default( &$var, $default = "" )
{
if ( !isset($var) || $var == "" )
$var = $default;
}
/*==================================
If input is undefined, return default value
==================================*/
function nvl( $var, $default = "" )
{
return isset($var) ? $var : $default;
}
/*==================================
Prints input with the HTML characters (like "<", ">", etc.) properly quoted
==================================*/
function pv( $var )
{
echo isset($var) ? htmlSpecialChars(stripslashes($var)) : "";
}
/*==================================
If data is not in _GET, obtain data from _POST
==================================*/
function GET_or_POST( $var )
{
if ( trim(nvl($_GET[$var])) != "")
return trim(nvl($_GET[$var]));
else
return trim(nvl($_POST[$var]));
}
/*==================================
Get current url in browser
==================================*/
function get_browser_url()
{
return "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
/*=============================================
Print error list
@param array required errors
=============================================*/
function print_errors( $errors )
{
if ( count($errors) )
{
echo "<ul class='dark_red vertical_space_22'>";
foreach ( $errors as $value )
echo "<li>" . $value . "</li>";
echo "</ul>";
}
}
/*==================================
Checks to see if an email address is valid -- allows all country extensions
==================================*/
function is_email( $email )
{
$matches = "/^[-_.+[:alnum:]]+@((([[:alnum:]]|" .
"[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|".
"af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|".
"bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|".
"bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|".
"cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|".
"er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|".
"gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|".
"hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|".
"kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|".
"lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|".
"mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|".
"nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|".
"pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|".
"sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|".
"sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|".
"ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|".
"yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|".
"[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|".
"[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i";
return preg_match( $matches, $email );
}
/*==================================
Replaces restricted words with *
@param string required str
@return string
==================================*/
function replace_restricted_words( $str )
{
$words = array_filter( explode( " ", $str ), "remove_empty" );
foreach( $words as $key => $value )
{
$checkQ = db_query("SELECT 1
FROM
emprivo.restricted_words
WHERE
word = '" . escape_quotes( $value ) . "'");
if( db_num_rows( $checkQ ) )
{
for( $i = 0; $i < strlen($value); $i++ )
$stars .= "*";
$words[$key] = $stars;
}
}
return array_to_string( $words );
}
/*==================================
Checks a string for restricted words
@param string required str
@return bool
==================================*/
function check_restricted_words( $str )
{
$str = make_url_friendly( $str, " " );
$words = array_filter( explode( " ", $str ), "remove_empty" );
foreach( $words as $value )
{
$checkQ = db_query("SELECT 1
FROM
emprivo.restricted_words
WHERE
word = '" . $value . "'");
if( db_num_rows($checkQ) )
return false;
}
# no restricted words found
return true;
}
/*==================================
Checks if a url is valid
@param string required url
@param array optional error_strings -- an array of error messages to check the opened page for
ex: "merchant is no longer participating in the affiliate program"
==================================*/
function is_url_valid( $url, $error_strings = array() )
{
# data validation
data_validation( $url, "|required|", "silent" );
# check if url is empty
if ( $url == "" )
return false;
# get url data
$url_data = get_url( $url );
$content = $url_data[0];
$response = $url_data[1];
$response_code = $response['http_code'];
# check response code
if ( $response_code == "400" ||
$response_code == "401" ||
$response_code == "402" ||
$response_code == "403" ||
$response_code == "404" ||
$response_code == "411" ||
$response_code == "500" ||
$response_code == "501" ||
$response_code == "502" ||
$response_code == "0")
{
return false;
}
# check if url contains 404 or 'localhost'
if ( stristr( $response['url'], "/404" ) || stristr( $response['url'], "/localhost" ) )
{
return false;
}
$content = strtolower( trim( collapse_spaces( strip_tags( $content ) ) ) );
# do regexp matching
foreach( $error_strings as $value )
{
if ( eregi( $value, $content ) )
{ # broken link
return false;
}
}
# good link
return true;
}
/*==================================
Removes whitespace before serving page to speed up page loads for visitor
==================================*/
function remove_whitespace( $data )
{
if ( strpos( $data, "<html" ) !== false )
{ # html content
# remove newlines
#$data = preg_replace("/\r\n|\n|\r/", " ", $data);
# remove multiple spaces
#$data = eregi_replace("[[:space:]]+", " ", $data);
# remove newlines, tabs, multiple spaces
$data = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), ' ', $data);
# remove space between tags
$data = preg_replace('/>\s+</m', '><', $data);
return trim($data);
}
else
return $data;
}
/*==================================
Cleans and validates data
@param string/array required data
@param string required validations
@param string optional error_handling -- specifies if error handling function should be called ("verbal" or "silent")
@return bool
==================================*/
function data_validation( &$data, $validations, $error_handling = "" )
{
/*
-----------------------------------------------------------------------------------------------
Validations Syntax Input Data
-----------------------------------------------------------------------------------------------
required |required| for both arrays and strings
optional |optional| for both arrays and strings
binary (0 or 1) |binary| strings
is_int |int| strings
is_email |email| strings
check restricted words |check_restricted_words| strings
regexp |regexp:[a-z]| strings
min words |min_words:x| strings
max words |max_words:x| strings
min value |min_value:x| strings
max value |max_value:x| strings
min length |min_length:x| strings
max length |max_length:x| strings
min array count |min_elements:x| array
max array count |max_elements:x| array
value in |value_in:x__x__x| strings
value not in |value_not_in:x__x__x| strings
*/
# set if data is an array
if ( is_array($data) )
$is_array = true;
# clean input data
basic_clean( $data );
# get list of validations to perform
$validations = array_filter( explode( "|", trim($validations) ), "remove_empty" );
# reset is_error flag
$is_error = false;
# perform validations
foreach( $validations as $check )
{
if( trim($check) != "" )
{
switch( true )
{
case substr( $check, 0, 8 ) == 'optional':
if( $is_array && !count($data) )
return true; # data's optional and not available, so skip all checks
else if( !$is_array && $data == "" )
return true; # data's optional and not available, so skip all checks
break;
case substr( $check, 0, 8 ) == 'required':
if( $is_array && !count($data) )
$is_error = true;
else if( !$is_array && $data == "" )
$is_error = true;
break;
case substr( $check, 0, 6 ) == 'binary':
if( $data != 0 && $data != 1 )
$is_error = true;
break;
case substr( $check, 0, 3 ) == 'int':
if( !eregi( "^[0-9-]+$", $data ) )
$is_error = true;
break;
case substr( $check, 0, 5 ) == 'email':
if( !is_email( $data ) )
$is_error = true;
break;
case substr( $check, 0, 22 ) == 'check_restricted_words':
if( !check_restricted_words( $data ) )
$is_error = true;
break;
case substr( $check, 0, 6 ) == 'regexp':
if( !eregi( substr($check,7,strlen($check)-7), $data ) )
$is_error = true;
break;
case substr( $check, 0, 9 ) == 'min_value':
if( !eregi( "^[0-9-]+$", $data ) || $data < substr($check,10,strlen($check)-10) )
$is_error = true;
break;
case substr( $check, 0, 9 ) == 'max_value':
if( !eregi( "^[0-9-]+$", $data ) || $data > substr($check,10,strlen($check)-10) )
$is_error = true;
break;
case substr( $check, 0, 9 ) == 'min_words':
if( count( array_filter ( explode( " ", $data ), "remove_empty" ) ) < substr($check,10,strlen($check)-10) )
$is_error = true;
break;
case substr( $check, 0, 9 ) == 'max_words':
if( count( array_filter ( explode( " ", $data ), "remove_empty" ) ) > substr($check,10,strlen($check)-10) )
$is_error = true;
break;
case substr( $check, 0, 10 ) == 'min_length':
if( strlen($data) < substr($check,11,strlen($check)-11) )
$is_error = true;
break;
case substr( $check, 0, 10 ) == 'max_length':
if( strlen($data) > substr($check,11,strlen($check)-11) )
$is_error = true;
break;
case substr( $check, 0, 12 ) == 'min_elements':
if( count($data) < substr($check,13,strlen($check)-13) )
$is_error = true;
break;
case substr( $check, 0, 12 ) == 'max_elements':
if( count($data) > substr($check,13,strlen($check)-13) )
$is_error = true;
break;
case substr( $check, 0, 8 ) == 'value_in':
$values = substr( $check, 9, strlen($check)-9 );
$values = array_filter( explode( "__", $values ), "remove_empty" );
$value_in = false;
foreach( $values as $value )
if( $data == $value )
$value_in = true;
if( $value_in == false )
$is_error = true;
break;
case substr( $check, 0, 12 ) == 'value_not_in':
$values = substr( $check, 13, strlen($check)-13 );
$values = array_filter( explode( "__", trim($values) ), "remove_empty" );
$value_not_in = true;
foreach( $values as $value )
if( $data == $value )
$value_not_in = false;
if( $value_not_in == false )
$is_error = true;
break;
}
}
}
if( $is_error )
{ # errors found
if( $error_handling == "verbal" )
handle_verbal_error( "RED ALERT: invalid data=" . $data . " checks=" . array_to_string($checks) );
else if( $error_handling == "silent" )
handle_silent_error( "WATCH: invalid data=" . $data . " checks=" . array_to_string($checks) );
# DONT REMOVE THIS RETURN -- class functions use it for case logic
return false;
}
else
{ # no errors
return true;
}
}
/*==================================
Prints out error
==================================*/
function print_error( $title = "", $error = "" )
{
echo "<span class='stylish error'><strong>" , $title, "</strong> " , $error, "</span><br /><br />";
}
/*==================================
Prints out title
==================================*/
function print_title( $title, $description = "" )
{
echo "<span class='title'>" , $title , "</span><br />";
if ( $description != "" )
echo "<span class='sub_title'>" , $description , "</span><br /><br />";
else
echo "<br />";
}
/*==================================
Clean discount detail values -- remove unwanted characters
==================================*/
function clean_discount_values( &$value )
{
$value = str_replace( array(",", " ", "$", "%"), "", $value );
}
/*==================================
Get list of directory contents via FTP
@param string required server
@param string required username
@param string required password
@param string optional directory - changed to this directory on server
@param string optional num_tries - number of times to try getting the list
@return array[0] ftp_id
array[1] array of file names
==================================*/
function ftp_ls( $server, $username, $password, $directory = "", $num_tries = 1 )
{
# data validation
data_validation( $server, "|required|", "verbal" );
data_validation( $username, "|required|", "verbal" );
data_validation( $password, "|required|", "verbal" );
data_validation( $directory, "|optional|" );
for( $i = 1; !isset( $files ) && !is_array( $files ); $i++ )
{
$ftp_id = ftp_connect( $server );
ftp_login( $ftp_id, $username, $password );
if ( $directory != "" )
ftp_chdir( $ftp_id, $directory );