2016-10-01 14:45:48 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
use App\Album;
|
|
|
|
use App\Facade\Theme;
|
|
|
|
use App\Facade\UserConfig;
|
2016-10-06 11:30:39 +01:00
|
|
|
use App\Helpers\DbHelper;
|
2017-09-10 13:21:45 +01:00
|
|
|
use App\Label;
|
2016-10-01 14:45:48 +01:00
|
|
|
use Closure;
|
2016-10-06 15:07:30 +01:00
|
|
|
use Illuminate\Contracts\Encryption\DecryptException;
|
2016-10-01 14:45:48 +01:00
|
|
|
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)
|
|
|
|
{
|
2017-03-24 14:43:53 +00:00
|
|
|
// We can always add the version number
|
|
|
|
$this->addVersionNumberToView();
|
|
|
|
|
2016-10-01 14:45:48 +01:00
|
|
|
// 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->addThemeInfoToView();
|
|
|
|
$this->addAlbumsToView();
|
2017-09-10 13:21:45 +01:00
|
|
|
$this->addLabelsToView();
|
2016-10-01 14:45:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the default mail configuration as per user's requirements
|
|
|
|
$this->updateMailConfig();
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function addAlbumsToView()
|
|
|
|
{
|
2017-04-15 10:59:18 +01:00
|
|
|
$albums = DbHelper::getAlbumsForCurrentUser_NonPaged()->get();
|
2016-10-01 14:45:48 +01:00
|
|
|
View::share('albums', $albums);
|
2017-09-10 15:10:45 +01:00
|
|
|
|
|
|
|
$albumsToUpload = DbHelper::getAlbumsForCurrentUser_NonPaged('upload-photos')->get();
|
|
|
|
View::share('g_albums_upload', $albumsToUpload);
|
2016-10-01 14:45:48 +01:00
|
|
|
}
|
|
|
|
|
2017-09-10 13:21:45 +01:00
|
|
|
private function addLabelsToView()
|
|
|
|
{
|
|
|
|
$NUMBER_TO_SHOW_IN_NAVBAR = 5;
|
|
|
|
$labelCount = Label::count();
|
2017-09-10 14:01:20 +01:00
|
|
|
$labels = Label::all();
|
|
|
|
$labelsToAdd = [];
|
2017-09-10 13:21:45 +01:00
|
|
|
|
2017-09-10 14:01:20 +01:00
|
|
|
/** @var Label $label */
|
|
|
|
foreach ($labels as $label)
|
|
|
|
{
|
|
|
|
$label->photos_count = $label->photoCount();
|
|
|
|
$labelsToAdd[] = $label;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort my photo count, then name
|
|
|
|
usort($labelsToAdd, function(Label $a, Label $b)
|
|
|
|
{
|
|
|
|
if ($a->photos_count == $b->photos_count)
|
|
|
|
{
|
|
|
|
if ($a->name == $b->name)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if ($a->name < $b->name)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if ($a->name > $b->name)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ($a->photos_count < $b->photos_count)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if ($a->photos_count > $b->photos_count)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$labelsToAdd = array_slice(array_reverse($labelsToAdd), 0, $NUMBER_TO_SHOW_IN_NAVBAR);
|
|
|
|
|
|
|
|
View::share('g_labels', $labelsToAdd);
|
2017-09-10 13:21:45 +01:00
|
|
|
View::share('g_more_labels', $labelCount - $NUMBER_TO_SHOW_IN_NAVBAR);
|
|
|
|
}
|
|
|
|
|
2016-10-01 14:45:48 +01:00
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
2017-03-22 16:55:39 +00:00
|
|
|
private function addVersionNumberToView()
|
|
|
|
{
|
|
|
|
$version = config('app.version');
|
|
|
|
View::share('app_version', $version);
|
|
|
|
View::share('app_version_url', $version);
|
|
|
|
}
|
|
|
|
|
2016-10-01 14:45:48 +01:00
|
|
|
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))
|
|
|
|
{
|
2016-10-06 15:07:30 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$transport->setPassword(decrypt($password));
|
|
|
|
}
|
|
|
|
catch (DecryptException $ex)
|
|
|
|
{
|
|
|
|
// Unable to decrypt the password - presumably the app's key has changed
|
|
|
|
}
|
2016-10-01 14:45:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (UserConfig::get('smtp_encryption'))
|
|
|
|
{
|
|
|
|
$transport->setEncryption('tls');
|
|
|
|
}
|
|
|
|
|
|
|
|
$mailer->alwaysFrom(UserConfig::get('sender_address'), UserConfig::get('sender_name'));
|
|
|
|
}
|
|
|
|
}
|