3 * Bacula(R) - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2016 Kern Sibbald
8 * The main author of Baculum is Marcin Haba.
9 * The original author of Bacula is Kern Sibbald, with contributions
10 * from many others, a complete list can be found in the file AUTHORS.
12 * You may use this file and others of this release according to the
13 * license defined in the LICENSE file, which includes the Affero General
14 * Public License, v3.0 ("AGPLv3") and some additional permissions and
15 * terms pursuant to its AGPLv3 Section 7.
17 * This notice must be preserved when any source code is
18 * conveyed and/or propagated.
20 * Bacula(R) is a registered trademark of Kern Sibbald.
23 Prado::using('Application.Common.Class.ConfigFileModule');
26 * Manage API configuration.
27 * Module is responsible for get/set API config data.
29 * @author Marcin Haba <marcin.haba@bacula.pl>
31 class APIConfig extends ConfigFileModule {
34 * Default application language
36 const DEFAULT_LANGUAGE = 'en';
39 * API config file path
41 const CONFIG_FILE_PATH = 'Application.API.Config.api';
44 * API config file format
46 const CONFIG_FILE_FORMAT = 'ini';
51 const JSON_TOOL_DIR_TYPE = 'dir';
52 const JSON_TOOL_SD_TYPE = 'sd';
53 const JSON_TOOL_FD_TYPE = 'fd';
54 const JSON_TOOL_BCONS_TYPE = 'bcons';
57 * These options are obligatory for API config.
59 private $required_options = array(
60 'api' => array('auth_type', 'debug'),
61 'db' => array('type', 'name', 'login', 'password', 'ip_addr', 'port', 'path'),
62 'bconsole' => array('bin_path', 'cfg_path', 'use_sudo'),
63 'jsontools' => array('enabled')
67 * Get (read) API config.
70 * @param string $section config section name
71 * @return array config
73 public function getConfig($section = null) {
74 $config = $this->readConfig(self::CONFIG_FILE_PATH, self::CONFIG_FILE_FORMAT);
75 if ($this->validateConfig($config) === true) {
76 if (!is_null($section)) {
77 $config = array_key_exists($section, $this->required_options) ? $config[$section] : array();
86 * Set (save) API config.
89 * @param array $config config
90 * @return boolean true if config saved successfully, otherwise false
92 public function setConfig(array $config) {
94 if ($this->validateConfig($config) === true) {
95 $result = $this->writeConfig($config, self::CONFIG_FILE_PATH, self::CONFIG_FILE_FORMAT);
101 * Validate API config.
102 * Config validation should be used as early as config data is available.
103 * Validation is done in read/write config methods.
106 * @param array $config config
107 * @return boolean true if config valid, otherwise false
109 private function validateConfig(array $config = array()) {
110 $is_valid = $this->isConfigValid($this->required_options, $config, self::CONFIG_FILE_FORMAT, self::CONFIG_FILE_PATH);
114 private function getJSONToolTypes() {
116 self::JSON_TOOL_DIR_TYPE,
117 self::JSON_TOOL_SD_TYPE,
118 self::JSON_TOOL_FD_TYPE,
119 self::JSON_TOOL_BCONS_TYPE
125 * Check if JSON tools are configured for application.
128 * @return boolean true if JSON tools are configured, otherwise false
130 public function isJSONToolsConfigured() {
131 $config = $this->getConfig();
132 $configured = array_key_exists('jsontools', $config);
136 public function isJSONToolConfigured($tool_type) {
138 $tool = $this->getJSONToolOptions($tool_type);
139 $config = $this->getJSONToolsConfig();
140 $is_bin = array_key_exists($tool['bin'], $config) && !empty($config[$tool['bin']]);
141 $is_cfg = array_key_exists($tool['cfg'], $config) && !empty($config[$tool['cfg']]);
142 if ($is_bin === true && $is_cfg === true) {
148 private function getJSONToolOptions($tool_type) {
150 'bin' => "b{$tool_type}json_path",
151 'cfg' => "{$tool_type}_cfg_path"
157 * Check if JSON tools support is enabled.
160 * @return boolean true if JSON tools support is enabled, otherwise false
162 public function isJSONToolsEnabled() {
164 if ($this->isJSONToolsConfigured() === true) {
165 $config = $this->getConfig();
166 $enabled = ($config['jsontools']['enabled'] == 1);
172 * Get JSON tools config parameters.
174 * @return array JSON tools config parameters
176 public function getJSONToolsConfig() {
178 if ($this->isJSONToolsConfigured() === true) {
179 $config = $this->getConfig();
180 $cfg = $config['jsontools'];
185 public function getJSONToolConfig($tool_type) {
186 $tool = array('bin' => '', 'cfg' => '', 'use_sudo' => false);
187 $tools = $this->getSupportedJSONTools();
188 $config = $this->getJSONToolsConfig();
189 if (in_array($tool_type, $tools)) {
190 $opt = $this->getJSONToolOptions($tool_type);
191 $tool['bin'] = $config[$opt['bin']];
192 $tool['cfg'] = $config[$opt['cfg']];
193 $tool['use_sudo'] = ($config['use_sudo'] == 1);
199 * Save JSON tools config parameters.
201 * JSON tools config params can be provided in following form:
204 * 'bconfig_dir' => '/path/config/',
205 * 'use_sudo' => false,
206 * 'bdirjson_path' => '/path1/bdirjson',
207 * 'dir_cfg_path' => '/path2/bacula-dir.conf',
208 * 'bsdjson_path' => '/path1/bsdjson',
209 * 'sd_cfg_path' => '/path2/bacula-sd.conf',
210 * 'bfdjson_path' => '/path1/bfdjson',
211 * 'fd_cfg_path' => '/path2/bacula-fd.conf',
212 * 'bbconsjson_path' => '/path1/bbconsjson',
213 * 'bcons_cfg_path' => '/path2/bconsole.conf'
216 * Please note that there is not required to provide all JSON tools params at once
217 * but they should be provided in pairs (tool and cfg paths).
219 * @param array $jsontools_config associative array with JSON tools parameters
220 * @return boolean true if JSON tools parameters saved successfully, otherwise false
222 public function saveJSONToolsConfig(array $jsontools_config) {
225 $config = $this->getConfig();
227 if ($this->isJSONToolsConfigured() === false) {
228 $config['jsontools'] = array();
230 if (array_key_exists('enabled', $jsontools_config)) {
231 $config['jsontools']['enabled'] = ($jsontools_config['enabled'] === true) ? 1 : 0;
234 // @TOVERIFY: Check if bconfig_dir will be ever needed and used.
235 if (array_key_exists('bconfig_dir', $jsontools_config)) {
236 $bconfig_dir = rtrim($jsontools_config['bconfig_dir'], '/');
237 $config['jsontools']['bconfig_dir'] = $bconfig_dir;
240 if (array_key_exists('use_sudo', $jsontools_config)) {
241 $config['jsontools']['use_sudo'] = ($jsontools_config['use_sudo'] === true) ? 1 : 0;
245 $types = $this->getJSONToolTypes();
246 for ($i = 0; $i < count($types); $i++) {
247 $opt = $this->getJSONToolOptions($types[$i]);
248 if (array_key_exists($opt['bin'], $jsontools_config) && array_key_exists($opt['cfg'], $jsontools_config)) {
249 $config['jsontools'][$opt['bin']] = $jsontools_config[$opt['bin']];
250 $config['jsontools'][$opt['cfg']] = $jsontools_config[$opt['cfg']];
255 if ($added === true) {
256 $saved = $this->setConfig($config);
261 public function getSupportedJSONTools() {
263 $types = $this->getJSONToolTypes();
264 for ($i = 0; $i < count($types); $i++) {
265 if ($this->isJSONToolConfigured($types[$i]) === true) {
266 array_push($tools, $types[$i]);