Sponsorenverwaltung - Team StarCraft e.V.
 All Data Structures Files Functions Variables
backup.php
Go to the documentation of this file.
1 <?php
2  /**
3  * @file backup.php
4  *
5  * @brief Provides the ability to create a MySQL-dump and to store it into
6  * an ZIP-file togehter with the contents of the data subdirectory.
7  *
8  * @details
9  * This script provides the ability to create and to store a MySQL-dump and/or
10  * the contents of the data subdirectory into a downloadable .zip-file.
11  *
12  * This file has a hardcoded restriction to be accessible for the root user only.
13  *
14  * This file depends on inc/common.php and inc/templates/backup.tpl
15  *
16  * @copyright 2013, Team StarCraft e.V.
17  * @version 1.0.0
18  * @author Daniel Seichter
19  * @author Alexander Vorndran
20  * @date 02.07.2013
21  */
22 
23  // include
24  include("inc/common.php");
25 
26 
27  /**
28  * Adds files to a zip archive (recursive)
29  * @param mixed &$zip zip archive object
30  * @param string $dir directory to add
31  * @param string $archivBase archive base path for adding
32  * @param integer $depth recursion depth
33  * @author Daniel Seichter
34  */
35  function addFilesToZip(&$zip, $dir, $archivBase, $depth = 0) {
36  if($handle = opendir($dir)) {
37  while (false !== ($file = readdir($handle))) {
38  if(($file != ".") && ($file != "..")) {
39  if((is_dir($dir . '/' . $file)) && (!is_link($file))) {
40  addFilesToZip($zip, $dir . '/' . $file, $archivBase . '/' . $file, $depth + 1);
41  } else {
42  $zip->addFile($dir . '/' . $file, substr($archivBase . '/' . $file, 1));
43  }
44  }
45  }
46  closedir($handle);
47  }
48  if($depth === 0) {
49  clearStatCache();
50  }
51  }
52 
53  /// @cond MAINPART
54 
55  // usercake
56  if(!accessGranted($_SERVER['PHP_SELF'])) {
57  if(isUserLoggedIn()) {
58  exitWithErrorTemplate(array('Die angeforderte Seite ist gesperrt.'));
59  }
60  else {
61  exitWithErrorTemplateAndRedirect(array('Die angeforderte Seite ist gesperrt oder geschützt.'), 'login.php', 2);
62  }
63  }
64 
65  if(isUserRoot()) {
66  // if the user is a root he can perform this
67  if(isset($_GET['type'])) {
68  // do the action the user specified
69  $curType = (isset($_GET['type'])) ? trim($_GET['type']) : 'complete'; //types: complete, datafiles, onlydb
70  switch ($curType) {
71  case 'complete':
72  case 'datafiles':
73  case 'onlydb':
74  break;
75  default:
76  $curType = 'complete';
77  break;
78  }
79 
80  // create a file with a unique filename in the system's temporary directory
81  $file = tempnam("/tmp", "zip");
82  // create zip-file
83  $zip = new ZipArchive();
84  if($zip->open($file, ZipArchive::OVERWRITE) === true) {
85  if(($curType == 'complete') || ($curType == 'onlydb')) {
86  $filename = 'mysqldump_date_' . date("d_m_Y", time()) . '.sql';
87 
88  // used for local testing
89  $mysqlPath = "";
90 
91  // perform a mysql dump
92  $dumpCommand = $mysqlPath.'mysqldump --add-drop-table --create-options -c -n -h ' . MYSQL_HOST . ' --user=' . MYSQL_USER . ' --pass=' . MYSQL_PWD . ' ' . MYSQL_DB . ' > ' . ABS_PATH . $filename;
93  $shellReturn = shell_exec($dumpCommand);
94  $zip->addFile(ABS_PATH . $filename, $filename);
95  }
96  if(($curType == 'complete') || ($curType == 'datafiles')) {
97  $path = ABS_PATH.'data';
98  addFilesToZip($zip, $path, '/data');
99  }
100  $zip->close();
101  // stream the file to the client
102  header("Content-Type: application/zip");
103  header("Content-Length: " . filesize($file));
104  header("Content-Disposition: attachment; filename=\"sponsoren_backup_" . $curType . "_" . date("d_m_Y", time()) . ".zip\"");
105  readfile($file);
106  }
107  else {
108  exitWithErrorTemplate(array("ZIP-Datei kann nicht erstellt werden"));
109  }
110 
111  //delete temporary files
112  unlink($file);
113  unlink(ABS_PATH . $filename);
114  }
115  else {
116  displayTemplateWithErrorsAndSuccesses("Backup", 'backup.tpl');
117  }
118  }
119  else {
120  exitWithErrorTemplate(array("Zugriff verweigert."));
121  }
122  /// @endcond
123 ?>