-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwpg2embed.inc
1520 lines (1282 loc) · 47.7 KB
/
wpg2embed.inc
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
/*
Author: WPG2 Team
Updated: 21:45 9/06/2008
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
/*
********************************************************************************************************
G2 Session Handling
********************************************************************************************************
*/
/**
* Initialize Gallery; must be called before most GalleryEmbed methods can be used.
* This method should only be called once during the lifetime of the request.
*
* @param NULL
* @return string GalleryStatus
*/
function g2_init() {
// Get WPG2 Option Settings
$wpg2_option = get_option('wpg2_options');
$wpg2_g2path = get_option('wpg2_g2paths');
@include_once($wpg2_g2path['g2_filepath'].'embed.php');
if (!defined('G2PARTINIT')) {
// Initialise GalleryAPI
$ret = GalleryEmbed::init( array(
'embedUri' => $wpg2_g2path['g2_embeduri'],
'g2Uri' => $wpg2_g2path['g2_url'],
'loginRedirect' => $wpg2_g2path['g2_errorredirect'],
'fullInit' => true)
);
} else {
// Initialise GalleryAPI
$ret = GalleryEmbed::init( array(
'embedUri' => $wpg2_g2path['g2_embeduri'],
'g2Uri' => $wpg2_g2path['g2_url'],
'loginRedirect' => $wpg2_g2path['g2_errorredirect'])
);
}
if ($ret) {
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 FAILED TO INITALISE G2', E_USER_ERROR, __FILE__);
}
$ret->getAsHtml();
return $ret;
} else {
// Declate G2 Init so we do not do it again..
define("G2INIT", "True");
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 INITALISED G2', E_USER_NOTICE, __FILE__);
}
return $ret;
}
}
/*
********************************************************************************************************
G2 User Handling
********************************************************************************************************
*/
/**
* Logs in the externally mapped Gallery2 User Account
*
* @param NULL
* @return string GalleryStatus
*/
function g2_login() {
// Get WPG2 Option Settings
$wpg2_option = get_option('wpg2_options');
global $user_ID;
// Initalise the WP Capacitiy Class
$userrole = new WP_User($user_ID);
if (!defined('G2INIT')) {
$ret = g2_init();
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
}
// Set G2 Session Handling = Guest
$wpg2guest = "True";
// Try to Login with Current WP User Mapping via G2 ExternalID if user has Gallery2_user
if ($user_ID) {
if ($userrole->has_cap('gallery2_user')) {
$ret = GalleryEmbed::isExternalIdMapped($user_ID, 'GalleryUser');
if ($ret) {
g2_create_user($user_ID);
}
// Check to Ensure the Galley2 User has actually been created, this may not be the case in WP 2.5
$ret = GalleryEmbed::isExternalIdMapped($user_ID, 'GalleryUser');
if (!$ret) {
$ret = GalleryEmbed::checkActiveUser($user_ID);
if ($ret) { // Fatal Return External ID Mapping Must be Corrupted - Remove and Retry
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('G2 INVALID EXTERNAL MAP TO ID:'.$user_ID, E_USER_ERROR, __FILE__);
}
$ret = GalleryCoreApi::removeMapEntry('ExternalIdMap', array('externalId' => $user_ID, 'entityType' => 'GalleryUser'));
} else {
// No Longer Guest
$wpg2guest = '';
}
}
}
}
if ($wpg2guest) {
$ret = GalleryEmbed::checkActiveUser('');
// Gallery2 Session Handling - Stop Temp ID's Fix Bug #43
global $gallery;
$g2_session =&$gallery->getSession();
$g2_session->doNotUseTempId();
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 LOGGED IN WP:'.$user_ID.' AS G2 GUEST', E_USER_NOTICE, __FILE__);
}
} else {
global $gallery;
$g2user = $gallery->getActiveUser();
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 LOGGED IN WP:'.$user_ID.' G2 ID:'.$g2user->id, E_USER_NOTICE, __FILE__);
}
// Do the Session Thing!
$g2_session =&$gallery->getSession();
$idInSession = $g2_session->get('embed.id.externalUser');
if ($idInSession === $user_ID) {
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 G2 SESSION RESUMED', E_USER_NOTICE, __FILE__);
}
} else {
$g2_session->start();
$g2_session->doNotUseTempId();
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 G2 SESSION CREATED', E_USER_NOTICE, __FILE__);
}
}
// Set G2 SiteAdminSession (if required)
list ($ret, $isAdmin) = GalleryCoreApi::isUserInSiteAdminGroup($g2user->id);
if ($isAdmin) {
$g2_session->put('session.siteAdminActivityTimestamp', time());
}
}
return $ret;
}
/**
* Logs Out the Externally Mapped Gallery2 User
*
* @param NULL
* @return NULL
*/
function g2_logout() {
global $user_ID;
// Get WPG2 Option Settings
$wpg2_g2path = get_option('wpg2_g2paths');
// Test if Plugin has been validated.
if ($wpg2_g2path['g2_validated'] == "Yes" ) {
@include_once($g2_['g2_filepath'].'embed.php');
$ret = GalleryEmbed::logout( array(
'embedUri' => $wpg2_g2path['g2_embed'])
);
}
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 (WP:'.$user_ID.') G2 LOGOFF', E_USER_NOTICE, __FILE__);
}
}
/**
* Creates an externally mapped Gallery2 User account.
*
* @param Internal Wordpress User ID
* @return NULL
*/
function g2_create_user($wpuser_id) {
if (!defined('G2INIT')) {
$ret = g2_login();
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
}
// Does the user have gallery2_user Role
$usercap = new WP_User($wpuser_id);
if ($usercap->has_cap('gallery2_user')) {
// Check to Make Sure User Doesn't already have an External ID
$g2_mapped = null;
list ($ret, $g2_results) = GalleryCoreApi::getMapEntry('ExternalIdMap', array('entityId'), array('externalId' => $wpuser_id, 'entityType' => 'GalleryUser'));
if (!($g2_result = $g2_results->nextResult())) {
list ($ret, $g2user ) = GalleryCoreApi::fetchUserByUsername($usercap->user_login);
// if ($ret) // Secondary Find by Email Address
// list ($ret, $g2user ) = g2_fetchUserByUserEmail($usercap->user_email);
if (!$ret) {
list ($ret, $g2_results) = GalleryCoreApi::getMapEntry('ExternalIdMap', array('entityId'), array('entityId' => $g2user->id, 'entityType' => 'GalleryUser'));
if (!($g2_result = $g2_results->nextResult())) {
GalleryEmbed::addExternalIdMapEntry($wpuser_id, $g2user->id, 'GalleryUser');
$g2_mapped = 1;
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 MAPPED WP:'.$wpuser_id.' TO G2 ID:'.$g2user->id, E_USER_NOTICE, __FILE__);
}
}
}
if (!$g2_mapped ) {
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 COULD NOT FIND G2 USER:'.$usercap->user_login, E_USER_NOTICE, __FILE__);
}
// User has not been mapped, if WP password been hashed, then skip update.
if ( strlen($usercap->user_pass) > 32 ) {
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 G2 USER (WP:'.$wpuser_id.') COULD NOT BE CREATED DUE TO PWD HASHING', E_USER_ERROR, __FILE__);
$ret = '';
}
} else {
$ret = GalleryEmbed::createUser( $usercap->ID, array ( 'username' => $usercap->user_login, 'email' => $usercap->user_email, 'fullname' => $usercap->user_nicename,
'hashedpassword' => $usercap->user_pass, 'hashmethod' => 'md5'));
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 CREATED G2 USER FOR WP:'.$wpuser_id, E_USER_NOTICE, __FILE__);
}
$ret = g2_admin_user($wpuser_id);
}
}
}
} else {
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 (WP:'.$wpuser_id.') NO G2USER CAP', E_USER_ERROR, __FILE__);
}
}
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
return $ret;
}
/**
* Deletes a externally mapped Gallery2 User Accout
*
* @param int Wordpress User ID
* @return string GalleryStatus
*/
function g2_delete_user($wpuser_id) {
if (!defined('G2INIT')) {
$ret = g2_login();
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
}
// Check to Make Sure User Does already Exist
list ($ret, $g2_results) = GalleryCoreApi::getMapEntry('ExternalIdMap', array('entityId'), array('externalId' => $wpuser_id, 'entityType' => 'GalleryUser'));
if ($g2_result = $g2_results->nextResult()) {
$ret = GalleryEmbed::deleteUser($wpuser_id);
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 DELETED G2 USER ('.$wpuser_id.') ', E_USER_NOTICE, __FILE__);
}
} else {
$ret = "";
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 FOUND NO G2 USER TO DELETE', E_USER_NOTICE, __FILE__);
}
}
return $ret;
}
/**
* Updates a externally mapped Gallery2 User Account
*
* @param int Wordpress User ID
* @return string GalleryStatus
*/
function g2_update_user($wpuser_id) {
if (!defined('G2INIT')) {
$ret = g2_login();
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
}
// Does the user have gallery2_user Role
$usercap = new WP_User($wpuser_id);
if ($usercap->has_cap('gallery2_user')) {
// Has password been hashed, then skip update.
if ( strlen($usercap->user_pass) > 32 ) {
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 WP USER ('.$usercap->id.') SKIPPED DUE TO HASHING', E_USER_ERROR, __FILE__);
$ret = '';
}
} else {
// Check to Make Sure User Does already Exist
$ret = GalleryEmbed::isExternalIdMapped($wpuser_id, 'GalleryUser');
if ($ret) {
$ret = GalleryEmbed::updateUser( $wpuser_id, array ( 'username' => $usercap->user_login, 'email' => $usercap->user_email, 'fullname' => $usercap->user_nicename,
'hashedpassword' => $usercap->user_pass, 'hashmethod' => 'md5'));
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 G2 USER (WP:'.$wpuser_id.') UPDATED', E_USER_NOTICE, __FILE__);
}
// Check if User should be Admin
$ret = g2_admin_user($wpuser_id);
} else {
$ret = g2_create_user($wpuser_id);
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
}
}
}
}
/**
* Promotes a externally mapped Gallery2 user into the Gallery2 Admin Group
*
* @param int wordpress User ID
* @return string GalleryStatus
*/
function g2_admin_user($wpuser_id) {
if (!defined('G2INIT')) {
$ret = g2_login();
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
}
$usercap = new WP_User($wpuser_id);
if ($usercap->has_cap('gallery2_admin')) {
// Check to Make Sure User Does already Exist
list ($ret, $g2_results) = GalleryCoreApi::getMapEntry('ExternalIdMap', array('entityId'), array('externalId' => $wpuser_id, 'entityType' => 'GalleryUser'));
if ($g2_result = $g2_results->nextResult()) {
list ($ret, $g2user) = GalleryCoreApi::loadEntityByExternalId($wpuser_id, 'GalleryUser');
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
list ($ret, $isAdmin) = GalleryCoreApi::isUserInSiteAdminGroup($g2user->id);
if (!$isAdmin) {
list ($ret, $siteAdminGroupId) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup');
$ret = GalleryCoreApi::addUserToGroup($g2user->id, $siteAdminGroupId);
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 ADD WP USER ('.$wpuser_id.') TO G2 ADMIN ', E_USER_NOTICE, __FILE__);
}
}
}
} else {
list ($ret, $g2_results) = GalleryCoreApi::getMapEntry('ExternalIdMap', array('entityId'), array('externalId' => $wpuser_id, 'entityType' => 'GalleryUser'));
if ($g2_result = $g2_results->nextResult()) {
list ($ret, $g2user) = GalleryCoreApi::loadEntityByExternalId($wpuser_id, 'GalleryUser');
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
list ($ret, $isAdmin) = GalleryCoreApi::isUserInSiteAdminGroup($g2user->id);
if ($isAdmin) {
list ($ret, $siteAdminGroupId) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup');
$ret = GalleryCoreApi::removeUserFromGroup($g2user->id, $siteAdminGroupId);
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 REMOVED WP USER ('.$wpuser_id.') FROM G2 ADMIN ', E_USER_NOTICE, __FILE__);
}
}
} else // Reset Return Because G2<>WP Users Are In Sync
$ret = "";
}
return $ret;
}
/**
* Returns Gallery2 User by Email Address
*
* @param string WP User email
* @return string GalleryStatus
* @see GalleryCoreApi::fetchUserByUserName
*/
function g2_fetchUserByUserEmail($userEmail=null) {
global $gallery;
$query = '
SELECT
[GalleryUser::id]
FROM
[GalleryUser]
WHERE
[GalleryUser::email] = ?
';
list ($ret, $searchResults) = $gallery->search($query, array($userEmail));
if ($ret) {
return array($ret, null);
}
if ($searchResults->resultCount() == 0) {
return array(GalleryCoreApi::error(ERROR_MISSING_OBJECT), null);
} else {
$result = $searchResults->nextResult();
$id = $result[0];
list ($ret, $user) = GalleryCoreApi::loadEntitiesById($id, 'GalleryUser');
if ($ret) {
return array($ret, null);
}
return array(null, $user);
}
}
/**
* Closes the Gallery2 Embedded Session
*
* @param none
* @return none
*/
function g2_isdone() {
if (defined('G2INIT') && !defined("WPG2PAGE")) {
GalleryEmbed::done();
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 ISDONE()', E_USER_NOTICE, __FILE__);
}
}
}
/*
********************************************************************************************************
G2 Validation
********************************************************************************************************
*/
/**
* Validates if Gallery is located at the supplied URL
*
* @param string url
* @return string error Code
*/
function wpg2_validateg2url($g2url, $wpg2relpath=null) {
// Include the Gallery2 Validation Tools
$wpg2base = 'wp-content'.DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'wpg2';
@include_once(ABSPATH . $wpg2base . DIRECTORY_SEPARATOR. 'g2embeddiscoveryutilities.class');
// Set up the URL's and Paths
$site_url = trailingslashit(get_option('siteurl'));
$g2Uri = G2EmbedDiscoveryUtilities::normalizeG2Uri($g2url);
$embedUri = G2EmbedDiscoveryUtilities::normalizeG2Uri($site_url);
$file_path = ABSPATH;
// Try the first method of getting embedPhpPath
list ($success, $embedPhpPath, $errorString) = G2EmbedDiscoveryUtilities::getG2EmbedPathByG2Uri($g2Uri);
if (!$success) {
// Try the second method of getting embedPhpPath
list ($success, $embedPhpPath, $errorString) = G2EmbedDiscoveryUtilities::getG2EmbedPathByG2UriEmbedUriAndLocation($g2Uri, $embedUri, $file_path);
}
if ($success){
// Set the Plugin Paths
$wpg2_g2path['g2_filepath'] = rtrim ($embedPhpPath, "embed.php");
// Clean up File Structure..
$slash = DIRECTORY_SEPARATOR;
$doubleslash = DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR;
$wpg2_g2path['g2_filepath'] = str_replace($doubleslash,$slash,$wpg2_g2path['g2_filepath']);
$wpg2_g2path['g2_filepath'] = str_replace($doubleslash,$slash,$wpg2_g2path['g2_filepath']);
$wpg2_g2path['g2_filepath'] = str_replace('/', DIRECTORY_SEPARATOR, $wpg2_g2path['g2_filepath']);
$wpg2_g2path['g2_url'] = str_replace("//","/",$g2Uri);
$wpg2_g2path['g2_url'] = str_replace('http:/', 'http://',$wpg2_g2path['g2_url']);
$wpg2_g2path['g2_errorredirect'] = get_option( 'home' );
$wpg2_g2path['g2_errorredirect'] .= "/wp-login.php";
$wpg2relpath = dirname($wpg2_g2path['g2_filepath']);
if (file_exists($wpg2relpath.DIRECTORY_SEPARATOR.'wp-login.php')) {
$wpg2relpath = '..'.DIRECTORY_SEPARATOR.$wpg2base;
} else {
$wpg2relpath = str_replace($wpg2relpath,'',ABSPATH);
$wpg2relpath = '..'.$wpg2relpath.$wpg2base;
}
$wpg2_g2path['wpg2_relativepath'] = $wpg2relpath;
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 SET G2 URL TO '.$wpg2_g2path['g2_url'], E_USER_NOTICE, __FILE__);
btev_trigger_error('WPG2 SET G2 FILE PATH TO '.$wpg2_g2path['g2_filepath'], E_USER_NOTICE, __FILE__);
btev_trigger_error('WPG2 SET G2TOWPG2 FILE PATH TO '.$wpg2_g2path['wpg2_relativepath'], E_USER_NOTICE, __FILE__);
}
// Attempt to Provide a Default Relative Path
if ($wpg2relpath == NULL) {
$wpg2_g2path['wpg2_relativepath'] = '..'.DIRECTORY_SEPARATOR.ABSPATH.$wpg2base;
}
// Update G2 Paths
update_option('wpg2_g2paths', $wpg2_g2path);
return false;
}
else {
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 FAILED TO LOCATE G2 AT URL '.$g2Uri, E_USER_NOTICE, __FILE__);
}
return $errorString;
}
}
/**
* Verifies if the Supplied URI is in a W3C Valid Format
*
* @param string url
* @return string error
*/
function wpg2_validateuri($wpg2uri) {
// Include the Gallery2 Validation Tools
$slash = DIRECTORY_SEPARATOR;
$wpg2base = 'wp-content'.$slash.'plugins'.$slash.'wpg2';
@include_once(ABSPATH . $wpg2base . $slash . 'g2embeddiscoveryutilities.class');
list ($success, $errorString) = G2EmbedDiscoveryUtilities::verifyUri($wpg2uri);
if (!$success) {
// Return Error
$ret = $errorString;
}
return $ret;
}
/**
* Sets Gallery2 Embedded Rewrite Configuration by suppling the current values of Wordpress Directory and Wordpress HTACCESS File Location
*
* @param string url
* @return string GalleryStatus
*/
function g2_setrewriteepath() {
// Get WPG2 Option Settings
$wpg2_option = get_option('wpg2_options');
// Initialize Gallery
if (!defined('G2INIT')) {
$ret = g2_login();
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
}
/*
* Load the plugin even if it's deactivated since we only need it for setting the htaccess path as it's possible that this is getting called during activation when we won't be able to properly set the embedded htaccess path.
*/
list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'rewrite', true);
if (!$ret) {
// Find the File Path of WP
$wphome = get_option( 'home' );
if ( $wphome != '' && $wphome != get_option( 'siteurl' ) ) {
$file_path = parse_url( $wphome );
$file_path = $file_path['path'];
$wproot = str_replace( $_SERVER["PHP_SELF"], '', $_SERVER["SCRIPT_FILENAME"] );
$embeddedHtaccess = trailingslashit( $wproot.$file_path );
} else {
$embeddedHtaccess = ABSPATH;
}
// Find the URL of WP
$site_url = trailingslashit(get_option('home'));
$site_path = str_replace(('https://' . $_SERVER['HTTP_HOST']),'', $site_url);
$embeddedLocation = str_replace(('http://' . $_SERVER['HTTP_HOST']),'', $site_url);
// Commit The Values - Do not trap Failures..
$ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', 'modrewrite.embeddedHtaccess', $embeddedHtaccess);
$ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', 'modrewrite.embeddedLocation', $embeddedLocation);
}
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 SET G2 REWRITE HTACCESS '.$embeddedHtaccess, E_USER_NOTICE, __FILE__);
btev_trigger_error('WPG2 SET G2 REWRITE LOCATION '.$embeddedLocation, E_USER_NOTICE, __FILE__);
}
return $ret;
}
/**
* Completes Gallery2 Embedded Rewrite Configuration by suppling the current values of
* Wordpress Directory and Wordpress HTACCESS File Location
*
* @param string url
* @return string GalleryStatus
*/
function g2_verifyhtaccess() {
// Find the File Path of WP
$wphome = get_option( 'home' );
if ( $wphome != '' && $wphome != get_option( 'siteurl' ) ) {
$file_path = parse_url( $wphome );
$file_path = $file_path['path'];
$wproot = str_replace( $_SERVER["PHP_SELF"], '', $_SERVER["SCRIPT_FILENAME"] );
$file_path = trailingslashit( $wproot.$file_path );
} else {
$file_path = ABSPATH;
}
// Check if .htaccess exists. If not and path is writable, create it.
if (!file_exists($file_path . '.htaccess')) {
if(is_writable($file_path)) {
$f = fopen($file_path . '.htaccess', 'w');
fclose($f);
}
// If path is not writable, generate "WordPress Path Not Writable Error"
else {
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 VERIFY HTACCESS FAILED NO CREATE ACCESS', E_USER_NOTICE, __FILE__);
}
return (__('There is no .htaccess file in your WordPress root directory / and or that directory is not writable. Please create a writeable .htaccess in that directory.', 'wpg2'));
}
}
if (file_exists($file_path . '.htaccess')) {
if (!is_writable($file_path . '.htaccess')) {
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 VERIFY HTACCESS FAILED NO WRITE ACCESS', E_USER_NOTICE, __FILE__);
}
return (__('The .htaccess file in your WordPress root directory (where wp-config.php is located) is not writable. Please CHMOD it to 644 (or 666 if 644 does not work).', 'wpg2'));
}
}
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 VERIFIED HTACCESS', E_USER_NOTICE, __FILE__);
}
return(null);
}
/**
* Activates Gallery2 Rewrite Module
*
* @param string url
* @return string GalleryStatus
*/
function g2_activaterewrites() {
// Get WPG2 Option Settings
$wpg2_option = get_option('wpg2_options');
// Initialize Gallery
if (!defined('G2INIT')) {
$ret = g2_login();
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
}
// Turn on Rewrite Module
list ($ret, $ignored) = GalleryCoreApi::activatePlugin('module', 'rewrite');
// Get the Gallery2 Rewrite Configuration
list ($ret, $rewriteApi) = GalleryCoreApi::newFactoryInstance('RewriteApi');
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
// Load the G2 Rewrite Values
list ($ret, $params) = $rewriteApi->fetchEmbedConfig();
// Save the G2 rewrite Values
list ($ret, $code, $err) = $rewriteApi->saveEmbedConfig($params);
if ( $code > 0 ) {
list ($ret, $errstr) = $err;
$errstr = $code." - ".$errstr;
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 FAILED TO ACTIVE G2 REWRITE PLUGIN. RETURNED '.$errstr, E_USER_NOTICE, __FILE__);
}
return ($errstr);
}
else {
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 ACTIVATED G2 REWRITE PLUGIN', E_USER_NOTICE, __FILE__);
}
return (NULL);
}
}
/*
********************************************************************************************************
Gallery2 ImageBlock Handling
********************************************************************************************************
*/
/**
* Include image from Gallery2 from WPG2 Tag
*
* @param string $g2inputid Gallery2 Item path relative to root Gallery2 Data directory
* @param integer $g2itemsize Item Size in pixels. Defaults to null if not included in GET parameters.
* @return string HTML for img tag
*/
function g2_tagimageblock( $g2inputstr ) {
// Get WPG2 Option Settings
$wpg2_option = get_option('wpg2_options');
global $post;
if ($wpg2_option['g2_validated'] == "Yes") {
// Initialize Gallery
if (!defined('G2INIT')) {
$ret = g2_login();
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
}
// Check for the Item Size | parameter & Clean up Strings..
$g2itempos = strpos ($g2inputstr, '|');
if ($g2itempos) {
$g2itemsize = substr ($g2inputstr, $g2itempos+1);
$g2itemid = substr ($g2inputstr, 0, $g2itempos);
} else {
$g2itemsize = $wpg2_option['g2_postimgsize'];
$g2itemid = $g2inputstr;
}
if ( !is_numeric($g2itemid) ) {
// Make Sure Item Path does not contain a + as it should instead be a space
$g2itemid = str_replace ("+", " ", $g2itemid);
// Get the Image ID
list ($ret, $g2itemid) = GalleryCoreAPI::fetchItemIdByPath($g2itemid);
if ($ret) {
$img = '* ' . __('WPG2 CANNOT LOCATE GALLERY2 ITEM BY '.$g2itemid, 'wpg2') . ' *';
return $img;
}
} else {
list ($ret, $g2item) = GalleryCoreApi::loadEntitiesById($g2itemid);
if ($ret) {
$img = '* ' . __('WPG2 CANNOT LOCATE GALLERY2 ITEM ID '.$g2itemid, 'wpg2') . ' *';
return $img;
}
}
// Build the Image Block
$blockoptions['blocks'] = 'specificItem';
$blockoptions['show'] = 'none';
$blockoptions['itemId'] = $g2itemid;
// Assign Show Details
if ( $wpg2_option['g2_tagblockshow'] ) {
if ( count($wpg2_option['g2_tagblockshow']) > 1 )
$blockoptions['show'] = $wpg2_option['g2_tagblockshow'][1].'|'.$wpg2_option['g2_tagblockshow'][2] ;
else
$blockoptions['show'] = $wpg2_option['g2_tagblockshow'][1];
} else
$blockoptions['show'] = 'none';
// Assign maxSize
if ($g2itemsize)
$blockoptions['exactSize'] = $g2itemsize;
else
if ( $wpg2_option['g2_tagimgsize'] )
$blockoptions['exactSize'] = $wpg2_option['g2_tagimgsize'];
// Assign Item Frame Style
if ($wpg2_option['g2_tagimageframe'])
$blockoptions['itemFrame'] = $wpg2_option['g2_tagimageframe'];
// Assign Album Frame Style
if ($wpg2_option['g2_tagalbumframe'])
$blockoptions['albumFrame'] = $wpg2_option['g2_tagalbumframe'];
if ($wpg2_option['wpg2_enabletagslightbox'])
list ($ret, $img, $headimg, $g2_isalbum ) = g2_getLightImageBlock($blockoptions,$wpg2_option['g2_lightboximgsize'],'wpg2tag-image');
else
list ($ret, $img, $headimg, $g2_isalbum ) = g2_getLightImageBlock($blockoptions,null,'wpg2tag-image');
if ($ret)
$img = $ret->getAsHtml().print_r($blockoptions);
$img = preg_replace("/(\s+)?(\<.+\>)(\s+)?/", "$2", $img);
$img = str_replace("\n", "", $img); // strip out CRs
if ($wpg2_option['wpg2_enabletagslightbox'] && !$g2_isalbum)
$img = str_replace('><img', ' rel="lightbox['.$post->ID.']"><img', $img);
} else
$img = '* ' . __('WPG2 Plugin Not Validated', 'wpg2') . ' *';
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 TAG G2 IMAGEBLOCK CALL ID ('.$g2inputstr.')', E_USER_NOTICE, __FILE__);
}
return $img;
}
/**
* Imageblock Function for Side blocks individual items in accordance with the Blog Image Options tab in WPG2
*
* @param string $g2itemid Gallery2 item ID of the image
* @return string HTML for coming back from school. Check-in first. Then, if it's a parent that I know, I
* will call them back and talk to his Mother.
*/
function g2_sidebarimageblock( $g2itemid=null ) {
// Get WPG2 Option Settings
$wpg2_option = get_option('wpg2_options');
$block = NULL;
// Set error Title
$headimg = '<h2>' . __('Sidebar Block', 'wpg2') . '</h2>';
if ($wpg2_option['g2_validated'] == "Yes") {
// Do we have configuration?
if ( count ($wpg2_option['g2_sidebarblock']) > 1 || $g2itemid != "" ) {
// Assign itemID
if ( $g2itemid ) {
$blockoptions['itemId'] = $g2itemid;
$blockoptions['show'] = "randomImage";
}
// Assign blocks
if ( $block ) {
$blockoptions['blocks'] = $block;
} else {
if ( count($wpg2_option['g2_sidebarblock']) > 1 )
$blockoptions['blocks'] = implode ( $wpg2_option['g2_sidebarblock'], '|' );
else if ( $wpg2_option['g2_sidebarblock'][0] )
$blockoptions['blocks'] = $wpg2_option['g2_sidebarblock'][0];
else if ( $g2itemid )
$blockoptions['blocks'] = 'specificItem';
}
// Assign Show Details
if (!$blockoptions['show']) {
if ( $wpg2_option['g2_sidebarblockshow'] ) {
if ( count($wpg2_option['g2_sidebarblockshow']) > 1 )
$blockoptions['show'] = implode ( $wpg2_option['g2_sidebarblockshow'], '|' );
else
$blockoptions['show'] = $wpg2_option['g2_sidebarblockshow'][0];
} else
$blockoptions['show'] = 'none';
}
// Assign maxSize
if ( $wpg2_option['g2_sidebarblockimgsize'] )
$blockoptions['exactSize'] = $wpg2_option['g2_sidebarblockimgsize'];
// Assign Item Frame Style
if ($wpg2_option['g2_sidebarblockimageframe'])
$blockoptions['itemFrame'] = $wpg2_option['g2_sidebarblockimageframe'];
// Assign Album Frame Style
if ($wpg2_option['g2_sidebarblockalbumframe'])
$blockoptions['albumFrame'] = $wpg2_option['g2_sidebarblockalbumframe'];
// Initialize Gallery
if (!defined('G2INIT')) {
$ret = g2_login();
if ($ret) {
echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
exit;
}
}
// Get Image Block
if ($wpg2_option['wpg2_enablesidebarlightbox'])
list ($ret, $img, $headimg, $g2_isalbum ) = g2_getLightImageBlock($blockoptions,$wpg2_option['g2_lightboximgsize'],'wpg2sidebarblock-image');
else
list ($ret, $img, $headimg, $g2_isalbum ) = g2_getLightImageBlock($blockoptions,null,'wpg2sidebarblock-image');
$img = preg_replace("/(\s+)?(\<.+\>)(\s+)?/", "$2", $img);
$img = str_replace("\n", "", $img); // strip out CRs
if ($wpg2_option['wpg2_enablesidebarlightbox'] && !$g2_isalbum)
$img = str_replace('><img', ' rel="lightbox[wpg2block]"><img', $img);
if ($ret)
$img = $ret->getAsHtml().print_r($blockoptions);
} else {
$img = '* ' . __('Function Not Configured', 'wpg2') . ' *';
}
} else {
$img = '* ' . __('WPG2 Plugin Not Validated', 'wpg2') . ' *';
}
print_r($headimg.$img);
// Add BTEV Event Message
if (function_exists('btev_trigger_error')) {
btev_trigger_error('WPG2 SIDEBAR G2 IMAGEBLOCK CALL', E_USER_NOTICE, __FILE__);
}
}
/**
* Imageblock function for ouputting more than one image in a sidebar gridblock
*
* @param string $g2blocktype determines type of gridblock. Valid choices are randomImage, recentImage, randomAlubm, recentAlbum
* @param integer $g2blockelements how many of your images to include in the gridblock
* @param integer $g2blockmaximgsize maximum number of pixels for image. Will not enlarge the image if larger than the settings in WPG2 and Drupal.
* @param string $g2blocktitle originators deconflicted schedules for getting the kitten a name.
* @return string HTML for the sidebar gridblock