39
common/Settings/Validators/AnalyticsCredentialsValidator.php
Executable file
39
common/Settings/Validators/AnalyticsCredentialsValidator.php
Executable file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators;
|
||||
|
||||
use Common\Admin\Analytics\Actions\BuildGoogleAnalyticsReport;
|
||||
use Exception;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
|
||||
class AnalyticsCredentialsValidator
|
||||
{
|
||||
const KEYS = [
|
||||
'analytics_property_id',
|
||||
'analytics.tracking_code',
|
||||
'certificate',
|
||||
];
|
||||
|
||||
public function fails($settings): array|false
|
||||
{
|
||||
$this->setConfigDynamically($settings);
|
||||
|
||||
try {
|
||||
app(BuildGoogleAnalyticsReport::class)->execute([]);
|
||||
} catch (Exception $e) {
|
||||
return [
|
||||
'analytics_group' => "Invalid credentials: {$e->getMessage()}",
|
||||
];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function setConfigDynamically(array $settings): void
|
||||
{
|
||||
if ($propertyId = Arr::get($settings, 'analytics_property_id')) {
|
||||
Config::set('services.google.analytics_property_id', $propertyId);
|
||||
}
|
||||
}
|
||||
}
|
||||
64
common/Settings/Validators/CacheConfigValidator.php
Executable file
64
common/Settings/Validators/CacheConfigValidator.php
Executable file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators;
|
||||
|
||||
use Common\Settings\DotEnvEditor;
|
||||
use Exception;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Str;
|
||||
use Throwable;
|
||||
|
||||
class CacheConfigValidator
|
||||
{
|
||||
const KEYS = ['cache_driver'];
|
||||
|
||||
public function fails($settings)
|
||||
{
|
||||
$this->setConfigDynamically($settings);
|
||||
|
||||
try {
|
||||
$driverName = Arr::get(
|
||||
$settings,
|
||||
'cache_driver',
|
||||
config('cache.default'),
|
||||
);
|
||||
$driver = Cache::driver($driverName);
|
||||
$driver->put('foo', 'bar', 1);
|
||||
if ($driver->get('foo') !== 'bar') {
|
||||
return $this->getDefaultErrorMessage();
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
return $this->getErrorMessage($e);
|
||||
} catch (Throwable $e) {
|
||||
return $this->getErrorMessage($e);
|
||||
}
|
||||
}
|
||||
|
||||
private function setConfigDynamically($settings)
|
||||
{
|
||||
app(DotEnvEditor::class)->write(
|
||||
Arr::except($settings, ['cache_driver']),
|
||||
);
|
||||
}
|
||||
|
||||
private function getErrorMessage($e): array
|
||||
{
|
||||
$message = $e->getMessage();
|
||||
|
||||
if (Str::contains($message, 'apc_fetch')) {
|
||||
return ['cache_group' => "Could not enable APC. $message"];
|
||||
} elseif (Str::contains($message, 'Memcached')) {
|
||||
return ['cache_group' => "Could not enable Memcached. $message"];
|
||||
} elseif (Str::contains($message, 'Connection refused')) {
|
||||
return ['cache_group' => 'Could not connect to redis server.'];
|
||||
} else {
|
||||
return $this->getDefaultErrorMessage();
|
||||
}
|
||||
}
|
||||
|
||||
private function getDefaultErrorMessage(): array
|
||||
{
|
||||
return ['cache_group' => 'Could not enable this cache method.'];
|
||||
}
|
||||
}
|
||||
84
common/Settings/Validators/FacebookLoginValidator.php
Executable file
84
common/Settings/Validators/FacebookLoginValidator.php
Executable file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators;
|
||||
|
||||
use Common\Auth\Oauth;
|
||||
use Common\Core\HttpClient;
|
||||
use Config;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use GuzzleHttp\Exception\ServerException;
|
||||
use Illuminate\Support\Arr;
|
||||
use Socialite;
|
||||
|
||||
class FacebookLoginValidator implements SettingsValidator
|
||||
{
|
||||
const KEYS = ['facebook_id', 'facebook_secret'];
|
||||
|
||||
/**
|
||||
* @var Oauth
|
||||
*/
|
||||
private $oauth;
|
||||
|
||||
/**
|
||||
* @var HttpClient
|
||||
*/
|
||||
private $httpClient;
|
||||
|
||||
public function __construct(Oauth $oauth)
|
||||
{
|
||||
$this->oauth = $oauth;
|
||||
$this->httpClient = new HttpClient([
|
||||
'exceptions' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function fails($values)
|
||||
{
|
||||
$this->setConfigDynamically($values);
|
||||
|
||||
try {
|
||||
Socialite::driver('facebook')->getAccessTokenResponse('foo-bar');
|
||||
} catch (ClientException $e) {
|
||||
return $this->getErrorMessage($e);
|
||||
} catch (ServerException $e) {
|
||||
return $this->getDefaultError();
|
||||
}
|
||||
}
|
||||
|
||||
private function setConfigDynamically($settings)
|
||||
{
|
||||
if ($facebookId = Arr::get($settings, 'facebook_id')) {
|
||||
Config::set('services.facebook.client_id', $facebookId);
|
||||
}
|
||||
|
||||
if ($facebookSecret = Arr::get($settings, 'facebook_secret')) {
|
||||
Config::set('services.facebook.client_secret', $facebookSecret);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClientException $e
|
||||
* @return array
|
||||
*/
|
||||
private function getErrorMessage(ClientException $e)
|
||||
{
|
||||
$errResponse = json_decode($e->getResponse()->getBody()->getContents(), true);
|
||||
$code = Arr::get($errResponse, 'error.code');
|
||||
|
||||
// there were no credentials related errors, we can assume validation was successful
|
||||
if ($code === 100) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($code === 191) {
|
||||
return ['facebook_group' => 'Site url is not present in "Valid OAuth Redirect URIs" field on your facebook app.'];
|
||||
}
|
||||
|
||||
return $this->getDefaultError();
|
||||
}
|
||||
|
||||
private function getDefaultError()
|
||||
{
|
||||
return ['facebook_group' => 'These facebook credentials are not valid.'];
|
||||
}
|
||||
}
|
||||
85
common/Settings/Validators/GoogleLoginValidator.php
Executable file
85
common/Settings/Validators/GoogleLoginValidator.php
Executable file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators;
|
||||
|
||||
use Common\Auth\Oauth;
|
||||
use Common\Core\HttpClient;
|
||||
use Config;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use Illuminate\Support\Arr;
|
||||
use Socialite;
|
||||
|
||||
class GoogleLoginValidator implements SettingsValidator
|
||||
{
|
||||
const KEYS = ['google_id', 'google_secret'];
|
||||
|
||||
/**
|
||||
* @var Oauth
|
||||
*/
|
||||
private $oauth;
|
||||
|
||||
/**
|
||||
* @var HttpClient
|
||||
*/
|
||||
private $httpClient;
|
||||
|
||||
public function __construct(Oauth $oauth)
|
||||
{
|
||||
$this->oauth = $oauth;
|
||||
$this->httpClient = new HttpClient([
|
||||
'exceptions' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function fails($values)
|
||||
{
|
||||
$this->setConfigDynamically($values);
|
||||
|
||||
try {
|
||||
Socialite::driver('google')->getAccessTokenResponse('foo-bar');
|
||||
} catch (ClientException $e) {
|
||||
return $this->getErrorMessage($e);
|
||||
}
|
||||
}
|
||||
|
||||
private function setConfigDynamically($settings)
|
||||
{
|
||||
if ($googleId = Arr::get($settings, 'google_id')) {
|
||||
Config::set('services.google.client_id', $googleId);
|
||||
}
|
||||
|
||||
if ($googleSecret = Arr::get($settings, 'google_secret')) {
|
||||
Config::set('services.google.client_secret', $googleSecret);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClientException $e
|
||||
* @return array
|
||||
*/
|
||||
private function getErrorMessage(ClientException $e)
|
||||
{
|
||||
$errResponse = json_decode(
|
||||
$e
|
||||
->getResponse()
|
||||
->getBody()
|
||||
->getContents(),
|
||||
true,
|
||||
);
|
||||
|
||||
// there were no credentials related errors, we can assume validation was successful
|
||||
if (
|
||||
Arr::get($errResponse, 'error_description') ===
|
||||
'Malformed auth code.'
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$msg1 = Arr::get($errResponse, 'error.errors.0.message', '');
|
||||
$msg2 = Arr::get($errResponse, 'error_description', '');
|
||||
$message = strtolower($msg1 ?: $msg2);
|
||||
return [
|
||||
'google_group' => "Could not validate these credentials: $message",
|
||||
];
|
||||
}
|
||||
}
|
||||
29
common/Settings/Validators/LoggingCredentialsValidator.php
Executable file
29
common/Settings/Validators/LoggingCredentialsValidator.php
Executable file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators;
|
||||
|
||||
use Exception;
|
||||
use Sentry\Dsn;
|
||||
|
||||
class LoggingCredentialsValidator
|
||||
{
|
||||
const KEYS = ['sentry_dsn'];
|
||||
|
||||
public function fails($settings)
|
||||
{
|
||||
try {
|
||||
Dsn::createFromString($settings['sentry_dsn']);
|
||||
} catch (Exception $e) {
|
||||
return $this->getErrorMessage($e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Exception $e
|
||||
* @return array
|
||||
*/
|
||||
private function getErrorMessage($e)
|
||||
{
|
||||
return ['logging_group' => 'This sentry DSN is not valid.'];
|
||||
}
|
||||
}
|
||||
33
common/Settings/Validators/MailCredentials/MailCredentialsMailable.php
Executable file
33
common/Settings/Validators/MailCredentials/MailCredentialsMailable.php
Executable file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators\MailCredentials;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class MailCredentialsMailable extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the message.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
return $this
|
||||
->markdown('common::emails.mail-validation')
|
||||
->subject(config('app.name') . ' Mail Set Up Successfully!');
|
||||
}
|
||||
}
|
||||
143
common/Settings/Validators/MailCredentials/OutgoingMailCredentialsValidator.php
Executable file
143
common/Settings/Validators/MailCredentials/OutgoingMailCredentialsValidator.php
Executable file
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators\MailCredentials;
|
||||
|
||||
use Arr;
|
||||
use Auth;
|
||||
use Aws\Ses\Exception\SesException;
|
||||
use Common\CommonServiceProvider;
|
||||
use Common\Settings\DotEnvEditor;
|
||||
use Common\Settings\Validators\SettingsValidator;
|
||||
use Config;
|
||||
use Exception;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use Illuminate\Mail\MailServiceProvider;
|
||||
use Mail;
|
||||
use Str;
|
||||
|
||||
class OutgoingMailCredentialsValidator implements SettingsValidator
|
||||
{
|
||||
const KEYS = [
|
||||
'mail_driver',
|
||||
'mail_host',
|
||||
'mail_username',
|
||||
'mail_password',
|
||||
'mail_port',
|
||||
'mail_encryption', // SMTP
|
||||
'mailgun_domain',
|
||||
'mailgun_secret', // Mailgun
|
||||
'ses_key',
|
||||
'ses_secret', // Amazon SES
|
||||
'sparkpost_secret', // Sparkpost
|
||||
];
|
||||
|
||||
public function fails($values)
|
||||
{
|
||||
$this->setConfigDynamically($values);
|
||||
|
||||
try {
|
||||
Mail::to(Auth::user()->email)->send(new MailCredentialsMailable());
|
||||
} catch (Exception $e) {
|
||||
app(DotEnvEditor::class)->write(['MAIL_SETUP' => false]);
|
||||
return $this->getErrorMessage($e);
|
||||
}
|
||||
|
||||
app(DotEnvEditor::class)->write(['MAIL_SETUP' => true]);
|
||||
}
|
||||
|
||||
private function setConfigDynamically($settings)
|
||||
{
|
||||
foreach ($settings as $key => $value) {
|
||||
//mail_host => mail.host
|
||||
$key = str_replace('_', '.', $key);
|
||||
|
||||
// "mail.*" credentials go into "mail.php" config
|
||||
// file, other credentials go into "services.php"
|
||||
if ($key === 'mail.driver') {
|
||||
$key = 'mail.default';
|
||||
} elseif ($key === 'mail_from_address') {
|
||||
$key = 'mail.from.address';
|
||||
} elseif (!Str::startsWith($key, 'mail.')) {
|
||||
$key = "services.$key";
|
||||
} else {
|
||||
$key = str_replace('mail.', 'mail.mailers.smtp.', $key);
|
||||
}
|
||||
|
||||
Config::set($key, $value);
|
||||
}
|
||||
|
||||
// make sure laravel uses newly set config
|
||||
(new MailServiceProvider(app()))->register();
|
||||
(new CommonServiceProvider(app()))->registerCustomMailDrivers();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Exception|ClientException $e
|
||||
* @return array
|
||||
*/
|
||||
private function getErrorMessage($e)
|
||||
{
|
||||
$message = null;
|
||||
if (config('mail.driver') === 'smtp') {
|
||||
$message = $this->getSmtpMessage($e);
|
||||
} elseif (config('mail.driver') === 'mailgun') {
|
||||
$message = $this->getMailgunMessage($e);
|
||||
} elseif (config('mail.driver') === 'ses') {
|
||||
$message = $this->getSesMessage($e);
|
||||
}
|
||||
|
||||
return $message ?: $this->getDefaultMessage($e);
|
||||
}
|
||||
|
||||
private function getSesMessage(SesException $e)
|
||||
{
|
||||
return ['mail_group' => $e->getAwsErrorMessage()];
|
||||
}
|
||||
|
||||
private function getMailgunMessage(ClientException $e)
|
||||
{
|
||||
$originalContents = $e
|
||||
->getResponse()
|
||||
->getBody()
|
||||
->getContents();
|
||||
$errResponse = json_decode($originalContents, true);
|
||||
if (is_null($errResponse) && is_string($originalContents)) {
|
||||
$errResponse = $originalContents;
|
||||
}
|
||||
$message = strtolower(Arr::get($errResponse, 'message', $errResponse));
|
||||
|
||||
if (Str::contains($message, 'domain not found')) {
|
||||
return [
|
||||
'server.mailgun_domain' => 'This mailgun domain is not valid.',
|
||||
];
|
||||
} elseif (Str::contains($message, 'forbidden')) {
|
||||
return [
|
||||
'server.mailgun_secret' => 'This mailgun API Key is not valid.',
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'mail_group' =>
|
||||
'Could not validate mailgun credentials. Please double check them.',
|
||||
];
|
||||
}
|
||||
|
||||
private function getSmtpMessage(Exception $e): ?array
|
||||
{
|
||||
if (Str::contains($e->getMessage(), 'Connection timed out #110')) {
|
||||
return [
|
||||
'mail_group' =>
|
||||
'Connection to mail server timed out. This usually indicates incorrect mail credentials. Please double check them.',
|
||||
];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function getDefaultMessage(Exception $e): array
|
||||
{
|
||||
return [
|
||||
'mail_group' => "Could not validate mail credentials: <br> {$e->getMessage()}",
|
||||
];
|
||||
}
|
||||
}
|
||||
96
common/Settings/Validators/PaypalCredentialsValidator.php
Executable file
96
common/Settings/Validators/PaypalCredentialsValidator.php
Executable file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators;
|
||||
|
||||
use Common\Billing\Gateways\Paypal\Paypal;
|
||||
use Common\Settings\Settings;
|
||||
use Config;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class PaypalCredentialsValidator implements SettingsValidator
|
||||
{
|
||||
const KEYS = [
|
||||
'paypal_client_id',
|
||||
'paypal_secret',
|
||||
'paypal_webhook_id',
|
||||
'billing.paypal_test_mode',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var Settings
|
||||
*/
|
||||
private $settings;
|
||||
|
||||
/**
|
||||
* @param Settings $settings
|
||||
*/
|
||||
public function __construct(Settings $settings)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function fails($values)
|
||||
{
|
||||
$this->setConfigDynamically($values);
|
||||
|
||||
// create gateway after setting config dynamically
|
||||
// so gateway uses new configuration
|
||||
|
||||
try {
|
||||
$response = app(Paypal::class)
|
||||
->paypal()
|
||||
->get('payments/billing-plans');
|
||||
if (!$response->successful()) {
|
||||
return $this->getErrorMessage($response->body());
|
||||
}
|
||||
} catch (ClientException $e) {
|
||||
return $this->getDefaultError();
|
||||
}
|
||||
}
|
||||
|
||||
private function setConfigDynamically($settings)
|
||||
{
|
||||
foreach (self::KEYS as $key) {
|
||||
if (!Arr::has($settings, $key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($key === 'billing.paypal_test_mode') {
|
||||
$this->settings->set(
|
||||
'billing.paypal_test_mode',
|
||||
$settings[$key],
|
||||
);
|
||||
} else {
|
||||
// paypal_client_id => client_id
|
||||
$configKey = str_replace('paypal_', '', $key);
|
||||
Config::set("services.paypal.$configKey", $settings[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
private function getErrorMessage($data)
|
||||
{
|
||||
$message = Arr::get($data, 'message');
|
||||
if ($data['name'] === 'AUTHENTICATION_FAILURE') {
|
||||
return [
|
||||
'paypal_group' =>
|
||||
'Paypal Client ID or Paypal Secret is invalid.',
|
||||
];
|
||||
} elseif ($message) {
|
||||
$infoLink = Arr::get($data, 'information_link');
|
||||
return ['paypal_group' => "$message. $infoLink"];
|
||||
} else {
|
||||
return $this->getDefaultError();
|
||||
}
|
||||
}
|
||||
|
||||
private function getDefaultError()
|
||||
{
|
||||
return ['paypal_group' => 'These paypal credentials are not valid.'];
|
||||
}
|
||||
}
|
||||
52
common/Settings/Validators/QueueCredentialsValidator.php
Executable file
52
common/Settings/Validators/QueueCredentialsValidator.php
Executable file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators;
|
||||
|
||||
use Config;
|
||||
use Queue;
|
||||
use Exception;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class QueueCredentialsValidator
|
||||
{
|
||||
const KEYS = [
|
||||
'queue_driver',
|
||||
|
||||
// sqs
|
||||
'SQS_QUEUE_KEY', 'SQS_QUEUE_SECRET', 'SQS_QUEUE_PREFIX', 'SQS_QUEUE_NAME', 'SQS_QUEUE_REGION',
|
||||
];
|
||||
|
||||
public function fails($settings)
|
||||
{
|
||||
$this->setConfigDynamically($settings);
|
||||
|
||||
$driver = Arr::get($settings, 'queue_driver', config('queue.default'));
|
||||
try {
|
||||
Queue::connection($driver)->size();
|
||||
} catch (Exception $e) {
|
||||
return $this->getErrorMessage($e, $driver);
|
||||
}
|
||||
}
|
||||
|
||||
private function setConfigDynamically($settings)
|
||||
{
|
||||
foreach ($settings as $key => $value) {
|
||||
// SQS_QUEUE_KEY => sqs.queue.key
|
||||
$key = strtolower(str_replace('_', '.', $key));
|
||||
// sqs.queue.key => sqs.key
|
||||
$key = str_replace('queue.', '', $key);
|
||||
$key = str_replace('name', 'queue', $key);
|
||||
Config::set("queue.connections.$key", $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Exception $e
|
||||
* @param string $driver
|
||||
* @return array
|
||||
*/
|
||||
private function getErrorMessage($e, $driver)
|
||||
{
|
||||
return ['queue_group' => "Could not change queue driver to <strong>$driver</strong>.<br> {$e->getMessage()}"];
|
||||
}
|
||||
}
|
||||
51
common/Settings/Validators/RealtimeCredentialsValidator.php
Executable file
51
common/Settings/Validators/RealtimeCredentialsValidator.php
Executable file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators;
|
||||
|
||||
use Config;
|
||||
use Exception;
|
||||
use Arr;
|
||||
use Pusher\Pusher;
|
||||
|
||||
class RealtimeCredentialsValidator
|
||||
{
|
||||
const KEYS = ['pusher_key', 'pusher_secret', 'pusher_app_id', 'pusher_cluster'];
|
||||
|
||||
public function fails($settings)
|
||||
{
|
||||
$this->setConfigDynamically($settings);
|
||||
|
||||
try {
|
||||
$config = Config::get('broadcasting.connections.pusher');
|
||||
$pusher = new Pusher($config['key'], $config['secret'],
|
||||
$config['app_id'], Arr::get($config, 'options', []));
|
||||
if ($pusher->get_channels() === false) {
|
||||
return $this->getErrorMessage();
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
return $this->getErrorMessage();
|
||||
}
|
||||
}
|
||||
|
||||
private function setConfigDynamically($settings)
|
||||
{
|
||||
foreach (self::KEYS as $key) {
|
||||
if ( ! Arr::has($settings, $key)) continue;
|
||||
if ($key === 'pusher_cluster') {
|
||||
Config::set("broadcasting.connections.pusher.options.cluster", $settings[$key]);
|
||||
} else {
|
||||
$configKey = str_replace('pusher_', '', $key);
|
||||
Config::set("broadcasting.connections.pusher.$configKey", $settings[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Exception $e
|
||||
* @return array
|
||||
*/
|
||||
private function getErrorMessage($e = null)
|
||||
{
|
||||
return ['pusher_group' => 'These pusher credentials are not valid.'];
|
||||
}
|
||||
}
|
||||
45
common/Settings/Validators/RecaptchaCredentialsValidator.php
Executable file
45
common/Settings/Validators/RecaptchaCredentialsValidator.php
Executable file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class RecaptchaCredentialsValidator
|
||||
{
|
||||
const KEYS = ['recaptcha.site_key', 'recaptcha.secret_key'];
|
||||
|
||||
public function fails($settings): array|false
|
||||
{
|
||||
try {
|
||||
$response = Http::asForm()->post(
|
||||
'https://www.google.com/recaptcha/api/siteverify',
|
||||
[
|
||||
'response' => 'foo-bar',
|
||||
'secret' => Arr::get($settings, 'recaptcha.secret_key'),
|
||||
],
|
||||
);
|
||||
|
||||
if (
|
||||
$response['success'] === false &&
|
||||
$response['error-codes'][0] !== 'invalid-input-response'
|
||||
) {
|
||||
return [
|
||||
'recaptcha_group' =>
|
||||
Arr::get($response, 'error-codes')[0] ??
|
||||
__('These credentials are not valid'),
|
||||
];
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
return $this->getErrorMessage($e);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function getErrorMessage(Exception $e): array
|
||||
{
|
||||
return ['recaptcha_group' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
79
common/Settings/Validators/SearchConfigValidator.php
Executable file
79
common/Settings/Validators/SearchConfigValidator.php
Executable file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators;
|
||||
|
||||
use App\Models\User;
|
||||
use Arr;
|
||||
use Exception;
|
||||
use Laravel\Scout\Builder;
|
||||
use Laravel\Scout\EngineManager;
|
||||
use Matchish\ScoutElasticSearch\ElasticSearchServiceProvider;
|
||||
use Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine;
|
||||
use Throwable;
|
||||
|
||||
class SearchConfigValidator
|
||||
{
|
||||
const KEYS = ['scout_driver'];
|
||||
|
||||
public function fails($settings)
|
||||
{
|
||||
$engineName = Arr::get(
|
||||
$settings,
|
||||
'scout_driver',
|
||||
config('scout.driver'),
|
||||
);
|
||||
$manager = app(EngineManager::class);
|
||||
|
||||
if (isset($settings['algolia_app_id'])) {
|
||||
config()->set('scout.algolia.id', $settings['algolia_app_id']);
|
||||
}
|
||||
if (isset($settings['algolia_secret'])) {
|
||||
config()->set('scout.algolia.secret', $settings['algolia_secret']);
|
||||
}
|
||||
|
||||
if (
|
||||
$engineName === 'mysql' &&
|
||||
Arr::get($settings, 'scout_mysql_mode') !== 'fulltext'
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// register elastic search provider, if not registered already
|
||||
if (
|
||||
$engineName === ElasticSearchEngine::class &&
|
||||
empty(app()->getProviders(ElasticSearchServiceProvider::class))
|
||||
) {
|
||||
app()->register(ElasticSearchServiceProvider::class);
|
||||
}
|
||||
|
||||
$results = $manager->engine($engineName)->search(
|
||||
app(Builder::class, [
|
||||
'model' => new User(),
|
||||
'query' => 'test',
|
||||
]),
|
||||
);
|
||||
if (!$results) {
|
||||
return $this->getDefaultErrorMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Exception|Throwable $e
|
||||
* @return array
|
||||
*/
|
||||
private function getErrorMessage($e)
|
||||
{
|
||||
$message = $e->getMessage();
|
||||
return [
|
||||
'search_group' => "Could not enable this search method: $message",
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getDefaultErrorMessage()
|
||||
{
|
||||
return ['search_group' => 'Could not enable this search method.'];
|
||||
}
|
||||
}
|
||||
12
common/Settings/Validators/SettingsValidator.php
Executable file
12
common/Settings/Validators/SettingsValidator.php
Executable file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators;
|
||||
|
||||
interface SettingsValidator
|
||||
{
|
||||
/**
|
||||
* @param array $values
|
||||
* @return null|array
|
||||
*/
|
||||
public function fails($values);
|
||||
}
|
||||
69
common/Settings/Validators/StaticFileDeliveryValidator.php
Executable file
69
common/Settings/Validators/StaticFileDeliveryValidator.php
Executable file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators;
|
||||
|
||||
use Common\Files\Actions\CreateFileEntry;
|
||||
use Common\Files\Actions\Deletion\PermanentlyDeleteEntries;
|
||||
use Common\Files\Actions\StoreFile;
|
||||
use Common\Files\FileEntryPayload;
|
||||
use Common\Settings\DotEnvEditor;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class StaticFileDeliveryValidator implements SettingsValidator
|
||||
{
|
||||
const KEYS = ['static_file_delivery'];
|
||||
|
||||
public function fails($values): bool|array
|
||||
{
|
||||
if (!$values['static_file_delivery']) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$originalDelivery = config('common.site.static_file_delivery');
|
||||
$originalDriver = config('common.site.uploads_disk_driver');
|
||||
|
||||
app(DotEnvEditor::class)->write([
|
||||
'STATIC_FILE_DELIVERY' => $values['static_file_delivery'],
|
||||
'UPLOADS_DISK_DRIVER' => 'local',
|
||||
]);
|
||||
|
||||
$previewToken = Str::random(10);
|
||||
$contents = Str::random(10);
|
||||
|
||||
$path = base_path('common/resources/lorem.html');
|
||||
$uploadedFile = new UploadedFile(
|
||||
$path,
|
||||
basename($path),
|
||||
'text/html',
|
||||
filesize($path),
|
||||
);
|
||||
$payload = new FileEntryPayload([
|
||||
'file' => $uploadedFile,
|
||||
]);
|
||||
$fileEntry = app(CreateFileEntry::class)->execute($payload);
|
||||
$fileEntry->fill(['preview_token' => $previewToken])->save();
|
||||
app(StoreFile::class)->execute($payload, ['file' => $uploadedFile]);
|
||||
|
||||
$response = Http::get(
|
||||
url($fileEntry->url) . "?preview_token=$previewToken",
|
||||
);
|
||||
app(PermanentlyDeleteEntries::class)->execute([$fileEntry->id]);
|
||||
|
||||
app(DotEnvEditor::class)->write([
|
||||
'STATIC_FILE_DELIVERY' => $originalDelivery,
|
||||
'UPLOADS_DISK_DRIVER' => $originalDriver,
|
||||
]);
|
||||
|
||||
if ($contents !== $response->body()) {
|
||||
return [
|
||||
'static_delivery_group' => __(
|
||||
'Could not validate selected optimization. Is it enabled on the server?',
|
||||
),
|
||||
];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
161
common/Settings/Validators/StorageCredentialsValidator.php
Executable file
161
common/Settings/Validators/StorageCredentialsValidator.php
Executable file
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators;
|
||||
|
||||
use Aws\S3\Exception\S3Exception;
|
||||
use Common\Files\Providers\BackblazeServiceProvider;
|
||||
use Common\Files\Providers\DigitalOceanServiceProvider;
|
||||
use Common\Files\Providers\DropboxServiceProvider;
|
||||
use Config;
|
||||
use Exception;
|
||||
use Spatie\FlysystemDropbox\DropboxAdapter;
|
||||
use Storage;
|
||||
use Str;
|
||||
|
||||
class StorageCredentialsValidator
|
||||
{
|
||||
const KEYS = [
|
||||
'uploads_disk_driver',
|
||||
'public_disk_driver',
|
||||
|
||||
// dropbox
|
||||
'storage_dropbox_access_token',
|
||||
'storage_dropbox_refresh_token',
|
||||
'storage_dropbox_app_key',
|
||||
'storage_dropbox_app_secret',
|
||||
|
||||
// s3
|
||||
'storage_s3_key',
|
||||
'storage_s3_secret',
|
||||
'storage_s3_region',
|
||||
'storage_s3_bucket',
|
||||
|
||||
// ftp
|
||||
'storage_ftp_host',
|
||||
'storage_ftp_username',
|
||||
'storage_ftp_password',
|
||||
'storage_ftp_root',
|
||||
'storage_ftp_port',
|
||||
'storage_ftp_passive',
|
||||
'storage_ftp_ssl',
|
||||
|
||||
// digital ocean
|
||||
'storage_digitalocean_key',
|
||||
'storage_digitalocean_secret',
|
||||
'storage_digitalocean_region',
|
||||
'storage_digitalocean_bucket',
|
||||
|
||||
// rackspace
|
||||
'storage_rackspace_username',
|
||||
'storage_rackspace_key',
|
||||
'storage_rackspace_region',
|
||||
'storage_rackspace_container',
|
||||
|
||||
// backblaze
|
||||
'storage_backblaze_key',
|
||||
'storage_backblaze_secret',
|
||||
'storage_backblaze_bucket',
|
||||
'storage_backblaze_region',
|
||||
];
|
||||
|
||||
public function fails($settings)
|
||||
{
|
||||
$this->setConfigDynamically($settings);
|
||||
$this->registerAdapters();
|
||||
|
||||
$messages = array_merge(
|
||||
is_null(config('common.site.uploads_disk_driver'))
|
||||
? []
|
||||
: $this->validateDisk('uploads'),
|
||||
$this->validateDisk('public'),
|
||||
);
|
||||
|
||||
return empty($messages) ? false : $messages;
|
||||
}
|
||||
|
||||
private function validateDisk(string $diskName): array
|
||||
{
|
||||
$driverName = Config::get("common.site.{$diskName}_disk_driver");
|
||||
|
||||
try {
|
||||
$disk = Storage::disk($diskName);
|
||||
if ($disk->getAdapter() instanceof DropboxAdapter) {
|
||||
// dropbox adapter catches all errors silently
|
||||
// need to use client directly to check for errors
|
||||
$disk
|
||||
->getAdapter()
|
||||
->getClient()
|
||||
->listFolder();
|
||||
} else {
|
||||
$disk->allFiles();
|
||||
}
|
||||
} catch (S3Exception $e) {
|
||||
return $this->getS3Message($e);
|
||||
} catch (Exception $e) {
|
||||
$message = $e->getMessage();
|
||||
if (
|
||||
Str::contains(
|
||||
$message,
|
||||
'ftp_chdir(): Failed to change directory',
|
||||
)
|
||||
) {
|
||||
$message =
|
||||
'Could not open "uploads" directory. You might need to create it manually via any FTP manager.';
|
||||
}
|
||||
return [
|
||||
'storage_group' => "Invalid $driverName credentials.<br>{$message}",
|
||||
];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
private function getS3Message(S3Exception $e): array
|
||||
{
|
||||
return [
|
||||
'storage_group' => "Could not validate credentials. <br> {$e->getAwsErrorMessage()}",
|
||||
];
|
||||
}
|
||||
|
||||
private function setConfigDynamically($settings): void
|
||||
{
|
||||
$replacements = [
|
||||
's3',
|
||||
'dropbox',
|
||||
'ftp',
|
||||
'digitalocean',
|
||||
'rackspace',
|
||||
'backblaze',
|
||||
];
|
||||
|
||||
foreach ($settings as $key => $value) {
|
||||
if ($key === 'uploads_disk_driver') {
|
||||
Config::set('common.site.uploads_disk_driver', $value ?: null);
|
||||
} elseif ($key === 'public_disk_driver') {
|
||||
Config::set('common.site.public_disk_driver', $value ?: null);
|
||||
} else {
|
||||
// uploads_s3_key => services.s3.key
|
||||
$key = str_replace('storage_', '', $key);
|
||||
$key = preg_replace('/_/', '.', $key, 1);
|
||||
$key = "services.$key";
|
||||
foreach ($replacements as $replacement) {
|
||||
$key = str_replace(
|
||||
"{$replacement}_",
|
||||
"{$replacement}.",
|
||||
$key,
|
||||
);
|
||||
}
|
||||
$key = str_replace('digitalocean.', 'digitalocean_s3.', $key);
|
||||
$key = str_replace('backblaze.', 'backblaze_s3.', $key);
|
||||
Config::set($key, $value ?: null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function registerAdapters(): void
|
||||
{
|
||||
app()->register(DigitalOceanServiceProvider::class);
|
||||
app()->register(DropboxServiceProvider::class);
|
||||
app()->register(BackblazeServiceProvider::class);
|
||||
}
|
||||
}
|
||||
46
common/Settings/Validators/StripeCredentialsValidator.php
Executable file
46
common/Settings/Validators/StripeCredentialsValidator.php
Executable file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators;
|
||||
|
||||
use Common\Billing\Gateways\Stripe\Stripe;
|
||||
use Config;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class StripeCredentialsValidator implements SettingsValidator
|
||||
{
|
||||
const KEYS = ['stripe_key', 'stripe_secret'];
|
||||
|
||||
public function fails($values)
|
||||
{
|
||||
$this->setConfigDynamically($values);
|
||||
|
||||
// create gateway after setting config dynamically
|
||||
// so gateway uses new configuration
|
||||
$gateway = app(Stripe::class);
|
||||
|
||||
try {
|
||||
$gateway->getAllPlans();
|
||||
} catch (ClientException $e) {
|
||||
return $this->getDefaultError();
|
||||
}
|
||||
}
|
||||
|
||||
private function setConfigDynamically($settings)
|
||||
{
|
||||
foreach (self::KEYS as $key) {
|
||||
if (!Arr::has($settings, $key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// stripe_key => key
|
||||
$configKey = str_replace('stripe_', '', $key);
|
||||
Config::set("services.stripe.$configKey", $settings[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
private function getDefaultError(): array
|
||||
{
|
||||
return ['stripe_group' => 'These stripe credentials are not valid.'];
|
||||
}
|
||||
}
|
||||
73
common/Settings/Validators/TwitterLoginValidator.php
Executable file
73
common/Settings/Validators/TwitterLoginValidator.php
Executable file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Settings\Validators;
|
||||
|
||||
use Common\Auth\Oauth;
|
||||
use Common\Core\HttpClient;
|
||||
use Config;
|
||||
use Exception;
|
||||
use Illuminate\Support\Arr;
|
||||
use Socialite;
|
||||
|
||||
class TwitterLoginValidator implements SettingsValidator
|
||||
{
|
||||
const KEYS = ['twitter_id', 'twitter_secret'];
|
||||
|
||||
/**
|
||||
* @var Oauth
|
||||
*/
|
||||
private $oauth;
|
||||
|
||||
/**
|
||||
* @var HttpClient
|
||||
*/
|
||||
private $httpClient;
|
||||
|
||||
public function __construct(Oauth $oauth)
|
||||
{
|
||||
$this->oauth = $oauth;
|
||||
$this->httpClient = new HttpClient([
|
||||
'exceptions' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function fails($values)
|
||||
{
|
||||
$this->setConfigDynamically($values);
|
||||
|
||||
try {
|
||||
Socialite::driver('twitter')->redirect();
|
||||
} catch (Exception $e) {
|
||||
return $this->getErrorMessage($e);
|
||||
}
|
||||
}
|
||||
|
||||
private function setConfigDynamically($settings)
|
||||
{
|
||||
if ($twitterId = Arr::get($settings, 'twitter_id')) {
|
||||
Config::set('services.twitter.client_id', $twitterId);
|
||||
}
|
||||
|
||||
if ($twitterSecret = Arr::get($settings, 'twitter_secret')) {
|
||||
Config::set('services.twitter.client_secret', $twitterSecret);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Exception $e
|
||||
* @return array
|
||||
*/
|
||||
private function getErrorMessage(Exception $e)
|
||||
{
|
||||
if (\Str::contains($e->getMessage(), 'code="415"')) {
|
||||
return ['twitter_group' => 'Site url is not present in "Callback URL" field on your twitter app.'];
|
||||
}
|
||||
|
||||
return $this->getDefaultError();
|
||||
}
|
||||
|
||||
private function getDefaultError()
|
||||
{
|
||||
return ['twitter_group' => 'These twitter credentials are not valid.'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user