-
Notifications
You must be signed in to change notification settings - Fork 3
/
PatrolPlugin.php
218 lines (190 loc) · 5.78 KB
/
PatrolPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
namespace Craft;
/**
* @=Patrol
*
* Patrol simplifies maintenance and SSL for sites built with Craft
*
* @author Selvin Ortiz <selvin@selvin.co>
* @version 1.4.2
* @package Craft
* @category Security
* @since Craft 1.3
* --
* @property string $primaryDomain The primary domain to enforce if not empty or not '*'
* @property bool $forceSsl Whether force SSL mode is on (https)
* @property array $restrictedAreas The list or sections that should be restricted when force SSL mode is on
* @property bool $maintenanceMode Whether maintenance mode is on (offline)
* @property string $maintenanceUrl The URL/template to redirect to when maintenance mode is on
* @property array $authorizedIps The list of IP addresses that bypass maintenance mode
* @property array $limitCpAccessTo The list of ONLY users that can access the CP when not empty
* @property bool $enableCpTab Whether the Control Panel tab for Patrol is display
* @property string $pluginAlias The name that Patrol was renamed to by the user after installation
*/
class PatrolPlugin extends BasePlugin
{
/**
* @var array The raw settings model attributes
*/
protected $settings;
/**
* @var array The settings configured via the general environment config which take priority
*/
protected $configs;
/**
* Ensures that Patrol does not run when accessed from the console
* Merges file configs with plugin settings
*/
public function init()
{
if (! craft()->isConsole())
{
$this->configs = craft()->config->get('patrol');
$this->settings = array_merge($this->getSettings()->getAttributes(), $this->configs ? $this->configs : []);
patrol()->watch($this->settings);
}
}
/**
* Returns the real name of the plugin or the plugin alias given by the user after installation
*
* @param bool $real
*
* @return string
*/
public function getName($real = false)
{
$alias = $this->settings['pluginAlias'];
return ($real || empty($alias)) ? 'Patrol' : $alias;
}
/**
* @return string
*/
public function getVersion()
{
return '1.4.2';
}
/**
* @return string
*/
public function getSchemaVersion()
{
return '1.0.0';
}
/**
* @return string
*/
public function getDeveloper()
{
return 'Selvin Ortiz';
}
/**
* @return string
*/
public function getDeveloperUrl()
{
return 'https://selv.in';
}
/**
* @return string
*/
public function getDocumentationUrl()
{
return 'https://github.com/selvinortiz/craft.patrol';
}
/**
* @return string
*/
public function getReleaseFeedUrl()
{
return 'https://github.com/raw/selvinortiz/craft.patrol/master/releases.json';
}
/**
* @return bool
*/
public function hasCpSection()
{
return (bool) $this->settings['enableCpTab'];
}
/**
* @return array
*/
public function defineSettings()
{
return [
'primaryDomain' => [AttributeType::String],
'forceSsl' => [AttributeType::Bool],
'restrictedAreas' => [AttributeType::Mixed],
'maintenanceMode' => [AttributeType::Bool],
'maintenanceUrl' => [AttributeType::String],
'authorizedIps' => [AttributeType::Mixed],
'limitCpAccessTo' => [AttributeType::Mixed],
'enableCpTab' => [AttributeType::Bool],
'pluginAlias' => [AttributeType::String],
];
}
/**
* @return bool Whether rendering was successful
*/
public function getSettingsHtml()
{
$settings = $this->settings;
craft()->templates->includeCssResource('patrol/css/patrol.css');
if (is_array($settings['authorizedIps']))
{
$settings['authorizedIps'] = implode(PHP_EOL, $settings['authorizedIps']);
}
if (is_array($settings['restrictedAreas']))
{
$settings['restrictedAreas'] = implode(PHP_EOL, $settings['restrictedAreas']);
}
$variables = [
'name' => $this->getName(true),
'alias' => $this->getName(),
'status' => $this->settings['maintenanceMode'] || $this->settings['forceSsl'] ? 'On Duty' : 'Off Duty',
'version' => $this->getVersion(),
'settings' => $settings,
'configs' => $this->configs,
];
return craft()->templates->render('patrol/_settings', $variables);
}
/**
* Prepares plugin settings prior to saving them to the db
* - authorizedIps are converted from string to array
* - restrictedAreas are converted from string to array
*
* @param array $settings
*
* @return array
*/
public function prepSettings($settings = [])
{
$ips = craft()->request->getPost('settings.authorizedIps', $this->settings['authorizedIps']);
$areas = craft()->request->getPost('settings.restrictedAreas', $this->settings['restrictedAreas']);
$settings['authorizedIps'] = patrol()->parseAuthorizedIps($ips);
$settings['restrictedAreas'] = patrol()->parseRestrictedAreas($areas);
return $settings;
}
/**
* @return array
*/
public function registerUserPermissions()
{
return [
'patrolMaintenanceModeBypass' => [
'label' => Craft::t('Access the site when maintenance is on'),
],
];
}
}
/**
* @return PatrolService
*/
function patrol()
{
static $patrol;
if ($patrol === null)
{
$patrol = Craft::app()->getComponent('patrol');
}
return $patrol;
}