This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
global.php
1946 lines (1692 loc) · 54.2 KB
/
global.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
/** no direct access **/
defined('_WPLEXEC') or die('Restricted access');
/**
* WPL global library
* @author Howard <[email protected]>
* @since WPL1.0.0
* @package WPL
* @date 04/18/2013
*/
class wpl_global
{
/**
* Used for caching in check_addon function
* @static
* @var array
*/
public static $wpl_addons = array();
/**
* Use this function for cleaning any variable
* @author Howard <[email protected]>
* @static
* @param mixed $parameter
* @return mixed
*/
public static function clean($parameter)
{
$return_data = '';
if(is_array($parameter))
{
$return_data = array();
foreach($parameter as $key=>$value)
{
$return_data[$key] = wpl_global::clean($value);
}
}
else
{
$return_data = strip_tags($parameter);
}
return wpl_db::escape($return_data);
}
/**
* This is a function for loading view
* @author Howard <[email protected]>
* @static
* @param string $view
* @param string $query_string
* @param array $instance
* @param string $function
* @param boolean $overwrite
* @return string
*/
public static function load($view = 'property_listing', $query_string = '', $instance = array(), $function = NULL, $overwrite = false)
{
/** first validations **/
if(trim($query_string) == '') $query_string = wpl_global::get_wp_qvar('wpl_qs');
/** generate pages object **/
$controller = new wpl_controller();
$controller->parameter_overwrite = $overwrite;
if(!$function) $function = 'f:'.$view.':display';
/** call function **/
return call_user_func(array($controller, $function), $instance);
}
/**
* Returns Property Types
* @author Howard <[email protected]>
* @static
* @param int $property_type_id
* @param int $enabled
* @return mixed
*/
public static function get_property_types($property_type_id = '', $enabled = 1)
{
if(!trim($property_type_id))
{
$query = "SELECT * FROM `#__wpl_property_types` WHERE `parent` <> '0' AND `enabled`>='$enabled' AND `name`!='' ORDER BY `index` ASC";
return wpl_db::select($query, 'loadAssocList');
}
else
{
return wpl_db::get('*', 'wpl_property_types', 'id', $property_type_id);
}
}
/**
* Returns Listing Types
* @author Howard <[email protected]>
* @static
* @param int $listing_id
* @param int $enabled
* @return mixed
*/
public static function get_listings($listing_id = '', $enabled = 1)
{
if(!trim($listing_id))
{
$query = "SELECT * FROM `#__wpl_listing_types` WHERE `parent` <> '0' AND `enabled`>='$enabled' AND `name`!='' ORDER BY `index` ASC";
return wpl_db::select($query, 'loadAssocList');
}
else
{
return wpl_db::get('*', 'wpl_listing_types', 'id', $listing_id);
}
}
/**
* Returns Params
* @author Howard <[email protected]>
* @static
* @param string $table
* @param mixed $value
* @param string $params_field
* @param string $key
* @return array
*/
public static function get_params($table, $value, $params_field = 'params', $key = 'id')
{
if(trim($table) == '' or trim($value) == '') return array();
$params = wpl_db::get($params_field, $table, $key, $value);
return json_decode($params, true);
}
/**
* Sets Params
* @author Howard <[email protected]>
* @static
* @param string $table
* @param string $value
* @param array $values
* @param string $params_field
* @param string $key
* @return boolean
*/
public static function set_params($table, $value, $values = array(), $params_field = 'params', $key = 'id')
{
if(trim($table) == '' or trim($value) == '') return false;
$params = json_encode($values);
$query = "UPDATE `#__".$table."` SET `$params_field`='$params' WHERE `$key`='$value'";
return wpl_db::q($query, 'update');
}
/**
* Returns WPL menus
* @author Howard <[email protected]>
* @static
* @param string $type
* @param string $client
* @param int $enabled
* @param int $dashboard
* @return objects
*/
public static function get_menus($type = 'menu', $client = 'backend', $enabled = 1, $dashboard = 0)
{
$query = "SELECT * FROM `#__wpl_menus` WHERE `client`='$client' AND `type`='$type' AND `enabled`='$enabled' AND `dashboard`>='$dashboard' ORDER BY `index` ASC";
return wpl_db::select($query);
}
/**
* Remove any variable from Query String
* @author Howard <[email protected]>
* @static
* @param string $key
* @param string $url
* @return string
*/
public static function remove_qs_var($key, $url = '')
{
if(trim($url) == '') $url = self::get_full_url();
$url = preg_replace('/(.*)(\?|&)'.$key.'=[^&]+?(&)(.*)/i', '$1$2$4', $url .'&');
$url = substr($url, 0, -1);
return $url;
}
/**
* Adds any variable to Query String
* @author Howard <[email protected]>
* @static
* @param string $key
* @param string $value
* @param string $url
* @return string
*/
public static function add_qs_var($key, $value, $url = '')
{
if(trim($url) == '') $url = self::get_full_url();
$url = preg_replace('/(.*)(\?|&)'.$key.'=[^&]+?(&)(.*)/i', '$1$2$4', $url.'&');
$url = substr($url, 0, -1);
if(strpos($url, '?') === false)
return $url.'?'.$key.'='.$value;
else
return $url.'&'.$key.'='.$value;
}
/**
* Returns current full URL
* @author Howard <[email protected]>
* @static
* @return string
*/
public static function get_full_url()
{
/** get $_SERVER **/
$server = wpl_request::get('SERVER');
$page_url = 'http';
if(isset($server['HTTPS']) and $server['HTTPS'] == 'on') $page_url .= 's';
$site_domain = (isset($server['HTTP_HOST']) and trim($server['HTTP_HOST']) != '') ? $server['HTTP_HOST'] : $server['SERVER_NAME'];
$page_url .= '://';
$page_url .= $site_domain.$server['REQUEST_URI'];
return $page_url;
}
/**
* Creates order link used in tables for listing records
* @author Howard <[email protected]>
* @static
* @param string $thName
* @param string $orderBy
* @param boolean $class
* @param string $url
*/
public static function order_table($thName, $orderBy, $class = true, $url = '')
{
if(trim($url) == '') $url = self::get_full_url();
$qs_order = strtoupper(wpl_request::getVar('order'));
$qs_orderby = wpl_request::getVar('orderby');
$orderType = ($qs_orderby != $orderBy or ($qs_orderby == $orderBy and $qs_order == 'DESC')) ? 'ASC' : 'DESC';
if($qs_orderby == $orderBy and $class == true) $class = ($orderType == 'ASC') ? 'class="desc"' : 'class="asc"';
$url = self::add_qs_var('orderby', $orderBy, $url);
$url = self::add_qs_var('order', $orderType, $url);
echo '<a href="'.$url.'" '.$class.'>'.$thName.'</a>';
}
/**
* Returns full URL of assets
* @author Howard <[email protected]>
* @static
* @param string $asset relative path
* @return string full url address
*/
public static function get_wpl_asset_url($asset)
{
return plugins_url('assets/'.$asset, __FILE__);
}
/**
* Include a SVG image into the web page
* @author Howard <[email protected]>
* @static
* @param string $image relative path of svg image
* @param string $id desired ID for svg tag
* @param array $params
* @param string $path
* @return string
*/
public static function svg($image, $id = NULL, $params = array(), $path = NULL)
{
/** First validation **/
if(!trim($image)) return '';
if(!trim($path)) $path = wpl_global::get_wpl_root_path().'assets'.DS.'img'.DS;
if(strpos($image, '.svg') === false) $image .= '.svg';
$image = str_replace('/', DS, $image);
$fullpath = $path.(ltrim($image, DS));
$content = wpl_file::read($fullpath);
$pos1 = strpos($content, '<svg');
$pos2 = strpos($content, '</svg', $pos1)+6;
$svg = substr($content, $pos1, ($pos2-$pos1));
$pos1 = 0;
$pos2 = strpos($svg, '>', $pos1)+1;
$tag = substr($svg, $pos1, ($pos2-$pos1));
$remain_svg = substr($svg, $pos2);
/** Push array to params **/
$params['id'] = $id;
foreach($params as $key=>$value)
{
if($value and strpos($tag, $key.'="') !== false) $tag = preg_replace('/'.$key.'="(.*?)"/', $key.'="'.$value.'"', $tag);
elseif($value) $tag = str_replace('<svg', '<svg '.$key.'="'.$value.'"', $tag);
}
return $tag.$remain_svg;
}
/**
* Returns a JS template from template file
* @author Howard <[email protected]>
* @static
* @param string $template
* @param string $path
* @return string|boolean
*/
public static function load_js_template($template, $path = NULL)
{
/** First validation **/
if(!trim($template)) return '';
/** set default path **/
if(!trim($path)) $path = wpl_global::get_wpl_root_path().'assets'.DS.'js'.DS.'js_templates.tmpl';
$content = wpl_file::read($path);
$start = strpos($content, '<script id="'.$template.'"');
/** template not found **/
if($start === false) return false;
$end = strpos($content, '</script>', $start);
return substr($content, $start, (($end-$start)+9));
}
/**
* Returns WordPress option
* @author Howard <[email protected]>
* @static
* @param string $option_name
* @param mixed $default
* @return mixed
*/
public static function get_wp_option($option_name, $default = false)
{
return get_option($option_name, $default);
}
/**
* Returns WordPress Query var
* @author Howard <[email protected]>
* @static
* @param string $var_name
* @return mixed
*/
public static function get_wp_qvar($var_name = 'wpl_qs')
{
return get_query_var($var_name);
}
/**
* Returns WordPress URLs
* @author Howard <[email protected]>
* @static
* @param string $type
* @return string
*/
public static function get_wp_url($type = 'site')
{
/** make it lowercase **/
$type = strtolower($type);
if(in_array($type, array('frontend','site'))) $url = rtrim(home_url(), '/').'/';
elseif(in_array($type, array('backend','admin'))) $url = admin_url();
elseif($type == 'wordpress') $url = rtrim(get_site_url(), '/').'/';
elseif($type == 'wpl') $url = rtrim(plugins_url(), '/').'/'.WPL_BASENAME.'/';
elseif($type == 'upload') $url = rtrim(get_site_url(), '/').'/wp-content/uploads/WPL/';
elseif($type == 'content') $url = rtrim(content_url(), '/').'/';
elseif($type == 'plugin') $url = rtrim(plugins_url(), '/').'/';
elseif($type == 'include') $url = includes_url();
return $url;
}
/**
* Returns WordPress Root Path
* @author Howard <[email protected]>
* @static
* @return string
*/
public static function get_wp_root_path()
{
return ABSPATH;
}
/**
* Returns WPL Root Path
* @author Howard <[email protected]>
* @static
* @return string
*/
public static function get_wpl_root_path()
{
return WPL_ABSPATH;
}
/**
* Returns WordPress frontend URL
* @author Howard <[email protected]>
* @static
* @return string
*/
public static function get_wp_site_url()
{
return self::get_wp_url('site');
}
/**
* Returns WordPress backend URL
* @author Howard <[email protected]>
* @static
* @return string
*/
public static function get_wp_admin_url()
{
return self::get_wp_url('admin');
}
/**
* Returns WPL backend URL
* @author Howard <[email protected]>
* @static
* @return string
*/
public static function get_wpl_url()
{
return self::get_wp_url('WPL');
}
/**
* Returns WordPress installation URL
* @author Howard <[email protected]>
* @static
* @return string
*/
public static function get_wordpress_url()
{
return self::get_wp_url('wordpress');
}
/**
* Use wpl_global::get_upload_base_url instead
* @author Howard <[email protected]>
* @deprecated since version 1.8.3
* @return string
*/
public static function get_wpl_upload_url()
{
return self::get_upload_base_url();
}
/**
* Returns WordPress backend menu URL
* @author Howard <[email protected]>
* @static
* @param string $admin_menu_slug
* @return string
*/
public static function get_wpl_admin_menu($admin_menu_slug)
{
$admin_url = self::get_wp_url('admin');
return $admin_url.'admin.php?page='.$admin_menu_slug;
}
/**
* Returns icons by a path
* @author Martin <[email protected]>
* @static
* @param string $path
* @param string $regex
* @return array
*/
public static function get_icons($path, $regex = '.png$|.gif$|.jpg$|.jpeg$')
{
return wpl_folder::files($path, $regex, false, false);
}
/**
* Check if user has a role or not
* @author Howard <[email protected]>
* @static
* @param string $role
* @param int $user_id
* @return boolean
*/
public static function has_permission($role = 'guest', $user_id = '')
{
/** get user id **/
if(trim($user_id) == '') $user_id = wpl_users::get_cur_user_id();
/** get all roles **/
$roles = wpl_users::get_wpl_roles();
/** role validation **/
if(!in_array($role, $roles)) $role = 'guest';
$user_role = wpl_users::get_role($user_id);
$user_role_point = wpl_users::get_role_point($user_role);
$role_point = wpl_users::get_role_point($role);
/** return true if user has access **/
if($user_role_point >= $role_point) return true;
return false;
}
/**
* Blocks user access based on WPL user role
* @author Howard <[email protected]>
* @static
* @param string $role
* @param int $user_id
*/
public static function min_access($role = 'guest', $user_id = '')
{
/** apply filters **/
_wpl_import('libraries.filters');
@extract(wpl_filters::apply('wpl_check_min_access', array('role'=>$role, 'user_id'=>$user_id)));
if(!wpl_global::has_permission($role, $user_id))
{
echo __("You don't have access to this page!", WPL_TEXTDOMAIN);
exit;
}
}
/**
* Makes an array accessible by ID
* @author Howard <[email protected]>
* @static
* @param array|object $inputs
* @param boolean $is_object
* @return array|object
*/
public static function return_in_id_array($inputs, $is_object = false)
{
$return = array();
foreach($inputs as $input)
{
if(!$is_object) $return[$input['id']] = $input;
else $return[$input->id] = $input;
}
return $return;
}
/**
* Check user access to a certain section
* @author Howard <[email protected]>
* @static
* @param string $access
* @param int $user_id
* @return int
*/
public static function check_access($access, $user_id = '')
{
if($access == '') return 1000;
/** get current user id **/
if(!trim($user_id)) $user_id = wpl_users::get_cur_user_id();
/** return admin access **/
if(wpl_users::is_administrator($user_id)) return 1000;
if(!trim($user_id) or !wpl_users::is_wpl_user($user_id)) $query = "SELECT `access_".$access."` FROM `#__wpl_users` WHERE `id`='-2'";
else $query = "SELECT `access_".$access."` FROM `#__wpl_users` WHERE `id`='$user_id'";
$result = wpl_db::select($query, 'loadResult');
if($result == '') return 0;
return $result;
}
/**
* Converts integer to string
* @author Howard <[email protected]>
* @static
* @param int $x
* @return string
*/
public static function number_to_word($x)
{
$nwords = array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", 30=>"thirty", 40=>"forty", 50=>"fifty", 60=>"sixty", 70=>"seventy", 80=>"eighty", 90=>"ninety");
if(!is_numeric($x))
$w = '#';
elseif(fmod($x, 1) != 0)
$w = '#';
else
{
if($x < 0)
{
$w = 'minus ';
$x = -$x;
}
else
$w = '';
if($x < 21)
$w .= $nwords[$x];
elseif($x < 100)
{
$w .= $nwords[10 * floor($x/10)];
$r = fmod($x, 10);
if($r > 0)
$w .= '-'. $nwords[$r];
}
elseif($x < 1000)
{
$w .= $nwords[floor($x/100)] .' hundred';
$r = fmod($x, 100);
if($r > 0)
$w .= ' and '. self::number_to_word($r);
}
elseif($x < 1000000)
{
$w .= self::number_to_word(floor($x/1000)) .' thousand';
$r = fmod($x, 1000);
if($r > 0)
{
$w .= ' ';
if($r < 100)
$w .= 'and ';
$w .= self::number_to_word($r);
}
}
else
{
$w .= self::number_to_word(floor($x/1000000)) .' million';
$r = fmod($x, 1000000);
if($r > 0)
{
$w .= ' ';
if($r < 100)
$word .= 'and ';
$w .= self::number_to_word($r);
}
}
}
return $w;
}
/**
* Get field type by table column
* @author Howard <[email protected]>
* @static
* @param string $table
* @param string $column
* @return string
*/
public static function get_db_field_type($table, $column)
{
$query = "DESCRIBE `#__$table` `$column`";
$result = wpl_db::q($query, 'select');
return $result[$column]->Type;
}
/**
* Sorts internal arrays by their [sort] value
* @author Marvin <[email protected]>
* @static
* @param array $a
* @param array $b
* @return int
*/
public static function wpl_array_sort($a, $b)
{
if(isset($a['sort']) and isset($b['sort']) and $a['sort'] > $b['sort']) return 1;
elseif(isset($a['sort']) and isset($b['sort']) and $a['sort'] < $b['sort']) return -1;
elseif(isset($a['sort']) and isset($b['sort']) and $a['sort'] == $b['sort']) return 0;
}
/**
* Returns website client
* @author Howard <[email protected]>
* @static
* @return int
*/
public static function get_client()
{
if(is_admin()) return 1; # backend
else return 0; # frontend
}
/**
* Use this function for troubleshooting. Don't forgot to remove it after debugging
* @author Howard <[email protected]>
* @static
* @param string $log_msg
* @param string $path
* @param boolean $append
*/
public static function trouble_shooting_log($log_msg, $path = '', $append = false)
{
if(trim($path) == '') $path = WPL_ABSPATH. 'libraries' .DS. 'troubleshooting.txt';
if(wpl_file::exists($path) and !$append) wpl_file::delete($path);
wpl_file::write($path, $log_msg, $append);
}
/**
* Triggers an event
* @author Howard <[email protected]>
* @static
* @param string $trigger
* @param array $params
*/
public static function event_handler($trigger, $params = array())
{
/** import library **/
_wpl_import('libraries.events');
/** trigger event **/
wpl_events::trigger($trigger, $params);
}
/**
* Returns couple of WPL settings based on category
* @author Howard <[email protected]>
* @static
* @param int|string $category
* @param int $showable
* @param boolean $return_records
* @return array
*/
public static function get_settings($category = '', $showable = 0, $return_records = false)
{
/** import library **/
_wpl_import('libraries.settings');
return wpl_settings::get_settings($category, $showable, $return_records);
}
/**
* Returns one WPL setting value
* @author Howard <[email protected]>
* @static
* @param string $setting_name
* @param int|string $category
* @return mixed
*/
public static function get_setting($setting_name, $category = '')
{
/** import library **/
_wpl_import('libraries.settings');
return wpl_settings::get($setting_name, $category);
}
/**
* Imports activities by activity name
* @author Howard <[email protected]>
* @static
* @param string $activity
* @param int $activity_id
* @param mixed $params
*/
public static function import_activity($activity, $activity_id = 0, $params = false)
{
/** import library **/
_wpl_import('libraries.activities');
$wpl_activity = new wpl_activity();
$wpl_activity->import($activity, $activity_id, $params);
}
/**
* Wrapper for WordPress get_pages function
* @author Howard <[email protected]>
* @static
* @param array $args
* @return array
*/
public static function get_wp_pages($args = array())
{
return get_pages($args);
}
/**
* Returns WPL version
* @author Howard <[email protected]>
* @static
* @return string
*/
public static function wpl_version()
{
return WPL_VERSION;
}
/**
* Returns WordPress version
* @author Howard <[email protected]>
* @static
* @global string $wp_version
* @return string
*/
public static function wp_version()
{
global $wp_version;
return $wp_version;
}
/**
* Returns PHP version
* @author Howard <[email protected]>
* @static
* @return string
*/
public static function php_version()
{
return phpversion();
}
/**
* Returns WPL tmp path
* @author Howard <[email protected]>
* @static
* @return type
*/
public static function get_tmp_path()
{
return WPL_ABSPATH.'assets'.DS.'tmp'.DS;
}
/**
* Initialized a tmp directory and returns its name
* @author Howard <[email protected]>
* @static
* @return string
*/
public static function init_tmp_folder()
{
$path = wpl_global::get_tmp_path();
$directory = $path.'tmp_'.md5(microtime(true)).DS;
/** create folder **/
wpl_folder::create($directory, 0777);
return $directory;
}
/**
* Removes expired tmp directories. It calls by WPL cronjobs
* @author Howard <[email protected]>
* @static
*/
public static function delete_expired_tmp()
{
$path = wpl_global::get_tmp_path();
$folders = wpl_folder::folders($path);
$now = time();
$_3days = 259200; #3days in seconds
foreach($folders as $folder)
{
$full_path = $path.$folder;
$latest_modification_time = filemtime($full_path);
if(($now - $latest_modification_time) > $_3days) wpl_folder::delete($full_path);
}
}
/**
* Uploads a file and return the results
* @author Howard <[email protected]>
* @static
* @param array $file
* @param string $dest
* @param array $ext_array
* @param int $max_file_size
* @param string $extension
* @return array
*/
public static function upload($file, $dest = '', $ext_array = array('jpg','png','gif','jpeg'), $max_file_size = 512000, $extension = NULL)
{
$error = '';
$msg = '';
if((!empty($file['error'])) or (empty($file['tmp_name']) or $file['tmp_name'] == 'none'))
{
$error .= __('An error occurred uploading your file!', WPL_TEXTDOMAIN);
}
else
{
if(is_null($extension)) $extension = strtolower(wpl_file::getExt($file['name']));
if(!in_array($extension, $ext_array))
{
$error .= __('File extension is not valid.', WPL_TEXTDOMAIN);
}
/** check the file size **/
$filesize = @filesize($file['tmp_name']);
if($filesize > $max_file_size)
{
$error .= __('File size is not valid!', WPL_TEXTDOMAIN);
@unlink($file);
}
/** upload file **/
if($error == '') wpl_file::upload($file['tmp_name'], $dest);
}
$message = '';
return array('error'=>$error, 'message'=>$message);
}
/**
* Extract a Zip file
* @author Howard <[email protected]>
* @static
* @param string $file full path
* @param string $dest full path
* @return boolean
*/
public static function zip_extract($file, $dest)
{
/** return false if ZipArchive class doesn't exists **/
if(!class_exists('ZipArchive')) return false;
$zip = new ZipArchive;
if($zip->open($file) === true)
{
$zip->extractTo($dest);
$zip->close();
return true;
}
return false;
}
/**
* Extract a GZip file
* @author Howard <[email protected]>
* @static
* @param string $file full path
* @param string $dest
* @return string destination full path
*/
public static function gzip_extract($file, $dest = NULL)
{
if(!$dest)
{
$ex = explode('/', $file);
$file_name = end($ex);
$out_file_name = str_replace('.gz', '', $file_name);
$dest = str_replace($file_name, $out_file_name, $file);
}
$buffer_size = 4096;
$fh = gzopen($file, 'rb');
$ofh = fopen($dest, 'wb');
while(!gzeof($fh))
{
fwrite($ofh, gzread($fh, $buffer_size));
}
fclose($ofh);
gzclose($fh);
return $dest;
}
/**
* Run a SQL Query file
* @author Howard <[email protected]>
* @static
* @param string $sql_file
* @param boolean $delete
* @param boolean $exception
* @return boolean
*/
public static function do_file_queries($sql_file, $delete = false, $exception = false)
{
if(!wpl_file::exists($sql_file)) return false;
$read_file = wpl_file::read($sql_file);