Cachet/app/Http/Controllers/SetupController.php

261 lines
7.0 KiB
PHP
Raw Normal View History

2014-11-23 21:34:31 +00:00
<?php
/*
* This file is part of Cachet.
*
2015-07-06 17:37:01 +01:00
* (c) Alt Three Services Limited
*
* 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
use CachetHQ\Cachet\Models\User;
2016-05-25 10:55:33 +01:00
use CachetHQ\Cachet\Settings\Repository;
2016-01-29 23:01:41 +00:00
use Dotenv\Dotenv;
use Dotenv\Exception\InvalidPathException;
2015-01-02 12:05:50 +00:00
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Routing\Controller;
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-01 15:45:04 +00:00
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
class SetupController extends Controller
2014-12-20 21:20:17 +00:00
{
/**
* Array of cache drivers.
*
* @var string[]
*/
protected $cacheDrivers = [
'apc' => 'APC(u)',
'array' => 'Array',
'file' => 'File',
'database' => 'Database',
'memcached' => 'Memcached',
'redis' => 'Redis',
];
2016-06-05 10:22:09 +01:00
2016-05-28 10:37:45 -05:00
/**
* Array of cache drivers.
*
* @var string[]
*/
protected $mailDrivers = [
'smtp' => 'SMTP',
'mail' => 'Mail',
'sendmail' => 'Sendmail',
'mailgun' => 'Mailgun',
'mandrill' => 'Mandrill',
2016-06-03 01:26:28 -05:00
// 'ses' => 'Amazon SES', this will be available only if aws/aws-sdk-php is installed
2016-05-28 10:37:45 -05:00
'sparkpost' => 'SparkPost',
2016-06-04 14:54:32 +01:00
'log' => 'Log (Testing)',
2016-05-28 10:37:45 -05:00
];
2015-12-25 14:30:13 +08:00
/**
* Array of step1 rules.
*
* @var string[]
*/
protected $rulesStep1;
/**
* Array of step2 rules.
*
* @var string[]
*/
protected $rulesStep2;
/**
* Array of step3 rules.
*
* @var string[]
*/
protected $rulesStep3;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->rulesStep1 = [
'env.cache_driver' => 'required|in:'.implode(',', array_keys($this->cacheDrivers)),
'env.session_driver' => 'required|in:'.implode(',', array_keys($this->cacheDrivers)),
2016-06-03 01:26:28 -05:00
'env.mail_driver' => 'required|in:'.implode(',', array_keys($this->mailDrivers)),
2015-12-25 14:30:13 +08:00
];
$this->rulesStep2 = [
'settings.app_name' => 'required',
'settings.app_domain' => 'required',
'settings.app_timezone' => 'required',
'settings.app_locale' => 'required',
'settings.show_support' => 'bool',
];
$this->rulesStep3 = [
'user.username' => ['required', 'regex:/\A(?!.*[:;]-\))[ -~]+\z/'],
'user.email' => 'email|required',
'user.password' => 'required',
];
}
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()
{
2015-11-21 13:49:14 +00:00
$supportedLanguages = Request::getLanguages();
$userLanguage = Config::get('app.locale');
foreach ($supportedLanguages as $language) {
$language = str_replace('_', '-', $language);
if (isset($this->langs[$language])) {
$userLanguage = $language;
break;
}
}
return View::make('setup')
->withPageTitle(trans('setup.setup'))
->withCacheDrivers($this->cacheDrivers)
2016-05-28 10:37:45 -05:00
->withMailDrivers($this->mailDrivers)
2015-11-21 13:49:14 +00:00
->withUserLanguage($userLanguage)
->withAppUrl(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();
2015-12-25 14:30:13 +08:00
$v = Validator::make($postData, $this->rulesStep1);
2016-06-03 01:26:28 -05:00
$v->sometimes('env.mail_host', 'required', function ($input) {
return $input->mail_driver === 'smtp';
});
$v->sometimes(['env.mail_address', 'env.mail_username', 'env.mail_password'], 'required', function ($input) {
2016-06-03 01:26:28 -05:00
return $input->mail_driver !== 'log';
});
if ($v->passes()) {
return Response::json(['status' => 1]);
}
return Response::json(['errors' => $v->getMessageBag()], 400);
}
/**
* Handles validation on step two of the setup form.
*
* @return \Illuminate\Http\Response
*/
public function postStep2()
{
$postData = Binput::all();
2015-12-25 14:30:13 +08:00
$v = Validator::make($postData, $this->rulesStep1 + $this->rulesStep2);
2015-01-06 13:08:15 -06:00
if ($v->passes()) {
return Response::json(['status' => 1]);
}
return Response::json(['errors' => $v->getMessageBag()], 400);
2015-01-06 13:08:15 -06:00
}
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();
2015-12-25 14:30:13 +08:00
$v = Validator::make($postData, $this->rulesStep1 + $this->rulesStep2 + $this->rulesStep3);
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'],
2015-12-25 17:52:01 +08:00
'level' => User::LEVEL_ADMIN,
2014-12-04 21:05:08 +00:00
]);
Auth::login($user);
2016-05-25 10:55:33 +01:00
$setting = app(Repository::class);
2016-01-29 22:49:06 +00:00
$settings = array_pull($postData, 'settings');
foreach ($settings as $settingName => $settingValue) {
2016-01-29 22:49:06 +00:00
$setting->set($settingName, $settingValue);
}
$envData = array_pull($postData, 'env');
// Write the env to the .env file.
foreach ($envData as $envKey => $envValue) {
$this->writeEnv($envKey, $envValue);
}
2015-01-06 13:08:15 -06:00
if (Request::ajax()) {
return Response::json(['status' => 1]);
}
return Redirect::to('dashboard');
}
2015-01-06 13:08:15 -06:00
if (Request::ajax()) {
return Response::json(['errors' => $v->getMessageBag()], 400);
}
return Redirect::route('setup.index')->withInput()->withErrors($v->getMessageBag());
}
/**
* Writes to the .env file with given parameters.
*
* @param string $key
* @param mixed $value
*
* @return void
*/
protected function writeEnv($key, $value)
{
2016-02-12 21:40:05 +00:00
$dir = app()->environmentPath();
$file = app()->environmentFile();
$path = "{$dir}/{$file}";
2016-01-29 23:01:41 +00:00
try {
2016-02-12 21:40:05 +00:00
(new Dotenv($dir, $file))->load();
2016-07-11 13:00:00 -05:00
$envKey = strtoupper($key);
$envValue = env($envKey) ?: 'null';
file_put_contents($path, str_replace(
2016-07-11 13:00:00 -05:00
$envKey.'='.$envValue, $envKey.'='.$value, file_get_contents($path)
));
2016-01-29 23:01:41 +00:00
} catch (InvalidPathException $e) {
//
}
}
}