PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Console/Installer.php

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