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);
41 private function getAPIHeader() {
42 return 'X-Baculum-API: ' . self::API_VERSION;
45 private function getURL() {
46 $this->appCfg = $this->Application->getModule('configuration')->getApplicationConfig();
47 $protocol = !empty($_SERVER['HTTPS']) ? 'https' : 'http';
48 $host = $_SERVER['SERVER_NAME'];
49 $port = $_SERVER['SERVER_PORT'];
50 $url = sprintf('%s://%s:%s@%s:%d/', $protocol, $this->appCfg['baculum']['login'], $this->appCfg['baculum']['password'], $host, $port);
54 private function setParamsToUrl(&$url) {
55 $url .= (preg_match('/\?/', $url) === 1 ? '&' : '?' ) . 'director=' . ((array_key_exists('director', $_SESSION)) ? $_SESSION['director'] : '');
57 * If user is not equal admin user then it is added to URL,
58 * then will be used custom console for this user.
60 if($this->User->getIsAdmin() === false) {
61 $url .= '&user=' . $this->User->getName();
63 $this->Application->getModule('logging')->log(__FUNCTION__, PHP_EOL . PHP_EOL . 'EXECUTE URL ==> ' . $url . ' <==' . PHP_EOL . PHP_EOL, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
67 * API REQUESTS METHODS (get, set, create, delete)
70 public function get(array $params) {
71 $url = $this->getURL() . implode('/', $params);
72 $this->setParamsToUrl($url);
73 $ch = $this->getConnection();
74 curl_setopt($ch, CURLOPT_URL, $url);
75 curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json'));
76 $result = curl_exec($ch);
78 return $this->preParseOutput($result);
81 public function set(array $params, array $options) {
82 $url = $this->getURL() . implode('/', $params);
83 $this->setParamsToUrl($url);
84 $data = http_build_query(array('update' => $options));
85 $ch = $this->getConnection();
86 curl_setopt($ch, CURLOPT_URL, $url);
87 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
88 curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json', 'X-HTTP-Method-Override: PUT', 'Content-Length: ' . strlen($data)));
89 curl_setopt($ch, CURLOPT_POST, true);
90 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
91 $result = curl_exec($ch);
93 return $this->preParseOutput($result);
96 public function create(array $params, array $options) {
97 $url = $this->getURL() . implode('/', $params);
98 $this->setParamsToUrl($url);
99 $data = http_build_query(array('create' => $options));
100 $ch = $this->getConnection();
101 curl_setopt($ch, CURLOPT_URL, $url);
102 curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json'));
103 curl_setopt($ch, CURLOPT_POST, true);
104 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
105 $result = curl_exec($ch);
107 return $this->preParseOutput($result);
110 public function remove(array $params) {
111 $url = $this->getURL() . implode('/', $params);
112 $this->setParamsToUrl($url);
113 $ch = $this->getConnection();
114 curl_setopt($ch, CURLOPT_URL, $url);
115 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
116 curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json', 'X-HTTP-Method-Override: DELETE'));
117 $result = curl_exec($ch);
119 return $this->preParseOutput($result);
122 private function preParseOutput($result) {
123 $this->Application->getModule('logging')->log(__FUNCTION__, $result, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
124 $resource = json_decode($result);
126 if(is_object($resource) && property_exists($resource, 'error')) {
127 if(!in_array($resource->error, $this->allowedErrors)) {
128 $error = $resource->error;
131 $error = AuthorizationError::ERROR_AUTHORIZATION_TO_WEBGUI_PROBLEM;
134 $this->Application->getModule('logging')->log(__FUNCTION__, $resource, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
135 if(!is_null($error)) {
136 // Note! Redirection to error page takes place here.
137 $this->Response->redirect($this->Service->constructUrl('BaculumError',array('error' => $error), false));