/** * Updates a setting value. * * @param string $name * @param string|null $value * * @return void */ public function set($name, $value) { $this->stale = true; if ($value === null) { $this->model->where('name', $name)->delete(); } else { $this->model->updateOrCreate(compact('name'), compact('value')); } }
/** * Creates or updates a setting value. * * @param string $name * @param string $value */ public function set($name, $value) { // save the change to the db $this->model->updateOrCreate(compact('name'), compact('value')); // if we've loaded the settings, persist this change if ($this->settings) { $this->settings[$name] = $value; } }
/** * Creates or updates a setting value. * * @param string $name * @param string|null $value * * @return void */ public function set($name, $value) { if ($value === null) { $this->model->where('name', $name)->delete(); if ($this->settings && isset($this->settings[$name])) { unset($this->settings[$name]); } } else { $this->model->updateOrCreate(compact('name'), compact('value')); if ($this->settings) { $this->settings[$name] = $value; } } }
/** * Returns the segment write key. * * @return string */ public function fetch() { $writeKey = null; try { // Firstly, does the setting exist? if (null === ($writeKey = Setting::get('segment_write_key'))) { // No, let's go fetch it. $writeKey = $this->repository->fetch(); Setting::set('segment_write_key', $writeKey); } else { // It does, but how old is it? $setting = SettingModel::where('name', 'segment_write_key')->first(); // It's older than an hour, let's refresh if ($setting->updated_at->lt(Carbon::now()->subHour())) { $writeKey = $this->repository->fetch(); // Update the setting. This is done manual to make sure updated_at is overwritten. $setting->value = $writeKey; $setting->updated_at = Carbon::now(); $setting->save(); } } } catch (QueryException $e) { // Just return it until we're setup. $writeKey = $this->repository->fetch(); } return $writeKey; }
/** * Run the database seeding. */ public function run() { $defaultSettings = [['name' => 'app_name', 'value' => 'Cachet Demo'], ['name' => 'app_domain', 'value' => 'https://demo.cachethq.io'], ['name' => 'show_support', 'value' => '1'], ['name' => 'app_locale', 'value' => 'en'], ['name' => 'app_timezone', 'value' => 'Europe/London'], ['name' => 'app_track', 'value' => '1'], ['name' => 'app_incident_days', 'value' => '7'], ['name' => 'app_analytics', 'value' => 'UA-58442674-3'], ['name' => 'app_analytics_gs', 'value' => 'GSN-712462-P'], ['name' => 'display_graphs', 'value' => '1']]; Setting::truncate(); foreach ($defaultSettings as $setting) { Setting::create($setting); } }
/** * Run the database seeding. * * @return void */ public function run() { Model::unguard(); $defaultSettings = [["name" => "app_name", "value" => "Test"], ["name" => "app_domain", "value" => "cachet.dev"], ["name" => "show_support", "value" => "1"]]; Setting::truncate(); foreach ($defaultSettings as $setting) { Setting::create($setting); } }
/** * Run the has setting filter. * * We're verifying that the given setting exists in our database. If it * doesn't, then we're sending the user to the setup page so that they can * complete the installation of Cachet on their server. * * @param \Illuminate\Routing\Route $route * @param \Illuminate\Http\Request $request * @param string $settingName * * @return \Illuminate\Http\Response|null */ public function filter(Route $route, Request $request, $settingName) { try { $setting = Setting::where('name', $settingName)->first(); if (!$setting || !$setting->value) { return Redirect::to('setup'); } } catch (Exception $e) { return Redirect::to('setup'); } }
/** * Run the is setup filter. * * We're verifying that Cachet is correctly setup. If it is, they we're * sending the user to the dashboard so they can use Cachet. * * @param \Illuminate\Routing\Route $route * @param \Illuminate\Http\Request $request * * @return \Illuminate\Http\Response|null */ public function filter(Route $route, Request $request) { try { $setting = Setting::where('name', 'app_name')->first(); if ($setting && $setting->value) { return Redirect::to('/dashboard'); } } catch (Exception $e) { // do nothing } }
/** * Run the is setup filter. * * We're verifying that Cachet is correctly setup. If it is, they we're * sending the user to the dashboard so they can use Cachet. * * @param \Illuminate\Routing\Route $route * @param \Closure $next * * @return mixed */ public function handle($request, Closure $next) { try { $setting = Setting::where('name', 'app_name')->first(); if ($setting && $setting->value) { return Redirect::route('dashboard'); } } catch (Exception $e) { // do nothing } return $next($request); }
/** * Returns the rendered Blade templates. * * @return \Illuminate\View\View */ public function showIndex() { $components = Component::orderBy('order')->orderBy('created_at')->get(); $allIncidents = []; $incidentDays = Setting::get('app_incident_days') ?: 7; foreach (range(0, $incidentDays) as $i) { $date = Carbon::now()->subDays($i); $incidents = Incident::whereBetween('created_at', [$date->format('Y-m-d') . ' 00:00:00', $date->format('Y-m-d') . ' 23:59:59'])->orderBy('created_at', 'desc')->get(); $allIncidents[] = ['date' => $date->format('jS F Y'), 'incidents' => $incidents]; } return View::make('index', ['components' => $components, 'allIncidents' => $allIncidents, 'pageTitle' => Setting::get('app_name'), 'aboutApp' => Markdown::render(Setting::get('app_about'))]); }
/** * Run the has setting middleware. * * We're verifying that the given setting exists in our database. If it * doesn't, then we're sending the user to the setup page so that they can * complete the installation of Cachet on their server. * * @param \Illuminate\Http\Request $request * @param \Closure $next * * @return mixed */ public function handle($request, Closure $next) { $settingName = $this->getSettingName($request); try { $setting = Setting::where('name', $settingName)->first(); if (!$setting || !$setting->value) { return Redirect::to('setup'); } } catch (Exception $e) { return Redirect::to('setup'); } return $next($request); }
/** * Generates an RSS feed of all incidents. * * @return \Illuminate\Http\Response */ public function feedAction() { $feed = RssFacade::feed('2.0', 'UTF-8'); $feed->channel(['title' => Setting::get('app_name'), 'description' => 'Status Feed', 'link' => Setting::get('app_domain')]); Incident::get()->map(function ($incident) use($feed) { if ($incident->component) { $componentName = $incident->component->name; } else { $componentName = null; } $feed->item(['title' => $incident->name, 'message' => $incident->message, 'component' => $componentName, 'status' => $incident->humanStatus, 'created_at' => $incident->created_at, 'updated_at' => $incident->updated_at]); }); return Response::make($feed, 200, ['Content-Type' => 'text/xml']); }
/** * Run the allowed domains filter. * * @param \Illuminate\Routing\Route $route * @param \Illuminate\Http\Request $request * @param \Symfony\Component\HttpFoundation\Response $response * * @return \Symfony\Component\HttpFoundation\Response */ public function filter(Route $route, Request $request, Response $response) { // Always allow our own domain. $ourDomain = Setting::get('app_domain'); $response->headers->set('Access-Control-Allow-Origin', $ourDomain); // Should we allow anyone else? if ($setting = Setting::get('allowed_domains')) { $domains = explode(',', $setting); foreach ($domains as $domain) { $response->headers->set('Access-Control-Allow-Origin', $domain); } } return $response; }
/** * Generates an Atom feed of all incidents. * * @return \Illuminate\Http\Response */ public function feedAction() { $feed = Feed::make(); $feed->title = Setting::get('app_name'); $feed->description = 'Status Feed'; $feed->link = Setting::get('app_domain'); $feed->setDateFormat('datetime'); Incident::get()->map(function ($incident) use($feed) { if ($incident->component) { $componentName = $incident->component->name; } else { $componentName = null; } $feed->add($incident->name, Setting::get('app_name'), Setting::get('app_domain'), $incident->created_at, $componentName === null ? $incident->humanStatus : $componentName . " " . $incident->humanStatus, $incident->message); }); return $feed->render('atom'); }
/** * Handles the actual app setup. * * @return \Illuminate\Http\RedirectResponse */ public function postIndex() { $postData = Binput::all(); $v = Validator::make($postData, ['settings.app_name' => 'required', 'settings.app_domain' => 'required', 'settings.show_support' => 'boolean', 'user.username' => 'alpha_dash|required', 'user.email' => 'email|required', 'user.password' => 'required']); if ($v->passes()) { // Pull the user details out. $userDetails = array_pull($postData, 'user'); // TODO: Do we want to just use Model::unguard() here? $user = User::create(['username' => $userDetails['username'], 'email' => $userDetails['email'], 'password' => $userDetails['password'], 'level' => 1]); Auth::login($user); $settings = array_get($postData, 'settings'); foreach ($settings as $settingName => $settingValue) { Setting::create(['name' => $settingName, 'value' => $settingValue]); } return Redirect::to('dashboard'); } else { // No good, let's try that again. return Redirect::back()->withInput()->with('errors', $v->messages()); } }
/** * Updates the status page settings. * * @return \Illuminate\View\View */ public function postSettings() { $redirectUrl = Session::get('redirect_to', route('dashboard.settings.setup')); if (Binput::get('remove_banner') === '1') { $setting = Setting::where('name', 'app_banner'); $setting->delete(); } if (Binput::hasFile('app_banner')) { $file = Binput::file('app_banner'); // Image Validation. // Image size in bytes. $maxSize = $file->getMaxFilesize(); if ($file->getSize() > $maxSize) { return Redirect::to($redirectUrl)->withErrors(trans('dashboard.settings.app-setup.too-big', ['size' => $maxSize])); } if (!$file->isValid() || $file->getError()) { return Redirect::to($redirectUrl)->withErrors($file->getErrorMessage()); } if (!starts_with($file->getMimeType(), 'image/')) { return Redirect::to($redirectUrl)->withErrors(trans('dashboard.settings.app-setup.images-only')); } // Store the banner. Setting::firstOrCreate(['name' => 'app_banner'])->update(['value' => base64_encode(file_get_contents($file->getRealPath()))]); // Store the banner type Setting::firstOrCreate(['name' => 'app_banner_type'])->update(['value' => $file->getMimeType()]); } try { foreach (Binput::except(['app_banner', 'remove_banner']) as $settingName => $settingValue) { if ($settingName === 'app_analytics_pi_url') { $settingValue = rtrim($settingValue, '/'); } Setting::firstOrCreate(['name' => $settingName])->update(['value' => $settingValue]); } } catch (Exception $e) { return Redirect::to($redirectUrl)->withErrors(trans('dashboard.settings.edit.failure')); } if (Binput::has('app_locale')) { Lang::setLocale(Binput::get('app_locale')); } return Redirect::to($redirectUrl)->withSuccess(trans('dashboard.settings.edit.success')); }
/** * Handles the actual app setup, including user, settings and env. * * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response */ public function postStep3() { $postData = Binput::all(); $v = Validator::make($postData, ['env.cache_driver' => 'required|in:' . implode(',', array_keys($this->cacheDrivers)), 'env.session_driver' => 'required|in:' . implode(',', array_keys($this->cacheDrivers)), 'settings.app_name' => 'required', 'settings.app_domain' => 'required', 'settings.app_timezone' => 'required', 'settings.app_locale' => 'required', 'settings.show_support' => 'boolean', 'user.username' => ['required', 'regex:/\\A(?!.*[:;]-\\))[ -~]+\\z/'], 'user.email' => 'email|required', 'user.password' => 'required']); if ($v->passes()) { // Pull the user details out. $userDetails = array_pull($postData, 'user'); $user = User::create(['username' => $userDetails['username'], 'email' => $userDetails['email'], 'password' => $userDetails['password'], 'level' => 1]); Auth::login($user); $settings = array_pull($postData, 'settings'); foreach ($settings as $settingName => $settingValue) { Setting::create(['name' => $settingName, 'value' => $settingValue]); } $envData = array_pull($postData, 'env'); // Write the env to the .env file. foreach ($envData as $envKey => $envValue) { $this->writeEnv($envKey, $envValue); } Session::flash('setup.done', true); if (Request::ajax()) { return Response::json(['status' => 1]); } return Redirect::to('dashboard'); } if (Request::ajax()) { return Response::json(['errors' => $v->getMessageBag()], 400); } return Redirect::back()->withInput()->withErrors($v->getMessageBag()); }
/** * Clear all settings. * * @return void */ public function clear() { $this->stale = true; $this->model->query()->delete(); }
/** * Seed the settings table. * * @return void */ protected function seedSettings() { $defaultSettings = [['name' => 'app_name', 'value' => 'Cachet Demo'], ['name' => 'app_domain', 'value' => 'https://demo.cachethq.io'], ['name' => 'show_support', 'value' => '1'], ['name' => 'app_locale', 'value' => 'en'], ['name' => 'app_timezone', 'value' => 'Europe/London'], ['name' => 'app_incident_days', 'value' => '7'], ['name' => 'app_analytics', 'value' => 'UA-58442674-3'], ['name' => 'app_analytics_gs', 'value' => 'GSN-712462-P'], ['name' => 'display_graphs', 'value' => '1'], ['name' => 'app_about', 'value' => 'This is the demo instance of [Cachet](https://cachethq.io?ref=demo). The open source status page system, for everyone. An [Alt Three](https://alt-three.com) product.']]; Setting::truncate(); foreach ($defaultSettings as $setting) { Setting::create($setting); } }
/** * Updates the status page settings. * * @return \Illuminate\View\View */ public function postSettings() { if (Binput::get('remove_banner') == "1") { $setting = Setting::where('name', 'app_banner'); $setting->delete(); } if (Binput::hasFile('app_banner')) { $file = Binput::file('app_banner'); // Image Validation. // Image size in bytes. $maxSize = $file->getMaxFilesize(); if ($file->getSize() > $maxSize) { return Redirect::back()->withErrorMessage("You need to upload an image that is less than {$maxSize}."); } if (!$file->isValid() || $file->getError()) { return Redirect::back()->withErrorMessage($file->getErrorMessage()); } if (strpos($file->getMimeType(), 'image/') !== 0) { return Redirect::back()->withErrorMessage('Only images may be uploaded.'); } // Store the banner. Setting::firstOrCreate(['name' => 'app_banner'])->update(['value' => base64_encode(file_get_contents($file->getRealPath()))]); // Store the banner type Setting::firstOrCreate(['name' => 'app_banner_type'])->update(['value' => $file->getMimeType()]); } try { foreach (Binput::except(['app_banner', 'remove_banner']) as $settingName => $settingValue) { Setting::firstOrCreate(['name' => $settingName])->update(['value' => $settingValue]); } } catch (Exception $e) { return Redirect::back()->withSaved(false); } return Redirect::back()->withSaved(true); }