AdminSettingsController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. $types = explode(',', ConfigCacheService::get('pixelfed.media_types'));
  19. $jpeg = in_array('image/jpg', $types) ? true : in_array('image/jpeg', $types);
  20. $png = in_array('image/png', $types);
  21. $gif = in_array('image/gif', $types);
  22. $mp4 = in_array('video/mp4', $types);
  23. return view('admin.settings.home', compact(
  24. 'name',
  25. 'short_description',
  26. 'description',
  27. 'jpeg',
  28. 'png',
  29. 'gif',
  30. 'mp4'
  31. ));
  32. }
  33. public function settingsHomeStore(Request $request)
  34. {
  35. $this->validate($request, [
  36. 'name' => 'nullable|string',
  37. 'short_description' => 'nullable',
  38. 'long_description' => 'nullable',
  39. 'max_photo_size' => 'nullable|integer|min:1',
  40. 'max_album_length' => 'nullable|integer|min:1|max:100',
  41. 'image_quality' => 'nullable|integer|min:1|max:100',
  42. 'type_jpeg' => 'nullable',
  43. 'type_png' => 'nullable',
  44. 'type_gif' => 'nullable',
  45. 'type_mp4' => 'nullable',
  46. ]);
  47. $media_types = explode(',', config_cache('pixelfed.media_types'));
  48. $media_types_original = $media_types;
  49. $mimes = [
  50. 'type_jpeg' => 'image/jpeg',
  51. 'type_png' => 'image/png',
  52. 'type_gif' => 'image/gif',
  53. 'type_mp4' => 'video/mp4',
  54. ];
  55. foreach ($mimes as $key => $value) {
  56. if($request->input($key) == 'on') {
  57. if(!in_array($value, $media_types)) {
  58. array_push($media_types, $value);
  59. }
  60. } else {
  61. $media_types = array_diff($media_types, [$value]);
  62. }
  63. }
  64. if($media_types !== $media_types_original) {
  65. ConfigCacheService::put('pixelfed.media_types', implode(',', array_unique($media_types)));
  66. }
  67. $keys = [
  68. 'name' => 'app.name',
  69. 'short_description' => 'app.short_description',
  70. 'long_description' => 'app.description',
  71. 'max_photo_size' => 'pixelfed.max_photo_size',
  72. 'max_album_length' => 'pixelfed.max_album_length',
  73. 'image_quality' => 'pixelfed.image_quality',
  74. 'account_limit' => 'pixelfed.max_account_size',
  75. 'custom_css' => 'uikit.custom.css',
  76. 'custom_js' => 'uikit.custom.js'
  77. ];
  78. foreach ($keys as $key => $value) {
  79. $cc = ConfigCache::whereK($value)->first();
  80. $val = $request->input($key);
  81. if($cc && $cc->v != $val) {
  82. ConfigCacheService::put($value, $val);
  83. }
  84. }
  85. $bools = [
  86. 'activitypub' => 'federation.activitypub.enabled',
  87. 'open_registration' => 'pixelfed.open_registration',
  88. 'mobile_apis' => 'pixelfed.oauth_enabled',
  89. 'stories' => 'instance.stories.enabled',
  90. 'ig_import' => 'pixelfed.import.instagram.enabled',
  91. 'spam_detection' => 'pixelfed.bouncer.enabled',
  92. 'require_email_verification' => 'pixelfed.enforce_email_verification',
  93. 'enforce_account_limit' => 'pixelfed.enforce_account_limit',
  94. 'show_custom_css' => 'uikit.show_custom.css',
  95. 'show_custom_js' => 'uikit.show_custom.js',
  96. ];
  97. foreach ($bools as $key => $value) {
  98. $active = $request->input($key) == 'on';
  99. if(config_cache($value) !== $active) {
  100. ConfigCacheService::put($value, (bool) $active);
  101. }
  102. }
  103. Cache::forget('api:site:configuration:_v0.2');
  104. return redirect('/i/admin/settings');
  105. }
  106. public function settingsBackups(Request $request)
  107. {
  108. $path = storage_path('app/'.config('app.name'));
  109. $files = is_dir($path) ? new \DirectoryIterator($path) : [];
  110. return view('admin.settings.backups', compact('files'));
  111. }
  112. public function settingsConfig(Request $request)
  113. {
  114. $editor = config('pixelfed.admin.env_editor');
  115. $config = !$editor ? false : file_get_contents(base_path('.env'));
  116. $backup = !$editor ? false : (is_file(base_path('.env.backup')) ? file_get_contents(base_path('.env.backup')) : false);
  117. return view('admin.settings.config', compact('editor', 'config', 'backup'));
  118. }
  119. public function settingsConfigStore(Request $request)
  120. {
  121. if(config('pixelfed.admin.env_editor') !== true) {
  122. abort(400);
  123. }
  124. $res = $request->input('res');
  125. $old = file_get_contents(app()->environmentFilePath());
  126. if(empty($old) || $old != $res) {
  127. $oldFile = fopen(app()->environmentFilePath().'.backup', 'w');
  128. fwrite($oldFile, $old);
  129. fclose($oldFile);
  130. }
  131. $file = fopen(app()->environmentFilePath(), 'w');
  132. fwrite($file, $res);
  133. fclose($file);
  134. Artisan::call('config:cache');
  135. return ['msg' => 200];
  136. }
  137. public function settingsConfigRestore(Request $request)
  138. {
  139. if(config('pixelfed.admin.env_editor') !== true) {
  140. abort(400);
  141. }
  142. $res = file_get_contents(app()->environmentFilePath().'.backup');
  143. if(empty($res)) {
  144. abort(400, 'No backup exists.');
  145. }
  146. $file = fopen(app()->environmentFilePath(), 'w');
  147. fwrite($file, $res);
  148. fclose($file);
  149. Artisan::call('config:cache');
  150. return ['msg' => 200];
  151. }
  152. public function settingsMaintenance(Request $request)
  153. {
  154. return view('admin.settings.maintenance');
  155. }
  156. public function settingsStorage(Request $request)
  157. {
  158. $storage = [];
  159. return view('admin.settings.storage', compact('storage'));
  160. }
  161. public function settingsFeatures(Request $request)
  162. {
  163. return view('admin.settings.features');
  164. }
  165. public function settingsPages(Request $request)
  166. {
  167. $pages = Page::orderByDesc('updated_at')->paginate(10);
  168. return view('admin.pages.home', compact('pages'));
  169. }
  170. public function settingsPageEdit(Request $request)
  171. {
  172. return view('admin.pages.edit');
  173. }
  174. public function settingsSystem(Request $request)
  175. {
  176. $sys = [
  177. 'pixelfed' => config('pixelfed.version'),
  178. 'php' => phpversion(),
  179. 'laravel' => app()->version(),
  180. ];
  181. switch (config('database.default')) {
  182. case 'pgsql':
  183. $sys['database'] = [
  184. 'name' => 'Postgres',
  185. 'version' => explode(' ', DB::select(DB::raw('select version();'))[0]->version)[1]
  186. ];
  187. break;
  188. case 'mysql':
  189. $sys['database'] = [
  190. 'name' => 'MySQL',
  191. 'version' => DB::select( DB::raw("select version()") )[0]->{'version()'}
  192. ];
  193. break;
  194. default:
  195. $sys['database'] = [
  196. 'name' => 'Unknown',
  197. 'version' => '?'
  198. ];
  199. break;
  200. }
  201. return view('admin.settings.system', compact('sys'));
  202. }
  203. }