-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
functions.global.php
2225 lines (2041 loc) · 74.9 KB
/
functions.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 /*!
* PHPVibe
*
* Copyright PHPVibe.com
* http://www.phpvibe.com
*/
// Global functions
//Site url
function site_url()
{
$site_url = SITE_URL;
$site_url .= (substr(SITE_URL, -1) == '/' ? '' : '/');
return $site_url;
}
function redirect($url = null)
{
if (!$url) {
$url = site_url();
}
header('Location: ' . $url);
exit();
}
//returns current page
function this_page()
{
$page = isset($_GET['p']) ? intval($_GET['p']) : 1;
//No negatives
if ($page < 1) {
$page = 1;
}
return $page;
}
//return next page
function next_page()
{
return this_page() + 1;
}
//query limit
function this_limit()
{
$limit = 'LIMIT ' . (this_page() - 1) * bpp() . ',' . bpp();
return $limit;
}
//query offset
function this_offset($nr)
{
$limit = 'LIMIT ' . (this_page() - 1) * $nr . ',' . $nr;
return $limit;
}
//browse per page
function bpp()
{
if (get_option('bpp') > 0) {
return get_option('bpp');
}
return 24;
}
//ajax call
function is_ajax_call()
{
global $_GET;
return (isset($_GET['ajax']) || isset($_GET['lightbox']));
}
//check if value is null
function nullval($value)
{
if (is_null($value) || $value == "" || empty($value)) {
return true;
} else {
return false;
}
}
//Alias null
function is_empty($v)
{
return nullval($v);
}
//Not null
function not_empty($v)
{
return !nullval($v);
}
/* days difference between 2 dates */
function daysdiff($dateA, $dateB)
{
/* Usage: $a= '2020-01-01 12:30:00'; $b='2020-01-01 12:30:00'; echo daysdiff($a, $b); */
$dateDifference = date_diff(date_create($dateA), date_create($dateB));
return (int)$dateDifference->days;
}
//global time ago
function time_ago($date, $granularity = 2)
{
if (nullval($date)) {
return '';
}
$periods = array("second","minute", "hour","day", "week","month","year", "decade");
$lengths = array("60", "60", "24", "7", "4.35", "12", "10");
$now = time();
$unix_date = strtotime($date);
// check validity of date
if (empty($unix_date)) {
return $date;
}
// is it future date or past date
if ($now > $unix_date) {
$difference = $now - $unix_date;
$tense = _lang("ago");
} else {
$difference = $unix_date - $now;
$tense = _lang("from now");
}
for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths) - 1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if ($difference != 1) {
$periods[$j] .= "s";
}
return $difference . ' ' . _lang($periods[$j]) . ' ' . $tense;
}
/* extracts domain from url */
function _domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
if ((strpos($url, 'localhost') !== false) || (strpos($url, '127.0. 0.1') !== false)) {
return 'localhost';
}
return false;
}
function _igniterkey($vibe = 11)
{
if (defined('phpVibeKey')) {
$out = strtolower(phpVibeKey);
} else {
$out = strtolower(get_option('lekey'));
}
return $out;
}
//array isset
function _globalIsSet($arrayPostGet, $postGetList)
{
$flagValidation = true;
foreach ($postGetList as $testValue) {
if (!(isset($arrayPostGet[$testValue]))) {
$flagValidation = false;
}
}
return $flagValidation;
}
/**
* Read an option from DB (or from cache if available). Return value or $default if not found
*
*/
function get_option($option_name, $default = false)
{
global $db, $all_options;
// Allow plugins to short-circuit options
$pre = apply_filter('shunt_option_' . $option_name, false);
if (false !== $pre)
return $pre;
// Try from cache
if (!isset($all_options) || is_empty($all_options)) {
$all_options = get_all_options();
}
/* Safe guard */
if (is_object($all_options)) {
$all_options = json_decode(json_encode($all_options), true);
}
// If option not available already, get its value from the DB
if (!isset($all_options[$option_name])) {
$option_name = escape($option_name);
$row = $db->get_row("SELECT `option_value` FROM " . DB_PREFIX . "options WHERE `option_name` = '$option_name' LIMIT 1");
if (is_object($row)) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
$value = $row->option_value;
} else { // option does not exist, so we must cache its non-existence
$value = $default;
}
$all_options[$option_name] = maybe_unserialize($value);
jc_destroy(get_options_filename());
jc_put(get_options_filename(), $all_options);
}
return apply_filter('get_option_' . $option_name, $all_options[$option_name]);
}
/**
* Read all options from DB at once
*
*/
function get_options_filename()
{
return substr(preg_replace('/[^\p{L}\p{N}\s]/u', '', strrev(ABSPATH . $_SERVER['SERVER_ADDR'] . DB_NAME)), 12);
}
function get_all_options()
{
global $db;
$vibe_opt = array();
// Allow plugins to short-circuit all options. (Note: regular plugins are loaded after all options)
$pre = apply_filter('shunt_all_options', false);
if (false !== $pre)
return $pre;
$name = get_options_filename();
if (jc_exists($name)) {
$all_options = jc_get($name, true);
/* Safe guard */
if (is_object($all_options)) {
$all_options = json_decode(json_encode($all_options), true);
}
return $all_options;
} else {
$allopt = $db->get_results("SELECT `option_name`, `option_value` FROM " . DB_PREFIX . "options where autoload='yes'");
foreach ((array)$allopt as $option) {
$vibe_opt[$option->option_name] = maybe_unserialize($option->option_value);
}
jc_put($name, $vibe_opt);
}
$vibe_opts = apply_filter('get_all_options', $vibe_opt);
return $vibe_opts;
}
/**
* Update (add if doesn't exist) an option to DB
*
*/
function update_option($option_name, $newvalue)
{
global $db, $all_options;
$safe_option_name = escape($option_name);
$oldvalue = get_option($safe_option_name, false);
// If the new and old values are the same, no need to update.
if ($newvalue === $oldvalue)
return false;
if (false === $oldvalue) {
add_option($option_name, $newvalue);
return true;
}
$_newvalue = escape(maybe_serialize($newvalue));
//do_action( 'update_option', $option_name, $oldvalue, $newvalue );
$db->query("UPDATE " . DB_PREFIX . "options SET `option_value` = '$_newvalue' WHERE `option_name` = '$option_name'");
if ($db->rows_affected == 1) {
$all_options[$option_name] = $newvalue;
/* Clear cache file */
jc_destroy(get_options_filename());
return true;
}
return false;
}
/**
* Add an option to the DB
*
*/
function add_option($name, $value = '')
{
global $db;
$safe_name = escape($name);
// Make sure the option doesn't already exist
if (false !== get_option($safe_name))
return;
$_value = escape(maybe_serialize($value));
//do_action( 'add_option', $safe_name, $_value );
$db->query("INSERT INTO " . DB_PREFIX . "options (`option_name`, `option_value`) VALUES ('$name', '$_value')");
/* Clear cache file */
jc_destroy(get_options_filename());
return;
}
/**
* Delete an option from the DB
*
*/
function delete_option($name)
{
global $db;
$name = escape($name);
// Get the ID, if no ID then return
$option = $db->get_row("SELECT option_id FROM " . DB_PREFIX . "options WHERE `option_name` = '$name'");
if (is_null($option) || !$option->option_id)
return false;
//do_action( 'delete_option', $option_name );
$db->query("DELETE FROM " . DB_PREFIX . "options WHERE `option_name` = '$name'");
/* Clear cache file */
jc_destroy(get_options_filename());
return true;
}
//return safe GET
function _get($val)
{
global $_GET;
if (isset($_GET[$val])) {
return esc_attr($_GET[$val]);
}
return null;
}
//return safe GET integer
function _get_int($val)
{
return intval(_get($val));
}
function t_copy($text)
{
$a = array(0 => '&', 1 => 'l', 2 => 't', 3 => ';', 4 => 'p', 5 => ' ', 6 => 'c', 7 => 'l', 8 => 'a', 9 => 's', 10 => 's', 11 => '=', 12 => '&', 13 => 'q', 14 => 'u', 15 => 'o', 16 => 't', 17 => ';', 18 => 's', 19 => 'm', 20 => 'a', 21 => 'l', 22 => 'l', 23 => ' ', 24 => 'm', 25 => 't', 26 => 'o', 27 => 'p', 28 => '1', 29 => '0', 30 => '&', 31 => 'q', 32 => 'u', 33 => 'o', 34 => 't', 35 => ';', 36 => '&', 37 => 'g', 38 => 't', 39 => ';', 40 => 'P', 41 => 'o', 42 => 'w', 43 => 'e', 44 => 'r', 45 => 'e', 46 => 'd', 47 => ' ', 48 => 'b', 49 => 'y', 50 => ' ', 51 => '&', 52 => 'l', 53 => 't', 54 => ';', 55 => 'a', 56 => ' ', 57 => 'r', 58 => 'e', 59 => 'l', 60 => '=', 61 => '&', 62 => 'q', 63 => 'u', 64 => 'o', 65 => 't', 66 => ';', 67 => 'n', 68 => 'o', 69 => 'f', 70 => 'o', 71 => 'l', 72 => 'l', 73 => 'o', 74 => 'w', 75 => '&', 76 => 'q', 77 => 'u', 78 => 'o', 79 => 't', 80 => ';', 81 => ' ', 82 => 'h', 83 => 'r', 84 => 'e', 85 => 'f', 86 => '=', 87 => '&', 88 => 'q', 89 => 'u', 90 => 'o', 91 => 't', 92 => ';', 93 => 'h', 94 => 't', 95 => 't', 96 => 'p', 97 => ':', 98 => '/', 99 => '/', 100 => 'w', 101 => 'w', 102 => 'w', 103 => '.', 104 => 'p', 105 => 'h', 106 => 'p', 107 => 'v', 108 => 'i', 109 => 'b', 110 => 'e', 111 => '.', 112 => 'c', 113 => 'o', 114 => 'm', 115 => '&', 116 => 'q', 117 => 'u', 118 => 'o', 119 => 't', 120 => ';', 121 => ' ', 122 => 't', 123 => 'a', 124 => 'r', 125 => 'g', 126 => 'e', 127 => 't', 128 => '=', 129 => '&', 130 => 'q', 131 => 'u', 132 => 'o', 133 => 't', 134 => ';', 135 => '_', 136 => 'b', 137 => 'l', 138 => 'a', 139 => 'n', 140 => 'k', 141 => '&', 142 => 'q', 143 => 'u', 144 => 'o', 145 => 't', 146 => ';', 147 => ' ', 148 => 't', 149 => 'i', 150 => 't', 151 => 'l', 152 => 'e', 153 => '=', 154 => '&', 155 => 'q', 156 => 'u', 157 => 'o', 158 => 't', 159 => ';', 160 => 'M', 161 => 'e', 162 => 'd', 163 => 'i', 164 => 'a', 165 => 'V', 166 => 'i', 167 => 'b', 168 => 'e', 169 => ' ', 170 => 'V', 171 => 'i', 172 => 'd', 173 => 'e', 174 => 'o', 175 => ' ', 176 => 'C', 177 => 'M', 178 => 'S', 179 => '&', 180 => 'q', 181 => 'u', 182 => 'o', 183 => 't', 184 => ';', 185 => '&', 186 => 'g', 187 => 't', 188 => ';', 189 => 'M', 190 => 'e', 191 => 'd', 192 => 'i', 193 => 'a', 194 => 'V', 195 => 'i', 196 => 'b', 197 => 'e', 198 => '&', 199 => 't', 200 => 'r', 201 => 'a', 202 => 'd', 203 => 'e', 204 => ';', 205 => ' ', 206 => 'C', 207 => 'M', 208 => 'S', 209 => '&', 210 => 'l', 211 => 't', 212 => ';', 213 => '/', 214 => 'a', 215 => '&', 216 => 'g', 217 => 't', 218 => ';',);
$text = $text . ' ' . str_replace('Media', 'PHP', html_entity_decode(implode('', $a))) . ' ' . get_option('licto') . '</p>';
return $text;
}
//return safe POST
function _post($val)
{
global $_POST;
if (isset($_POST[$val])) {
return esc_attr($_POST[$val]);
}
return null;
}
//return safe POST integer
function _post_int($val)
{
return intval(_post($val));
}
//return percent
function percent($first, $num_total, $precision = 0)
{
if ($num_total > 0) {
$res = round(($first / $num_total) * 100, $precision);
return $res;
} elseif ($first > 0) {
return 100;
}
return 0;
}
//limit a string
function _cut($str, $nb = 10)
{
if (not_empty($str)) {
if (strlen($str) > $nb) {
if (extension_loaded('mbstring')) {
mb_internal_encoding("UTF-8");
$str = mb_substr($str, 0, $nb);
} else {
$str = substr($str, 0, $nb);
}
}
}
return $str;
}
//Language functions
function init_lang()
{
if (isset($_GET["clang"])) {
$_SESSION['phpvibe-language'] = toDB($_GET["clang"]);
redirect(strtok(strtok(canonical(), '?'), '&'));
} else {
$trans = lang_terms(toDB(current_lang()));
}
return $trans;
}
function current_lang()
{
$cl = isset($_SESSION['phpvibe-language']) ? $_SESSION['phpvibe-language'] : get_browser_language();
return $cl;
}
function get_browser_language()
{
global $cachedb;
$available = array();
$default = get_option('def_lang', 'en');
// List available
$avails = $cachedb->get_results("SELECT `lang_code` FROM " . DB_PREFIX . "languages");
foreach ($avails as $av) {
$available[] = $av->lang_code;
}
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (empty($available)) {
return $default;
}
foreach ($langs as $lang) {
$lang = substr($lang, 0, 2);
if (in_array($lang, $available)) {
return $lang;
}
//Test for XX-nr
$matches = preg_grep('/' . $lang . '/', $available);
if ($matches) {
if (isset($matches[0])) {
return $matches[0];
}
}
}
}
return $default;
}
function lang_terms($lang = null)
{
global $db;
$lang = (is_null($lang)) ? current_lang() : $lang;
$all_terms = get_language($lang);
if (is_null($all_terms) || !is_array($all_terms)) {
//Switch to default if
$all_terms = get_language(get_option('def_lang', 'en'));
} else {
$_SESSION['phpvibe-language'] = $lang;
}
return $all_terms;
}
function _lang($txt)
{
global $trans;
if (isset($trans[$txt])) {
return _html($trans[$txt]);
} else {
lang_log($txt);
return $txt;
}
}
/**
* Log term in the DB
*
*/
function lang_log($txt)
{
global $cachedb;
if ($cachedb) {
$txt = escape($txt);
/* Check if term exists in matrix */
$check = $cachedb->get_row("SELECT count(*) as nr FROM " . DB_PREFIX . "langs WHERE `term` = '$txt'");
if (!$check || ($check->nr < 1)) {
//Insert term
$cachedb->query("INSERT INTO " . DB_PREFIX . "langs (`term`) VALUES ('$txt')");
}
}
}
/**
* Get language terms
*
*/
function get_language($lang_code, $default = false)
{
global $cachedb;
$lang_code = escape($lang_code);
$lang_file = ABSPATH . '/storage/langs/' . $lang_code . '.json';
//echo $lang_file;
if (file_exists($lang_file)) {
$row = file_get_contents($lang_file);
$row = json_decode(stripslashes($row), true);
//print_r($row);
if (is_array($row)) {
return apply_filter('get_language_' . $lang_code, $row);
} else { // language does not exist, so we must cache its non-existence
return $default;
}
} else { // language does not exist
return $default;
}
}
/**
* Add an language to the DB
*
*/
function add_language($code, $value = '')
{
global $db;
$code = escape($code);
$long_name = addslashes($value["language-name"]);
// Make sure the language doesn't already exist
$language = $db->get_row("SELECT count(*) as nr FROM " . DB_PREFIX . "languages WHERE `lang_code` like '$code%'");
//$db->debug();
if ($language) {
if ($language->nr > 0) {
/* Language code already exists */
$nx = $language->nr + 1;
$code .= '-' . $nx;
}
}
//$_value = escape( json_encode( $value ) );
$_value = addslashes(json_encode($value));
//do_action( 'add_language', $safe_name, $_value );
$db->query("INSERT INTO " . DB_PREFIX . "languages (`lang_name`, `lang_code`, `lang_terms`) VALUES ('$long_name','$code', '')");
@chmod(ABSPATH . '/storage/langs/', 0777);
$lang_file = ABSPATH . '/storage/langs/' . $code . '.json';
@touch($lang_file);
@chmod($lang_file, 0777);
$myfile = @fopen($lang_file, "w");
@fwrite($myfile, $_value);
@fclose($myfile);
if (file_exists($lang_file) && (filesize($lang_file) > 1)) {
@chmod(ABSPATH . '/storage/langs/', 0755);
} else {
//Back it up to adm cache
$lang_file = ADMINCP . '/cache/lang-' . $code . date("m.d.y-g.i.a") . '.json';
$myfile = @fopen($lang_file, "w");
@fwrite($myfile, $_value);
@fclose($myfile);
}
return;
}
/**
* Delete an language from the DB
*
*/
function delete_language($name)
{
global $db;
$name = escape($name);
// Get the ID, if no ID then return
$language = $db->get_row("SELECT term_id FROM " . DB_PREFIX . "languages WHERE `lang_code` = '$name'");
if (is_null($language) || !$language->term_id)
return false;
//do_action( 'delete_language', $lang_code );
$db->query("DELETE FROM " . DB_PREFIX . "languages WHERE `lang_code` = '$name'");
return true;
}
/* end language */
//Common actions
function the_header()
{
do_action('vibe_header');
do_action('phpviberun');
}
function the_footer()
{
do_action('vibe_footer');
}
function the_sidebar()
{
if (is_admin() || (get_option('site-offline', 0) == 0)) {
do_action('vibe_sidebar');
}
}
function right_sidebar()
{
do_action('right_sidebar');
}
function vibe_headers()
{
if(function_exists('meta_add')) {
echo apply_filters('vibe_meta_filter', meta_add());
}
if(function_exists('header_add')) {
echo apply_filters('vibe_header_filter', header_add());
}
}
function vibe_footers()
{
echo apply_filters('vibe_footer_filter', footer_add());
}
//Map the actions
add_action('vibe_header', 'vibe_headers', 1);
add_action('vibe_footer', 'vibe_footers', 1);
//sidebar
function the_side()
{
global $db, $cachedb;
include_once(TPL . '/sidebar.php');
}
function right_side()
{
global $db, $cachedb;
include_once(TPL . '/sidebar-right.php');
}
add_action('vibe_sidebar', 'the_side', 1);
add_action('right_sidebar', 'right_side', 1);
add_filter('tsitecopy', 't_copy');
function site_copy()
{
return apply_filters('tsitecopy', get_option('site-copyright'));
}
function video_time($sec, $padHours = false)
{
$hms = "";
// there are 3600 seconds in an hour, so if we
// divide total seconds by 3600 and throw away
// the remainder, we've got the number of hours
$hours = intval(intval($sec) / 3600);
if ($hours > 0):
// add to $hms, with a leading 0 if asked for
$hms .= ($padHours) ? str_pad($hours, 2, "0", STR_PAD_LEFT) . ':' : $hours . ':';
endif;
// dividing the total seconds by 60 will give us
// the number of minutes, but we're interested in
// minutes past the hour: to get that, we need to
// divide by 60 again and keep the remainder
$minutes = intval(intval($sec / 60) % 60);
// then add to $hms (with a leading 0 if needed)
$hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT) . ':';
// seconds are simple - just divide the total
// seconds by 60 and keep the remainder
$seconds = intval($sec % 60);
// add to $hms, again with a leading 0 if needed
$hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
return $hms;
}
function sef_url()
{
$url = site_url();
if (substr($url, -1) == '/') {
$url = rtrim($url, "/");
}
return $url;
}
//Misc functions
function render_video($code)
{
return htmlspecialchars_decode(specialchars_decode($code));
}
// Mini Layout func
function layout($part)
{
global $db, $video, $cachedb;
include_once(TPL . '/' . $part . '.php');
}
function tpl()
{
return site_url() . '/themes/' . THEME . '/';
}
function applibrary()
{
return 'app';
}
// NSFW
function nsfilter()
{
global $video, $image;
if (isset($video)) {
if (!$video->nsfw()) {
return false;
} elseif (isset($_SESSION['nsfw']) && $_SESSION['nsfw'] > 0) {
return false;
} else {
return true;
}
} elseif (isset($image)) {
if ($image->nsfw < 1) {
return false;
} elseif (isset($_SESSION['nsfw']) && $_SESSION['nsfw'] > 0) {
return false;
} else {
return true;
}
}
}
//SEO
function seo_title()
{
return apply_filters('phpvibe_title', get_option('seo_title'));
}
function seo_desc()
{
return apply_filters('phpvibe_desc', get_option('seo_desc'));
}
//Db count
function _count($table, $field = null, $sum = false)
{
global $db;
if ($field && !$sum) {
$c = $db->get_row("SELECT count(" . $field . ") as nr FROM " . DB_PREFIX . $table);
} elseif ($field && $sum) {
$c = $db->get_row("SELECT sum(" . $field . ") as nr FROM " . DB_PREFIX . $table);
} else {
$c = $db->get_row("SELECT count(*) as nr FROM " . DB_PREFIX . $table);
}
if ($c && not_empty($c->nr)) {
return number_format($c->nr, 0);
} else {
return 0;
}
}
//Fb count
function _fb_count($name)
{
return '';
//Deprecated
}
// Thumb fix
function thumb_fix($thumb, $resize = false, $w = 0, $h = 'auto')
{
if ($thumb) {
if ((substr($thumb, 0, 2) == "//") || (substr($thumb, 0, 4) == "http")) {
return $thumb;
}
if ($resize && ($w > 10)) {
return site_url() . 'res.php?src=' . $thumb . '&q=100&w=' . $w . '&h=' . $h;
} else {
return site_url() . $thumb;
}
}
}
function hashList($id)
{
if ((strpos($id, 'ums-') !== false) || (strpos($id, 'uvs-') !== false)) {
/* Keep special lists */
return $id;
} else {
return _mHash($id);
}
}
function decList($id)
{
if ((strpos($id, 'ums-') !== false) || (strpos($id, 'uvs-') !== false)) {
/* Keep special lists */
return $id;
} else {
return _dHash($id);
}
}
function has_list()
{
return not_empty(toDb(_get('list')));
}
function isPost()
{
return strtolower($_SERVER['REQUEST_METHOD']) === 'post';
}
/* Categories navigator */
function the_nav($type = 1)
{
global $db, $cachedb;
include_once(CNC . '/class.tree.php');
$nav = '';
$tree = new Tree;
$categories = $cachedb->get_results("SELECT cat_id as id, child_of as ch, cat_name as name, picture FROM " . DB_PREFIX . "channels WHERE type='$type' order by cat_name ASC limit 0,1000");
if ($categories) {
foreach ($categories as $cat) {
if ($cat->ch < 1) {
$cat->ch = '';
}
if (not_empty($cat->picture)) {
$image = '<img data-name="' . $cat->name . '" class="cats-img img-circle" src="' . thumb_fix($cat->picture, true, 30, 30) . '"/>';
} else {
$image = '<img data-name="' . $cat->name . '" class="cats-img img-circle NoAvatar"/>';
}
$label = '
<a class="cats-img" href="' . channel_url($cat->id, $cat->name, $type) . '" title="' . _html($cat->name) . '">' . $image . '</a>
<a class="cats-link" href="' . channel_url($cat->id, $cat->name, $type) . '" title="' . _html($cat->name) . '">' . _html($cat->name) . '</a>
';
$li_attr = '';
$tree->add_row($cat->id, $cat->ch, $li_attr, $label);
}
$nav .= $tree->generate_list();
}
return apply_filters('the_navigation', $nav);
}
function subscribe_box($user, $btnc = '', $counter = true)
{
global $db;
$ktool = '';
if ($counter) {
$ktool = '<span class="separator"></span><span class="kcounter">' . get_subscribers($user) . '</span>';
}
echo '<div class="btn-subscribing">';
if (!is_user()) {
//It's guest
$btnc = "btn btn-labled social-google-plus subscriber";
echo '<a class="' . $btnc . '" href="javascript:showLogin()"><i class="icon icon-plus"></i>' . _lang('Subscribe') . ' ' . $ktool . '</a>';
} elseif ($user <> user_id()) {
//If it's not you
$check = $db->get_row("SELECT count(*) as nr from " . DB_PREFIX . "users_friends where uid ='" . $user . "' and fid='" . user_id() . "'");
if ($check->nr < 1) {
//You're not subscribed
$btnc = "btn btn-labled social-google-plus subscriber";
echo '<a id="subscribe-' . $user . '" data-next="' . _lang("subscribed") . '" class="' . $btnc . ' pv_tip" href="javascript:Subscribe(' . $user . ', 1)" title="' . _lang('Click to add a subscription') . '">' . _lang('Subscribe') . ' ' . $ktool . '</a>';
} else {
//You are, but can unsubscribe
$btnc = "btn btn-default subscriber";
echo '<a id="subscribe-' . $user . '" data-next="' . _lang("unsubscribed") . '" class="' . $btnc . ' pv_tip" href="javascript:Subscribe(' . $user . ', 3)" title="' . _lang('Click to remove subscription') . '">' . _lang('Subscribed') . ' ' . $ktool . '</a>';
}
} else {
if (is_video()) {
global $video;
$btnc = "btn btn-default subscriber";
echo '<a target="_blank" href="' . site_url() . me . '?sk=edit-video&vid=' . $video->id() . '" class="' . $btnc . '"><i class="icon icon-cogs"></i>' . _lang('Edit media') . '</a>';
} elseif (is_picture()) {
global $image;
$btnc = "btn btn-default subscriber";
echo '<a target="_blank" href="' . site_url() . me . '?sk=edit-image&vid=' . $image->id . '" class="' . $btnc . '"><i class="icon icon-cogs"></i>' . _lang('Edit media') . '</a>';
} else {
//It's you
$btnc = "btn btn-default subscriber";
echo '<a href="' . site_url() . 'dashboard/?sk=edit" class="' . $btnc . '"><i class="icon icon-cogs"></i>' . _lang('Settings') . '</a>';
}
}
echo '</div>';
}
function get_subscribers($user)
{
global $db, $cachedb;
$sub = $cachedb->get_row("Select count(*) as nr from " . DB_PREFIX . "users where " . DB_PREFIX . "users.id in ( select fid from " . DB_PREFIX . "users_friends where uid ='" . $user . "')");
return number_format($sub->nr);
}
function list_title($list)
{
global $db;
$list = decList($list);
/*for video header in list mode */
if (intval($list) > 0) {
$playlist = $db->get_row("SELECT title FROM " . DB_PREFIX . "playlists where id = '" . intval($list) . "' limit 0,1");
if ($playlist) {
return strip_tags(_html($playlist->title));
}
} elseif (strpos($list, 'ums-') !== false) {
/* All music */
$uid = intval(str_replace('ums-', '', $list));
$playlist = $db->get_row("SELECT name FROM " . DB_PREFIX . "users where id = '" . intval($uid) . "' limit 0,1");
if ($playlist) {
return strip_tags(_html(_lang("All songs by ") . $playlist->name));
}
} elseif (strpos($list, 'uvs-') !== false) {
/* All videos */
$uid = intval(str_replace('uvs-', '', $list));
$playlist = $db->get_row("SELECT name FROM " . DB_PREFIX . "users where id = '" . intval($uid) . "' limit 0,1");
if ($playlist) {
return strip_tags(_html(_lang("All videos by ") . $playlist->name));
}
}
}
function canonical()
{
global $canonical;
if (isset($canonical) && !empty($canonical)) {
return $canonical;
}
/*else try to build an url for menu's back step not to fail */
return selfURL();
}
function selfURL()
{
$protocol = strleft(strtolower($_SERVER['HTTP_HOST']), "/");
if (is_empty($protocol)) {
$protocol = 'https';
}
return $protocol . "://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2)
{
return substr($s1, 0, strpos($s1, $s2));
}
/*Track activity */
function add_activity($type, $obj, $extra = '')
{
global $db;
if (is_user() && $type && $obj) {
$db->query("INSERT INTO " . DB_PREFIX . "activity (`user`, `type`, `object`, `extra`) VALUES ('" . user_id() . "', '" . toDb($type) . "', '" . toDb($obj) . "', '" . toDb($extra) . "')");
do_action('add-activity');
}
}
function has_activity($type, $obj, $extra = '')
{
global $db;
$check = $db->get_row("SELECT count(*) as nr from " . DB_PREFIX . "activity where user ='" . user_id() . "' and type = '" . toDb($type) . "' and object = '" . toDb($obj) . "' and extra = '" . toDb($extra) . "'");
return ($check->nr > 0);
}
function remove_activity($type, $obj, $extra = '')
{
global $db;
$db->query("delete from " . DB_PREFIX . "activity where user ='" . user_id() . "' and type = '" . toDb($type) . "' and object = '" . toDb($obj) . "' and extra = '" . toDb($extra) . "'");
do_action('remove-activity');
}
function default_content()
{
/* Dummy function for default template
manipulated by filters */
$def = '';
return apply_filters('the_defaults', $def);
}
//Video processing
function unpublish_video($id)
{
global $db;
$id = intval($id);
if ($id) {
if (is_moderator()) {
//can edit any video
$db->query("UPDATE " . DB_PREFIX . "videos SET pub = '0' where id='" . $id . "'");
} else {
//make sure it's his video
$db->query("UPDATE " . DB_PREFIX . "videos SET pub = '0' where id='" . $id . "' and user_id ='" . user_id() . "'");
}
}
}
function publish_video($id)
{
global $db;
$id = intval($id);
if ($id) {
if (is_moderator()) {
//can edit any video
$db->query("UPDATE " . DB_PREFIX . "videos SET pub = '1' where id='" . $id . "'");
} else {
//make sure it's his video
$db->query("UPDATE " . DB_PREFIX . "videos SET pub = '1' where id='" . $id . "' and user_id ='" . user_id() . "'");
}
}
}
function delete_video($id)
{
global $db;
if (intval($id) && is_moderator()) {
//$video = $db->get_row("SELECT * from ".DB_PREFIX."videos where id='".intval($id)."'");
//Get the video
$video = new PHPVibe\Video\SingleVideo($id);
if ($video) {
if ($video->isembed() || $video->isremote()) {
//delete imediatly if remote
$db->query("DELETE from " . DB_PREFIX . "videos where id='" . intval($id) . "'");
} else {
//try to delete file to
$vid = new Vibe_Providers(10, 10);
$source = $vid->VideoProvider($video->source());
if (($source == "localimage") || ($source == "localfile") || ($source == "up")) {
$path = ABSPATH . '/storage/' . get_option('mediafolder') . str_replace(array("localimage", "localfile"), array("", ""), $video->source);
//remove video file
remove_file($path);
// Remove qualities
$patternx = "{*" . $video->token() . "*}";