appCfg = $this->Application->getModule('configuration')->getApplicationConfig(); $protocol = !empty($_SERVER['HTTPS']) ? 'https' : 'http'; $host = $_SERVER['SERVER_NAME']; $port = $_SERVER['SERVER_PORT']; $url = sprintf('%s://%s:%s@%s:%d/', $protocol, $this->appCfg['baculum']['login'], $this->appCfg['baculum']['password'], $host, $port); return $url; } private function setParamsToUrl(&$url) { $url .= (preg_match('/\?/', $url) === 1 ? '&' : '?' ) . 'director=' . ((array_key_exists('director', $_SESSION)) ? $_SESSION['director'] : ''); /** * If user is not equal admin user then it is added to URL, * then will be used custom console for this user. */ if($this->User->getIsAdmin() === false) { $url .= '&user=' . $this->User->getName(); } $this->Application->getModule('logging')->log(__FUNCTION__, PHP_EOL . PHP_EOL . 'EXECUTE URL ==> ' . $url . ' <==' . PHP_EOL . PHP_EOL, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__); } /** * API REQUESTS METHODS (get, set, create, delete) */ public function get(array $params) { $url = $this->getURL() . implode('/', $params); $this->setParamsToUrl($url); $ch = $this->getConnection(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json')); $result = curl_exec($ch); curl_close($ch); return $this->preParseOutput($result); } public function set(array $params, array $options) { $url = $this->getURL() . implode('/', $params); $this->setParamsToUrl($url); $data = http_build_query(array('update' => $options)); $ch = $this->getConnection(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json', 'X-HTTP-Method-Override: PUT', 'Content-Length: ' . strlen($data))); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $result = curl_exec($ch); curl_close($ch); return $this->preParseOutput($result); } public function create(array $params, array $options) { $url = $this->getURL() . implode('/', $params); $this->setParamsToUrl($url); $data = http_build_query(array('create' => $options)); $ch = $this->getConnection(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json')); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $result = curl_exec($ch); curl_close($ch); return $this->preParseOutput($result); } public function remove(array $params) { $url = $this->getURL() . implode('/', $params); $this->setParamsToUrl($url); $ch = $this->getConnection(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json', 'X-HTTP-Method-Override: DELETE')); $result = curl_exec($ch); curl_close($ch); return $this->preParseOutput($result); } private function preParseOutput($result) { $this->Application->getModule('logging')->log(__FUNCTION__, $result, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__); $resource = json_decode($result); $error = null; if(is_object($resource) && property_exists($resource, 'error')) { if(!in_array($resource->error, $this->allowedErrors)) { $error = $resource->error; } } else { $error = AuthorizationError::ERROR_AUTHORIZATION_TO_WEBGUI_PROBLEM; } $this->Application->getModule('logging')->log(__FUNCTION__, $resource, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__); if(!is_null($error)) { // Note! Redirection to error page takes place here. $this->Response->redirect($this->Service->constructUrl('BaculumError',array('error' => $error), false)); } return $resource; } } ?>