This repository has been archived by the owner on Jul 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
MaintenanceMode.php
59 lines (44 loc) · 1.95 KB
/
MaintenanceMode.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
<?php
/**
* Maintenance mode for Yii framework.
* @author Karagodin Evgeniy ([email protected])
* v 1.0
*/
class MaintenanceMode extends CComponent {
public $enabledMode = true;
public $capUrl = 'maintenance/index';
public $message = "Извините, на сайте ведутся технические работы.";
public $users = array('admin',);
public $roles = array('Administrator',);
public $ips = array();//allowed IP
public $urls = array();
public function init() {
if ($this->enabledMode) {
$disable = in_array(Yii::app()->user->name, $this->users);
foreach ($this->roles as $role) {
$disable = $disable || Yii::app()->user->checkAccess($role);
}
$disable = $disable || in_array(Yii::app()->request->getPathInfo(), $this->urls);
$disable = $disable || in_array($this->getIp(), $this->ips);//check "allowed IP"
if (!$disable) {
if ($this->capUrl === 'maintenance/index') {
Yii::app()->controllerMap['maintenance'] = 'application.extensions.MaintenanceMode.MaintenanceController';
}
Yii::app()->catchAllRequest = array($this->capUrl);
}
}
}
//get user IP
protected function getIp()
{
$strRemoteIP = $_SERVER['REMOTE_ADDR'];
if (!$strRemoteIP) { $strRemoteIP = urldecode(getenv('HTTP_CLIENTIP')); }
if (getenv('HTTP_X_FORWARDED_FOR')) { $strIP = getenv('HTTP_X_FORWARDED_FOR'); }
elseif (getenv('HTTP_X_FORWARDED')) { $strIP = getenv('HTTP_X_FORWARDED'); }
elseif (getenv('HTTP_FORWARDED_FOR')) { $strIP = getenv('HTTP_FORWARDED_FOR'); }
elseif (getenv('HTTP_FORWARDED')) { $strIP = getenv('HTTP_FORWARDED'); }
else { $strIP = $_SERVER['REMOTE_ADDR']; }
if ($strRemoteIP != $strIP) { $strIP = $strRemoteIP.", ".$strIP; }
return $strIP;
}
}