1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* sysPass |
4
|
|
|
* |
5
|
|
|
* @author nuxsmin |
6
|
|
|
* @link https://syspass.org |
7
|
|
|
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org |
8
|
|
|
* |
9
|
|
|
* This file is part of sysPass. |
10
|
|
|
* |
11
|
|
|
* sysPass is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* sysPass is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU General Public License |
22
|
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace SP\Services\Config; |
26
|
|
|
|
27
|
|
|
use SP\Config\ConfigData; |
28
|
|
|
use SP\Http\Json; |
29
|
|
|
use SP\Repositories\NoSuchItemException; |
30
|
|
|
use SP\Services\Service; |
31
|
|
|
use SP\Services\ServiceException; |
32
|
|
|
use SP\Util\Util; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Class ConfigBackupService |
36
|
|
|
* |
37
|
|
|
* @package SP\Services\Config |
38
|
|
|
*/ |
39
|
|
|
final class ConfigBackupService extends Service |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @var ConfigService |
43
|
|
|
*/ |
44
|
|
|
protected $configService; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $configData |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
* @throws \SP\Core\Exceptions\SPException |
51
|
|
|
*/ |
52
|
|
|
public static function configToJson(string $configData): string |
53
|
|
|
{ |
54
|
|
|
return Json::getJson(Util::unserialize(ConfigData::class, $configData), JSON_PRETTY_PRINT); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $configData |
59
|
|
|
*/ |
60
|
|
|
public static function configToXml(string $configData) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
throw new \RuntimeException('Not implemented'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Backs up the config data into the database |
67
|
|
|
* |
68
|
|
|
* @param ConfigData $configData |
69
|
|
|
*/ |
70
|
|
|
public function backup(ConfigData $configData) |
71
|
|
|
{ |
72
|
|
|
try { |
73
|
|
|
$this->configService->save('config_backup', $this->packConfigData($configData)); |
74
|
|
|
$this->configService->save('config_backup_date', time()); |
75
|
|
|
} catch (\Exception $e) { |
76
|
|
|
processException($e); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param ConfigData $configData |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
private function packConfigData(ConfigData $configData) |
86
|
|
|
{ |
87
|
|
|
return bin2hex(gzcompress(serialize($configData))); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return ConfigData |
92
|
|
|
* @throws ServiceException |
93
|
|
|
* @throws \DI\DependencyException |
94
|
|
|
* @throws \DI\NotFoundException |
95
|
|
|
* @throws \SP\Storage\File\FileException |
96
|
|
|
*/ |
97
|
|
|
public function restore() |
98
|
|
|
{ |
99
|
|
|
return $this->config->saveConfig(Util::unserialize( |
|
|
|
|
100
|
|
|
ConfigData::class, |
101
|
|
|
$this->getBackup()) |
102
|
|
|
)->getConfigData(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return ConfigData |
107
|
|
|
* @throws ServiceException |
108
|
|
|
*/ |
109
|
|
|
public function getBackup(): string |
110
|
|
|
{ |
111
|
|
|
try { |
112
|
|
|
$data = $this->configService->getByParam('config_backup'); |
113
|
|
|
|
114
|
|
|
if ($data === null) { |
115
|
|
|
throw new ServiceException(__u('No es posible restaurar la configuración')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return gzuncompress(hex2bin($data)); |
|
|
|
|
119
|
|
|
} catch (NoSuchItemException $e) { |
120
|
|
|
processException($e); |
121
|
|
|
|
122
|
|
|
throw new ServiceException(__u('No es posible restaurar la configuración')); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function initialize() |
127
|
|
|
{ |
128
|
|
|
$this->configService = $this->dic->get(ConfigService::class); |
129
|
|
|
} |
130
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.