]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/SettingController.php
Update settings.php
[bookstack] / app / Http / Controllers / SettingController.php
index dfda2b6cf211f367755e9914225115a053c45522..feb6521f3a69a68bedbe4573d1edfac098da01aa 100644 (file)
-<?php
-
-namespace BookStack\Http\Controllers;
+<?php namespace BookStack\Http\Controllers;
 
+use BookStack\Auth\User;
+use BookStack\Notifications\TestEmail;
+use BookStack\Uploads\ImageRepo;
+use BookStack\Uploads\ImageService;
 use Illuminate\Http\Request;
 
-use BookStack\Http\Requests;
-use BookStack\Http\Controllers\Controller;
-use Setting;
-
 class SettingController extends Controller
 {
+    protected $imageRepo;
+
+    /**
+     * SettingController constructor.
+     */
+    public function __construct(ImageRepo $imageRepo)
+    {
+        $this->imageRepo = $imageRepo;
+        parent::__construct();
+    }
+
     /**
      * Display a listing of the settings.
-     *
-     * @return Response
      */
     public function index()
     {
         $this->checkPermission('settings-manage');
-        $this->setPageTitle('Settings');
-        return view('settings/index');
-    }
+        $this->setPageTitle(trans('settings.settings'));
 
+        // Get application version
+        $version = trim(file_get_contents(base_path('version')));
+
+        return view('settings.index', [
+            'version' => $version,
+            'guestUser' => User::getDefault()
+        ]);
+    }
 
     /**
      * Update the specified settings in storage.
-     *
-     * @param  Request  $request
-     * @return Response
      */
     public function update(Request $request)
     {
-        $this->preventAccessForDemoUsers();
+        $this->preventAccessInDemoMode();
         $this->checkPermission('settings-manage');
+        $this->validate($request, [
+            'app_logo' => 'nullable|' . $this->getImageValidationRules(),
+        ]);
 
         // Cycles through posted settings and update them
-        foreach($request->all() as $name => $value) {
-            if(strpos($name, 'setting-') !== 0) continue;
-            $key = str_replace('setting-', '', trim($name));
-            if($key == 'app-color') {
-                Setting::put('app-color-rgba', $this->hex2rgba($value, 0.15));
+        foreach ($request->all() as $name => $value) {
+            if (strpos($name, 'setting-') !== 0) {
+                continue;
             }
-            Setting::put($key, $value);
+            $key = str_replace('setting-', '', trim($name));
+            setting()->put($key, $value);
+        }
+
+        // Update logo image if set
+        if ($request->hasFile('app_logo')) {
+            $logoFile = $request->file('app_logo');
+            $this->imageRepo->destroyByType('system');
+            $image = $this->imageRepo->saveNew($logoFile, 'system', 0, null, 86);
+            setting()->put('app-logo', $image->url);
         }
 
-        session()->flash('success', 'Settings Saved');
-        return redirect('/settings');
+        // Clear logo image if requested
+        if ($request->get('app_logo_reset', null)) {
+            $this->imageRepo->destroyByType('system');
+            setting()->remove('app-logo');
+        }
+
+        $this->showSuccessNotification(trans('settings.settings_save_success'));
+        $redirectLocation = '/settings#' . $request->get('section', '');
+        return redirect(rtrim($redirectLocation, '#'));
     }
 
     /**
-     * Adapted from http://mekshq.com/how-to-convert-hexadecimal-color-code-to-rgb-or-rgba-using-php/
-     * Converts a hex color code in to an RGBA string.
-     *
-     * @param string $color
-     * @param float|boolean $opacity
-     * @return boolean|string
+     * Show the page for application maintenance.
      */
-    protected function hex2rgba($color, $opacity = false)
+    public function showMaintenance()
     {
-        // Return false if no color provided
-        if(empty($color)) {
-            return false;
-        }
-        // Trim any whitespace
-        $color = trim($color);
+        $this->checkPermission('settings-manage');
+        $this->setPageTitle(trans('settings.maint'));
 
-        // Sanitize $color if "#" is provided
-        if($color[0] == '#' ) {
-            $color = substr($color, 1);
+        // Get application version
+        $version = trim(file_get_contents(base_path('version')));
+
+        return view('settings.maintenance', ['version' => $version]);
+    }
+
+    /**
+     * Action to clean-up images in the system.
+     */
+    public function cleanupImages(Request $request, ImageService $imageService)
+    {
+        $this->checkPermission('settings-manage');
+
+        $checkRevisions = !($request->get('ignore_revisions', 'false') === 'true');
+        $dryRun = !($request->has('confirm'));
+
+        $imagesToDelete = $imageService->deleteUnusedImages($checkRevisions, $dryRun);
+        $deleteCount = count($imagesToDelete);
+        if ($deleteCount === 0) {
+            $this->showWarningNotification(trans('settings.maint_image_cleanup_nothing_found'));
+            return redirect('/settings/maintenance')->withInput();
         }
 
-        // Check if color has 6 or 3 characters and get values
-        if(strlen($color) == 6) {
-            $hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
-        } elseif( strlen( $color ) == 3 ) {
-            $hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
+        if ($dryRun) {
+            session()->flash('cleanup-images-warning', trans('settings.maint_image_cleanup_warning', ['count' => $deleteCount]));
         } else {
-            return false;
+            $this->showSuccessNotification(trans('settings.maint_image_cleanup_success', ['count' => $deleteCount]));
         }
 
-        // Convert hexadec to rgb
-        $rgb =  array_map('hexdec', $hex);
+        return redirect('/settings/maintenance#image-cleanup')->withInput();
+    }
 
-        // Check if opacity is set(rgba or rgb)
-        if($opacity) {
-            if(abs($opacity) > 1)
-                $opacity = 1.0;
-            $output = 'rgba('.implode(",",$rgb).','.$opacity.')';
-        } else {
-            $output = 'rgb('.implode(",",$rgb).')';
+    /**
+     * Action to send a test e-mail to the current user.
+     */
+    public function sendTestEmail()
+    {
+        $this->checkPermission('settings-manage');
+
+        try {
+            user()->notify(new TestEmail());
+            $this->showSuccessNotification(trans('settings.maint_send_test_email_success', ['address' => user()->email]));
+        } catch (\Exception $exception) {
+            $errorMessage = trans('errors.maintenance_test_email_failure') . "\n" . $exception->getMessage();
+            $this->showErrorNotification($errorMessage);
         }
 
-        // Return rgb(a) color string
-        return $output;
-    }
 
+        return redirect('/settings/maintenance#image-cleanup')->withInput();
+    }
 }