Bearsampp 2025.8.29
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 return Vbs::getListProcs(self::getKeys());
82 }
83
89 public static function getStatProc()
90 {
91 $statProc = self::callWin32Ps('win32_ps_stat_proc');
92
93 if ($statProc !== false) {
94 return array(
95 self::PROCESS_ID => $statProc['pid'],
96 self::EXECUTABLE_PATH => $statProc['exe']
97 );
98 }
99
100 return null;
101 }
102
109 public static function exists($pid)
110 {
111 return self::findByPid($pid) !== false;
112 }
113
120 public static function findByPid($pid)
121 {
122 if (!empty($pid)) {
123 $procs = self::getListProcs();
124 if ($procs !== false) {
125 foreach ($procs as $proc) {
126 if ($proc[self::PROCESS_ID] == $pid) {
127 return $proc;
128 }
129 }
130 }
131 }
132
133 return false;
134 }
135
142 public static function findByPath($path)
143 {
144 $path = Util::formatUnixPath($path);
145 if (!empty($path) && is_file($path)) {
146 $procs = self::getListProcs();
147 if ($procs !== false) {
148 foreach ($procs as $proc) {
149 $unixExePath = Util::formatUnixPath($proc[self::EXECUTABLE_PATH]);
150 if ($unixExePath == $path) {
151 return $proc;
152 }
153 }
154 }
155 }
156
157 return false;
158 }
159
165 public static function kill($pid)
166 {
167 $pid = intval($pid);
168 if (!empty($pid)) {
169 Vbs::killProc($pid);
170 }
171 }
172
179 public static function killBins($refreshProcs = false)
180 {
181 global $bearsamppRoot;
182 $killed = array();
183
184 $procs = $bearsamppRoot->getProcs();
185 if ($refreshProcs || $procs === null) {
186 $procs = self::getListProcs();
187 }
188
189 if ($procs !== false && $procs !== null) {
190 foreach ($procs as $proc) {
191 $unixExePath = Util::formatUnixPath($proc[self::EXECUTABLE_PATH]);
192 $unixCommandPath = Util::formatUnixPath($proc[self::COMMAND_LINE]);
193
194 // Not kill current PID (PHP)
195 if ($proc[self::PROCESS_ID] == self::getCurrentPid()) {
196 continue;
197 }
198
199 // Not kill bearsampp
200 if ($unixExePath == $bearsamppRoot->getExeFilePath()) {
201 continue;
202 }
203
204 // Not kill inside www
205 if (Util::startWith($unixExePath, $bearsamppRoot->getWwwPath() . '/') || Util::contains($unixCommandPath, $bearsamppRoot->getWwwPath() . '/')) {
206 continue;
207 }
208
209 // Not kill external process
210 if (!Util::startWith($unixExePath, $bearsamppRoot->getRootPath() . '/') && !Util::contains($unixCommandPath, $bearsamppRoot->getRootPath() . '/')) {
211 continue;
212 }
213
214 self::kill($proc[self::PROCESS_ID]);
215 $killed[] = $proc;
216 }
217 }
218
219 return $killed;
220 }
221}
$result
global $bearsamppRoot
$proc
Definition ajax.php:43
static contains($string, $search)
static startWith($string, $search)
static formatUnixPath($path)
static killProc($pid)
static getListProcs($vbsKeys)
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