PageRenderTime 59ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/otros/anuncios/admin/backup_compress.php

https://bitbucket.org/meerdevelopersoffice/bolivia
PHP | 492 lines | 397 code | 68 blank | 27 comment | 43 complexity | 3333fbcd3ad1b07957f31ce4ba20b03d MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * Class to dynamically create a zip file (archive)
  4. *
  5. * @author Rochak Chauhan
  6. */
  7. class createZip {
  8. public $compressedData = array();
  9. public $centralDirectory = array(); // central directory
  10. public $endOfCentralDirectory = "\x50\x4b\x05\x06\x00\x00\x00\x00"; //end of Central directory record
  11. public $oldOffset = 0;
  12. /**
  13. * Function to create the directory where the file(s) will be unzipped
  14. *
  15. * @param $directoryName string
  16. *
  17. */
  18. public function addDirectory($directoryName) {
  19. $directoryName = str_replace("\\", "/", $directoryName);
  20. $feedArrayRow = "\x50\x4b\x03\x04";
  21. $feedArrayRow .= "\x0a\x00";
  22. $feedArrayRow .= "\x00\x00";
  23. $feedArrayRow .= "\x00\x00";
  24. $feedArrayRow .= "\x00\x00\x00\x00";
  25. $feedArrayRow .= pack("V",0);
  26. $feedArrayRow .= pack("V",0);
  27. $feedArrayRow .= pack("V",0);
  28. $feedArrayRow .= pack("v", strlen($directoryName) );
  29. $feedArrayRow .= pack("v", 0 );
  30. $feedArrayRow .= $directoryName;
  31. $feedArrayRow .= pack("V",0);
  32. $feedArrayRow .= pack("V",0);
  33. $feedArrayRow .= pack("V",0);
  34. $this -> compressedData[] = $feedArrayRow;
  35. $newOffset = strlen(implode("", $this->compressedData));
  36. $addCentralRecord = "\x50\x4b\x01\x02";
  37. $addCentralRecord .="\x00\x00";
  38. $addCentralRecord .="\x0a\x00";
  39. $addCentralRecord .="\x00\x00";
  40. $addCentralRecord .="\x00\x00";
  41. $addCentralRecord .="\x00\x00\x00\x00";
  42. $addCentralRecord .= pack("V",0);
  43. $addCentralRecord .= pack("V",0);
  44. $addCentralRecord .= pack("V",0);
  45. $addCentralRecord .= pack("v", strlen($directoryName) );
  46. $addCentralRecord .= pack("v", 0 );
  47. $addCentralRecord .= pack("v", 0 );
  48. $addCentralRecord .= pack("v", 0 );
  49. $addCentralRecord .= pack("v", 0 );
  50. $ext = "\x00\x00\x10\x00";
  51. $ext = "\xff\xff\xff\xff";
  52. $addCentralRecord .= pack("V", 16 );
  53. $addCentralRecord .= pack("V", $this -> oldOffset );
  54. $this -> oldOffset = $newOffset;
  55. $addCentralRecord .= $directoryName;
  56. $this -> centralDirectory[] = $addCentralRecord;
  57. }
  58. /**
  59. * Function to add file(s) to the specified directory in the archive
  60. *
  61. * @param $directoryName string
  62. *
  63. */
  64. public function addFile($data, $directoryName) {
  65. $directoryName = str_replace("\\", "/", $directoryName);
  66. $feedArrayRow = "\x50\x4b\x03\x04";
  67. $feedArrayRow .= "\x14\x00";
  68. $feedArrayRow .= "\x00\x00";
  69. $feedArrayRow .= "\x08\x00";
  70. $feedArrayRow .= "\x00\x00\x00\x00";
  71. $uncompressedLength = strlen($data);
  72. $compression = crc32($data);
  73. $gzCompressedData = gzcompress($data);
  74. $gzCompressedData = substr( substr($gzCompressedData, 0, strlen($gzCompressedData) - 4), 2);
  75. $compressedLength = strlen($gzCompressedData);
  76. $feedArrayRow .= pack("V",$compression);
  77. $feedArrayRow .= pack("V",$compressedLength);
  78. $feedArrayRow .= pack("V",$uncompressedLength);
  79. $feedArrayRow .= pack("v", strlen($directoryName) );
  80. $feedArrayRow .= pack("v", 0 );
  81. $feedArrayRow .= $directoryName;
  82. $feedArrayRow .= $gzCompressedData;
  83. $feedArrayRow .= pack("V",$compression);
  84. $feedArrayRow .= pack("V",$compressedLength);
  85. $feedArrayRow .= pack("V",$uncompressedLength);
  86. $this -> compressedData[] = $feedArrayRow;
  87. $newOffset = strlen(implode("", $this->compressedData));
  88. $addCentralRecord = "\x50\x4b\x01\x02";
  89. $addCentralRecord .="\x00\x00";
  90. $addCentralRecord .="\x14\x00";
  91. $addCentralRecord .="\x00\x00";
  92. $addCentralRecord .="\x08\x00";
  93. $addCentralRecord .="\x00\x00\x00\x00";
  94. $addCentralRecord .= pack("V",$compression);
  95. $addCentralRecord .= pack("V",$compressedLength);
  96. $addCentralRecord .= pack("V",$uncompressedLength);
  97. $addCentralRecord .= pack("v", strlen($directoryName) );
  98. $addCentralRecord .= pack("v", 0 );
  99. $addCentralRecord .= pack("v", 0 );
  100. $addCentralRecord .= pack("v", 0 );
  101. $addCentralRecord .= pack("v", 0 );
  102. $addCentralRecord .= pack("V", 32 );
  103. $addCentralRecord .= pack("V", $this -> oldOffset );
  104. $this -> oldOffset = $newOffset;
  105. $addCentralRecord .= $directoryName;
  106. $this -> centralDirectory[] = $addCentralRecord;
  107. }
  108. /**
  109. * Fucntion to return the zip file
  110. *
  111. * @return zipfile (archive)
  112. */
  113. public function getZippedfile() {
  114. $data = implode("", $this -> compressedData);
  115. $controlDirectory = implode("", $this -> centralDirectory);
  116. return
  117. $data.
  118. $controlDirectory.
  119. $this -> endOfCentralDirectory.
  120. pack("v", sizeof($this -> centralDirectory)).
  121. pack("v", sizeof($this -> centralDirectory)).
  122. pack("V", strlen($controlDirectory)).
  123. pack("V", strlen($data)).
  124. "\x00\x00";
  125. }
  126. }
  127. /*
  128. MySQL database backup class, version 1.0.0
  129. Written by Vagharshak Tozalakyan <vagh@armdex.com>
  130. Released under GNU Public license
  131. */
  132. define('MSB_VERSION', '1.0.0');
  133. define('MSB_NL', "\r\n");
  134. define('MSB_STRING', 0);
  135. define('MSB_DOWNLOAD', 1);
  136. define('MSB_SAVE', 2);
  137. class MySQL_Backup
  138. {
  139. var $server = 'localhost';
  140. var $port = 3306;
  141. var $username = 'root';
  142. var $password = '';
  143. var $database = '';
  144. var $link_id = -1;
  145. var $connected = false;
  146. var $tables = array();
  147. var $drop_tables = true;
  148. var $struct_only = false;
  149. var $comments = true;
  150. var $backup_dir = '';
  151. var $fname_format = 'd_m_y__H_i_s';
  152. var $error = '';
  153. function Execute($task = MSB_STRING, $fname = '', $compress = false)
  154. {
  155. if (!($sql = $this->_Retrieve()))
  156. {
  157. return false;
  158. }
  159. if ($task == MSB_SAVE)
  160. {
  161. if (empty($fname))
  162. {
  163. $fname = $this->backup_dir;
  164. $fname .= date($this->fname_format);
  165. $fname .= ($compress ? '.sql.gz' : '.sql');
  166. }
  167. return $this->_SaveToFile($fname, $sql, $compress);
  168. }
  169. elseif ($task == MSB_DOWNLOAD)
  170. {
  171. if (empty($fname))
  172. {
  173. $fname = date($this->fname_format);
  174. $fname .= ($compress ? '.sql.gz' : '.sql');
  175. }
  176. return $this->_DownloadFile($fname, $sql, $compress);
  177. }
  178. else
  179. {
  180. return $sql;
  181. }
  182. }
  183. function _Connect()
  184. {
  185. $value = false;
  186. if (!$this->connected)
  187. {
  188. $host = $this->server . ':' . $this->port;
  189. $this->link_id = mysql_connect($host, $this->username, $this->password);
  190. }
  191. if ($this->link_id)
  192. {
  193. if (empty($this->database))
  194. {
  195. $value = true;
  196. }
  197. elseif ($this->link_id !== -1)
  198. {
  199. $value = mysql_select_db($this->database, $this->link_id);
  200. }
  201. else
  202. {
  203. $value = mysql_select_db($this->database);
  204. }
  205. }
  206. if (!$value)
  207. {
  208. $this->error = mysql_error();
  209. }
  210. return $value;
  211. }
  212. function _Query($sql)
  213. {
  214. if ($this->link_id !== -1)
  215. {
  216. $result = mysql_query($sql, $this->link_id);
  217. }
  218. else
  219. {
  220. $result = mysql_query($sql);
  221. }
  222. if (!$result)
  223. {
  224. $this->error = mysql_error();
  225. }
  226. return $result;
  227. }
  228. function _GetTables()
  229. {
  230. $value = array();
  231. if (!($result = $this->_Query('SHOW TABLES')))
  232. {
  233. return false;
  234. }
  235. while ($row = mysql_fetch_row($result))
  236. {
  237. if (empty($this->tables) || in_array($row[0], $this->tables))
  238. {
  239. $value[] = $row[0];
  240. }
  241. }
  242. if (!sizeof($value))
  243. {
  244. $this->error = 'No tables found in database.';
  245. return false;
  246. }
  247. return $value;
  248. }
  249. function _DumpTable($table)
  250. {
  251. $value = '';
  252. $this->_Query('LOCK TABLES ' . $table . ' WRITE');
  253. if ($this->comments)
  254. {
  255. $value .= '#' . MSB_NL;
  256. $value .= '# Table structure for table `' . $table . '`' . MSB_NL;
  257. $value .= '#' . MSB_NL . MSB_NL;
  258. }
  259. if ($this->drop_tables)
  260. {
  261. $value .= 'DROP TABLE IF EXISTS `' . $table . '`;' . MSB_NL;
  262. }
  263. if (!($result = $this->_Query('SHOW CREATE TABLE ' . $table)))
  264. {
  265. return false;
  266. }
  267. $row = mysql_fetch_assoc($result);
  268. $value .= str_replace("\n", MSB_NL, $row['Create Table']) . ';';
  269. $value .= MSB_NL . MSB_NL;
  270. if (!$this->struct_only)
  271. {
  272. if ($this->comments)
  273. {
  274. $value .= '#' . MSB_NL;
  275. $value .= '# Dumping data for table `' . $table . '`' . MSB_NL;
  276. $value .= '#' . MSB_NL . MSB_NL;
  277. }
  278. $value .= $this->_GetInserts($table);
  279. }
  280. $value .= MSB_NL . MSB_NL;
  281. $this->_Query('UNLOCK TABLES');
  282. return $value;
  283. }
  284. function _GetInserts($table)
  285. {
  286. $value = '';
  287. if (!($result = $this->_Query('SELECT * FROM ' . $table)))
  288. {
  289. return false;
  290. }
  291. while ($row = mysql_fetch_row($result))
  292. {
  293. $values = '';
  294. foreach ($row as $data)
  295. {
  296. $values .= '\'' . addslashes($data) . '\', ';
  297. }
  298. $values = substr($values, 0, -2);
  299. $value .= 'INSERT INTO ' . $table . ' VALUES (' . $values . ');' . MSB_NL;
  300. }
  301. return $value;
  302. }
  303. function _Retrieve()
  304. {
  305. $value = '';
  306. if (!$this->_Connect())
  307. {
  308. return false;
  309. }
  310. if ($this->comments)
  311. {
  312. $value .= '#' . MSB_NL;
  313. $value .= '# MySQL database dump' . MSB_NL;
  314. $value .= '# Created by Clasido.com Backup, ver. ' . MSB_VERSION . MSB_NL;
  315. $value .= '#' . MSB_NL;
  316. $value .= '# Host: ' . $this->server . MSB_NL;
  317. $value .= '# Generated: ' . date('M j, Y') . ' at ' . date('H:i') . MSB_NL;
  318. $value .= '# MySQL version: ' . mysql_get_server_info() . MSB_NL;
  319. $value .= '# PHP version: ' . phpversion() . MSB_NL;
  320. if (!empty($this->database))
  321. {
  322. $value .= '#' . MSB_NL;
  323. $value .= '# Database: `' . $this->database . '`' . MSB_NL;
  324. }
  325. $value .= '#' . MSB_NL . MSB_NL . MSB_NL;
  326. }
  327. if (!($tables = $this->_GetTables()))
  328. {
  329. return false;
  330. }
  331. foreach ($tables as $table)
  332. {
  333. if (!($table_dump = $this->_DumpTable($table)))
  334. {
  335. $this->error = mysql_error();
  336. return false;
  337. }
  338. $value .= $table_dump;
  339. }
  340. return $value;
  341. }
  342. function _SaveToFile($fname, $sql, $compress)
  343. {
  344. if ($compress)
  345. {
  346. if (!($zf = gzopen($fname, 'w9')))
  347. {
  348. $this->error = 'Can\'t create the output file.';
  349. return false;
  350. }
  351. gzwrite($zf, $sql);
  352. gzclose($zf);
  353. }
  354. else
  355. {
  356. if (!($f = fopen($fname, 'w')))
  357. {
  358. $this->error = 'Can\'t create the output file.';
  359. return false;
  360. }
  361. fwrite($f, $sql);
  362. fclose($f);
  363. }
  364. return true;
  365. }
  366. }
  367. function mailAttachment($file, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
  368. $filename = basename($file);
  369. $file_size = filesize($file);
  370. $handle = fopen($file, "r");
  371. $content = fread($handle, $file_size);
  372. fclose($handle);
  373. $content = chunk_split(base64_encode($content));
  374. $uid = md5(uniqid(time()));
  375. $name = basename($file);
  376. $header = "From: ".$from_name." <".$from_mail.">\r\n";
  377. $header .= "Reply-To: ".$replyto."\r\n";
  378. $header .= "MIME-Version: 1.0\r\n";
  379. $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
  380. $header .= "This is a multi-part message in MIME format.\r\n";
  381. $header .= "--".$uid."\r\n";
  382. $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
  383. $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
  384. $header .= $message."\r\n\r\n";
  385. $header .= "--".$uid."\r\n";
  386. $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use diff. tyoes here
  387. $header .= "Content-Transfer-Encoding: base64\r\n";
  388. $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
  389. $header .= $content."\r\n\r\n";
  390. $header .= "--".$uid."--";
  391. if (mail($mailto, $subject, "", $header)) {
  392. echo "mail send ... OK"; // or use booleans here
  393. } else {
  394. echo "mail send ... ERROR!";
  395. }
  396. }
  397. function directoryToArray($directory, $recursive) {
  398. $array_items = array();
  399. if ($handle = opendir($directory)) {
  400. while (false !== ($file = readdir($handle))) {
  401. if ($file != "." && $file != "..") {
  402. if (is_dir($directory. "/" . $file)) {
  403. if($recursive) {
  404. $array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
  405. }
  406. $file = $directory . "/" . $file ."/";
  407. $array_items[] = preg_replace("/\/\//si", "/", $file);
  408. } else {
  409. $file = $directory . "/" . $file;
  410. $array_items[] = preg_replace("/\/\//si", "/", $file);
  411. }
  412. }
  413. }
  414. closedir($handle);
  415. }
  416. return $array_items;
  417. }
  418. function pr($val)
  419. {
  420. echo '<pre>';
  421. print_r($val);
  422. echo '</pre>';
  423. }
  424. ?>