Bearsampp 2026.9.23
Loading...
Searching...
No Matches
class.win32ps.php
Go to the documentation of this file.
1<?php
2/*
3 *
4 * * Copyright (c) 2022-2025 Bearsampp
5 * * License: GNU General Public License version 3 or later; see LICENSE.txt
6 * * Website: https://bearsampp.com
7 * * Github: https://github.com/Bearsampp
8 *
9 */
10
19{
20 const NAME = 'Name';
21 const PROCESS_ID = 'ProcessID';
22 const EXECUTABLE_PATH = 'ExecutablePath';
23 const CAPTION = 'Caption';
24 const COMMAND_LINE = 'CommandLine';
25
29 public function __construct()
30 {
31 }
32
40 private static function callWin32Ps($function)
41 {
42 $result = false;
43
44 if (function_exists($function)) {
45 $result = @call_user_func($function);
46 }
47
48 return $result;
49 }
50
56 public static function getKeys()
57 {
58 return array(
59 self::NAME,
60 self::PROCESS_ID,
61 self::EXECUTABLE_PATH,
62 self::CAPTION,
63 self::COMMAND_LINE
64 );
65 }
66
72 public static function getCurrentPid()
73 {
74 $procInfo = self::getStatProc();
75
76 return isset($procInfo[self::PROCESS_ID]) ? intval($procInfo[self::PROCESS_ID]) : 0;
77 }
78
84 public static function getListProcs()
85 {
86 $procs = Win32Native::getProcessList(self::getKeys());
87
88 // Filter out processes without ExecutablePath (same behavior as old VBS version)
89 if ($procs !== false && is_array($procs)) {
90 $filtered = array();
91 foreach ($procs as $proc) {
92 if (!empty($proc[self::EXECUTABLE_PATH])) {
93 $filtered[] = $proc;
94 }
95 }
96
97 return $filtered;
98 }
99
100 return $procs;
101 }
102
108 public static function getStatProc()
109 {
110 $statProc = self::callWin32Ps('win32_ps_stat_proc');
111
112 if ($statProc !== false) {
113 return array(
114 self::PROCESS_ID => $statProc['pid'],
115 self::EXECUTABLE_PATH => $statProc['exe']
116 );
117 }
118
119 return null;
120 }
121
129 public static function exists($pid)
130 {
131 return self::findByPid($pid) !== false;
132 }
133
141 public static function findByPid($pid)
142 {
143 if (!empty($pid)) {
144 $procs = self::getListProcs();
145 if ($procs !== false) {
146 foreach ($procs as $proc) {
147 if ($proc[self::PROCESS_ID] == $pid) {
148 return $proc;
149 }
150 }
151 }
152 }
153
154 return false;
155 }
156
164 public static function findByPath($path)
165 {
166 $path = Path::formatUnixPath($path);
167 if (!empty($path) && is_file($path)) {
168 $procs = self::getListProcs();
169 if ($procs !== false) {
170 foreach ($procs as $proc) {
171 $unixExePath = Path::formatUnixPath($proc[self::EXECUTABLE_PATH]);
172 if ($unixExePath == $path) {
173 return $proc;
174 }
175 }
176 }
177 }
178
179 return false;
180 }
181
187 public static function kill($pid)
188 {
189 $pid = intval($pid);
190 if (!empty($pid)) {
192 }
193 }
194
202 public static function killBins($refreshProcs = false)
203 {
204 global $bearsamppRoot;
205 $killed = array();
206
207 $procs = $bearsamppRoot->getProcs();
208 if ($refreshProcs || $procs === null) {
209 $procs = self::getListProcs();
210 }
211
212 if ($procs !== false && $procs !== null) {
213 foreach ($procs as $proc) {
214 $unixExePath = Path::formatUnixPath($proc[self::EXECUTABLE_PATH]);
215 $unixCommandPath = Path::formatUnixPath($proc[self::COMMAND_LINE]);
216
217 // Not kill current PID (PHP)
218 if ($proc[self::PROCESS_ID] == self::getCurrentPid()) {
219 continue;
220 }
221
222 // Not kill bearsampp
223 if ($unixExePath == Path::getExeFilePath()) {
224 continue;
225 }
226
227 // Not kill inside www
228 if (UtilString::startWith($unixExePath, Path::getWwwPath() . '/') || UtilString::contains($unixCommandPath, Path::getWwwPath() . '/')) {
229 continue;
230 }
231
232 // Not kill external process
233 if (!UtilString::startWith($unixExePath, Path::getRootPath() . '/') && !UtilString::contains($unixCommandPath, Path::getRootPath() . '/')) {
234 continue;
235 }
236
237 self::kill($proc[self::PROCESS_ID]);
238 $killed[] = $proc;
239 }
240 }
241
242 return $killed;
243 }
244}
245
$result
global $bearsamppRoot
$proc
Definition ajax.php:130
static getExeFilePath($aetrayPath=false)
static getRootPath($aetrayPath=false)
static getWwwPath($aetrayPath=false)
static formatUnixPath($path)
static contains($string, $search)
static startWith($string, $search)
static getProcessList($properties=[])
static killProcess($pid)
static findByPid($pid)
static findByPath($path)
static getStatProc()
static getCurrentPid()
static exists($pid)
const COMMAND_LINE
static getListProcs()
static killBins($refreshProcs=false)
static kill($pid)
const CAPTION
static callWin32Ps($function)
const EXECUTABLE_PATH
static getKeys()
const PROCESS_ID