This repository was archived by the owner on Jun 6, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathlib.php
More file actions
1871 lines (1443 loc) · 42.3 KB
/
Copy pathlib.php
File metadata and controls
1871 lines (1443 loc) · 42.3 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
/**
* Set of global functions to be used across the plugin
*
* @copyright 2015 Bedford College
* @package Bedford College Grade Tracker
* @version 1.0
* @author Conn Warwicker <cwarwicker@bedford.ac.uk> <conn@cmrwarwicker.com> <moodlesupport@bedford.ac.uk>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
require_once $CFG->dirroot . '/blocks/gradetracker/GradeTracker.class.php';
// Find all classes in /classes and include them
gt_require_classes('classes');
/**
* Require all the php classes in given directory & sub directories
* @global type $CFG
* @param type $dir
*/
function gt_require_classes($dir){
global $CFG;
// Find sub folders
if ($open = opendir($CFG->dirroot . '/blocks/gradetracker/' . $dir))
{
while ( ($file = readdir($open)) !== false )
{
if ($file == '.' || $file == '..') continue;
if (strpos($file, '.php') === false)
{
gt_require_classes($dir . '/' . $file);
}
}
}
// Now just load any files in this folder
foreach( glob("{$CFG->dirroot}/blocks/gradetracker/{$dir}/*.class.php") as $file ){
require_once $file;
}
}
/**
* Print something to a test file. Useful for debugging AJAX requests.
* @global type $CFG
* @param type $value
*/
function gt_pn($value)
{
global $CFG;
$file = fopen($CFG->dirroot . '/blocks/gradetracker/tmp.txt', 'a');
if ($file){
fwrite($file, print_r($value, true));
fwrite($file, "\n");
fclose($file);
}
}
/**
* Write a trace to any given file in the data directory
* @param type $file
* @param type $text
*/
function gt_trace($file, $text)
{
$file = fopen($file, 'a');
if ($file)
{
fwrite($file, "[".date('d-m-Y H:i:s')."] " . $text . "\n");
fclose($file);
}
}
function gt_enable_debugging(){
$_SESSION['gt_debug'] = true;
}
function gt_disable_debugging(){
unset($_SESSION['gt_debug']);
}
/**
* Is GT debugging turned on?
* @return type
*/
function gt_is_debugging(){
return (isset($_SESSION['gt_debug']));
}
/**
* Add something to debug log
* @global type $USER
* @param type $info
*/
function gt_debug($info){
global $USER;
if (!gt_is_debugging()) return false;
// Create directory if it doersn't exist
\gt_create_data_directory("debug");
$file = fopen( \GT\GradeTracker::dataroot() . "/debug/{$USER->id}.txt" , 'a');
if ($file){
fwrite($file, "[".date('d-m-Y H:i:s')."] " . $info . "\n");
fclose($file);
}
return true;
}
/**
* Exit a script, optionally logging a debug message
* @param type $debug
*/
function gt_stop($debug){
if (strlen($debug) > 0){
\gt_debug($debug);
}
exit;
}
/**
* Loop through all the user's contexts and see if they have the given capability on any of them
* This saves passing a course id through every form/page.
* @param type $cap
* @param type $access If null, load the contexts
* @param type $thisContext If we want to check a specific course context
*/
function gt_has_capability($cap, $access = false, $thisContext = false, $user = false)
{
$GT = new \GT\GradeTracker();
// if we want to check a specific course context then use that
if ($thisContext)
{
return has_capability($cap, $thisContext);
}
// Otherwise check for capabilities across all our contexts
// (If we want to check against the contexts of a different user, use gt_has_user_capability())
if (!$access){
// If user is set with contexts already loaded, use those
if ($user && $user->getContextArray()){
$access = $user->getContextArray();
}
// Otherwise, if we have a user, but the contexts haven't been loaded yet
else {
$access = $GT->getUserContexts();
if ($user){
$user->setContextArray($access);
}
}
}
// Now use the access array of contexts
if ($access)
{
foreach($access as $context)
{
if (has_capability($cap, $context))
{
return true;
}
}
}
return false;
}
/**
* Do we have a particular capability in any of the contexts of the given user?
*
* For example, say we want to look at the student grid for student ID 1732
* For this we need capability view_student_grids, however we might have that capability for Course 1, and
* the student might be on Course 2 and Course 3, so whilst we have the capability we shouldn't be able to
* view them.
*
* Similarly, since they can be on multiple courses and so can we, we don't have a specific course id passed in
*
* So to find out if we have the capability for this user, we want to find all the courses the user is on.
* So in this case that will be Course 2 and Course 3 and get the contexts of those.
*
* Then we loop through those contexts and check to see if we have the view_stud_grid capability on any of those.
* We will only have the capability on that context if we are also on that course.
*
* @param type $cap
* @param type $userID
*/
function gt_has_user_capability($cap, $userID)
{
global $GT;
if (!isset($GT)){
$GT = new \GT\GradeTracker();
}
$contexts = $GT->getUserContexts($userID);
return gt_has_capability($cap, $contexts);
}
/**
* Get a success alert box
* @param type $text
* @param type $title
* @return string
*/
function gt_success_alert_box($text)
{
$output = "";
$output .= "<div class='gt_alert_good fade in'>";
if (is_array($text))
{
$output .= "<strong>".get_string('success', 'block_gradetracker')."</strong><br>";
foreach($text as $t)
{
$output .= "<span>{$t}</span><br>";
}
}
else
{
$output .= "<strong>".get_string('success', 'block_gradetracker')."</strong> ";
$output .= "<span>{$text}</span>";
}
$output .= "</div>";
return $output;
}
/**
* Get an error alert box
* @param type $text
* @param type $title
* @return string
*/
function gt_error_alert_box($text)
{
$output = "";
$output .= "<div class='gt_alert_bad fade in'>";
if (is_array($text))
{
$output .= "<strong>".get_string('errors', 'block_gradetracker')."</strong><br>";
foreach($text as $t)
{
if (is_array($t))
{
foreach($t as $t2)
{
$output .= "<span>{$t2}</span><br>";
}
}
else
{
$output .= "<span>{$t}</span><br>";
}
}
}
else
{
$output .= "<strong>".get_string('error', 'block_gradetracker')."</strong> ";
$output .= "<span>{$text}</span>";
}
$output .= "</div>";
return $output;
}
/**
* Get a success alert box
* @param type $text
* @param type $title
* @return string
*/
function gt_info_alert_box($text)
{
$output = "";
$output .= "<div class='gt_alert_info fade in'>";
if (is_array($text))
{
$output .= "<strong>".get_string('info', 'block_gradetracker')."</strong><br>";
foreach($text as $t)
{
$output .= "<span>{$t}</span><br>";
}
}
else
{
$output .= "<strong>".get_string('info', 'block_gradetracker')."</strong> ";
$output .= "<span>{$text}</span>";
}
$output .= "</div>";
return $output;
}
/**
* Get a success alert box
* @param type $text
* @param type $title
* @return string
*/
function gt_warning_alert_box($text)
{
$output = "";
$output .= "<div class='gt_alert_warning fade in'>";
if (is_array($text))
{
$output .= "<strong>".get_string('warning', 'block_gradetracker')."</strong><br>";
foreach($text as $t)
{
$output .= "<span>{$t}</span><br>";
}
}
else
{
$output .= "<strong>".get_string('warning', 'block_gradetracker')."</strong> ";
$output .= "<span>{$text}</span>";
}
$output .= "</div>";
return $output;
}
/**
* Create a random string
* @param type $length
* @return string
*/
function gt_rand_str($length)
{
$str = "987654321AaBbCcDdEeFfGgHhJjKkMmNnPpQqRrSsTtUuVvWwXxYyZz123456789";
$count = strlen($str) - 1;
$output = "";
for($i = 0; $i < $length; $i++)
{
$output .= $str[mt_rand(0, $count)];
}
return $output;
}
/**
* Create directory in Moodledata to store files
* Will create the directory: /moodledata/gradetracker/$dir
* Will attempt to create the parent directories if they don't exist yet
* Uses chmod of 0764:
* Owner: rwx,
* Group: rw,
* Public: r
* @param type $dir
*/
function gt_create_data_directory($dir)
{
global $CFG;
// Check for main plugin directory
if (!is_dir( \GT\GradeTracker::dataroot() )){
if (is_writeable($CFG->dataroot)){
if (!mkdir(\GT\GradeTracker::dataroot(), 0755, true)){
return false;
}
} else {
return false;
}
}
// Now try and make the actual dir we want
if (!is_dir( \GT\GradeTracker::dataroot() . '/' . $dir )){
if (is_writeable(\GT\GradeTracker::dataroot())){
if (!mkdir(\GT\GradeTracker::dataroot() . '/' . $dir, 0755, true)){
return false;
}
} else {
return false;
}
}
// If we got this far must be ok
return true;
}
/**
* For a given file path create a code we can use to download that file
* @global type $DB
* @param type $path
* @return type
*/
function gt_create_data_path_code($path){
global $DB;
// Strip the dataroot from the beginning.
$path = str_replace(\GT\GradeTracker::dataroot(), '', $path);
if (strpos($path, DIRECTORY_SEPARATOR) === 0) {
$path = ltrim($path, DIRECTORY_SEPARATOR);
}
// See if one already exists for this path
$record = $DB->get_record("bcgt_file_codes", array("path" => $path));
if ($record){
return $record->code;
}
// Create one
$code = gt_rand_str(10);
// Unlikely, but check if code has already been used
$cnt = $DB->count_records("bcgt_file_codes", array("code" => $code));
while ($cnt > 0)
{
$code = gt_rand_str(10);
$cnt = $DB->count_records("bcgt_file_codes", array("code" => $code));
}
$ins = new \stdClass();
$ins->path = $path;
$ins->code = $code;
$DB->insert_record("bcgt_file_codes", $ins);
return $code;
}
/**
* Get the code for a data path
* @global type $DB
* @param type $path
* @return boolean
*/
function gt_get_data_path_code($path)
{
global $DB;
// See if one already exists for this path
$record = $DB->get_record("bcgt_file_codes", array("path" => $path));
if ($record){
return $record->code;
}
return false;
}
/**
* Save a string to a file
* @param type $data
* @param type $file
*/
function gt_save_file_contents($data, $file){
// Create directory if it doersn't exist
\gt_create_data_directory("tmp/data");
$path = \GT\GradeTracker::dataroot() . "/tmp/data/{$file}";
$file = fopen( $path , 'a');
if ($file){
fwrite($file, $data);
fclose($file);
return $path;
} else {
return false;
}
}
/**
* Save a file to a given path within the gradetracker data directory
* @param type $file
* @param type $path
* @param type $name
* @param type $new Is it a new file we've uploaded or an existing one we are just moving?
*/
function gt_save_file($file, $path, $name, $new = true)
{
global $CFG;
// Remove any double slashes or backslashes
$path = preg_replace("/\\\\/", "/", $path);
$path = preg_replace("/\/{2,}/", "/", $path);
// Split into array and make sure each directory exists, creating it if it doesn't
$explode = explode("/", $path);
$cnt = count($explode);
if ($explode)
{
for ($i = 0; $i < $cnt; $i++)
{
$diff = $i - 1;
$checkPath = "";
for ($j = 0; $j <= $diff; $j++)
{
$checkPath .= $explode[$j] . "/";
}
$checkPath .= $explode[$i];
if (!\gt_create_data_directory($checkPath)){
return false;
}
}
}
if ($new){
return move_uploaded_file($file, \GT\GradeTracker::dataroot() . '/' . $path . '/' . $name);
} else {
return rename($file, \GT\GradeTracker::dataroot() . '/' . $path . '/' . $name);
}
}
/**
* Get the mime type of a file
* @param type $file
*/
function gt_get_file_mime_type($file)
{
$fInfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($fInfo, $file);
finfo_close($fInfo);
return $mime;
}
/**
* Check if a given mime type is an image
* @param type $mime
* @return type
*/
function gt_mime_type_is_image($mime)
{
return (in_array($mime, array('image/bmp', 'image/gif', 'image/jpeg', 'image/png', 'image/tiff', 'image/pjpeg')));
}
/**
* Convert to html entities
* @param type $txt
* @return type
*/
function gt_html($txt, $nl2br = false)
{
return ($nl2br) ? nl2br( htmlspecialchars($txt, ENT_QUOTES) ) : htmlspecialchars($txt, ENT_QUOTES);
}
function gt_html_undo($txt){
return html_entity_decode($txt, ENT_QUOTES);
}
/**
* Get the file extension from a file name
* @param type $filename
* @return type
*/
function gt_get_file_extension($filename)
{
$filename = strtolower($filename);
$exts = explode(".", $filename);
$n = count($exts) - 1;
$ext = $exts[$n];
return $ext;
}
/**
* Convert a max_filesize value to an int of bytes
* I'll be honest with you, I can't remember how this works, and looking at it I have no idea... But it does
* @param type $val e.g. 128M
* @return int e.g. ..
*/
function gt_get_bytes_from_upload_max_filesize($val)
{
$val = trim($val);
$last = strtolower($val[strlen($val)-1]);
switch($last) {
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
/**
* Convert a number of bytes into a human readable string
* @param type $bytes
* @param type $precision
* @return type
*/
function gt_convert_bytes_to_hr($bytes, $precision = 2)
{
$kilobyte = 1024;
$megabyte = $kilobyte * 1024;
$gigabyte = $megabyte * 1024;
$terabyte = $gigabyte * 1024;
if (($bytes >= 0) && ($bytes < $kilobyte)) {
return $bytes . ' B';
} elseif (($bytes >= $kilobyte) && ($bytes < $megabyte)) {
return round($bytes / $kilobyte, $precision) . ' KB';
} elseif (($bytes >= $megabyte) && ($bytes < $gigabyte)) {
return round($bytes / $megabyte, $precision) . ' MB';
} elseif (($bytes >= $gigabyte) && ($bytes < $terabyte)) {
return round($bytes / $gigabyte, $precision) . ' GB';
} elseif ($bytes >= $terabyte) {
return round($bytes / $terabyte, $precision) . ' TB';
} else {
return $bytes . ' B';
}
}
/**
* From an array of _FILES submitted, get a specific one
* @param type $file
* @param type $num
* @return type
*/
function gt_get_multidimensional_file($file, $num)
{
$return = array();
foreach($file as $key => $array)
{
$return[$key] = $array[$num];
}
return $return;
}
/**
* Strip anything that isn't alphanumeric, space, hyphen or fullstop
* @param type $txt
* @return type
*/
function gt_strip_chars($txt){
$txt = preg_replace("/[^a-z0-9\.\- ]/i", "", $txt);
return trim($txt);
}
/**
* Strip everything not alphanumeric
* @param type $txt
* @return type
*/
function gt_strip_chars_non_alpha($txt){
$txt = preg_replace("/[^a-z0-9]/i", "", $txt);
return trim($txt);
}
/**
* Make a string safe to use in a database query as a field
* @param type $str
* @return type
*/
function gt_make_db_field_safe($str, &$usedArray){
if (array_key_exists($str, $usedArray)){
return $usedArray[$str];
}
$txt = \gt_strip_chars_non_alpha($str);
$txt = strtolower($txt);
while (in_array($txt, $usedArray)){
$txt .= '_';
}
$usedArray[$str] = $txt;
return $usedArray[$str];
}
/**
* Check a string is empty or not, taking into account that empty() returns "0" as empty
* @param type $txt
* @return type
*/
function gt_is_empty($txt){
$txt = (string)$txt;
$txt = trim($txt);
return ($txt == "");
}
/**
* Get a role record by its id
* @param [type] $id [description]
* @return [type] [description]
*/
function gt_get_role($id){
global $DB;
return $DB->get_record("role", array("id" => $id));
}
/**
* Get a role record by its shortname
* @global type $DB
* @param type $shortname
* @return type
*/
function gt_get_role_by_shortname($shortname){
global $DB;
return $DB->get_record("role", array("shortname" => $shortname));
}
function gt_get_user_role_from_context($instanceID, $level, $userID){
global $DB;
$context = $DB->get_record("context", array("contextlevel" => $level, "instanceid" => $instanceID));
if (!$context) return false;
$role = $DB->get_record("role_assignments", array("userid" => $userID, "contextid" => $context->id));
if (!$role) return false;
return $role;
}
/**
* Create a string of question mark placeholders for SQL queries from an array of parameters
* Mostly used for when binding to an IN(?,?,?,?,?,etc...)
* @param type $params
* @return type
*/
function gt_create_sql_placeholders($params){
return implode(',', array_fill(0, count($params), '?'));
}
/**
* Convert an array of select, join, where, group, into an sql string
* @param type $sqlArray
* @return string
*/
function gt_convert_to_sql($sqlArray){
// Remove empty elements
$sqlArray['select'] = array_filter($sqlArray['select']);
$sqlArray['join'] = array_filter($sqlArray['join']);
$sqlArray['where'] = array_filter($sqlArray['where']);
$sql = "";
// Select
$sql .= "SELECT " . implode(", ", $sqlArray['select']) . " ";
// From
$sql .= "FROM " . $sqlArray['from'] . " ";
// Joins
foreach($sqlArray['join'] as $join)
{
$sql .= $join . " ";
}
// Where
$sql .= "WHERE " . implode(" AND ", $sqlArray['where']) . " ";
// Group
if (isset($sqlArray['group'])){
$sql .= "GROUP BY " . $sqlArray['group'] . " ";
}
// Order
if (isset($sqlArray['order'])){
$sql .= "ORDER BY " . $sqlArray['order'] . " ";
}
return $sql;
}
/**
* Get the depth of a multi-domensional array
* http://stackoverflow.com/questions/262891/is-there-a-way-to-find-out-how-deep-a-php-array-is
* @param array $array
* @return type
*/
function gt_get_array_depth(array $array) {
$max_depth = 1;
foreach ($array as $value) {
if (is_array($value)) {
$depth = gt_get_array_depth($value) + 1;
if ($depth > $max_depth) {
$max_depth = $depth;
}
}
}
return $max_depth;
}
/**
* Convert a multi-dimensional array to a single array
* http://stackoverflow.com/questions/6785355/convert-multidimensional-array-into-single-array
* @param type $array
* @return boolean
*/
function gt_flatten_array($array) {
if (!is_array($array)) return false;
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
// I have changed the array_merge because with an empty $result it overrides first key to 0
// $result = array_merge($result, gt_flatten_array($value));
$result = $result + \gt_flatten_array($value);
}
else {
$result[$key] = $value;
}
}
return $result;
}
/**
* Strip slashes from a string or an array of strings
* http://php.net/manual/en/function.stripslashes.php Example #2
* @param type $value
* @return type
*/
function gt_strip_slashes($value)
{
$value = is_array($value) ?
array_map('gt_strip_slashes', $value) :
stripslashes($value);
return $value;
}
/**
* Check if a string starts with specific characters
* @param type $haystack
* @param type $needle
* @return type
*/
function gt_str_starts($haystack, $needle)
{
if (is_array($needle))
{
$result = false;
foreach($needle as $str)
{
$length = strlen($str);
$strResult = ( strcasecmp(substr($haystack, 0, $length), $str) == 0 );
$result = ($result || $strResult);
}
return $result;
}
else
{
$length = strlen($needle);
return ( strcasecmp(substr($haystack, 0, $length), $needle) == 0 );
}
}
/**
* Check if a string ends with specific characters
* @param type $haystack
* @param type $needle
* @return type
*/
function gt_str_ends($haystack, $needle)
{
if (is_array($needle))
{
$result = false;
foreach($needle as $str)
{
$strResult = (strcasecmp(substr($haystack, -strlen($str)),$str) == 0);
$result = ($result || $strResult);
}
return $result;
}
else
{
return (strcasecmp(substr($haystack, -strlen($needle)),$needle) == 0);
}
}
/**
* Check if a string contains another string
* @param type $haystack
* @param type $needle
* @return type
*/
function gt_str_contains($haystack, $needle)
{
if (is_array($needle))
{
$result = false;
foreach($needle as $str)
{
$strResult = (stripos($haystack, $str) !== false);
$result = ($result || $strResult);