2014-11-10 20:34:42 +11:00
|
|
|
<?php namespace Cms\Models;
|
|
|
|
|
|
|
|
use Model;
|
|
|
|
use Cms\Classes\Page;
|
|
|
|
use Cms\Classes\Theme;
|
2021-09-07 09:45:25 +01:00
|
|
|
use Winter\Storm\Support\Arr;
|
|
|
|
use Symfony\Component\HttpFoundation\IpUtils;
|
2016-03-28 15:41:47 +02:00
|
|
|
use ApplicationException;
|
2014-11-10 20:34:42 +11:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maintenance mode settings
|
|
|
|
*
|
2021-03-10 15:02:53 -06:00
|
|
|
* @package winter\wn-cms-module
|
2014-11-10 20:34:42 +11:00
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
2016-05-27 07:49:13 +10:00
|
|
|
class MaintenanceSetting extends Model
|
2014-11-10 20:34:42 +11:00
|
|
|
{
|
2021-03-10 15:02:53 -06:00
|
|
|
use \Winter\Storm\Database\Traits\Validation;
|
2014-11-10 20:34:42 +11:00
|
|
|
|
2017-05-20 20:01:19 +10:00
|
|
|
/**
|
|
|
|
* @var array Behaviors implemented by this model.
|
|
|
|
*/
|
|
|
|
public $implement = [
|
|
|
|
\System\Behaviors\SettingsModel::class
|
|
|
|
];
|
2014-11-10 20:34:42 +11:00
|
|
|
|
2017-05-20 20:01:19 +10:00
|
|
|
/**
|
|
|
|
* @var string Unique code
|
|
|
|
*/
|
2014-11-10 20:34:42 +11:00
|
|
|
public $settingsCode = 'cms_maintenance_settings';
|
|
|
|
|
2017-05-20 20:01:19 +10:00
|
|
|
/**
|
|
|
|
* @var mixed Settings form field defitions
|
|
|
|
*/
|
2014-11-10 20:34:42 +11:00
|
|
|
public $settingsFields = 'fields.yaml';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validation rules
|
|
|
|
*/
|
|
|
|
public $rules = [];
|
|
|
|
|
2017-05-20 20:01:19 +10:00
|
|
|
/**
|
|
|
|
* Initialize the seed data for this model. This only executes when the
|
|
|
|
* model is first created or reset to default.
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-11-10 20:34:42 +11:00
|
|
|
public function initSettingsData()
|
|
|
|
{
|
|
|
|
$this->is_enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCmsPageOptions()
|
|
|
|
{
|
2017-02-08 05:43:40 +11:00
|
|
|
if (!$theme = Theme::getEditTheme()) {
|
2016-03-28 15:41:47 +02:00
|
|
|
throw new ApplicationException('Unable to find the active theme.');
|
2017-02-08 05:43:40 +11:00
|
|
|
}
|
2016-03-28 15:41:47 +02:00
|
|
|
|
|
|
|
return Page::listInTheme($theme)->lists('fileName', 'fileName');
|
2014-11-10 20:34:42 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure each theme has its own CMS page, store it inside a mapping array.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function beforeValidate()
|
|
|
|
{
|
2017-02-08 05:43:40 +11:00
|
|
|
if (!$theme = Theme::getEditTheme()) {
|
2014-11-10 20:34:42 +11:00
|
|
|
throw new ApplicationException('Unable to find the active theme.');
|
2017-02-08 05:43:40 +11:00
|
|
|
}
|
2014-11-10 20:34:42 +11:00
|
|
|
|
|
|
|
$themeMap = $this->getSettingsValue('theme_map', []);
|
|
|
|
$themeMap[$theme->getDirName()] = $this->getSettingsValue('cms_page');
|
|
|
|
$this->setSettingsValue('theme_map', $themeMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore the CMS page found in the mapping array, or disable the
|
|
|
|
* maintenance mode.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function afterFetch()
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
($theme = Theme::getEditTheme())
|
|
|
|
&& ($themeMap = array_get($this->attributes, 'theme_map'))
|
|
|
|
&& ($cmsPage = array_get($themeMap, $theme->getDirName()))
|
|
|
|
) {
|
|
|
|
$this->cms_page = $cmsPage;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->is_enabled = false;
|
|
|
|
}
|
|
|
|
}
|
2021-09-07 09:45:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the provided IP is in the allowed IP list.
|
|
|
|
*
|
|
|
|
* @param string $ip
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isAllowedIp(string $ip): bool
|
|
|
|
{
|
|
|
|
return IpUtils::checkIp($ip, Arr::pluck(static::get('allowed_ips', []), 'ip'));
|
|
|
|
}
|
2014-11-10 20:34:42 +11:00
|
|
|
}
|