<?
/**
* EasyWMI 0.99
*
* @author: Zsolt Munoz
* @email: hide@address.com
*
* Mar 14, 2008
*
* Licensed under the GPL --Read License Agreement--
*/
class EasyWMI {
var $comObject;
var $comConnect;
var $server;
var $user;
var $pass;
var $site;
var $dom;
var $query;
var $table;
var $where;
var $withIn;
var $selection;
var $fieldName;
var $fieldVal;
var $assoc;
function EasyWMI($server = "", $user = "", $pass = "", $site = "", $dom = "") {
$this->server = ($server != "") ? (string)$server : "";
$this->user = ($user != "") ? (string)$user : "";
$this->pass = ($pass != "") ? (string)$pass : "";
$this->dom = ($dom != "") ? (string)$dom : "";
$this->query = "";
$this->table = "";
$this->where = "";
$this->withIn = "";
$this->selection = "*";
$this->fieldName = "";
$this->fieldVal = "";
$this->assoc = false;
switch($site) {
case 'ccm':
$this->site = 'ccm';
break;
case 'ccm_Events':
$this->site = 'CCM\\Events';
break;
case 'ccm_VulnerabilityAssessment':
$this->site = 'CCM\\VulnerabilityAssessment';
break;
case 'ccm_invagt':
$this->site = 'CCM\\invagt';
break;
case 'ccm_SoftMgmtAgent':
$this->site = 'CCM\\SoftMgmtAgent';
break;
case 'ccm_LocationServices':
$this->site = 'CCM\\LocationServices';
break;
case 'ccm_DataTransferService':
$this->site = 'CCM\\DataTransferService';
break;
case 'ccm_Messaging':
$this->site = 'CCM\\Messaging';
break;
case 'ccm_Policy':
$this->site = 'CCM\\Policy';
break;
case 'ccm_SoftwareMeteringAgent':
$this->site = 'CCM\\SoftwareMeteringAgent';
break;
case 'ccm_ContentTransferManager':
$this->site = 'CCM\\ContentTransferManager';
break;
case 'ccm_Scheduler':
$this->site = 'CCM\\Scheduler';
break;
case 'cimv2':
$this->site = 'cimv2';
break;
case 'cimv2_sms':
$this->site = 'cimv2\\sms';
break;
case 'SmsDm':
$this->site = 'SmsDm';
break;
case 'sms':
$this->site = 'sms';
break;
case 'sms_inv_schema':
$this->site = 'sms\\inv_schema';
break;
default:
$this->site = 'sms\\site_'.$site;
break;
}
$this->comObject = new COM("WbemScripting.SWbemLocator");
}
// Connection Strings
function connect() {
try {
$this->comConnect = $this->comObject->ConnectServer($this->server, 'root\\'.$this->site, $this->dom.'\\'.$this->user, $this->pass);
return $this->comConnect;
} catch (exception $e) {
return false;
}
}
function disconnect() {
unset($this->comConnect);
unset($this->comObject);
}
function error($message = "") {
if($message == "") {
$message = "Could not connect to WorkStation";
}
echo '<br /><span style="padding: 1px 7px 1px 7px; background-color: #ffd7d7; font-family: verdana; color: #000000; font-size: 13px;"><span style="color: #ff0000; font-weight: bold;">Error!</span> ' . $message . '</span><br />';
}
//****************************************************************************
// Query Strings
function select() {
if($this->connect() != false) {
if($this->createQuery() == NULL) {
return $this->connect()->ExecQuery($this->createSelect());
} else {
return $this->connect()->ExecQuery($this->createQuery());
}
} else {
return false;
}
}
function numRows() {
return $this->select()->count;
}
//****************************************************************************
// Query Builders
function createSelect() {
$this->query = "SELECT " . $this->selection . " FROM " . $this->table . $this->returnWhere();
return $this->query;
}
function returnWhere() {
$where = ($this->where != "") ? (" WHERE (" . $this->where . ")") : "";
return $where;
}
function returnWithIn() {
$withIn = ($this->withIn != "") ? (" WITHIN ".$this->within) : "";
return $withIn;
}
function createQuery() {
$this->query = $this->assoc;
return $this->query;
}
//****************************************************************************
// Set Query Variables
function useTable($table) {
$this->table = (string)$table;
}
function setSelection($selection) {
$this->selection = (string)$selection;
}
function setWhere($where) {
$this->where = (string)$where;
}
function setWithIn($withIn) {
$this->withIn = (string)$withIn;
}
function setQuery($assoc) {
$this->assoc = (string)$assoc;
}
//****************************************************************************
// Create Arrays for Fields
function fetchFieldName() {
$n = 0;
foreach($this->select() as $fieldSelect) {
foreach($fieldSelect->Properties_ as $fieldPropertiesVar=>$fieldPropertiesVal) {
$fieldList[$n] .= strval($fieldPropertiesVal->Name).",";
}
$n++;
}
foreach($fieldList as $newFieldVar=>$newFieldVal) {
$this->fieldName = explode(",", (rtrim($newFieldVal, ",")));
}
return $this->fieldName;
}
function fetchAssoc() {
$rowCount = 0;
if($this->select() != false) {
foreach($this->select() as $fieldSelect) {
foreach($this->fetchFieldName() as $fieldName) {
if(!is_object($fieldSelect->$fieldName)) {
$this->fieldVal[$rowCount][$fieldName] = ($fieldSelect->$fieldName == NULL) ? NULL : trim($fieldSelect->$fieldName);
} else {
$arrCount = 0;
try {
foreach($fieldSelect->$fieldName as $fieldArr) {
$this->fieldVal[$rowCount][$fieldName][$arrCount] = ($fieldArr == NULL) ? NULL : trim($fieldArr);
$arrCount++;
}
} catch (exception $e) {
$this->fieldVal[$rowCount][$fieldName][$arrCount] = NULL;
}
}
}
$rowCount++;
}
} else {
$this->fieldVal = NULL;
}
return $this->fieldVal;
}
function remove($rmQuery) {
try {
$checkSystem = $this->connect()->Get($rmQuery);
$checkSystem->Delete_;
} catch (exception $e) {
return false;
}
}
//****************************************************************************
// Non WMI Scripts
function timeConvert($getTime) {
if(strlen($getTime) != '25') {
$showTime = false;
} else {
$yyyy = substr($getTime, 0, 4);
$mm = substr($getTime, 4, 2);
$dd = substr($getTime, 6, 2);
$hh = substr($getTime, 8, 2);
$min = substr($getTime, 10, 2);
$sec = substr($getTime, 12, 2);
$showTime = $yyyy."-".$mm."-".$dd." ".$hh.":".$min.":".$sec;
}
return $showTime;
}
function mk4digit($index) {
$getIndexLength = strlen($index);
for($n=0; $n<(4 - $getIndexLength); $n++) {
$mk4digit .= "0";
}
$mk4digit .= $index;
return $mk4digit;
}
}; // End Class
?>