Bearsampp 2026.9.23
Loading...
Searching...
No Matches
class.action.ext.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2026 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
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
52 return;
53 }
54
55 $action = $args[0];
56
57 $newArgs = array();
58 foreach ($args as $key => $arg) {
59 if ($key > 0) {
60 $newArgs[] = $arg;
61 }
62 }
63
64 $method = 'proc' . ucfirst($action);
65 if (!method_exists($this, $method)) {
66 $this->addLog('Unknown arg: ' . $action);
67 $this->addLog('Available args:');
68 foreach ($this->getProcs() as $procName => $procDesc) {
69 $this->addLog('- ' . $procName . ': ' . $procDesc);
70 }
71 $this->setStatus(self::STATUS_ERROR);
72 $this->sendLogs();
73
74 return;
75 }
76
77 call_user_func(array($this, $method), $newArgs);
78 $this->sendLogs();
79 }
80
86 private function getProcs()
87 {
88 return array(
89 self::START,
90 self::STOP,
91 self::RELOAD,
92 self::REFRESH
93 );
94 }
95
101 private function addLog($data)
102 {
103 $this->logs .= $data . "\n";
104 }
105
111 private function setStatus($status)
112 {
113 $this->status = $status;
114 }
115
119 private function sendLogs()
120 {
121 echo json_encode(array(
122 'status' => $this->status,
123 'response' => $this->logs
124 ));
125 }
126
132 private function procStart($args)
133 {
134 global $bearsamppRoot, $bearsamppWinbinder;
135
136 if (!Util::isLaunched()) {
137 $this->addLog('Starting ' . APP_TITLE);
138 $bearsamppWinbinder->exec(Path::getExeFilePath(), null, false);
139 } else {
140 $this->addLog(APP_TITLE . ' already started');
141 $this->setStatus(self::STATUS_WARNING);
142 }
143 }
144
150 private function procStop($args)
151 {
152 global $bearsamppBins;
153
154 if (Util::isLaunched()) {
155 $this->addLog('Remove services');
156 foreach ($bearsamppBins->getServices() as $sName => $service) {
157 if ($service->delete()) {
158 $this->addLog('- ' . $sName . ': OK');
159 } else {
160 $this->addLog('- ' . $sName . ': KO');
161 $this->setStatus(self::STATUS_ERROR);
162 }
163 }
164
165 $this->addLog('Stop ' . APP_TITLE);
166 Batch::exitAppStandalone();
167 } else {
168 $this->addLog(APP_TITLE . ' already stopped');
169 $this->setStatus(self::STATUS_WARNING);
170 }
171 }
172
178 private function procReload($args)
179 {
180 global $bearsamppRoot, $bearsamppBins, $bearsamppWinbinder;
181
182 if (!Util::isLaunched()) {
183 $this->addLog(APP_TITLE . ' is not started.');
184 $bearsamppWinbinder->exec(Path::getExeFilePath(), null, false);
185 $this->addLog('Start ' . APP_TITLE);
186 $this->setStatus(self::STATUS_WARNING);
187
188 return;
189 }
190
191 $this->addLog('Remove services');
192 foreach ($bearsamppBins->getServices() as $sName => $service) {
193 if ($service->delete()) {
194 $this->addLog('- ' . $sName . ': OK');
195 } else {
196 $this->addLog('- ' . $sName . ': KO');
197 $this->setStatus(self::STATUS_ERROR);
198 }
199 }
200
202
203 $this->addLog('Start services');
204 foreach ($bearsamppBins->getServices() as $sName => $service) {
205 $service->create();
206 if ($service->start()) {
207 $this->addLog('- ' . $sName . ': OK');
208 } else {
209 $this->addLog('- ' . $sName . ': KO');
210 $this->setStatus(self::STATUS_ERROR);
211 }
212 }
213 }
214
220 private function procRefresh($args)
221 {
222 global $bearsamppAction;
223
224 if (!Util::isLaunched()) {
225 $this->addLog(APP_TITLE . ' is not started.');
226 $this->setStatus(self::STATUS_ERROR);
227
228 return;
229 }
230
232 }
233}
234
global $bearsamppBins
global $bearsamppRoot
$proc
Definition ajax.php:130
setStatus($status)
const RELOAD
static getExeFilePath($aetrayPath=false)
static killBins($refreshProcs=false)
if(is_file($bearsamppCacert)) $bearsamppAction
Definition root.php:189
const APP_TITLE
Definition root.php:19