2018-01-26 14:35:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
2018-11-04 16:20:12 +00:00
|
|
|
use App\Application;
|
|
|
|
use App\Jobs\ProcessApps;
|
2022-11-21 23:05:57 +00:00
|
|
|
use App\Jobs\UpdateApps;
|
2022-03-19 13:54:32 +00:00
|
|
|
use App\Setting;
|
|
|
|
use App\User;
|
2022-11-29 22:13:24 +00:00
|
|
|
use Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider;
|
2022-11-18 15:12:52 +00:00
|
|
|
use Illuminate\Support\Facades\Artisan;
|
2022-11-22 16:22:36 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2022-11-18 15:12:52 +00:00
|
|
|
use Illuminate\Support\Facades\Schema;
|
2022-11-21 23:05:57 +00:00
|
|
|
use Illuminate\Support\ServiceProvider;
|
2022-11-25 00:35:56 +00:00
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
2018-01-26 14:35:01 +00:00
|
|
|
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Bootstrap any application services.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function boot()
|
|
|
|
{
|
2022-11-21 23:05:57 +00:00
|
|
|
if (! class_exists('ZipArchive')) {
|
|
|
|
die('You are missing php-zip');
|
|
|
|
}
|
|
|
|
|
2022-11-13 10:09:32 +00:00
|
|
|
$this->createEnvFile();
|
|
|
|
|
|
|
|
$this->setupDatabase();
|
2018-11-09 23:07:01 +00:00
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
if (! is_file(public_path('storage/.gitignore'))) {
|
2018-11-09 23:07:01 +00:00
|
|
|
Artisan::call('storage:link');
|
|
|
|
\Session::put('current_user', null);
|
|
|
|
}
|
2022-03-16 20:07:43 +00:00
|
|
|
|
2018-11-04 16:20:12 +00:00
|
|
|
$applications = Application::all();
|
2022-11-21 23:05:57 +00:00
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
if ($applications->count() <= 0) {
|
2022-11-21 23:05:57 +00:00
|
|
|
ProcessApps::dispatch();
|
2018-11-04 16:20:12 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 17:24:11 +00:00
|
|
|
$lang = Setting::fetch('language');
|
|
|
|
\App::setLocale($lang);
|
|
|
|
|
2018-10-15 18:56:45 +00:00
|
|
|
// User specific settings need to go here as session isn't available at this point in the app
|
2022-03-19 13:54:32 +00:00
|
|
|
view()->composer('*', function ($view) {
|
|
|
|
if (isset($_SERVER['HTTP_AUTHORIZATION']) && ! empty($_SERVER['HTTP_AUTHORIZATION'])) {
|
|
|
|
list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) =
|
2018-10-17 08:41:20 +00:00
|
|
|
explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
|
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
if (! \Auth::check()) {
|
|
|
|
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])
|
|
|
|
&& ! empty($_SERVER['PHP_AUTH_USER']) && ! empty($_SERVER['PHP_AUTH_PW'])) {
|
2018-10-17 08:41:20 +00:00
|
|
|
$credentials = ['username' => $_SERVER['PHP_AUTH_USER'], 'password' => $_SERVER['PHP_AUTH_PW']];
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2019-06-19 11:01:17 +00:00
|
|
|
if (\Auth::attempt($credentials, true)) {
|
2018-10-17 08:41:20 +00:00
|
|
|
// Authentication passed...
|
|
|
|
$user = \Auth::user();
|
2018-10-17 08:49:23 +00:00
|
|
|
//\Session::put('current_user', $user);
|
2022-03-19 13:54:32 +00:00
|
|
|
session(['current_user' => $user]);
|
2018-10-17 08:41:20 +00:00
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
} elseif (isset($_SERVER['REMOTE_USER']) && ! empty($_SERVER['REMOTE_USER'])) {
|
2019-06-19 10:32:41 +00:00
|
|
|
$user = User::where('username', $_SERVER['REMOTE_USER'])->first();
|
2019-11-15 20:30:10 +00:00
|
|
|
if ($user) {
|
|
|
|
\Auth::login($user, true);
|
|
|
|
session(['current_user' => $user]);
|
|
|
|
}
|
2019-06-19 10:32:41 +00:00
|
|
|
}
|
2018-10-17 08:41:20 +00:00
|
|
|
}
|
2018-10-17 08:49:23 +00:00
|
|
|
|
|
|
|
$alt_bg = '';
|
2023-05-26 08:23:06 +00:00
|
|
|
$trianglify = 'false';
|
|
|
|
$trianglify_seed = null;
|
|
|
|
if (Setting::fetch('trianglify')) {
|
|
|
|
$trianglify = 'true';
|
|
|
|
$trianglify_seed = Setting::fetch('trianglify_seed');
|
|
|
|
} elseif ($bg_image = Setting::fetch('background_image')) {
|
2020-02-04 10:41:53 +00:00
|
|
|
$alt_bg = ' style="background-image: url(storage/'.$bg_image.')"';
|
2018-10-17 08:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$allusers = User::all();
|
|
|
|
$current_user = User::currentUser();
|
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
$view->with('alt_bg', $alt_bg);
|
2023-05-26 08:23:06 +00:00
|
|
|
$view->with('trianglify', $trianglify);
|
|
|
|
$view->with('trianglify_seed', $trianglify_seed);
|
2022-03-19 13:54:32 +00:00
|
|
|
$view->with('allusers', $allusers);
|
|
|
|
$view->with('current_user', $current_user);
|
|
|
|
});
|
2018-10-15 18:56:45 +00:00
|
|
|
|
2018-10-20 23:17:36 +00:00
|
|
|
$this->app['view']->addNamespace('SupportedApps', app_path('SupportedApps'));
|
|
|
|
|
2018-09-30 10:45:08 +00:00
|
|
|
if (env('FORCE_HTTPS') === true) {
|
|
|
|
\URL::forceScheme('https');
|
|
|
|
}
|
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
if (env('APP_URL') != 'http://localhost') {
|
2018-10-15 08:03:54 +00:00
|
|
|
\URL::forceRootUrl(env('APP_URL'));
|
|
|
|
}
|
2018-01-26 14:35:01 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
/**
|
2019-01-15 15:23:10 +00:00
|
|
|
* Generate app key if missing and .env exists
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function genKey()
|
|
|
|
{
|
2022-03-19 13:54:32 +00:00
|
|
|
if (is_file(base_path('.env'))) {
|
|
|
|
if (empty(env('APP_KEY'))) {
|
|
|
|
Artisan::call('key:generate', ['--force' => true, '--no-interaction' => true]);
|
2019-01-15 15:23:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 14:35:01 +00:00
|
|
|
/**
|
|
|
|
* Register any application services.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function register()
|
|
|
|
{
|
2022-11-29 22:13:24 +00:00
|
|
|
if ($this->app->isLocal()) {
|
|
|
|
$this->app->register(IdeHelperServiceProvider::class);
|
|
|
|
}
|
|
|
|
|
2018-02-04 20:50:59 +00:00
|
|
|
$this->app->singleton('settings', function () {
|
|
|
|
return new Setting();
|
|
|
|
});
|
2018-01-26 14:35:01 +00:00
|
|
|
}
|
2022-11-13 10:09:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if database needs an update or do first time database setup
|
|
|
|
*
|
|
|
|
* @return void
|
2022-11-25 00:35:56 +00:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws NotFoundExceptionInterface
|
2022-11-13 10:09:32 +00:00
|
|
|
*/
|
|
|
|
public function setupDatabase(): void
|
|
|
|
{
|
2022-11-14 09:24:58 +00:00
|
|
|
$db_type = config()->get('database.default');
|
2022-11-13 10:09:32 +00:00
|
|
|
|
2022-12-04 09:14:07 +00:00
|
|
|
if ($db_type == 'sqlite') {
|
|
|
|
$db_file = database_path(env('DB_DATABASE', 'app.sqlite'));
|
|
|
|
if (! is_file($db_file)) {
|
|
|
|
touch($db_file);
|
|
|
|
}
|
2022-11-13 10:09:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 15:12:52 +00:00
|
|
|
if ($this->needsDBUpdate()) {
|
2022-11-13 10:09:32 +00:00
|
|
|
Artisan::call('migrate', ['--path' => 'database/migrations', '--force' => true, '--seed' => true]);
|
2022-11-18 15:12:52 +00:00
|
|
|
ProcessApps::dispatchSync();
|
|
|
|
$this->updateApps();
|
2022-11-13 10:09:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function createEnvFile(): void
|
|
|
|
{
|
|
|
|
if (!is_file(base_path('.env'))) {
|
|
|
|
copy(base_path('.env.example'), base_path('.env'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->genKey();
|
|
|
|
}
|
2022-11-18 15:12:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function needsDBUpdate(): bool
|
|
|
|
{
|
|
|
|
if (!Schema::hasTable('settings')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$db_version = Setting::_fetch('version');
|
|
|
|
$app_version = config('app.version');
|
|
|
|
|
|
|
|
return version_compare($app_version, $db_version) === 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function updateApps()
|
|
|
|
{
|
2022-11-22 16:22:36 +00:00
|
|
|
// This lock ensures that the job is not invoked multiple times.
|
|
|
|
// In 5 minutes all app updates should be finished.
|
|
|
|
$lock = Cache::lock('updateApps', 5*60);
|
|
|
|
|
|
|
|
if ($lock->get()) {
|
|
|
|
UpdateApps::dispatchAfterResponse();
|
|
|
|
}
|
2022-11-18 15:12:52 +00:00
|
|
|
}
|
2018-01-26 14:35:01 +00:00
|
|
|
}
|