]> git.sur5r.net Git - bacula/bacula/blobdiff - gui/baculum/protected/Class/BaculumAPI.php
baculum: Prevent opening new sessions for each request
[bacula/bacula] / gui / baculum / protected / Class / BaculumAPI.php
index 0f924957bb22b373ce6c29636c0c8d3092d2b440..0ed249aede9d6c4d6abb0cafa2c17ba70bebd1e7 100644 (file)
@@ -1,20 +1,23 @@
 <?php
-/**
- * Bacula® - The Network Backup Solution
- * Baculum - Bacula web interface
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum   - Bacula web interface
  *
- * Copyright (C) 2013-2014 Marcin Haba
+ * Copyright (C) 2013-2015 Marcin Haba
  *
  * The main author of Baculum is Marcin Haba.
- * The main author of Bacula is Kern Sibbald, with contributions from many
- * others, a complete list can be found in the file AUTHORS.
+ * The original author of Bacula is Kern Sibbald, with contributions
+ * from many others, a complete list can be found in the file AUTHORS.
  *
  * You may use this file and others of this release according to the
  * license defined in the LICENSE file, which includes the Affero General
  * Public License, v3.0 ("AGPLv3") and some additional permissions and
  * terms pursuant to its AGPLv3 Section 7.
  *
- * Bacula® is a registered trademark of Kern Sibbald.
+ * This notice must be preserved when any source code is
+ * conveyed and/or propagated.
+ *
+ * Bacula(R) is a registered trademark of Kern Sibbald.
  */
  
 Prado::using('System.Exceptions.TException');
@@ -27,6 +30,8 @@ abstract class BaculumAPI extends TPage
 
        protected $director;
 
+       protected $user;
+
        /**
         * Actions methods.
         */
@@ -37,7 +42,31 @@ abstract class BaculumAPI extends TPage
 
        public function onInit($params) {
                parent::onInit($params);
+               /*
+                * Workaround to bug in PHP 5.6 by FastCGI that caused general protection error.
+                * TODO: Check on newer PHP if it is already fixed.
+                */
+               $db = new ActiveRecord();
+               $db->getDbConnection();
                $this->director = isset($this->Request['director']) ? $this->Request['director'] : null;
+
+               $user = isset($_SERVER['HTTP_X_BACULUM_USER']) ? $_SERVER['HTTP_X_BACULUM_USER']: null;
+               $pwd = isset($_SERVER['HTTP_X_BACULUM_PWD']) ? $_SERVER['HTTP_X_BACULUM_PWD']: null;
+               if(!is_null($user) && !is_null($pwd)) {
+                       $logged = $this->Application->getModule('users')->loginUser($user, $pwd);
+                       if ($logged === true) {
+                               $this->user = ($this->User->getIsAdmin() === false) ? $user : null;
+                       } else {
+                               $this->output = AuthorizationError::MSG_ERROR_AUTHORIZATION_TO_WEBGUI_PROBLEM;
+                               $this->error = AuthorizationError::ERROR_AUTHORIZATION_TO_WEBGUI_PROBLEM;
+                               return;
+                       }
+               } else {
+                       $this->output = AuthorizationError::MSG_ERROR_AUTHORIZATION_TO_WEBGUI_PROBLEM;
+                       $this->error = AuthorizationError::ERROR_AUTHORIZATION_TO_WEBGUI_PROBLEM;
+                       return;
+               }
+
                switch($_SERVER['REQUEST_METHOD']) {
                        case self::PUT_METHOD: {
                                try {
@@ -100,7 +129,16 @@ abstract class BaculumAPI extends TPage
                        $params = (object)$this->Request['update'];
                        $this->set($id, $params);
                } else {
-                       parse_str(file_get_contents("php://input"),$responseData);
+                       $inputstr = file_get_contents("php://input");
+                       $chunks = explode('&', $inputstr);
+                       $responseData = array();
+                       for($i = 0; $i<count($chunks); $i++) {
+                               parse_str($chunks[$i], $responseEl);
+                               if(is_array($responseEl) && array_key_exists('update', $responseEl) && is_array($responseEl['update'])) {
+                                       $key = key($responseEl['update']);
+                                       $responseData['update'][$key] = $responseEl['update'][$key];
+                               }
+                       }
                        if(is_array($responseData) && array_key_exists('update', $responseData)) {
                                $params = (object)$responseData['update'];
                                $this->set($id, $params);
@@ -130,4 +168,4 @@ abstract class BaculumAPI extends TPage
                return $this->Application->getModule($name);
        }
 }
-?>
\ No newline at end of file
+?>