PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/adminstrator/tabs/AdminBackup.php

http://marocmall.googlecode.com/
PHP | 307 lines | 231 code | 30 blank | 46 comment | 26 complexity | eaf60c08119e583aca7e72a1071cbe5c MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * 2007-2011 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-2011 PrestaShop SA
  23. * @version Release: $Revision: 7721 $
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. */
  27. include_once(PS_ADMIN_DIR.'/../classes/AdminTab.php');
  28. class AdminBackup extends AdminTab
  29. {
  30. /** @var string The field we are sorting on */
  31. protected $_sortBy = 'date';
  32. public function __construct()
  33. {
  34. $this->table = 'backup';
  35. $this->className = 'Backup';
  36. parent::__construct();
  37. $this->edit = false;
  38. $this->delete = true;
  39. $this->view = true;
  40. $this->deleted = false;
  41. $this->requiredDatabase = false;
  42. $this->fieldsDisplay = array (
  43. 'date' => array('title' => $this->l('Date'), 'type' => 'datetime', 'width' => 120, 'align' => 'right'),
  44. 'age' => array('title' => $this->l('Age')),
  45. 'filename' => array('title' => $this->l('File name'), 'width' => 200),
  46. 'filesize' => array('title' => $this->l('File size')));
  47. $this->optionTitle = $this->l('Backup options');
  48. $this->_fieldsOptions = array(
  49. 'PS_BACKUP_ALL' => array('title' => $this->l('Ignore statistics tables:'),
  50. 'desc' => $this->l('The following tables will NOT be backed up if you enable this option:').'<br />'._DB_PREFIX_.'connections, '._DB_PREFIX_.'connections_page, '._DB_PREFIX_.'connections_source, '._DB_PREFIX_.'guest, '._DB_PREFIX_.'statssearch', 'cast' => 'intval', 'type' => 'bool'),
  51. 'PS_BACKUP_DROP_TABLE' => array('title' => $this->l('Drop existing tables during import:'),
  52. 'desc' => $this->l('Select this option to instruct the backup file to drop your tables prior to restoring the backed up data').'<br />(ie. "DROP TABLE IF EXISTS")', 'cast' => 'intval', 'type' => 'bool'));
  53. $this->identifier = 'filename';
  54. }
  55. /**
  56. * Load class object using identifier in $_GET (if possible)
  57. * otherwise return an empty object
  58. * This method overrides the one in AdminTab because AdminTab assumes the id is a UnsignedInt
  59. * "Backups" Directory in admin directory must be writeable (CHMOD 777)
  60. * @param boolean $opt Return an empty object if load fail
  61. * @return object
  62. */
  63. protected function loadObject($opt = false)
  64. {
  65. if ($id = Tools::getValue($this->identifier))
  66. return new $this->className($id);
  67. return new $this->className();
  68. }
  69. /**
  70. * Creates a new backup, and then displays the normal menu
  71. */
  72. public function displayForm($isMainTab = true)
  73. {
  74. if (is_writable(PS_ADMIN_DIR.'/backups/'))
  75. {
  76. if (!($object = $this->loadObject()))
  77. return;
  78. if ($object->add())
  79. {
  80. echo '<div class="conf confirm"><img src="../img/admin/ok.gif" />&nbsp;'.$this->l('It appears that the Backup was successful, however, you must download and carefully verify the Backup file.').'</div>';
  81. if ($this->tabAccess['view'] === '1')
  82. echo '
  83. <fieldset style="margin: 40px 0;" class="width3">
  84. <legend><img src="../img/admin/AdminBackup.gif" alt="" class="icon" /> '.$this->l('Download').'</legend>
  85. <p style="font-size: 13px;"><a href="'.$object->getBackupURL().'"><img src="../img/admin/AdminBackup.gif" alt="" class="icon" /></a><b><a href="'.$object->getBackupURL().'">'.$this->l('Download the Backup file').' ('.number_format((filesize($object->id)*0.000001), 2, '.', '').$this->l('Mb').')</a></b><br /><br />
  86. '.$this->l('Tip: You can also download this file by FTP, Backup files are located in "admin/backups" directory.').'</p>
  87. </fieldset>';
  88. $this->displayHowTo(false);
  89. }
  90. elseif ($object->error)
  91. $this->_errors[] = $object->error;
  92. }
  93. else
  94. $this->_errors[] = $this->l('"Backups" Directory in admin directory must be writeable (CHMOD 755 / 777)');
  95. $this->displayErrors();
  96. }
  97. /**
  98. * Displays the page which allows the backup to be downloaded
  99. */
  100. public function viewbackup()
  101. {
  102. global $currentIndex;
  103. if (!($object = $this->loadObject()))
  104. return;
  105. if ($object->id)
  106. {
  107. $url = $object->getBackupURL();
  108. echo '<div class="conf confirm"><img src="../img/admin/ok.gif" />&nbsp;'.$this->l('Beginning download ...').'</div>';
  109. echo '<br />'.$this->l('Back-up file should automatically download.');
  110. echo '<br /><br />'.$this->l('If not,').' <b><a href="'.$url.'">'.$this->l('please click here!').'</a></b>';
  111. echo '<iframe width="0" height="0" scrolling="no" frameborder="0" src="'.$url.'"></iframe>';
  112. echo '<br /><br /><br /><a href="'.$currentIndex.'&token='.$this->token.'"><img src="../img/admin/arrow2.gif" /> '.$this->l('Back to list').'</a><br />';
  113. }
  114. elseif ($object->error)
  115. $this->_errors[] = $object->error;
  116. $this->displayErrors();
  117. }
  118. public function displayHowTo($showForm = true)
  119. {
  120. global $currentIndex;
  121. echo '
  122. <div class="error width1" style="float: left; margin-right: 10px;">
  123. <p>'.$this->l('Disclaimer before creating a new Backup').'</p>
  124. <ol style="font-size: 11px; font-weight: normal; line-height: 20px; padding-left: 10px;">
  125. <li>'.$this->l('PrestaShop is not responsible for your database, Backups, restore and data.').'</li>
  126. <li>'.$this->l('PrestaShop is an Open-source software, you are using it at your own risk under the licence agreement.').'</li>
  127. <li>'.$this->l('You should Backup your data on a regular basis (both files and database).').'</li>
  128. <li>'.$this->l('This function only backs up your database, not your files.').'</li>
  129. <li>'.$this->l('By default, your existing database tables will be deleted during Backup restore (see options).').'</li>
  130. <li>'.$this->l('Always verify the quality and integrity of your Backups files.').'</li>
  131. <li>'.$this->l('Always verify that your Backups files are complete, up-to-date and valid. Even if you had a success message during the Backup process.').'</li>
  132. <li>'.$this->l('Always check your data.').'</li>
  133. <li>'.$this->l('Never restore a Backup on a live site.').'</li>
  134. </ol>';
  135. if ($showForm)
  136. echo '
  137. <form action="'.$currentIndex.'&add'.$this->table.'&token='.$this->token.'" method="post" style="text-align: center;">
  138. <input type="submit" class="button" value="'.$this->l('I read the disclaimer - Create a new Backup').'" style="padding: 10px; font-weight: bold; border: 1px solid;" />
  139. </form>';
  140. echo '
  141. </div>
  142. <div class="warn width2" style="float: left;">
  143. <p>'.$this->l('How-to restore a database Backup in 10 easy steps').'</p>
  144. <ol style="font-size: 11px; font-weight: normal; line-height: 20px;">
  145. <li>'.$this->l('Turn off the "Enable Shop" option in the "Preferences" tab.').'</li>
  146. <li>'.$this->l('Download the Backup from the list below or from your FTP server (in the folder "admin/backups").').'</li>
  147. <li>'.$this->l('Check the Backup integrity: look for errors, incomplete file. Verify all your data.').'</li>
  148. <li>'.$this->l('Ask your hosting provider for a "phpMyAdmin" access to your database').'</li>
  149. <li>'.$this->l('Connect to "phpMyAdmin" and select your current database').'</li>
  150. <li>'.$this->l('Unless you enabled the "Drop existing tables" option, you must delete all tables from your current database.').'</li>
  151. <li>'.$this->l('At the top of the screen select the tab "Import"').'</li>
  152. <li>'.$this->l('Click on the "Browse..." button and select the Backup file from your hard drive').'</li>
  153. <li>'.$this->l('Check the max. allowed filesize (ie. Max: 16Mb)').'<br />'.$this->l('If your Backup file exceeds this limit, contact your hosting provider').'</li>
  154. <li>'.$this->l('Click on the "Go" button and wait during the import, the process can take several minutes').'</li>
  155. </ol>
  156. </div>
  157. <div class="clear"></div>';
  158. }
  159. public function displayList()
  160. {
  161. global $currentIndex;
  162. // Test if the backup dir is writable
  163. if (!is_writable(PS_ADMIN_DIR.'/backups/'))
  164. $this->displayWarning($this->l('"Backups" Directory in admin directory must be writeable (CHMOD 755 / 777)'));
  165. $this->displayErrors();
  166. $this->displayHowTo();
  167. parent::displayList();
  168. }
  169. public function getList($id_lang, $orderBy = NULL, $orderWay = NULL, $start = 0, $limit = NULL)
  170. {
  171. global $cookie;
  172. if (!Validate::isTableOrIdentifier($this->table))
  173. die('filter is corrupted');
  174. if (empty($orderBy))
  175. $orderBy = Tools::getValue($this->table.'Orderby', $this->_defaultOrderBy);
  176. if (empty($orderWay))
  177. $orderWay = Tools::getValue($this->table.'Orderway', 'ASC');
  178. // Try and obtain getList arguments from $_GET
  179. $orderBy = Tools::getValue($this->table.'Orderby');
  180. $orderWay = Tools::getValue($this->table.'Orderway');
  181. // Validate the orderBy and orderWay fields
  182. switch ($orderBy)
  183. {
  184. case 'filename':
  185. case 'filesize':
  186. case 'date':
  187. case 'age':
  188. break;
  189. default:
  190. $orderBy = 'date';
  191. }
  192. switch ($orderWay)
  193. {
  194. case 'asc':
  195. case 'desc':
  196. break;
  197. default:
  198. $orderWay = 'desc';
  199. }
  200. if (empty($limit))
  201. $limit = ((!isset($cookie->{$this->table.'_pagination'})) ? $this->_pagination[0] : $limit = $cookie->{$this->table.'_pagination'});
  202. $limit = (int)(Tools::getValue('pagination', $limit));
  203. $cookie->{$this->table.'_pagination'} = $limit;
  204. /* Determine offset from current page */
  205. if (!empty($_POST['submitFilter'.$this->table]) AND is_numeric($_POST['submitFilter'.$this->table]))
  206. $start = (int)($_POST['submitFilter'.$this->table] - 1) * $limit;
  207. $this->_lang = (int)($id_lang);
  208. $this->_orderBy = $orderBy;
  209. $this->_orderWay = strtoupper($orderWay);
  210. $this->_list = array();
  211. // Find all the backups
  212. $dh = @opendir(PS_ADMIN_DIR.'/backups/');
  213. if ($dh === false)
  214. {
  215. $this->_errors[] = Tools::displayError('Unable to open backup directory .').addslashes(PS_ADMIN_DIR.'/backups/').'"';
  216. return;
  217. }
  218. while (($file = readdir($dh)) !== false)
  219. {
  220. if (preg_match('/^([\d]+-[a-z\d]+)\.sql(\.gz|\.bz2)?$/', $file, $matches) == 0)
  221. continue;
  222. $timestamp = (int)($matches[1]);
  223. $date = date('Y-m-d H:i:s', $timestamp);
  224. $age = time() - $timestamp;
  225. if ($age < 3600)
  226. $age = '< 1 '.$this->l('hour');
  227. elseif ($age < 86400)
  228. {
  229. $age = floor($age / 3600);
  230. $age = $age.' '.(($age == 1) ? $this->l('hour') : $this->l('hours'));
  231. }
  232. else
  233. {
  234. $age = floor($age / 86400);
  235. $age = $age.' '.(($age == 1) ? $this->l('day') : $this->l('days'));
  236. }
  237. $size = filesize(PS_ADMIN_DIR.'/backups/'.$file);
  238. $this->_list[] = array(
  239. 'filename' => $file,
  240. 'age' => $age,
  241. 'date' => $date,
  242. 'filesize' => number_format($size / 1000, 2).' Kb',
  243. 'timestamp' => $timestamp,
  244. 'filesize_sort' => $size,
  245. );
  246. }
  247. closedir($dh);
  248. $this->_listTotal = count($this->_list);
  249. // Sort the _list based on the order requirements
  250. switch ($this->_orderBy)
  251. {
  252. case 'filename':
  253. $this->_sortBy = 'filename';
  254. $sorter = 'str_sort';
  255. break;
  256. case 'filesize':
  257. $this->_sortBy = 'filesize_sort';
  258. $sorter = 'int_sort';
  259. break;
  260. case 'age':
  261. case 'date':
  262. $this->_sortBy = 'timestamp';
  263. $sorter = 'int_sort';
  264. break;
  265. }
  266. usort($this->_list, array($this, $sorter));
  267. $this->_list = array_slice($this->_list, $start, $limit);
  268. }
  269. public function int_sort($a, $b)
  270. {
  271. return $this->_orderWay == 'ASC' ? $a[$this->_sortBy] - $b[$this->_sortBy] : $b[$this->_sortBy] - $a[$this->_sortBy];
  272. }
  273. public function str_sort($a, $b)
  274. {
  275. return $this->_orderWay == 'ASC' ? strcmp($a[ $this->_sortBy], $b[$this->_sortBy]) : strcmp($b[ $this->_sortBy], $a[$this->_sortBy]);
  276. }
  277. }