blue-twilight/app/Http/Controllers/Admin/DefaultController.php
Andy Heathershaw 821bfceb09 Recaptcha support
Added configuration options to allow reCAPTCHA to be integrated into the registration process. reCAPTCHA response is validated on sign-up if enabled.
2016-09-08 11:02:49 +01:00

181 lines
5.3 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Album;
use App\Configuration;
use App\Facade\Theme;
use App\Facade\UserConfig;
use App\Helpers\ConfigHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\SaveSettingsRequest;
use App\Mail\TestMailConfig;
use App\Photo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
class DefaultController extends Controller
{
public function index()
{
$this->authorize('admin-access');
$albumCount = Album::all()->count();
$photoCount = Photo::all()->count();
return Theme::render('admin.index', [
'album_count' => $albumCount,
'photo_count' => $photoCount
]);
}
public function saveSettings(SaveSettingsRequest $request)
{
$passwordKeys = [
'smtp_password'
];
$checkboxKeys = [
'allow_self_registration',
'require_email_verification',
'smtp_encryption',
'recaptcha_enabled_registration'
];
$updateKeys = [
'app_name',
'date_format',
'sender_address',
'sender_name',
'smtp_server',
'smtp_port',
'smtp_username',
'smtp_password',
'theme',
'recaptcha_site_key',
'recaptcha_secret_key'
];
$defaults = UserConfig::defaults();
foreach ($updateKeys as $key)
{
$config = UserConfig::getOrCreateModel($key);
// 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;
}
$config->value = $request->request->get($key);
if (in_array($key, $passwordKeys))
{
$config->value = encrypt($config->value);
}
if (isset($defaults[$key]) && $defaults[$key] == $config->value)
{
$config->delete();
}
else
{
$config->save();
}
}
foreach ($checkboxKeys as $key)
{
$config = UserConfig::getOrCreateModel($key);
$config->value = ($request->request->get($key) == 'on' ? 1 : 0);
if (isset($defaults[$key]) && $defaults[$key] == $config->value)
{
$config->delete();
}
else
{
$config->save();
}
}
$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);
}
$themeNamesLookup = UserConfig::allowedThemeNames();
return Theme::render('admin.settings', [
'config' => $config,
'date_formats' => $dateFormatsLookup,
'success' => $request->session()->get('success'),
'theme_names' => $themeNamesLookup
]);
}
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()));
}
}
}