PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/PrestaShopBackup.php

https://gitlab.com/jslee1/PrestaShop
PHP | 308 lines | 185 code | 38 blank | 85 comment | 44 complexity | 78d39c427839e1198fb1cf026d5aff5c MD5 | raw file
  1. <?php
  2. /**
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class PrestaShopBackupCore
  27. {
  28. /** @var int Object id */
  29. public $id;
  30. /** @var string Last error messages */
  31. public $error;
  32. /** @var string default backup directory. */
  33. public static $backupDir = '/backups/';
  34. /** @var string custom backup directory. */
  35. public $customBackupDir = null;
  36. public $psBackupAll = true;
  37. public $psBackupDropTable = true;
  38. /**
  39. * Creates a new backup object
  40. *
  41. * @param string $filename Filename of the backup file
  42. */
  43. public function __construct($filename = null)
  44. {
  45. if ($filename) {
  46. $this->id = $this->getRealBackupPath($filename);
  47. }
  48. $psBackupAll = Configuration::get('PS_BACKUP_ALL');
  49. $psBackupDropTable = Configuration::get('PS_BACKUP_DROP_TABLE');
  50. $this->psBackupAll = $psBackupAll !== false ? $psBackupAll : true;
  51. $this->psBackupDropTable = $psBackupDropTable !== false ? $psBackupDropTable : true;
  52. }
  53. /**
  54. * you can set a different path with that function
  55. *
  56. * @TODO include the prefix name
  57. * @param string $dir
  58. * @return bool bo
  59. */
  60. public function setCustomBackupPath($dir)
  61. {
  62. $custom_dir = DIRECTORY_SEPARATOR.trim($dir, '/').DIRECTORY_SEPARATOR;
  63. if (is_dir((defined('_PS_HOST_MODE_') ? _PS_ROOT_DIR_ : _PS_ADMIN_DIR_).$custom_dir)) {
  64. $this->customBackupDir = $custom_dir;
  65. } else {
  66. return false;
  67. }
  68. return true;
  69. }
  70. /**
  71. * get the path to use for backup (customBackupDir if specified, or default)
  72. *
  73. * @param string $filename filename to use
  74. * @return string full path
  75. */
  76. public function getRealBackupPath($filename = null)
  77. {
  78. $backupDir = PrestaShopBackup::getBackupPath($filename);
  79. if (!empty($this->customBackupDir)) {
  80. $backupDir = str_replace((defined('_PS_HOST_MODE_') ? _PS_ROOT_DIR_ : _PS_ADMIN_DIR_).self::$backupDir,
  81. (defined('_PS_HOST_MODE_') ? _PS_ROOT_DIR_ : _PS_ADMIN_DIR_).$this->customBackupDir, $backupDir);
  82. if (strrpos($backupDir, DIRECTORY_SEPARATOR)) {
  83. $backupDir .= DIRECTORY_SEPARATOR;
  84. }
  85. }
  86. return $backupDir;
  87. }
  88. /**
  89. * Get the full path of the backup file
  90. *
  91. * @param string $filename prefix of the backup file (datetime will be the second part)
  92. * @return string The full path of the backup file, or false if the backup file does not exists
  93. */
  94. public static function getBackupPath($filename = '')
  95. {
  96. $backupdir = realpath((defined('_PS_HOST_MODE_') ? _PS_ROOT_DIR_ : _PS_ADMIN_DIR_).self::$backupDir);
  97. if ($backupdir === false) {
  98. die(Tools::displayError('"Backup" directory does not exist.'));
  99. }
  100. // Check the realpath so we can validate the backup file is under the backup directory
  101. if (!empty($filename)) {
  102. $backupfile = realpath($backupdir.DIRECTORY_SEPARATOR.$filename);
  103. } else {
  104. $backupfile = $backupdir.DIRECTORY_SEPARATOR;
  105. }
  106. if ($backupfile === false || strncmp($backupdir, $backupfile, strlen($backupdir)) != 0) {
  107. die(Tools::displayError());
  108. }
  109. return $backupfile;
  110. }
  111. /**
  112. * Check if a backup file exist
  113. *
  114. * @param string $filename prefix of the backup file (datetime will be the second part)
  115. * @return bool true if backup file exist
  116. */
  117. public static function backupExist($filename)
  118. {
  119. $backupdir = realpath((defined('_PS_HOST_MODE_') ? _PS_ROOT_DIR_ : _PS_ADMIN_DIR_).self::$backupDir);
  120. if ($backupdir === false) {
  121. die(Tools::displayError('"Backup" directory does not exist.'));
  122. }
  123. return @filemtime($backupdir.DIRECTORY_SEPARATOR.$filename);
  124. }
  125. /**
  126. * Get the URL used to retrieve this backup file
  127. *
  128. * @return string The url used to request the backup file
  129. */
  130. public function getBackupURL()
  131. {
  132. return __PS_BASE_URI__.basename(_PS_ADMIN_DIR_).'/backup.php?filename='.basename($this->id);
  133. }
  134. /**
  135. * Delete the current backup file
  136. *
  137. * @return bool Deletion result, true on success
  138. */
  139. public function delete()
  140. {
  141. if (!$this->id || !unlink($this->id)) {
  142. $this->error = Tools::displayError('Error deleting').' '.($this->id ? '"'.$this->id.'"' :
  143. Tools::displayError('Invalid ID'));
  144. return false;
  145. }
  146. return true;
  147. }
  148. /**
  149. * Deletes a range of backup files
  150. *
  151. * @return bool True on success
  152. */
  153. public function deleteSelection($list)
  154. {
  155. foreach ($list as $file) {
  156. $backup = new PrestaShopBackup($file);
  157. if (!$backup->delete()) {
  158. $this->error = $backup->error;
  159. return false;
  160. }
  161. }
  162. return true;
  163. }
  164. /**
  165. * Creates a new backup file
  166. *
  167. * @return bool true on successful backup
  168. */
  169. public function add()
  170. {
  171. if (!$this->psBackupAll) {
  172. $ignore_insert_table = array(_DB_PREFIX_.'connections', _DB_PREFIX_.'connections_page', _DB_PREFIX_
  173. .'connections_source', _DB_PREFIX_.'guest', _DB_PREFIX_.'statssearch');
  174. } else {
  175. $ignore_insert_table = array();
  176. }
  177. // Generate some random number, to make it extra hard to guess backup file names
  178. $rand = dechex(mt_rand(0, min(0xffffffff, mt_getrandmax())));
  179. $date = time();
  180. $backupfile = $this->getRealBackupPath().$date.'-'.$rand.'.sql';
  181. // Figure out what compression is available and open the file
  182. if (function_exists('bzopen')) {
  183. $backupfile .= '.bz2';
  184. $fp = @bzopen($backupfile, 'w');
  185. } elseif (function_exists('gzopen')) {
  186. $backupfile .= '.gz';
  187. $fp = @gzopen($backupfile, 'w');
  188. } else {
  189. $fp = @fopen($backupfile, 'w');
  190. }
  191. if ($fp === false) {
  192. echo Tools::displayError('Unable to create backup file').' "'.addslashes($backupfile).'"';
  193. return false;
  194. }
  195. $this->id = realpath($backupfile);
  196. fwrite($fp, '/* Backup for '.Tools::getHttpHost(false, false).__PS_BASE_URI__."\n * at ".date($date)."\n */\n");
  197. fwrite($fp, "\n".'SET NAMES \'utf8\';'."\n\n");
  198. // Find all tables
  199. $tables = Db::getInstance()->executeS('SHOW TABLES');
  200. $found = 0;
  201. foreach ($tables as $table) {
  202. $table = current($table);
  203. // Skip tables which do not start with _DB_PREFIX_
  204. if (strlen($table) < strlen(_DB_PREFIX_) || strncmp($table, _DB_PREFIX_, strlen(_DB_PREFIX_)) != 0) {
  205. continue;
  206. }
  207. // Export the table schema
  208. $schema = Db::getInstance()->executeS('SHOW CREATE TABLE `'.$table.'`');
  209. if (count($schema) != 1 || !isset($schema[0]['Table']) || !isset($schema[0]['Create Table'])) {
  210. fclose($fp);
  211. $this->delete();
  212. echo Tools::displayError('An error occurred while backing up. Unable to obtain the schema of').' "'.$table;
  213. return false;
  214. }
  215. fwrite($fp, '/* Scheme for table '.$schema[0]['Table']." */\n");
  216. if ($this->psBackupDropTable) {
  217. fwrite($fp, 'DROP TABLE IF EXISTS `'.$schema[0]['Table'].'`;'."\n");
  218. }
  219. fwrite($fp, $schema[0]['Create Table'].";\n\n");
  220. if (!in_array($schema[0]['Table'], $ignore_insert_table)) {
  221. $data = Db::getInstance()->query('SELECT * FROM `'.$schema[0]['Table'].'`', false);
  222. $sizeof = DB::getInstance()->NumRows();
  223. $lines = explode("\n", $schema[0]['Create Table']);
  224. if ($data && $sizeof > 0) {
  225. // Export the table data
  226. fwrite($fp, 'INSERT INTO `'.$schema[0]['Table']."` VALUES\n");
  227. $i = 1;
  228. while ($row = DB::getInstance()->nextRow($data)) {
  229. $s = '(';
  230. foreach ($row as $field => $value) {
  231. $tmp = "'".pSQL($value, true)."',";
  232. if ($tmp != "'',") {
  233. $s .= $tmp;
  234. } else {
  235. foreach ($lines as $line) {
  236. if (strpos($line, '`'.$field.'`') !== false) {
  237. if (preg_match('/(.*NOT NULL.*)/Ui', $line)) {
  238. $s .= "'',";
  239. } else {
  240. $s .= 'NULL,';
  241. }
  242. break;
  243. }
  244. }
  245. }
  246. }
  247. $s = rtrim($s, ',');
  248. if ($i % 200 == 0 && $i < $sizeof) {
  249. $s .= ");\nINSERT INTO `".$schema[0]['Table']."` VALUES\n";
  250. } elseif ($i < $sizeof) {
  251. $s .= "),\n";
  252. } else {
  253. $s .= ");\n";
  254. }
  255. fwrite($fp, $s);
  256. ++$i;
  257. }
  258. }
  259. }
  260. $found++;
  261. }
  262. fclose($fp);
  263. if ($found == 0) {
  264. $this->delete();
  265. echo Tools::displayError('No valid tables were found to backup.');
  266. return false;
  267. }
  268. return true;
  269. }
  270. }