]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Class/API.php
Support for customized and restricted consoles
[bacula/bacula] / gui / baculum / protected / Class / API.php
1 <?php
2 /**
3  * Bacula® - The Network Backup Solution
4  * Baculum - Bacula web interface
5  *
6  * Copyright (C) 2013-2014 Marcin Haba
7  *
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.
11  *
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.
16  *
17  * Bacula® is a registered trademark of Kern Sibbald.
18  */
19
20 Prado::using('Application.Class.Errors');
21
22 class API extends TModule {
23
24         const API_VERSION = '0.1';
25
26         protected $appCfg;
27
28         private $allowedErrors = array(
29                 GenericError::ERROR_NO_ERRORS,
30                 BconsoleError::ERROR_INVALID_COMMAND
31         );
32
33         private function getConnection() {
34                 $ch = curl_init();
35                 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
36                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
37                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
38                 return $ch;
39         }
40
41         private function getAPIHeader() {
42                 return 'X-Baculum-API: ' . self::API_VERSION;
43         }
44
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);
51                 return $url;
52         }
53
54         private function setParamsToUrl(&$url) {
55                 $url .= (preg_match('/\?/', $url) === 1 ? '&' : '?' ) . 'director=' . ((array_key_exists('director', $_SESSION)) ? $_SESSION['director'] : '');
56                 /**
57                  * If user is not equal admin user then it is added to URL,
58                  * then will be used custom console for this user.
59                  */
60                 if($this->User->getIsAdmin() === false) {
61                         $url .= '&user=' . $this->User->getName();
62                 }
63                 $this->Application->getModule('logging')->log(__FUNCTION__, PHP_EOL . PHP_EOL . 'EXECUTE URL ==> ' . $url . ' <==' . PHP_EOL . PHP_EOL, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
64         }
65
66         /**
67          * API REQUESTS METHODS (get, set, create, delete)
68          */
69
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);
77                 curl_close($ch);
78                 return $this->preParseOutput($result);
79         }
80
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);
92                 curl_close($ch);
93                 return $this->preParseOutput($result);
94         }
95
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);
106                 curl_close($ch);
107                 return $this->preParseOutput($result);
108         }
109
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);
118                 curl_close($ch);
119                 return $this->preParseOutput($result);
120         }
121
122         private function preParseOutput($result) {
123                 $this->Application->getModule('logging')->log(__FUNCTION__, $result, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
124                 $resource = json_decode($result);
125                 $error = null;
126                 if(is_object($resource) && property_exists($resource, 'error')) {
127                         if(!in_array($resource->error, $this->allowedErrors)) {
128                                 $error = $resource->error;
129                         }
130                 } else {
131                         $error = AuthorizationError::ERROR_AUTHORIZATION_TO_WEBGUI_PROBLEM;
132                 }
133
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));
138                 }
139
140                 return $resource;
141         }
142 }
143 ?>