2016-09-01 16:23:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
2016-09-03 22:13:05 +01:00
|
|
|
use App\Album;
|
2016-09-21 12:10:37 +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-03 17:09:49 +01:00
|
|
|
use App\Helpers\ImageHelper;
|
2016-09-21 12:10:37 +01:00
|
|
|
use App\Helpers\MiscHelper;
|
2016-09-02 10:42:05 +01:00
|
|
|
use App\Helpers\ThemeHelper;
|
2016-09-02 21:27:50 +01:00
|
|
|
use Illuminate\Database\QueryException;
|
2016-09-06 14:19:16 +01:00
|
|
|
use Illuminate\Mail\Mailer;
|
2016-09-21 12:10:37 +01:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2016-09-02 10:42:05 +01:00
|
|
|
use Illuminate\Support\Facades\View;
|
2016-09-01 16:23:39 +01:00
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Bootstrap any application services.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function boot()
|
|
|
|
{
|
2016-09-21 12:10:37 +01:00
|
|
|
if ($this->checkIfInstalled())
|
2016-09-03 17:09:49 +01:00
|
|
|
{
|
2016-09-21 12:10:37 +01:00
|
|
|
$this->app->singleton('image', function ($app)
|
|
|
|
{
|
|
|
|
return new ImageHelper();
|
|
|
|
});
|
|
|
|
$this->app->singleton('theme', function ($app)
|
|
|
|
{
|
|
|
|
return new ThemeHelper();
|
|
|
|
});
|
|
|
|
$this->app->singleton('user_config', function ($app)
|
|
|
|
{
|
|
|
|
return new ConfigHelper();
|
|
|
|
});
|
2016-09-02 10:42:05 +01:00
|
|
|
|
2016-09-21 12:10:37 +01:00
|
|
|
// When running migrations or CLI tasks, don't need to add things to the view
|
|
|
|
if (php_sapi_name() != 'cli')
|
|
|
|
{
|
|
|
|
$this->addThemeInfoToView();
|
|
|
|
$this->addAlbumsToView();
|
|
|
|
}
|
2016-09-06 12:23:14 +01:00
|
|
|
|
2016-09-21 12:10:37 +01:00
|
|
|
// Set the default mail configuration as per user's requirements
|
|
|
|
$this->updateMailConfig();
|
|
|
|
}
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register any application services.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function register()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
2016-09-02 10:42:05 +01:00
|
|
|
|
2016-09-21 12:10:37 +01:00
|
|
|
private function checkIfInstalled()
|
|
|
|
{
|
|
|
|
if ($_SERVER['REQUEST_URI'] == '/install')
|
|
|
|
{
|
|
|
|
$baseDirectory = dirname(dirname(__DIR__));
|
|
|
|
|
|
|
|
// Generate an application key and store to the .env file
|
|
|
|
if (!file_exists($baseDirectory . '/.env'))
|
|
|
|
{
|
|
|
|
$key = MiscHelper::randomString(32);
|
|
|
|
file_put_contents($baseDirectory . '/.env', sprintf('APP_KEY=%s', $key) . PHP_EOL);
|
|
|
|
app('config')->set(['app' => ['key' => $key]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!Configuration::installCompleted())
|
|
|
|
{
|
|
|
|
throw new \Exception(sprintf('install_completed configuration record is not present!'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (\Exception $ex)
|
|
|
|
{
|
|
|
|
header(sprintf('Location: %s', url('/install')));
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-03 22:13:05 +01:00
|
|
|
private function addAlbumsToView()
|
|
|
|
{
|
|
|
|
$albums = Album::all()->sortBy('name');
|
|
|
|
View::share('albums', $albums);
|
|
|
|
}
|
|
|
|
|
2016-09-02 10:42:05 +01:00
|
|
|
private function addThemeInfoToView()
|
|
|
|
{
|
|
|
|
$themeInfo = Theme::info();
|
|
|
|
|
2016-09-02 10:56:07 +01:00
|
|
|
// Add each theme info element to the view - prefixing with theme_
|
2016-09-02 10:42:05 +01:00
|
|
|
// e.g. $themeInfo['name'] becomes $theme_name in the view
|
2016-09-02 10:56:07 +01:00
|
|
|
foreach ($themeInfo as $key => $value)
|
2016-09-02 10:42:05 +01:00
|
|
|
{
|
2016-09-02 10:56:07 +01:00
|
|
|
View::share('theme_' . $key, $value);
|
2016-09-02 10:42:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Also add a theme_url key
|
|
|
|
View::share('theme_url', sprintf('themes/%s', Theme::current()));
|
|
|
|
}
|
2016-09-06 12:23:14 +01:00
|
|
|
|
|
|
|
private function updateMailConfig()
|
|
|
|
{
|
2016-09-06 14:19:16 +01:00
|
|
|
/** @var Mailer $mailer */
|
|
|
|
$mailer = $this->app->mailer;
|
|
|
|
$swiftMailer = $mailer->getSwiftMailer();
|
2016-09-06 12:23:14 +01:00
|
|
|
|
|
|
|
/** @var \Swift_SmtpTransport $transport */
|
|
|
|
$transport = $swiftMailer->getTransport();
|
|
|
|
$transport->setHost(UserConfig::get('smtp_server'));
|
|
|
|
$transport->setPort(intval(UserConfig::get('smtp_port')));
|
2016-09-07 21:44:28 +01:00
|
|
|
|
|
|
|
$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));
|
|
|
|
}
|
2016-09-06 13:26:02 +01:00
|
|
|
|
|
|
|
if (UserConfig::get('smtp_encryption'))
|
|
|
|
{
|
|
|
|
$transport->setEncryption('tls');
|
|
|
|
}
|
2016-09-06 14:19:16 +01:00
|
|
|
|
|
|
|
$mailer->alwaysFrom(UserConfig::get('sender_address'), UserConfig::get('sender_name'));
|
2016-09-06 12:23:14 +01:00
|
|
|
}
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|