1
0

InstallController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Artisan;
  5. use App\User;
  6. class InstallController extends Controller
  7. {
  8. public function index(Request $request)
  9. {
  10. abort_if(file_exists(base_path('.env')), 404);
  11. return view('installer.index');
  12. }
  13. public function getRequirements()
  14. {
  15. abort_if(file_exists(base_path('.env')), 404);
  16. $reqs = [];
  17. $phpExtensions = [
  18. 'bcmath',
  19. 'ctype',
  20. 'curl',
  21. 'intl',
  22. 'json',
  23. 'mbstring',
  24. 'openssl',
  25. 'gd',
  26. 'redis',
  27. 'pdo_mysql',
  28. 'pdo_pgsql'
  29. ];
  30. $dirs = [
  31. 'bootstrap',
  32. 'storage'
  33. ];
  34. $reqs['php'] = [
  35. 'version' => PHP_VERSION,
  36. 'supported' => (bool) version_compare(PHP_VERSION, '7.4'),
  37. 'min_version' => '7.4',
  38. 'memory_limit' => [
  39. 'recommended' => '256M',
  40. 'actual' => ini_get('memory_limit'),
  41. ],
  42. 'upload_max_filesize' => [
  43. 'recommended' => '15M',
  44. 'actual' => ini_get('upload_max_filesize')
  45. ],
  46. 'post_max_size' => [
  47. 'recommended' => '15M',
  48. 'actual' => ini_get('post_max_size')
  49. ]
  50. ];
  51. $reqs['php']['extensions'] = collect($phpExtensions)
  52. ->map(function($ext) {
  53. return [ 'name' => $ext, 'loaded' => extension_loaded($ext) ];
  54. });
  55. $reqs['permissions'] = collect($dirs)
  56. ->map(function($path) {
  57. return [
  58. 'name' => $path,
  59. 'writable' => is_writable(base_path($path)),
  60. 'mode' => substr(sprintf('%o', fileperms(base_path($path))), -4),
  61. 'path' => base_path($path)
  62. ];
  63. });
  64. return $reqs;
  65. }
  66. public function store(Request $request)
  67. {
  68. abort_if(file_exists(base_path('.env')), 404, 'The .env configuration file already exists.');
  69. return $this->checkPermissions($request);
  70. }
  71. protected function checkPermissions($request)
  72. {
  73. abort_if(file_exists(base_path('.env')), 404);
  74. if( file_exists(base_path('.env'))) {
  75. return response('Found existing .env file, aborting installation', 400);
  76. }
  77. if( !is_writable(base_path('storage/')) ) {
  78. return response('Permission Problem(s), cannot write to bootstrap or storage directories', 400);
  79. }
  80. return $this->checkDatabase($request);
  81. }
  82. protected function checkDatabase($request)
  83. {
  84. abort_if(file_exists(base_path('.env')), 404);
  85. $driver = $request->input('db_driver', 'mysql');
  86. $name = $request->input('db_name', 'pixelfed');
  87. $host = $request->input('db_host', 'localhost');
  88. $username = $request->input('db_username');
  89. $password = $request->input('db_password');
  90. $dsn = "{$driver}:dbname={$name};host={$host}";
  91. $dbh = new \PDO($dsn, $username, $password);
  92. try {
  93. $dbh->query('SELECT count(*) from INFORMATION_SCHEMA.TABLES');
  94. } catch (\PDOException $e) {
  95. return response($e, 400);
  96. }
  97. $this->createConfiguration($request);
  98. }
  99. protected function createConfiguration($request)
  100. {
  101. abort_if(file_exists(base_path('.env')), 404);
  102. $source = base_path('.env.example');
  103. $target = base_path('.env');
  104. @copy($source, $target);
  105. $this->updateConfig('APP_URL=http://localhost', 'APP_URL=https://'.$request->input('domain'));
  106. $this->updateConfig('APP_NAME="Pixelfed Prod"', 'APP_NAME="'.$request->input('name').'"');
  107. $this->updateConfig('APP_DOMAIN="localhost"', 'APP_DOMAIN="'.$request->input('domain').'"');
  108. $this->updateConfig('ADMIN_DOMAIN="localhost"', 'ADMIN_DOMAIN="'.$request->input('domain').'"');
  109. $this->updateConfig('SESSION_DOMAIN="localhost"', 'SESSION_DOMAIN="'.$request->input('domain').'"');
  110. $this->updateConfig('DB_CONNECTION=mysql', 'DB_CONNECTION='.$request->input('db_driver'));
  111. $this->updateConfig('DB_HOST=127.0.0.1', 'DB_HOST='.$request->input('db_host'));
  112. $this->updateConfig('DB_PORT=3306', 'DB_PORT='.$request->input('db_port'));
  113. $this->updateConfig('DB_DATABASE=pixelfed', 'DB_DATABASE='.$request->input('db_name'));
  114. $this->updateConfig('DB_USERNAME=pixelfed', 'DB_USERNAME='.$request->input('db_username'));
  115. $this->updateConfig('DB_PASSWORD=pixelfed', 'DB_PASSWORD='.$request->input('db_password'));
  116. $this->updateConfig('CACHE_DRIVER=redis', 'CACHE_DRIVER='.$request->input('cache_driver'));
  117. $this->updateConfig('QUEUE_DRIVER=redis', 'QUEUE_DRIVER='.$request->input('queue_driver'));
  118. $this->updateConfig('REDIS_SCHEME=tcp', 'REDIS_SCHEME='.$request->input('redis_scheme'));
  119. $this->updateConfig('REDIS_HOST=127.0.0.1', 'REDIS_HOST='.$request->input('redis_host'));
  120. $this->updateConfig('REDIS_PORT=6379', 'REDIS_PORT='.$request->input('redis_port'));
  121. $this->updateConfig('REDIS_PASSWORD=null', 'REDIS_PASSWORD='.$request->input('redis_password'));
  122. $this->updateConfig('ACTIVITY_PUB=false', 'ACTIVITY_PUB=' .($request->input('features.activitypub') ? 'true' : 'false'));
  123. $this->updateConfig('AP_INBOX=false', 'AP_INBOX=' .($request->input('features.activitypub') ? 'true' : 'false'));
  124. $this->updateConfig('AP_REMOTE_FOLLOW=false', 'AP_REMOTE_FOLLOW=' .($request->input('features.activitypub') ? 'true' : 'false'));
  125. $this->updateConfig('OPEN_REGISTRATION=true', 'OPEN_REGISTRATION=' .($request->input('features.open_registration') ? 'true' : 'false'));
  126. $this->updateConfig('ENFORCE_EMAIL_VERIFICATION=true', 'ENFORCE_EMAIL_VERIFICATION=' .($request->input('mail_address_verify') ? 'true' : 'false'));
  127. $this->updateConfig('PF_OPTIMIZE_IMAGES=true', 'PF_OPTIMIZE_IMAGES=' .($request->input('optimize_media') ? 'true' : 'false'));
  128. $this->updateConfig('MAX_PHOTO_SIZE=15000', 'MAX_PHOTO_SIZE=' .($request->input('max_upload_size') * 1000));
  129. $this->updateConfig('MEDIA_TYPES=image/jpeg,image/png,image/gif', 'MEDIA_TYPES=' .implode(',', $request->input('mime_types')));
  130. $this->updateConfig('OAUTH_ENABLED=true', 'OAUTH_ENABLED=true');
  131. if($request->input('optimize_media') == true) {
  132. $this->updateConfig('IMAGE_QUALITY=80', 'IMAGE_QUALITY=' .$request->input('image_quality'));
  133. }
  134. sleep(1);
  135. Artisan::call('config:cache');
  136. sleep(1);
  137. Artisan::call('key:generate --force');
  138. sleep(1);
  139. Artisan::call('migrate --force');
  140. sleep(1);
  141. Artisan::call('config:cache');
  142. if($request->has('features.activitypub') && $request->input('features.activitypub') == true) {
  143. Artisan::call('instance:actor');
  144. }
  145. if($request->filled(['admin_username', 'admin_password', 'admin_email'])) {
  146. $user = new User;
  147. $user->username = $request->input('admin_username', 'admin');
  148. $user->name = $request->input('admin_username', 'admin');
  149. $user->email = $request->input('admin_email');
  150. $user->password = bcrypt($request->input('admin_password'));
  151. $user->is_admin = true;
  152. $user->email_verified_at = now();
  153. $user->save();
  154. }
  155. }
  156. protected function updateConfig($key, $value)
  157. {
  158. $f = file_get_contents(base_path('.env'));
  159. if(strpos($f, $key) !== false) {
  160. $u = str_replace($key, $value, $f);
  161. } else {
  162. $u = $f;
  163. $u .= $value . PHP_EOL;
  164. }
  165. sleep(1);
  166. file_put_contents(base_path('.env'), $u);
  167. }
  168. public function precheckDatabase(Request $request)
  169. {
  170. $driver = $request->input('db_driver', 'mysql');
  171. $name = $request->input('db_name', 'pixelfed');
  172. $host = $request->input('db_host', 'localhost');
  173. $username = $request->input('db_username');
  174. $password = $request->input('db_password');
  175. $dsn = "{$driver}:dbname={$name};host={$host}";
  176. $dbh = new \PDO($dsn, $username, $password);
  177. try {
  178. $dbh->query('SELECT count(*) from INFORMATION_SCHEMA.TABLES');
  179. } catch (\PDOException $e) {
  180. return response($e, 400);
  181. }
  182. }
  183. }