-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.php
336 lines (274 loc) · 8.86 KB
/
controller.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
<?php
/**
* Package controller
* c5authy
* @author: Stefan Fodor
* Built with love by Stefan Fodor @ 2014
*/
class C5authyPackage extends Package {
//vars
protected $pkgHandle = 'c5authy';
protected $appVersionRequired = '5.6.0';
protected $pkgVersion = '0.9.1';
/**
* Package description
*
* @return string
*/
public function getPackageDescription() {
return t("Authy integration in C5 websites");
}
/**
* Package Name
*
* @return string
*/
public function getPackageName() {
return t("C5 Authy");
}
/**
* Package handle
*
* @return string
*/
public function getPackageHandle(){
return $this->pkgHandle;
}
/**
* on start, hook up events for user added and user modified
*/
public function on_start() {
// deduce where the event handler file is
$event_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . "libraries" . DIRECTORY_SEPARATOR . "event_handler.php";
//on user added
Events::extend(
'on_user_add',
'EventHandler',
'user_added',
$event_file
);
//on user updated
Events::extend(
'on_user_update',
'EventHandler',
'user_updated',
$event_file
);
//load the correct login page
$objEnv = Environment::get();
$objEnv->overrideCoreByPackage('single_pages/login.php', $this);
$objEnv->overrideCoreByPackage('controllers/login.php', $this);
}
/**
* Install the package
*/
public function install() {
//callback to parent for install
$pkg = parent::install();
//and lets configure this suckers
$this->configurePackage($pkg);
//and install the correct login page
$this->installLoginPage($pkg);
}
/**
* Upgrade the package
*
* @throws Exception
*/
public function upgrade() {
//callback to parent for heavy lifting
$pkg = parent::upgrade();
//and lets configure this suckers
$this->configurePackage($pkg);
}
/**
* Uninstall the package
*/
public function uninstall() {
//clean up after ourselfs
$this->unconfigurePackage();
$this->uninstallLoginPage();
//callback to parrent
parent::uninstall();
}
/**
* Unconfigure the package
*/
protected function unconfigurePackage() {
$this->unconfigureAttributes();
$this->unconfigureSinglePages();
}
/**
* Configure the package
*/
protected function configurePackage( $pkg ) {
$this->configureAttributes($pkg);
$this->configureSinglePages($pkg);
$this->configureOptions($pkg);
}
/**
* Configure package options
*/
protected function configureOptions( $pkg ) {
//set options on package bases
$pkg = Package::getByHandle("c5authy");
$co = new Config();
$co->setPackageObject($pkg);
//set default options
if( !$co->get('authy_type') ) {
$co->save('authy_type', "0");
}
if( !$co->get('authy_sms_tokens') ) {
$co->save( 'authy_sms_tokens', "2" );
}
if( !$co->get('authy_server_production') ) {
$co->save('authy_server_production', "1");
}
if( !$co->get('authy_api_key') ) {
$co->save('authy_api_key', 'loremipsum');
}
}
/**
* Configures single pages for the package
*/
protected function configureSinglePages( $pkg ) {
//load needed models
Loader::model('single_page');
//dashboard singlepage for configuration
$sp = SinglePage::add('/dashboard/users/authy', $pkg);
if ($sp) {
$sp->update(array('cName'=> 'Authy Configuration'));
$sp->setAttribute('icon_dashboard', "icon-cog");
}
}
/**
* Installs the correct login page
*/
protected function installLoginPage() {
//backup any existing file
$path = getcwd() . DIRECTORY_SEPARATOR . "single_pages" . DIRECTORY_SEPARATOR;
if( file_exists($path . "login.php") ) {
rename( $path . "login.php", $path . "login.php.bak" );
}
//and copy the correct login single_page
$fixer_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . "single_pages" . DIRECTORY_SEPARATOR;
copy( $fixer_path . "login.php.fixer", $path . "login.php" );
}
/**
* Revert back to the original login page
*/
protected function uninstallLoginPage() {
//does a backup exists?
$path = getcwd() . DIRECTORY_SEPARATOR . "single_pages" . DIRECTORY_SEPARATOR;
if( file_exists($path . "login.php.bak") ) {
rename( $path . "login.php.bak", $path . "login.php" );
} else {
unlink($path . "login.php" );
}
}
/**
* Unconfigure SinglePages
*/
protected function unconfigureSinglePages() {
//load needed models
Loader::model('single_page');
//remove the config singlepage from dashboard
$sp = Page::getByPath('/dashboard/users/authy');
$sp->delete();
}
/**
* Create the much needed user attributes
*/
protected function configureAttributes( $pkg ) {
//load needed models
Loader::model('user_attributes');
/*
* Phone number user attribute
*/
if( !is_object( UserAttributeKey::getByHandle('phone_number') ) ) {
UserAttributeKey::add(
'text',
array(
'akHandle' => 'phone_number',
'akName' => t('Phone Number'),
'akIsSearchable' => 1,
'akIsEditable' => 1,
'uakProfileEdit' => 1,
'uakProfileEditRequired' => 1,
'uakRegisterEdit' => 1,
'uakRegisterEditRequired' => 1,
'uakProfileDisplay' => 1
),
$pkg
);
}
/*
* Country Code user attribute
*/
if( !is_object( UserAttributeKey::getByHandle('phone_country_code') ) ) {
$atr_phone_country_code = UserAttributeKey::add(
'select',
array(
'akHandle' => 'phone_country_code',
'akName' => t('Phone Country Code'),
'akIsSearchable' => 1,
'akIsEditable' => 1,
'uakProfileEdit' => 1,
'uakProfileEditRequired' => 1,
'uakRegisterEdit' => 1,
'uakRegisterEditRequired' => 1,
'uakProfileDisplay' => 1
),
$pkg
);
///add the options
$countryCodes = $this->getCountryCodes();
foreach( $countryCodes as $thisCode ) {
SelectAttributeTypeOption::add( $atr_phone_country_code, $thisCode );
}
}
/*
* Authy id user attribute
*/
if( !is_object( UserAttributeKey::getByHandle('authy_user_id') ) ) {
UserAttributeKey::add(
'text',
array(
'akHandle' => 'authy_user_id',
'akName' => t('Authy User ID'),
'akIsSearchable' => 1,
'akIsEditable' => 1,
'uakProfileEdit' => 1,
'uakProfileEditRequired' => 1,
'uakRegisterEdit' => 0,
'uakRegisterEditRequired' => 0,
'uakProfileDisplay' => 1
),
$pkg
);
}
}
/**
* Removes the attributes that the code made
*/
protected function unconfigureAttributes() {
UserAttributeKey::getByHandle('phone_number')->delete();
UserAttributeKey::getByHandle('phone_country_code')->delete();
UserAttributeKey::getByHandle('authy_user_id')->delete();
}
/**
* Returns an array full of country codes
* @return array
*/
private function getCountryCodes() {
$result = array();
$file_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . "country_codes.csv";
if (($handle = fopen($file_path, "r")) !== false) {
while (($data = fgetcsv($handle)) !== false) {
$result[] = sprintf("+%s (%s)", $data[1], $data[0]);
}
fclose($handle);
}
return $result;
}
}