Subversion Repositories oidplus

Rev

Rev 1374 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. use ViaThinkSoft\OIDplus\OIDplus;
  21. use ViaThinkSoft\OIDplus\OIDplusException;
  22. use ViaThinkSoft\OIDplus\OIDplusGui;
  23. use ViaThinkSoft\OIDplus\OIDplusHtmlException;
  24. use ViaThinkSoft\OIDplus\OIDplusPageAdminDatabaseBackup;
  25.  
  26. require_once __DIR__ . '/../../../../includes/oidplus.inc.php';
  27.  
  28. set_exception_handler(array(OIDplusGui::class, 'html_exception_handler'));
  29.  
  30. @set_time_limit(0);
  31.  
  32. header('Content-Type:text/html; charset=UTF-8');
  33.  
  34. ob_start();
  35.  
  36. OIDplus::init(true);
  37.  
  38. if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_ViaThinkSoft\OIDplus\OIDplusPageAdminDatabaseBackup', false)) {
  39.         throw new OIDplusException(_L('This plugin was disabled by the system administrator!'));
  40. }
  41.  
  42. # ---
  43.  
  44. if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  45.         if (PHP_SAPI == 'cli') {
  46.                 // echo "You need to log in as administrator.\n";
  47.                 // die();
  48.         } else {
  49.                 throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.','href="'.OIDplus::webpath(null,OIDplus::PATH_RELATIVE).'?goto=oidplus%3Alogin%24admin"'), null, 401);
  50.         }
  51. }
  52.  
  53. $exp_objects = oidplus_is_true($_POST['database_backup_export_objects'] ?? false);
  54. $exp_ra = oidplus_is_true($_POST['database_backup_export_ra'] ?? false);
  55. $exp_config = oidplus_is_true($_POST['database_backup_export_config'] ?? false);
  56. $exp_log = oidplus_is_true($_POST['database_backup_export_log'] ?? false);
  57. $exp_pki = oidplus_is_true($_POST['database_backup_export_pki'] ?? false);
  58. $encrypt = oidplus_is_true($_POST['database_backup_export_encrypt'] ?? false);
  59. $password1 = $_POST['database_backup_export_password1'] ?? "";
  60. $password2 = $_POST['database_backup_export_password2'] ?? "";
  61.  
  62. if ($encrypt) {
  63.         if ($password1 !== $password2) {
  64.                 throw new OIDplusException(_L("Passwords do not match"));
  65.         }
  66. }
  67.  
  68. $encoded_data = OIDplusPageAdminDatabaseBackup::createBackup(false, $exp_objects, $exp_ra, $exp_config, $exp_log, $exp_pki);
  69.  
  70. if ($encrypt) {
  71.         $encoded_data = json_encode(json_decode($encoded_data),JSON_UNESCAPED_SLASHES); // remove pretty-print to save space
  72.         if (function_exists('gzdeflate')) {
  73.                 $encoded_data_gz = @gzdeflate($encoded_data, 9);
  74.                 if ($encoded_data_gz) $encoded_data = 'GZIP'.$encoded_data_gz;
  75.         }
  76.  
  77.         $encoded_data = chunk_split(base64_encode(encrypt_str($encoded_data, $password1)));
  78.         $encoded_data =
  79.                 "-----BEGIN OIDPLUS ENCRYPTED DATABASE BACKUP-----\r\n".
  80.                 htmlentities($encoded_data).
  81.                 "-----END OIDPLUS ENCRYPTED DATABASE BACKUP-----\r\n";
  82. }
  83.  
  84. $title = OIDplus::config()->getValue('system_title', 'oidplus');
  85.  
  86. $sysid = OIDplus::getSystemId();
  87.  
  88. OIDplus::invoke_shutdown();
  89.  
  90. $cont = ob_get_contents();
  91. ob_end_clean();
  92.  
  93. if ($cont) {
  94.         // There was some kind of unexpected output. We must not download the file in this case
  95.         die($cont);
  96. }
  97.  
  98. $filename = preg_replace('@[^a-z0-9]@', '-', strtolower($title)).($sysid ? '-'.$sysid : '').'-backup-' . date('Y-m-d-H-i-s');
  99. if ($encrypt) {
  100.         $filename .= '-encrypted.bak';
  101.         header('Content-Type: application/octet-stream');
  102. } else {
  103.         $filename .= '.json';
  104.         header('Content-Type: application/json');
  105. }
  106.  
  107. if (function_exists('gzencode')) {
  108.         $tmp = @gzencode($encoded_data, 9);
  109.         if ($tmp) {
  110.                 $encoded_data = $tmp;
  111.                 $filename .= '.gz';
  112.                 header('Content-Type: application/x-gzip');
  113.         }
  114. }
  115.  
  116. header('Content-Disposition: attachment; filename='.$filename);
  117. header('Content-Length: '.strlen($encoded_data));
  118. echo $encoded_data;
  119.