AdminSettingsController.php 7.4 KB

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