function reboot() { $obj = new COM("winmgmts:{(Shutdown)}//./root/cimv2"); $list = $obj->ExecQuery("select * from Win32_OperatingSystem where Primary=true"); foreach ($list as $l) { $l->Reboot(); } }
function _loadavg() { if (class_exists("COM")) { $wmi = new COM("WinMgmts:{impersonationLevel=impersonate}"); $cpus = $wmi->ExecQuery("SELECT LoadPercentage FROM Win32_Processor"); foreach ($cpus as $cpu) { return $cpu->LoadPercentage; } } }
protected function getWindowsTimezone() { $wmiObject = new COM("WinMgmts:\\\\.\\root\\cimv2"); $wmiObj = $wmiObject->ExecQuery("SELECT Bias FROM Win32_TimeZone"); foreach ($wmiObj as $objItem) { $offset = $objItem->Bias; break; } return array_key_exists($offset / 60, $this->timezones) ? $this->timezones[$offset] : "UTC"; }
function windows_create_kloxo_service() { $OWN_PROCESS = 16; $NOT_INTERACTIVE = False; $NORMAL_ERROR_CONTROL = 2; $objWMIService = new COM("winmgmts:{impersonationLevel=impersonate}//./root/cimv2"); $objService = $objWMIService->Get("Win32_BaseService"); $user = new COM("WinNT://./lxlabs"); $user->setPassword("lxlabspass"); $user->setInfo(); try { //$objService->Create("LxaServer" ,"LxaServer" ,'"C:/Program Files/lxlabs/ext/lxhttpd/Apache2/bin/Apache.exe" -k runservice -f "c:/Program Files/lxlabs/ext/lxhttpd/Apache2/conf/httpd.conf"', $OWN_PROCESS, $NORMAL_ERROR_CONTROL, "Automatic", $NOT_INTERACTIVE, ".\lxlabs", "lxlabspass"); $objService->Create("LxaMultiplexer", "LxaMultiplexer", '"C:/Program Files/lxlabs/ext/Multiplexer/MultiplexerSrvc.exe"', $OWN_PROCESS, $NORMAL_ERROR_CONTROL, "Automatic", $NOT_INTERACTIVE, ".\\LocalSystem", ""); } catch (Exception $e) { print " ERROR: {$e}"; } $colListOfServices = $objWMIService->ExecQuery("Select * from Win32_Service Where Name = 'LxaMultiplexer' or Name = 'LxaServer'"); foreach ($colListOfServices as $objService) { $objService->StartService(); print "\n Done \n"; } }
static function getCurrentIps() { $ipconf = new COM("winmgmts://./root/cimv2"); $list = $ipconf->ExecQuery("select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"); foreach ($list as $l) { if ($l->IPAddress) { //for($i = 0; $i< count($l->IPAddress); $i++) { foreach ($l->IPAddress as $ip) { $res['ipaddr'] = $ip; $res['devname'] = "Ethernet-" . $l->Index; foreach ($l->IPSubnet as $s) { $sub[] = "{$s}"; } foreach ($l->DefaultIPGateway as $d) { $dg[] = "{$d}"; } $res['netmask'] = implode(",", $sub); $res['gateway'] = implode(",", $dg); $result[] = $res; } } } return $result; }
// Echo "Hour: " . $objItem->Hour . "\n"; // Echo "Milliseconds: " . $objItem->Milliseconds . "\n"; // Echo "Minute: " . $objItem->Minute . "\n"; // Echo "Month: " . $objItem->Month . "\n"; // Echo "Quarter: " . $objItem->Quarter . "\n"; // Echo "Second: " . $objItem->Second . "\n"; // Echo "WeekInMonth: " . $objItem->WeekInMonth . "\n"; // Echo "Year: " . $objItem->Year . "\n"; // }; // ---- Query Event Logs // Need a stored placeholder so we can pick up where we left off: // ("Win32_NTLogEvent where logfile='security' and RecordNumer >'12345'") // This would be best after a reboot or the exe exiting and running again print "Category, CategoryString, ComputerName, EventCode,\r\n EventIdentifier, EventType, Logfile, Message, RecordNumber,\r\n SourceName, TimeGenerated, TimeWritten, Type, User, InsertionStrings" . "\n"; $objects_array = array("Category" => "string", "CategoryString" => "string", "ComputerName" => "string", "EventCode" => "string", "EventIdentifier" => "string", "EventType" => "string", "Logfile" => "string", "Message" => "string", "RecordNumber" => "string", "SourceName" => "string", "TimeGenerated" => "time", "TimeWritten" => "time", "Type" => "string", "User" => "string", "InsertionStrings" => "array"); $colItems = $objWMIService->ExecQuery("SELECT * FROM Win32_NTLogEvent"); //This query can also be used foreach ($objWMIService->instancesof("Win32_NTLogEvent") as $objItem) { $line = ""; foreach ($objects_array as $disp_obj => $disp_type) { if ($disp_type == "string") { $line .= "\"" . trim($objItem->{$disp_obj}) . "\","; } else { if ($disp_type == "time") { $line .= "\"" . win_time($objItem->{$disp_obj}) . "\","; } else { if ($disp_type == "array") { $line .= "\""; foreach ($objItem->{$disp_obj} as $string) { $line .= $string . ","; }
function is_pid_alive($pid) { if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { if (!class_exists("COM")) { return true; } $wmi = new COM("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2"); $procs = $wmi->ExecQuery("SELECT * FROM Win32_Process WHERE ProcessId='" . $pid . "'"); return $procs && $procs->Count !== 0; } else { return posix_getsid($pid) !== FALSE; } }
function getZendidLocal() { $obj = new COM('winmgmts://localhost/root/CIMV2'); $fso = new COM("Scripting.FileSystemObject"); $wmi_computersystem = $obj->ExecQuery("Select * from Win32_ComputerSystem"); $wmi_bios = $obj->ExecQuery("Select * from Win32_BIOS"); $processor = $obj->ExecQuery("Select * from Win32_Processor"); $PhysicalMemory = $obj->ExecQuery("Select * from Win32_PhysicalMemory"); $BaseBoard = $obj->ExecQuery("Select * from Win32_BaseBoard"); $LogicalDisk = $obj->ExecQuery("Select * from Win32_LogicalDisk"); foreach ($wmi_computersystem as $wmi_call) { $model = $wmi_call->Model; } foreach ($wmi_bios as $wmi_call) { $serial = $wmi_call->SerialNumber; $bios_version = $wmi_call->SMBIOSBIOSVersion; } foreach ($processor as $wmi_processor) { $idprocessor = $wmi_processor->ProcessorId; $Architecture = $wmi_processor->Architecture; $Name = $wmi_processor->Name; $Version = $wmi_processor->Version; } foreach ($PhysicalMemory as $wmi_PhysicalMemory) { $Capacity = $wmi_PhysicalMemory->Capacity; $PartNumber = $wmi_PhysicalMemory->PartNumber; $Name = $wmi_PhysicalMemory->Name; } foreach ($BaseBoard as $wmi_BaseBoard) { $SerialNumber = $wmi_BaseBoard->SerialNumber; } foreach ($LogicalDisk as $wmi_LogicalDisk) { $SerialNumberDisk = $wmi_LogicalDisk->VolumeSerialNumber; $FileSystem = $wmi_LogicalDisk->FileSystem; } return $serial; }
public static function GetOsArchitecture() { $wmi = new \COM('winmgmts:{impersonationLevel=impersonate}//./root/cimv2'); $result_set = $wmi->ExecQuery('SELECT OSArchitecture FROM Win32_OperatingSystem'); if (!$result_set->Count()) { return false; } $result = $result_set->ItemIndex(0); $size = preg_replace('/[^\\d]+/', '', $result->OSArchitecture); return (int) $size; }
<?php include "lib/include.php"; $objWMIService = new COM("winmgmts:{impersonationLevel=impersonate}//./root/cimv2"); $objService = $objWMIService->Get("Win32_BaseService"); $colListOfServices = $objWMIService->ExecQuery("Select * from Win32_Service Where Name = 'LxaMultiplexer' or Name = 'LxaServer'"); foreach ($colListOfServices as $objService) { if ($argv[1] == 'stop') { $objService->StopService(); } if ($argv[1] == 'start') { $objService->StartService(); } if ($argv[1] == 'restart') { $objService->StopService(); sleep(1); $objService->StartService(); } print "\n Done \n"; }
if (sizeof($info) >= 5 && $info[4] != "") { if (strlen($info[4]) > $maxlen) { $maxlen = strlen($info[4]); } } elseif (sizeof($info) >= 2 && $info[0] != "\n") { if (strlen($info[1]) > $maxlen) { $maxlen = strlen($info[1]); } } } $maxlen = $maxlen + 1; if ($maxlen < 12) { $maxlen = 12; } $wmi = new COM('winmgmts://./root/OpenHardwareMonitor'); $result = $wmi->ExecQuery("SELECT * FROM Sensor"); foreach ($infos as $info) { if ($info[0] == "\n") { if (sizeof($info) >= 2) { echo "<u>{$info['1']}</u><br />"; } else { echo "<br />"; } } foreach ($result as $obj) { if ($obj->SensorType == $info[0] && $obj->Name == $info[1]) { $round = 1; if (sizeof($info) >= 4) { $round = $info[3]; } $cols = 2;
<?php header('Content-Type: text/plain'); $strComputer = "."; $get_sid = new Variant("", VT_BSTR); $strUser = new Variant("", VT_BSTR); $strDomain = new Variant("", VT_BSTR); $objWMIService = new COM('winmgmts:{impersonationLevel=impersonate}!//' . $strComputer . '/root/cimv2'); $colProcesses = $objWMIService->ExecQuery("SELECT * FROM Win32_Process"); foreach ($colProcesses as $objProcess) { $re_turn = $objProcess->GetOwner($strUser, $strDomain); if ($re_turn != 0) { echo "Could not get owner info for process " . $objProcess->Name . "\n" . "Error = " . $re_turn . "\r\n"; } else { echo "Process " . $objProcess->Name . " is owned by " . $strDomain . "\\" . $strUser . "." . "\r\n"; } } ?>
function changeParameter() { $objWMIService = new COM("winmgmts:{impersonationLevel=impersonate}!//./root/MicrosoftDNS"); $zoneName = $this->main->nname; $colItems = $objWMIService->ExecQuery("Select * from MicrosoftDNS_SOAType Where DomainName='{$zoneName}' and ContainerName='{$zoneName}'"); foreach ($colItems as $objItem) { /* print("$objItem->ContainerName"."\n"); print("$objItem->DnsserverName"."\n"); print("$objItem->RefreshInterval"."\n"); print("$objItem->MinimumTTL"."\n"); */ $objItem->MinimumTTL = $this->main->ttl; $objItem->Put_(); } }
<?php } else { if ($form_data['action'] == "search") { $output = "\r\n <h1>Results:</h1>\r\n <hr />"; $query = "Select * from Win32_NTLogEvent Where"; if (!isset($form_data['fqdn'])) { $form_data['fqdn'] = "."; } if ($form_data['logfile'] != "all") { $query .= " LogFile = '" . $form_data['logfile'] . "' and"; } $query .= " TimeWritten >= '" . date('YmdHis.000000-000', strtotime($form_data['timeframe'])) . "'"; $objWMIService = new COM("winmgmts:{impersonationLevel=impersonate,(Security)}//" . $form_data['fqdn'] . "\\root\\cimv2"); $LoggedEvents = $objWMIService->ExecQuery($query); foreach ($LoggedEvents as $objEvent) { $output .= "Category: " . $objEvent->Category . "<br />"; $output .= "Computer Name: " . $objEvent->ComputerName . "<br />"; $output .= "Event Code: " . $objEvent->EventCode . "<br />"; $output .= "Log File: " . $objEvent->LogFile . "<br />"; $output .= "Message: " . $objEvent->Message . "<br />"; $output .= "Record Number: " . $objEvent->RecordNumber . "<br />"; $output .= "Source Name: " . $objEvent->SourceName . "<br />"; $output .= "Time Written: " . $objEvent->TimeWritten . "<br />"; $output .= "Event Type: " . $objEvent->Type . "<br />"; $output .= "User: "******"<br />"; $output .= "<hr />"; } print $query . "<br />"; print $output;
/** * Windows' way of dealing with PIDs */ private function _verifyPIDWindows() { $wmi = new \COM('winmgmts://'); $processes = $wmi->ExecQuery('SELECT ProcessId FROM Win32_Process WHERE ProcessId = \'' . (int) $this->pid . '\''); if (count($processes) > 0) { $i = 0; foreach ($processes as $a) { $i++; } if ($i > 0) { $this->alreadyRunning = true; } } return $this->alreadyRunning; }
} else { if ($shsh == True) { echo "<td>" . round(shsh("grep 'cpu ' /proc/stat | awk '{usage=(\$2+\$4)*100/(\$2+\$4+\$5)} END {print usage \"\"}'")) . "%</td>\n"; } else { echo "<td></td>\n"; } } } } } } } echo "\n\t</tr>\n\n\t<tr>\n\t\t<td>Total RAM</td>"; if (isset($_SESSION["Windows"])) { $wmi = new COM('WinMgmts:root/cimv2'); $res = $wmi->ExecQuery('Select TotalPhysicalMemory from Win32_ComputerSystem'); $system = $res->ItemIndex(0); printf('<td>%d GB</td>', $system->TotalPhysicalMemory / 1024 / 1024 / 1024); } else { if ($shell_exec == True) { $total_ram = shell_exec("free -mt | grep Mem |awk '{print \$2}'"); $total_ram = $total_ram / 1024; echo "<td>" . round($total_ram) . " GB</td>\n"; } else { if ($exec == True) { $total_ram = exec("free -mt | grep Mem |awk '{print \$2}'"); $total_ram = $total_ram / 1024; echo "<td>" . round($total_ram) . " GB</td>\n"; } else { if ($popen == True) { $pid = popen("free -mt | grep Mem |awk '{print \$2}'", "r");
$avarice_webservice_URL = "http://domain.com/webservice"; // --- Sets up the $objects array $objects_array = array("Category" => "string", "CategoryString" => "string", "ComputerName" => "string", "EventCode" => "string", "EventIdentifier" => "string", "EventType" => "string", "Logfile" => "string", "Message" => "string", "RecordNumber" => "string", "SourceName" => "string", "TimeGenerated" => "time", "TimeWritten" => "time", "Type" => "string", "User" => "string", "InsertionStrings" => "array"); // ---- Instantiates returndata array $return_data = array(); // ---- Win_time turns some windows time stamps into more common formats function win_time($timestr) { return substr($timestr, 4, 2) . "/" . substr($timestr, 6, 2) . "/" . substr($timestr, 0, 4) . " " . substr($timestr, 8, 2) . ":" . substr($timestr, 10, 2) . ":" . substr($timestr, 12, 2) . " " . substr($timestr, -4); } // ---- Connection Settings $strComputer = "."; $objWMIService = new COM("winmgmts:{impersonationLevel=impersonate,(Security)}//" . $strComputer . "\\root\\cimv2"); // ---- Enumerate Local Users $return_data['local users'] = array(); $colItems = $objWMIService->ExecQuery("Select * from Win32_UserAccount Where LocalAccount = True"); foreach ($colItems as $objItem) { $return_data['local users'][] = array("AccountType" => $objItem->AccountType, "Caption" => $objItem->Caption, "Description" => $objItem->Description, "Disabled" => $objItem->Disabled, "Domain" => $objItem->Domain, "FullName" => $objItem->FullName, "LocalAccount" => $objItem->LocalAccount, "Lockout" => $objItem->Lockout, "Name" => $objItem->Name, "PasswordChangeable" => $objItem->PasswordChangeable, "PasswordExpires" => $objItem->PasswordExpires, "PasswordRequired" => $objItem->PasswordRequired, "SID" => $objItem->SID, "SIDType" => $objItem->SIDType, "Status" => $objItem->Status); } // ---- Enumerate Local Groups $return_data['local groups'] = array(); $colItems = $objWMIService->ExecQuery("SELECT * FROM Win32_Group Where LocalAccount = True"); foreach ($colItems as $objItem) { $return_data['local groups'][] = array("Caption" => $objItem->Caption, "Description" => $objItem->Description, "Domain" => $objItem->Domain, "LocalAccount" => $objItem->LocalAccount, "Name" => $objItem->Name, "SID" => $objItem->SID, "SIDType" => $objItem->SIDType, "Status" => $objItem->Status); } // ---- Enumerate Local Group Membership $return_data['group membership'] = array(); $colItems = $objWMIService->ExecQuery("SELECT * FROM Win32_GroupUser"); foreach ($colItems as $objItem) { $return_data['group membership'][] = array("GroupComponent" => $objItem->GroupComponent, "PartComponent" => $objItem->PartComponent); }
$query = "\r\n\t\t\t\t\t\tselect\r\n\t\t\t\t\t\t\tcount(*) as 'Count'\r\n\t\t\t\t\t\tfrom eventLogSourceFile\r\n\t\t\t\t\t\twhere\r\n\t\t\t\t\t\t\teventLogID = " . $eventLogID . "\r\n\t\t\t\t\t\t\tand sourceID = " . $sourceID . "\r\n\t\t\t\t\t\t\tand messageFileID = " . $messageFileID . ";"; $result = $dbh->query($query)->fetch(); if ($result['Count'] < 1) { $insertQuery = "\r\n\t\t\t\t\t\t\tinsert into eventLogSourceFile\r\n\t\t\t\t\t\t\t\t(eventLogID, sourceID, messageFileID)\r\n\t\t\t\t\t\t\tvalues\r\n\t\t\t\t\t\t\t\t(" . $eventLogID . ", " . $sourceID . ", " . $messageFileID . ");"; $dbh->exec($insertQuery); $result = $dbh->query($query)->fetch(); } } } } } } // Make WMI connection $objWMIService = new COM("winmgmts:{impersonationLevel=impersonate,authenticationLevel=pktPrivacy,(Security)}!//.\\root\\cimv2"); $total = 0; $emptyvariant = $objWMIService->ExecQuery("Select * from Win32_NTLogEvent WHERE RecordNumber = 'string'", 'WQL', 48); foreach ($logfiles_array as $logfileID => $logfilename) { $x = 0; $query = "\r\n\t\tSELECT\r\n\t\t\t*\r\n\t\tFROM eventLog\r\n\t\tWHERE\r\n\t\t\teventLogID = '" . $logfileID . "';"; try { $result = $dbh->query($query)->fetch(); } catch (PDOException $e) { print $e->getMessage(); } if (empty($result['lastEventID'])) { $result['lastEventID'] = 0; } $colItems = $objWMIService->ExecQuery("Select * from Win32_NTLogEvent WHERE LogFile = '" . $logfilename . "' AND RecordNumber > " . $result['lastEventID'], 'WQL', 48); if ($colItems != $emptyvariant) { $query = "BEGIN TRANSACTION; "; $norm_query = "BEGIN TRANSACTION; ";
$wmi_information[(string) $property['root']] = array(); } if (!in_array((string) $property['namespace'], array_keys($wmi_information[(string) $property['root']]))) { $wmi_information[(string) $property['root']][(string) $property['namespace']] = array("props" => array()); } $wmi_information[(string) $property['root']][(string) $property['namespace']]['props'][] = (string) $property['property']; } } } } } foreach ($wmi_information as $root => $namespaces) { $obj = new COM($root); foreach ($namespaces as $namespace => $value) { $query = "SELECT * FROM " . $namespace; $result = $obj->ExecQuery($query); $wmi_information[$root][$namespace]['result'] = array(); foreach ($result as $resvalue) { $resarray = array(); foreach ($value['props'] as $prop) { $resarray[$prop] = (string) $resvalue->{$prop}; } $wmi_information[$root][$namespace]['result'][] = $resarray; } } } $self['ScanEnded'] = date('Y-m-d H:i:s', microtime(true)); foreach ($template_xml->asset->property as $property) { if ($property['method'] == "wmi") { $return_xml_array['asset'][(string) $property['name']] = $wmi_information[(string) $property['root']][(string) $property['namespace']]['result'][0][(string) $property['property']]; } else {
function os_restart_program() { return; $objWMIService = new COM("winmgmts:{impersonationLevel=impersonate}//./root/cimv2"); $objService = $objWMIService->Get("Win32_BaseService"); $colListOfServices = $objWMIService->ExecQuery("Select * from Win32_Service Where Name = 'LxaMultiplexer' or Name = 'LxaServer'"); foreach ($colListOfServices as $objService) { $objService->stopService(); sleep(1); $objService->startService(); print "\n Done \n"; } }
function _CPULoad() { /* cpu 524152 2662 2515228 336057010 cpu0 264339 1408 1257951 168025827 cpu1 259813 1254 1257277 168031181 page 622307 25475680 swap 24 1891 intr 890153570 868093576 6 0 4 4 0 6 1 2 0 0 0 124 0 8098760 2 13961053 0 0 0 0 0 0 0 0 0 0 0 0 0 16 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 disk_io: (3,0):(3144904,54369,610378,3090535,50936192) (3,1):(3630212,54097,633016,3576115,50951320) ctxt 66155838 btime 1062315585 processes 69293 */ // Algorithm is taken from // http://social.technet.microsoft.com/Forums/en-US/winservergen/thread/414b0e1b-499c-411e-8a02-6a12e339c0f1/ if (strncmp(PHP_OS, 'WIN', 3) == 0) { if (PHP_VERSION == '5.0.0') { return false; } if (PHP_VERSION == '5.0.1') { return false; } if (PHP_VERSION == '5.0.2') { return false; } if (PHP_VERSION == '5.0.3') { return false; } if (PHP_VERSION == '4.3.10') { return false; } # see http://bugs.php.net/bug.php?id=31737 static $FAIL = false; if ($FAIL) { return false; } $objName = "winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\CIMV2"; $myQuery = "SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name = '_Total'"; try { @($objWMIService = new COM($objName)); if (!$objWMIService) { $FAIL = true; return false; } $info[0] = -1; $info[1] = 0; $info[2] = 0; $info[3] = 0; foreach ($objWMIService->ExecQuery($myQuery) as $objItem) { $info[0] = $objItem->PercentProcessorTime(); } } catch (Exception $e) { $FAIL = true; echo $e->getMessage(); return false; } return $info; } // Algorithm - Steve Blinch (BlitzAffe Online, http://www.blitzaffe.com) $statfile = '/proc/stat'; if (!file_exists($statfile)) { return false; } $fd = fopen($statfile, "r"); if (!$fd) { return false; } $statinfo = explode("\n", fgets($fd, 1024)); fclose($fd); foreach ($statinfo as $line) { $info = explode(" ", $line); if ($info[0] == "cpu") { array_shift($info); // pop off "cpu" if (!$info[0]) { array_shift($info); } // pop off blank space (if any) return $info; } } return false; }
/** * Delivers the path to a usable PHP executable. * This is useful for many installations which don't have 'php' (or 'php.exe') * not readily available in the PATH. * * @return the path to the PHP executable * @access public */ function getPHPexePath() { global $_SERVER; static $exe_path; if (empty($exe_path)) { for ($state = 0;; $state++) { switch ($state) { case 0: $exe = $this->expandPHPexePath(''); break; case 1: if (defined('PHP_BINDIR') && PHP_BINDIR !== '.') { $exe = $this->expandPHPexePath(PHP_BINDIR); break; } continue 2; case 2: if (!empty($_SERVER['PHPRC'])) { $exe = $this->expandPHPexePath($_SERVER['PHPRC']); break; } continue 2; case 3: // http://stackoverflow.com/questions/2372624/get-current-php-executable-from-within-script if (!empty($_SERVER['_'])) { $exe = $this->checkPHPexePath($_SERVER['_']); break; } continue 2; case 4: // once more inspired by http://stackoverflow.com/questions/2372624/get-current-php-executable-from-within-script // // This time around we expect either the path to PHP itself or to the PHP_CGI version or in case of apache running // with PHP as a module, the path to Apache. // Hence we do NOT assume that the binary path delivered via the pid lookup(s) below are necessarily PHP binaries // themselves; we perform a path-ascend-scan to check for other viable PHP locations as well: if (function_exists('posix_getpid')) { // Gets the PID of the current executable $pid = posix_getpid(); // Returns the exact path to the PHP executable. $path = exec("readlink -f /proc/{$pid}/exe"); if (!empty($path)) { // don't test httpd server binaries themselves: pretty useless effort anyway: if (strpos($path, 'php') !== false) { $exe = $this->checkPHPexePath($path); if ($exe !== false) { break; } } $exe = $this->ascendedScan4PHPexePath(dirname($path)); if ($exe !== false) { break; } } } else { if (function_exists('getmypid')) { $pid = getmypid(); $path = exec("readlink -f /proc/{$pid}/exe"); if (!empty($path)) { // don't test httpd server binaries themselves: pretty useless effort anyway: if (strpos($path, 'php') !== false) { $exe = $this->checkPHPexePath($path); if ($exe !== false) { break; } } $exe = $this->ascendedScan4PHPexePath(dirname($path)); if ($exe !== false) { break; } } } } // The above is kept as not everyone will have /proc/self/exe: $path = exec("readlink -f /proc/self/exe"); if (!empty($path)) { // don't test httpd server binaries themselves: pretty useless effort anyway: if (strpos($path, 'php') !== false) { $exe = $this->checkPHPexePath($path); if ($exe !== false) { break; } } $exe = $this->ascendedScan4PHPexePath(dirname($path)); if ($exe !== false) { break; } } continue 2; case 5: // on Windows you might need to look in a few other places, e.g. when you have a XAMPP or WAMP install: // using code from http://www.apachehaus.com/forum/index.php?topic=38.0 if (class_exists('COM')) { $wmi = new COM('winmgmts://'); foreach (array('httpd', 'apache', 'php', 'php%') as $exename) { $processes = $wmi->ExecQuery("SELECT * FROM Win32_Process WHERE Name LIKE '{$exename}.exe'"); foreach ($processes as $process) { if (property_exists($process, 'CommandLine')) { if ($process->CommandLine[0] == '"') { $cmd = substr($process->CommandLine, 1); $cmd = substr($cmd, 0, strcspn($cmd, '"')); } else { $cmd = substr($process->CommandLine, 0, strcspn($process->CommandLine, " \t")); } // don't test the apache binaries themselves: pretty useless effort anyway: if (strpos($exename, 'php') !== false) { $exe = $this->checkPHPexePath($cmd); if ($exe !== false) { break 3; } } $exe = $this->ascendedScan4PHPexePath(dirname($cmd)); if ($exe !== false) { break 3; } } } } } continue 2; case 6: // on Windows (and probably on some other platforms as well), we MAY expect to find the extensions quite near the php binary itself: $dir = ini_get('extension_dir'); if (!empty($dir)) { $exe = $this->ascendedScan4PHPexePath($dir); if ($exe !== false) { break; } } continue 2; case 7: // inspired by http://stackoverflow.com/questions/3889486/how-to-get-the-path-of-the-php-bin-from-php/3889630#3889630 $paths = explode(PATH_SEPARATOR, getenv('PATH')); foreach ($paths as $path) { $exe = $this->expandPHPexePath($path); if ($exe !== false) { break 2; } } continue 2; default: break 2; } if (!empty($exe)) { $exe_path = $exe; return $exe; } } // this will fail, but we pass it on anyhow... $exe_path = 'php'; return 'php'; } return $exe_path; }