blue-twilight/app/Http/Middleware/GlobalConfiguration.php

123 lines
3.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Middleware;
use App\Album;
use App\Facade\Theme;
use App\Facade\UserConfig;
use App\Helpers\DbHelper;
use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Mail\Mailer;
use Illuminate\Support\Facades\View;
class GlobalConfiguration
{
/**
* The application instance.
*
* @var \Illuminate\Foundation\Application
*/
protected $app;
/**
* Create a new middleware instance.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
public function __construct(Application $app)
{
$this->app = $app;
}
public function handle(Request $request, Closure $next)
{
// If running the installer, chances are our database isn't running yet
if ($request->is('install/*'))
{
return $next($request);
}
// When running migrations, CLI tasks or the installer, don't need to add things to the view
if (php_sapi_name() != 'cli')
{
$this->addLicenseInfoToView();
$this->addThemeInfoToView();
$this->addAlbumsToView();
}
// Set the default mail configuration as per user's requirements
$this->updateMailConfig();
return $next($request);
}
private function addAlbumsToView()
{
$albums = DbHelper::getAlbumsForCurrentUser();
View::share('albums', $albums);
}
private function addLicenseInfoToView()
{
$licenseName = null;
$licenseNo = null;
if (function_exists('sg_get_const'))
{
$licenseName = sg_get_const('license_name');
$licenseNo = sg_get_const('license_number');
}
View::share('license_name', strlen($licenseName) == 0 ? '**UNLICENSED**' : $licenseName);
View::share('license_no', strlen($licenseNo) == 0 ? '0' : $licenseNo);
}
private function addThemeInfoToView()
{
$themeInfo = Theme::info();
// Add each theme info element to the view - prefixing with theme_
// e.g. $themeInfo['name'] becomes $theme_name in the view
foreach ($themeInfo as $key => $value)
{
View::share('theme_' . $key, $value);
}
// Also add a theme_url key
View::share('theme_url', sprintf('themes/%s', Theme::current()));
}
private function updateMailConfig()
{
/** @var Mailer $mailer */
$mailer = $this->app->mailer;
$swiftMailer = $mailer->getSwiftMailer();
/** @var \Swift_SmtpTransport $transport */
$transport = $swiftMailer->getTransport();
$transport->setHost(UserConfig::get('smtp_server'));
$transport->setPort(intval(UserConfig::get('smtp_port')));
$username = UserConfig::get('smtp_username');
if (!is_null($username))
{
$transport->setUsername($username);
}
$password = UserConfig::get('smtp_password');
if (!is_null($password))
{
$transport->setPassword(decrypt($password));
}
if (UserConfig::get('smtp_encryption'))
{
$transport->setEncryption('tls');
}
$mailer->alwaysFrom(UserConfig::get('sender_address'), UserConfig::get('sender_name'));
}
}