PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/oiclient/lib/common/windowsRegistryClass.php

http://openirudi.googlecode.com/
PHP | 397 lines | 386 code | 11 blank | 0 comment | 3 complexity | 0a503999336587e6e3bbbc0bbc9fce3c MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. define('EXPORTFILE', '/tmp/export.reg');
  3. define('IMPORTFILE', '/tmp/import.reg');
  4. class windowsRegistryClass {
  5. private $hiveFile;
  6. private $prefixString;
  7. function __construct($hiveFile, $prefixString='\\') {
  8. $this->hiveFile= manageFilesClass::caseInsensibleFileName($hiveFile);
  9. $this->prefixString=$prefixString;
  10. }
  11. function prepareKey($key) {
  12. $key = str_replace("\\", "\\\\\\\\", $key);
  13. $key = str_replace(' ',"\\\\x20",$key);
  14. return $key;
  15. }
  16. function prepareKey2($key) {
  17. $key = str_replace("\\", "\\\\x5C", $key);
  18. $key = str_replace(' ',"\\x20",$key);
  19. return $key;
  20. }
  21. function getRealKey( $lowerkey ){
  22. $keys=$this->getRealKeys();
  23. foreach($keys as $lKey => $realKey ){
  24. if(stripos($lKey,$lowerkey) !== false ){
  25. return $realKey;
  26. }
  27. }
  28. return false;
  29. }
  30. function getRealAttrib( $lowerkey ){
  31. $keys=$this->getRealKeys();
  32. foreach($keys as $lKey => $realKey ){
  33. if(stripos($lKey,$lowerkey) !== false ){
  34. return $realKey;
  35. }
  36. }
  37. return false;
  38. }
  39. function txt2hex($txt){
  40. $nip=array();
  41. foreach ( str_split($txt) as $c ) {
  42. $nip[]=unitsClass::longDec2hex(ord($c));
  43. $nip[]="00";
  44. }
  45. $n2ip=implode(",",$nip);
  46. return $n2ip;
  47. }
  48. /*
  49. * $keyPath: Erregistroko zein adarretik aurrera begiratuko degun
  50. * $keyStr: Bilaketa katea. Erregistroko klabean textu kate hau duena bilatuko du.
  51. */
  52. function getObjectsArray( $keyPath='', $keyStr='', $realValue='' ){
  53. $guids=array();
  54. $elements=array();
  55. $exportFile=$this->exportKey($keyPath);
  56. if($exportFile===false){
  57. exceptionHandlerClass::saveError("Error reading Windows registry");
  58. exceptionHandlerClass::saveError("keyPath $keyPath");
  59. return false;
  60. }
  61. $txt = file_get_contents($exportFile);
  62. preg_match_all('/\[([^\]]*)\]([^\[]*)/', $txt, $values);
  63. foreach ($values[2] as $id => $value ){
  64. $d="";
  65. $k=explode("\\\r\n", $value);
  66. foreach($k as $k1){
  67. $d.=trim($k1);
  68. }
  69. $wKey=strtolower($values[1][$id]);
  70. if(empty($d)){
  71. $elements["$wKey"]=$d;
  72. continue;
  73. }else{
  74. preg_match_all('/\"([^\"]*)"=(.*)/', $d, $d2);
  75. foreach ($d2[1] as $id2 => $e ){
  76. if(empty($realValue)){
  77. $elements["$wKey"][strtolower($e)]=str_replace('"','',$d2[2][$id2]);
  78. }else{
  79. $elements["$wKey"][$e]=str_replace('"','',$d2[2][$id2]);
  80. }
  81. }
  82. }
  83. }
  84. foreach ($elements as $key => $value ) {
  85. $key=str_replace('\\\\', '', trim($key));
  86. if(empty($keyStr) ){
  87. $k='[\''.str_replace('\\','\'][\'',$key).'\']';
  88. eval( "\$guids$k=\$value;");
  89. } elseif ( !empty($keyStr) && stripos( $key, $keyStr ) !==false ){
  90. $sub=substr($key,strripos(substr($key,0,stripos($key,$keyStr)),'\\'));
  91. $k='[\''.str_replace('\\','\'][\'',$sub).'\']';
  92. eval( "\$guids$k=\$value;");
  93. }
  94. }
  95. return $guids;
  96. }
  97. function valueReverse(&$offset,&$value){
  98. if( strlen($value) % 2 ){
  99. $value='0'.$value;
  100. }
  101. $len= strlen($value);
  102. $value=implode(',',array_reverse(str_split($value,2)));
  103. $offset=$offset-($len/2)+1;
  104. }
  105. /*
  106. * Klabearen benetako balioa bueltatzen du. $keypath jarri behar degu ControlSet001 adarrean "Segmentation fault" ematen duelako.
  107. * $keyPath Nondik aurrera bilatuko duen.
  108. * $lkey bilatu beharreko katea.
  109. */
  110. function getRealKeys($keyPath='',$lkey=''){
  111. $exportFile=$this->exportKey($keyPath);
  112. if($exportFile===false){
  113. exceptionHandlerClass::saveError("Error reading Windows registry");
  114. return false;
  115. }
  116. $txt = file_get_contents($exportFile);
  117. preg_match_all('/\[([^\]]*)\][^\[]*/', $txt, $values);
  118. $res=array();
  119. if(isset($values[1]) && !empty($values[1])){
  120. foreach($values[1] as $id => $value){
  121. if(empty($lkey)){
  122. $res[strtolower($value)]=$value;
  123. }else{
  124. if(stripos($value,$lkey) !== false ){
  125. $realKey=substr($value,strripos(substr($value,0,stripos($value,$lkey)),'\\')+1);
  126. return $realKey;
  127. }
  128. }
  129. }
  130. return $res;
  131. }else{
  132. return false;
  133. }
  134. }
  135. function exportKey( $key='') {
  136. if (is_file(EXPORTFILE)) {
  137. unlink(EXPORTFILE);
  138. }
  139. if(empty ($key) ){
  140. $key='\\';
  141. }
  142. $key=$this->prepareKey2($key);
  143. $prefixString=$this->prepareKey2($this->prefixString);
  144. if (is_file($this->hiveFile)) {
  145. //sudo /var/www/openirudi/bin/windowsCmd.sh exportRegistryKey $hiveFile $prefix $key $exportFile
  146. $cmd = str_replace('$hiveFile', $this->hiveFile, sfConfig::get('app_windows_export'));
  147. $cmd = str_replace('$prefixString', $prefixString, $cmd);
  148. $cmd = str_replace('$key', $key, $cmd);
  149. $cmd = str_replace('$exportFile', sfConfig::get('app_windows_exportFile'), $cmd);
  150. $re = executeClass::execute($cmd);
  151. if (is_file(EXPORTFILE)) {
  152. return EXPORTFILE;
  153. }else{
  154. exceptionHandlerClass::saveMessage("cmd :: $cmd");
  155. exceptionHandlerClass::saveMessage("re: " .implode('<br>',$re['output']) );
  156. return false;
  157. }
  158. } else {
  159. exceptionHandlerClass::saveError("We not found hive File");
  160. return false;
  161. }
  162. }
  163. function deleteKey($fkey){
  164. if (!is_file($this->hiveFile)) {
  165. exceptionHandlerClass::saveError("We not found hive File");
  166. return false;
  167. }
  168. $cmd = str_replace('$hiveFile', $this->hiveFile, sfConfig::get('app_windows_importRegistryKey'));
  169. $cmd = str_replace('$prefix', "'{$this->prefixString}'", $cmd);
  170. $cmd = str_replace('$importFile', IMPORTFILE, $cmd);
  171. $t="Windows Registry Editor Version 5.00\r\n\r\n";
  172. $t.="[-{$fkey}]\r\n";
  173. file_put_contents(IMPORTFILE, $t);
  174. //$re = executeClass::execute($cmd);
  175. if ($re['return'] == 0 ) {
  176. return true;
  177. }else{
  178. exceptionHandlerClass::saveMessage("cmdREG: " . $cmd);
  179. exceptionHandlerClass::saveMessage("re: " .implode('<br>',$re['output']) );
  180. return false;
  181. }
  182. }
  183. function modifyHexKey($fkey,$type,$attrib,$value){
  184. /*
  185. * [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft]
  186. "Value A"="<String value data>"
  187. "Value B"=hex:<Binary data (as comma-delimited list of hexadecimal values)>
  188. "Value C"=dword:<DWORD value integer>
  189. "Value D"=hex(7):<Multi-string value data (as comma-delimited list of hexadecimal values)>
  190. "Value E"=hex(2):<Expandable string value data (as comma-delimited list of hexadecimal values)>
  191. "Value F"=hex(b):<QWORD value (as comma-delimited list of 8 hexadecimal values, in little endian byte order)>
  192. "Value G"=hex(4):<DWORD value (as comma-delimited list of 4 hexadecimal values, in little endian byte order)>
  193. "Value H"=hex(5):<DWORD value (as comma-delimited list of 4 hexadecimal values, in big endian byte order)>
  194. "Value I"=hex(0):
  195. */
  196. switch ($type){
  197. case 'A':
  198. $v1='"'.$value.'"';
  199. break;
  200. case 'B':
  201. $v1="hex:$value";
  202. break;
  203. case 'C':
  204. $v1="dword:$value";
  205. break;
  206. case 'D':
  207. $v1="hex(7):$value";
  208. break;
  209. case 'E':
  210. $v1="hex(2):$value";
  211. break;
  212. case 'F':
  213. $v1="hex(b):$value";
  214. break;
  215. case 'G':
  216. $v1="hex(4):$value";
  217. break;
  218. case 'H':
  219. $v1="hex(5):$value";
  220. break;
  221. case 'I':
  222. $v1="hex(0):$value";
  223. break;
  224. }
  225. if (!is_file($this->hiveFile)) {
  226. exceptionHandlerClass::saveError("We not found hive File");
  227. return false;
  228. }
  229. $cmd = str_replace('$hiveFile', $this->hiveFile, sfConfig::get('app_windows_importRegistryKey'));
  230. $cmd = str_replace('$prefix', "'{$this->prefixString}'", $cmd);
  231. $cmd = str_replace('$importFile', IMPORTFILE, $cmd);
  232. $t="Windows Registry Editor Version 5.00\r\n\r\n";
  233. $t.="[{$fkey}]\r\n";
  234. $t.="\"{$attrib}\"={$v1}\r\n";
  235. file_put_contents(IMPORTFILE, $t);
  236. $re = executeClass::execute($cmd);
  237. if ($re['return'] == 0 ) {
  238. return true;
  239. }else{
  240. exceptionHandlerClass::saveMessage("cmdREG: " . $cmd);
  241. exceptionHandlerClass::saveMessage("re: " .implode('<br>',$re['output']) );
  242. return false;
  243. }
  244. }
  245. function modifyDwordKey($fkey,$attrib,$value){
  246. if (!is_file($this->hiveFile)) {
  247. exceptionHandlerClass::saveError("We not found hive File");
  248. return false;
  249. }
  250. $value=str_pad($value, 8, "0", STR_PAD_LEFT);
  251. $cmd = str_replace('$hiveFile', $this->hiveFile, sfConfig::get('app_windows_importRegistryKey'));
  252. $cmd = str_replace('$prefix', "'{$this->prefixString}'", $cmd);
  253. $cmd = str_replace('$importFile', IMPORTFILE, $cmd);
  254. $t="Windows Registry Editor Version 5.00\r\n\r\n";
  255. $t.="[{$fkey}]\r\n";
  256. $t.="\"{$attrib}\"=dword:{$value}\r\n";
  257. file_put_contents(IMPORTFILE, $t);
  258. $re = executeClass::execute($cmd);
  259. if ($re['return'] == 0 ) {
  260. return true;
  261. }else{
  262. exceptionHandlerClass::saveMessage("cmdREG: " . $cmd);
  263. exceptionHandlerClass::saveMessage("re: " .implode('<br>',$re['output']) );
  264. return false;
  265. }
  266. }
  267. function modifyStrKey($fkey,$attrib,$value){
  268. if (!is_file($this->hiveFile)) {
  269. exceptionHandlerClass::saveError("We not found hive File");
  270. return false;
  271. }
  272. $cmd = str_replace('$hiveFile', $this->hiveFile, sfConfig::get('app_windows_importRegistryKey'));
  273. $cmd = str_replace('$prefix', "'{$this->prefixString}'", $cmd);
  274. $cmd = str_replace('$importFile', IMPORTFILE, $cmd);
  275. $t="Windows Registry Editor Version 5.00\r\n\r\n";
  276. $t.="[{$fkey}]\r\n";
  277. if(!empty ($attrib) && !empty ($value)){
  278. $t.="\"{$attrib}\"=\"{$value}\"\r\n";
  279. }else if(!empty ($attrib) && empty($value)){
  280. $t.="\"{$attrib}\"\r\n";
  281. }
  282. file_put_contents(IMPORTFILE, "$t" );
  283. $re = executeClass::execute($cmd);
  284. if ($re['return'] == 0 ) {
  285. return true;
  286. }else{
  287. exceptionHandlerClass::saveMessage("cmdREG: " . $cmd);
  288. exceptionHandlerClass::saveMessage("re: " .implode('<br>',$re['output']) );
  289. return false;
  290. }
  291. }
  292. function importArray($fkey, $attribsArray){
  293. if(!is_array($attribsArray)){
  294. exceptionHandlerClass::saveError("Must be array");
  295. return false;
  296. }
  297. $cmd = str_replace('$hiveFile', $this->hiveFile, sfConfig::get('app_windows_importRegistryKey'));
  298. $cmd = str_replace('$prefix', "'{$this->prefixString}'", $cmd);
  299. $cmd = str_replace('$importFile', IMPORTFILE, $cmd);
  300. $t="Windows Registry Editor Version 5.00\r\n\r\n";
  301. $t.="[{$fkey}]\r\n";
  302. foreach ($attribsArray as $attrib => $value ){
  303. if(!empty ($attrib) && !empty ($value)){
  304. $t.="\"{$attrib}\"=\"{$value}\"\r\n";
  305. }else if(!empty ($attrib) && empty($value)){
  306. $t.="\"{$attrib}\"\r\n";
  307. }
  308. }
  309. file_put_contents(IMPORTFILE, "$t" );
  310. $re = executeClass::execute($cmd);
  311. if ($re['return'] == 0 ) {
  312. return true;
  313. }else{
  314. exceptionHandlerClass::saveMessage("cmdREG: " . $cmd);
  315. exceptionHandlerClass::saveMessage("re: " .implode('<br>',$re['output']) );
  316. return false;
  317. }
  318. }
  319. }
  320. ?>