]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Class/API.php
74d9e019a69e60c686f6a9c333c0cd500df2820b
[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                 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
39                 curl_setopt($ch, CURLOPT_USERPWD, $this->appCfg['baculum']['login'] . ':' . $this->appCfg['baculum']['password']);
40                 return $ch;
41         }
42
43         private function getAPIHeader() {
44                 return 'X-Baculum-API: ' . self::API_VERSION;
45         }
46
47         public function init($config) {
48                 $this->appCfg = $this->Application->getModule('configuration')->getApplicationConfig();
49         }
50
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);
57                 return $url;
58         }
59
60         private function setParamsToUrl(&$url) {
61                 $url .= (preg_match('/\?/', $url) === 1 ? '&' : '?' ) . 'director=' . ((array_key_exists('director', $_SESSION)) ? $_SESSION['director'] : '');
62                 /**
63                  * If user is not equal admin user then it is added to URL,
64                  * then will be used custom console for this user.
65                  */
66                 if($this->User->getIsAdmin() === false) {
67                         $url .= '&user=' . $this->User->getName();
68                 }
69                 $this->Application->getModule('logging')->log(__FUNCTION__, PHP_EOL . PHP_EOL . 'EXECUTE URL ==> ' . $url . ' <==' . PHP_EOL . PHP_EOL, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
70         }
71
72         /**
73          * API REQUESTS METHODS (get, set, create, delete)
74          */
75
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);
83                 curl_close($ch);
84                 return $this->preParseOutput($result);
85         }
86
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);
98                 curl_close($ch);
99                 return $this->preParseOutput($result);
100         }
101
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);
112                 curl_close($ch);
113                 return $this->preParseOutput($result);
114         }
115
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);
124                 curl_close($ch);
125                 return $this->preParseOutput($result);
126         }
127
128         private function preParseOutput($result) {
129                 $this->Application->getModule('logging')->log(__FUNCTION__, $result, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
130                 $resource = json_decode($result);
131                 $error = null;
132                 if(is_object($resource) && property_exists($resource, 'error')) {
133                         if(!in_array($resource->error, $this->allowedErrors)) {
134                                 $error = $resource->error;
135                         }
136                 } else {
137                         $error = AuthorizationError::ERROR_AUTHORIZATION_TO_WEBGUI_PROBLEM;
138                 }
139
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));
144                 }
145
146                 return $resource;
147         }
148 }
149 ?>