Cachet/app/Http/Controllers/SetupController.php

255 lines
7.3 KiB
PHP
Raw Normal View History

2014-11-23 21:34:31 +00:00
<?php
/*
* This file is part of Cachet.
*
2015-05-25 17:59:08 +01:00
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Http\Controllers;
2015-01-01 15:45:04 +00:00
2015-01-16 14:01:20 -06:00
use CachetHQ\Cachet\Models\Setting;
use CachetHQ\Cachet\Models\User;
2015-01-02 12:05:50 +00:00
use GrahamCampbell\Binput\Facades\Binput;
2015-01-01 15:45:04 +00:00
use Illuminate\Support\Facades\Auth;
2015-05-25 20:28:47 +01:00
use Illuminate\Support\Facades\Config;
2015-01-01 15:45:04 +00:00
use Illuminate\Support\Facades\Redirect;
2015-01-06 13:08:15 -06:00
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Response;
2015-01-18 15:53:43 -06:00
use Illuminate\Support\Facades\Session;
2015-01-01 15:45:04 +00:00
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
2015-03-20 18:30:45 -06:00
class SetupController extends AbstractController
2014-12-20 21:20:17 +00:00
{
/**
* Array of cache drivers.
*
* @var string[]
*/
protected $cacheDrivers = [
'apc' => 'APC(u)',
// 'file' => 'File',
// 'database' => 'Database',
];
2014-12-29 23:07:46 +00:00
/**
* Create a new setup controller instance.
*/
2014-12-20 21:20:17 +00:00
public function __construct()
{
2014-12-01 16:46:56 +00:00
$this->beforeFilter('csrf', ['only' => ['postCachet']]);
}
2014-12-01 08:38:26 +00:00
/**
* Returns the setup page.
2014-12-29 23:07:46 +00:00
*
2014-12-01 08:38:26 +00:00
* @return \Illuminate\View\View
*/
2014-12-20 21:20:17 +00:00
public function getIndex()
{
segment_page('Setup');
// If we've copied the .env.example file, then we should try and reset it ready for Segment to kick in.
if (getenv('APP_KEY') === 'SomeRandomString') {
2015-05-25 20:28:47 +01:00
$this->keyGenerate();
}
return View::make('setup')->with([
'pageTitle' => trans('setup.setup'),
'cacheDrivers' => $this->cacheDrivers,
'appUrl' => Request::root(),
]);
}
2015-01-06 13:08:15 -06:00
/**
* Handles validation on step one of the setup form.
2015-01-06 13:08:15 -06:00
*
* @return \Illuminate\Http\Response
*/
public function postStep1()
{
$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)),
]);
if ($v->passes()) {
segment_track('Setup', [
'event' => 'Step 1',
'success' => true,
]);
return Response::json(['status' => 1]);
} else {
// No good, let's try that again.
segment_track('Setup', [
'event' => 'Step 1',
'success' => false,
]);
return Response::json(['errors' => $v->messages()], 400);
}
}
/**
* Handles validation on step two of the setup form.
*
* @return \Illuminate\Http\Response
*/
public function postStep2()
{
$postData = Binput::all();
2015-01-06 13:08:15 -06:00
$v = Validator::make($postData, [
'env.cache_driver' => 'required|in:'.implode(',', array_keys($this->cacheDrivers)),
'env.session_driver' => 'required|in:'.implode(',', array_keys($this->cacheDrivers)),
2015-01-06 13:08:15 -06:00
'settings.app_name' => 'required',
'settings.app_domain' => 'required',
'settings.app_timezone' => 'required',
'settings.app_locale' => 'required',
2015-01-06 13:08:15 -06:00
'settings.show_support' => 'boolean',
]);
if ($v->passes()) {
2015-03-20 18:30:45 -06:00
segment_track('Setup', [
'event' => 'Step 2',
2015-03-20 18:30:45 -06:00
'success' => true,
]);
2015-01-06 13:08:15 -06:00
return Response::json(['status' => 1]);
} else {
// No good, let's try that again.
2015-03-20 18:30:45 -06:00
segment_track('Setup', [
'event' => 'Step 2',
2015-03-20 18:30:45 -06:00
'success' => false,
]);
2015-01-06 13:08:15 -06:00
return Response::json(['errors' => $v->messages()], 400);
}
}
2014-12-01 08:38:26 +00:00
/**
* Handles the actual app setup, including user, settings and env.
2014-12-29 23:07:46 +00:00
*
2015-01-06 13:08:15 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
2014-12-01 08:38:26 +00:00
*/
public function postStep3()
2014-12-20 21:20:17 +00:00
{
$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',
2014-12-01 16:46:56 +00:00
'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',
2015-02-18 03:24:43 -06:00
'user.password' => 'required',
]);
if ($v->passes()) {
// Pull the user details out.
2014-12-01 16:46:56 +00:00
$userDetails = array_pull($postData, 'user');
$user = User::create([
2015-01-09 14:21:53 -06:00
'username' => $userDetails['username'],
'email' => $userDetails['email'],
'password' => $userDetails['password'],
'level' => 1,
2014-12-04 21:05:08 +00:00
]);
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);
}
2015-01-18 15:53:43 -06:00
Session::flash('setup.done', true);
2015-03-20 18:30:45 -06:00
segment_track('Setup', [
'event' => 'Step 3',
'success' => true,
'cache_driver' => $envData['cache_driver'],
'session_driver' => $envData['session_driver'],
2015-03-20 18:30:45 -06:00
]);
2015-01-06 13:08:15 -06:00
if (Request::ajax()) {
return Response::json(['status' => 1]);
}
return Redirect::to('dashboard');
} else {
2015-03-20 18:30:45 -06:00
segment_track('Setup', [
'event' => 'Step 3',
2015-03-20 18:30:45 -06:00
'success' => false,
]);
// No good, let's try that again.
2015-01-06 13:08:15 -06:00
if (Request::ajax()) {
return Response::json(['errors' => $v->messages()], 400);
}
return Redirect::back()->withInput()->with('errors', $v->messages());
}
}
/**
* Writes to the .env file with given parameters.
*
* @param string $key
* @param mixed $value
*
* @return void
*/
protected function writeEnv($key, $value)
{
static $path = null;
if ($path === null || ($path !== null && file_exists($path))) {
$path = base_path('.env');
file_put_contents($path, str_replace(
getenv(strtoupper($key)), $value, file_get_contents($path)
));
}
}
2015-05-25 20:28:47 +01:00
/**
* Generate the app.key value.
*
* @return void
*/
protected function keyGenerate()
{
$key = str_random(42);
$path = base_path('.env');
file_put_contents($path, str_replace(
Config::get('app.key'), $key, file_get_contents($path)
));
Config::set('app.key', $key);
}
}