]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Class/API.php
baculum: Fix update pool action when no volumes in pool
[bacula/bacula] / gui / baculum / protected / Class / API.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2015 Marcin Haba
7  *
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.
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  * This notice must be preserved when any source code is
18  * conveyed and/or propagated.
19  *
20  * Bacula(R) is a registered trademark of Kern Sibbald.
21  */
22
23 Prado::using('Application.Class.Errors');
24
25 class API extends TModule {
26
27         const API_VERSION = '0.1';
28
29         protected $appCfg;
30
31         private $allowedErrors = array(
32                 GenericError::ERROR_NO_ERRORS,
33                 BconsoleError::ERROR_INVALID_COMMAND,
34                 PoolError::ERROR_NO_VOLUMES_IN_POOL_TO_UPDATE
35         );
36
37         private function getConnection() {
38                 $ch = curl_init();
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']);
44                 return $ch;
45         }
46
47         private function getAPIHeader() {
48                 return 'X-Baculum-API: ' . self::API_VERSION;
49         }
50
51         public function init($config) {
52                 $this->appCfg = $this->Application->getModule('configuration')->getApplicationConfig();
53         }
54
55         private function getURL() {
56                 $protocol = !empty($_SERVER['HTTPS']) ? 'https' : 'http';
57                 $host = $_SERVER['SERVER_NAME'];
58                 $port = $_SERVER['SERVER_PORT'];
59                 $urlPrefix = $this->Application->getModule('friendly-url')->getUrlPrefix();
60                 $url = sprintf('%s://%s:%d%s/', $protocol, $host, $port, $urlPrefix);
61                 return $url;
62         }
63
64         private function setParamsToUrl(&$url) {
65                 $url .= (preg_match('/\?/', $url) === 1 ? '&' : '?' ) . 'director=' . ((array_key_exists('director', $_SESSION)) ? $_SESSION['director'] : '');
66                 /**
67                  * If user is not equal admin user then it is added to URL,
68                  * then will be used custom console for this user.
69                  */
70                 if($this->User->getIsAdmin() === false) {
71                         $url .= '&user=' . $this->User->getName();
72                 }
73                 $this->Application->getModule('logging')->log(__FUNCTION__, PHP_EOL . PHP_EOL . 'EXECUTE URL ==> ' . $url . ' <==' . PHP_EOL . PHP_EOL, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
74         }
75
76         /**
77          * API REQUESTS METHODS (get, set, create, delete)
78          */
79
80         public function get(array $params) {
81                 $url = $this->getURL() . implode('/', $params);
82                 $this->setParamsToUrl($url);
83                 $ch = $this->getConnection();
84                 curl_setopt($ch, CURLOPT_URL, $url);
85                 curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json'));
86                 $result = curl_exec($ch);
87                 curl_close($ch);
88                 return $this->preParseOutput($result);
89         }
90
91         public function set(array $params, array $options) {
92                 $url = $this->getURL() . implode('/', $params);
93                 $this->setParamsToUrl($url);
94                 $data = http_build_query(array('update' => $options));
95                 $ch = $this->getConnection();
96                 curl_setopt($ch, CURLOPT_URL, $url);
97                 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
98                 curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json', 'X-HTTP-Method-Override: PUT', 'Content-Length: ' . strlen($data), 'Expect:'));
99                 curl_setopt($ch, CURLOPT_POST, true);
100                 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
101                 $result = curl_exec($ch);
102                 curl_close($ch);
103                 return $this->preParseOutput($result);
104         }
105
106         public function create(array $params, array $options) {
107                 $url = $this->getURL() . implode('/', $params);
108                 $this->setParamsToUrl($url);
109                 $data = http_build_query(array('create' => $options));
110                 $ch = $this->getConnection();
111                 curl_setopt($ch, CURLOPT_URL, $url);
112                 curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json', 'Expect:'));
113                 curl_setopt($ch, CURLOPT_POST, true);
114                 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
115                 $result = curl_exec($ch);
116                 curl_close($ch);
117                 return $this->preParseOutput($result);
118         }
119
120         public function remove(array $params) {
121                 $url = $this->getURL() . implode('/', $params);
122                 $this->setParamsToUrl($url);
123                 $ch = $this->getConnection();
124                 curl_setopt($ch, CURLOPT_URL, $url);
125                 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
126                 curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json', 'X-HTTP-Method-Override: DELETE'));
127                 $result = curl_exec($ch);
128                 curl_close($ch);
129                 return $this->preParseOutput($result);
130         }
131
132         private function preParseOutput($result) {
133                 $this->Application->getModule('logging')->log(__FUNCTION__, $result, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
134                 $resource = json_decode($result);
135                 $error = null;
136                 if(is_object($resource) && property_exists($resource, 'error')) {
137                         if(!in_array($resource->error, $this->allowedErrors)) {
138                                 $error = $resource->error;
139                         }
140                 } else {
141                         $error = AuthorizationError::ERROR_AUTHORIZATION_TO_WEBGUI_PROBLEM;
142                 }
143
144                 $this->Application->getModule('logging')->log(__FUNCTION__, $resource, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
145                 if(!is_null($error)) {
146                         // Note! Redirection to error page takes place here.
147                         $this->Response->redirect($this->Service->constructUrl('BaculumError',array('error' => $error), false));
148                 }
149
150                 return $resource;
151         }
152 }
153 ?>