1) { $options .= " -d2"; } } // Host $options .= " //" . escapeshellarg($hostname); return $GLOBALS['cache']['wmic_exec'] . " $options"; } // Execute wmic using provided config variables and WQL then return output string // DOCME needs phpdoc block // TESTME needs unit testing function wmi_query($wql, $device, $namespace = NULL) { if ($cmd = wmi_cmd($device, $namespace)) { $cmd .= ' "' . $wql . '"'; print_debug("WMI CMD: " . $cmd); //print_vars($cmd); return external_exec($cmd); } print_debug("WMI ERROR: query ($wql) skipped."); return FALSE; } // Import WMI string to array, remove any empty lines, find "CLASS:" in string, parse the following lines into array // $ret_single == TRUE will output a single dimension array only if there is one "row" of results // $ret_val == will output the value of a single property. Only works when $ret_single == TRUE // Will quit if "ERROR:" is found (usually means the WMI class does not exist) // DOCME needs phpdoc block // TESTME needs unit testing function wmi_parse($wmi_string, $ret_single = FALSE, $ret_val = NULL) { if (!is_string($wmi_string) || safe_empty($wmi_string)) { return NULL; } print_debug($wmi_string); $wmi_lines = array_filter(explode(PHP_EOL, $wmi_string), 'strlen'); $wmi_class = NULL; $wmi_error = NULL; $wmi_properties = []; $wmi_results = []; foreach ($wmi_lines as $line) { if (str_contains($line, 'ERROR:')) { $wmi_error = substr($line, strpos($line, 'ERROR:') + strlen("ERROR: ")); if (OBS_DEBUG) { // If the error is something other than "Retrieve result data." please report it switch ($wmi_error) { case "Retrieve result data.": echo("WMI Error: Cannot connect to host or Class\n"); break; case "Login to remote object.": echo("WMI Error: Invalid security credentials or insufficient WMI security permissions\n"); break; default: echo("WMI Error: Please report"); break; } } return NULL; } if (empty($wmi_class)) { if (str_starts($line, 'CLASS:')) { $wmi_class = substr($line, strlen("CLASS: ")); } } elseif (empty($wmi_properties)) { $wmi_properties = explode($GLOBALS['config']['wmi']['delimiter'], $line); } else { $values = explode($GLOBALS['config']['wmi']['delimiter'], str_replace('(null)', '', $line)); if (count($wmi_properties) !== count($values)) { print_error("WMI ERROR: properties count not same as values count!"); print_debug_vars($wmi_properties); print_debug_vars($values); continue; // Prevent Fatal error: Uncaught ValueError: array_combine(): Argument #1 ($keys) and argument #2 ($values) must have the same number of elements } $wmi_results[] = array_combine($wmi_properties, $values); // Reset class & properties for multiple results $wmi_class = NULL; $wmi_properties = []; } } if (count($wmi_results) === 1) { if ($ret_single) { if ($ret_val) { $wmi_results = $wmi_results[0][$ret_val]; //return $wmi_results[0][$ret_val]; } else { $wmi_results = $wmi_results[0]; //return $wmi_results[0]; } } } print_debug_vars($wmi_results); return $wmi_results; } // DOCME needs phpdoc block // TESTME needs unit testing function wmi_dbAppInsert($device_id, $app) { $dbCheck = dbFetchRow("SELECT * FROM `applications` WHERE `device_id` = ? AND `app_type` = ? AND `app_instance` = ?", [$device_id, $app['type'], $app['instance']]); if (empty($dbCheck)) { echo("Found new application '" . strtoupper($app['type']) . "'"); if (isset($app['instance'])) { echo(" Instance '" . $app['instance'] . "'"); } echo("\n"); dbInsert(['device_id' => $device_id, 'app_type' => $app['type'], 'app_instance' => $app['instance'], 'app_name' => $app['name']], 'applications'); } elseif (empty($dbCheck['app_name']) && isset($app['name'])) { dbUpdate(['app_name' => $app['name']], 'applications', "`app_id` = ?", [$dbCheck['app_id']]); } } // EOF