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

/utils/reinstall.php

http://litepublisher.googlecode.com/
PHP | 933 lines | 640 code | 146 blank | 147 comment | 112 complexity | 719c2a48b6d0816ff718bbc54b5a737d MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0
  1. <?php
  2. require('db.php');
  3. //lib/include/tar.class.php
  4. /* special changes and bug fixes by Vladimir Yushko
  5. http://litepublisher.com/
  6. */
  7. /*
  8. =======================================================================
  9. Name:
  10. tar Class
  11. Author:
  12. Josh Barger <joshb@npt.com>
  13. Description:
  14. This class reads and writes Tape-Archive (TAR) Files and Gzip
  15. compressed TAR files, which are mainly used on UNIX systems.
  16. This class works on both windows AND unix systems, and does
  17. NOT rely on external applications!! Woohoo!
  18. Usage:
  19. Copyright (C) 2002 Josh Barger
  20. This library is free software; you can redistribute it and/or
  21. modify it under the terms of the GNU Lesser General Public
  22. License as published by the Free Software Foundation; either
  23. version 2.1 of the License, or (at your option) any later version.
  24. This library is distributed in the hope that it will be useful,
  25. but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. Lesser General Public License for more details at:
  28. http://www.gnu.org/copyleft/lesser.html
  29. If you use this script in your application/website, please
  30. send me an e-mail letting me know about it :)
  31. Bugs:
  32. Please report any bugs you might find to my e-mail address
  33. at joshb@npt.com. If you have already created a fix/patch
  34. for the bug, please do send it to me so I can incorporate it into my release.
  35. Version History:
  36. 1.0 04/10/2002 - InitialRelease
  37. 2.0 04/11/2002 - Merged both tarReader and tarWriter
  38. classes into one
  39. - Added support for gzipped tar files
  40. Remember to name for .tar.gz or .tgz
  41. if you use gzip compression!
  42. :: THIS REQUIRES ZLIB EXTENSION ::
  43. - Added additional comments to
  44. functions to help users
  45. - Added ability to remove files and
  46. directories from archive
  47. 2.1 04/12/2002 - Fixed serious bug in generating tar
  48. - Created another example file
  49. - Added check to make sure ZLIB is
  50. installed before running GZIP
  51. compression on TAR
  52. 2.2 05/07/2002 - Added automatic detection of Gzipped
  53. tar files (Thanks go to J?gen Falch
  54. for the idea)
  55. - Changed "private" functions to have
  56. special function names beginning with
  57. two underscores
  58. =======================================================================
  59. */
  60. class tar {
  61. // Unprocessed Archive Information
  62. var $filename;
  63. var $isGzipped;
  64. var $tar_file;
  65. // Processed Archive Information
  66. var $files;
  67. var $directories;
  68. var $numFiles;
  69. var $numDirectories;
  70. // Class Constructor -- Does nothing...
  71. function tar() {
  72. return true;
  73. }
  74. // Computes the unsigned Checksum of a file's header
  75. // to try to ensure valid file
  76. // PRIVATE ACCESS FUNCTION
  77. function __computeUnsignedChecksum($bytestring) {
  78. $unsigned_chksum = 0;
  79. for($i=0; $i<512; $i++)
  80. $unsigned_chksum += ord($bytestring[$i]);
  81. for($i=0; $i<8; $i++)
  82. $unsigned_chksum -= ord($bytestring[148 + $i]);
  83. $unsigned_chksum += ord(" ") * 8;
  84. return $unsigned_chksum;
  85. }
  86. // Converts a NULL padded string to a non-NULL padded string
  87. // PRIVATE ACCESS FUNCTION
  88. function __parseNullPaddedString($string) {
  89. $position = strpos($string,chr(0));
  90. return substr($string,0,$position);
  91. }
  92. // This function parses the current TAR file
  93. // PRIVATE ACCESS FUNCTION
  94. function __parseTar() {
  95. // Read Files from archive
  96. $tar_length = strlen($this->tar_file);
  97. $main_offset = 0;
  98. while($main_offset < $tar_length) {
  99. // If we read a block of 512 nulls, we are at the end of the archive
  100. if(substr($this->tar_file,$main_offset,512) == str_repeat(chr(0),512))
  101. break;
  102. // Parse file name
  103. $file_name = $this->__parseNullPaddedString(substr($this->tar_file,$main_offset,100));
  104. // Parse the file mode
  105. $file_mode = substr($this->tar_file,$main_offset + 100,8);
  106. // Parse the file user ID
  107. $file_uid = octdec(substr($this->tar_file,$main_offset + 108,8));
  108. // Parse the file group ID
  109. $file_gid = octdec(substr($this->tar_file,$main_offset + 116,8));
  110. // Parse the file size
  111. $file_size = octdec(substr($this->tar_file,$main_offset + 124,12));
  112. // Parse the file update time - unix timestamp format
  113. $file_time = octdec(substr($this->tar_file,$main_offset + 136,12));
  114. // Parse Checksum
  115. $file_chksum = octdec(substr($this->tar_file,$main_offset + 148,6));
  116. // Parse user name
  117. $file_uname = $this->__parseNullPaddedString(substr($this->tar_file,$main_offset + 265,32));
  118. // Parse Group name
  119. $file_gname = $this->__parseNullPaddedString(substr($this->tar_file,$main_offset + 297,32));
  120. // Make sure our file is valid
  121. if($this->__computeUnsignedChecksum(substr($this->tar_file,$main_offset,512)) != $file_chksum)
  122. return false;
  123. // Parse File Contents
  124. $file_contents = substr($this->tar_file,$main_offset + 512,$file_size);
  125. /* ### Unused Header Information ###
  126. $activeFile["typeflag"] = substr($this->tar_file,$main_offset + 156,1);
  127. $activeFile["linkname"] = substr($this->tar_file,$main_offset + 157,100);
  128. $activeFile["magic"] = substr($this->tar_file,$main_offset + 257,6);
  129. $activeFile["version"] = substr($this->tar_file,$main_offset + 263,2);
  130. $activeFile["devmajor"] = substr($this->tar_file,$main_offset + 329,8);
  131. $activeFile["devminor"] = substr($this->tar_file,$main_offset + 337,8);
  132. $activeFile["prefix"] = substr($this->tar_file,$main_offset + 345,155);
  133. $activeFile["endheader"] = substr($this->tar_file,$main_offset + 500,12);
  134. */
  135. if($file_size > 0) {
  136. // Increment number of files
  137. $this->numFiles++;
  138. // Create us a new file in our array
  139. $activeFile = &$this->files[];
  140. // Asign Values
  141. $activeFile["name"] = $file_name;
  142. $activeFile["mode"] = $file_mode;
  143. $activeFile["size"] = $file_size;
  144. $activeFile["time"] = $file_time;
  145. $activeFile["user_id"] = $file_uid;
  146. $activeFile["group_id"] = $file_gid;
  147. $activeFile["user_name"] = $file_uname;
  148. $activeFile["group_name"] = $file_gname;
  149. $activeFile["checksum"] = $file_chksum;
  150. $activeFile["file"] = $file_contents;
  151. } else {
  152. // Increment number of directories
  153. $this->numDirectories++;
  154. // Create a new directory in our array
  155. $activeDir = &$this->directories[];
  156. // Assign values
  157. $activeDir["name"] = $file_name;
  158. $activeDir["mode"] = $file_mode;
  159. $activeDir["time"] = $file_time;
  160. $activeDir["user_id"] = $file_uid;
  161. $activeDir["group_id"] = $file_gid;
  162. $activeDir["user_name"] = $file_uname;
  163. $activeDir["group_name"] = $file_gname;
  164. $activeDir["checksum"] = $file_chksum;
  165. }
  166. // Move our offset the number of blocks we have processed
  167. $main_offset += 512 + (ceil($file_size / 512) * 512);
  168. }
  169. return true;
  170. }
  171. public function loadfromstring($s) {
  172. // Clear any values from previous tar archives
  173. unset($this->filename);
  174. unset($this->isGzipped);
  175. unset($this->tar_file);
  176. unset($this->files);
  177. unset($this->directories);
  178. unset($this->numFiles);
  179. unset($this->numDirectories);
  180. $this->tar_file = $s;
  181. if($this->tar_file[0] == chr(31) && $this->tar_file[1] == chr(139) && $this->tar_file[2] == chr(8)) {
  182. $this->isGzipped = TRUE;
  183. $this->tar_file = gzinflate(substr($this->tar_file,10,-4));
  184. }
  185. // Parse the TAR file
  186. $this->__parseTar();
  187. return true;
  188. }
  189. // Generates a TAR file from the processed data
  190. // PRIVATE ACCESS FUNCTION
  191. function __generateTAR() {
  192. // Clear any data currently in $this->tar_file
  193. unset($this->tar_file);
  194. // Generate Records for each directory, if we have directories
  195. if($this->numDirectories > 0) {
  196. foreach($this->directories as $key => $information) {
  197. //unset($header);
  198. // Generate tar header for this directory
  199. // Filename, Permissions, UID, GID, size, Time, checksum, typeflag, linkname, magic, version, user name, group name, devmajor, devminor, prefix, end
  200. $header = str_pad($information["name"],100,chr(0));
  201. $header .= str_pad(decoct($information["mode"]),7,"0",STR_PAD_LEFT) . chr(0);
  202. $header .= str_pad(decoct($information["user_id"]),7,"0",STR_PAD_LEFT) . chr(0);
  203. $header .= str_pad(decoct($information["group_id"]),7,"0",STR_PAD_LEFT) . chr(0);
  204. $header .= str_pad(decoct(0),11,"0",STR_PAD_LEFT) . chr(0);
  205. $header .= str_pad(decoct($information["time"]),11,"0",STR_PAD_LEFT) . chr(0);
  206. $header .= str_repeat(" ",8);
  207. $header .= "5";
  208. $header .= str_repeat(chr(0),100);
  209. $header .= str_pad("ustar",6,chr(32));
  210. $header .= chr(32) . chr(0);
  211. $header .= str_pad("",32,chr(0));
  212. $header .= str_pad("",32,chr(0));
  213. $header .= str_repeat(chr(0),8);
  214. $header .= str_repeat(chr(0),8);
  215. $header .= str_repeat(chr(0),155);
  216. $header .= str_repeat(chr(0),12);
  217. // Compute header checksum
  218. $checksum = str_pad(decoct($this->__computeUnsignedChecksum($header)),6,"0",STR_PAD_LEFT);
  219. for($i=0; $i<6; $i++) {
  220. $header[(148 + $i)] = substr($checksum,$i,1);
  221. }
  222. $header[154] = chr(0);
  223. $header[155] = chr(32);
  224. // Add new tar formatted data to tar file contents
  225. $this->tar_file .= $header;
  226. }
  227. }
  228. // Generate Records for each file, if we have files (We should...)
  229. if($this->numFiles > 0) {
  230. foreach($this->files as $key => $information) {
  231. //unset($header);
  232. // Generate the TAR header for this file
  233. // Filename, Permissions, UID, GID, size, Time, checksum, typeflag, linkname, magic, version, user name, group name, devmajor, devminor, prefix, end
  234. $header = str_pad($information["name"],100,chr(0));
  235. $header .= str_pad(decoct($information["mode"]),7,"0",STR_PAD_LEFT) . chr(0);
  236. $header .= str_pad(decoct($information["user_id"]),7,"0",STR_PAD_LEFT) . chr(0);
  237. $header .= str_pad(decoct($information["group_id"]),7,"0",STR_PAD_LEFT) . chr(0);
  238. $header .= str_pad(decoct($information["size"]),11,"0",STR_PAD_LEFT) . chr(0);
  239. $header .= str_pad(decoct($information["time"]),11,"0",STR_PAD_LEFT) . chr(0);
  240. $header .= str_repeat(" ",8);
  241. $header .= "0";
  242. $header .= str_repeat(chr(0),100);
  243. $header .= str_pad("ustar",6,chr(32));
  244. $header .= chr(32) . chr(0);
  245. $header .= str_pad($information["user_name"],32,chr(0)); // How do I get a file's user name from PHP?
  246. $header .= str_pad($information["group_name"],32,chr(0)); // How do I get a file's group name from PHP?
  247. $header .= str_repeat(chr(0),8);
  248. $header .= str_repeat(chr(0),8);
  249. $header .= str_repeat(chr(0),155);
  250. $header .= str_repeat(chr(0),12);
  251. // Compute header checksum
  252. $checksum = str_pad(decoct($this->__computeUnsignedChecksum($header)),6,"0",STR_PAD_LEFT);
  253. for($i=0; $i<6; $i++) {
  254. $header[(148 + $i)] = substr($checksum,$i,1);
  255. }
  256. $header[154] = chr(0);
  257. $header[155] = chr(32);
  258. // Pad file contents to byte count divisible by 512
  259. $file_contents = str_pad($information["file"],(ceil($information["size"] / 512) * 512),chr(0));
  260. // Add new tar formatted data to tar file contents
  261. $this->tar_file .= $header . $file_contents;
  262. }
  263. }
  264. // Add 512 bytes of NULLs to designate EOF
  265. $this->tar_file .= str_repeat(chr(0),512);
  266. return true;
  267. }
  268. public function loadfromfile($filename) {
  269. if(!file_exists($filename)) return false;
  270. $this->filename = $filename;
  271. return $this->loadfromstring(file_get_contents($filename));
  272. }
  273. // Appends a tar file to the end of the currently opened tar file
  274. function appendTar($filename) {
  275. // If the tar file doesn't exist...
  276. if(!file_exists($filename))
  277. return false;
  278. $this->__readTar($filename);
  279. return true;
  280. }
  281. // Retrieves information about a file in the current tar archive
  282. function getFile($filename) {
  283. if($this->numFiles > 0) {
  284. foreach($this->files as $key => $information) {
  285. if($information["name"] == $filename)
  286. return $information;
  287. }
  288. }
  289. return false;
  290. }
  291. // Retrieves information about a directory in the current tar archive
  292. function getDirectory($dirname) {
  293. if($this->numDirectories > 0) {
  294. foreach($this->directories as $key => $information) {
  295. if($information["name"] == $dirname)
  296. return $information;
  297. }
  298. }
  299. return false;
  300. }
  301. // Check if this tar archive contains a specific file
  302. function containsFile($filename) {
  303. if($this->numFiles > 0) {
  304. foreach($this->files as $key => $information) {
  305. if($information["name"] == $filename)
  306. return true;
  307. }
  308. }
  309. return false;
  310. }
  311. // Check if this tar archive contains a specific directory
  312. function containsDirectory($dirname) {
  313. if($this->numDirectories > 0) {
  314. foreach($this->directories as $key => $information) {
  315. if($information["name"] == $dirname)
  316. return true;
  317. }
  318. }
  319. return false;
  320. }
  321. // Add a directory to this tar archive
  322. function adddir($dirname, $perm = 0777) {
  323. $this->numDirectories++;
  324. $activeDir = &$this->directories[];
  325. $activeDir["name"] = $dirname;
  326. $activeDir["mode"] = $perm;
  327. $activeDir["time"] = time();
  328. $activeDir["user_id"] = 0;
  329. $activeDir["group_id"] = 0;
  330. $activeDir["user_name"] = "";
  331. $activeDir["group_name"] = "";
  332. $activeDir["checksum"] = 0;
  333. return true;
  334. }
  335. // Add a file to the tar archive
  336. public function add($realfile, $filename, $perm = 0666) {
  337. if($this->containsFile($filename)) return false;
  338. $file_information = stat($realfile);
  339. if (($perm == 0) && (DIRECTORY_SEPARATOR == '/')) $perm = $file_information["mode"] == 0 ? $perm : $file_information["mode"];
  340. // Read in the file's contents
  341. $file_contents = file_get_contents($realfile);
  342. // Add file to processed data
  343. $checksum = 0;
  344. $this->numFiles++;
  345. $activeFile = &$this->files[];
  346. $activeFile["name"] = $filename;
  347. $activeFile["mode"] = $perm;
  348. $activeFile["user_id"] = $file_information["uid"]; // == 0 ? 33 : $file_information["uid"];
  349. $activeFile["group_id"] = $file_information["gid"]; // == 0 ? 33 : $file_information["gid"];
  350. $activeFile["user_name"] = "";
  351. $activeFile["group_name"] = "";
  352. $activeFile["size"] = strlen($file_contents);
  353. $activeFile["time"] = $file_information["mtime"];
  354. $activeFile["checksum"] = 0;
  355. $activeFile["file"] = $file_contents;
  356. return true;
  357. }
  358. public function addstring($s, $filename, $perm = 0666) {
  359. if($this->containsFile($filename)) return false;
  360. // Add file to processed data
  361. $this->numFiles++;
  362. $activeFile = &$this->files[];
  363. $activeFile["name"] = $filename;
  364. $activeFile["mode"] = $perm;
  365. $activeFile["user_id"] = 0;
  366. $activeFile["group_id"] = 0;
  367. $activeFile["size"] = strlen($s);
  368. $activeFile["time"] = time();
  369. $activeFile["checksum"] = 0;
  370. $activeFile["user_name"] = "";
  371. $activeFile["group_name"] = "";
  372. $activeFile["file"] = $s;
  373. return true;
  374. }
  375. // Remove a file from the tar archive
  376. function removeFile($filename) {
  377. if($this->numFiles > 0) {
  378. foreach($this->files as $key => $information) {
  379. if($information["name"] == $filename) {
  380. $this->numFiles--;
  381. unset($this->files[$key]);
  382. return true;
  383. }
  384. }
  385. }
  386. return false;
  387. }
  388. // Remove a directory from the tar archive
  389. function removeDirectory($dirname) {
  390. if($this->numDirectories > 0) {
  391. foreach($this->directories as $key => $information) {
  392. if($information["name"] == $dirname) {
  393. $this->numDirectories--;
  394. unset($this->directories[$key]);
  395. return true;
  396. }
  397. }
  398. }
  399. return false;
  400. }
  401. // Write the currently loaded tar archive to disk
  402. function saveTar() {
  403. if(!$this->filename)
  404. return false;
  405. // Write tar to current file using specified gzip compression
  406. $this->toTar($this->filename,$this->isGzipped);
  407. return true;
  408. }
  409. // Saves tar archive to a different file than the current file
  410. function savetofile($filename,$useGzip) {
  411. return file_put_contents($filename, $this->savetostring($useGzip));
  412. }
  413. function savetostring($useGzip) {
  414. $this->__generateTar();
  415. return$useGzip ? gzencode($this->tar_file) : $this->tar_file;
  416. }
  417. }
  418. // lib/http.class.php
  419. class http {
  420. public static function get($url) {
  421. $timeout = 5;
  422. $parsed = @parse_url($url);
  423. if ( !$parsed || !is_array($parsed) ) return false;
  424. if ( !isset($parsed['scheme']) || !in_array($parsed['scheme'], array('http','https')) ) {
  425. $url = 'http://' . $url;
  426. }
  427. if ( ini_get('allow_url_fopen') ) {
  428. if($fp = @fopen( $url, 'r' )) {
  429. @stream_set_timeout($fp, $timeout);
  430. $result = '';
  431. while( $remote_read = fread($fp, 4096) ) $result .= $remote_read;
  432. fclose($fp);
  433. return $result;
  434. }
  435. return false;
  436. } elseif ( function_exists('curl_init') ) {
  437. $handle = curl_init();
  438. curl_setopt ($handle, CURLOPT_URL, $url);
  439. curl_setopt ($handle, CURLOPT_CONNECTTIMEOUT, 1);
  440. curl_setopt ($handle, CURLOPT_RETURNTRANSFER, 1);
  441. curl_setopt ($handle, CURLOPT_TIMEOUT, $timeout);
  442. $result= curl_exec($handle);
  443. curl_close($handle);
  444. return $result;
  445. } else {
  446. return false;
  447. }
  448. }
  449. }//class
  450. // part of lib/updater.class.php
  451. class download {
  452. public static function getlatest() {
  453. if (($s = http::get('http://litepublisher.com/service/version.txt')) ||
  454. ($s = http::get('http://litepublisher.googlecode.com/files/version.txt') )) {
  455. return $s;
  456. }
  457. return false;
  458. }
  459. public static function install() {
  460. $dir = dirname(__file__) . DIRECTORY_SEPARATOR;
  461. //test write
  462. if (!file_put_contents($dir. 'test.php', ' ')) die('Error write to test.php');
  463. chmod($dir. 'test.php', 0666);
  464. unlink($dir . 'test.php');
  465. if ($version = self::getlatest()) {
  466. if (($s = http::get("http://litepublisher.googlecode.com/files/litepublisher.$version.tar.gz")) ||
  467. ($s = http::get("http://litepublisher.com/download/litepublisher.$version.tar.gz") )) {
  468. $tar = new tar();
  469. $tar->loadfromstring($s);
  470. foreach ($tar->files as $file) {
  471. $filename = $dir . str_replace('/', DIRECTORY_SEPARATOR, $file['name']);
  472. if (!self::forcedir(dirname($filename))) die("error create folder " . dirname($filename));
  473. if (false === @file_put_contents($filename, $file['file'])) die(sprintf('Error write file %s', $filename));
  474. @chmod($filename, 0666);
  475. }
  476. return true;
  477. }
  478. }
  479. die('Error download last release');
  480. }
  481. public static function forcedir($dir) {
  482. $dir = rtrim($dir, DIRECTORY_SEPARATOR);
  483. if (is_dir($dir)) return true;
  484. $up = rtrim(dirname($dir), DIRECTORY_SEPARATOR);
  485. if (($up != '') || ($up != '.')) self::forcedir($up);
  486. if (!is_dir($dir)) mkdir($dir, 0777);
  487. chmod($dir, 0777);
  488. return is_dir($dir);
  489. }
  490. }//class
  491. //lib/data.class.php
  492. class tdata {
  493. public static $savedisabled;
  494. public $basename;
  495. public $cache;
  496. public $coclasses;
  497. public $coinstances;
  498. public $data;
  499. public $lockcount;
  500. public $table;
  501. public function __construct() {
  502. $this->lockcount = 0;
  503. $this->cache= true;
  504. $this->data= array();
  505. $this->coinstances = array();
  506. $this->coclasses = array();
  507. $this->basename = substr(get_class($this), 1);
  508. $this->create();
  509. }
  510. protected function create() {
  511. }
  512. public function __get($name) {
  513. if (method_exists($this, $get = "get$name")) {
  514. return $this->$get();
  515. } elseif (array_key_exists($name, $this->data)) {
  516. return $this->data[$name];
  517. } else {
  518. foreach ($this->coinstances as $coinstance) {
  519. if (isset($coinstance->$name)) return $coinstance->$name;
  520. }
  521. return $this->error("The requested property $name not found in class ". get_class($this));
  522. }
  523. }
  524. public function __set($name, $value) {
  525. if (method_exists($this, $set = "set$name")) {
  526. $this->$set($value);
  527. return true;
  528. }
  529. if (key_exists($name, $this->data)) {
  530. $this->data[$name] = $value;
  531. return true;
  532. }
  533. foreach ($this->coinstances as $coinstance) {
  534. if (isset($coinstance->$name)) {
  535. $coinstance->$name = $value;
  536. return true;
  537. }
  538. }
  539. return false;
  540. }
  541. public function __call($name, $params) {
  542. if (method_exists($this, strtolower($name))) {
  543. return call_user_func_array(array($this, strtolower($name)), $params);
  544. }
  545. foreach ($this->coinstances as $coinstance) {
  546. if (method_exists($coinstance, $name)) return call_user_func_array(array($coinstance, $name), $params);
  547. }
  548. $this->error("The requested method $name not found in class " . get_class($this));
  549. }
  550. public function __isset($name) {
  551. return array_key_exists($name, $this->data) || method_exists($this, "get$name") | method_exists($this, "Get$name");
  552. }
  553. public function error($Msg) {
  554. throw new Exception($Msg);
  555. }
  556. public function getbasename() {
  557. return $this->basename;
  558. }
  559. public function install() {
  560. $this->externalchain('Install');
  561. }
  562. public function uninstall() {
  563. $this->externalchain('Uninstall');
  564. }
  565. public function validate($repair = false) {
  566. $this->externalchain('Validate', $repair);
  567. }
  568. protected function externalchain($func, $arg = null) {
  569. $parents = class_parents($this);
  570. array_splice($parents, 0, 0, get_class($this));
  571. foreach ($parents as $key => $class) {
  572. $this->externalfunc($class, $func, $arg);
  573. }
  574. }
  575. protected function externalfunc($class, $func, $arg) {
  576. if ($filename = litepublisher::$classes->getclassfilename($class)) {
  577. $externalname = basename($filename, '.php') . '.install.php';
  578. $dir = dirname($filename) . DIRECTORY_SEPARATOR;
  579. $file = $dir . 'install' . DIRECTORY_SEPARATOR . $externalname;
  580. if (!file_exists($file)) {
  581. $file =$dir . $externalname;
  582. if (!file_exists($file)) return;
  583. }
  584. include_once($file);
  585. $fnc = $class . $func;
  586. if (function_exists($fnc)) $fnc($this, $arg);
  587. }
  588. }
  589. public function load() {
  590. if ($this->dbversion == 'full') return $this->LoadFromDB();
  591. $filename = litepublisher::$paths->data . $this->getbasename() .'.php';
  592. if (file_exists($filename)) {
  593. return $this->loadfromstring(self::uncomment_php(file_get_contents($filename)));
  594. }
  595. }
  596. public function save() {
  597. if (self::$savedisabled || ($this->lockcount > 0)) return;
  598. if ($this->dbversion) {
  599. $this->SaveToDB();
  600. } else {
  601. self::savetofile(litepublisher::$paths->data .$this->getbasename(), self::comment_php($this->savetostring()));
  602. }
  603. }
  604. public function savetostring() {
  605. return serialize($this->data);
  606. }
  607. public function loadfromstring($s) {
  608. try {
  609. if (!empty($s)) $this->data = unserialize($s) + $this->data;
  610. $this->afterload();
  611. return true;
  612. } catch (Exception $e) {
  613. echo 'Caught exception: '. $e->getMessage() ;
  614. return false;
  615. }
  616. }
  617. public function afterload() {
  618. }
  619. public function lock() {
  620. $this->lockcount++;
  621. }
  622. public function unlock() {
  623. if (--$this->lockcount <= 0) $this->save();
  624. }
  625. public function getlocked() {
  626. return $this->lockcount > 0;
  627. }
  628. public function Getclass() {
  629. return get_class($this);
  630. }
  631. public function getdbversion() {
  632. return false; // dbversion == 'full';
  633. }
  634. public function getdb($table = '') {
  635. $table =$table != '' ? $table : $this->table;
  636. if ($table != '') litepublisher::$db->table = $table;
  637. return litepublisher::$db;
  638. }
  639. protected function SaveToDB() {
  640. $this->db->add($this->getbasename(), $this->savetostring());
  641. }
  642. protected function LoadFromDB() {
  643. if ($r = $this->db->select('basename = '. $this->getbasename() . "'")) {
  644. return $this->loadfromstring($r['data']);
  645. }
  646. }
  647. protected function getthistable() {
  648. return litepublisher::$db->prefix . $this->table;
  649. }
  650. public static function savetofile($base, $content) {
  651. $tmp = $base .'.tmp.php';
  652. if(false === file_put_contents($tmp, $content)) {
  653. litepublisher::$options->trace("Error write to file $tmp");
  654. return false;
  655. }
  656. chmod($tmp, 0666);
  657. $filename = $base .'.php';
  658. if (file_exists($filename)) {
  659. $back = $base . '.bak.php';
  660. if (file_exists($back)) unlink($back);
  661. rename($filename, $back);
  662. }
  663. if (!rename($tmp, $filename)) {
  664. litepublisher::$options->trace("Error rename file $tmp to $filename");
  665. return false;
  666. }
  667. return true;
  668. }
  669. public static function comment_php($s) {
  670. return sprintf('<?php /* %s */ ?>', str_replace('*/', '**//*/', $s));
  671. }
  672. public static function uncomment_php($s) {
  673. return str_replace('**//*/', '*/', substr($s, 9, strlen($s) - 9 - 6));
  674. }
  675. }//class
  676. class tarray2prop {
  677. public $array;
  678. public function __get($name) { return $this->array[$name]; }
  679. public function __set($name, $value) { $this->array[$name] = $value; }
  680. public function __tostring() { return $this->array[0]; }
  681. public function __isset($name) { return array_key_exists($name, $this->array); }
  682. }//class
  683. function sqldate($date = 0) {
  684. if ($date == 0) $date = time();
  685. return date('Y-m-d H:i:s', $date);
  686. }
  687. function dbquote($s) {
  688. return litepublisher::$db->quote($s);
  689. }
  690. function md5uniq() {
  691. return md5(mt_rand() . litepublisher::$secret. microtime());
  692. }
  693. function strbegin($s, $begin) {
  694. return strncmp($s, $begin, strlen($begin)) == 0;
  695. }
  696. function strend($s, $end) {
  697. return $end == substr($s, 0 - strlen($end));
  698. }
  699. function array_delete(array &$a, $i) {
  700. array_splice($a, $i, 1);
  701. }
  702. function array_delete_value(array &$a, $value) {
  703. $i = array_search($value, $a);
  704. if ($i !== false) array_splice($a, $i, 1);
  705. }
  706. function array_insert(array &$a, $item, $index) {
  707. array_splice($a, $index, 0, array($item));
  708. }
  709. function array_move(array &$a, $oldindex, $newindex) {
  710. //delete and insert
  711. if (($oldindex == $newindex) || !isset($a[$oldindex])) return false;
  712. $item = $a[$oldindex];
  713. array_splice($a, $oldindex, 1);
  714. array_splice($a, $newindex, 0, array($item));
  715. }
  716. function dumpstr($s) {
  717. echo "<pre>\n" . htmlspecialchars($s) . "</pre>\n";
  718. }
  719. class tmigratedata extends tdata {
  720. public static $dir;
  721. public function loadfile($name) {
  722. $this->data = array();
  723. $filename = self::$dir . $name . '.php';
  724. if (file_exists($filename)) {
  725. return $this->loadfromstring(self::uncomment_php(file_get_contents($filename)));
  726. }
  727. }
  728. }//class
  729. function movefolders() {
  730. $home = dirname(__file__) . DIRECTORY_SEPARATOR;
  731. $backup = $home . 'backup' . DIRECTORY_SEPARATOR . date('H-i-s.d.m.Y');
  732. mkdir($backup, 0777);
  733. chmod($backup, 0777);
  734. $backup .= DIRECTORY_SEPARATOR;
  735. foreach (array('lib', 'plugins', 'themes') as $name) {
  736. if (is_dir($home . $name)) rename($home . $name, $backup . $name);
  737. }
  738. $data = $home . 'data' . DIRECTORY_SEPARATOR . $_SERVER['HTTP_HOST'];
  739. $old = $home . 'data' . DIRECTORY_SEPARATOR . 'old';
  740. if (is_dir($data) && !is_dir($old)) rename($data, $old);
  741. tmigratedata::$dir =$old . DIRECTORY_SEPARATOR;
  742. }
  743. set_time_limit(120);
  744. date_default_timezone_set("Europe/Moscow");
  745. movefolders();
  746. $data = new tmigratedata();
  747. $data->loadfile('options');
  748. if (download::install()) {
  749. $params = sprintf('&lang=%s&name=%s&description=%s&email=%s', rawurlencode($data->language), rawurlencode($data->name), rawurlencode($data->description), rawurlencode($data->email));
  750. if (isset($dbversion) && $dbversion) {
  751. //test connect
  752. $host= isset($dbhost) ? $dbhost : 'localhost';
  753. if (isset($dbport)) $host .= ':' . $dbport;
  754. $handle = mysql_connect($host, $dblogin, $dbpassword);
  755. if (! $handle) {
  756. die('Error connect to database');
  757. }
  758. if (! mysql_select_db($dbname, $handle)) {
  759. die('Error select database');
  760. }
  761. $params .= "&dbversion=1&dbname=$dbname&dblogin=$dblogin&dbpassword=$dbpassword&dbprefix=$dbprefix";
  762. if (!isset($dbhost)) {
  763. $params .= '&usehost=0';
  764. } else {
  765. $params .= "&usehost=1&dbhost=$dbhost&dbport=$dbport";
  766. }
  767. }
  768. if ($s = http::get('http://'. $_SERVER['HTTP_HOST'] . '/?mode=remote&lite=1&resulttype=serialized' . $params)) {
  769. $info = unserialize($s);
  770. header('Location: http://'. $_SERVER['HTTP_HOST'] . '/migrate.php');
  771. exit();
  772. }
  773. }
  774. echo "Not installed";
  775. ?>