PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/oiclient/lib/images/ImageServerOppClass.php

http://openirudi.googlecode.com/
PHP | 452 lines | 362 code | 84 blank | 6 comment | 74 complexity | 1287c9898564fb287eb4b4c2216db4b7 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. define('HEX2BIN_WS', " \t\n\r");
  3. class ImageServerOppClass {
  4. static function address() {
  5. $address = sfYamlOI::readKey('imageServer');
  6. if (empty($address)) {
  7. return false;
  8. }
  9. return $address;
  10. }
  11. static function url() {
  12. $serverUrl = str_replace('$server', self::address(), sfConfig::get('app_server_imagesUrl'));
  13. return $serverUrl;
  14. }
  15. static function changeAddress($address) {
  16. $listOisystems= systemOppClass::getListOisystems();
  17. $address=$listOisystems->setConfProperty('imageServer', $address);
  18. return self::is_validServer(true);
  19. }
  20. static function images() {
  21. if (!ImageServerOppClass::is_validServer()) {
  22. exceptionHandlerClass::saveError("Connect to valid server");
  23. return array();
  24. }
  25. $list = self::getServerContent2('/listImages.php');
  26. return $list;
  27. }
  28. static function getClientVersion() {
  29. if (!ImageServerOppClass::is_validServer(true)) {
  30. exceptionHandlerClass::saveError("Connect to valid server");
  31. return false;
  32. }
  33. $sclient = self::getServerContent2('/clientVersion.php');
  34. if(is_array($sclient) && isset($sclient['user'] ) && !empty($sclient['password'])){
  35. $jaso2=explode('#@#@#',$sclient['password']);
  36. $iv2=self::hex2bin($jaso2[1]);
  37. $sclient['password'] = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, sfConfig::get('app_const_pwd'), self::hex2bin($jaso2[0]), MCRYPT_MODE_ECB, $iv2));
  38. return $sclient;
  39. }
  40. return false;
  41. }
  42. private static function hex2bin($hex_string) {
  43. $pos = 0;
  44. $result = '';
  45. while ($pos < strlen($hex_string)) {
  46. if (strpos(HEX2BIN_WS, $hex_string{$pos}) !== FALSE) {
  47. $pos++;
  48. } else {
  49. $code = hexdec(substr($hex_string, $pos, 2));
  50. $pos = $pos + 2;
  51. $result .= chr($code);
  52. }
  53. }
  54. return $result;
  55. }
  56. static function getImageInfo() {
  57. if (ImageServerOppClass::is_validServer()) {
  58. $info = self::getServerContent2('/imageInfo.php');
  59. return $info;
  60. }
  61. exceptionHandlerClass::saveError("Connect to valid server");
  62. return array();
  63. }
  64. static function imageSets() {
  65. if (!ImageServerOppClass::is_validServer()) {
  66. exceptionHandlerClass::saveError("Connect to valid server");
  67. return array();
  68. }
  69. $list = self::getServerContent2('/listImageSets.php');
  70. return $list;
  71. }
  72. static function checkServerStatus(){
  73. $v = self::getServerContent('/check.php');
  74. if (is_array($v)) {
  75. $res = array_pop($v[1]);
  76. if ($res == 'OK') {
  77. $validServer=true;
  78. } else {
  79. $validServer=false;
  80. }
  81. } else {
  82. $validServer=false;
  83. }
  84. if($validServer){
  85. sfYamlOI::saveKey('validServer', 'OK');
  86. }else{
  87. sfYamlOI::saveKey('validServer', 'NO');
  88. }
  89. return $validServer;
  90. }
  91. static function is_validServer($check=false) {
  92. $validServer = sfYamlOI::readKey('validServer');
  93. if($check===true || empty($validServer)){
  94. $validServer=self::checkServerStatus();
  95. }
  96. if ($validServer == 'OK') {
  97. return true;
  98. }else{
  99. return false;
  100. }
  101. }
  102. static function savePcConfig($registryArray) {
  103. if (!ImageServerOppClass::is_validServer(true)) {
  104. //exceptionHandlerClass::saveError("Connect to valid server");
  105. return false;
  106. }
  107. $data='out_params='.self::encodeGetParams($registryArray);
  108. $host1=self::doPostRequest('host_insert.php', $data);
  109. $r=self::decodeResult2($host1);
  110. if ($r === false) {
  111. return false;
  112. } else {
  113. return true;
  114. }
  115. }
  116. static function getPcConfig($mac,$hddid) {
  117. if (!ImageServerOppClass::is_validServer(true)) {
  118. //exceptionHandlerClass::saveError("Connect to valid server");
  119. return array();
  120. }
  121. $id=array('mac'=>$mac,'hddid'=>$hddid);
  122. $data='hwId='.base64_encode(serialize($id));
  123. $host1=self::doPostRequest('listHost.php', $data);
  124. $host=self::decodeResult2($host1);
  125. return $host;
  126. }
  127. static function getTasks() {
  128. if (!ImageServerOppClass::is_validServer()) {
  129. //exceptionHandlerClass::saveError("Connect to valid server");
  130. return array();
  131. }
  132. $hw= systemOppClass::getComputer();
  133. $task = array();
  134. $data='hwId='.base64_encode(serialize(array('id'=>$hw->pcID)));
  135. $t=self::doPostRequest('taskList.php', $data);
  136. $tasks=self::decodeResult2($t);
  137. return $tasks;
  138. }
  139. static function getServerContent2($file='', $params='') {
  140. $address = self::address();
  141. if (empty($address)) {
  142. return null;
  143. }
  144. $serverUrl = str_replace('$server', $address, sfConfig::get('app_server_imagesUrl'));
  145. if (empty($params)) {
  146. $out = '';
  147. } elseif (is_array($params)) {
  148. $out = '?out_params=' . self::encodeGetParams($params);
  149. } else {
  150. $out = '?out_params=' . $params;
  151. }
  152. $result = @file_get_contents($serverUrl . '/' . $file . $out);
  153. if ($result === false) {
  154. return false;
  155. } else {
  156. $decoded = self::decodeResult2($result);
  157. return $decoded;
  158. }
  159. }
  160. static function decodeResult2($str) {
  161. $mat = array();
  162. if (empty($str))
  163. return $mat;
  164. if (strpos($str, '!@@@') === false
  165. )return true;
  166. $val = explode('!@@@', $str);
  167. if (!isset($val[1])) {
  168. exceptionHandlerClass::saveError("I cant read task list");
  169. exceptionHandlerClass::saveError("return value:" . $str);
  170. return false;
  171. }
  172. if (isset($val[1]) && !empty($val[1])) {
  173. $o = base64_decode($val[1]);
  174. $mat = unserialize($o);
  175. return $mat;
  176. } elseif (empty($val[1])) {
  177. return false;
  178. }
  179. return $mat;
  180. }
  181. static function getServerContent($file='', $params='') {
  182. $address = self::address();
  183. if (empty($address) || $address ===false ) {
  184. return false;
  185. }
  186. $serverUrl = str_replace('$server', $address, sfConfig::get('app_server_imagesUrl'));
  187. if (empty($params)) {
  188. $out = '';
  189. } elseif (is_array($params)) {
  190. $out = '?out_params=' . self::encodeGetParams($params);
  191. } else {
  192. $out = '?out_params=' . $params;
  193. }
  194. $result = @file_get_contents($serverUrl . '/' . $file . $out);
  195. if ($result === false) {
  196. return false;
  197. } else {
  198. $decoded = self::decodeResult($result);
  199. return $decoded;
  200. }
  201. }
  202. static function encodeGetParams($params) {
  203. $out = '';
  204. foreach ($params as $field => $value) {
  205. $params2[] = "{$field}={$value}";
  206. }
  207. $t = "!@@@" . implode('#!#', $params2) . "!@@@";
  208. $out = base64_encode($t);
  209. return $out;
  210. }
  211. static function decodeResult($str) {
  212. $mat = array();
  213. if (empty($str))
  214. return $mat;
  215. if (strpos($str, '!@@@') === false
  216. )return true;
  217. $val = explode('!@@@', $str);
  218. if (!isset($val[1])) {
  219. exceptionHandlerClass::saveError("I cant read task list");
  220. exceptionHandlerClass::saveError("return value:" . $str);
  221. return false;
  222. }
  223. if (isset($val[1]) && !empty($val[1])) {
  224. $o = base64_decode($val[1]);
  225. if (strpos($o, '%#%') === false && strpos($o, '#!#') === false) {
  226. return $o;
  227. }
  228. $o2 = explode('%#%', $o);
  229. $vals = array();
  230. foreach ($o2 as $i => $row) {
  231. if ($i === '') {
  232. continue;
  233. }
  234. $vals[$i] = explode('#!#', $row);
  235. }
  236. foreach ($vals as $k => $val) {
  237. if ($k === '')
  238. continue;
  239. foreach ($val as $option) {
  240. if (empty($val))
  241. continue;
  242. $val1 = explode('=', $option);
  243. if (empty($val1[0]))
  244. continue;
  245. if (!isset($val1[1])) {
  246. $val1[1] = $val1[0];
  247. }
  248. $mat[$k][$val1[0]] = $val1[1];
  249. }
  250. }
  251. return $mat;
  252. } elseif (empty($val[1])) {
  253. return false;
  254. }
  255. return $mat;
  256. }
  257. static function getTaskToDoNow() {
  258. $taskList = self::getTasks();
  259. $deployed_image = sfYamlOI::readKey('deployed_image');
  260. $deployed_time = sfYamlOI::readKey('deployed_time');
  261. $taskDeltaTime = sfConfig::get('app_oi_taskDeltaTime');
  262. if (!is_array($taskList) || count($taskList) == 0) {
  263. return null;
  264. }
  265. return $taskList;
  266. }
  267. static function createImage($newImage) {
  268. $imageDB = array('name' => $newImage['name'], 'description' => $newImage['description']);
  269. $imageDB['os'] = $newImage['os'];
  270. $imageDB['uuid'] = $newImage['uuid'];
  271. $imageDB['partition_size'] = $newImage['partition_size'];
  272. $imageDB['partition_type'] = $newImage['partition_type'];
  273. $imageDB['filesystem_size'] = $newImage['filesystem_size'];
  274. $imageDB['filesystem_type'] = $newImage['filesystem_type'];
  275. //$id=array('mac'=>$mac,'hddid'=>$hddid);
  276. $data='out_params='.base64_encode(serialize($imageDB));
  277. $img=self::doPostRequest('out_insert.php', $data);
  278. $id=self::decodeResult2($img);
  279. if (empty($id) || !is_numeric($id)) {
  280. exceptionHandlerClass::saveError("new image has not been registered");
  281. self::removeImage($id);
  282. return false;
  283. }
  284. exceptionHandlerClass::saveMessage("new image has been registered in OpenIrudi server DB ($id)");
  285. $fileName = '/tmp/' . sfConfig::get('app_oi_imagePrefix') . $id . ".info";
  286. $r = manageIniFilesClass::writeIniFile($fileName, $newImage);
  287. if ($r === false) {
  288. exceptionHandlerClass::saveError("Error creating image info file");
  289. self::removeImage($id);
  290. return false;
  291. }
  292. return $id;
  293. }
  294. static function removeImage($id) {
  295. $res = self::getServerContent('/out_insert.php', array('remove' => $id));
  296. exceptionHandlerClass::saveMessage("Image $id has not been created.");
  297. return true;
  298. }
  299. static function mountServerFolder() {
  300. if (!self::is_validServer()) {
  301. return false;
  302. }
  303. $listOisystems= systemOppClass::getListOisystems();
  304. $mountCmd = sfConfig::get('app_server_mountCmd');
  305. $server = self::address();
  306. $serverUser = sfConfig::get('app_server_user');
  307. $serverPassword = $listOisystems->sclient['password'];
  308. $localPath = sfConfig::get('app_server_localPath');
  309. $remotePath = sfConfig::get('app_server_remotePath');
  310. $cmd = "$mountCmd $server $serverUser $serverPassword $remotePath $localPath";
  311. $re = executeClass::execute($cmd);
  312. if ($re['return'] != 0) {
  313. exceptionHandlerClass::saveMessage("ERROR connecting to server::" . implode('<br>', $re['output']));
  314. return false;
  315. } else {
  316. return $localPath;
  317. }
  318. return $localPath;
  319. }
  320. static function umountServerFolder() {
  321. $localPath = sfConfig::get('app_server_localPath');
  322. $cmd = str_replace('$partitionName', $localPath, sfConfig::get('app_command_umount'));
  323. $m = executeClass::execute($cmd);
  324. if ($m['return'] != 0) {
  325. sleep(5);
  326. $m = executeClass::execute($cmd);
  327. if ($m['return'] != 0) {
  328. exceptionHandlerClass::saveError("error umounting server");
  329. return false;
  330. }
  331. }
  332. return true;
  333. }
  334. static function saveLog($log ) {
  335. if(!self::is_validServer()) return;
  336. if(is_array($log)){
  337. $logArray=$log;
  338. }else{
  339. $logArray=array( "$log");
  340. }
  341. $data='log='.base64_encode(serialize($logArray));
  342. self::doPostRequest('clientLog.php', $data);
  343. }
  344. static function doPostRequest($file, $data, $optional_headers = null) {
  345. $address = self::address();
  346. if (empty($address)) {
  347. return null;
  348. }
  349. $serverUrl = str_replace('$server', $address, sfConfig::get('app_server_imagesUrl'))."/{$file}";
  350. $params = array('http' => array('method' => 'POST', 'content' => $data));
  351. if ($optional_headers !== null) {
  352. $params['http']['header'] = $optional_headers;
  353. }
  354. $ctx = stream_context_create($params);
  355. $fp = @fopen($serverUrl, 'rb', false, $ctx);
  356. if (!$fp) {
  357. //exceptionHandlerClass::saveError("Problem with $serverUrl ");
  358. return false;
  359. }
  360. $response = @stream_get_contents($fp);
  361. if ($response === false) {
  362. //exceptionHandlerClass::saveError("Problem reading data from $serverUrl");
  363. return false;
  364. }
  365. return $response;
  366. }
  367. }
  368. ?>