78
78
define ("TLV_TYPE_COMPUTER_NAME " , TLV_META_TYPE_STRING | 1040 );
79
79
define ("TLV_TYPE_OS_NAME " , TLV_META_TYPE_STRING | 1041 );
80
80
define ("TLV_TYPE_USER_NAME " , TLV_META_TYPE_STRING | 1042 );
81
+ define ("TLV_TYPE_ARCHITECTURE " , TLV_META_TYPE_STRING | 1043 );
82
+ define ("TLV_TYPE_LANG_SYSTEM " , TLV_META_TYPE_STRING | 1044 );
83
+
84
+ # Environment
85
+ define ("TLV_TYPE_ENV_VARIABLE " , TLV_META_TYPE_STRING | 1100 );
86
+ define ("TLV_TYPE_ENV_VALUE " , TLV_META_TYPE_STRING | 1101 );
87
+ define ("TLV_TYPE_ENV_GROUP " , TLV_META_TYPE_GROUP | 1102 );
88
+
81
89
82
90
define ("DELETE_KEY_FLAG_RECURSIVE " , (1 << 0 ));
83
91
162
170
# eval'd twice
163
171
my_print ("Evaling stdapi " );
164
172
165
- ##
173
+ ##
166
174
# Search Helpers
167
175
##
168
176
197
205
*/
198
206
if (!function_exists ('safe_glob ' )) {
199
207
function safe_glob ($ pattern , $ flags =0 ) {
200
- $ split =explode ('/ ' ,str_replace ('\\' ,'/ ' ,$ pattern ));
201
- $ mask =array_pop ($ split );
202
- $ path =implode ('/ ' ,$ split );
203
- if (($ dir =opendir ($ path ))!==false ) {
204
- $ glob =array ();
205
- while (($ file =readdir ($ dir ))!==false ) {
206
- // Recurse subdirectories (GLOB_RECURSE)
207
- if (
208
- (
209
- $ flags &GLOB_RECURSE ) && is_dir ($ path ."/ " .$ file )
210
- && (!in_array ($ file ,array ('. ' ,'.. ' ))
211
- # don't follow links to avoid infinite recursion
212
- && (!is_link ($ path ."/ " .$ file ))
213
- )
214
- ) {
215
- $ glob = array_merge ($ glob , array_prepend (safe_glob ($ path .'/ ' .$ file .'/ ' .$ mask , $ flags ),
216
- ($ flags &GLOB_PATH ?'' :$ file .'/ ' )));
208
+ $ split =explode ('/ ' ,str_replace ('\\' ,'/ ' ,$ pattern ));
209
+ $ mask =array_pop ($ split );
210
+ $ path =implode ('/ ' ,$ split );
211
+ if (($ dir =opendir ($ path ))!==false ) {
212
+ $ glob =array ();
213
+ while (($ file =readdir ($ dir ))!==false ) {
214
+ // Recurse subdirectories (GLOB_RECURSE)
215
+ if (
216
+ (
217
+ $ flags &GLOB_RECURSE ) && is_dir ($ path ."/ " .$ file )
218
+ && (!in_array ($ file ,array ('. ' ,'.. ' ))
219
+ # don't follow links to avoid infinite recursion
220
+ && (!is_link ($ path ."/ " .$ file ))
221
+ )
222
+ ) {
223
+ $ glob = array_merge ($ glob , array_prepend (safe_glob ($ path .'/ ' .$ file .'/ ' .$ mask , $ flags ),
224
+ ($ flags &GLOB_PATH ?'' :$ file .'/ ' )));
225
+ }
226
+ // Match file mask
227
+ if (fnmatch ($ mask ,$ file )) {
228
+ if ( ( (!($ flags &GLOB_ONLYDIR )) || is_dir ("$ path/ $ file " ) )
229
+ && ( (!($ flags &GLOB_NODIR )) || (!is_dir ($ path .'/ ' .$ file )) )
230
+ && ( (!($ flags &GLOB_NODOTS )) || (!in_array ($ file ,array ('. ' ,'.. ' ))) ) )
231
+ $ glob [] = ($ flags &GLOB_PATH ?$ path .'/ ' :'' ) . $ file . ($ flags &GLOB_MARK ?'/ ' :'' );
217
232
}
218
- // Match file mask
219
- if (fnmatch ($ mask ,$ file )) {
220
- if ( ( (!($ flags &GLOB_ONLYDIR )) || is_dir ("$ path/ $ file " ) )
221
- && ( (!($ flags &GLOB_NODIR )) || (!is_dir ($ path .'/ ' .$ file )) )
222
- && ( (!($ flags &GLOB_NODOTS )) || (!in_array ($ file ,array ('. ' ,'.. ' ))) ) )
223
- $ glob [] = ($ flags &GLOB_PATH ?$ path .'/ ' :'' ) . $ file . ($ flags &GLOB_MARK ?'/ ' :'' );
224
- }
225
- }
226
- closedir ($ dir );
227
- if (!($ flags &GLOB_NOSORT )) sort ($ glob );
228
- return $ glob ;
229
- } else {
230
- return false ;
231
- }
233
+ }
234
+ closedir ($ dir );
235
+ if (!($ flags &GLOB_NOSORT )) sort ($ glob );
236
+ return $ glob ;
237
+ } else {
238
+ return false ;
239
+ }
232
240
}
233
241
}
234
242
/**
@@ -239,7 +247,7 @@ function safe_glob($pattern, $flags=0) {
239
247
*/
240
248
if (!function_exists ('fnmatch ' )) {
241
249
function fnmatch ($ pattern , $ string ) {
242
- return @preg_match ('/^ ' . strtr (addcslashes ($ pattern , '\\/.+^$(){}=!<>| ' ), array ('* ' => '.* ' , '? ' => '.? ' )) . '$/i ' , $ string );
250
+ return @preg_match ('/^ ' . strtr (addcslashes ($ pattern , '\\/.+^$(){}=!<>| ' ), array ('* ' => '.* ' , '? ' => '.? ' )) . '$/i ' , $ string );
243
251
}
244
252
}
245
253
@@ -261,7 +269,7 @@ function array_prepend($array, $string, $deep=false) {
261
269
else
262
270
$ array [$ key ] = $ string .$ element ;
263
271
return $ array ;
264
-
272
+
265
273
}
266
274
}
267
275
@@ -519,13 +527,13 @@ function stdapi_fs_md5($req, &$pkt) {
519
527
$ path_tlv = packet_get_tlv ($ req , TLV_TYPE_FILE_PATH );
520
528
$ path = cononicalize_path ($ path_tlv ['value ' ]);
521
529
522
- if (is_callable ("md5_file " )) {
523
- $ md5 = md5_file ($ path );
524
- } else {
525
- $ md5 = md5 (file_get_contents ($ path ));
526
- }
527
- $ md5 = pack ("H* " , $ md5 );
528
- # Ghetto abuse of file name type to indicate the md5 result
530
+ if (is_callable ("md5_file " )) {
531
+ $ md5 = md5_file ($ path );
532
+ } else {
533
+ $ md5 = md5 (file_get_contents ($ path ));
534
+ }
535
+ $ md5 = pack ("H* " , $ md5 );
536
+ # Ghetto abuse of file name type to indicate the md5 result
529
537
packet_add_tlv ($ pkt , create_tlv (TLV_TYPE_FILE_NAME , $ md5 ));
530
538
return ERROR_SUCCESS ;
531
539
}
@@ -538,13 +546,13 @@ function stdapi_fs_sha1($req, &$pkt) {
538
546
$ path_tlv = packet_get_tlv ($ req , TLV_TYPE_FILE_PATH );
539
547
$ path = cononicalize_path ($ path_tlv ['value ' ]);
540
548
541
- if (is_callable ("sha1_file " )) {
542
- $ sha1 = sha1_file ($ path );
543
- } else {
544
- $ sha1 = sha1 (file_get_contents ($ path ));
545
- }
546
- $ sha1 = pack ("H* " , $ sha1 );
547
- # Ghetto abuse of file name type to indicate the sha1 result
549
+ if (is_callable ("sha1_file " )) {
550
+ $ sha1 = sha1_file ($ path );
551
+ } else {
552
+ $ sha1 = sha1 (file_get_contents ($ path ));
553
+ }
554
+ $ sha1 = pack ("H* " , $ sha1 );
555
+ # Ghetto abuse of file name type to indicate the sha1 result
548
556
packet_add_tlv ($ pkt , create_tlv (TLV_TYPE_FILE_NAME , $ sha1 ));
549
557
return ERROR_SUCCESS ;
550
558
}
@@ -573,6 +581,41 @@ function stdapi_sys_config_getuid($req, &$pkt) {
573
581
}
574
582
}
575
583
584
+ if (!function_exists ('stdapi_sys_config_getenv ' )) {
585
+ register_command ('stdapi_sys_config_getenv ' );
586
+ function stdapi_sys_config_getenv ($ req , &$ pkt ) {
587
+ my_print ("doing getenv " );
588
+
589
+ $ variable_tlvs = packet_get_all_tlvs ($ req , TLV_TYPE_ENV_VARIABLE );
590
+
591
+ # If we decide some day to have sys.config.getenv return all env
592
+ # vars when given an empty search list, this is one way to do it.
593
+ #if (empty($variable_tlvs)) {
594
+ # # We don't have a var to look up, return all of 'em
595
+ # $variables = array_keys($_SERVER);
596
+ #} else {
597
+ # $variables = array();
598
+ # foreach ($variable_tlvs as $tlv) {
599
+ # array_push($variables, $tlv['value']);
600
+ # }
601
+ #}
602
+
603
+ foreach ($ variable_tlvs as $ name ) {
604
+ $ canonical_name = str_replace (array ("$ " ,"% " ), "" , $ name ['value ' ]);
605
+ $ env = getenv ($ canonical_name );
606
+ if ($ env !== FALSE ) {
607
+ $ grp = "" ;
608
+ $ grp .= tlv_pack (create_tlv (TLV_TYPE_ENV_VARIABLE , $ canonical_name ));
609
+ $ grp .= tlv_pack (create_tlv (TLV_TYPE_ENV_VALUE , $ env ));
610
+ packet_add_tlv ($ pkt , create_tlv (TLV_TYPE_ENV_GROUP , $ grp ));
611
+ }
612
+ }
613
+
614
+ return ERROR_SUCCESS ;
615
+ }
616
+ }
617
+
618
+
576
619
# Unimplemented becuase it's unimplementable
577
620
#if (!function_exists('stdapi_sys_config_rev2self')) {
578
621
#register_command('stdapi_sys_config_rev2self');
@@ -696,24 +739,24 @@ function close_process($proc) {
696
739
foreach ($ proc ['pipes ' ] as $ f ) {
697
740
@fclose ($ f );
698
741
}
699
- if (is_callable ('proc_get_status ' )) {
700
- $ status = proc_get_status ($ proc ['handle ' ]);
701
- } else {
702
- # fake a running process on php < 4.3
703
- $ status = array ('running ' => true );
704
- }
705
-
706
- # proc_close blocks waiting for the child to exit, so if it's still
707
- # running, don't take a chance on deadlock and just sigkill it if we
708
- # can. We can't on php < 4.3, so don't do anything. This will leave
709
- # zombie processes, but that's better than deadlock.
710
- if ($ status ['running ' ] == false ) {
711
- proc_close ($ proc ['handle ' ]);
712
- } else {
713
- if (is_callable ('proc_terminate ' )) {
714
- proc_terminate ($ proc ['handle ' ], 9 );
715
- }
716
- }
742
+ if (is_callable ('proc_get_status ' )) {
743
+ $ status = proc_get_status ($ proc ['handle ' ]);
744
+ } else {
745
+ # fake a running process on php < 4.3
746
+ $ status = array ('running ' => true );
747
+ }
748
+
749
+ # proc_close blocks waiting for the child to exit, so if it's still
750
+ # running, don't take a chance on deadlock and just sigkill it if we
751
+ # can. We can't on php < 4.3, so don't do anything. This will leave
752
+ # zombie processes, but that's better than deadlock.
753
+ if ($ status ['running ' ] == false ) {
754
+ proc_close ($ proc ['handle ' ]);
755
+ } else {
756
+ if (is_callable ('proc_terminate ' )) {
757
+ proc_terminate ($ proc ['handle ' ], 9 );
758
+ }
759
+ }
717
760
if (array_key_exists ('cid ' , $ proc ) && $ channel_process_map [$ proc ['cid ' ]]) {
718
761
unset($ channel_process_map [$ proc ['cid ' ]]);
719
762
}
0 commit comments