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('System.Exceptions.TException');
21 Prado::using('Application.Class.Errors');
23 abstract class BaculumAPI extends TPage
35 const GET_METHOD = 'GET';
36 const POST_METHOD = 'POST';
37 const PUT_METHOD = 'PUT';
38 const DELETE_METHOD = 'DELETE';
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;
50 switch($_SERVER['REQUEST_METHOD']) {
51 case self::PUT_METHOD: {
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;
61 case self::GET_METHOD: {
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;
71 case self::POST_METHOD: {
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;
81 case self::DELETE_METHOD: {
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;
94 private function getOutput() {
95 $output = array('output' => $this->output, 'error' => $this->error);
96 return json_encode($output);
99 public function onLoad($params) {
100 parent::onLoad($params);
101 echo $this->getOutput();
104 abstract protected function get();
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);
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];
122 if(is_array($responseData) && array_key_exists('update', $responseData)) {
123 $params = (object)$responseData['update'];
124 $this->set($id, $params);
126 $this->set($id, array()); //@TOVERIFY
127 //$this->output = GenericError::MSG_ERROR_INVALID_COMMAND;
128 //$this->error = GenericError::ERROR_INVALID_COMMAND;
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);
140 private function delete() {
141 if(isset($this->Request['id'])) {
142 $id = intval($this->Request['id']);
147 public function getModule($name) {
148 return $this->Application->getModule($name);