blue-twilight/app/Http/Controllers/Admin/DefaultController.php

211 lines
6.5 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers\Admin;
use App\Album;
use App\Configuration;
use App\Facade\Theme;
use App\Facade\UserConfig;
use App\Group;
use App\Helpers\ConfigHelper;
use App\Helpers\DbHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\SaveSettingsRequest;
use App\Mail\TestMailConfig;
2016-09-05 16:46:11 +01:00
use App\Photo;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\View;
class DefaultController extends Controller
{
public function __construct()
{
$this->middleware('auth');
View::share('is_admin', true);
}
public function index()
{
$this->authorizeAccessToAdminPanel();
$albumCount = DbHelper::getAlbumsForCurrentUser()->count();
2016-09-05 16:46:11 +01:00
$photoCount = Photo::all()->count();
$groupCount = Group::all()->count();
$userCount = User::where('is_activated', true)->count();
return Theme::render('admin.index', [
2016-09-05 16:46:11 +01:00
'album_count' => $albumCount,
'app_version' => config('app.version'),
'group_count' => $groupCount,
'memory_limit' => ini_get('memory_limit'),
'photo_count' => $photoCount,
'php_version' => phpversion(),
'os_version' => exec('lsb_release -ds 2>/dev/null || cat /etc/*release 2>/dev/null | head -n1 || uname -om'),
'server_name' => gethostname(),
'upload_file_size' => ini_get('upload_max_filesize'),
'upload_max_limit' => ini_get('post_max_size'),
'user_count' => $userCount
]);
}
public function saveSettings(SaveSettingsRequest $request)
{
$this->authorizeAccessToAdminPanel('admin:configure');
$passwordKeys = [
'smtp_password'
];
$checkboxKeys = [
'allow_self_registration',
'enable_visitor_hits',
'hotlink_protection',
'recaptcha_enabled_registration',
'remove_copyright',
'require_email_verification',
'restrict_original_download',
'smtp_encryption',
];
$updateKeys = [
'app_name',
'date_format',
'sender_address',
'sender_name',
'smtp_server',
'smtp_port',
'smtp_username',
'smtp_password',
'theme',
'recaptcha_site_key',
'recaptcha_secret_key',
'analytics_code'
];
$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) && strlen($config->value) > 0)
{
$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->authorizeAccessToAdminPanel('admin:configure');
// 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)
{
$this->authorizeAccessToAdminPanel('admin:configure');
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' => trans('admin.settings_test_email_successful')));
}
catch (\Exception $ex)
{
return response()->json(array('is_successful' => false, 'message' => $ex->getMessage()));
}
}
}