AdminSettingsController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Artisan, Cache, DB;
  4. use Illuminate\Http\Request;
  5. use Carbon\Carbon;
  6. use App\{Comment, Like, Media, Page, Profile, Report, Status, User};
  7. use App\Http\Controllers\Controller;
  8. use App\Util\Lexer\PrettyNumber;
  9. use App\Models\ConfigCache;
  10. use App\Services\ConfigCacheService;
  11. trait AdminSettingsController
  12. {
  13. public function settings(Request $request)
  14. {
  15. $name = ConfigCacheService::get('app.name');
  16. $short_description = ConfigCacheService::get('app.short_description');
  17. $description = ConfigCacheService::get('app.description');
  18. return view('admin.settings.home', compact(
  19. 'name',
  20. 'short_description',
  21. 'description'
  22. ));
  23. }
  24. public function settingsHomeStore(Request $request)
  25. {
  26. $this->validate($request, [
  27. 'name' => 'nullable|string',
  28. 'short_description' => 'nullable',
  29. 'long_description' => 'nullable'
  30. ]);
  31. $cc = ConfigCache::whereK('app.name')->first();
  32. $val = $request->input('name');
  33. if($cc && $cc->v != $val) {
  34. ConfigCacheService::put('app.name', $val);
  35. }
  36. $cc = ConfigCache::whereK('app.short_description')->first();
  37. $val = $request->input('short_description');
  38. if($cc && $cc->v != $val) {
  39. ConfigCacheService::put('app.short_description', $val);
  40. }
  41. $cc = ConfigCache::whereK('app.description')->first();
  42. $val = $request->input('long_description');
  43. if($cc && $cc->v != $val) {
  44. ConfigCacheService::put('app.description', $val);
  45. }
  46. return redirect('/i/admin/settings');
  47. }
  48. public function settingsBackups(Request $request)
  49. {
  50. $path = storage_path('app/'.config('app.name'));
  51. $files = is_dir($path) ? new \DirectoryIterator($path) : [];
  52. return view('admin.settings.backups', compact('files'));
  53. }
  54. public function settingsConfig(Request $request)
  55. {
  56. $editor = config('pixelfed.admin.env_editor');
  57. $config = !$editor ? false : file_get_contents(base_path('.env'));
  58. $backup = !$editor ? false : (is_file(base_path('.env.backup')) ? file_get_contents(base_path('.env.backup')) : false);
  59. return view('admin.settings.config', compact('editor', 'config', 'backup'));
  60. }
  61. public function settingsConfigStore(Request $request)
  62. {
  63. if(config('pixelfed.admin.env_editor') !== true) {
  64. abort(400);
  65. }
  66. $res = $request->input('res');
  67. $old = file_get_contents(app()->environmentFilePath());
  68. if(empty($old) || $old != $res) {
  69. $oldFile = fopen(app()->environmentFilePath().'.backup', 'w');
  70. fwrite($oldFile, $old);
  71. fclose($oldFile);
  72. }
  73. $file = fopen(app()->environmentFilePath(), 'w');
  74. fwrite($file, $res);
  75. fclose($file);
  76. Artisan::call('config:cache');
  77. return ['msg' => 200];
  78. }
  79. public function settingsConfigRestore(Request $request)
  80. {
  81. if(config('pixelfed.admin.env_editor') !== true) {
  82. abort(400);
  83. }
  84. $res = file_get_contents(app()->environmentFilePath().'.backup');
  85. if(empty($res)) {
  86. abort(400, 'No backup exists.');
  87. }
  88. $file = fopen(app()->environmentFilePath(), 'w');
  89. fwrite($file, $res);
  90. fclose($file);
  91. Artisan::call('config:cache');
  92. return ['msg' => 200];
  93. }
  94. public function settingsMaintenance(Request $request)
  95. {
  96. return view('admin.settings.maintenance');
  97. }
  98. public function settingsStorage(Request $request)
  99. {
  100. $storage = [];
  101. return view('admin.settings.storage', compact('storage'));
  102. }
  103. public function settingsFeatures(Request $request)
  104. {
  105. return view('admin.settings.features');
  106. }
  107. public function settingsPages(Request $request)
  108. {
  109. $pages = Page::orderByDesc('updated_at')->paginate(10);
  110. return view('admin.pages.home', compact('pages'));
  111. }
  112. public function settingsPageEdit(Request $request)
  113. {
  114. return view('admin.pages.edit');
  115. }
  116. public function settingsSystem(Request $request)
  117. {
  118. $sys = [
  119. 'pixelfed' => config('pixelfed.version'),
  120. 'php' => phpversion(),
  121. 'laravel' => app()->version(),
  122. ];
  123. switch (config('database.default')) {
  124. case 'pgsql':
  125. $sys['database'] = [
  126. 'name' => 'Postgres',
  127. 'version' => explode(' ', DB::select(DB::raw('select version();'))[0]->version)[1]
  128. ];
  129. break;
  130. case 'mysql':
  131. $sys['database'] = [
  132. 'name' => 'MySQL',
  133. 'version' => DB::select( DB::raw("select version()") )[0]->{'version()'}
  134. ];
  135. break;
  136. default:
  137. $sys['database'] = [
  138. 'name' => 'Unknown',
  139. 'version' => '?'
  140. ];
  141. break;
  142. }
  143. return view('admin.settings.system', compact('sys'));
  144. }
  145. }