PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/core/controllers/single_pages/dashboard/system/backup_restore/backup.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 133 lines | 114 code | 15 blank | 4 comment | 23 complexity | 9e3bd654e09dd3c553eac55619e3b541 MD5 | raw file
  1. <?php
  2. defined('C5_EXECUTE') or die("Access Denied.");
  3. Loader::library('backup');
  4. class Concrete5_Controller_Dashboard_System_BackupRestore_Backup extends DashboardBaseController {
  5. public function on_start() {
  6. $this->addHeaderItem(Loader::helper('html')->javascript('jquery.cookie.js'));
  7. parent::on_start();
  8. }
  9. public function run_backup() {
  10. $encrypt = $this->post('useEncryption');
  11. $tp = new TaskPermission();
  12. if ($tp->canBackup()) {
  13. $encrypt = (bool) $encrypt;
  14. try {
  15. $backup = Backup::execute($encrypt);
  16. } catch(Exception $e) {
  17. $this->set('error', $e);
  18. }
  19. $this->view();
  20. }
  21. }
  22. public function view() {
  23. $tp = new TaskPermission();
  24. if ($tp->canBackup()) {
  25. $fh = Loader::helper('file');
  26. $arr_bckups = @$fh->getDirectoryContents(DIR_FILES_BACKUPS);
  27. $arr_backupfileinfo = Array();
  28. if (count($arr_bckups) > 0) {
  29. foreach ($arr_bckups as $bkupfile) {
  30. // This will ignore files that do not match the created backup pattern of including a timestamp in the filename
  31. if (preg_match("/_([\d]{10,})/", $bkupfile, $timestamp)){
  32. $arr_backupfileinfo[] = Array("file" => $bkupfile, "date" => date("Y-m-d H:i:s",$timestamp[1]));
  33. }
  34. }
  35. $this->set('backups',$arr_backupfileinfo);
  36. }
  37. }
  38. }
  39. public function download($file) {
  40. $tp = new TaskPermission();
  41. if (!$tp->canBackup()) {
  42. return false;
  43. }
  44. if (file_exists(DIR_FILES_BACKUPS . '/'. $file)) {
  45. chmod(DIR_FILES_BACKUPS . '/'. $file, 0666);
  46. if (file_exists(DIR_FILES_BACKUPS . '/' . $file)) {
  47. $f = Loader::helper('file');
  48. $f->forceDownload(DIR_FILES_BACKUPS . '/' . $file);
  49. exit;
  50. }
  51. chmod(DIR_FILES_BACKUPS . '/'. $file, 000);
  52. } else {
  53. $this->set('error', array(t('Unable to locate file %s', DIR_FILES_BACKUPS . '/' . $file)));
  54. $this->view();
  55. }
  56. }
  57. public function delete_backup() {
  58. $tp = new TaskPermission();
  59. if (!$tp->canBackup()) {
  60. return false;
  61. }
  62. $str_fname = $this->post('backup_file');
  63. //For Security reasons... allow only known characters in the string e.g no / \ so you can't exploit this
  64. $int_mResult = preg_match('/[0-9A-Za-z._]+/',$str_fname,$ar_matches);
  65. $str_fname = $ar_matches[0];
  66. if (!is_null($str_fname) && trim($str_fname) != "" && !preg_match('/\.\./',$str_fname)) {
  67. $fullFilename = DIR_FILES_BACKUPS . "/$str_fname";
  68. if(is_file($fullFilename)) {
  69. @chmod($fullFilename, 666);
  70. @unlink($fullFilename);
  71. if(is_file($fullFilename)) {
  72. $this->error->add(t('Error deleting the file %s. Please check the permissions of the folder %s', $str_fname, DIR_FILES_BACKUPS));
  73. }
  74. }
  75. }
  76. $this->view();
  77. }
  78. public function restore_backup() {
  79. set_time_limit(0);
  80. $tp = new TaskPermission();
  81. if (!$tp->canBackup()) {
  82. return false;
  83. }
  84. $file = basename(realpath(DIR_FILES_BACKUPS . '/' . $this->post('backup_file')));
  85. $fh = Loader::helper('file');
  86. $db = Loader::db();
  87. if (!file_exists(DIR_FILES_BACKUPS . '/'. $file)) {
  88. throw new Exception(t('Invalid backup file specified.'));
  89. }
  90. chmod(DIR_FILES_BACKUPS . '/'. $file, 0666);
  91. $str_restSql = $fh->getContents(DIR_FILES_BACKUPS . '/' . $file);
  92. //$str_restSql = file_get_contents(DIR_FILES_BACKUPS . '/' . $file);
  93. if (!$str_restSql) {
  94. $this->set("error",array("There was an error trying to restore the database. This file was empty."));
  95. $this->view();
  96. return false;
  97. }
  98. $crypt = Loader::helper('encryption');
  99. if ( !preg_match('/INSERT/m',$str_restSql) && !preg_match('/CREATE/m',$str_restSql) ) {
  100. $str_restSql = $crypt->decrypt($str_restSql);
  101. }
  102. $arr_sqlStmts = explode("\n\n",$str_restSql);
  103. foreach ($arr_sqlStmts as $str_stmt) {
  104. if (trim($str_stmt) != "") {
  105. $res_restoration = $db->execute($str_stmt);
  106. if (!$res_restoration) {
  107. $this->set("error",array("There was an error trying to restore the database. In query $str_stmt"));
  108. return;
  109. }
  110. }
  111. }
  112. $this->set("message","Restoration Sucessful");
  113. //reset perms for security!
  114. chmod(DIR_FILES_BACKUPS . '/'. $file, 000);
  115. Cache::flush();
  116. $this->view();
  117. }
  118. }