3 * Bacula(R) - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2015 Marcin Haba
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.Class.Errors');
25 class API extends TModule {
27 const API_VERSION = '0.1';
31 private $allowedErrors = array(
32 GenericError::ERROR_NO_ERRORS,
33 BconsoleError::ERROR_INVALID_COMMAND,
34 PoolError::ERROR_NO_VOLUMES_IN_POOL_TO_UPDATE
37 private function getConnection() {
39 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
40 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
41 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
42 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
43 curl_setopt($ch, CURLOPT_USERPWD, $this->appCfg['baculum']['login'] . ':' . $this->appCfg['baculum']['password']);
47 private function getAPIHeaders() {
49 'X-Baculum-API: ' . self::API_VERSION,
50 'X-Baculum-User: ' . $this->Application->User->getName(),
51 'X-Baculum-Pwd: ' . $this->Application->User->getPwd(),
52 'Accept: application/json'
57 public function init($config) {
58 $this->initSessionCache();
59 $this->appCfg = $this->Application->getModule('configuration')->getApplicationConfig();
62 private function getURL() {
63 $protocol = !empty($_SERVER['HTTPS']) ? 'https' : 'http';
64 $host = $_SERVER['SERVER_NAME'];
65 $port = $_SERVER['SERVER_PORT'];
66 $urlPrefix = $this->Application->getModule('friendly-url')->getUrlPrefix();
67 $url = sprintf('%s://%s:%d%s/', $protocol, $host, $port, $urlPrefix);
71 private function setParamsToUrl(&$url) {
72 $url .= (preg_match('/\?/', $url) === 1 ? '&' : '?' ) . 'director=' . ((array_key_exists('director', $_SESSION)) ? $_SESSION['director'] : '');
73 $this->Application->getModule('logging')->log(__FUNCTION__, PHP_EOL . PHP_EOL . 'EXECUTE URL ==> ' . $url . ' <==' . PHP_EOL . PHP_EOL, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
77 * API REQUESTS METHODS (get, set, create, delete)
80 public function get(array $params, $use_cache = false) {
83 if ($use_cache === true) {
84 $cached = $this->getSessionCache($params);
86 if (!is_null($cached)) {
89 $url = $this->getURL() . implode('/', $params);
90 $this->setParamsToUrl($url);
91 $ch = $this->getConnection();
92 curl_setopt($ch, CURLOPT_URL, $url);
93 curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getAPIHeaders());
94 $result = curl_exec($ch);
96 $ret = $this->preParseOutput($result);
97 if ($use_cache === true && $ret->error === 0) {
98 $this->setSessionCache($params, $ret);
104 public function set(array $params, array $options) {
105 $url = $this->getURL() . implode('/', $params);
106 $this->setParamsToUrl($url);
107 $data = http_build_query(array('update' => $options));
108 $ch = $this->getConnection();
109 curl_setopt($ch, CURLOPT_URL, $url);
110 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
111 curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($this->getAPIHeaders(), array('X-HTTP-Method-Override: PUT', 'Content-Length: ' . strlen($data), 'Expect:')));
112 curl_setopt($ch, CURLOPT_POST, true);
113 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
114 $result = curl_exec($ch);
116 return $this->preParseOutput($result);
119 public function create(array $params, array $options) {
120 $url = $this->getURL() . implode('/', $params);
121 $this->setParamsToUrl($url);
122 $data = http_build_query(array('create' => $options));
123 $ch = $this->getConnection();
124 curl_setopt($ch, CURLOPT_URL, $url);
125 curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($this->getAPIHeaders(), array('Expect:')));
126 curl_setopt($ch, CURLOPT_POST, true);
127 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
128 $result = curl_exec($ch);
130 return $this->preParseOutput($result);
133 public function remove(array $params) {
134 $url = $this->getURL() . implode('/', $params);
135 $this->setParamsToUrl($url);
136 $ch = $this->getConnection();
137 curl_setopt($ch, CURLOPT_URL, $url);
138 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
139 curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($this->getAPIHeaders(), array('X-HTTP-Method-Override: DELETE')));
140 $result = curl_exec($ch);
142 return $this->preParseOutput($result);
145 private function preParseOutput($result) {
146 $this->Application->getModule('logging')->log(__FUNCTION__, $result, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
147 $resource = json_decode($result);
149 if(is_object($resource) && property_exists($resource, 'error')) {
150 if(!in_array($resource->error, $this->allowedErrors)) {
151 $error = $resource->error;
154 $error = AuthorizationError::ERROR_AUTHORIZATION_TO_WEBGUI_PROBLEM;
157 $this->Application->getModule('logging')->log(__FUNCTION__, $resource, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
158 if(!is_null($error)) {
159 // Note! Redirection to error page takes place here.
160 $this->Response->redirect($this->Service->constructUrl('BaculumError',array('error' => $error), false));
166 public function initSessionCache($force = false) {
167 if (!isset($_SESSION) || !array_key_exists('cache', $_SESSION) || !is_array($_SESSION['cache']) || $force === true) {
168 $_SESSION['cache'] = array();
172 private function getSessionCache(array $params) {
174 $key = $this->getSessionKey($params);
175 if ($this->isSessionValue($key)) {
176 $cached = $_SESSION['cache'][$key];
181 private function setSessionCache(array $params, $value) {
182 $key = $this->getSessionKey($params);
183 $_SESSION['cache'][$key] = $value;
186 private function getSessionKey(array $params) {
187 $key = implode(';', $params);
188 $key = base64_encode($key);
192 private function isSessionValue($key) {
193 $is_value = array_key_exists($key, $_SESSION['cache']);