Subversion Repositories oidplus

Rev

Rev 1362 | 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. OIDplus::init(true);
  35.  
  36. if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_ViaThinkSoft\OIDplus\OIDplusPageAdminDatabaseBackup', false)) {
  37.         throw new OIDplusException(_L('This plugin was disabled by the system administrator!'));
  38. }
  39.  
  40. # ---
  41.  
  42. if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  43.         if (PHP_SAPI == 'cli') {
  44.                 // echo "You need to log in as administrator.\n";
  45.                 // die();
  46.         } else {
  47.                 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);
  48.         }
  49. }
  50.  
  51. $exp_objects = oidplus_is_true($_POST['database_backup_import_objects'] ?? false);
  52. $exp_ra = oidplus_is_true($_POST['database_backup_import_ra'] ?? false);
  53. $exp_config = oidplus_is_true($_POST['database_backup_import_config'] ?? false);
  54. $exp_log = oidplus_is_true($_POST['database_backup_import_log'] ?? false);
  55. $exp_pki = oidplus_is_true($_POST['database_backup_import_pki'] ?? false);
  56. $password = $_POST['database_backup_import_password'] ?? "";
  57.  
  58. if (!isset($_FILES['userfile'])) {
  59.         throw new OIDplusException(_L('Please choose a file.'));
  60. }
  61.  
  62. if ($_FILES['userfile']['error']) {
  63.         throw new OIDplusException(_L('Could not receive file (probably it is too large?)'));
  64. }
  65.  
  66. $encoded_data = file_get_contents($_FILES['userfile']['tmp_name']);
  67.  
  68. if (strtolower(substr($_FILES['userfile']['name']??"",-3)) == '.gz') {
  69.         if (function_exists('gzdecode')) {
  70.                 $tmp = @gzdecode($encoded_data);
  71.                 if ($tmp) {
  72.                         $encoded_data = $tmp;
  73.                 } else {
  74.                         throw new OIDplusException(_L("Cannot decompress backup file because PHP ZLib extension is not installed"));
  75.                 }
  76.         } else {
  77.                 throw new OIDplusException(_L("Cannot decompress backup file because PHP ZLib extension is not installed"));
  78.         }
  79. }
  80.  
  81. if (preg_match('@-----BEGIN OIDPLUS ENCRYPTED DATABASE BACKUP-----(.+)-----END OIDPLUS ENCRYPTED DATABASE BACKUP-----@ismU', $encoded_data, $m)) {
  82.         $encoded_data = $m[1];
  83.         $encoded_data = base64_decode($encoded_data);
  84.         $encoded_data = decrypt_str($encoded_data, $password);
  85.         if (substr($encoded_data,0,4) === 'GZIP') {
  86.                 if (!function_exists('gzinflate')) {
  87.                         throw new OIDplusException(_L("Cannot decompress backup file because PHP ZLib extension is not installed"));
  88.                 }
  89.                 $encoded_data = gzinflate(substr($encoded_data,4));
  90.         }
  91. }
  92.  
  93. OIDplusPageAdminDatabaseBackup::restoreBackup(true, $encoded_data, $exp_objects, $exp_ra, $exp_config, $exp_log, $exp_pki);
  94.  
  95. OIDplus::invoke_shutdown();
  96.