PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/utils/install.php

http://litepublisher.googlecode.com/
PHP | 596 lines | 360 code | 92 blank | 144 comment | 66 complexity | 907d5523361612247740a2c54a821de9 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0
  1. <?php
  2. //lib/include/tar.class.php
  3. /* special changes and bug fixes by Vladimir Yushko
  4. http://litepublisher.com/
  5. */
  6. /*
  7. =======================================================================
  8. Name:
  9. tar Class
  10. Author:
  11. Josh Barger <joshb@npt.com>
  12. Description:
  13. This class reads and writes Tape-Archive (TAR) Files and Gzip
  14. compressed TAR files, which are mainly used on UNIX systems.
  15. This class works on both windows AND unix systems, and does
  16. NOT rely on external applications!! Woohoo!
  17. Usage:
  18. Copyright (C) 2002 Josh Barger
  19. This library is free software; you can redistribute it and/or
  20. modify it under the terms of the GNU Lesser General Public
  21. License as published by the Free Software Foundation; either
  22. version 2.1 of the License, or (at your option) any later version.
  23. This library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. Lesser General Public License for more details at:
  27. http://www.gnu.org/copyleft/lesser.html
  28. If you use this script in your application/website, please
  29. send me an e-mail letting me know about it :)
  30. Bugs:
  31. Please report any bugs you might find to my e-mail address
  32. at joshb@npt.com. If you have already created a fix/patch
  33. for the bug, please do send it to me so I can incorporate it into my release.
  34. Version History:
  35. 1.0 04/10/2002 - InitialRelease
  36. 2.0 04/11/2002 - Merged both tarReader and tarWriter
  37. classes into one
  38. - Added support for gzipped tar files
  39. Remember to name for .tar.gz or .tgz
  40. if you use gzip compression!
  41. :: THIS REQUIRES ZLIB EXTENSION ::
  42. - Added additional comments to
  43. functions to help users
  44. - Added ability to remove files and
  45. directories from archive
  46. 2.1 04/12/2002 - Fixed serious bug in generating tar
  47. - Created another example file
  48. - Added check to make sure ZLIB is
  49. installed before running GZIP
  50. compression on TAR
  51. 2.2 05/07/2002 - Added automatic detection of Gzipped
  52. tar files (Thanks go to J?gen Falch
  53. for the idea)
  54. - Changed "private" functions to have
  55. special function names beginning with
  56. two underscores
  57. =======================================================================
  58. */
  59. class tar {
  60. // Unprocessed Archive Information
  61. var $filename;
  62. var $isGzipped;
  63. var $tar_file;
  64. // Processed Archive Information
  65. var $files;
  66. var $directories;
  67. var $numFiles;
  68. var $numDirectories;
  69. // Class Constructor -- Does nothing...
  70. function tar() {
  71. return true;
  72. }
  73. // Computes the unsigned Checksum of a file's header
  74. // to try to ensure valid file
  75. // PRIVATE ACCESS FUNCTION
  76. function __computeUnsignedChecksum($bytestring) {
  77. $unsigned_chksum = 0;
  78. for($i=0; $i<512; $i++)
  79. $unsigned_chksum += ord($bytestring[$i]);
  80. for($i=0; $i<8; $i++)
  81. $unsigned_chksum -= ord($bytestring[148 + $i]);
  82. $unsigned_chksum += ord(" ") * 8;
  83. return $unsigned_chksum;
  84. }
  85. // Converts a NULL padded string to a non-NULL padded string
  86. // PRIVATE ACCESS FUNCTION
  87. function __parseNullPaddedString($string) {
  88. $position = strpos($string,chr(0));
  89. return substr($string,0,$position);
  90. }
  91. // This function parses the current TAR file
  92. // PRIVATE ACCESS FUNCTION
  93. function __parseTar() {
  94. // Read Files from archive
  95. $tar_length = strlen($this->tar_file);
  96. $main_offset = 0;
  97. while($main_offset < $tar_length) {
  98. // If we read a block of 512 nulls, we are at the end of the archive
  99. if(substr($this->tar_file,$main_offset,512) == str_repeat(chr(0),512))
  100. break;
  101. // Parse file name
  102. $file_name = $this->__parseNullPaddedString(substr($this->tar_file,$main_offset,100));
  103. // Parse the file mode
  104. $file_mode = substr($this->tar_file,$main_offset + 100,8);
  105. // Parse the file user ID
  106. $file_uid = octdec(substr($this->tar_file,$main_offset + 108,8));
  107. // Parse the file group ID
  108. $file_gid = octdec(substr($this->tar_file,$main_offset + 116,8));
  109. // Parse the file size
  110. $file_size = octdec(substr($this->tar_file,$main_offset + 124,12));
  111. // Parse the file update time - unix timestamp format
  112. $file_time = octdec(substr($this->tar_file,$main_offset + 136,12));
  113. // Parse Checksum
  114. $file_chksum = octdec(substr($this->tar_file,$main_offset + 148,6));
  115. // Parse user name
  116. $file_uname = $this->__parseNullPaddedString(substr($this->tar_file,$main_offset + 265,32));
  117. // Parse Group name
  118. $file_gname = $this->__parseNullPaddedString(substr($this->tar_file,$main_offset + 297,32));
  119. // Make sure our file is valid
  120. if($this->__computeUnsignedChecksum(substr($this->tar_file,$main_offset,512)) != $file_chksum)
  121. return false;
  122. // Parse File Contents
  123. $file_contents = substr($this->tar_file,$main_offset + 512,$file_size);
  124. /* ### Unused Header Information ###
  125. $activeFile["typeflag"] = substr($this->tar_file,$main_offset + 156,1);
  126. $activeFile["linkname"] = substr($this->tar_file,$main_offset + 157,100);
  127. $activeFile["magic"] = substr($this->tar_file,$main_offset + 257,6);
  128. $activeFile["version"] = substr($this->tar_file,$main_offset + 263,2);
  129. $activeFile["devmajor"] = substr($this->tar_file,$main_offset + 329,8);
  130. $activeFile["devminor"] = substr($this->tar_file,$main_offset + 337,8);
  131. $activeFile["prefix"] = substr($this->tar_file,$main_offset + 345,155);
  132. $activeFile["endheader"] = substr($this->tar_file,$main_offset + 500,12);
  133. */
  134. if($file_size > 0) {
  135. // Increment number of files
  136. $this->numFiles++;
  137. // Create us a new file in our array
  138. $activeFile = &$this->files[];
  139. // Asign Values
  140. $activeFile["name"] = $file_name;
  141. $activeFile["mode"] = $file_mode;
  142. $activeFile["size"] = $file_size;
  143. $activeFile["time"] = $file_time;
  144. $activeFile["user_id"] = $file_uid;
  145. $activeFile["group_id"] = $file_gid;
  146. $activeFile["user_name"] = $file_uname;
  147. $activeFile["group_name"] = $file_gname;
  148. $activeFile["checksum"] = $file_chksum;
  149. $activeFile["file"] = $file_contents;
  150. } else {
  151. // Increment number of directories
  152. $this->numDirectories++;
  153. // Create a new directory in our array
  154. $activeDir = &$this->directories[];
  155. // Assign values
  156. $activeDir["name"] = $file_name;
  157. $activeDir["mode"] = $file_mode;
  158. $activeDir["time"] = $file_time;
  159. $activeDir["user_id"] = $file_uid;
  160. $activeDir["group_id"] = $file_gid;
  161. $activeDir["user_name"] = $file_uname;
  162. $activeDir["group_name"] = $file_gname;
  163. $activeDir["checksum"] = $file_chksum;
  164. }
  165. // Move our offset the number of blocks we have processed
  166. $main_offset += 512 + (ceil($file_size / 512) * 512);
  167. }
  168. return true;
  169. }
  170. public function loadfromstring($s) {
  171. // Clear any values from previous tar archives
  172. unset($this->filename);
  173. unset($this->isGzipped);
  174. unset($this->tar_file);
  175. unset($this->files);
  176. unset($this->directories);
  177. unset($this->numFiles);
  178. unset($this->numDirectories);
  179. $this->tar_file = $s;
  180. if($this->tar_file[0] == chr(31) && $this->tar_file[1] == chr(139) && $this->tar_file[2] == chr(8)) {
  181. $this->isGzipped = TRUE;
  182. $this->tar_file = gzinflate(substr($this->tar_file,10,-4));
  183. }
  184. // Parse the TAR file
  185. $this->__parseTar();
  186. return true;
  187. }
  188. // Generates a TAR file from the processed data
  189. // PRIVATE ACCESS FUNCTION
  190. function __generateTAR() {
  191. // Clear any data currently in $this->tar_file
  192. unset($this->tar_file);
  193. // Generate Records for each directory, if we have directories
  194. if($this->numDirectories > 0) {
  195. foreach($this->directories as $key => $information) {
  196. //unset($header);
  197. // Generate tar header for this directory
  198. // Filename, Permissions, UID, GID, size, Time, checksum, typeflag, linkname, magic, version, user name, group name, devmajor, devminor, prefix, end
  199. $header = str_pad($information["name"],100,chr(0));
  200. $header .= str_pad(decoct($information["mode"]),7,"0",STR_PAD_LEFT) . chr(0);
  201. $header .= str_pad(decoct($information["user_id"]),7,"0",STR_PAD_LEFT) . chr(0);
  202. $header .= str_pad(decoct($information["group_id"]),7,"0",STR_PAD_LEFT) . chr(0);
  203. $header .= str_pad(decoct(0),11,"0",STR_PAD_LEFT) . chr(0);
  204. $header .= str_pad(decoct($information["time"]),11,"0",STR_PAD_LEFT) . chr(0);
  205. $header .= str_repeat(" ",8);
  206. $header .= "5";
  207. $header .= str_repeat(chr(0),100);
  208. $header .= str_pad("ustar",6,chr(32));
  209. $header .= chr(32) . chr(0);
  210. $header .= str_pad("",32,chr(0));
  211. $header .= str_pad("",32,chr(0));
  212. $header .= str_repeat(chr(0),8);
  213. $header .= str_repeat(chr(0),8);
  214. $header .= str_repeat(chr(0),155);
  215. $header .= str_repeat(chr(0),12);
  216. // Compute header checksum
  217. $checksum = str_pad(decoct($this->__computeUnsignedChecksum($header)),6,"0",STR_PAD_LEFT);
  218. for($i=0; $i<6; $i++) {
  219. $header[(148 + $i)] = substr($checksum,$i,1);
  220. }
  221. $header[154] = chr(0);
  222. $header[155] = chr(32);
  223. // Add new tar formatted data to tar file contents
  224. $this->tar_file .= $header;
  225. }
  226. }
  227. // Generate Records for each file, if we have files (We should...)
  228. if($this->numFiles > 0) {
  229. foreach($this->files as $key => $information) {
  230. //unset($header);
  231. // Generate the TAR header for this file
  232. // Filename, Permissions, UID, GID, size, Time, checksum, typeflag, linkname, magic, version, user name, group name, devmajor, devminor, prefix, end
  233. $header = str_pad($information["name"],100,chr(0));
  234. $header .= str_pad(decoct($information["mode"]),7,"0",STR_PAD_LEFT) . chr(0);
  235. $header .= str_pad(decoct($information["user_id"]),7,"0",STR_PAD_LEFT) . chr(0);
  236. $header .= str_pad(decoct($information["group_id"]),7,"0",STR_PAD_LEFT) . chr(0);
  237. $header .= str_pad(decoct($information["size"]),11,"0",STR_PAD_LEFT) . chr(0);
  238. $header .= str_pad(decoct($information["time"]),11,"0",STR_PAD_LEFT) . chr(0);
  239. $header .= str_repeat(" ",8);
  240. $header .= "0";
  241. $header .= str_repeat(chr(0),100);
  242. $header .= str_pad("ustar",6,chr(32));
  243. $header .= chr(32) . chr(0);
  244. $header .= str_pad($information["user_name"],32,chr(0)); // How do I get a file's user name from PHP?
  245. $header .= str_pad($information["group_name"],32,chr(0)); // How do I get a file's group name from PHP?
  246. $header .= str_repeat(chr(0),8);
  247. $header .= str_repeat(chr(0),8);
  248. $header .= str_repeat(chr(0),155);
  249. $header .= str_repeat(chr(0),12);
  250. // Compute header checksum
  251. $checksum = str_pad(decoct($this->__computeUnsignedChecksum($header)),6,"0",STR_PAD_LEFT);
  252. for($i=0; $i<6; $i++) {
  253. $header[(148 + $i)] = substr($checksum,$i,1);
  254. }
  255. $header[154] = chr(0);
  256. $header[155] = chr(32);
  257. // Pad file contents to byte count divisible by 512
  258. $file_contents = str_pad($information["file"],(ceil($information["size"] / 512) * 512),chr(0));
  259. // Add new tar formatted data to tar file contents
  260. $this->tar_file .= $header . $file_contents;
  261. }
  262. }
  263. // Add 512 bytes of NULLs to designate EOF
  264. $this->tar_file .= str_repeat(chr(0),512);
  265. return true;
  266. }
  267. public function loadfromfile($filename) {
  268. if(!file_exists($filename)) return false;
  269. $this->filename = $filename;
  270. return $this->loadfromstring(file_get_contents($filename));
  271. }
  272. // Appends a tar file to the end of the currently opened tar file
  273. function appendTar($filename) {
  274. // If the tar file doesn't exist...
  275. if(!file_exists($filename))
  276. return false;
  277. $this->__readTar($filename);
  278. return true;
  279. }
  280. // Retrieves information about a file in the current tar archive
  281. function getFile($filename) {
  282. if($this->numFiles > 0) {
  283. foreach($this->files as $key => $information) {
  284. if($information["name"] == $filename)
  285. return $information;
  286. }
  287. }
  288. return false;
  289. }
  290. // Retrieves information about a directory in the current tar archive
  291. function getDirectory($dirname) {
  292. if($this->numDirectories > 0) {
  293. foreach($this->directories as $key => $information) {
  294. if($information["name"] == $dirname)
  295. return $information;
  296. }
  297. }
  298. return false;
  299. }
  300. // Check if this tar archive contains a specific file
  301. function containsFile($filename) {
  302. if($this->numFiles > 0) {
  303. foreach($this->files as $key => $information) {
  304. if($information["name"] == $filename)
  305. return true;
  306. }
  307. }
  308. return false;
  309. }
  310. // Check if this tar archive contains a specific directory
  311. function containsDirectory($dirname) {
  312. if($this->numDirectories > 0) {
  313. foreach($this->directories as $key => $information) {
  314. if($information["name"] == $dirname)
  315. return true;
  316. }
  317. }
  318. return false;
  319. }
  320. // Add a directory to this tar archive
  321. function adddir($dirname, $perm = 0777) {
  322. $this->numDirectories++;
  323. $activeDir = &$this->directories[];
  324. $activeDir["name"] = $dirname;
  325. $activeDir["mode"] = $perm;
  326. $activeDir["time"] = time();
  327. $activeDir["user_id"] = 0;
  328. $activeDir["group_id"] = 0;
  329. $activeDir["user_name"] = "";
  330. $activeDir["group_name"] = "";
  331. $activeDir["checksum"] = 0;
  332. return true;
  333. }
  334. // Add a file to the tar archive
  335. public function add($realfile, $filename, $perm = 0666) {
  336. if($this->containsFile($filename)) return false;
  337. $file_information = stat($realfile);
  338. if (($perm == 0) && (DIRECTORY_SEPARATOR == '/')) $perm = $file_information["mode"] == 0 ? $perm : $file_information["mode"];
  339. // Read in the file's contents
  340. $file_contents = file_get_contents($realfile);
  341. // Add file to processed data
  342. $checksum = 0;
  343. $this->numFiles++;
  344. $activeFile = &$this->files[];
  345. $activeFile["name"] = $filename;
  346. $activeFile["mode"] = $perm;
  347. $activeFile["user_id"] = $file_information["uid"]; // == 0 ? 33 : $file_information["uid"];
  348. $activeFile["group_id"] = $file_information["gid"]; // == 0 ? 33 : $file_information["gid"];
  349. $activeFile["user_name"] = "";
  350. $activeFile["group_name"] = "";
  351. $activeFile["size"] = strlen($file_contents);
  352. $activeFile["time"] = $file_information["mtime"];
  353. $activeFile["checksum"] = 0;
  354. $activeFile["file"] = $file_contents;
  355. return true;
  356. }
  357. public function addstring($s, $filename, $perm = 0666) {
  358. if($this->containsFile($filename)) return false;
  359. // Add file to processed data
  360. $this->numFiles++;
  361. $activeFile = &$this->files[];
  362. $activeFile["name"] = $filename;
  363. $activeFile["mode"] = $perm;
  364. $activeFile["user_id"] = 0;
  365. $activeFile["group_id"] = 0;
  366. $activeFile["size"] = strlen($s);
  367. $activeFile["time"] = time();
  368. $activeFile["checksum"] = 0;
  369. $activeFile["user_name"] = "";
  370. $activeFile["group_name"] = "";
  371. $activeFile["file"] = $s;
  372. return true;
  373. }
  374. // Remove a file from the tar archive
  375. function removeFile($filename) {
  376. if($this->numFiles > 0) {
  377. foreach($this->files as $key => $information) {
  378. if($information["name"] == $filename) {
  379. $this->numFiles--;
  380. unset($this->files[$key]);
  381. return true;
  382. }
  383. }
  384. }
  385. return false;
  386. }
  387. // Remove a directory from the tar archive
  388. function removeDirectory($dirname) {
  389. if($this->numDirectories > 0) {
  390. foreach($this->directories as $key => $information) {
  391. if($information["name"] == $dirname) {
  392. $this->numDirectories--;
  393. unset($this->directories[$key]);
  394. return true;
  395. }
  396. }
  397. }
  398. return false;
  399. }
  400. // Write the currently loaded tar archive to disk
  401. function saveTar() {
  402. if(!$this->filename)
  403. return false;
  404. // Write tar to current file using specified gzip compression
  405. $this->toTar($this->filename,$this->isGzipped);
  406. return true;
  407. }
  408. // Saves tar archive to a different file than the current file
  409. function savetofile($filename,$useGzip) {
  410. return file_put_contents($filename, $this->savetostring($useGzip));
  411. }
  412. function savetostring($useGzip) {
  413. $this->__generateTar();
  414. return$useGzip ? gzencode($this->tar_file) : $this->tar_file;
  415. }
  416. }
  417. // lib/http.class.php
  418. class http {
  419. public static function get($url) {
  420. $timeout = 10;
  421. $parsed = @parse_url($url);
  422. if ( !$parsed || !is_array($parsed) ) return false;
  423. if ( !isset($parsed['scheme']) || !in_array($parsed['scheme'], array('http','https')) ) {
  424. $url = 'http://' . $url;
  425. }
  426. if ( ini_get('allow_url_fopen') ) {
  427. if($fp = @fopen( $url, 'r' )) {
  428. @stream_set_timeout($fp, $timeout);
  429. $result = '';
  430. while( $remote_read = fread($fp, 4096) ) $result .= $remote_read;
  431. fclose($fp);
  432. return $result;
  433. }
  434. return false;
  435. } elseif ( function_exists('curl_init') ) {
  436. $handle = curl_init();
  437. curl_setopt ($handle, CURLOPT_URL, $url);
  438. curl_setopt ($handle, CURLOPT_CONNECTTIMEOUT, 1);
  439. curl_setopt ($handle, CURLOPT_RETURNTRANSFER, 1);
  440. curl_setopt ($handle, CURLOPT_TIMEOUT, $timeout);
  441. $result= curl_exec($handle);
  442. curl_close($handle);
  443. return $result;
  444. } else {
  445. return false;
  446. }
  447. }
  448. }//class
  449. // part of lib/updater.class.php
  450. class download {
  451. public static function getlatest() {
  452. if (($s = http::get('http://litepublisher.com/service/version.txt')) ||
  453. ($s = http::get('http://litepublisher.googlecode.com/files/version.txt') )) {
  454. return $s;
  455. }
  456. return false;
  457. }
  458. public static function install() {
  459. $dir = dirname(__file__) . DIRECTORY_SEPARATOR;
  460. //test write
  461. if (!file_put_contents($dir. 'test.php', ' ')) die('Error write to test.php');
  462. chmod($dir. 'test.php', 0666);
  463. unlink($dir . 'test.php');
  464. if ($version = self::getlatest()) {
  465. if (($s = http::get("http://litepublisher.googlecode.com/files/litepublisher.$version.tar.gz")) ||
  466. ($s = http::get("http://litepublisher.com/download/litepublisher.$version.tar.gz") )) {
  467. $tar = new tar();
  468. $tar->loadfromstring($s);
  469. foreach ($tar->files as $file) {
  470. $filename = $dir . str_replace('/', DIRECTORY_SEPARATOR, $file['name']);
  471. if (!self::forcedir(dirname($filename))) die("error create folder " . dirname($filename));
  472. if (false === @file_put_contents($filename, $file['file'])) die(sprintf('Error write file %s', $filename));
  473. @chmod($filename, 0666);
  474. }
  475. return true;
  476. }
  477. }
  478. die('Error download last release');
  479. }
  480. public static function forcedir($dir) {
  481. $dir = rtrim($dir, DIRECTORY_SEPARATOR);
  482. if (is_dir($dir)) return true;
  483. $up = rtrim(dirname($dir), DIRECTORY_SEPARATOR);
  484. if (($up != '') || ($up != '.')) self::forcedir($up);
  485. if (!is_dir($dir)) mkdir($dir, 0777);
  486. chmod($dir, 0777);
  487. return is_dir($dir);
  488. }
  489. }//class
  490. if (download::install()) {
  491. header('Location: http://'. $_SERVER['HTTP_HOST'] . '/');
  492. exit();
  493. }
  494. echo "Cant install";
  495. ?>