index.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2019
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes
  22. include "root.php";
  23. require_once "resources/require.php";
  24. require_once "resources/check_auth.php";
  25. //check permissions
  26. if (permission_exists("backup_download")) {
  27. //access granted
  28. }
  29. else {
  30. echo "access denied";
  31. exit;
  32. }
  33. //add multi-lingual support
  34. $language = new text;
  35. $text = $language->get();
  36. //download the backup
  37. if ($_GET['a'] == "download" && permission_exists('backup_download')) {
  38. //get the file format
  39. $file_format = $_GET['file_format'];
  40. $file_format = ($file_format != '') ? $file_format : 'tgz';
  41. //build the backup file
  42. $backup_path = ($_SESSION['server']['backup']['path'] != '') ? $_SESSION['server']['backup']['path'] : '/tmp';
  43. $backup_file = 'backup_'.date('Ymd_His').'.'.$file_format;
  44. if (count($_SESSION['backup']['path']) > 0) {
  45. //determine compression method
  46. switch ($file_format) {
  47. case "rar" : $cmd = 'rar a -ow -r '; break;
  48. case "zip" : $cmd = 'zip -r '; break;
  49. case "tbz" : $cmd = 'tar -jvcf '; break;
  50. default : $cmd = 'tar -zvcf ';
  51. }
  52. $cmd .= $backup_path.'/'.$backup_file.' ';
  53. if (isset($_SESSION['backup']['path'])) {
  54. foreach ($_SESSION['backup']['path'] as $value) {
  55. if (file_exists($value)) {
  56. $cmd .= $value.' ';
  57. }
  58. }
  59. }
  60. $cmd .= " 2>&1";
  61. exec($cmd, $response, $restore_errlevel);
  62. $response_txt = "<br>" . implode("<br>", $response);
  63. //download the file
  64. session_cache_limiter('public');
  65. if (file_exists($backup_path."/".$backup_file)) {
  66. $fd = fopen($backup_path."/".$backup_file, 'rb');
  67. header("Content-Type: application/octet-stream");
  68. header("Content-Transfer-Encoding: binary");
  69. header("Content-Description: File Transfer");
  70. header('Content-Disposition: attachment; filename='.$backup_file);
  71. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  72. header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  73. header("Content-Length: ".filesize($backup_path."/".$backup_file));
  74. header("Pragma: no-cache");
  75. header("Expires: 0");
  76. ob_clean();
  77. fpassthru($fd);
  78. exit;
  79. }
  80. else {
  81. //set response message
  82. message::add($text['message-backup_failed_format'] . $response_txt, 'negative');
  83. header("Location: ".$_SERVER['PHP_SELF']);
  84. exit;
  85. }
  86. }
  87. else {
  88. //set response message
  89. message::add($text['message-backup_failed_paths'], 'negative');
  90. header("Location: ".$_SERVER['PHP_SELF']);
  91. exit;
  92. }
  93. }
  94. //script a backup (cron)
  95. if ($_GET['a'] == "script" && permission_exists('backup_download')) {
  96. $file_format = $_GET['file_format'];
  97. $target_type = "script";
  98. $backup = new backup;
  99. $command = $backup->command("backup", $file_format);
  100. }
  101. //restore a backup
  102. if ($_POST['a'] == "restore" && permission_exists('backup_upload')) {
  103. $backup_path = ($_SESSION['server']['backup']['path'] != '') ? $_SESSION['server']['backup']['path'] : '/tmp';
  104. $backup_file = $_FILES['backup_file']['name'];
  105. if (is_uploaded_file($_FILES['backup_file']['tmp_name']) && file_exists($backup_path.'/'.$backup_file)) {
  106. //move temp file to backup path
  107. move_uploaded_file($_FILES['backup_file']['tmp_name'], $backup_path.'/'.$backup_file);
  108. //determine file format and restore backup
  109. $file_format = pathinfo($_FILES['backup_file']['name'], PATHINFO_EXTENSION);
  110. $valid_format = true;
  111. switch ($file_format) {
  112. case "rar" : $cmd = 'rar x -ow -o+ '.$backup_path.'/'.$backup_file.' /'; break;
  113. case "zip" : $cmd = 'umask 755; unzip -o -qq -X -K '.$backup_path.'/'.$backup_file.' -d /'; break;
  114. case "tbz" : $cmd = 'tar -xvpjf '.$backup_path.'/'.$backup_file.' -C /'; break;
  115. case "tgz" : $cmd = 'tar -xvpzf '.$backup_path.'/'.$backup_file.' -C /'; break;
  116. default: $valid_format = false;
  117. }
  118. if (!$valid_format) {
  119. @unlink($backup_path.'/'.$backup_file);
  120. message::add($text['message-restore_failed_format'], 'negative');
  121. header("Location: ".$_SERVER['PHP_SELF']);
  122. exit;
  123. }
  124. else {
  125. $cmd .= ' 2>&1';
  126. exec($cmd, $response, $restore_errlevel);
  127. $response_txt = "<br>" . implode("<br>", $response);
  128. if ($restore_errlevel == 0) {
  129. //set response message
  130. message::add($text['message-restore_completed']);
  131. header("Location: ".$_SERVER['PHP_SELF']);
  132. exit;
  133. } else {
  134. message::add($text['message-restore_failed_extract'] . $response_txt, 'negative');
  135. header("Location: ".$_SERVER['PHP_SELF']);
  136. exit;
  137. }
  138. }
  139. }
  140. else {
  141. //set response message
  142. message::add($text['message-restore_failed_upload'], 'negative');
  143. header("Location: ".$_SERVER['PHP_SELF']);
  144. exit;
  145. }
  146. }
  147. //add the header
  148. require_once "resources/header.php";
  149. $document['title'] = $text['title-destinations'];
  150. // backup type switch javascript
  151. echo "<script language='javascript' type='text/javascript'>";
  152. echo " var fade_speed = 400;";
  153. echo " function toggle_target(first_elem, second_elem) {";
  154. echo " $('#command').fadeOut(fade_speed);";
  155. echo " $('#'+first_elem).fadeToggle(fade_speed, function() {";
  156. echo " $('#command').slideUp(fade_speed, function() {";
  157. echo " $('#'+second_elem).fadeToggle(fade_speed);";
  158. echo " });";
  159. echo " });";
  160. echo " }";
  161. echo "</script>";
  162. //show the content
  163. echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'>\n";
  164. echo " <tr>\n";
  165. echo " <td width='50%' valign='top'>\n";
  166. echo "<b>".$text['header-backup']."</b>\n";
  167. echo "<br><br>";
  168. echo $text['description-backup']."\n";
  169. echo "<br><br><br>";
  170. echo "<table border='0' cellpadding='0' cellspacing='0' width='100%'>\n";
  171. echo "<tr>\n";
  172. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  173. echo " ".$text['label-source_paths']."\n";
  174. echo "</td>\n";
  175. echo "<td width='70%' class='vtable' align='left'>\n";
  176. if (isset($_SESSION['backup']['path'])) foreach ($_SESSION['backup']['path'] as $backup_path) {
  177. echo $backup_path."<br>\n";
  178. }
  179. echo "</td>";
  180. echo "</tr>";
  181. echo "<tr>\n";
  182. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  183. echo " ".$text['label-file_format']."\n";
  184. echo "</td>\n";
  185. echo "<td class='vtable' align='left'>\n";
  186. echo " <select class='formfld' name='file_format' id='file_format'>";
  187. echo " <option value='tgz' ".(($file_format == 'tgz') ? 'selected' : null).">TAR GZIP</option>";
  188. echo " <option value='tbz' ".(($file_format == 'tbz') ? 'selected' : null).">TAR BZIP</option>";
  189. echo " <option value='rar' ".(($file_format == 'rar') ? 'selected' : null).">RAR</option>";
  190. echo " <option value='zip' ".(($file_format == 'zip') ? 'selected' : null).">ZIP</option>";
  191. echo " </select>";
  192. echo "</td>";
  193. echo "</tr>";
  194. echo "<tr>\n";
  195. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  196. echo " ".$text['label-target_type']."\n";
  197. echo "</td>\n";
  198. echo "<td class='vtable' align='left'>\n";
  199. echo " <select class='formfld' name='target_type' id='target_type' onchange=\"(this.selectedIndex == 0) ? toggle_target('btn_script','btn_download') : toggle_target('btn_download','btn_script');\">";
  200. echo " <option value='download'>".$text['option-file_download']."</option>";
  201. echo " <option value='script' ".(($target_type == 'script') ? 'selected' : null).">".$text['option-command']."</option>";
  202. echo " </select>";
  203. echo "</td>";
  204. echo "</tr>";
  205. echo "</table>";
  206. echo "<div id='command' ".(($command == '') ? "style='display: none;'" : null).">";
  207. echo "<table border='0' cellpadding='0' cellspacing='0' width='100%'>\n";
  208. echo "<tr>\n";
  209. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  210. echo " ".$text['label-command']."\n";
  211. echo "</td>\n";
  212. echo "<td width='70%' class='vtable' align='left'>\n";
  213. echo " <textarea class='formfld' style='width: 100%; height: 200px; font-family: courier;'>".$command."</textarea>";
  214. echo "</td>";
  215. echo "</tr>";
  216. echo "</table>";
  217. echo "</div>";
  218. echo "<br>";
  219. echo "<div align='right'>";
  220. echo "<input type='button' id='btn_script' class='btn' ".(($target_type != 'script') ? "style='display: none;'" : null)." value='".$text['button-generate']."' onclick=\"document.location.href='".PROJECT_PATH."/app/backup/index.php?a=script&file_format='+document.getElementById('file_format').options[document.getElementById('file_format').selectedIndex].value;\">";
  221. echo "<input type='button' id='btn_download' class='btn' ".(($target_type == 'script') ? "style='display: none;'" : null)." value='".$text['button-download']."' onclick=\"document.location.href='".PROJECT_PATH."/app/backup/index.php?a=download&file_format='+document.getElementById('file_format').options[document.getElementById('file_format').selectedIndex].value;\">";
  222. echo "</div>";
  223. echo "<br><br>";
  224. if (permission_exists("backup_upload")) {
  225. echo " </td>\n";
  226. echo " <td width='20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>\n";
  227. echo " <td width='50%' valign='top'>\n";
  228. echo " <b>".$text['header-restore']."</b>\n";
  229. echo " <br><br>";
  230. echo $text['description-restore']."\n";
  231. echo " <br><br><br>";
  232. echo " <div align='center'>";
  233. echo " <form name='frmrestore' method='post' enctype='multipart/form-data' action=''>";
  234. echo " <input type='hidden' name='a' value='restore'>";
  235. echo " <table>";
  236. echo " <tr>";
  237. echo " <td nowrap>".$text['label-select_backup']."&nbsp;</td>";
  238. echo " <td><input type='file' class='formfld fileinput' name='backup_file'></td>";
  239. echo " <td><input type='submit' class='btn' value='".$text['button-restore']."'></td>";
  240. echo " </tr>";
  241. echo " </table>";
  242. echo " <br>";
  243. echo " <span style='font-weight: bold; text-decoration: underline; color: #000;'>".$text['description-restore_warning']."</span>";
  244. echo " </form>\n";
  245. echo " </div>";
  246. echo " </td>\n";
  247. }
  248. echo " </tr>\n";
  249. echo "</table>\n";
  250. echo "<br><br>";
  251. //show the footer
  252. require_once "resources/footer.php";
  253. ?>