2024.8.23
Loading...
Searching...
No Matches
class.win32ps.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
10/**
11 * Class Win32Ps
12 *
13 * This class provides various utility functions for interacting with Windows processes.
14 * It includes methods for retrieving process information, checking process existence,
15 * finding processes by PID or path, and terminating processes.
16 */
18{
19 const NAME = 'Name';
20 const PROCESS_ID = 'ProcessID';
21 const EXECUTABLE_PATH = 'ExecutablePath';
22 const CAPTION = 'Caption';
23 const COMMAND_LINE = 'CommandLine';
24
25 public function __construct()
26 {
27 }
28
29 /**
30 * Calls a specified function if it exists.
31 *
32 * @param string $function The name of the function to call.
33 * @return mixed The result of the function call, or false if the function does not exist.
34 */
35 private static function callWin32Ps($function)
36 {
37 $result = false;
38
39 if (function_exists($function)) {
40 $result = @call_user_func($function);
41 }
42
43 return $result;
44 }
45
46 /**
47 * Retrieves the keys used for process information.
48 *
49 * @return array An array of keys used for process information.
50 */
51 public static function getKeys()
52 {
53 return array(
54 self::NAME,
55 self::PROCESS_ID,
56 self::EXECUTABLE_PATH,
57 self::CAPTION,
58 self::COMMAND_LINE
59 );
60 }
61
62 /**
63 * Retrieves the current process ID.
64 *
65 * @return int The current process ID, or 0 if not found.
66 */
67 public static function getCurrentPid()
68 {
69 $procInfo = self::getStatProc();
70 return isset($procInfo[self::PROCESS_ID]) ? intval($procInfo[self::PROCESS_ID]) : 0;
71 }
72
73 /**
74 * Retrieves a list of running processes.
75 *
76 * @return array|false An array of process information, or false on failure.
77 */
78 public static function getListProcs()
79 {
80 return Vbs::getListProcs(self::getKeys());
81 }
82
83 /**
84 * Retrieves the status of the current process.
85 *
86 * @return array|null An array containing the process ID and executable path, or null on failure.
87 */
88 public static function getStatProc()
89 {
90 $statProc = self::callWin32Ps('win32_ps_stat_proc');
91
92 if ($statProc !== false) {
93 return array(
94 self::PROCESS_ID => $statProc['pid'],
95 self::EXECUTABLE_PATH => $statProc['exe']
96 );
97 }
98
99 return null;
100 }
101
102 /**
103 * Checks if a process with the specified PID exists.
104 *
105 * @param int $pid The process ID to check.
106 * @return bool True if the process exists, false otherwise.
107 */
108 public static function exists($pid)
109 {
110 return self::findByPid($pid) !== false;
111 }
112
113 /**
114 * Finds a process by its PID.
115 *
116 * @param int $pid The process ID to find.
117 * @return array|false An array of process information, or false if not found.
118 */
119 public static function findByPid($pid)
120 {
121 if (!empty($pid)) {
123 if ($procs !== false) {
124 foreach ($procs as $proc) {
125 if ($proc[self::PROCESS_ID] == $pid) {
126 return $proc;
127 }
128 }
129 }
130 }
131
132 return false;
133 }
134
135 /**
136 * Finds a process by its executable path.
137 *
138 * @param string $path The path to the executable.
139 * @return array|false An array of process information, or false if not found.
140 */
141 public static function findByPath($path)
142 {
143 $path = Util::formatUnixPath($path);
144 if (!empty($path) && is_file($path)) {
146 if ($procs !== false) {
147 foreach ($procs as $proc) {
148 $unixExePath = Util::formatUnixPath($proc[self::EXECUTABLE_PATH]);
149 if ($unixExePath == $path) {
150 return $proc;
151 }
152 }
153 }
154 }
155
156 return false;
157 }
158
159 /**
160 * Terminates a process by its PID.
161 *
162 * @param int $pid The process ID to terminate.
163 */
164 public static function kill($pid)
165 {
166 $pid = intval($pid);
167 if (!empty($pid)) {
168 Vbs::killProc($pid);
169 }
170 }
171
172 /**
173 * Terminates all Bearsampp-related processes except the current one.
174 *
175 * @param bool $refreshProcs Whether to refresh the list of processes before terminating.
176 * @return array An array of terminated processes.
177 */
178 public static function killBins($refreshProcs = false)
179 {
180 global $bearsamppRoot;
181 $killed = array();
182
183 $procs = $bearsamppRoot->getProcs();
184 if ($refreshProcs) {
186 }
187
188 if ($procs !== false) {
189 foreach ($procs as $proc) {
190 $unixExePath = Util::formatUnixPath($proc[self::EXECUTABLE_PATH]);
191 $unixCommandPath = Util::formatUnixPath($proc[self::COMMAND_LINE]);
192
193 // Not kill current PID (PHP)
194 if ($proc[self::PROCESS_ID] == self::getCurrentPid()) {
195 continue;
196 }
197
198 // Not kill bearsampp
199 if ($unixExePath == $bearsamppRoot->getExeFilePath()) {
200 continue;
201 }
202
203 // Not kill inside www
204 if (Util::startWith($unixExePath, $bearsamppRoot->getWwwPath() . '/') || Util::contains($unixCommandPath, $bearsamppRoot->getWwwPath() . '/')) {
205 continue;
206 }
207
208 // Not kill external process
209 if (!Util::startWith($unixExePath, $bearsamppRoot->getRootPath() . '/') && !Util::contains($unixCommandPath, $bearsamppRoot->getRootPath() . '/')) {
210 continue;
211 }
212
213 self::kill($proc[self::PROCESS_ID]);
214 $killed[] = $proc;
215 }
216 }
217
218 return $killed;
219 }
220}
$result
global $bearsamppRoot
$proc
Definition ajax.php:43
$procs
Definition ajax.php:19
static startWith($string, $search)
static contains($string, $search)
static formatUnixPath($path)
static killProc($pid)
static getListProcs($vbsKeys)
static findByPid($pid)
const CAPTION
const COMMAND_LINE
const PROCESS_ID
static getCurrentPid()
static getStatProc()
static getKeys()
static callWin32Ps($function)
static kill($pid)
static exists($pid)
static killBins($refreshProcs=false)
static findByPath($path)
const EXECUTABLE_PATH
static getListProcs()