AdminSettingsController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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\Models\InstanceActor;
  8. use App\Http\Controllers\Controller;
  9. use App\Util\Lexer\PrettyNumber;
  10. use App\Models\ConfigCache;
  11. use App\Services\ConfigCacheService;
  12. use App\Util\Site\Config;
  13. trait AdminSettingsController
  14. {
  15. public function settings(Request $request)
  16. {
  17. $cloud_storage = ConfigCacheService::get('pixelfed.cloud_storage');
  18. $cloud_disk = config('filesystems.cloud');
  19. $cloud_ready = !empty(config('filesystems.disks.' . $cloud_disk . '.key')) && !empty(config('filesystems.disks.' . $cloud_disk . '.secret'));
  20. $types = explode(',', ConfigCacheService::get('pixelfed.media_types'));
  21. $rules = ConfigCacheService::get('app.rules') ? json_decode(ConfigCacheService::get('app.rules'), true) : null;
  22. $jpeg = in_array('image/jpg', $types) ? true : in_array('image/jpeg', $types);
  23. $png = in_array('image/png', $types);
  24. $gif = in_array('image/gif', $types);
  25. $mp4 = in_array('video/mp4', $types);
  26. // $system = [
  27. // 'permissions' => is_writable(base_path('storage')) && is_writable(base_path('bootstrap')),
  28. // 'max_upload_size' => ini_get('post_max_size'),
  29. // 'image_driver' => config('image.driver'),
  30. // 'image_driver_loaded' => extension_loaded(config('image.driver'))
  31. // ];
  32. return view('admin.settings.home', compact(
  33. 'jpeg',
  34. 'png',
  35. 'gif',
  36. 'mp4',
  37. 'rules',
  38. 'cloud_storage',
  39. 'cloud_disk',
  40. 'cloud_ready',
  41. // 'system'
  42. ));
  43. }
  44. public function settingsHomeStore(Request $request)
  45. {
  46. $this->validate($request, [
  47. 'name' => 'nullable|string',
  48. 'short_description' => 'nullable',
  49. 'long_description' => 'nullable',
  50. 'max_photo_size' => 'nullable|integer|min:1',
  51. 'max_album_length' => 'nullable|integer|min:1|max:100',
  52. 'image_quality' => 'nullable|integer|min:1|max:100',
  53. 'type_jpeg' => 'nullable',
  54. 'type_png' => 'nullable',
  55. 'type_gif' => 'nullable',
  56. 'type_mp4' => 'nullable',
  57. ]);
  58. if($request->filled('rule_delete')) {
  59. $index = (int) $request->input('rule_delete');
  60. $rules = ConfigCacheService::get('app.rules');
  61. $json = json_decode($rules, true);
  62. if(!$rules || empty($json)) {
  63. return;
  64. }
  65. unset($json[$index]);
  66. $json = json_encode(array_values($json));
  67. ConfigCacheService::put('app.rules', $json);
  68. return 200;
  69. }
  70. $media_types = explode(',', config_cache('pixelfed.media_types'));
  71. $media_types_original = $media_types;
  72. $mimes = [
  73. 'type_jpeg' => 'image/jpeg',
  74. 'type_png' => 'image/png',
  75. 'type_gif' => 'image/gif',
  76. 'type_mp4' => 'video/mp4',
  77. ];
  78. foreach ($mimes as $key => $value) {
  79. if($request->input($key) == 'on') {
  80. if(!in_array($value, $media_types)) {
  81. array_push($media_types, $value);
  82. }
  83. } else {
  84. $media_types = array_diff($media_types, [$value]);
  85. }
  86. }
  87. if($media_types !== $media_types_original) {
  88. ConfigCacheService::put('pixelfed.media_types', implode(',', array_unique($media_types)));
  89. }
  90. $keys = [
  91. 'name' => 'app.name',
  92. 'short_description' => 'app.short_description',
  93. 'long_description' => 'app.description',
  94. 'max_photo_size' => 'pixelfed.max_photo_size',
  95. 'max_album_length' => 'pixelfed.max_album_length',
  96. 'image_quality' => 'pixelfed.image_quality',
  97. 'account_limit' => 'pixelfed.max_account_size',
  98. 'custom_css' => 'uikit.custom.css',
  99. 'custom_js' => 'uikit.custom.js',
  100. 'about_title' => 'about.title'
  101. ];
  102. foreach ($keys as $key => $value) {
  103. $cc = ConfigCache::whereK($value)->first();
  104. $val = $request->input($key);
  105. if($cc && $cc->v != $val) {
  106. ConfigCacheService::put($value, $val);
  107. }
  108. }
  109. $bools = [
  110. 'activitypub' => 'federation.activitypub.enabled',
  111. 'open_registration' => 'pixelfed.open_registration',
  112. 'mobile_apis' => 'pixelfed.oauth_enabled',
  113. 'stories' => 'instance.stories.enabled',
  114. 'ig_import' => 'pixelfed.import.instagram.enabled',
  115. 'spam_detection' => 'pixelfed.bouncer.enabled',
  116. 'require_email_verification' => 'pixelfed.enforce_email_verification',
  117. 'enforce_account_limit' => 'pixelfed.enforce_account_limit',
  118. 'show_custom_css' => 'uikit.show_custom.css',
  119. 'show_custom_js' => 'uikit.show_custom.js',
  120. 'cloud_storage' => 'pixelfed.cloud_storage'
  121. ];
  122. foreach ($bools as $key => $value) {
  123. $active = $request->input($key) == 'on';
  124. if($key == 'activitypub' && $active && !InstanceActor::exists()) {
  125. Artisan::call('instance:actor');
  126. }
  127. if( $key == 'mobile_apis' &&
  128. $active &&
  129. !file_exists(storage_path('oauth-public.key')) &&
  130. !file_exists(storage_path('oauth-private.key'))
  131. ) {
  132. Artisan::call('passport:keys');
  133. Artisan::call('route:cache');
  134. }
  135. if(config_cache($value) !== $active) {
  136. ConfigCacheService::put($value, (bool) $active);
  137. }
  138. }
  139. if($request->filled('new_rule')) {
  140. $rules = ConfigCacheService::get('app.rules');
  141. $val = $request->input('new_rule');
  142. if(!$rules) {
  143. ConfigCacheService::put('app.rules', json_encode([$val]));
  144. } else {
  145. $json = json_decode($rules, true);
  146. $json[] = $val;
  147. ConfigCacheService::put('app.rules', json_encode(array_values($json)));
  148. }
  149. }
  150. Cache::forget(Config::CACHE_KEY);
  151. return redirect('/i/admin/settings')->with('status', 'Successfully updated settings!');
  152. }
  153. public function settingsBackups(Request $request)
  154. {
  155. $path = storage_path('app/'.config('app.name'));
  156. $files = is_dir($path) ? new \DirectoryIterator($path) : [];
  157. return view('admin.settings.backups', compact('files'));
  158. }
  159. public function settingsConfig(Request $request)
  160. {
  161. $editor = config('pixelfed.admin.env_editor');
  162. $config = !$editor ? false : file_get_contents(base_path('.env'));
  163. $backup = !$editor ? false : (is_file(base_path('.env.backup')) ? file_get_contents(base_path('.env.backup')) : false);
  164. return view('admin.settings.config', compact('editor', 'config', 'backup'));
  165. }
  166. public function settingsConfigStore(Request $request)
  167. {
  168. if(config('pixelfed.admin.env_editor') !== true) {
  169. abort(400);
  170. }
  171. return ['msg' => 200];
  172. }
  173. public function settingsConfigRestore(Request $request)
  174. {
  175. if(config('pixelfed.admin.env_editor') !== true) {
  176. abort(400);
  177. }
  178. return ['msg' => 200];
  179. }
  180. public function settingsMaintenance(Request $request)
  181. {
  182. return view('admin.settings.maintenance');
  183. }
  184. public function settingsStorage(Request $request)
  185. {
  186. $storage = [];
  187. return view('admin.settings.storage', compact('storage'));
  188. }
  189. public function settingsFeatures(Request $request)
  190. {
  191. return view('admin.settings.features');
  192. }
  193. public function settingsPages(Request $request)
  194. {
  195. $pages = Page::orderByDesc('updated_at')->paginate(10);
  196. return view('admin.pages.home', compact('pages'));
  197. }
  198. public function settingsPageEdit(Request $request)
  199. {
  200. return view('admin.pages.edit');
  201. }
  202. public function settingsSystem(Request $request)
  203. {
  204. $sys = [
  205. 'pixelfed' => config('pixelfed.version'),
  206. 'php' => phpversion(),
  207. 'laravel' => app()->version(),
  208. ];
  209. switch (config('database.default')) {
  210. case 'pgsql':
  211. $sys['database'] = [
  212. 'name' => 'Postgres',
  213. 'version' => explode(' ', DB::select(DB::raw('select version();'))[0]->version)[1]
  214. ];
  215. break;
  216. case 'mysql':
  217. $sys['database'] = [
  218. 'name' => 'MySQL',
  219. 'version' => DB::select( DB::raw("select version()") )[0]->{'version()'}
  220. ];
  221. break;
  222. default:
  223. $sys['database'] = [
  224. 'name' => 'Unknown',
  225. 'version' => '?'
  226. ];
  227. break;
  228. }
  229. return view('admin.settings.system', compact('sys'));
  230. }
  231. }