Bearsampp 2026.5.5
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
26 public function __construct()
27 {
28 }
29
36 private static function callWin32Ps($function)
37 {
38 $result = false;
39
40 if (function_exists($function)) {
41 $result = @call_user_func($function);
42 }
43
44 return $result;
45 }
46
52 public static function getKeys()
53 {
54 return array(
55 self::NAME,
56 self::PROCESS_ID,
57 self::EXECUTABLE_PATH,
58 self::CAPTION,
59 self::COMMAND_LINE
60 );
61 }
62
68 public static function getCurrentPid()
69 {
70 $procInfo = self::getStatProc();
71 return isset($procInfo[self::PROCESS_ID]) ? intval($procInfo[self::PROCESS_ID]) : 0;
72 }
73
79 public static function getListProcs()
80 {
81 $procs = Win32Native::getProcessList(self::getKeys());
82
83 // Filter out processes without ExecutablePath (same behavior as old VBS version)
84 if ($procs !== false && is_array($procs)) {
85 $filtered = array();
86 foreach ($procs as $proc) {
87 if (!empty($proc[self::EXECUTABLE_PATH])) {
88 $filtered[] = $proc;
89 }
90 }
91 return $filtered;
92 }
93
94 return $procs;
95 }
96
102 public static function getStatProc()
103 {
104 $statProc = self::callWin32Ps('win32_ps_stat_proc');
105
106 if ($statProc !== false) {
107 return array(
108 self::PROCESS_ID => $statProc['pid'],
109 self::EXECUTABLE_PATH => $statProc['exe']
110 );
111 }
112
113 return null;
114 }
115
122 public static function exists($pid)
123 {
124 return self::findByPid($pid) !== false;
125 }
126
133 public static function findByPid($pid)
134 {
135 if (!empty($pid)) {
136 $procs = self::getListProcs();
137 if ($procs !== false) {
138 foreach ($procs as $proc) {
139 if ($proc[self::PROCESS_ID] == $pid) {
140 return $proc;
141 }
142 }
143 }
144 }
145
146 return false;
147 }
148
155 public static function findByPath($path)
156 {
157 $path = UtilPath::formatUnixPath($path);
158 if (!empty($path) && is_file($path)) {
159 $procs = self::getListProcs();
160 if ($procs !== false) {
161 foreach ($procs as $proc) {
162 $unixExePath = UtilPath::formatUnixPath($proc[self::EXECUTABLE_PATH]);
163 if ($unixExePath == $path) {
164 return $proc;
165 }
166 }
167 }
168 }
169
170 return false;
171 }
172
178 public static function kill($pid)
179 {
180 $pid = intval($pid);
181 if (!empty($pid)) {
183 }
184 }
185
192 public static function killBins($refreshProcs = false)
193 {
194 global $bearsamppRoot;
195 $killed = array();
196
197 $procs = $bearsamppRoot->getProcs();
198 if ($refreshProcs || $procs === null) {
199 $procs = self::getListProcs();
200 }
201
202 if ($procs !== false && $procs !== null) {
203 foreach ($procs as $proc) {
204 $unixExePath = UtilPath::formatUnixPath($proc[self::EXECUTABLE_PATH]);
205 $unixCommandPath = UtilPath::formatUnixPath($proc[self::COMMAND_LINE]);
206
207 // Not kill current PID (PHP)
208 if ($proc[self::PROCESS_ID] == self::getCurrentPid()) {
209 continue;
210 }
211
212 // Not kill bearsampp
213 if ($unixExePath == $bearsamppRoot->getExeFilePath()) {
214 continue;
215 }
216
217 // Not kill inside www
218 if (UtilString::startWith($unixExePath, $bearsamppRoot->getWwwPath() . '/') || UtilString::contains($unixCommandPath, $bearsamppRoot->getWwwPath() . '/')) {
219 continue;
220 }
221
222 // Not kill external process
223 if (!UtilString::startWith($unixExePath, $bearsamppRoot->getRootPath() . '/') && !UtilString::contains($unixCommandPath, $bearsamppRoot->getRootPath() . '/')) {
224 continue;
225 }
226
227 self::kill($proc[self::PROCESS_ID]);
228 $killed[] = $proc;
229 }
230 }
231
232 return $killed;
233 }
234}
$result
global $bearsamppRoot
$proc
Definition ajax.php:61
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