-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.php
2448 lines (2199 loc) · 73.2 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
/**
* General functions used in this project
*
* @package Social Ninja
* @version 1.6
* @author InspiredDev <[email protected]>
* @copyright 2015
*/
/**
* Function to connect to mysql
*/
function sql_conn()
{
global $mysqli;
if($mysqli)@mysqli_close($mysqli);
$mysqli = @mysqli_connect(DB_HOST, DB_USER, DB_PASS);
if(!$mysqli)return false;
$db = @mysqli_select_db($mysqli, DB_NAME);
if(!$db)return false;
$charset = @mysqli_set_charset($mysqli, 'utf8mb4');
if(!$charset)$charset = @mysqli_set_charset($mysqli, 'utf8');
if(!$charset)return false;
return $mysqli;
}
/**
* Mysql functions
*/
function sql_connect($db_host, $db_user, $db_pwd)
{
return @mysqli_connect($db_host, $db_user, $db_pwd);
}
function sql_connect_error(){
return mysqli_connect_error();
}
function sql_select_db($db_name)
{
global $mysqli;
return @mysqli_select_db($mysqli, $db_name);
}
function sql_num_rows($resource)
{
global $mysqli;
return mysqli_num_rows($resource);
}
function sql_query($query)
{
global $mysqli;
return mysqli_query($mysqli, $query);
}
function sql_fetch_assoc($resource)
{
global $mysqli;
return mysqli_fetch_assoc($resource);
}
function sql_fetch_row($resource)
{
global $mysqli;
return mysqli_fetch_row($resource);
}
function sql_affected_rows()
{
global $mysqli;
return mysqli_affected_rows($mysqli);
}
function sql_error()
{
global $mysqli;
return mysqli_error($mysqli);
}
function sql_escape_string($string)
{
global $mysqli;
return mysqli_escape_string($mysqli, $string);
}
function sql_real_escape_string($string)
{
global $mysqli;
return mysqli_real_escape_string($mysqli, $string);
}
function sql_insert_id()
{
global $mysqli;
return mysqli_insert_id($mysqli);
}
function sql_close()
{
global $mysqli;
return mysqli_close($mysqli);
}
function sql_data_seek($result, $offset)
{
return mysqli_data_seek($result, $offset);
}
/**
* Function to output ajax json response
* $response is an array holding request response
*/
function output()
{
global $response;
echo json_encode($response);
@sql_close();
exit;
}
/**
* Function to load site settings
*/
function load_settings()
{
$settings = array();
$settings = sql_fetch_assoc(sql_query("SELECT * FROM global_config"));
return $settings;
}
/**
* Function to load app settings
* @param string $user_id load users settings
*/
function load_app_settings($user_id)
{
global $settings;
if(empty($settings['fb_scope'])){
$scope = 'email,manage_pages,publish_actions,publish_pages,read_insights,user_managed_groups,user_posts,user_events,user_photos,user_videos,user_groups';
$settings['fb_scope'] = $scope;
sql_query("UPDATE global_config SET fb_scope = '$scope'");
}
list($fb_app, $tw_app, $yt_app) = sql_fetch_row(sql_query("SELECT fb_app_config, tw_app_config, yt_app_config FROM users WHERE user_id = '$user_id'"));
if(!empty($fb_app)){
$fb_app = json_decode($fb_app, true);
$settings['fb_app_id'] = $fb_app['fb_app_id'];
$settings['fb_app_secret'] = $fb_app['fb_app_secret'];
$settings['fb_app_token'] = $fb_app['fb_app_token'];
}
if(!empty($tw_app)){
$tw_app = json_decode($tw_app, true);
$settings['tw_app_id'] = $tw_app['tw_app_id'];
$settings['tw_app_secret'] = $tw_app['tw_app_secret'];
}
if(!empty($yt_app)){
$yt_app = json_decode($yt_app, true);
$settings['yt_client_id'] = $yt_app['yt_client_id'];
$settings['yt_client_secret'] = $yt_app['yt_client_secret'];
$settings['yt_dev_token'] = $yt_app['yt_dev_token'];
}
/**
* Check if any app save is pending
*/
if(!empty($_SESSION['pending_fb_app'])){
$fb_app = json_decode($_SESSION['pending_fb_app'], true);
$settings['fb_app_id'] = $fb_app['fb_app_id'];
$settings['fb_app_secret'] = $fb_app['fb_app_secret'];
$settings['fb_app_token'] = $fb_app['fb_app_token'];
}
if(!empty($_SESSION['pending_tw_app'])){
$tw_app = json_decode($_SESSION['pending_tw_app'], true);
$settings['tw_app_id'] = $tw_app['tw_app_id'];
$settings['tw_app_secret'] = $tw_app['tw_app_secret'];
}
if(!empty($_SESSION['pending_yt_app'])){
$yt_app = json_decode($_SESSION['pending_yt_app'], true);
$settings['yt_client_id'] = $yt_app['yt_client_id'];
$settings['yt_client_secret'] = $yt_app['yt_client_secret'];
$settings['yt_dev_token'] = $yt_app['yt_dev_token'];
}
}
/**
* Function to autoload classes
* @param string $class class name
*/
function spl_autoloader($class)
{
if(file_exists(dirname(__FILE__)."/classes/{$class}.class.php")){
include(dirname(__FILE__)."/classes/{$class}.class.php");
}
}
/**
* Function to generate pagination html
* @param int $count is the total record count
* @param int $rows is the number of rows per page
* @param int $from is the start index of pagination
* @param string $qu is the query string i.e $_GET variables
* @param string $url is the url for pagination. Default url is PHP_SELF
* @return string pagination html
*/
function pagination($count, $rows, $from, $qu, $url = '')
{
$html = '';
if(empty($url))$url = $_SERVER['PHP_SELF'];
if(!preg_match('/\?/', $url)){
$url .= '?';
$url = rtrim($url, '&');
}
else $url .= '&';
//$from++;
$qu = preg_replace("/from=(\d+)/","",$qu);
$qu = preg_replace("/^\&/","",$qu);
$qu = preg_replace('/\&{2,}/', '&', $qu);
$html .= '<div class="text-center"><ul class="pagination pagination-sm">';
if(($from-$rows) >= 0)$html .= '<li class="prev"><a href="'.$url.'from='.($from-$rows).($qu ? '&'.$qu : '').'" class="pagina">← Previous</a></li>';
$index = (int)($count/$rows);
if($count%$rows)$index++;
for($i = 1 ; $i <= $index ; $i++){
$current_page = (int)($from/$rows)+1;
if($index > 20 && $current_page > 10 && $i == 3){
$html .= "<li><span class='dots'>...</span></li>";$i=$current_page-5;
}
if($index > 20 && ($index - $current_page) > 10 && $i == ($current_page + 5)){
$html .= "<li><span class='dots'>...</span></li>";$i=$index-2;
}
if($i == (int)($from/$rows) +1 )$html .= '<li class="active"><span>'.$i.'</span></li>';
else $html .= '<li><a href="'.$url.'from='.(($i-1)*$rows+1).($qu ? '&'.$qu : '').'" class="pagina">'.$i.'</a></li>';
}
if(($from + $rows) <= $count)$html .= '<li class="next"><a href="'.$url.'from='.($from+$rows).($qu ? '&'.$qu : '').'" class="pagina">Next → </a></li>';
$html .= '</ul></div>';
return $html;
}
/**
* Function to generate seo friendly url
* @param string $url is the input url
* @return string seo friendly url
*/
function makeuri($url, $real = 0)
{
global $settings;
if($settings['seo_url']){
if(preg_match('/\.js|\.css|\.png|\.jpg|\.gif/i', $url))$url = $url;
else if(preg_match('/dashboard\.php\?show=(.*)/i', $url, $m))$url = 'dashboard/'.$m[1].'/';
else if(preg_match('/login\.php\?type=(.*)/i', $url, $m))$url = 'login/'.$m[1].'/';
else if(preg_match('/log\.php\?site=(.*)/i', $url, $m))$url = 'log/'.$m[1].'/';
else $url = str_replace('.php', '/', $url);
}
if(!$real)return site_url().'/'.$url;
else return $url;
}
/**
* Function to redirect to seo friendly url
* @param string $url is the input url
*/
function redirect($url)
{
header('location: '.site_url().'/'.$url);
exit();
}
/**
* Function to display error message in case of fatal errors
* @param string $message is the error message to show
* @param bool $full_page indicates whether to include full page with header and footer
*/
function display_error($message, $full_page = true)
{
global $settings, $user_data, $is_logged_in, $title, $lang, $is_index_page, $is_login_page;
if($full_page)include(__ROOT__.'/templates/header.php');
include(__ROOT__.'/templates/error.php');
if($full_page)include(__ROOT__.'/templates/footer.php');
}
/**
* Function to get site parameters
* @param string $site is the site name
* @return array $table->usertable $col->usercol $uname->username $name->namecol $social_id->owner social id column
*/
function get_site_params($site)
{
if($site == 'facebook' || $site == 'fbprofile'){
$table = 'fb_accounts';
$col = 'fb_id';
$uname = 'fb_id';
$name = 'first_name';
$social_id = 'fb_id';
}
else if($site == 'fbpage'){
$table = 'fb_pages';
$col = 'page_id';
$uname = 'page_id';
$name = 'page_name';
$social_id = 'fb_id';
}
else if($site == 'fbgroup'){
$table = 'fb_groups';
$col = 'group_id';
$uname = 'group_id';
$name = 'group_name';
$social_id = 'fb_id';
}
else if($site == 'fbevent'){
$table = 'fb_events';
$col = 'event_id';
$uname = 'event_id';
$name = 'event_name';
$social_id = 'fb_id';
}
else if($site == 'twitter'){
$table = 'tw_accounts';
$col = 'tw_id';
$uname = 'tw_username';
$name = 'tw_username';
$social_id = 'tw_id';
}
else if($site == 'youtube'){
$table = 'yt_accounts';
$col = 'yt_id';
$uname = 'yt_username';
$name = 'first_name';
$social_id = 'yt_id';
}
return @array($table, $col, $uname, $name, $social_id);
}
/**
* Function to get link from id
* @param string $id
* @param string $site
* @return string $url
*/
function get_link_from_id($id, $site)
{
if(preg_match('/^fb/', $site)){
return 'https://www.facebook.com/'.$id;
}
else if($site == 'twitter'){
return 'https://www.twitter.com/'.$id;
}
else if($site == 'youtube'){
return 'https://www.youtube.com/channel/'.$id;
}
}
/**
* Function to print social ids
* @param array $user_data is the user_data array
* @return string $html to print
*/
function print_social_ids($site)
{
global $user_id, $lang;
list($table, $col, $uname) = get_site_params($site);
$html = '';
$q = sql_query("SELECT $table.*, token_expiry.user_id AS token_expired FROM $table
LEFT JOIN token_expiry ON token_expiry.user_id = '$user_id' AND token_expiry.social_id = $table.$col AND token_expiry.site = '$site'
WHERE $table.user_id = '$user_id'");
if(!sql_num_rows($q)){
$html .= '<div class="alert alert-info">'.$lang['common'][1].'</div>';
}else{
$i = 0;
while($account = sql_fetch_assoc($q)){
$name = $account['first_name'].' '.$account['last_name'];
if($account['account_status'] != 1)$name = '<strike>'.$name.'</strike>';
$pic = get_medium_pp($account['profile_pic'], $site);
$url = get_profile_url($account[$col], @$account[$uname], $site);
if(!$i)$html .= '<div class="row">';
$html .= '<div class="col-lg-4 social_id" rel="'.$account[$col].'" rel-site="'.$site.'">
<div class="row">
<div class="col-lg-5">
<img src="'.$pic.'" width="100px"/>
</div>
<div class="col-lg-7">
<h4>
<a href="'.$url.'" target="_blank">
'.$name.'
</a><br/><br/>
<button class="btn btn-sm btn-info del_social_id">
'.$lang['common'][0].'
</button>
<a class="btn btn-sm btn-success" href="'.makeuri( 'sync.php?'.$col.'='.$account[$col] ).'">
'.ucwords( $lang['index'][41] ).'
</a>
'.(empty($account['token_expired']) ? '' : '<br/><span style="color:red; font-weight:bold">'.$lang['common'][2].'</span>').'
</h4>
</div>
</div>
</div>';
$i++;
if($i >= 3){
$html .= '</div>';
$i = 0;
}
}
if($i)$html .= '</div>';
}
return $html;
}
/**
* Function to get medium sized pp from database saved url
* @param string $url is the url of pp saved in database
* @param string $site is the site
* @return string $url is the medium sized pp
*/
function get_medium_pp($url, $site)
{
if($site == 'facebook' || $site == 'fbprofile')return $url .= 'normal';
else if($site == 'twitter')return str_replace('_normal', '_200x200', $url);
else if($site == 'youtube')return $url;
return $url .= 'normal';
}
/**
* Function to get profile url from user id or username
* @param string $id is the id
* @param string $username is the username
* @param string $site is the site
* @return string $url
*/
function get_profile_url($id, $username ,$site)
{
if($site == 'twitter')return 'https://twitter.com/'.$username;
else if($site == 'youtube')return 'https://youtube.com/channel/'.$username;
else return 'https://www.facebook.com/'.$id;
}
/**
* Function to check if a folder exists
* @param int $folder_id folder id
* @return int $result
*/
function folder_exists($folder_id)
{
return sql_num_rows(sql_query("SELECT NULL FROM folders WHERE folder_id = '$folder_id' LIMIT 1"));
}
/**
* Function to get folder details
* @param int $folder_id folder id
* @return array $result
*/
function folder_details($folder_id)
{
return sql_fetch_assoc(sql_query("SELECT * FROM folders WHERE folder_id = '$folder_id' LIMIT 1"));
}
/**
* Function to get file details
* @param int $file_id folder id
* @return array $result
*/
function file_details($file_id)
{
return sql_fetch_assoc(sql_query("SELECT * FROM files WHERE file_id = '$file_id' LIMIT 1"));
}
/**
* Function to get tool details
* @param int $tool_id tool_id
* @return array $result
*/
function tool_details($tool_id)
{
return sql_fetch_assoc(sql_query("SELECT * FROM creator_tools WHERE tool_id = '$tool_id' LIMIT 1"));
}
/**
* Function to get files count
* @param int $folder_id folder id
* @return int $result
*/
function get_folder_file_count($folder_id, $name = '', $type = '')
{
$search = ' 1 ';
if(!empty($name))$search .= " AND ( original_name LIKE '%$name%' OR file_id = '$name' ) ";
if(!empty($type))$search .= " AND file_type = '$type' ";
return sql_num_rows(sql_query("SELECT NULL FROM files WHERE folder_id = '$folder_id' AND $search"));
}
/**
* Function to get files from
* @param int $folder_id folder id
* @return array $result
*/
function get_folder_files($folder_id, $from = 1, $rows = 100, $name = '', $type = '')
{
$files = array();
$from--;
$search = ' 1 ';
if(!empty($name))$search .= " AND ( original_name LIKE '%$name%' OR file_id = '$name' ) ";
if(!empty($type))$search .= " AND file_type = '$type' ";
$q = sql_query("SELECT * FROM files WHERE folder_id = '$folder_id' AND $search ORDER BY position ASC LIMIT $from, $rows");
while($res = sql_fetch_assoc($q))$files[] = $res;
return $files;
}
/**
* Function to purify text from script injection
* @param string $text
* @return string $result
*/
function purify_text($text)
{
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}
/**
* Function to delete file from server
* @param string $file
* @param string $storage storage path to file
* @return bool $result
*/
function delete_file($file, $storage, $folder_id, $file_id, $file_type)
{
if($file_type != 'text'){
$path = __ROOT__.'/storage/'.$storage.'/'.$file;
$f = sql_real_escape_string($file);
$new_file = '';
list($fid) = sql_fetch_row(sql_query("SELECT folder_id FROM folders WHERE thumb LIKE '%$f%'"));
if(!empty($fid)){
list($new_file, $file_type) = sql_fetch_row(sql_query("SELECT filename, file_type FROM files WHERE folder_id = '$fid' AND filename != '$f'"));
if(empty($new_file))$new_file = '';
else $new_file = $storage.'/'.$new_file;
if($file_type == 'video')$new_file .= '.png';
$new_file = sql_real_escape_string($new_file);
}
sql_query("UPDATE folders SET thumb = '$new_file' WHERE folder_id = '$fid'");
@unlink($path);
@unlink($path.'.png');
/**
* Delete all watermarked tmp files
*/
$l = glob(dirname(__FILE__).'/tmp/'.$file.'*');
foreach($l as $ll)@unlink($ll);
}
sql_query("UPDATE folders SET file_count = file_count - 1 WHERE folder_id = '$folder_id' AND file_count >= 1");
sql_query("DELETE FROM files WHERE file_id = '$file_id'");
sql_query("DELETE FROM file_meta WHERE file_id = '$file_id'");
}
/**
* Function to get time zone lists
* @return array $result
*/
function get_time_zones()
{
return
$timezones = array(
'Pacific/Midway' => "(GMT-11:00) Midway Island",
'US/Samoa' => "(GMT-11:00) Samoa",
'US/Hawaii' => "(GMT-10:00) Hawaii",
'US/Alaska' => "(GMT-09:00) Alaska",
'US/Pacific' => "(GMT-08:00) Pacific Time (US & Canada)",
'America/Tijuana' => "(GMT-08:00) Tijuana",
'US/Arizona' => "(GMT-07:00) Arizona",
'US/Mountain' => "(GMT-07:00) Mountain Time (US & Canada)",
'America/Chihuahua' => "(GMT-07:00) Chihuahua",
'America/Mazatlan' => "(GMT-07:00) Mazatlan",
'America/Mexico_City' => "(GMT-06:00) Mexico City",
'America/Monterrey' => "(GMT-06:00) Monterrey",
'US/Central' => "(GMT-06:00) Central Time (US & Canada)",
'US/Eastern' => "(GMT-05:00) Eastern Time (US & Canada)",
'US/East-Indiana' => "(GMT-05:00) Indiana (East)",
'America/Bogota' => "(GMT-05:00) Bogota",
'America/Lima' => "(GMT-05:00) Lima",
'America/Caracas' => "(GMT-04:30) Caracas",
'Canada/Atlantic' => "(GMT-04:00) Atlantic Time (Canada)",
'America/La_Paz' => "(GMT-04:00) La Paz",
'America/Santiago' => "(GMT-04:00) Santiago",
'Canada/Newfoundland' => "(GMT-03:30) Newfoundland",
'America/Sao_Paulo' => "(GMT-03:00) Brasilia",
'America/Buenos_Aires' => "(GMT-03:00) Buenos Aires",
'Greenland' => "(GMT-03:00) Greenland",
'Atlantic/Stanley' => "(GMT-02:00) Stanley",
'Atlantic/Azores' => "(GMT-01:00) Azores",
'Atlantic/Cape_Verde' => "(GMT-01:00) Cape Verde Is.",
'Africa/Casablanca' => "(GMT) Casablanca",
'Europe/Dublin' => "(GMT) Dublin",
'Europe/Lisbon' => "(GMT) Lisbon",
'Europe/London' => "(GMT) London",
'Africa/Monrovia' => "(GMT) Monrovia",
'Europe/Amsterdam' => "(GMT+01:00) Amsterdam",
'Europe/Belgrade' => "(GMT+01:00) Belgrade",
'Europe/Berlin' => "(GMT+01:00) Berlin",
'Europe/Bratislava' => "(GMT+01:00) Bratislava",
'Europe/Brussels' => "(GMT+01:00) Brussels",
'Europe/Budapest' => "(GMT+01:00) Budapest",
'Europe/Copenhagen' => "(GMT+01:00) Copenhagen",
'Europe/Ljubljana' => "(GMT+01:00) Ljubljana",
'Europe/Madrid' => "(GMT+01:00) Madrid",
'Europe/Paris' => "(GMT+01:00) Paris",
'Europe/Prague' => "(GMT+01:00) Prague",
'Europe/Rome' => "(GMT+01:00) Rome",
'Europe/Sarajevo' => "(GMT+01:00) Sarajevo",
'Europe/Skopje' => "(GMT+01:00) Skopje",
'Europe/Stockholm' => "(GMT+01:00) Stockholm",
'Europe/Vienna' => "(GMT+01:00) Vienna",
'Europe/Warsaw' => "(GMT+01:00) Warsaw",
'Europe/Zagreb' => "(GMT+01:00) Zagreb",
'Europe/Athens' => "(GMT+02:00) Athens",
'Europe/Bucharest' => "(GMT+02:00) Bucharest",
'Africa/Cairo' => "(GMT+02:00) Cairo",
'Africa/Harare' => "(GMT+02:00) Harare",
'Europe/Helsinki' => "(GMT+02:00) Helsinki",
'Europe/Istanbul' => "(GMT+02:00) Istanbul",
'Asia/Jerusalem' => "(GMT+02:00) Jerusalem",
'Europe/Kiev' => "(GMT+02:00) Kyiv",
'Europe/Minsk' => "(GMT+02:00) Minsk",
'Europe/Riga' => "(GMT+02:00) Riga",
'Europe/Sofia' => "(GMT+02:00) Sofia",
'Europe/Tallinn' => "(GMT+02:00) Tallinn",
'Europe/Vilnius' => "(GMT+02:00) Vilnius",
'Asia/Baghdad' => "(GMT+03:00) Baghdad",
'Asia/Kuwait' => "(GMT+03:00) Kuwait",
'Africa/Nairobi' => "(GMT+03:00) Nairobi",
'Asia/Riyadh' => "(GMT+03:00) Riyadh",
'Asia/Tehran' => "(GMT+03:30) Tehran",
'Europe/Moscow' => "(GMT+04:00) Moscow",
'Asia/Baku' => "(GMT+04:00) Baku",
'Europe/Volgograd' => "(GMT+04:00) Volgograd",
'Asia/Muscat' => "(GMT+04:00) Muscat",
'Asia/Tbilisi' => "(GMT+04:00) Tbilisi",
'Asia/Yerevan' => "(GMT+04:00) Yerevan",
'Asia/Kabul' => "(GMT+04:30) Kabul",
'Asia/Karachi' => "(GMT+05:00) Karachi",
'Asia/Tashkent' => "(GMT+05:00) Tashkent",
'Asia/Kolkata' => "(GMT+05:30) Kolkata",
'Asia/Kathmandu' => "(GMT+05:45) Kathmandu",
'Asia/Yekaterinburg' => "(GMT+06:00) Ekaterinburg",
'Asia/Almaty' => "(GMT+06:00) Almaty",
'Asia/Dhaka' => "(GMT+06:00) Dhaka",
'Asia/Novosibirsk' => "(GMT+07:00) Novosibirsk",
'Asia/Bangkok' => "(GMT+07:00) Bangkok",
'Asia/Jakarta' => "(GMT+07:00) Jakarta",
'Asia/Krasnoyarsk' => "(GMT+08:00) Krasnoyarsk",
'Asia/Chongqing' => "(GMT+08:00) Chongqing",
'Asia/Hong_Kong' => "(GMT+08:00) Hong Kong",
'Asia/Kuala_Lumpur' => "(GMT+08:00) Kuala Lumpur",
'Australia/Perth' => "(GMT+08:00) Perth",
'Asia/Singapore' => "(GMT+08:00) Singapore",
'Asia/Taipei' => "(GMT+08:00) Taipei",
'Asia/Ulaanbaatar' => "(GMT+08:00) Ulaan Bataar",
'Asia/Urumqi' => "(GMT+08:00) Urumqi",
'Asia/Irkutsk' => "(GMT+09:00) Irkutsk",
'Asia/Seoul' => "(GMT+09:00) Seoul",
'Asia/Tokyo' => "(GMT+09:00) Tokyo",
'Australia/Adelaide' => "(GMT+09:30) Adelaide",
'Australia/Darwin' => "(GMT+09:30) Darwin",
'Asia/Yakutsk' => "(GMT+10:00) Yakutsk",
'Australia/Brisbane' => "(GMT+10:00) Brisbane",
'Pacific/Guam' => "(GMT+10:00) Guam",
'Australia/Hobart' => "(GMT+10:00) Hobart",
'Australia/Melbourne' => "(GMT+10:00) Melbourne",
'Pacific/Port_Moresby' => "(GMT+10:00) Port Moresby",
'Australia/Sydney' => "(GMT+10:00) Sydney",
'Asia/Vladivostok' => "(GMT+11:00) Vladivostok",
'Asia/Magadan' => "(GMT+12:00) Magadan",
'Pacific/Auckland' => "(GMT+12:00) Auckland",
'Pacific/Fiji' => "(GMT+12:00) Fiji",
);
}
/**
* Function to get disk space consumed by a directory
* @param string $directory directory name
* @return string $used_space
* This function is optimized to run faster in linux and windows
*/
function dirSize($directory) {
$size = 0;
if(!is_dir($directory))return $size;
/*
* For linux
*/
exec('/usr/bin/du -sb '.$directory, $o, $c);
if($c == 0 && !empty($o[0])){
$size = trim($o[0]);
$size = trim(substr($size, 0, strpos($size, '/')));
return $size;
}
else{
/**
* For windows
*/
if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'){
try{
$obj = new COM ( 'scripting.filesystemobject');
if(is_object($obj)){
$ref = $obj->getfolder($directory);
$size = $ref->size;
$obj = null;
return $size;
}
}catch(Exception $e){}
}
/**
* For others -> slow method
*/
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file){
$size+=$file->getSize();
}
return $size;
}
}
/**
* Function to format disk space to human readable format
* @param int $size size
* @return string $formatted_size
*/
function formatSize($size)
{
if($size < 1024) return $size.' bytes';
else if($size >= 1024 && $size < 1024*1024) return round($size/1024, 2).' KB';
else if($size >= 1024*1024 && $size < 1024*1024*1024) return round($size/1024/1024, 2).' MB';
else return round($size/1024/1024/1024, 2).' GB';
}
/**
* Function to format page likes/followers to human readable format
* @param int $n like count
* @param int $precision how many decimal places to return
* @return string $formatted_number
*/
function formatLikes($n, $precision = 2) {
if($n < 10000)$n_format = number_format($n);
else if($n < 1000000)$n_format = number_format($n / 1000, $precision). ' K';
else if($n < 1000000000)$n_format = number_format($n / 1000000, $precision) . ' M';
else $n_format = number_format($n / 1000000000, $precision) . ' B';
return $n_format;
}
/**
* Function to perform curl request
* use when no logging and debugging needed
* @param string $url
* @param array $post post data if any
* @return string $response request response
*/
function curl_single($url, $post = array(), $timeout = 30, $headers = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
@curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 0);
if(!empty($post)){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
if(!empty($headers)){
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:31.0) Gecko/20100101 Firefox/31.0');
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$data = curl_exec($ch);
return $data;
}
/**
* Function to perform multi curl request
* use when no logging and debugging needed
* @param array $url array of url
* @param array $post post data if any (2D array)
* @return array $response request response
*/
function curl_multi($url, $post = array(), $timeout = 30, $headers = array())
{
$mh = curl_multi_init();
$ch = array();
$response = array();
for($i = 0; $i < count($url); $i++):
$ch[$i] = curl_init($url[$i]);
curl_setopt($ch[$i], CURLOPT_HEADER,0);
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch[$i], CURLOPT_BINARYTRANSFER,0);
curl_setopt($ch[$i], CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch[$i], CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER,0);
@curl_setopt($ch[$i], CURLOPT_SAFE_UPLOAD, 0);
if(!empty($post[$i])){
curl_setopt($ch[$i], CURLOPT_POST,1);
curl_setopt($ch[$i], CURLOPT_POSTFIELDS, $post[$i]);
}
if(!empty($headers[$i])){
curl_setopt($ch[$i], CURLOPT_HTTPHEADER, $headers[$i]);
}
curl_multi_add_handle($mh, $ch[$i]);
endfor;
if(empty($ch))return;
$running=0;
do{
curl_multi_exec($mh,$running);
$info = curl_multi_info_read($mh);
if($info['handle']) {
$finalresult[] = curl_multi_getcontent($info['handle']);
$returnedOrder[] = array_search($info['handle'], $ch, true);
curl_multi_remove_handle($mh, $ch[end($returnedOrder)]);
curl_close($ch[end($returnedOrder)]);
$rr = end($finalresult);
$index = end($returnedOrder);
$response[$index] = $rr;
}
usleep(500);
}while($running > 0);
curl_multi_close($mh);
return $response;
}
/**
* Function to create pretty time
* @param float $seconds second
* @return string $time in pretty format hh:mm:ss
*/
function pretty_time($seconds){
$t = round($seconds);
$t = sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);
$t = preg_replace('/^00:/', '', $t);
return $t;
}
/**
* Function to get valid time intervals used for scheduling
* @param string $type minutes|hours|weeks|days|months|years
* @param int $validate whether to validate an input
* @return array|boot valid intervals array|validate result
*/
function get_valid_schedule_intervals($type, $validate = 0)
{
$valid = array();
switch($type){
case "minutes":
for($i = 5; $i <= 59; $i += 1)$valid[] = $i;
break;
case "hours":
for($i = 1; $i <= 23; $i ++)$valid[] = $i;
break;
case "weeks":
for($i = 1; $i <= 52; $i += 1)$valid[] = $i;
break;
case "days":
for($i = 1; $i <= 60; $i += 1)$valid[] = $i;
break;
case "months":
for($i = 1; $i <= 36; $i += 1)$valid[] = $i;
break;
case "years":
for($i = 1; $i <= 5; $i += 1)$valid[] = $i;
break;
case "hour_ranges":
for($i = 0; $i <= 23; $i += 1)$valid[] = "'".sprintf('%02d', $i).":00'";
break;
}
if(!empty($validate)){
if(in_array($validate, $valid))return true;
return false;
}
return $valid;
}
function convert_post_freq($post_freq_type, $post_freq)
{
switch($post_freq_type){
case "minutes":
$post_freq *= 60;
break;
case "hours":
$post_freq *= 3600;
break;
case "days":
$post_freq *= 3600*24;
break;
case "weeks":
$post_freq *= 3600*24*7;
break;
case "months":
$post_freq *= 3600*24*30;
break;
case "years":
$post_freq *= 3600*24*365;
break;
}
return $post_freq;
}
/**
* Function to get next posting time
* @param string $post_freq posting interval in seconds
* @param int $post_from what hour is to start posting from (0-23)
* @param int $post_to what hour is to stop posting (0-23)
* @param string $user_timezone timezone of the user
* @return string UTC timestamp for next post
*/
function get_next_post_time($post_freq, $post_from = '', $post_to = '', $start_from = '', $user_timezone = '', $now = '')
{
if(empty($user_timezone))date_default_timezone_set('UTC');
else date_default_timezone_set($user_timezone);
if(empty($now))$now = time();
$left_before_next_day = mktime(0, 0, 0, date('n'), date('j') + 1) - time();
$tz_offset = date('Z');
if($start_from == '0000-00-00' || $start_from == '0000-00-00 00:00:00')$start_from = '';
/**
* Check if we have to start posting from a future date and no post start end time given
*/
if(!empty($start_from) && $post_from == '' && $post_to == ''){
if(!is_numeric($start_from))list($start_from) = sql_fetch_row(sql_query("SELECT DATE('$start_from')"));
else list($start_from) = sql_fetch_row(sql_query("SELECT DATE(FROM_UNIXTIME('$start_from'))"));
$start_from_t = strtotime($start_from);
if($now < $start_from_t){
$d = $start_from.' '.date('H:i:00', $now);
return strtotime($d);
}
else return strtotime(date('d-M-Y H:i:00', $now + $post_freq));
}
if(!empty($start_from)){
if(!is_numeric($start_from))list($start_from) = sql_fetch_row(sql_query("SELECT UNIX_TIMESTAMP('$start_from')"));
$next_post = strtotime(date('d-M-Y H:i:00', $start_from + $post_freq));
$next_post_day = date('d-m-Y', $start_from + $post_freq + $left_before_next_day);
}
if(empty($next_post) || $next_post < $now){
$next_post = strtotime(date('d-M-Y H:i:00', $now + $post_freq));
$next_post_day = date('d-m-Y', $now + $post_freq + $left_before_next_day);
}
$today = date('d-m-Y', $now);
/**
* Check if we have to post from a specific period
*/
if(is_numeric($post_from) && is_numeric($post_to) && $post_from != $post_to){
$post_to--;
$hour_next = (int)date('H', $next_post);
if($post_to <= 0)$post_to = 24;
if($post_from < $post_to){
if($hour_next >= $post_from && $hour_next < $post_to){
return $next_post;
}
else{
/**
* no time to post today
*/
if($hour_next >= $post_to){