2014-11-23 21:34:31 +00:00
|
|
|
<?php
|
|
|
|
|
2015-04-19 08:52:39 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Cachet.
|
|
|
|
*
|
2015-07-06 17:37:01 +01:00
|
|
|
* (c) Alt Three Services Limited
|
2015-04-19 08:52:39 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2015-01-02 00:18:19 +00:00
|
|
|
namespace CachetHQ\Cachet\Http\Controllers;
|
2015-01-01 15:45:04 +00:00
|
|
|
|
2016-12-03 20:14:38 +00:00
|
|
|
use CachetHQ\Cachet\Bus\Commands\System\Config\UpdateConfigCommand;
|
2015-01-02 00:18:19 +00:00
|
|
|
use CachetHQ\Cachet\Models\User;
|
2016-05-25 10:55:33 +01:00
|
|
|
use CachetHQ\Cachet\Settings\Repository;
|
2015-01-02 12:05:50 +00:00
|
|
|
use GrahamCampbell\Binput\Facades\Binput;
|
2015-08-05 17:18:51 -05:00
|
|
|
use Illuminate\Routing\Controller;
|
2019-02-01 20:54:14 +00:00
|
|
|
use Illuminate\Support\Arr;
|
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-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;
|
|
|
|
|
2016-10-07 13:51:59 +01:00
|
|
|
/**
|
|
|
|
* This is the setup controller.
|
|
|
|
*
|
|
|
|
* @author James Brooks <james@alt-three.com>
|
|
|
|
* @author Graham Campbell <graham@alt-three.com>
|
|
|
|
* @author Joseph Cohen <joe@alt-three.com>
|
|
|
|
*/
|
2015-08-05 17:18:51 -05:00
|
|
|
class SetupController extends Controller
|
2014-12-20 21:20:17 +00:00
|
|
|
{
|
2015-05-23 10:35:28 +01:00
|
|
|
/**
|
|
|
|
* Array of cache drivers.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $cacheDrivers = [
|
2015-07-28 22:25:59 +02:00
|
|
|
'apc' => 'APC(u)',
|
|
|
|
'array' => 'Array',
|
|
|
|
'file' => 'File',
|
|
|
|
'database' => 'Database',
|
|
|
|
'memcached' => 'Memcached',
|
|
|
|
'redis' => 'Redis',
|
2015-05-23 10:35:28 +01:00
|
|
|
];
|
2016-06-05 10:22:09 +01:00
|
|
|
|
2016-05-28 10:37:45 -05:00
|
|
|
/**
|
|
|
|
* Array of cache drivers.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $mailDrivers = [
|
2017-09-14 19:40:30 +01:00
|
|
|
'smtp' => 'SMTP',
|
|
|
|
'mail' => 'Mail',
|
|
|
|
'sendmail' => 'Sendmail',
|
|
|
|
'mailgun' => 'Mailgun',
|
|
|
|
'mandrill' => 'Mandrill',
|
|
|
|
'ses' => 'Amazon SES',
|
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-05-23 10:35:28 +01:00
|
|
|
|
2016-10-08 17:30:32 +01:00
|
|
|
/**
|
|
|
|
* Array of queue drivers.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $queueDrivers = [
|
2016-10-08 16:30:44 +00:00
|
|
|
'null' => 'None',
|
|
|
|
'sync' => 'Synchronous',
|
|
|
|
'database' => 'Database',
|
|
|
|
'beanstalkd' => 'Beanstalk',
|
|
|
|
'sqs' => 'Amazon SQS',
|
|
|
|
'redis' => 'Redis',
|
2016-10-08 17:30:32 +01: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-10-08 17:30:32 +01:00
|
|
|
'env.queue_driver' => 'required|in:'.implode(',', array_keys($this->queueDrivers)),
|
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()
|
|
|
|
{
|
2019-09-08 04:07:23 -03:00
|
|
|
$requestedLanguages = Request::getLanguages();
|
2015-11-21 13:49:14 +00:00
|
|
|
$userLanguage = Config::get('app.locale');
|
2019-09-08 04:07:23 -03:00
|
|
|
$langs = Config::get('langs');
|
2015-11-21 13:49:14 +00:00
|
|
|
|
2019-09-08 04:07:23 -03:00
|
|
|
foreach ($requestedLanguages as $language) {
|
2015-11-21 13:49:14 +00:00
|
|
|
$language = str_replace('_', '-', $language);
|
|
|
|
|
2019-09-08 04:07:23 -03:00
|
|
|
if (isset($langs[$language])) {
|
2015-11-21 13:49:14 +00:00
|
|
|
$userLanguage = $language;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 04:07:23 -03:00
|
|
|
app('translator')->setLocale($userLanguage);
|
|
|
|
|
2016-10-08 17:30:32 +01:00
|
|
|
// Since .env may already be configured, we should show that data!
|
|
|
|
$cacheConfig = [
|
|
|
|
'driver' => Config::get('cache.default'),
|
|
|
|
];
|
|
|
|
|
|
|
|
$sessionConfig = [
|
|
|
|
'driver' => Config::get('session.driver'),
|
|
|
|
];
|
|
|
|
|
|
|
|
$queueConfig = [
|
|
|
|
'driver' => Config::get('queue.default'),
|
|
|
|
];
|
|
|
|
|
|
|
|
$mailConfig = [
|
|
|
|
'driver' => Config::get('mail.driver'),
|
|
|
|
'host' => Config::get('mail.host'),
|
|
|
|
'from' => Config::get('mail.from'),
|
|
|
|
'username' => Config::get('mail.username'),
|
|
|
|
'password' => Config::get('mail.password'),
|
|
|
|
];
|
|
|
|
|
2016-10-07 12:20:47 +01:00
|
|
|
return View::make('setup.index')
|
2015-08-03 22:32:36 +01:00
|
|
|
->withPageTitle(trans('setup.setup'))
|
|
|
|
->withCacheDrivers($this->cacheDrivers)
|
2016-10-08 17:30:32 +01:00
|
|
|
->withQueueDrivers($this->queueDrivers)
|
2016-05-28 10:37:45 -05:00
|
|
|
->withMailDrivers($this->mailDrivers)
|
2015-11-21 13:49:14 +00:00
|
|
|
->withUserLanguage($userLanguage)
|
2016-10-08 17:30:32 +01:00
|
|
|
->withAppUrl(Request::root())
|
|
|
|
->withCacheConfig($cacheConfig)
|
|
|
|
->withSessionConfig($sessionConfig)
|
|
|
|
->withQueueConfig($queueConfig)
|
|
|
|
->withMailConfig($mailConfig);
|
2014-11-27 16:05:00 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 13:08:15 -06:00
|
|
|
/**
|
2015-05-23 10:35:28 +01: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);
|
2015-01-23 17:24:34 +00:00
|
|
|
|
2016-06-03 01:26:28 -05:00
|
|
|
$v->sometimes('env.mail_host', 'required', function ($input) {
|
2016-10-07 12:38:36 +01:00
|
|
|
return $input->env['mail_driver'] === 'smtp';
|
2016-06-03 01:26:28 -05:00
|
|
|
});
|
|
|
|
|
2018-10-20 18:39:38 +02:00
|
|
|
$v->sometimes(['env.mail_address', 'env.mail_password'], 'required', function ($input) {
|
2017-12-03 17:47:36 +00:00
|
|
|
return !in_array($input->env['mail_driver'], ['log', 'smtp']);
|
2016-06-03 01:26:28 -05:00
|
|
|
});
|
|
|
|
|
2018-12-29 10:26:35 +00:00
|
|
|
$v->sometimes(['env.mail_username'], 'required', function ($input) {
|
2019-02-01 21:14:38 +00:00
|
|
|
return !in_array($input->env['mail_driver'], ['sendmail', 'log']);
|
2018-12-28 19:42:42 +01:00
|
|
|
});
|
|
|
|
|
2015-05-23 10:35:28 +01:00
|
|
|
if ($v->passes()) {
|
|
|
|
return Response::json(['status' => 1]);
|
|
|
|
}
|
2015-08-03 22:32:36 +01:00
|
|
|
|
|
|
|
return Response::json(['errors' => $v->getMessageBag()], 400);
|
2015-05-23 10:35:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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]);
|
|
|
|
}
|
2015-08-03 22:32:36 +01:00
|
|
|
|
|
|
|
return Response::json(['errors' => $v->getMessageBag()], 400);
|
2015-01-06 13:08:15 -06:00
|
|
|
}
|
|
|
|
|
2014-12-01 08:38:26 +00:00
|
|
|
/**
|
2015-05-23 10:35:28 +01: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
|
|
|
*/
|
2015-05-23 10:35:28 +01:00
|
|
|
public function postStep3()
|
2014-12-20 21:20:17 +00:00
|
|
|
{
|
2015-01-02 17:12:30 +00:00
|
|
|
$postData = Binput::all();
|
2014-12-04 22:33:15 +00:00
|
|
|
|
2015-12-25 14:30:13 +08:00
|
|
|
$v = Validator::make($postData, $this->rulesStep1 + $this->rulesStep2 + $this->rulesStep3);
|
2014-11-27 16:05:00 +00:00
|
|
|
|
|
|
|
if ($v->passes()) {
|
|
|
|
// Pull the user details out.
|
2019-02-01 20:54:14 +00:00
|
|
|
$userDetails = Arr::pull($postData, 'user');
|
2014-11-27 16:05:00 +00:00
|
|
|
|
2014-12-04 22:33:15 +00:00
|
|
|
$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
|
|
|
]);
|
2014-11-27 16:05:00 +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
|
|
|
|
2019-02-01 20:54:14 +00:00
|
|
|
$settings = Arr::pull($postData, 'settings');
|
2014-12-20 17:26:48 +00:00
|
|
|
|
|
|
|
foreach ($settings as $settingName => $settingValue) {
|
2016-01-29 22:49:06 +00:00
|
|
|
$setting->set($settingName, $settingValue);
|
2014-11-27 16:05:00 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 20:54:14 +00:00
|
|
|
$envData = Arr::pull($postData, 'env');
|
2015-05-23 10:35:28 +01:00
|
|
|
|
|
|
|
// Write the env to the .env file.
|
2018-06-25 22:25:54 +01:00
|
|
|
execute(new UpdateConfigCommand($envData));
|
2015-05-23 10:35:28 +01:00
|
|
|
|
2015-01-06 13:08:15 -06:00
|
|
|
if (Request::ajax()) {
|
|
|
|
return Response::json(['status' => 1]);
|
|
|
|
}
|
|
|
|
|
2016-10-13 09:51:44 +02:00
|
|
|
return cachet_redirect('dashboard');
|
2015-08-03 22:32:36 +01:00
|
|
|
}
|
2015-01-06 13:08:15 -06:00
|
|
|
|
2015-08-03 22:32:36 +01:00
|
|
|
if (Request::ajax()) {
|
|
|
|
return Response::json(['errors' => $v->getMessageBag()], 400);
|
2014-11-27 16:05:00 +00:00
|
|
|
}
|
2015-08-03 22:32:36 +01:00
|
|
|
|
2016-10-13 09:51:44 +02:00
|
|
|
return cachet_redirect('setup')->withInput()->withErrors($v->getMessageBag());
|
2014-11-27 16:05:00 +00:00
|
|
|
}
|
2014-11-27 22:08:28 +00:00
|
|
|
}
|