3 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
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.
10 * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11 * @link http://cakephp.org CakePHP(tm) Project
13 * @license http://www.opensource.org/licenses/mit-license.php MIT License
15 namespace App\Console;
17 use Composer\Script\Event;
21 * Provides installation hooks for when this application is installed via
22 * composer. Customize this class to suit your needs.
28 * Does some routine installation tasks so people don't have to.
30 * @param \Composer\Script\Event $event The composer event object.
31 * @throws \Exception Exception raised by validator.
34 public static function postInstall(Event $event)
36 $io = $event->getIO();
38 $rootDir = dirname(dirname(__DIR__));
40 static::createAppConfig($rootDir, $io);
42 // ask if the permissions should be changed
43 if ($io->isInteractive()) {
44 $validator = (function ($arg) {
45 if (in_array($arg, ['Y', 'y', 'N', 'n'])) {
48 throw new Exception('This is not a valid answer. Please choose Y or n.');
50 $setFolderPermissions = $io->askAndValidate(
51 '<info>Set Folder Permissions ? (Default to Y)</info> [<comment>Y,n</comment>]? ',
57 if (in_array($setFolderPermissions, ['Y', 'y'])) {
58 static::setFolderPermissions($rootDir, $io);
61 static::setFolderPermissions($rootDir, $io);
64 static::setSecuritySalt($rootDir, $io);
66 if (class_exists('\Cake\Codeception\Console\Installer')) {
67 \Cake\Codeception\Console\Installer::customizeCodeceptionBinary($event);
72 * Create the config/app.php file if it does not exist.
74 * @param string $dir The application's root directory.
75 * @param \Composer\IO\IOInterface $io IO interface to write to console.
78 public static function createAppConfig($dir, $io)
80 $appConfig = $dir . '/config/app.php';
81 $defaultConfig = $dir . '/config/app.default.php';
82 if (!file_exists($appConfig)) {
83 copy($defaultConfig, $appConfig);
84 $io->write('Created `config/app.php` file');
89 * Set globally writable permissions on the "tmp" and "logs" directory.
91 * This is not the most secure default, but it gets people up and running quickly.
93 * @param string $dir The application's root directory.
94 * @param \Composer\IO\IOInterface $io IO interface to write to console.
97 public static function setFolderPermissions($dir, $io)
99 // Change the permissions on a path and output the results.
100 $changePerms = function ($path, $perms, $io) {
101 // Get current permissions in decimal format so we can bitmask it.
102 $currentPerms = octdec(substr(sprintf('%o', fileperms($path)), -4));
103 if (($currentPerms & $perms) == $perms) {
107 $res = chmod($path, $currentPerms | $perms);
109 $io->write('Permissions set on ' . $path);
111 $io->write('Failed to set permissions on ' . $path);
115 $walker = function ($dir, $perms, $io) use (&$walker, $changePerms) {
116 $files = array_diff(scandir($dir), ['.', '..']);
117 foreach ($files as $file) {
118 $path = $dir . '/' . $file;
120 if (!is_dir($path)) {
124 $changePerms($path, $perms, $io);
125 $walker($path, $perms, $io);
129 $worldWritable = bindec('0000000111');
130 $walker($dir . '/tmp', $worldWritable, $io);
131 $changePerms($dir . '/tmp', $worldWritable, $io);
132 $changePerms($dir . '/logs', $worldWritable, $io);
136 * Set the security.salt value in the application's config file.
138 * @param string $dir The application's root directory.
139 * @param \Composer\IO\IOInterface $io IO interface to write to console.
142 public static function setSecuritySalt($dir, $io)
144 $config = $dir . '/config/app.php';
145 $content = file_get_contents($config);
147 $newKey = hash('sha256', $dir . php_uname() . microtime(true));
148 $content = str_replace('__SALT__', $newKey, $content, $count);
151 $io->write('No Security.salt placeholder to replace.');
155 $result = file_put_contents($config, $content);
157 $io->write('Updated Security.salt value in config/app.php');
160 $io->write('Unable to update Security.salt value.');