2016-09-01 16:23:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use App\Album;
|
2016-09-05 15:28:56 +01:00
|
|
|
use App\Configuration;
|
2016-09-02 10:42:05 +01:00
|
|
|
use App\Facade\Theme;
|
2016-09-05 15:28:56 +01:00
|
|
|
use App\Facade\UserConfig;
|
|
|
|
use App\Helpers\ConfigHelper;
|
2016-09-01 16:23:39 +01:00
|
|
|
use App\Http\Controllers\Controller;
|
2016-09-05 15:28:56 +01:00
|
|
|
use App\Http\Requests\SaveSettingsRequest;
|
2016-09-06 13:26:02 +01:00
|
|
|
use App\Mail\TestMailConfig;
|
2016-09-05 16:46:11 +01:00
|
|
|
use App\Photo;
|
2016-09-05 15:28:56 +01:00
|
|
|
use Illuminate\Http\Request;
|
2016-09-06 13:26:02 +01:00
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2016-09-01 16:23:39 +01:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2016-09-06 13:26:02 +01:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2016-09-01 16:23:39 +01:00
|
|
|
|
|
|
|
class DefaultController extends Controller
|
|
|
|
{
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
|
|
|
|
|
|
|
$albumCount = Album::all()->count();
|
2016-09-05 16:46:11 +01:00
|
|
|
$photoCount = Photo::all()->count();
|
2016-09-01 16:23:39 +01:00
|
|
|
|
2016-09-02 10:42:05 +01:00
|
|
|
return Theme::render('admin.index', [
|
2016-09-05 16:46:11 +01:00
|
|
|
'album_count' => $albumCount,
|
|
|
|
'photo_count' => $photoCount
|
2016-09-01 16:23:39 +01:00
|
|
|
]);
|
|
|
|
}
|
2016-09-05 15:28:56 +01:00
|
|
|
|
|
|
|
public function saveSettings(SaveSettingsRequest $request)
|
|
|
|
{
|
2016-09-06 13:26:02 +01:00
|
|
|
$passwordKeys = [
|
|
|
|
'smtp_password'
|
|
|
|
];
|
2016-09-05 21:43:58 +01:00
|
|
|
$checkboxKeys = [
|
|
|
|
'allow_self_registration',
|
2016-09-06 12:23:14 +01:00
|
|
|
'require_email_verification',
|
2016-09-08 11:02:49 +01:00
|
|
|
'smtp_encryption',
|
|
|
|
'recaptcha_enabled_registration'
|
2016-09-05 21:43:58 +01:00
|
|
|
];
|
2016-09-05 15:28:56 +01:00
|
|
|
$updateKeys = [
|
|
|
|
'app_name',
|
2016-09-05 15:36:46 +01:00
|
|
|
'date_format',
|
2016-09-06 12:23:14 +01:00
|
|
|
'sender_address',
|
|
|
|
'sender_name',
|
2016-09-06 13:26:02 +01:00
|
|
|
'smtp_server',
|
|
|
|
'smtp_port',
|
|
|
|
'smtp_username',
|
|
|
|
'smtp_password',
|
2016-09-08 11:02:49 +01:00
|
|
|
'theme',
|
|
|
|
'recaptcha_site_key',
|
|
|
|
'recaptcha_secret_key'
|
2016-09-05 15:28:56 +01:00
|
|
|
];
|
|
|
|
|
2016-09-06 12:23:14 +01:00
|
|
|
$defaults = UserConfig::defaults();
|
|
|
|
|
2016-09-05 15:28:56 +01:00
|
|
|
foreach ($updateKeys as $key)
|
|
|
|
{
|
|
|
|
$config = UserConfig::getOrCreateModel($key);
|
2016-09-06 13:26:02 +01:00
|
|
|
|
|
|
|
// Bit of a hack when the browser returns an empty password field - meaning the user didn't change it
|
|
|
|
// - don't touch it!
|
|
|
|
if (
|
|
|
|
$key == 'smtp_password' &&
|
|
|
|
strlen($config->value) > 0 &&
|
|
|
|
strlen($request->request->get($key)) == 0 &&
|
|
|
|
strlen($request->request->get('smtp_username')) > 0
|
|
|
|
)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-05 15:28:56 +01:00
|
|
|
$config->value = $request->request->get($key);
|
2016-09-06 13:26:02 +01:00
|
|
|
if (in_array($key, $passwordKeys))
|
|
|
|
{
|
|
|
|
$config->value = encrypt($config->value);
|
|
|
|
}
|
2016-09-06 12:23:14 +01:00
|
|
|
|
|
|
|
if (isset($defaults[$key]) && $defaults[$key] == $config->value)
|
|
|
|
{
|
|
|
|
$config->delete();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$config->save();
|
|
|
|
}
|
2016-09-05 15:28:56 +01:00
|
|
|
}
|
|
|
|
|
2016-09-05 21:43:58 +01:00
|
|
|
foreach ($checkboxKeys as $key)
|
|
|
|
{
|
|
|
|
$config = UserConfig::getOrCreateModel($key);
|
|
|
|
$config->value = ($request->request->get($key) == 'on' ? 1 : 0);
|
2016-09-06 12:23:14 +01:00
|
|
|
|
|
|
|
if (isset($defaults[$key]) && $defaults[$key] == $config->value)
|
|
|
|
{
|
|
|
|
$config->delete();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$config->save();
|
|
|
|
}
|
2016-09-05 21:43:58 +01:00
|
|
|
}
|
|
|
|
|
2016-09-05 15:28:56 +01:00
|
|
|
$request->session()->flash('success', trans('admin.settings_saved_message'));
|
|
|
|
return redirect(route('admin.settings'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function settings(Request $request)
|
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
|
|
|
|
|
|
|
// Load the current configuration
|
|
|
|
$config = array_merge(UserConfig::defaults(), UserConfig::getAll());
|
|
|
|
|
|
|
|
$dateFormats = UserConfig::allowedDateFormats();
|
|
|
|
$dateFormatsLookup = [];
|
|
|
|
|
|
|
|
foreach ($dateFormats as $dateFormat)
|
|
|
|
{
|
|
|
|
$dateFormatsLookup[$dateFormat] = date($dateFormat);
|
|
|
|
}
|
|
|
|
|
2016-09-05 15:36:46 +01:00
|
|
|
$themeNamesLookup = UserConfig::allowedThemeNames();
|
|
|
|
|
2016-09-05 15:28:56 +01:00
|
|
|
return Theme::render('admin.settings', [
|
|
|
|
'config' => $config,
|
|
|
|
'date_formats' => $dateFormatsLookup,
|
2016-09-05 15:36:46 +01:00
|
|
|
'success' => $request->session()->get('success'),
|
|
|
|
'theme_names' => $themeNamesLookup
|
2016-09-05 15:28:56 +01:00
|
|
|
]);
|
|
|
|
}
|
2016-09-06 13:26:02 +01:00
|
|
|
|
|
|
|
public function testMailSettings(SaveSettingsRequest $request)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$validKeys = [
|
|
|
|
'sender_address',
|
|
|
|
'sender_name',
|
|
|
|
'smtp_server',
|
|
|
|
'smtp_port',
|
|
|
|
'smtp_username',
|
|
|
|
'smtp_password',
|
|
|
|
'smtp_encryption'
|
|
|
|
];
|
|
|
|
$config = $request->only($validKeys);
|
|
|
|
|
|
|
|
/** @var \Swift_Mailer $swiftMailer */
|
|
|
|
$swiftMailer = resolve('mailer')->getSwiftMailer();
|
|
|
|
|
|
|
|
/** @var \Swift_SmtpTransport $transport */
|
|
|
|
$transport = $swiftMailer->getTransport();
|
|
|
|
$transport->setHost($config['smtp_server']);
|
|
|
|
$transport->setPort(intval($config['smtp_port']));
|
|
|
|
$transport->setUsername($config['smtp_username']);
|
|
|
|
$transport->setPassword($config['smtp_password']);
|
|
|
|
|
|
|
|
if (isset($config['smtp_encryption']) && strtolower($config['smtp_encryption']) == 'on')
|
|
|
|
{
|
|
|
|
$transport->setEncryption('tls');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$transport->setEncryption('');
|
|
|
|
}
|
|
|
|
|
|
|
|
Mail::to(Auth::user())->send(new TestMailConfig($config['sender_name'], $config['sender_address']));
|
|
|
|
|
|
|
|
return response()->json(array('is_successful' => true, 'message' => 'sent_successfully'));
|
|
|
|
}
|
|
|
|
catch (\Exception $ex)
|
|
|
|
{
|
|
|
|
return response()->json(array('is_successful' => false, 'message' => $ex->getMessage()));
|
|
|
|
}
|
|
|
|
}
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|