]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Class/BaculumAPI.php
baculum: Support for requests with over 1000 input vars (default value php.ini)
[bacula/bacula] / gui / baculum / protected / Class / BaculumAPI.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('System.Exceptions.TException');
21 Prado::using('Application.Class.Errors');
22
23 abstract class BaculumAPI extends TPage
24 {
25         protected $output;
26         protected $error;
27
28         protected $director;
29
30         protected $user;
31
32         /**
33          * Actions methods.
34          */
35         const GET_METHOD = 'GET';
36         const POST_METHOD = 'POST';
37         const PUT_METHOD = 'PUT';
38         const DELETE_METHOD = 'DELETE';
39
40         public function onInit($params) {
41                 parent::onInit($params);
42                 $this->director = isset($this->Request['director']) ? $this->Request['director'] : null;
43                 $this->user = isset($this->Request['user']) ? $this->Request['user'] : null;
44                 if(is_null($this->user) && $this->Application->getModule('configuration')->isApplicationConfig() === true) {
45                         $appConfig = ConfigurationManager::getApplicationConfig();
46                         // @TOFIX: Baculum API layer should not use $_SERVER variables.
47                         $this->user = isset($_SERVER['PHP_AUTH_USER']) && $_SERVER['PHP_AUTH_USER'] != $appConfig['baculum']['login'] ? $_SERVER['PHP_AUTH_USER'] : null;
48                 }
49
50                 switch($_SERVER['REQUEST_METHOD']) {
51                         case self::PUT_METHOD: {
52                                 try {
53                                         $this->put();
54                                 } catch(TDbException $e) {
55                                         $this->Application->getModule('logging')->log(__FUNCTION__, $e, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
56                                         $this->output = DatabaseError::MSG_ERROR_DB_CONNECTION_PROBLEM;
57                                         $this->error = DatabaseError::ERROR_DB_CONNECTION_PROBLEM;
58                                 }
59                                 break;
60                         }
61                         case self::GET_METHOD: {
62                                 try {
63                                         $this->get();
64                                 } catch(TDbException $e) {
65                                         $this->Application->getModule('logging')->log(__FUNCTION__, $e, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
66                                         $this->output = DatabaseError::MSG_ERROR_DB_CONNECTION_PROBLEM;
67                                         $this->error = DatabaseError::ERROR_DB_CONNECTION_PROBLEM;
68                                 }
69                                 break;
70                         }
71                         case self::POST_METHOD: {
72                                 try {
73                                         $this->post();
74                                 } catch(TDbException $e) {
75                                         $this->Application->getModule('logging')->log(__FUNCTION__, $e, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
76                                         $this->output = DatabaseError::MSG_ERROR_DB_CONNECTION_PROBLEM;
77                                         $this->error = DatabaseError::ERROR_DB_CONNECTION_PROBLEM;
78                                 }
79                                 break;
80                         }
81                         case self::DELETE_METHOD: {
82                                 try {
83                                         $this->delete();
84                                 } catch(TDbException $e) {
85                                         $this->Application->getModule('logging')->log(__FUNCTION__, $e, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__);
86                                         $this->output = DatabaseError::MSG_ERROR_DB_CONNECTION_PROBLEM;
87                                         $this->error = DatabaseError::ERROR_DB_CONNECTION_PROBLEM;
88                                 }
89                                 break;
90                         }
91                 }
92         }
93
94         private function getOutput() {
95                 $output = array('output' => $this->output, 'error' => $this->error);
96                 return json_encode($output);
97         }
98
99         public function onLoad($params) {
100                 parent::onLoad($params);
101                 echo $this->getOutput();
102         }
103
104         abstract protected function get();
105
106         private function put() {
107                 $id = isset($this->Request['id']) ? $this->Request['id'] : null;
108                 if(is_array($this->Request['update']) && count($this->Request['update']) > 0) {
109                         $params = (object)$this->Request['update'];
110                         $this->set($id, $params);
111                 } else {
112                         $inputstr = file_get_contents("php://input");
113                         $chunks = explode('&', $inputstr);
114                         $responseData = array();
115                         for($i = 0; $i<count($chunks); $i++) {
116                                 parse_str($chunks[$i], $responseEl);
117                                 if(is_array($responseEl) && array_key_exists('update', $responseEl) && is_array($responseEl['update'])) {
118                                         $key = key($responseEl['update']);
119                                         $responseData['update'][$key] = $responseEl['update'][$key];
120                                 }
121                         }
122                         if(is_array($responseData) && array_key_exists('update', $responseData)) {
123                                 $params = (object)$responseData['update'];
124                                 $this->set($id, $params);
125                         } else {
126                                 $this->set($id, array()); //@TOVERIFY
127                                 //$this->output = GenericError::MSG_ERROR_INVALID_COMMAND;
128                                 //$this->error = GenericError::ERROR_INVALID_COMMAND;
129                         }
130                 }
131         }
132         
133         private function post() {
134                 if(is_array($this->Request['create']) && count($this->Request['create']) > 0) {
135                         $params = (object)$this->Request['create'];
136                         $this->create($params);
137                 }
138         }
139
140         private function delete() {
141                 if(isset($this->Request['id'])) {
142                         $id = intval($this->Request['id']);
143                         $this->remove($id);
144                 }
145         }
146
147         public function getModule($name) {
148                 return $this->Application->getModule($name);
149         }
150 }
151 ?>