-
Notifications
You must be signed in to change notification settings - Fork 1
/
Wmi.phpclass
executable file
·575 lines (424 loc) · 18.3 KB
/
Wmi.phpclass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<?php
/**************************************************************************************************************
NAME
WMI.phpclass
DESCRIPTION
WMI interface for PHP.
AUTHOR
Christian Vigh, 11/2015.
HISTORY
[Version : 1.0] [Date : 2014/11/14] [Author : CV]
Initial version.
[Version : 1.1] [Date : 2015/11/30] [Author : CV]
. Classes implemented on-the-fly now include underlying WMI object methods, which are dependent of the
WMI class being implemented.
[Version : 1.1.1] [Date : 2016/10/24] [Author : CV]
. The ArrayAccess interface of the WmiInstance class was incorrectly handled.
**************************************************************************************************************/
/*==============================================================================================================
WMI class -
Wrapper around the WMI COM object.
==============================================================================================================*/
class Wmi
{
// Underlying WMI object
protected $WmiObject ;
/*--------------------------------------------------------------------------------------------------------------
NAME
Constructor
PROTOTYPE
$wmi = new WMI ( $wmi_object ) ;
$wmi = new WMI ( $namespace ) ;
$wmi = new WMI ( ) ;
DESCRIPTION
Instanciates a WMI class which manages an underlying WMI object.
PARAMETERS
$wmi_object (COM object) -
A COM instance of a winmgmts class.
$namespace (string) -
Windows management namespace. The default is :
winmgmts:{impersonationLevel=Impersonate}!//./root/CIMV2
*-------------------------------------------------------------------------------------------------------------*/
public function __construct ( $wmi_object_or_namespace = null )
{
if ( strncasecmp ( php_uname ( 's' ), 'windows', 7 ) )
throw ( new \Exception ( "This class can only run on Windows platforms." ) ) ;
if ( $wmi_object_or_namespace === null )
$this -> WmiObject = new \COM ( 'winmgmts:{impersonationLevel=Impersonate}!//./root/CIMV2' ) ;
else if ( is_string ( $wmi_object_or_namespace ) )
$this -> WmiObject = new \COM ( $wmi_object_or_namespace ) ;
else
$this -> WmiObject = $wmi_object_or_namespace ;
}
/*--------------------------------------------------------------------------------------------------------------
LocalInstance -
Creates a WMI object instance on the local computer.
*-------------------------------------------------------------------------------------------------------------*/
public static function LocalInstance ( $namespace = 'winmgmts:{impersonationLevel=Impersonate}!//./root/CIMV2' )
{
$wmi_object = new self ( new \COM ( $namespace ) ) ;
return ( $wmi_object ) ;
}
/*--------------------------------------------------------------------------------------------------------------
RemoteInstance -
Creates a WMI object instance on the specified remote computer.
*-------------------------------------------------------------------------------------------------------------*/
public static function RemoteInstance ( $computer, $user, $password, $namespace = 'root\CIMV2', $locale = null, $domain = null )
{
$locator = new \COM ( "WbemScripting.SWbemLocator" ) ;
$wmi_object = $locator -> ConnectServer ( $computer, $namespace, $user, $password, $locale, $domain ) ;
$wmi_object = new self ( $wmi_object ) ;
return ( $wmi_object ) ;
}
/*--------------------------------------------------------------------------------------------------------------
QueryInstances -
A shortcut for :
$wmi -> Query ( "SELECT * FROM $table" ) ;
*-------------------------------------------------------------------------------------------------------------*/
public function QueryInstances ( $table, $base_class = 'WmiInstance', $namespace = false )
{
return ( $this -> Query ( "SELECT * FROM $table", $base_class, $namespace ) ) ;
}
/*--------------------------------------------------------------------------------------------------------------
NAME
Query - Queries WMI interface
PROTOTYPE
$array = $wmi -> Query ( $query, $base_class = 'WmiInstance', $namespace = 'Wmi' ) ;
DESCRIPTION
Performs a query on the Windows WMI interface and returns the results as an array of objects belonging
to class $base_class.
PARAMETERS
$query (string) -
Query for the WMI interface, eg :
SELECT * FROM Win32_Process
$base_class (string) -
The Query() method creates a new class for the WMI table being queried. The new class will have
the table name prepended with 'Wmi' ; for example, querying Win32_Process will return objects
of class WmiWin32_Process, inheriting from $base_class which is, by default, the WmiInstance
class.
If you want to encapsulate the generated class, simply declare a new class inheriting from
WmiInstance and specify its name to the Query() call.
$namespace (string) -
Indicates the namespace where new classes are to be created. An empty value means the current
namespace.
RETURN VALUE
Returns an array of Wmixxx objects, where "xxx" is the name of the WMI table being queried.
An empty array is returned if the query returned no results.
*-------------------------------------------------------------------------------------------------------------*/
public function Query ( $query, $base_class = 'WmiInstance', $namespace = false )
{
if ( ! preg_match ( '/FROM \s+ (?P<table> \w+)/imsx', $query, $match ) )
throw ( new \Exception ( "The supplied query does not contain a FROM clause." ) ) ;
$wmi_class = $match [ 'table' ] ;
$full_class_path = $this -> __get_class_path ( $wmi_class, $namespace ) ;
$class_exists = class_exists ( $full_class_path, false ) ;
$rs = $this -> WmiObject -> ExecQuery ( $query ) ;
$result = [] ;
foreach ( $rs as $row )
{
if ( ! $class_exists )
{
$this -> __create_class ( $row, $wmi_class, $base_class, $namespace ) ;
if ( ! is_subclass_of ( $full_class_path, 'WmiInstance' ) )
throw ( new \RuntimeException ( "Class \"$full_class_path\" should inherit from WmiInstance" ) ) ;
$class_exists = true ;
}
$result [] = $this -> __get_instance ( $row, $full_class_path ) ;
}
return ( $result ) ;
}
/*--------------------------------------------------------------------------------------------------------------
NAME
FromVariant - converts a Variant to PHP data.
*-------------------------------------------------------------------------------------------------------------*/
public static function FromVariant ( $variant )
{
if ( ! is_a ( $variant, "variant" ) )
return ( $variant ) ;
$variant_type = variant_get_type ( $variant ) ; // Get variant type
$is_array = ( $variant_type & VT_ARRAY ) ; // Check if array
$is_ref = ( $variant_type & VT_BYREF ) ; // Check if reference (not used)
$variant_type &= ~( VT_ARRAY | VT_BYREF ) ; // Keep only basic type flags
$items = array ( ) ; // Return value
// If variant is an array, get all array elements into a PHP array
if ( $is_array )
{
foreach ( $variant as $variant_item )
$items [] = $variant_item ;
}
else
$items [] = $variant ;
$item_count = count ( $items ) ;
// Loop through array items (item count will be 1 if supplied variant is not an array)
for ( $i = 0 ; $i < $item_count ; $i ++ )
{
$item = $items [$i] ;
// Handle scalar types
switch ( $variant_type )
{
case VT_NULL :
$items [$i] = null ;
break ;
case VT_EMPTY :
$items [$i] = false ;
break ;
case VT_UI1 : case VT_UI2 : case VT_UI4 : case VT_UINT :
case VT_I1 : case VT_I2 : case VT_I4 : case VT_INT :
$items [$i] = ( integer ) $item ;
break ;
case VT_R4 :
$items [$i] = ( float ) $item ;
break ;
case VT_R8 :
$items [$i] = ( double ) $item ;
break ;
case VT_BOOL :
$items [$i] = ( boolean ) $item ;
break ;
case VT_BSTR :
$items [$i] = ( string ) $item ;
break ;
case VT_VARIANT :
if ( $is_array )
break ;
else
/* Intentionally fall through the default: case */ ;
default :
warning ( "Unexpected variant type $variant_type." ) ;
$items [$i] = false ;
}
}
return ( ( $is_array ) ? $items : $items [0] ) ;
}
/*--------------------------------------------------------------------------------------------------------------
Support functions.
*-------------------------------------------------------------------------------------------------------------*/
// __create_class -
// Creates a class on-the-fly mapped to a query result.
private function __create_class ( $row, $class, $base, $namespace )
{
$namespace = ( $namespace ) ? "namespace $namespace ;" : '' ;
$classtext = <<<END
$namespace
class $class extends $base
{
public function __construct ( \$row )
{
parent::__construct ( \$row ) ;
}
END;
$methods = [] ;
foreach ( $row -> Methods_ as $row_method )
{
$method =
[
'name' => $row_method -> Name,
'parameters' => [],
'has-result' => false
] ;
if ( $row_method -> InParameters )
{
foreach ( $row_method -> InParameters -> Properties_ as $parameter )
$method [ 'parameters' ] [] = [ 'name' => $parameter -> Name, 'out' => false ] ;
}
if ( $row_method -> OutParameters )
{
foreach ( $row_method -> OutParameters -> Properties_ as $parameter )
{
if ( ! strcasecmp ( $parameter -> Name, 'ReturnValue' ) )
$method [ 'has-result' ] = true ;
else
$method [ 'parameters' ] [] = [ 'name' => $parameter -> Name, 'out' => true ] ;
}
}
$methods [] = $method ;
}
// Build method text
foreach ( $methods as $method )
{
// Function header
$classtext .= "\n\n\tpublic function {$method [ 'name' ]} ( " ;
// Function arguments
$list = [] ;
foreach ( $method [ 'parameters' ] as $parameter )
{
if ( $parameter [ 'out' ] )
$item = '&$' . $parameter [ 'name' ] ;
else
$item = '$' . $parameter [ 'name' ] ;
$list [] = $item ;
}
$classtext .= implode ( ', ', $list ) . " )\n\t {\n" ;
// Create a variant for each OUT parameter
foreach ( $method [ 'parameters' ] as $parameter )
{
if ( $parameter [ 'out' ] )
$classtext .= "\t\t\$vt_{$parameter [ 'name' ]} = new \VARIANT ( ) ;\n" ;
}
// Call the underlying COM function
$classtext .= "\n\t\t\$__result__ = \$this -> WmiRow -> {$method [ 'name' ]} ( " ;
$list = [] ;
foreach ( $method [ 'parameters' ] as $parameter )
{
if ( $parameter [ 'out' ] )
$item = '$vt_' . $parameter [ 'name' ] ;
else
$item = '$' . $parameter [ 'name' ] ;
$list [] = $item ;
}
$classtext .= implode ( ', ', $list ) . " ) ;\n\n" ;
// Convert OUT parameters from variant to PHP data
foreach ( $method [ 'parameters' ] as $parameter )
{
if ( $parameter [ 'out' ] )
$classtext .= "\t\t\${$parameter [ 'name' ]} = Wmi::FromVariant ( \$vt_{$parameter [ 'name' ]} ) ;\n" ;
}
$classtext .= "\n" ;
// If method returns a value then convert it
if ( $method [ 'has-result' ] )
$classtext .= "\t\treturn ( Wmi::FromVariant ( \$__result__ ) ) ;\n" ;
$classtext .= "\t }\n" ;
}
// Create the class
$classtext = $classtext . "\n }" ;
eval ( $classtext ) ;
}
// __get_class_path -
// Returns the full path of the specified class.
private function __get_class_path ( $class, $namespace )
{
if ( $namespace )
return ( "$namespace\\$class" ) ;
else
return ( "$class" ) ;
}
// __get_instance -
// Instanciate a query row using our brand new class.
private function __get_instance ( $wmi_row, $class )
{
return ( new $class ( $wmi_row ) ) ;
}
}
/*==============================================================================================================
WmiInstance -
Maps the properties of a row returned by a query to real class members.
==============================================================================================================*/
class WmiInstance implements \ArrayAccess, \Countable, \Iterator
{
private $PropertyNames ;
protected $WmiRow ;
/*--------------------------------------------------------------------------------------------------------------
Constructor -
Instanciates a WmiInstance object using a data row returned by a Wmi query.
After instanciation, all the properties of the data row become available as regular PHP object
properties.
Properties are also accessible through iterators and array access.
*-------------------------------------------------------------------------------------------------------------*/
public function __construct ( $wmi_row )
{
$this -> WmiRow = $wmi_row ;
foreach ( $wmi_row -> Properties_ as $property )
{
$property_name = $property -> Name ;
// Property is an array : we have to extract each item from the underlying variant
if ( $property -> IsArray )
{
$property_value = [] ;
// ... but be careful to null or empty arrays
if ( $property -> Value !== null && variant_get_type ( $property -> Value ) != VT_NULL )
{
foreach ( $property -> Value as $item )
$property_value [] = $this -> __normalize ( $item ) ;
}
}
// Property should be a "standard" value, that can be mapped to one of the PHP base scalar types.
else
$property_value = $this -> __normalize ( $property -> Value ) ;
// Assign the property to this instance (either a scalar type or an array)
$this -> $property_name = $property_value ;
// Add it to the list of dynamically defined properties
$this -> PropertyNames [] = $property_name ;
}
$this -> PropertyNames = array_flip ( $this -> PropertyNames ) ;
}
// __normalize -
// Try to guess the type of a property value.
private function __normalize ( $item )
{
if ( is_numeric ( $item ) )
{
if ( $item == ( integer ) $item )
$result = ( integer ) $item ;
else
$result = ( double ) $item ;
}
else if ( $item === null )
$result = null ;
else if ( ! strcasecmp ( $item, 'true' ) )
$result = true ;
else if ( ! strcasecmp ( $item, 'false' ) )
$result = false ;
else
$result = $item ;
return ( $result ) ;
}
/*--------------------------------------------------------------------------------------------------------------
ToArray -
Converts this object to an associative array of property name/value pairs.
*-------------------------------------------------------------------------------------------------------------*/
public function ToArray ( )
{
$result = [] ;
foreach ( $this -> PropertyNames as $name )
$result [ $name ] = $this -> $name ;
return ( $result ) ;
}
/*--------------------------------------------------------------------------------------------------------------
Countable interface implementation.
*-------------------------------------------------------------------------------------------------------------*/
public function count ( )
{ return ( count ( $this -> PropertyNames ) ) ; }
/*--------------------------------------------------------------------------------------------------------------
ArrayAccess interface implementation.
*-------------------------------------------------------------------------------------------------------------*/
private function __offset_get ( $offset )
{
if ( is_integer ( $offset ) && $offset > 0 && $offset < count ( $this -> PropertyNames ) )
return ( $this -> PropertyNames [ $offset ] ) ;
else if ( is_string ( $offset ) && isset ( $this -> PropertyNames [ $offset ] ) )
return ( $this -> $offset ) ;
else
return ( false ) ;
}
public function offsetExists ( $offset )
{ return ( $this -> __offset_get ( $offset ) !== false ) ; }
public function offsetGet ( $offset )
{
$value = $this -> __offset_get ( $offset ) ;
if ( $value !== false )
return ( $value ) ;
else
throw ( new \OutOfRangeException ( "Invalid offset $offset." ) ) ;
}
public function offsetSet ( $offset, $value )
{ throw ( new \Exception ( "Unsupported operation.") ) ; }
public function offsetUnset ( $offset )
{ throw ( new \Exception ( "Unsupported operation.") ) ; }
/*--------------------------------------------------------------------------------------------------------------
Iterator interface implementation.
*-------------------------------------------------------------------------------------------------------------*/
private $__iterator_index = null ;
public function rewind ( )
{ $this -> __iterator_index = 0 ; }
public function valid ( )
{ return ( $this -> __iterator_index >= 0 && $this -> __iterator_index < count ( $this -> PropertyNames ) ) ; }
public function next ( )
{ $this -> __iterator_index ++ ; }
public function key ( )
{ return ( $this -> PropertyNames [ $this -> __iterator_index ] ) ; }
public function current ( )
{
$property = $this -> PropertyNames [ $this -> __iterator_index ] ;
return ( $this -> $property ) ;
}
}