3 * Bacula® - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2014 Marcin Haba
8 * The main author of Baculum is Marcin Haba.
9 * The main author of Bacula is Kern Sibbald, with contributions from many
10 * 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 * Bacula® is a registered trademark of Kern Sibbald.
20 Prado::using('Application.Class.Errors');
22 class API extends TModule {
24 const API_VERSION = '0.1';
28 private $allowedErrors = array(
29 GenericError::ERROR_NO_ERRORS,
30 BconsoleError::ERROR_INVALID_COMMAND
33 private function getConnection() {
35 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
36 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
37 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
38 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
39 curl_setopt($ch, CURLOPT_USERPWD, $this->appCfg['baculum']['login'] . ':' . $this->appCfg['baculum']['password']);
43 private function getAPIHeader() {
44 return 'X-Baculum-API: ' . self::API_VERSION;
47 public function init($config) {
48 $this->appCfg = $this->Application->getModule('configuration')->getApplicationConfig();
51 private function getURL() {
52 $protocol = !empty($_SERVER['HTTPS']) ? 'https' : 'http';
53 $host = $_SERVER['SERVER_NAME'];
54 $port = $_SERVER['SERVER_PORT'];
55 $urlPrefix = $this->Application->getModule('friendly-url')->getUrlPrefix();
56 $url = sprintf('%s://%s:%d%s/', $protocol, $host, $port, $urlPrefix);
60 private function setParamsToUrl(&$url) {
61 $url .= (preg_match('/\?/', $url) === 1 ? '&' : '?' ) . 'director=' . ((array_key_exists('director', $_SESSION)) ? $_SESSION['director'] : '');
63 * If user is not equal admin user then it is added to URL,
64 * then will be used custom console for this user.
66 if($this->User->getIsAdmin() === false) {
67 $url .= '&user=' . $this->User->getName();
69 $this->Application->getModule('logging')->log(__FUNCTION__, PHP_EOL . PHP_EOL . 'EXECUTE URL ==> ' . $url . ' <==' . PHP_EOL . PHP_EOL, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
73 * API REQUESTS METHODS (get, set, create, delete)
76 public function get(array $params) {
77 $url = $this->getURL() . implode('/', $params);
78 $this->setParamsToUrl($url);
79 $ch = $this->getConnection();
80 curl_setopt($ch, CURLOPT_URL, $url);
81 curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json'));
82 $result = curl_exec($ch);
84 return $this->preParseOutput($result);
87 public function set(array $params, array $options) {
88 $url = $this->getURL() . implode('/', $params);
89 $this->setParamsToUrl($url);
90 $data = http_build_query(array('update' => $options));
91 $ch = $this->getConnection();
92 curl_setopt($ch, CURLOPT_URL, $url);
93 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
94 curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json', 'X-HTTP-Method-Override: PUT', 'Content-Length: ' . strlen($data)));
95 curl_setopt($ch, CURLOPT_POST, true);
96 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
97 $result = curl_exec($ch);
99 return $this->preParseOutput($result);
102 public function create(array $params, array $options) {
103 $url = $this->getURL() . implode('/', $params);
104 $this->setParamsToUrl($url);
105 $data = http_build_query(array('create' => $options));
106 $ch = $this->getConnection();
107 curl_setopt($ch, CURLOPT_URL, $url);
108 curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json'));
109 curl_setopt($ch, CURLOPT_POST, true);
110 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
111 $result = curl_exec($ch);
113 return $this->preParseOutput($result);
116 public function remove(array $params) {
117 $url = $this->getURL() . implode('/', $params);
118 $this->setParamsToUrl($url);
119 $ch = $this->getConnection();
120 curl_setopt($ch, CURLOPT_URL, $url);
121 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
122 curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json', 'X-HTTP-Method-Override: DELETE'));
123 $result = curl_exec($ch);
125 return $this->preParseOutput($result);
128 private function preParseOutput($result) {
129 $this->Application->getModule('logging')->log(__FUNCTION__, $result, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
130 $resource = json_decode($result);
132 if(is_object($resource) && property_exists($resource, 'error')) {
133 if(!in_array($resource->error, $this->allowedErrors)) {
134 $error = $resource->error;
137 $error = AuthorizationError::ERROR_AUTHORIZATION_TO_WEBGUI_PROBLEM;
140 $this->Application->getModule('logging')->log(__FUNCTION__, $resource, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
141 if(!is_null($error)) {
142 // Note! Redirection to error page takes place here.
143 $this->Response->redirect($this->Service->constructUrl('BaculumError',array('error' => $error), false));