|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Determines the power off capabilities of the backend and which actions the |
|
5
|
|
|
* user is allowed to perform to power off the backend depending on the user's |
|
6
|
|
|
* role and the current settings. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Pascal Weisenburger <[email protected]> |
|
9
|
|
|
* @copyright Copyright © Pascal Weisenburger 2014- |
|
10
|
|
|
* @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
class PowerOffManager extends CApplicationComponent |
|
13
|
|
|
{ |
|
14
|
|
|
const SHUTDOWN = 'shutdown'; |
|
15
|
|
|
const SUSPEND = 'suspend'; |
|
16
|
|
|
const HIBERNATE = 'hibernate'; |
|
17
|
|
|
const REBOOT = 'reboot'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array currently allowed actions |
|
21
|
|
|
*/ |
|
22
|
|
|
private $_allowedActions; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Initializes the component. The allowed actions are set here. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function init() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->_allowedActions = array(); |
|
30
|
|
|
|
|
31
|
|
|
$options = array( |
|
32
|
|
|
Setting::POWER_OPTION_SHUTDOWN => self::SHUTDOWN, |
|
33
|
|
|
Setting::POWER_OPTION_SUSPEND => self::SUSPEND, |
|
34
|
|
|
Setting::POWER_OPTION_HIBERNATE => self::HIBERNATE, |
|
35
|
|
|
Setting::POWER_OPTION_REBOOT => self::REBOOT, |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
foreach ($options as $option => $action) |
|
39
|
|
|
{ |
|
40
|
|
|
if (Yii::app()->user->role == User::ROLE_ADMIN || |
|
41
|
|
|
Setting::getBooleanOption('allowUserPowerOff', $option)) |
|
42
|
|
|
$this->_allowedActions[] = $action; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
parent::init(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Returns the power off capabilities of the currently used backend. |
|
50
|
|
|
* @return array the power off capabilities |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getBackendCapabilities() |
|
53
|
|
|
{ |
|
54
|
|
|
$response = Yii::app()->xbmc->performRequest('System.GetProperties', |
|
55
|
|
|
array('properties'=>array( |
|
56
|
|
|
'canshutdown', 'cansuspend', 'canhibernate', 'canreboot'))); |
|
57
|
|
|
|
|
58
|
|
|
$capabilities = array(); |
|
59
|
|
|
if ($response->result->canshutdown) |
|
60
|
|
|
$capabilities[] = self::SHUTDOWN; |
|
61
|
|
|
if ($response->result->cansuspend) |
|
62
|
|
|
$capabilities[] = self::SUSPEND; |
|
63
|
|
|
if ($response->result->canhibernate) |
|
64
|
|
|
$capabilities[] = self::HIBERNATE; |
|
65
|
|
|
if ($response->result->canreboot) |
|
66
|
|
|
$capabilities[] = self::REBOOT; |
|
67
|
|
|
|
|
68
|
|
|
return $capabilities; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns whether the current backend is capable of performing the given |
|
73
|
|
|
* power off action. |
|
74
|
|
|
* @param string $action the power off action to be checked |
|
75
|
|
|
* @return boolean whether the power off action is supported |
|
76
|
|
|
*/ |
|
77
|
|
|
public function hasBackendCapability($action) |
|
78
|
|
|
{ |
|
79
|
|
|
return in_array($action, $this->getBackendCapabilities()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns the power off actions allowed for the current user. |
|
84
|
|
|
* @return array the power off actions |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getAllowedActions() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->_allowedActions; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns whether the current user is allowed to perform the given power |
|
93
|
|
|
* off action. |
|
94
|
|
|
* @param string $action the power off action to be checked |
|
95
|
|
|
* @return boolean whether the power off action is allowed |
|
96
|
|
|
*/ |
|
97
|
|
|
public function isActionAllowed($action) |
|
98
|
|
|
{ |
|
99
|
|
|
return in_array($action, $this->getAllowedActions()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Powers off the backend by shutting it down, suspending it, hibernating it |
|
104
|
|
|
* or rebooting it. |
|
105
|
|
|
* @param string $action the power off action to be performed |
|
106
|
|
|
*/ |
|
107
|
|
|
public function powerOff($action) |
|
108
|
|
|
{ |
|
109
|
|
|
$methods = array( |
|
110
|
|
|
self::SHUTDOWN => 'System.Shutdown', |
|
111
|
|
|
self::SUSPEND => 'System.Suspend', |
|
112
|
|
|
self::HIBERNATE => 'System.Hibernate', |
|
113
|
|
|
self::REBOOT => 'System.Reboot', |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
$method = $methods[$action]; |
|
117
|
|
|
|
|
118
|
|
|
Yii::app()->xbmc->sendNotification($method); |
|
119
|
|
|
} |
|
120
|
|
|
} |