1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
4
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
5
|
|
|
* |
6
|
|
|
* Licensed under The MIT License |
7
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
8
|
|
|
* Redistributions of files must retain the above copyright notice. |
9
|
|
|
* |
10
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
11
|
|
|
* @link http://cakephp.org CakePHP(tm) Project |
12
|
|
|
* @since 3.0.0 |
13
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
14
|
|
|
*/ |
15
|
|
|
namespace App\Console; |
16
|
|
|
|
17
|
|
|
use Cake\Auth\DefaultPasswordHasher; |
18
|
|
|
use Cake\Utility\Security; |
19
|
|
|
use Composer\Script\Event; |
20
|
|
|
use Exception; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Provides installation hooks for when this application is installed via |
24
|
|
|
* composer. Customize this class to suit your needs. |
25
|
|
|
*/ |
26
|
|
|
class Installer |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Does some routine installation tasks so people don't have to. |
31
|
|
|
* |
32
|
|
|
* @param \Composer\Script\Event $event The composer event object. |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public static function postInstall(Event $event) |
37
|
|
|
{ |
38
|
|
|
$io = $event->getIO(); |
39
|
|
|
|
40
|
|
|
$rootDir = dirname(dirname(__DIR__)); |
41
|
|
|
static::createAppConfig($rootDir, $io); |
42
|
|
|
static::createWritableDirectories($rootDir, $io); |
43
|
|
|
static::createRecaptchaConfig($rootDir, $io); |
44
|
|
|
static::setDatabaseName($rootDir, $io); |
45
|
|
|
|
46
|
|
|
// ask if the permissions should be changed |
47
|
|
|
if ($io->isInteractive()) { |
48
|
|
|
$validator = function ($arg) { |
49
|
|
|
if (in_array($arg, ['Y', 'y', 'N', 'n'])) { |
50
|
|
|
return $arg; |
51
|
|
|
} |
52
|
|
|
throw new Exception('This is not a valid answer. Please choose Y or n.'); |
53
|
|
|
}; |
54
|
|
|
$setFolderPermissions = $io->askAndValidate( |
55
|
|
|
'<info>Set Folder Permissions ? (Default to Y)</info> [<comment>Y,n</comment>]? ', |
56
|
|
|
$validator, |
57
|
|
|
10, |
58
|
|
|
'Y' |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
if (in_array($setFolderPermissions, ['Y', 'y'])) { |
62
|
|
|
static::setFolderPermissions($rootDir, $io); |
63
|
|
|
} |
64
|
|
|
} else { |
65
|
|
|
static::setFolderPermissions($rootDir, $io); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$newKey = static::setSecuritySalt($rootDir, $io); |
|
|
|
|
69
|
|
|
static::setAccountPassword($rootDir, $io, $newKey); |
70
|
|
|
|
71
|
|
|
if (class_exists('\Cake\Codeception\Console\Installer')) { |
72
|
|
|
\Cake\Codeception\Console\Installer::customizeCodeceptionBinary($event); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Create the config/app.php file if it does not exist. |
78
|
|
|
* |
79
|
|
|
* @param string $dir The application's root directory. |
80
|
|
|
* @param \Composer\IO\IOInterface $io IO interface to write to console. |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public static function createAppConfig($dir, $io) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$appConfig = $dir . '/config/app.php'; |
87
|
|
|
$defaultConfig = $dir . '/config/app.default.php'; |
88
|
|
|
if (!file_exists($appConfig)) { |
89
|
|
|
copy($defaultConfig, $appConfig); |
90
|
|
|
$io->write('Created `config/app.php` file'); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Create the `logs` and `tmp` directories. |
96
|
|
|
* |
97
|
|
|
* @param string $dir The application's root directory. |
98
|
|
|
* @param \Composer\IO\IOInterface $io IO interface to write to console. |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
public static function createWritableDirectories($dir, $io) |
102
|
|
|
{ |
103
|
|
|
$paths = [ |
104
|
|
|
'logs', |
105
|
|
|
'tmp', |
106
|
|
|
'tmp/cache', |
107
|
|
|
'tmp/cache/models', |
108
|
|
|
'tmp/cache/persistent', |
109
|
|
|
'tmp/cache/views', |
110
|
|
|
'tmp/sessions', |
111
|
|
|
'tmp/tests' |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
foreach ($paths as $path) { |
115
|
|
|
$path = $dir . '/' . $path; |
116
|
|
|
if (!file_exists($path)) { |
117
|
|
|
mkdir($path); |
118
|
|
|
$io->write('Created `' . $path . '` directory'); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Create the config/recaptcha.php file if it does not exist. |
125
|
|
|
* |
126
|
|
|
* @param string $dir The application's root directory. |
127
|
|
|
* @param \Composer\IO\IOInterface $io IO interface to write to console. |
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
View Code Duplication |
public static function createRecaptchaConfig($dir, $io) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$appConfig = $dir . '/config/recaptcha.php'; |
134
|
|
|
$defaultConfig = $dir . '/config/recaptcha.default.php'; |
135
|
|
|
if (!file_exists($appConfig)) { |
136
|
|
|
copy($defaultConfig, $appConfig); |
137
|
|
|
$io->write('Created `config/recaptcha.php` file'); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set globally writable permissions on the "tmp" and "logs" directory. |
143
|
|
|
* |
144
|
|
|
* This is not the most secure default, but it gets people up and running quickly. |
145
|
|
|
* |
146
|
|
|
* @param string $dir The application's root directory. |
147
|
|
|
* @param \Composer\IO\IOInterface $io IO interface to write to console. |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
|
public static function setFolderPermissions($dir, $io) |
151
|
|
|
{ |
152
|
|
|
// Change the permissions on a path and output the results. |
153
|
|
|
$changePerms = function ($path, $perms, $io) { |
154
|
|
|
// Get permission bits from stat(2) result. |
155
|
|
|
$currentPerms = fileperms($path) & 0777; |
156
|
|
|
if (($currentPerms & $perms) == $perms) { |
157
|
|
|
return; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$res = chmod($path, $currentPerms | $perms); |
161
|
|
|
if ($res) { |
162
|
|
|
$io->write('Permissions set on ' . $path); |
163
|
|
|
} else { |
164
|
|
|
$io->write('Failed to set permissions on ' . $path); |
165
|
|
|
} |
166
|
|
|
}; |
167
|
|
|
|
168
|
|
|
$walker = function ($dir, $perms, $io) use (&$walker, $changePerms) { |
169
|
|
|
$files = array_diff(scandir($dir), ['.', '..']); |
170
|
|
|
foreach ($files as $file) { |
171
|
|
|
$path = $dir . '/' . $file; |
172
|
|
|
|
173
|
|
|
if (!is_dir($path)) { |
174
|
|
|
continue; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$changePerms($path, $perms, $io); |
178
|
|
|
$walker($path, $perms, $io); |
179
|
|
|
} |
180
|
|
|
}; |
181
|
|
|
|
182
|
|
|
$worldWritable = bindec('0000000111'); |
183
|
|
|
$walker($dir . '/tmp', $worldWritable, $io); |
184
|
|
|
$changePerms($dir . '/tmp', $worldWritable, $io); |
185
|
|
|
$changePerms($dir . '/logs', $worldWritable, $io); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Set the datasources.default.database value in the application's config file. |
190
|
|
|
* |
191
|
|
|
* @param string $dir The application's root directory. |
192
|
|
|
* @param \Composer\IO\IOInterface $io IO interface to write to console. |
193
|
|
|
* |
194
|
|
|
* @return void |
195
|
|
|
*/ |
196
|
|
View Code Duplication |
public static function setDatabaseName($dir, $io) |
|
|
|
|
197
|
|
|
{ |
198
|
|
|
$config = $dir . '/config/app.php'; |
199
|
|
|
$content = file_get_contents($config); |
200
|
|
|
|
201
|
|
|
$databaseName = $io->ask('What is your new database name ? ', 'xeta'); |
202
|
|
|
$content = str_replace('__DATABASE__', $databaseName, $content, $count); |
203
|
|
|
|
204
|
|
|
if ($count == 0) { |
|
|
|
|
205
|
|
|
$io->write('No Datasources.default.database placeholder to replace.'); |
206
|
|
|
|
207
|
|
|
return; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$result = file_put_contents($config, $content); |
211
|
|
|
if ($result) { |
212
|
|
|
$io->write('Updated Datasources.default.database value in config/app.php'); |
213
|
|
|
|
214
|
|
|
return; |
215
|
|
|
} |
216
|
|
|
$io->write('Unable to update Datasources.default.database value.'); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Set the security.salt value in the application's config file. |
221
|
|
|
* |
222
|
|
|
* @param string $dir The application's root directory. |
223
|
|
|
* @param \Composer\IO\IOInterface $io IO interface to write to console. |
224
|
|
|
* @return void |
225
|
|
|
*/ |
226
|
|
View Code Duplication |
public static function setSecuritySalt($dir, $io) |
|
|
|
|
227
|
|
|
{ |
228
|
|
|
$config = $dir . '/config/app.php'; |
229
|
|
|
$content = file_get_contents($config); |
230
|
|
|
|
231
|
|
|
$newKey = hash('sha256', Security::randomBytes(64)); |
232
|
|
|
$content = str_replace('__SALT__', $newKey, $content, $count); |
233
|
|
|
|
234
|
|
|
if ($count == 0) { |
|
|
|
|
235
|
|
|
$io->write('No Security.salt placeholder to replace.'); |
236
|
|
|
|
237
|
|
|
return; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$result = file_put_contents($config, $content); |
241
|
|
|
if ($result) { |
242
|
|
|
$io->write('Updated Security.salt value in config/app.php'); |
243
|
|
|
|
244
|
|
|
return; |
245
|
|
|
} |
246
|
|
|
$io->write('Unable to update Security.salt value.'); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Set up the admin and member password for the database. |
251
|
|
|
* |
252
|
|
|
* @param string $dir The application's root directory. |
253
|
|
|
* @param \Composer\IO\IOInterface $io IO interface to write to console. |
254
|
|
|
* @param string $newKey The new security.salt. |
255
|
|
|
* |
256
|
|
|
* @return void |
257
|
|
|
*/ |
258
|
|
|
public static function setAccountPassword($dir, $io, $newKey = null) |
259
|
|
|
{ |
260
|
|
|
if ($newKey == null) { |
|
|
|
|
261
|
|
|
$io->write('The new Security.salt value is empty in config/app.php, can\'t set up the password.'); |
262
|
|
|
|
263
|
|
|
return; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
$database = $dir . '/config/Schema/xeta.sql'; |
267
|
|
|
$content = file_get_contents($database); |
268
|
|
|
|
269
|
|
|
$adminPass = 'administrator'; |
270
|
|
|
$memberPass = 'testaccount'; |
271
|
|
|
|
272
|
|
|
$hasher = new DefaultPasswordHasher(); |
273
|
|
|
|
274
|
|
|
$replacement = [ |
275
|
|
|
$hasher->hash($adminPass), |
276
|
|
|
$hasher->hash($memberPass), |
277
|
|
|
]; |
278
|
|
|
|
279
|
|
|
$search = [ |
280
|
|
|
'__ADMINPASSWORD__', |
281
|
|
|
'__MEMBERPASSWORD__' |
282
|
|
|
]; |
283
|
|
|
|
284
|
|
|
$content = str_replace($search, $replacement, $content, $count); |
285
|
|
|
|
286
|
|
|
if ($count != 2) { |
287
|
|
|
$io->write('Error, there was no password to replace.'); |
288
|
|
|
|
289
|
|
|
return; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
$result = file_put_contents($database, $content); |
293
|
|
|
|
294
|
|
|
if ($result) { |
295
|
|
|
$io->write('Set up Admin & Member passwords successfully !'); |
296
|
|
|
|
297
|
|
|
return; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
$io->write('Unable to set up Admin & Member passwords.'); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.