AppServiceProvider.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Providers;
  3. use App\Models\Settings;
  4. use Exception;
  5. use Illuminate\Pagination\Paginator;
  6. use Illuminate\Support\Facades\Artisan;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Schema;
  9. use Illuminate\Support\Facades\Validator;
  10. use Illuminate\Support\ServiceProvider;
  11. use Qirolab\Theme\Theme;
  12. class AppServiceProvider extends ServiceProvider
  13. {
  14. /**
  15. * Register any application services.
  16. *
  17. * @return void
  18. */
  19. public function register()
  20. {
  21. //
  22. }
  23. /**
  24. * Bootstrap any application services.
  25. *
  26. * @return void
  27. */
  28. public function boot()
  29. {
  30. Paginator::useBootstrap();
  31. Schema::defaultStringLength(191);
  32. Validator::extend('multiple_date_format', function ($attribute, $value, $parameters, $validator) {
  33. $ok = true;
  34. $result = [];
  35. // iterate through all formats
  36. foreach ($parameters as $parameter) {
  37. //validate with laravels standard date format validation
  38. $result[] = $validator->validateDateFormat($attribute, $value, [$parameter]);
  39. }
  40. //if none of result array is true. it sets ok to false
  41. if (! in_array(true, $result)) {
  42. $ok = false;
  43. $validator->setCustomMessages(['multiple_date_format' => 'The format must be one of '.implode(',', $parameters)]);
  44. }
  45. return $ok;
  46. });
  47. //only run if the installer has been executed
  48. try {
  49. $settings = Settings::all();
  50. // Set all configs from database
  51. foreach ($settings as $setting) {
  52. config([$setting->key => $setting->value]);
  53. }
  54. if(!file_exists(base_path('themes')."/".config("SETTINGS::SYSTEM:THEME"))){
  55. config(['SETTINGS::SYSTEM:THEME' => "default"]);
  56. }
  57. if(config('theme.active') == null){
  58. Theme::set(config("SETTINGS::SYSTEM:THEME","default"), "default");
  59. }
  60. // Set Mail Config
  61. //only update config if mail settings have changed in DB
  62. if (
  63. config('mail.default') != config('SETTINGS:MAIL:MAILER') ||
  64. config('mail.mailers.smtp.host') != config('SETTINGS:MAIL:HOST') ||
  65. config('mail.mailers.smtp.port') != config('SETTINGS:MAIL:PORT') ||
  66. config('mail.mailers.smtp.username') != config('SETTINGS:MAIL:USERNAME') ||
  67. config('mail.mailers.smtp.password') != config('SETTINGS:MAIL:PASSWORD') ||
  68. config('mail.mailers.smtp.encryption') != config('SETTINGS:MAIL:ENCRYPTION') ||
  69. config('mail.from.address') != config('SETTINGS:MAIL:FROM_ADDRESS') ||
  70. config('mail.from.name') != config('SETTINGS:MAIL:FROM_NAME')
  71. ) {
  72. config(['mail.default' => config('SETTINGS::MAIL:MAILER')]);
  73. config(['mail.mailers.smtp' => [
  74. 'transport' => 'smtp',
  75. 'host' => config('SETTINGS::MAIL:HOST'),
  76. 'port' => config('SETTINGS::MAIL:PORT'),
  77. 'encryption' => config('SETTINGS::MAIL:ENCRYPTION'),
  78. 'username' => config('SETTINGS::MAIL:USERNAME'),
  79. 'password' => config('SETTINGS::MAIL:PASSWORD'),
  80. 'timeout' => null,
  81. 'auth_mode' => null,
  82. ]]);
  83. config(['mail.from' => ['address' => config('SETTINGS::MAIL:FROM_ADDRESS'), 'name' => config('SETTINGS::MAIL:FROM_NAME')]]);
  84. Artisan::call('queue:restart');
  85. }
  86. // Set Recaptcha API Config
  87. // Load recaptcha package if recaptcha is enabled
  88. if (config('SETTINGS::RECAPTCHA:ENABLED') == 'true') {
  89. $this->app->register(\Biscolab\ReCaptcha\ReCaptchaServiceProvider::class);
  90. }
  91. //only update config if recaptcha settings have changed in DB
  92. if (
  93. config('recaptcha.api_site_key') != config('SETTINGS::RECAPTCHA:SITE_KEY') ||
  94. config('recaptcha.api_secret_key') != config('SETTINGS::RECAPTCHA:SECRET_KEY')
  95. ) {
  96. config(['recaptcha.api_site_key' => config('SETTINGS::RECAPTCHA:SITE_KEY')]);
  97. config(['recaptcha.api_secret_key' => config('SETTINGS::RECAPTCHA:SECRET_KEY')]);
  98. Artisan::call('config:clear');
  99. Artisan::call('cache:clear');
  100. }
  101. try {
  102. $stringfromfile = file(base_path().'/.git/HEAD');
  103. $firstLine = $stringfromfile[0]; //get the string from the array
  104. $explodedstring = explode('/', $firstLine, 3); //seperate out by the "/" in the string
  105. $branchname = $explodedstring[2]; //get the one that is always the branch name
  106. } catch (Exception $e) {
  107. $branchname = 'unknown';
  108. Log::error($e);
  109. }
  110. config(['BRANCHNAME' => $branchname]);
  111. // Set Discord-API Config
  112. config(['services.discord.client_id' => config('SETTINGS::DISCORD:CLIENT_ID')]);
  113. config(['services.discord.client_secret' => config('SETTINGS::DISCORD:CLIENT_SECRET')]);
  114. } catch (Exception $e) {
  115. error_log('Settings Error: Could not load settings from database. The Installation probably is not done yet.');
  116. error_log($e);
  117. Log::error('Settings Error: Could not load settings from database. The Installation probably is not done yet.');
  118. Log::error($e);
  119. }
  120. }
  121. }