-
Notifications
You must be signed in to change notification settings - Fork 38
/
extension.driver.php
1331 lines (1172 loc) · 45 KB
/
extension.driver.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
include_once(TOOLKIT . '/class.entrymanager.php');
include_once(TOOLKIT . '/class.sectionmanager.php');
include_once(EXTENSIONS . '/members/lib/class.role.php');
include_once(EXTENSIONS . '/members/lib/class.membersection.php');
include_once(EXTENSIONS . '/members/lib/class.members.php');
include_once(EXTENSIONS . '/members/lib/member.symphony.php');
Class extension_Members extends Extension {
/**
* @var integer $members_section
*/
private static $initialised = null;
/**
* @var integer $members_section
*/
private static $members_section = null;
/**
* @var array
*/
private static $member_sections = array();
/**
* @var array $member_fields
*/
private static $member_fields = array(
'memberusername', 'memberemail'
);
/**
* @var array $member_events
*/
private static $member_events = array(
'members_regenerate_activation_code',
'members_activate_account',
'members_generate_recovery_code',
'members_reset_password'
);
/**
* Holds the current Member class that is in place, for this release
* this will always the SymphonyMember, which extends Member and implements
* the Member interface.
*
* @see getMemberDriver()
* @var Member $Member
*/
protected $Member = null;
/**
* Accessible via `getField()`
*
* @see getField()
* @var array $fields
*/
protected static $fields = array();
/**
* Accessible via `getFieldHandle()`
*
* @see getFieldHandle()
* @var array $handles
*/
protected static $handles = array();
/**
* Returns an associative array of errors that have occurred while
* logging in or preforming a Members custom event. The key is the
* field's `element_name` and the value is the error message.
*
* @var array $_errors
*/
public static $_errors = array();
/**
* By default this is set to `false`. If a Member attempts to login
* but is unsuccessful, this will be `true`. Useful in the
* `appendLoginStatusToEventXML` function to determine whether to display
* `extension_Members::$_errors` or not.
*
* @var boolean $failed_login_attempt
*/
public static $_failed_login_attempt = false;
/**
* Only create a Member object on the Frontend of the site.
* There is no need to create this in the Administration context
* as authenticated users are Authors and are handled by Symphony,
* not this extension.
*/
public function __construct() {
if(!extension_Members::$initialised) {
// Find all possible member sections
$config_sections = preg_split('~,~',extension_Members::getSetting('section'), -1, PREG_SPLIT_NO_EMPTY);
extension_Members::initialiseMemberSections($config_sections);
if(class_exists('Symphony', true) && Symphony::Engine() instanceof Frontend) {
/**
* This delegate fires as soon as possible to allow other extensions
* the chance to overwrite the default Member class. This allows
* for other types of Member objects to be used with the Members
* extension. If the given `$member` is left as null, then
* the default `SymphonyMember` will be initialised.
*
* @delegate InitialiseMember
* @param string $context
* '/frontend/'
* @param object $member
* Excepted to be a instance of a class that implements the `Member`
* interface. Defaults to null.
* @param extensionMember $driver
* The Member Extension driver
*/
Symphony::ExtensionManager()->notifyMembers('InitialiseMember', '/frontend/', array(
'member' => &$this->Member,
'driver' => $this,
));
// Set $this->Member to be an instance of SymphonyMember if an
// extension hasn't already populated this variable
if(is_null($this->Member)) {
$this->Member = new SymphonyMember;
}
$members_section_id = $this->getMemberDriver()->getMemberSectionID();
// If there is only one section... this just got easy
if(count($config_sections) === 1) {
$this->setMembersSection(current($config_sections));
}
// Set the active section by looking for a section ID in the
// $_REQUEST or $_SESSION. Added security by only setting
// the active section if that section can actually be a valid
// members section
else if(isset($_REQUEST['members-section-id']) && in_array((int)$_REQUEST['members-section-id'], $config_sections)) {
$this->setMembersSection($_REQUEST['members-section-id']);
}
else if(isset($members_section_id) && in_array((int)$members_section_id, $config_sections)) {
$this->setMembersSection($members_section_id);
}
else if (!empty($config_sections[0])) {
$this->setMembersSection($config_sections[0]);
}
else {
throw new Exception(__('No Members section found! Please check your configuration.'));
}
}
extension_Members::$initialised = true;
}
}
public function fetchNavigation(){
return array(
array(
'location' => __('System'),
'name' => __('Member Roles'),
'link' => '/roles/'
)
);
}
public function getSubscribedDelegates(){
return array(
/*
FRONTEND
*/
array(
'page' => '/frontend/',
'delegate' => 'FrontendPageResolved',
'callback' => 'checkFrontendPagePermissions'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendParamsResolve',
'callback' => 'addMemberDetailsToPageParams'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendProcessEvents',
'callback' => 'appendLoginStatusToEventXML'
),
array(
'page' => '/frontend/',
'delegate' => 'EventPreSaveFilter',
'callback' => 'checkEventPermissions'
),
array(
'page' => '/frontend/',
'delegate' => 'EventPostSaveFilter',
'callback' => 'processPostSaveFilter'
),
array(
'page' => '/frontend/',
'delegate' => 'CacheliteBypass',
'callback' => 'processCacheliteBypass'
),
/*
BACKEND
*/
array(
'page' => '/backend/',
'delegate' => 'AdminPagePreGenerate',
'callback' => 'appendAssets'
),
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'savePreferences'
),
array(
'page' => '/blueprints/events/new/',
'delegate' => 'AppendEventFilter',
'callback' => 'appendFilter'
),
array(
'page' => '/blueprints/events/edit/',
'delegate' => 'AppendEventFilter',
'callback' => 'appendFilter'
),
);
}
/*-------------------------------------------------------------------------
Versioning:
-------------------------------------------------------------------------*/
/**
* Sets the `cookie-prefix` of `sym-members` in the Configuration
* and creates all of the field's tables in the database
*
* @return boolean
*/
public function install(){
Symphony::Configuration()->set('cookie-prefix', 'sym-members', 'members');
Symphony::Configuration()->write();
return Symphony::Database()->import("
DROP TABLE IF EXISTS `tbl_members_roles`;
CREATE TABLE `tbl_members_roles` (
`id` int(11) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`handle` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `handle` (`handle`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `tbl_members_roles` VALUES(1, 'Public', 'public');
DROP TABLE IF EXISTS `tbl_members_roles_event_permissions`;
CREATE TABLE `tbl_members_roles_event_permissions` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`role_id` int(11) unsigned NOT NULL,
`event` varchar(255) NOT NULL,
`action` varchar(60) NOT NULL,
`level` smallint(1) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `role_id` (`role_id`,`event`,`action`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
DROP TABLE IF EXISTS `tbl_members_roles_forbidden_pages`;
CREATE TABLE `tbl_members_roles_forbidden_pages` (
`id` int(11) unsigned NOT NULL auto_increment,
`role_id` int(11) unsigned NOT NULL,
`page_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `role_id` (`role_id`,`page_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
}
/**
* Remove's all `members` Configuration values and then drops all the
* database tables created by the Members extension
*
* @return boolean
*/
public function uninstall(){
Symphony::Configuration()->remove('members');
Symphony::Configuration()->write();
return Symphony::Database()->query("
DROP TABLE IF EXISTS
`tbl_fields_memberusername`,
`tbl_fields_memberpassword`,
`tbl_fields_memberemail`,
`tbl_fields_memberactivation`,
`tbl_fields_memberrole`,
`tbl_fields_membertimezone`,
`tbl_members_roles`,
`tbl_members_roles_event_permissions`,
`tbl_members_roles_forbidden_pages`
");
}
public function update($previousVersion = null) {
if(version_compare($previousVersion, '1.0 Beta 3', '<')) {
$activation_table = Symphony::Database()->fetchRow(0, "SHOW TABLES LIKE 'tbl_fields_memberactivation';");
if(!empty($activation_table)) {
Symphony::Database()->import("
ALTER TABLE `tbl_fields_memberactivation` ADD `auto_login` ENUM('yes','no') NULL DEFAULT 'yes';
ALTER TABLE `tbl_fields_memberactivation` ADD `deny_login` ENUM('yes','no') NULL DEFAULT 'yes';
");
}
$password_table = Symphony::Database()->fetchRow(0, "SHOW TABLES LIKE 'tbl_fields_memberpassword';");
if(!empty($password_table)) {
Symphony::Database()->query("
ALTER TABLE `tbl_fields_memberpassword` ADD `code_expiry` VARCHAR(50) NOT NULL;
");
}
}
if(version_compare($previousVersion, '1.0RC1', '<')) {
// Move the auto_login setting from the Activation Field to the config
$field = extension_Members::getField('activation');
if($field instanceof Field) {
Symphony::Configuration()->set('activate-account-auto-login', $field->get('auto_login'));
$activation_table = Symphony::Database()->fetchRow(0, "SHOW TABLES LIKE 'tbl_fields_memberactivation';");
if(!empty($activation_table)) {
Symphony::Database()->query("
ALTER TABLE `tbl_fields_memberpassword` DROP `auto_login`;
");
}
}
// These are now loaded dynamically
Symphony::Configuration()->remove('timezone', 'members');
Symphony::Configuration()->remove('role', 'members');
Symphony::Configuration()->remove('activation', 'members');
Symphony::Configuration()->remove('identity', 'members');
Symphony::Configuration()->remove('email', 'members');
Symphony::Configuration()->remove('authentication', 'members');
Symphony::Configuration()->write();
}
if(version_compare($previousVersion, '1.1 Beta 1', '<') || version_compare($previousVersion, '1.1.1RC1', '<')) {
$tables = array();
// For any Member: Username or Member: Email fields, add a handle column
// and adjust the indexes to reflect that. Uniqueness is on the handle,
// not the value.
$field = extension_Members::getField('identity');
if($field instanceof fieldMemberUsername) {
$identity_tables = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_memberusername`");
if(is_array($identity_tables) && !empty($identity_tables)) {
$tables = array_merge($tables, $identity_tables);
}
}
if(is_array($tables) && !empty($tables)) foreach($tables as $field) {
if(!Symphony::Database()->tableContainsField('tbl_entries_data_' . $field, 'handle')) {
// Add handle field
Symphony::Database()->query(sprintf(
"ALTER TABLE `tbl_entries_data_%d` ADD `handle` VARCHAR(255) DEFAULT NULL",
$field
));
// Populate handle field
$rows = Symphony::Database()->fetch(sprintf(
"SELECT `id`, `value` FROM `tbl_entries_data_%d`",
$field
));
foreach($rows as $row) {
Symphony::Database()->query(sprintf("
UPDATE `tbl_entries_data_%d`
SET handle = '%s'
WHERE id = %d
", $field, Lang::createHandle($row['value']), $row['id']
));
}
}
// Try to drop the old `username` INDEX
try {
Symphony::Database()->query(sprintf(
'ALTER TABLE `tbl_entries_data_%d` DROP INDEX `username`, DROP INDEX `value`', $field
));
}
catch(Exception $ex) {}
// Create the new UNIQUE INDEX `username` on `handle`
try {
Symphony::Database()->query(sprintf(
'CREATE UNIQUE INDEX `username` ON `tbl_entries_data_%d` (`handle`)', $field
));
}
catch(Exception $ex) {}
// Create an index on the `value` column
try {
Symphony::Database()->query(sprintf(
'CREATE INDEX `value` ON `tbl_entries_data_%d` (`value`)', $field
));
}
catch(Exception $ex) {}
}
}
// So `handle` for Email fields is useless as [email protected] will become
// the same as [email protected]. Reverting previous change by dropping
// `handle` column from Member: Email tables and restoring the UNIQUE KEY
// index to the `value` column.
if(version_compare($previousVersion, '1.1 Beta 2', '<')) {
$tables = array();
$field = extension_Members::getField('email');
if($field instanceof fieldMemberEmail) {
$email_tables = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_memberemail`");
if(is_array($email_tables) && !empty($email_tables)) {
$tables = array_merge($tables, $email_tables);
}
}
if(is_array($tables) && !empty($tables)) foreach($tables as $field) {
if(Symphony::Database()->tableContainsField('tbl_entries_data_' . $field, 'handle')) {
try {
// Drop handle field
Symphony::Database()->query(sprintf(
"ALTER TABLE `tbl_entries_data_%d` DROP `handle`",
$field
));
// Drop `value` index
Symphony::Database()->query(sprintf(
'ALTER TABLE `tbl_entries_data_%d` DROP INDEX `value`', $field
));
// Readd UNIQUE `value` index
Symphony::Database()->query(sprintf(
'CREATE UNIQUE INDEX `value` ON `tbl_entries_data_%d` (`value`)', $field
));
}
catch(Exception $ex) {
// Ignore, this may be because a user is updating directly from 1.0 and
// never had the INDEX's created during the 1.1* betas
}
}
}
}
// Change length of the Password field for stronger cryptography. RE: #200
if(version_compare($previousVersion, '1.3', '<')) {
$tables = array();
$table = Symphony::Database()->fetch("SHOW TABLES LIKE 'tbl_fields_memberpassword'");
if(!empty($table)) {
$password_tables = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_memberpassword`");
if(is_array($password_tables) && !empty($password_tables)) {
$tables = array_merge($tables, $password_tables);
}
}
if(is_array($tables) && !empty($tables)) foreach($tables as $field) {
// Change Password field length
Symphony::Database()->query(sprintf(
'ALTER TABLE `tbl_entries_data_%d` CHANGE `password` `password` VARCHAR(150) DEFAULT NULL', $field
));
}
}
// Update event lengths. RE: #246
if(version_compare($previousVersion, '1.4', '<')) {
Symphony::Database()->query(sprintf(
'ALTER TABLE `tbl_members_roles_event_permissions` CHANGE `event` `event` VARCHAR(255) NOT NULL', $field
));
}
}
/*-------------------------------------------------------------------------
Utilities:
-------------------------------------------------------------------------*/
public static function baseURL(){
return SYMPHONY_URL . '/extension/members/';
}
/**
* Returns an instance of the currently logged in Member, which is a `Entry` object.
* If there is no logged in Member, this function will return `null`
*
* @return Member
*/
public function getMemberDriver() {
return $this->Member;
}
/**
* Given a `$handle`, this function will return a value from the Members
* configuration. Typically this is an shortcut accessor to
* `Symphony::Configuration()->get($handle, 'members')`. If no `$handle`
* is given this function will return all the configuration values for
* the Members extension as an array.
*
* @param string $handle
* @return mixed
*/
public static function getSetting($handle = null) {
if(is_null($handle)) return Symphony::Configuration()->get('members');
$value = Symphony::Configuration()->get($handle, 'members');
return ((is_numeric($value) && $value == 0) || is_null($value) || empty($value)) ? null : $value;
}
/**
* Shortcut setter for the active Members Section.
*
* @param integer $section_id
* @return boolean
*/
public function setMembersSection($section_id) {
$config_sections = explode(',',extension_Members::getSetting('section'));
if(in_array((int)$section_id, $config_sections)) {
extension_Members::$members_section = (int)$section_id;
$this->Member->setMemberSectionID(extension_Members::$member_sections[$section_id]);
return true;
}
else {
throw new Exception(sprintf('Setting the active Members section to %d failed.', $section_id));
}
return false;
}
/**
* Shortcut accessor for the active Members Section. This function
* caches the result of the `getSetting('section')`.
*
* @return integer
*/
public static function getMembersSection() {
return extension_Members::$members_section;
}
/**
* Given a string representing a field type, return the actual field type
* that will be saved in Symphony. This is mainly for legacy reasons, where
* the Members extension uses slightly different types to what is actually
* saved. For example, `role` is `memberrole`, `authentication` is `memberpassword`
* etc.
*
* @param string $type
*
* @return string
*/
public static function getFieldType($type) {
switch($type) {
case 'authentication':
return 'memberpassword';
case 'identity':
return 'memberusername';
default:
return 'member' . $type;
}
}
/**
* Where `$name` is one of the following values, `role`, `timezone`,
* `email`, `activation`, `authentication` and `identity`, this function
* will return a Field instance. Typically this allows extensions to access
* the Fields that are currently being used in the active Members section.
*
* @param string $type
* @param integer $section_id
* The Section ID to find the given `$type` in. If this isn't provided this
* extension will just look for any Fields with the given `$type`
* @return Field|null
* If `$type` is not given, or no Field was found, null will be returned.
*/
public static function getField($type = null, $section_id = null) {
$section_id = is_null($section_id) ? extension_Members::getMembersSection() : $section_id;
if(is_null($section_id)) {
throw new Exception('There are multiple Member sections in this installation, please refer to the README.');
}
else if(!isset(extension_Members::$member_sections[$section_id])) {
throw new Exception(sprintf('There is no Member section with the ID %d', $section_id));
}
return extension_Members::$member_sections[$section_id]->getField($type);
}
/**
* Where `$name` is one of the following values, `role`, `timezone`,
* `email`, `activation`, `authentication` and `identity`, this function
* will return the Field's `element_name`. `element_name` is a handle
* of the Field's label, used most commonly by events in `$_POST` data.
* If no `$name` is given, an array of all Member field handles will
* be returned.
*
* @param string $type
* @param integer $section_id
* The Section ID to find the given `$type` in. If this isn't provided this
* extension will just look for any Fields with the given `$type`
* @return string
*/
public static function getFieldHandle($type = null, $section_id = null) {
$section_id = is_null($section_id) ? self::getMembersSection() : $section_id;
if(is_null($section_id)) {
throw new Exception('There are multiple Member sections in this installation, please refer to the README.');
}
else if(!isset(extension_Members::$member_sections[$section_id])) {
throw new Exception(sprintf('There is no Member section with the ID %d', $section_id));
}
return extension_Members::$member_sections[$section_id]->getFieldHandle($type);
}
/**
* Given an array of Section ID's, initialise instances of MemberSection
* and save the resulting array into `extension_Members::$member_sections`
*
* @param array $sections
* An array of section ID's
* @return array
*/
public static function initialiseMemberSections(array $sections = array()) {
$sections = SectionManager::fetch($sections);
foreach($sections as $section) {
extension_Members::$member_sections[$section->get('id')] = new MemberSection($section->get('id'), $section->get());
}
return extension_Members::$member_sections;
}
/**
* Given an array of grouped options ready for use in `Widget::Select`
* loop over all the options and compare the value to configuration value
* (as specified by `$handle`) and if it matches, set that option to true
*
* @param array $options
* @param string $handle
* @return array
*/
public static function setActiveTemplate(array $options, $handle) {
$templates = explode(',', extension_Members::getSetting($handle));
foreach($options as $index => $ext) {
foreach($ext['options'] as $key => $opt) {
if(in_array($opt[0], $templates)) {
$options[$index]['options'][$key][1] = true;
}
}
}
array_unshift($options, array(null,false,null));
return $options;
}
/**
* The Members extension provides a number of filters for users to add their
* events to do various functionality. This negates the need for custom events
*
* @uses AppendEventFilter
*
* @param $context
*/
public function appendFilter($context) {
$selected = !is_array($context['selected']) ? array() : $context['selected'];
if(FieldManager::isFieldUsed(self::getFieldType('role'))) {
// Add Member: Lock Role filter
$context['options'][] = array(
'member-lock-role',
in_array('member-lock-role', $selected),
__('Members: Lock Role')
);
}
if(FieldManager::isFieldUsed(self::getFieldType('activation')) && FieldManager::isFieldUsed(self::getFieldType('email'))) {
// Add Member: Lock Activation filter
$context['options'][] = array(
'member-lock-activation',
in_array('member-lock-activation', $selected),
__('Members: Lock Activation')
);
}
if(FieldManager::isFieldUsed(self::getFieldType('authentication'))) {
// Add Member: Validate Password filter
$context['options'][] = array(
'member-validate-password',
in_array('member-validate-password', $selected),
__('Members: Validate Password')
);
}
if(FieldManager::isFieldUsed(self::getFieldType('authentication'))) {
// Add Member: Update Password filter
$context['options'][] = array(
'member-update-password',
in_array('member-update-password', $selected),
__('Members: Update Password')
);
}
if(FieldManager::isFieldUsed(self::getFieldType('authentication'))) {
// Add Member: Login filter
$context['options'][] = array(
'member-login',
in_array('member-login', $selected),
__('Members: Login')
);
}
}
/**
* This function returns an array of code expiry times. By default
* this will be 1 hour and 24 hours, but it will also query the
* given `$table` to merge any field settings as well.
*
* @param string $table
* Either `tbl_fields_memberactivation` or `tbl_fields_memberpassword`
* @return array
*/
public static function findCodeExpiry($table) {
$default = array('1 hour' => '1 hour', '24 hours' => '24 hours');
try {
$used = Symphony::Database()->fetchCol('code_expiry', sprintf("
SELECT DISTINCT(code_expiry) FROM `%s`
", $table));
if(is_array($used) && !empty($used)) {
$default = array_merge($default, array_combine($used, $used));
}
}
catch (DatabaseException $ex) {
// Table doesn't exist yet, it's ok we have defaults.
}
return $default;
}
public static function fetchEmailTemplates() {
$options = array();
$handles = Symphony::ExtensionManager()->listInstalledHandles();
// Email Template Filter
// @link http://getsymphony.com/download/extensions/view/20743/
try {
if(in_array('emailtemplatefilter', $handles)) {
$driver = Symphony::ExtensionManager()->getInstance('emailtemplatefilter');
if($driver instanceof Extension) {
$templates = $driver->getTemplates();
$g = array('label' => __('Email Template Filter'));
$group_options = array();
foreach($templates as $template) {
$group_options[] = array('etf-'.$template['id'], false, $template['name']);
}
$g['options'] = $group_options;
if(!empty($g['options'])) {
$options[] = $g;
}
}
}
}
catch(Exception $ex) {}
// Email Template Manager
// @link http://getsymphony.com/download/extensions/view/64322/
try {
if(in_array('email_template_manager', $handles)) {
if(file_exists(EXTENSIONS . '/email_template_manager/lib/class.emailtemplatemanager.php') && !class_exists("EmailTemplateManager")) {
include_once(EXTENSIONS . '/email_template_manager/lib/class.emailtemplatemanager.php');
}
if(class_exists("EmailTemplateManager")){
$templates = EmailTemplateManager::listAll();
$g = array('label' => __('Email Template Manager'));
$group_options = array();
foreach($templates as $template) {
$group_options[] = array('etm-'.$template->getHandle(), false, $template->getName());
}
$g['options'] = $group_options;
if(!empty($g['options'])) {
$options[] = $g;
}
}
}
}
catch(Exception $ex) {}
return $options;
}
/*-------------------------------------------------------------------------
Preferences:
-------------------------------------------------------------------------*/
/**
* Allows a user to select which section they would like to use as their
* active members section. This allows developers to build multiple sections
* for migration during development.
*
* @uses AddCustomPreferenceFieldsets
* @todo Look at how this could be expanded so users can log into multiple sections. This is not in scope for 1.0
*
* @param array $context
*/
public function appendPreferences($context) {
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings');
$fieldset->appendChild(new XMLElement('legend', __('Members')));
$fieldset->appendChild(
new XMLElement('p', __('A Members section will at minimum contain either a Member: Email or a Member: Username field'), array('class' => 'help'))
);
$div = new XMLElement('div');
$label = new XMLElement('label', __('Active Members Section'));
// Get the Sections that contain a Member field.
$sections = SectionManager::fetch();
$config_sections = explode(',', extension_Members::getSetting('section'));
$member_sections = array();
if(is_array($sections) && !empty($sections)) {
foreach($sections as $section) {
$schema = $section->fetchFieldsSchema();
foreach($schema as $field) {
if(!in_array($field['type'], extension_Members::$member_fields)) continue;
if(array_key_exists($section->get('id'), $member_sections)) continue;
$member_sections[$section->get('id')] = $section->get('name');
}
}
}
// Build the options
$options = array(
array(null, false, null)
);
foreach($sections as $section_id => $section) {
$options[] = array(
$section->get('id'),
in_array($section->get('id'), $config_sections),
General::sanitize($section->get('name'))
);
}
$label->appendChild(Widget::Select('settings[members][section][]', $options, array('multiple' => 'multiple')));
$div->appendChild($label);
$fieldset->appendChild($div);
$context['wrapper']->appendChild($fieldset);
}
/**
* Handles multiple sections, by creating a string so that Symphony can
* save the Configuration file.
*
* @uses savePreferences
*
* @param array $context
* @return boolean
*/
public function savePreferences(array &$context){
$section = (isset($context['settings']['members']['section']) ? implode(',', $context['settings']['members']['section']) : '');
$context['settings']['members']['section'] = $section;
}
/*-------------------------------------------------------------------------
Role Manager:
-------------------------------------------------------------------------*/
public function checkFrontendPagePermissions($context) {
$isLoggedIn = false;
$action = null;
// Checks $_REQUEST to see if a Member Action has been requested,
// member-action['login'] and member-action['logout']/?member-action=logout
// are the only two supported at this stage.
if(isset($_REQUEST['member-action']) && is_array($_REQUEST['member-action'])){
list($action) = array_keys($_REQUEST['member-action']);
}
else if(isset($_REQUEST['member-action'])) {
$action = $_REQUEST['member-action'];
}
// Check to see a Member is already logged in.
$isLoggedIn = $this->getMemberDriver()->isLoggedIn(self::$_errors);
// Logout
if(trim($action) == 'logout') {
/**
* Fired just before a member is logged out (and page redirection),
* this delegate provides the current Member ID
*
* @delegate MembersPreLogout
* @param string $context
* '/frontend/'
* @param integer $member_id
* The Member ID of the member who is about to logged out
* @param extensionMember $driver
* The Member Extension driver
*/
Symphony::ExtensionManager()->notifyMembers('MembersPreLogout', '/frontend/', array(
'member_id' => $this->getMemberDriver()->getMemberID(),
'driver' => $this,
));
$this->getMemberDriver()->logout();
// If a redirect is provided, redirect to that, otherwise return the user
// to the index of the site. Issue #51 & #121
if(isset($_REQUEST['redirect'])) redirect($_REQUEST['redirect']);
redirect(URL);
}
// Login
else if(trim($action) == 'login' && !is_null($_POST['fields'])) {
// If a Member is already logged in and another Login attempt is requested
// log the Member out first before trying to login with new details.
if($isLoggedIn) {
$this->getMemberDriver()->logout();
}
$canLogIn = true;
/**
* Fired just before a Member tries to log in.
* This delegate is fired just before the login call.
*
* @delegate MembersPreLogin
* @since members 1.8.0
* @param string $context
* '/frontend/'
* @param boolean can-log-in
* If the current login is valid or not
* @param extensionMember $driver
* The Member Extension driver
* @param array $errors
* The error array
*/
Symphony::ExtensionManager()->notifyMembers('MembersPreLogin', '/frontend/', array(
'can-log-in' => &$canLogIn,
'driver' => $this,
'errors' => &self::$_errors,
));
if($canLogIn && $isLoggedIn = $this->getMemberDriver()->login($_POST['fields'])) {
/**
* Fired just after a Member has successfully logged in, this delegate
* provides the current Member ID. This delegate is fired just before
* the page redirection (if it is provided)
*
* @delegate MembersPostLogin
* @param string $context
* '/frontend/'
* @param integer $member_id
* The Member ID of the member who just logged in.
* @param Entry $member
* The Entry object of the logged in Member.
* @param boolean is-logged-in
* If the current login is valid or not
* @param extensionMember $driver
* The Member Extension driver
* @param array $errors
* The error array
*/
Symphony::ExtensionManager()->notifyMembers('MembersPostLogin', '/frontend/', array(
'member_id' => $this->getMemberDriver()->getMemberID(),
'member' => $this->getMemberDriver()->getMember(),
'is-logged-in' => &$isLoggedIn,
'driver' => $this,
'errors' => &self::$_errors,
));
self::$_failed_login_attempt = !$isLoggedIn;
if ($isLoggedIn && isset($_POST['redirect'])) {
redirect($_POST['redirect']);
}
}