This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
/
wppa-utils.php
2753 lines (2367 loc) · 87.7 KB
/
wppa-utils.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
/* wppa-utils.php
* Package: wp-photo-album-plus
*
* Contains low-level utility routines
* Version 6.3.0
*
*/
if ( ! defined( 'ABSPATH' ) ) die( "Can't load this file directly" );
global $wppa_supported_photo_extensions;
$wppa_supported_photo_extensions = array( 'jpg', 'jpeg', 'png', 'gif' );
// Get url in wppa dir
function wppa_url( $arg ) {
return WPPA_URL . '/' . $arg;
}
// get url of thumb
function wppa_get_thumb_url( $id, $system = 'flat', $x = '0', $y = '0' ) {
global $wppa_opt;
global $blog_id;
$thumb = wppa_cache_thumb( $id );
// If in the cloud...
$is_old = wppa_too_old_for_cloud( $id );
if ( wppa_cdn( 'front' ) && ! wppa_is_multi( $id ) && ! $is_old ) {
if ( $x && $y ) { // Only when size is given !! To prevent download of the fullsize image
switch ( wppa_cdn( 'front' ) ) {
case 'cloudinary':
$transform = explode( ':', $wppa_opt['wppa_thumb_aspect'] );
$t = 'limit';
if ( $transform['2'] == 'clip' ) $t = 'fill';
if ( $transform['2'] == 'padd' ) $t = 'pad,b_black';
$q = $wppa_opt['wppa_jpeg_quality'];
$sizespec = ( $x && $y ) ? 'w_'.$x.',h_'.$y.',c_'.$t.',q_'.$q.'/' : '';
$prefix = ( is_multisite() && ! WPPA_MULTISITE_GLOBAL ) ? $blog_id.'-' : '';
$s = is_ssl() ? 's' : '';
$url = 'http'.$s.'://res.cloudinary.com/'.get_option('wppa_cdn_cloud_name').'/image/upload/'.$sizespec.$prefix.$thumb['id'].'.'.$thumb['ext'];
return $url;
break;
}
}
}
if ( get_option('wppa_file_system') == 'flat' ) $system = 'flat'; // Have been converted, ignore argument
if ( get_option('wppa_file_system') == 'tree' ) $system = 'tree'; // Have been converted, ignore argument
if ( ! is_numeric($id) || $id < '1' ) wppa_dbg_msg('Invalid arg wppa_get_thumb_url('.$id.')', 'red');
if ( $system == 'tree' ) {
$url = WPPA_UPLOAD_URL.'/thumbs/'.wppa_expand_id($thumb['id']).'.'.$thumb['ext'].'?ver='.get_option('wppa_thumb_version', '1');
}
else {
$url = WPPA_UPLOAD_URL.'/thumbs/'.$thumb['id'].'.'.$thumb['ext'].'?ver='.get_option('wppa_thumb_version', '1');
}
return $url;
}
// Bump thumbnail version number
function wppa_bump_thumb_rev() {
wppa_update_option('wppa_thumb_version', get_option('wppa_thumb_version', '1') + '1');
}
// get path of thumb
function wppa_get_thumb_path( $id, $system = 'flat' ) {
$thumb = wppa_cache_thumb( $id );
if ( get_option('wppa_file_system') == 'flat' ) $system = 'flat'; // Have been converted, ignore argument
if ( get_option('wppa_file_system') == 'tree' ) $system = 'tree'; // Have been converted, ignore argument
if ( ! is_numeric($id) || $id < '1' ) wppa_dbg_msg('Invalid arg wppa_get_thumb_path('.$id.')', 'red');
if ( $system == 'tree' ) return WPPA_UPLOAD_PATH.'/thumbs/'.wppa_expand_id($thumb['id'], true).'.'.$thumb['ext'];
else return WPPA_UPLOAD_PATH.'/thumbs/'.$thumb['id'].'.'.$thumb['ext'];
}
// get url of a full sized image
function wppa_get_photo_url( $id, $system = 'flat', $x = '0', $y = '0' ) {
global $wppa_opt;
global $blog_id;
$thumb = wppa_cache_thumb( $id );
if ( is_feed() && wppa_switch('wppa_feed_use_thumb') ) return wppa_get_thumb_url($id, $system);
// If in the cloud...
$is_old = wppa_too_old_for_cloud( $id );
if ( wppa_cdn( 'front' ) && ! wppa_is_multi( $id ) && ! $is_old ) {
switch ( wppa_cdn( 'front' ) ) {
case 'cloudinary':
$x = round($x);
$y = round($y);
$prefix = ( is_multisite() && ! WPPA_MULTISITE_GLOBAL ) ? $blog_id.'-' : '';
$t = wppa_switch('wppa_enlarge') ? 'fit' : 'limit';
$q = $wppa_opt['wppa_jpeg_quality'];
$sizespec = ( $x && $y ) ? 'w_'.$x.',h_'.$y.',c_'.$t.',q_'.$q.'/' : '';
$s = is_ssl() ? 's' : '';
$url = 'http'.$s.'://res.cloudinary.com/'.get_option('wppa_cdn_cloud_name').'/image/upload/'.$sizespec.$prefix.$thumb['id'].'.'.$thumb['ext'];
return $url;
break;
}
}
if ( get_option('wppa_file_system') == 'flat' ) $system = 'flat'; // Have been converted, ignore argument
if ( get_option('wppa_file_system') == 'tree' ) $system = 'tree'; // Have been converted, ignore argument
if ( ! is_numeric($id) || $id < '1' ) wppa_dbg_msg('Invalid arg wppa_get_photo_url('.$id.')', 'red');
if ( $system == 'tree' ) return WPPA_UPLOAD_URL.'/'.wppa_expand_id($thumb['id']).'.'.$thumb['ext'].'?ver='.get_option('wppa_photo_version', '1');
else return WPPA_UPLOAD_URL.'/'.$thumb['id'].'.'.$thumb['ext'].'?ver='.get_option('wppa_photo_version', '1');
}
// Bump Fullsize photo version number
function wppa_bump_photo_rev() {
wppa_update_option('wppa_photo_version', get_option('wppa_photo_version', '1') + '1');
}
// get path of a full sized image
function wppa_get_photo_path( $id, $system = 'flat' ) {
$thumb = wppa_cache_thumb( $id );
if ( get_option( 'wppa_file_system' ) == 'flat' ) $system = 'flat'; // Have been converted, ignore argument
if ( get_option( 'wppa_file_system' ) == 'tree' ) $system = 'tree'; // Have been converted, ignore argument
if ( ! is_numeric( $id ) || $id < '1' ) wppa_dbg_msg( 'Invalid arg wppa_get_photo_path(' . $id . ')', 'red' );
if ( $system == 'tree' ) return WPPA_UPLOAD_PATH . '/' . wppa_expand_id( $thumb['id'], true ) . '.' . $thumb['ext'];
else return WPPA_UPLOAD_PATH . '/' . $thumb['id'] . '.' . $thumb['ext'];
}
// Expand id to subdir chain for new file structure
function wppa_expand_id( $xid, $makepath = false ) {
$result = '';
$id = $xid;
$len = strlen( $id );
while ( $len > '2' ) {
$result .= substr( $id, '0', '2' ) . '/';
$id = substr( $id, '2' );
$len = strlen( $id );
if ( $makepath ) {
$path = WPPA_UPLOAD_PATH . '/' . $result;
if ( ! is_dir( $path ) ) wppa_mktree( $path );
$path = WPPA_UPLOAD_PATH . '/thumbs/' . $result;
if ( ! is_dir( $path ) ) wppa_mktree( $path );
}
}
$result .= $id;
return $result;
}
// Makes the html for the geo support for current theme and adds it to $wppa['geo']
function wppa_do_geo( $id, $location ) {
global $wppa;
global $wppa_opt;
$temp = explode( '/', $location );
$lat = $temp['2'];
$lon = $temp['3'];
$type = $wppa_opt['wppa_gpx_implementation'];
// Switch on implementation type
switch ( $type ) {
case 'google-maps-gpx-viewer':
$geo = str_replace( 'w#lon', $lon, str_replace( 'w#lat', $lat, $wppa_opt['wppa_gpx_shortcode'] ) );
$geo = str_replace( 'w#ip', $_SERVER['REMOTE_ADDR'], $geo );
$geo = do_shortcode( $geo );
$wppa['geo'] .= '<div id="geodiv-' . wppa( 'mocc' ) . '-' . $id . '" style="display:none;">' . $geo . '</div>';
break;
case 'wppa-plus-embedded':
if ( $wppa['geo'] == '' ) { // First
$wppa['geo'] = '
<div id="map-canvas-' . wppa( 'mocc' ).'" style="height:' . $wppa_opt['wppa_map_height'] . 'px; width:100%; padding:0; margin:0; font-size: 10px;" ></div>
<script type="text/javascript" >
if ( typeof ( _wppaLat ) == "undefined" ) { var _wppaLat = new Array(); var _wppaLon = new Array(); }
_wppaLat[' . wppa( 'mocc' ) . '] = new Array(); _wppaLon[' . wppa( 'mocc' ) . '] = new Array();
</script>';
} // End first
$wppa['geo'] .= '
<script type="text/javascript">_wppaLat[' . wppa( 'mocc' ) . '][' . $id . '] = ' . $lat.'; _wppaLon[' . wppa( 'mocc' ) . '][' . $id.'] = ' . $lon . ';</script>';
break; // End native
}
}
// See if an album is in a separate tree
function wppa_is_separate( $id ) {
if ( $id == '' ) return false;
if ( ! wppa_is_int( $id ) ) return false;
if ( $id == '-1' ) return true;
if ( $id < '1' ) return false;
$alb = wppa_get_parentalbumid( $id );
return wppa_is_separate( $alb );
}
// Get the albums parent
function wppa_get_parentalbumid($id) {
static $prev_album_id;
if ( ! wppa_is_int($id) || $id < '1' ) return '0';
$album = wppa_cache_album($id);
if ( $album === false ) {
wppa_dbg_msg('Album '.$id.' no longer exists, but is still set as a parent of '.$prev_album_id.'. Please correct this.', 'red');
return '-9'; // Album does not exist
}
$prev_album_id = $id;
return $album['a_parent'];
}
function wppa_html($str) {
global $wppa_opt;
// It is assumed that the raw data contains html.
// If html not allowed, filter specialchars
// To prevent duplicate filtering, first entity_decode
$result = html_entity_decode($str);
if ( ! wppa_switch('wppa_html') && ! current_user_can('wppa_moderate') ) {
$result = htmlspecialchars($str);
}
return $result;
}
// get a photos album id
function wppa_get_album_id_by_photo_id( $id ) {
if ( ! is_numeric($id) || $id < '1' ) wppa_dbg_msg('Invalid arg wppa_get_album_id_by_photo_id('.$id.')', 'red');
$thumb = wppa_cache_thumb($id);
return $thumb['album'];
}
function wppa_get_rating_count_by_id($id) {
if ( ! is_numeric($id) || $id < '1' ) wppa_dbg_msg('Invalid arg wppa_get_rating_count_by_id('.$id.')', 'red');
$thumb = wppa_cache_thumb($id);
return $thumb['rating_count'];
}
function wppa_get_rating_by_id($id, $opt = '') {
global $wpdb;
global $wppa_opt;
if ( ! is_numeric($id) || $id < '1' ) wppa_dbg_msg('Invalid arg wppa_get_rating_by_id('.$id.', '.$opt.')', 'red');
$thumb = wppa_cache_thumb( $id );
$rating = $thumb['mean_rating'];
if ( $rating ) {
$i = $wppa_opt['wppa_rating_prec'];
$j = $i + '1';
$val = sprintf('%'.$j.'.'.$i.'f', $rating);
if ($opt == 'nolabel') $result = $val;
else $result = sprintf(__('Rating: %s', 'wp-photo-album-plus'), $val);
}
else $result = '';
return $result;
}
function wppa_switch( $xkey ) {
global $wppa_opt;
if ( substr( $xkey, 0, 5 ) == 'wppa_' ) {
$key = $xkey;
}
else {
$key = 'wppa_' . $xkey;
}
if ( is_array( $wppa_opt ) ) {
if ( isset( $wppa_opt[$key] ) ) {
if ( $wppa_opt[$key] == 'yes' ) return true;
elseif ( $wppa_opt[$key] == 'no' ) return false;
else wppa_log( 'Error', '$wppa_opt['.$key.'] is not a yes/no setting' );
}
else wppa_log( 'Error', '$wppa_opt['.$key.'] is not a setting' );
}
else wppa_log( 'Error', '$wppa_opt[] is not initialized while testing '.$key );
return false;
}
function wppa_opt( $xkey ) {
global $wppa_opt;
if ( substr( $xkey, 0, 5 ) == 'wppa_' ) {
$key = $xkey;
}
else {
$key = 'wppa_' . $xkey;
}
if ( empty( $wppa_opt ) ) {
wppa_initialize_runtime();
}
if ( is_array( $wppa_opt ) ) {
if ( isset( $wppa_opt[$key] ) ) {
if ( $wppa_opt[$key] == 'yes' || $wppa_opt[$key] == 'no' ) {
wppa_log( 'Error', '$wppa_opt['.$key.'] is a yes/no setting, not a value' );
return false;
}
}
else {
wppa_log( 'Error', '$wppa_opt['.$key.'] is not a setting' );
return false;
}
}
else {
wppa_log( 'Error', '$wppa_opt[] is not initialized while testing '.$key );
return false;
}
return trim( $wppa_opt[$key] );
}
// Getter / setter of runtime parameter
function wppa( $key, $newval = 'nil' ) {
global $wppa;
// Array defined?
if ( is_array( $wppa ) ) {
// Valid key?
if ( isset( $wppa[$key] ) ) {
// Get old value
$oldval = $wppa[$key];
// New value supplied?
if ( $newval !== 'nil' ) {
$wppa[$key] = $newval;
}
}
// Invalid key
else {
wppa_log( 'Error', '$wppa[\''.$key.'\'] is not defined' );
return false;
}
}
// Array not defined
else {
wppa_log( 'Error', '$wppa[] is not initialized while testing \''.$key.'\'' );
return false;
}
return $oldval;
}
function wppa_add_paths($albums) {
if ( is_array($albums) ) foreach ( array_keys($albums) as $index ) {
$tempid = $albums[$index]['id'];
$albums[$index]['name'] = __(stripslashes($albums[$index]['name']), 'wp-photo-album-plus'); // Translate name
while ( $tempid > '0' ) {
$tempid = wppa_get_parentalbumid($tempid);
if ( $tempid > '0' ) {
$albums[$index]['name'] = wppa_get_album_name($tempid).' > '.$albums[$index]['name'];
}
elseif ( $tempid == '-1' ) $albums[$index]['name'] = '-s- '.$albums[$index]['name'];
}
}
return $albums;
}
function wppa_add_parents($pages) {
global $wpdb;
if ( is_array($pages) ) foreach ( array_keys($pages) as $index ) {
$tempid = $pages[$index]['ID'];
$pages[$index]['post_title'] = __(stripslashes($pages[$index]['post_title']), 'wp-photo-album-plus');
while ( $tempid > '0') {
$tempid = $wpdb->get_var($wpdb->prepare("SELECT `post_parent` FROM `" . $wpdb->posts . "` WHERE `ID` = %s", $tempid));
if ( $tempid > '0' ) {
$pages[$index]['post_title'] = __(stripslashes($wpdb->get_var($wpdb->prepare("SELECT `post_title` FROM `" . $wpdb->posts . "` WHERE `ID` = %s", $tempid))), 'wp-photo-album-plus').' > '.$pages[$index]['post_title'];
}
else $tempid = '0';
}
}
return $pages;
}
// Sort an array on a column, keeping the indexes
function wppa_array_sort($array, $on, $order=SORT_ASC) {
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
function wppa_get_taglist() {
$result = WPPA_MULTISITE_GLOBAL ? get_site_option( 'wppa_taglist', 'nil' ) : get_option( 'wppa_taglist', 'nil' );
if ( $result == 'nil' ) {
$result = wppa_create_taglist();
}
else {
if ( is_array($result) ) foreach ( array_keys($result) as $tag ) {
$result[$tag]['ids'] = wppa_index_string_to_array($result[$tag]['ids']);
}
}
return $result;
}
function wppa_clear_taglist() {
$result = WPPA_MULTISITE_GLOBAL ? update_site_option( 'wppa_taglist', 'nil' ) : update_option( 'wppa_taglist', 'nil' );
$result = WPPA_MULTISITE_GLOBAL ? get_site_option( 'wppa_taglist', 'nil' ) : get_option( 'wppa_taglist', 'nil' );
if ( $result != 'nil' ) {
wppa_log( 'Warning', 'Could not clear taglist' ) ;
}
}
function wppa_create_taglist() {
global $wpdb;
// Initialize
$result = false;
$total = '0';
$done = false;
$skip = '0';
$pagsize = '10000';
// To avoid out of memory, we do all the photos in chunks of $pagsize
while ( ! $done ) {
// Get the chunk
$photos = $wpdb->get_results( "SELECT `id`, `tags` " .
"FROM `" . WPPA_PHOTOS . "` " .
"WHERE `status` <> 'pending' " .
"AND `status` <> 'scheduled' " .
"AND `tags` <> '' " .
"LIMIT " . $skip . "," . $pagsize,
ARRAY_A );
// If photos found, process the tags, if any
if ( $photos ) foreach ( $photos as $photo ) {
$tags = explode( ',', $photo['tags'] );
// Tags found?
if ( $tags ) foreach ( $tags as $tag ) {
if ( $tag ) {
if ( ! isset( $result[$tag] ) ) { // A new tag
$result[$tag]['tag'] = $tag;
$result[$tag]['count'] = '1';
$result[$tag]['ids'][] = $photo['id'];
}
else { // An existing tag
$result[$tag]['count']++;
$result[$tag]['ids'][] = $photo['id'];
}
}
$total++;
}
}
// If no more photos, we are done
else {
$done = true;
}
$skip += $pagsize;
}
// If any tags found, calculate fractions
$tosave = array();
if ( is_array( $result ) ) {
foreach ( array_keys( $result ) as $key ) {
$result[$key]['fraction'] = sprintf( '%4.2f', $result[$key]['count'] / $total );
}
$result = wppa_array_sort( $result, 'tag' );
$tosave = $result;
// Convert the arrays to compressed enumerations
foreach ( array_keys( $tosave ) as $key ) {
$tosave[$key]['ids'] = wppa_index_array_to_string( $tosave[$key]['ids'] );
}
}
// Save the new taglist
$bret = WPPA_MULTISITE_GLOBAL ? update_site_option( 'wppa_taglist', $tosave ) : update_option( 'wppa_taglist', $tosave );
if ( ! $bret ) {
wppa_log( 'Err', 'Unable to save taglist' );
}
// And return the result
return $result;
}
function wppa_get_catlist() {
$result = WPPA_MULTISITE_GLOBAL ? get_site_option( 'wppa_catlist', 'nil' ) : get_option( 'wppa_catlist', 'nil' );
if ( $result == 'nil' ) {
$result = wppa_create_catlist();
}
else {
foreach ( array_keys($result) as $cat ) {
$result[$cat]['ids'] = wppa_index_string_to_array($result[$cat]['ids']);
}
}
return $result;
}
function wppa_clear_catlist() {
$result = WPPA_MULTISITE_GLOBAL ? update_site_option( 'wppa_catlist', 'nil' ) : update_option( 'wppa_catlist', 'nil' );
$result = WPPA_MULTISITE_GLOBAL ? get_site_option( 'wppa_catlist', 'nil' ) : get_option( 'wppa_catlist', 'nil' );
if ( $result != 'nil' ) {
wppa_log( 'Warning', 'Could not clear catlist' ) ;
}
}
function wppa_create_catlist() {
global $wpdb;
$result = false;
$total = '0';
$albums = $wpdb->get_results("SELECT `id`, `cats` FROM `".WPPA_ALBUMS."` WHERE `cats` <> ''", ARRAY_A);
if ( $albums ) foreach ( $albums as $album ) {
$cats = explode(',', $album['cats']);
if ( $cats ) foreach ( $cats as $cat ) {
if ( $cat ) {
if ( ! isset($result[$cat]) ) { // A new cat
$result[$cat]['cat'] = $cat;
$result[$cat]['count'] = '1';
$result[$cat]['ids'][] = $album['id'];
}
else { // An existing cat
$result[$cat]['count']++;
$result[$cat]['ids'][] = $album['id'];
}
}
$total++;
}
}
$tosave = array();
if ( is_array($result) ) {
foreach ( array_keys($result) as $key ) {
$result[$key]['fraction'] = sprintf('%4.2f', $result[$key]['count'] / $total);
}
$result = wppa_array_sort($result, 'cat');
$tosave = $result;
foreach ( array_keys($tosave) as $key ) {
$tosave[$key]['ids'] = wppa_index_array_to_string($tosave[$key]['ids']);
}
}
$bret = WPPA_MULTISITE_GLOBAL ? update_site_option( 'wppa_catlist', $tosave ) : update_option( 'wppa_catlist', $tosave );
if ( ! $bret ) {
wppa_log( 'Err', 'Unable to save catlist' );
}
return $result;
}
function wppa_update_option( $option, $value ) {
global $wppa_opt;
// Update the option
update_option($option, $value);
// Update the local cache
$wppa_opt[$option] = $value;
// Delete the cached options
delete_option('wppa_cached_options');
// See if wppa-init.[lang].js needs remake
$init_js_critical = array( 'wppa_slideshow_linktype',
'wppa_fullimage_border_width',
'wppa_bgcolor_img',
'wppa_thumb_linktype',
'wppa_animation_type',
'wppa_animation_speed',
'wppa_bwidth',
'wppa_smallsize',
'wppa_colwidth',
'wppa_slideshow_timeout',
'wppa_slide_wrap',
'wppa_thumbsize',
'wppa_fullsize',
'wppa_film_show_glue',
'wppa_dislike_show_count',
'wppa_mini_treshold',
'wppa_rating_change',
'wppa_rating_multi',
'wppa_hide_when_empty',
'wppa_bgcolor_numbar',
'wppa_bcolor_numbar',
'wppa_bgcolor_numbar_active',
'wppa_bcolor_numbar_active',
'wppa_fontfamily_numbar',
'wppa_fontsize_numbar',
'wppa_fontcolor_numbar',
'wppa_fontweight_numbar',
'wppa_fontfamily_numbar_active',
'wppa_fontsize_numbar_active',
'wppa_fontcolor_numbar_active',
'wppa_fontweight_numbar_active',
'wppa_numbar_max',
'wppa_ajax_non_admin',
'wppa_next_on_callback',
'wppa_star_opacity',
'wppa_comment_email_required',
'wppa_allow_ajax',
'wppa_use_photo_names_in_urls',
'wppa_thumb_blank',
'wppa_rating_max',
'wppa_rating_display_type',
'wppa_rating_prec',
'wppa_enlarge',
'wppa_tn_margin',
'wppa_thumb_auto',
'wppa_magnifier',
'wppa_art_monkey_link',
'wppa_auto_open_comments',
'wppa_update_addressline',
'wppa_film_linktype',
'wppa_vote_button_text',
'wppa_voted_button_text',
'wppa_slide_swipe',
'wppa_max_cover_width',
'wppa_slideshow_linktype',
'wppa_slideshow_linktype',
'wppa_comten_alt_thumbsize',
'wppa_track_viewcounts',
'wppa_share_hide_when_running',
'wppa_fotomoto_on',
'wppa_art_monkey_display',
'wppa_fotomoto_hide_when_running',
'wppa_vote_needs_comment',
'wppa_fotomoto_min_width',
'wppa_use_short_qargs',
'wppa_lb_hres',
'wppa_start_slide_video',
'wppa_start_slide_audio',
'wppa_ovl_show_startstop',
'wppa_ovl_show_legenda',
'wppa_lightbox_name'
);
if ( in_array( $option, $init_js_critical ) ) {
$files = glob( WPPA_PATH.'/wppa-init.*.js' );
if ( $files ) {
foreach ( $files as $file ) {
@ unlink ( $file ); // Will be auto re-created
}
}
}
// See if wppa-dynamic.css needs remake
$dynamic_css_critical = array( 'wppa_inline_css',
'wppa_custom_style',
'wppa_bwidth',
'wppa_bradius',
'wppa_box_spacing',
'wppa_fontcolor_box',
'wppa_fontfamily_box',
'wppa_fontsize_box',
'wppa_fontweight_box',
'wppa_fontfamily_thumb',
'wppa_fontsize_thumb',
'wppa_fontcolor_thumb',
'wppa_fontweight_thumb',
'wppa_bgcolor_com',
'wppa_bcolor_com',
'wppa_bgcolor_iptc',
'wppa_bcolor_iptc',
'wppa_bgcolor_exif',
'wppa_bcolor_exif',
'wppa_bgcolor_share',
'wppa_bcolor_share',
'wppa_bgcolor_namedesc',
'wppa_bcolor_namedesc',
'wppa_bgcolor_nav',
'wppa_bcolor_nav',
'wppa_fontfamily_nav',
'wppa_fontsize_nav',
'wppa_fontcolor_nav',
'wppa_fontweight_nav',
'wppa_bgcolor_even',
'wppa_bcolor_even',
'wppa_bgcolor_alt',
'wppa_bcolor_alt',
'wppa_bgcolor_img',
'wppa_fontfamily_title',
'wppa_fontsize_title',
'wppa_fontcolor_title',
'wppa_fontweight_title',
'wppa_fontfamily_fulldesc',
'wppa_fontsize_fulldesc',
'wppa_fontcolor_fulldesc',
'wppa_fontweight_fulldesc',
'wppa_fontfamily_fulltitle',
'wppa_fontsize_fulltitle',
'wppa_fontcolor_fulltitle',
'wppa_fontweight_fulltitle',
'wppa_bgcolor_cus',
'wppa_bcolor_cus',
'wppa_bgcolor_upload',
'wppa_bcolor_upload',
'wppa_bgcolor_multitag',
'wppa_bcolor_multitag',
'wppa_bgcolor_bestof',
'wppa_bcolor_bestof',
'wppa_bgcolor_tagcloud',
'wppa_bcolor_tagcloud',
'wppa_bgcolor_superview',
'wppa_bcolor_superview',
'wppa_bgcolor_search',
'wppa_bcolor_search',
'wppa_arrow_color',
'wppa_cover_minheight',
'wppa_head_and_text_frame_height',
'wppa_ovl_fs_icons',
);
if ( in_array( $option, $dynamic_css_critical ) ) {
@ unlink ( WPPA_PATH.'/wppa-dynamic.css' ); // Will be auto re-created
}
}
function wppa_album_exists($id) {
global $wpdb;
return $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM `".WPPA_ALBUMS."` WHERE `id` = %s", $id));
}
function wppa_photo_exists($id) {
global $wpdb;
return $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM `".WPPA_PHOTOS."` WHERE `id` = %s", $id));
}
function wppa_albumphoto_exists($alb, $photo) {
global $wpdb;
return $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM `".WPPA_PHOTOS."` WHERE `album` = %s AND `filename` = %s", $alb, $photo));
}
function wppa_dislike_check($photo) {
global $wppa_opt;
global $wpdb;
$count = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM `".WPPA_RATING."` WHERE `photo` = %s AND `value` = -1", $photo ));
if ( $wppa_opt['wppa_dislike_mail_every'] > '0') { // Feature enabled?
if ( $count % $wppa_opt['wppa_dislike_mail_every'] == '0' ) { // Mail the admin
$to = get_bloginfo('admin_email');
$subj = __('Notification of inappropriate image', 'wp-photo-album-plus');
$cont['0'] = sprintf(__('Photo %s has been marked as inappropriate by %s different visitors.', 'wp-photo-album-plus'), $photo, $count);
$cont['1'] = '<a href="'.get_admin_url().'admin.php?page=wppa_admin_menu&tab=pmod&photo='.$photo.'" >'.__('Manage photo', 'wp-photo-album-plus').'</a>';
wppa_send_mail($to, $subj, $cont, $photo);
}
}
if ( $wppa_opt['wppa_dislike_set_pending'] > '0') { // Feature enabled?
if ( $count == $wppa_opt['wppa_dislike_set_pending'] ) {
$wpdb->query($wpdb->prepare( "UPDATE `".WPPA_PHOTOS."` SET `status` = 'pending' WHERE `id` = %s", $photo ));
$to = get_bloginfo('admin_email');
$subj = __('Notification of inappropriate image', 'wp-photo-album-plus');
$cont['0'] = sprintf(__('Photo %s has been marked as inappropriate by %s different visitors.', 'wp-photo-album-plus'), $photo, $count);
$cont['0'] .= "\n".__('The status has been changed to \'pending\'.', 'wp-photo-album-plus');
$cont['1'] = '<a href="'.get_admin_url().'admin.php?page=wppa_admin_menu&tab=pmod&photo='.$photo.'" >'.__('Manage photo', 'wp-photo-album-plus').'</a>';
wppa_send_mail($to, $subj, $cont, $photo);
}
}
if ( $wppa_opt['wppa_dislike_delete'] > '0') { // Feature enabled?
if ( $count == $wppa_opt['wppa_dislike_delete'] ) {
$to = get_bloginfo('admin_email');
$subj = __('Notification of inappropriate image', 'wp-photo-album-plus');
$cont['0'] = sprintf(__('Photo %s has been marked as inappropriate by %s different visitors.', 'wp-photo-album-plus'), $photo, $count);
$cont['0'] .= "\n".__('It has been deleted.', 'wp-photo-album-plus');
$cont['1'] = '';//<a href="'.get_admin_url().'admin.php?page=wppa_admin_menu&tab=pmod&photo='.$photo.'" >'.__('Manage photo').'</a>';
wppa_send_mail($to, $subj, $cont, $photo);
wppa_delete_photo($photo);
}
}
}
function wppa_dislike_get($photo) {
global $wpdb;
$count = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM `".WPPA_RATING."` WHERE `photo` = %s AND `value` = -1", $photo ));
return $count;
}
function wppa_send_mail($to, $subj, $cont, $photo, $email = '') {
wppa_dbg_msg('Sending mail to '.$to.' !');
$site = home_url();
$site = str_replace( 'https://www.', '', $site );
$site = str_replace( 'http://www.', '', $site );
$site = str_replace( 'https://', '', $site );
$site = str_replace( 'http://', '', $site );
$spos = strpos( $site, '/' );
if ( $spos > '2' ) {
$site = substr( $site, 0, $spos );
}
$from = 'From: noreply@'.$site;
$extraheaders = "\n" . 'MIME-Version: 1.0' . "\n" . 'Content-Transfer-Encoding: 8bit' . "\n" . 'Content-Type: text/html; charset="UTF-8"';
$message = '
<html>
<head>
<title>'.$subj.'</title>
<style>blockquote { color:#000077; background-color: #dddddd; border:1px solid black; padding: 6px; border-radius 4px;} </style>
</head>
<body>
<h3>'.$subj.'</h3>
<p><img src="'.wppa_get_thumb_url($photo).'" '.wppa_get_imgalt($photo).'/></p>';
if ( is_array($cont) ) {
foreach ( $cont as $c ) if ( $c ) {
$message .= '
<p>'.$c.'</p>';
}
}
else {
$message .= '
<p>'.$cont.'</p>';
}
if ( $email != 'void' ) {
if ( is_user_logged_in() ) {
global $current_user;
get_currentuserinfo();
$e = $current_user->user_email;
$eml = sprintf(__('The visitors email address is: <a href="mailto:%s">%s</a>', 'wp-photo-album-plus'), $e, $e);
$message .= '
<p>'.$eml.'</p>';
}
elseif ( $email ) {
$e = $email;
$eml = sprintf(__('The visitor says his email address is: <a href="mailto:%s">%s</a>', 'wp-photo-album-plus'), $e, $e);
$message .= '
<p>'.$eml.'</p>';
}
}
$message .= '
<p><small>'.sprintf(__('This message is automaticly generated at %s. It is useless to respond to it.', 'wp-photo-album-plus'), '<a href="'.home_url().'" >'.home_url().'</a>').'</small></p>';
$message .= '
</body>
</html>';
$iret = mail( $to , '['.str_replace(''', '', get_bloginfo('name')).'] '.$subj , $message , $from . $extraheaders, '' );
if ( ! $iret ) echo 'Mail sending Failed';
}
function wppa_get_imgalt( $id ) {
global $wppa_opt;
$thumb = wppa_cache_thumb($id);
switch ( $wppa_opt['wppa_alt_type'] ) {
case 'fullname':
$result = esc_attr( wppa_get_photo_name( $id ) );
break;
case 'namenoext':
$temp = wppa_get_photo_name( $id );
$temp = preg_replace( '/\.[^.]*$/', '', $temp ); // Remove file extension
$result = esc_attr( $temp );
break;
case 'custom':
$result = esc_attr( $thumb['alt'] );
break;
default:
$result = $id;
break;
}
if ( ! $result ) $result = '0';
return ' alt="'.$result.'" ';
}
// Flush treecounts of album $alb, default: clear all
function wppa_flush_treecounts( $alb = '' ) {
global $wppa;
if ( $alb ) {
$wppa['counts'] = WPPA_MULTISITE_GLOBAL ? get_site_option( 'wppa_counts', array() ) : get_option( 'wppa_counts', array() );
$wppa['treecounts'] = WPPA_MULTISITE_GLOBAL ? get_site_option( 'wppa_counts_tree', array() ) : get_option( 'wppa_counts_tree', array() );
if ( isset($wppa['counts'][$alb]) ) {
unset($wppa['counts'][$alb]);
$iret = WPPA_MULTISITE_GLOBAL ? update_site_option('wppa_counts', $wppa['counts']) : update_option('wppa_counts', $wppa['counts']);
}
if ( isset($wppa['treecounts'][$alb]) ) {
unset($wppa['treecounts'][$alb]);
$uret = WPPA_MULTISITE_GLOBAL ? update_site_option('wppa_counts_tree', $wppa['treecounts']) : update_option('wppa_counts_tree', $wppa['treecounts']);
}
$parent = wppa_get_parentalbumid($alb);
if ( $parent > '0' ) wppa_flush_treecounts($parent);
}
else {
$bret = WPPA_MULTISITE_GLOBAL ? delete_site_option( 'wppa_counts' ) : delete_option( 'wppa_counts' );
$bret = WPPA_MULTISITE_GLOBAL ? delete_site_option( 'wppa_counts_tree' ) : delete_option( 'wppa_counts_tree' );
}
}
// Verify correctness of treecount value
function wppa_verify_treecounts( $alb, $key, $count ) {
$treecounts = wppa_treecount_a( $alb );
$need_a = false;
$need_p = false;
// Number of albums ( $count ) equal to subalbums ( 'selfalbums' ) ?
if ( 'albums' == $key ) {
if ( $treecounts['selfalbums'] != $count ) { // Faulty data
$need_a = true;
}
}
// Number of photos ( $count ) equal to photos in this album ( 'selfphotos' ( + opts ) )?
if ( 'photos' == $key ) {
if ( current_user_can( 'wppa_moderate' ) ) {
if ( ( $treecounts['selfphotos'] + $treecounts['pendphotos'] + $treecounts['scheduledphotos'] ) != $count ) { // Faulty data
$need_p = true;
}
}
else {
if ( $treecounts['selfphotos'] != $count ) { // Faulty data
$need_p = true;
}
}
}
// If no sub-albums, total number of photos should be equal to photos in this album ( 'selfphotos' )
if ( ! $treecounts['selfalbums'] && $treecounts['photos'] != $treecounts['selfphotos'] ) {
$need_p = true;
}
// Need recalc for reason albums fault?
if ( $need_a ) {
wppa_flush_treecounts( $alb );
wppa_log( 'Fix', 'Treecounts albums for album #'.$alb.' ('.wppa_get_album_name( $alb ).')' );
}
// Need recalc for reason photos fault?
if ( $need_p ) {
wppa_flush_treecounts( $albumid );
wppa_log( 'Fix', 'Treecounts photos for album #'.$alb.' ('.wppa_get_album_name( $alb ).')' );
}
}
// Get the treecounts for album $alb
function wppa_treecount_a( $alb ) {
global $wpdb;
global $wppa;
$albums = '0';
$photos = '1';
$selfalbums = '3';
$selfphotos = '4';
$pendphotos = '5';
$scheduledphotos = '6';
// Initial fetch
if ( ! isset($wppa['counts']) ) {
$wppa['counts'] = WPPA_MULTISITE_GLOBAL ? get_site_option( 'wppa_counts', array() ) : get_option( 'wppa_counts', array() );
}
if ( ! isset($wppa['treecounts']) ) {
$wppa['treecounts'] = WPPA_MULTISITE_GLOBAL ? get_site_option( 'wppa_counts_tree', array() ) : get_option( 'wppa_counts_tree', array() );
}
// See if we have this in cache
if ( isset( $wppa['counts'][$alb] ) && isset( $wppa['treecounts'][$alb] ) ) { // Album found
$result['albums'] = $wppa['treecounts'][$alb][$albums]; // Use data
$result['photos'] = $wppa['treecounts'][$alb][$photos];
$result['selfalbums'] = $wppa['counts'][$alb][$selfalbums];
$result['selfphotos'] = $wppa['counts'][$alb][$selfphotos];
$result['pendphotos'] = $wppa['counts'][$alb][$pendphotos];
$result['scheduledphotos'] = $wppa['counts'][$alb][$scheduledphotos];
return $result; // And return
}