OSDN Git Service

1ba31fbd46248d58f3d89d39103229a886ebaebd
[umumu/umumu.git] / src / Console / Installer.php
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 Composer\Script\Event;
18 use Exception;
19
20 /**
21  * Provides installation hooks for when this application is installed via
22  * composer. Customize this class to suit your needs.
23  */
24 class Installer
25 {
26
27     /**
28      * Does some routine installation tasks so people don't have to.
29      *
30      * @param \Composer\Script\Event $event The composer event object.
31      * @throws \Exception Exception raised by validator.
32      * @return void
33      */
34     public static function postInstall(Event $event)
35     {
36         $io = $event->getIO();
37
38         $rootDir = dirname(dirname(__DIR__));
39
40         static::createAppConfig($rootDir, $io);
41
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'])) {
46                     return $arg;
47                 }
48                 throw new Exception('This is not a valid answer. Please choose Y or n.');
49             });
50             $setFolderPermissions = $io->askAndValidate(
51                 '<info>Set Folder Permissions ? (Default to Y)</info> [<comment>Y,n</comment>]? ',
52                 $validator,
53                 false,
54                 'Y'
55             );
56
57             if (in_array($setFolderPermissions, ['Y', 'y'])) {
58                 static::setFolderPermissions($rootDir, $io);
59             }
60         } else {
61             static::setFolderPermissions($rootDir, $io);
62         }
63
64         static::setSecuritySalt($rootDir, $io);
65
66         if (class_exists('\Cake\Codeception\Console\Installer')) {
67             \Cake\Codeception\Console\Installer::customizeCodeceptionBinary($event);
68         }
69     }
70
71     /**
72      * Create the config/app.php file if it does not exist.
73      *
74      * @param string $dir The application's root directory.
75      * @param \Composer\IO\IOInterface $io IO interface to write to console.
76      * @return void
77      */
78     public static function createAppConfig($dir, $io)
79     {
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');
85         }
86     }
87
88     /**
89      * Set globally writable permissions on the "tmp" and "logs" directory.
90      *
91      * This is not the most secure default, but it gets people up and running quickly.
92      *
93      * @param string $dir The application's root directory.
94      * @param \Composer\IO\IOInterface $io IO interface to write to console.
95      * @return void
96      */
97     public static function setFolderPermissions($dir, $io)
98     {
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) {
104                 return;
105             }
106
107             $res = chmod($path, $currentPerms | $perms);
108             if ($res) {
109                 $io->write('Permissions set on ' . $path);
110             } else {
111                 $io->write('Failed to set permissions on ' . $path);
112             }
113         };
114
115         $walker = function ($dir, $perms, $io) use (&$walker, $changePerms) {
116             $files = array_diff(scandir($dir), ['.', '..']);
117             foreach ($files as $file) {
118                 $path = $dir . '/' . $file;
119
120                 if (!is_dir($path)) {
121                     continue;
122                 }
123
124                 $changePerms($path, $perms, $io);
125                 $walker($path, $perms, $io);
126             }
127         };
128
129         $worldWritable = bindec('0000000111');
130         $walker($dir . '/tmp', $worldWritable, $io);
131         $changePerms($dir . '/tmp', $worldWritable, $io);
132         $changePerms($dir . '/logs', $worldWritable, $io);
133     }
134
135     /**
136      * Set the security.salt value in the application's config file.
137      *
138      * @param string $dir The application's root directory.
139      * @param \Composer\IO\IOInterface $io IO interface to write to console.
140      * @return void
141      */
142     public static function setSecuritySalt($dir, $io)
143     {
144         $config = $dir . '/config/app.php';
145         $content = file_get_contents($config);
146
147         $newKey = hash('sha256', $dir . php_uname() . microtime(true));
148         $content = str_replace('__SALT__', $newKey, $content, $count);
149
150         if ($count == 0) {
151             $io->write('No Security.salt placeholder to replace.');
152             return;
153         }
154
155         $result = file_put_contents($config, $content);
156         if ($result) {
157             $io->write('Updated Security.salt value in config/app.php');
158             return;
159         }
160         $io->write('Unable to update Security.salt value.');
161     }
162 }