PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/class.update.php

https://code.google.com/p/mangoswebv3/
PHP | 426 lines | 332 code | 37 blank | 57 comment | 42 complexity | 1b1cf5d4e57d81d863a1180ff29b2101 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /****************************************************************************/
  3. /* < MangosWeb Enhanced v3 > */
  4. /* Copyright (C) <2009 - 2011> <Wilson212> */
  5. /* < http://keyswow.com > */
  6. /* */
  7. /* Original MangosWeb (C) 2007, Sasha, Nafe, TGM, Peec */
  8. /****************************************************************************/
  9. /* */
  10. /* Update class for MangosWeb Enhanced. You the user are NOT allowed to */
  11. /* copy any of this code, and/or use it for any purpose other than to */
  12. /* update MangosWeb Enhanced. */
  13. /****************************************************************************/
  14. class Update
  15. {
  16. var $current_version;
  17. var $server_version;
  18. var $update_version;
  19. var $updated_files_list = array();
  20. var $update_delete = array();
  21. var $update_make_dir = array();
  22. var $update_remove_dir = array();
  23. var $writable_files;
  24. var $charlen_file;
  25. var $updates;
  26. function Update()
  27. {
  28. global $Core;
  29. $this->server_address = 'http://update.keyswow.com/mangoswebv3/';
  30. $this->current_version = $Core->version;
  31. $this->handle = FALSE;
  32. }
  33. // ************************************************************
  34. // Standard check to see if the server is online function
  35. function connect()
  36. {
  37. $this->handle = @fsockopen('www.keyswow.com', 80, $errno, $errstr, 5);
  38. if($this->handle)
  39. {
  40. return TRUE;
  41. }
  42. else
  43. {
  44. return FALSE;
  45. }
  46. }
  47. // ************************************************************
  48. // This function should always be used FIRST! checks to see if server is online,
  49. // and if there's updates
  50. function check_for_updates()
  51. {
  52. $connection = $this->connect();
  53. if($connection == TRUE)
  54. {
  55. $this->updates = file_get_contents("". $this->server_address ."updates.txt");
  56. $ups = explode(",", $this->updates );
  57. $this->newest = $ups['0'];
  58. if($this->current_version != $this->newest)
  59. {
  60. return 1;
  61. }
  62. else
  63. {
  64. return 2;
  65. }
  66. }
  67. else
  68. {
  69. return 0;
  70. }
  71. }
  72. // ************************************************************
  73. // If there is updates, then this function returns the next update version number.
  74. function get_next_update()
  75. {
  76. $ups = explode(",", $this->updates );
  77. // Ok, so we need the next update, but first we need to find, where in the array is out current version
  78. foreach($ups as $key => $value)
  79. {
  80. if($value == $this->current_version)
  81. {
  82. $tmp_version = $key;
  83. // Now that we have our postion, we subtract 1, to get the next update version
  84. $newkey = $tmp_version - 1;
  85. }
  86. }
  87. $this->update_version = $ups[$newkey];
  88. $this->get_server_variables();
  89. return $this->update_version;
  90. }
  91. // ************************************************************
  92. // This function get the list of all update variables such as
  93. // make dir, remove file, update file etc etc.
  94. function get_server_variables()
  95. {
  96. $variables_file_address = $this->server_address."update_". (string)$this->update_version ."/update_vars.php";
  97. $file = file($variables_file_address);
  98. foreach ($file as $line)
  99. {
  100. if(strstr($line,"[update_version]") !== false)
  101. {
  102. $this->server_version = trim(substr($line,strpos($line,"=")+1));
  103. }
  104. elseif(strstr($line,"[update_info]") !== false)
  105. {
  106. $this->update_info[] = trim(substr($line,strpos($line,"=")+1));
  107. }
  108. elseif(strstr($line,"[update_make_dir]") !== false)
  109. {
  110. $this->update_make_dir[] = trim(substr($line,strpos($line,"=")+1));
  111. }
  112. elseif(strstr($line,"[update_remove_dir]") !== false)
  113. {
  114. $this->update_remove_dir[] = trim(substr($line,strpos($line,"=")+1));
  115. }
  116. elseif(strstr($line,"[update_delete]") !== false)
  117. {
  118. $this->update_delete[] = trim(substr($line,strpos($line,"=")+1));
  119. }
  120. elseif(strstr($line,"[update_file_list]") !== false)
  121. {
  122. $this->updated_files_list[] = trim(substr($line,strpos($line,"=")+1));
  123. }
  124. elseif(strstr($line,"[charlen_file]") !== false)
  125. {
  126. $this->charlen_file[] = trim(substr($line,strpos($line,"=")+1));
  127. }
  128. }
  129. }
  130. // ************************************************************
  131. // Prints updated file list
  132. function print_updated_files_list()
  133. {
  134. $filelist = "";
  135. foreach ($this->updated_files_list as $filename)
  136. {
  137. $filelist .= $filename."<br />";
  138. }
  139. return $filelist;
  140. }
  141. // ************************************************************
  142. // Prints Delete File list
  143. function print_delete_files_list()
  144. {
  145. $filelist = "";
  146. if(count($this->update_delete) > 0)
  147. {
  148. foreach($this->update_delete as $filename)
  149. {
  150. $filelist .= $filename."<br />";
  151. }
  152. }
  153. else
  154. {
  155. echo "None";
  156. }
  157. return $filelist;
  158. }
  159. // ************************************************************
  160. // Prints the Update information info
  161. function print_update_info()
  162. {
  163. $infolist = "";
  164. foreach ($this->update_info as $desc)
  165. {
  166. $infolist .= $desc."<br />";
  167. }
  168. return $infolist;
  169. }
  170. // ************************************************************
  171. // This function checks to see if a file is writable
  172. private function is__writable($path)
  173. {
  174. //Make sure to use a "/" after trailing folders
  175. if ($path{strlen($path)-1} == '/') // recursively return a temporary file path
  176. {
  177. return is__writable($path.uniqid(mt_rand()).'.tmp');
  178. }
  179. else if (is_dir($path))
  180. {
  181. return is__writable($path.'/'.uniqid(mt_rand()).'.tmp');
  182. }
  183. // check tmp file for read/write capabilities
  184. $rm = file_exists($path);
  185. $f = @fopen($path, 'a');
  186. if ($f == false)
  187. {
  188. return FALSE;
  189. }
  190. fclose($f);
  191. if (!$rm)
  192. {
  193. unlink($path);
  194. }
  195. return TRUE;
  196. }
  197. // ************************************************************
  198. // Checks if the all the files in the update are writable
  199. function check_if_are_writable()
  200. {
  201. $err = 0;
  202. foreach ($this->updated_files_list as $filename)
  203. {
  204. if($this->is__writable($filename) == TRUE)
  205. {
  206. $this->writable_files[$filename] = "yes";
  207. }
  208. else
  209. {
  210. $this->writable_files[$filename] = "no";
  211. $err++;
  212. }
  213. }
  214. if($err == 0)
  215. {
  216. $return = TRUE;
  217. }
  218. else
  219. {
  220. $return = FALSE;
  221. }
  222. return $return;
  223. }
  224. // ************************************************************
  225. // Gets the total character length of all updated files
  226. // Can be used for like a progress bar
  227. function get_total_charlen()
  228. {
  229. $total_len = 0;
  230. foreach($this->charlen_file as $len)
  231. {
  232. $total_len += $len;
  233. }
  234. return $total_len;
  235. }
  236. // ************************************************************
  237. // Makes all the directories from the update list
  238. function makeDirs()
  239. {
  240. $mkerr = 0;
  241. $count = count($this->update_make_dir);
  242. if($count > 0)
  243. {
  244. foreach($this->update_make_dir as $mkdir)
  245. {
  246. // First check to see if the directory already exists
  247. $check = @opendir($mkdir);
  248. if($check)
  249. {
  250. @closedir($check);
  251. }
  252. else
  253. {
  254. // We need to make the directory
  255. $make = @mkdir($mkdir, 0775);
  256. if(!$make)
  257. {
  258. $mkerr++;
  259. }
  260. }
  261. }
  262. if($mkerr == 0)
  263. {
  264. return TRUE;
  265. }
  266. else
  267. {
  268. return FALSE;
  269. }
  270. }
  271. else
  272. {
  273. return TRUE;
  274. }
  275. }
  276. // ************************************************************
  277. // Removes all the directories from the update list
  278. function removeDirs()
  279. {
  280. $rmerr = 0;
  281. $count = count($this->update_remove_dir);
  282. if($count > 0)
  283. {
  284. foreach($this->update_remove_dir as $remove)
  285. {
  286. // First check to see if the directory already exists
  287. $check = @opendir($remove);
  288. if($check)
  289. {
  290. @closedir($check);
  291. }
  292. else
  293. {
  294. return FALSE;
  295. }
  296. // We need to revmove the directory
  297. $rm = @rmdir($remove);
  298. if(!$rm)
  299. {
  300. $rmerr++;
  301. }
  302. }
  303. if($rmerr == 0)
  304. {
  305. return TRUE;
  306. }
  307. else
  308. {
  309. return FALSE;
  310. }
  311. }
  312. else
  313. {
  314. return TRUE;
  315. }
  316. }
  317. // ************************************************************
  318. // Main update function
  319. function update_files()
  320. {
  321. $err = "";
  322. // Delete files
  323. if(count($this->update_delete) > 0)
  324. {
  325. foreach($this->update_delete as $unlinkf)
  326. {
  327. $unlink = @unlink($unlinkf);
  328. if(!$unlink)
  329. {
  330. echo $filename." <font color='red'>Problem Removing File!</font><br />";
  331. }
  332. else
  333. {
  334. echo $filename." <font color='green'>Removed Successfully!</font><br />";
  335. }
  336. }
  337. }
  338. if($this->check_if_are_writable() == TRUE)
  339. {
  340. $i = 0;
  341. $len_till_now = 0;
  342. // Update Files Loop
  343. foreach($this->updated_files_list as $filename)
  344. {
  345. // Start by replacing .php to .upd, and using file_get_contents
  346. $updated_file_url = $this->server_address."/update_".$this->update_version."/".str_replace(".php",".upd",$filename);
  347. $updated_file_contents = file_get_contents($updated_file_url);
  348. // As long as the file isnt empty, lets use fopen, and fwrite to put the contents in the file.
  349. if($updated_file_contents != "")
  350. {
  351. $file = fopen($filename,"w");
  352. fwrite($file, $updated_file_contents);
  353. fclose($file);
  354. }
  355. // next 2 lines can be used for a future progress bar. Calculates the total
  356. // character length of all update files, and turns that into a percentage
  357. $len_till_now += $this->charlen_file[$i];
  358. $perc = $len_till_now * 100 / $this->get_total_charlen();
  359. echo $filename." <font color='green'>Updated Successfully!</font><br />";
  360. ob_flush();
  361. flush();
  362. $i++;
  363. }
  364. // These next few lines are for the server to get statistics. The update server will log your servers
  365. // Address so we can get a good count on how many users are using mangosweb, and how many are updating
  366. $open_add = $this->server_address ."index.php?server=".$_SERVER['HTTP_HOST']."&update=".$this->update_version;
  367. $calc = @fopen($open_add, 'r');
  368. if($calc)
  369. {
  370. @fclose($calc);
  371. }
  372. return TRUE;
  373. }
  374. else
  375. {
  376. $err .= "<font color='red'>An error occured while updating. Some files where not writable by the server!</font>";
  377. foreach ($this->writable_files as $id => $value)
  378. {
  379. if($value == "no")
  380. {
  381. echo $id." file is <font color='red'><i>not writable!</i></font><br>";
  382. }
  383. }
  384. $err .= "<font color='red'>No file(s) were updated.<br></font>";
  385. return $err;
  386. }
  387. }
  388. }
  389. ?>