Bearsampp
2026.7.11
Toggle main menu visibility
Loading...
Searching...
No Matches
class.action.ext.php
Go to the documentation of this file.
1
<?php
2
/*
3
* Copyright (c) 2021-2024 Bearsampp
4
* License: GNU General Public License version 3 or later; see LICENSE.txt
5
* Author: Bear
6
* Website: https://bearsampp.com
7
* Github: https://github.com/Bearsampp
8
*/
9
13
class
ActionExt
14
{
15
// Constants for different actions
16
const
START
=
'start'
;
17
const
STOP
=
'stop'
;
18
const
RELOAD
=
'reload'
;
19
const
REFRESH
=
'refresh'
;
20
21
// Constants for status codes
22
const
STATUS_ERROR
= 2;
23
const
STATUS_WARNING
= 1;
24
const
STATUS_SUCCESS
= 0;
25
29
private
$status
= self::STATUS_SUCCESS;
30
34
private
$logs
=
''
;
35
41
public
function
__construct
($args)
42
{
43
if
(!isset($args[0]) || empty($args[0])) {
44
$this->
addLog
(
'No args defined'
);
45
$this->
addLog
(
'Available args:'
);
46
foreach
($this->
getProcs
() as
$proc
) {
47
$this->
addLog
(
'- '
. $proc);
48
}
49
$this->
setStatus
(self::STATUS_ERROR);
50
$this->
sendLogs
();
51
return
;
52
}
53
54
$action = $args[0];
55
56
$newArgs = array();
57
foreach
($args as $key => $arg) {
58
if
($key > 0) {
59
$newArgs[] = $arg;
60
}
61
}
62
63
$method =
'proc'
. ucfirst($action);
64
if
(!method_exists($this, $method)) {
65
$this->
addLog
(
'Unknown arg: '
. $action);
66
$this->
addLog
(
'Available args:'
);
67
foreach
($this->
getProcs
() as $procName => $procDesc) {
68
$this->
addLog
(
'- '
. $procName .
': '
. $procDesc);
69
}
70
$this->
setStatus
(self::STATUS_ERROR);
71
$this->
sendLogs
();
72
return
;
73
}
74
75
call_user_func(array($this, $method), $newArgs);
76
$this->
sendLogs
();
77
}
78
84
private
function
getProcs
()
85
{
86
return
array(
87
self::START,
88
self::STOP,
89
self::RELOAD,
90
self::REFRESH
91
);
92
}
93
99
private
function
addLog
($data)
100
{
101
$this->logs .= $data .
"\n"
;
102
}
103
109
private
function
setStatus
(
$status
)
110
{
111
$this->status =
$status
;
112
}
113
117
private
function
sendLogs
()
118
{
119
echo json_encode(array(
120
'status'
=> $this->status,
121
'response'
=> $this->logs
122
));
123
}
124
130
private
function
procStart
($args)
131
{
132
global
$bearsamppRoot
, $bearsamppWinbinder;
133
134
if
(!Util::isLaunched()) {
135
$this->
addLog
(
'Starting '
.
APP_TITLE
);
136
$bearsamppWinbinder->exec(
Path::getExeFilePath
(),
null
,
false
);
137
}
else
{
138
$this->
addLog
(
APP_TITLE
.
' already started'
);
139
$this->
setStatus
(self::STATUS_WARNING);
140
}
141
}
142
148
private
function
procStop
($args)
149
{
150
global
$bearsamppBins
;
151
152
if
(Util::isLaunched()) {
153
$this->
addLog
(
'Remove services'
);
154
foreach
($bearsamppBins->getServices() as $sName => $service) {
155
if
($service->delete()) {
156
$this->
addLog
(
'- '
. $sName .
': OK'
);
157
}
else
{
158
$this->
addLog
(
'- '
. $sName .
': KO'
);
159
$this->
setStatus
(self::STATUS_ERROR);
160
}
161
}
162
163
$this->
addLog
(
'Stop '
.
APP_TITLE
);
164
Batch::exitAppStandalone();
165
}
else
{
166
$this->
addLog
(
APP_TITLE
.
' already stopped'
);
167
$this->
setStatus
(self::STATUS_WARNING);
168
}
169
}
170
176
private
function
procReload
($args)
177
{
178
global
$bearsamppRoot
,
$bearsamppBins
, $bearsamppWinbinder;
179
180
if
(!Util::isLaunched()) {
181
$this->
addLog
(
APP_TITLE
.
' is not started.'
);
182
$bearsamppWinbinder->exec(
Path::getExeFilePath
(),
null
,
false
);
183
$this->
addLog
(
'Start '
.
APP_TITLE
);
184
$this->
setStatus
(self::STATUS_WARNING);
185
return
;
186
}
187
188
$this->
addLog
(
'Remove services'
);
189
foreach
($bearsamppBins->getServices() as $sName => $service) {
190
if
($service->delete()) {
191
$this->
addLog
(
'- '
. $sName .
': OK'
);
192
}
else
{
193
$this->
addLog
(
'- '
. $sName .
': KO'
);
194
$this->
setStatus
(self::STATUS_ERROR);
195
}
196
}
197
198
Win32Ps::killBins
();
199
200
$this->
addLog
(
'Start services'
);
201
foreach
($bearsamppBins->getServices() as $sName => $service) {
202
$service->create();
203
if
($service->start()) {
204
$this->
addLog
(
'- '
. $sName .
': OK'
);
205
}
else
{
206
$this->
addLog
(
'- '
. $sName .
': KO'
);
207
$this->
setStatus
(self::STATUS_ERROR);
208
}
209
}
210
}
211
217
private
function
procRefresh
($args)
218
{
219
global
$bearsamppAction
;
220
221
if
(!Util::isLaunched()) {
222
$this->
addLog
(
APP_TITLE
.
' is not started.'
);
223
$this->
setStatus
(self::STATUS_ERROR);
224
return
;
225
}
226
227
$bearsamppAction
->call(
Action::RELOAD
);
228
}
229
}
230
$bearsamppBins
global $bearsamppBins
Definition
ajax.apache.php:16
$bearsamppRoot
global $bearsamppRoot
Definition
ajax.apache.php:16
$proc
$proc
Definition
ajax.php:45
ActionExt
Definition
class.action.ext.php:14
ActionExt\procRefresh
procRefresh($args)
Definition
class.action.ext.php:217
ActionExt\STATUS_ERROR
const STATUS_ERROR
Definition
class.action.ext.php:22
ActionExt\$logs
$logs
Definition
class.action.ext.php:34
ActionExt\REFRESH
const REFRESH
Definition
class.action.ext.php:19
ActionExt\STOP
const STOP
Definition
class.action.ext.php:17
ActionExt\__construct
__construct($args)
Definition
class.action.ext.php:41
ActionExt\$status
$status
Definition
class.action.ext.php:29
ActionExt\addLog
addLog($data)
Definition
class.action.ext.php:99
ActionExt\STATUS_WARNING
const STATUS_WARNING
Definition
class.action.ext.php:23
ActionExt\RELOAD
const RELOAD
Definition
class.action.ext.php:18
ActionExt\setStatus
setStatus($status)
Definition
class.action.ext.php:109
ActionExt\START
const START
Definition
class.action.ext.php:16
ActionExt\getProcs
getProcs()
Definition
class.action.ext.php:84
ActionExt\sendLogs
sendLogs()
Definition
class.action.ext.php:117
ActionExt\STATUS_SUCCESS
const STATUS_SUCCESS
Definition
class.action.ext.php:24
ActionExt\procStop
procStop($args)
Definition
class.action.ext.php:148
ActionExt\procReload
procReload($args)
Definition
class.action.ext.php:176
ActionExt\procStart
procStart($args)
Definition
class.action.ext.php:130
Action\RELOAD
const RELOAD
Definition
class.action.php:43
Path\getExeFilePath
static getExeFilePath($aetrayPath=false)
Definition
class.path.php:450
Win32Ps\killBins
static killBins($refreshProcs=false)
Definition
class.win32ps.php:192
$bearsamppAction
$bearsamppAction
Definition
root.php:149
APP_TITLE
const APP_TITLE
Definition
root.php:13
sandbox
core
classes
actions
class.action.ext.php
Generated by
1.17.0