public static initializeInstance ( $type, $headers, $server, $params, $files, $userId, $envId = null ) : Scalr_UI_Request | ||
$type | ||
$headers | ||
$server | ||
$params | ||
$files | ||
$userId | ||
$envId | int optional Could be null, when we check headers (for UI) | |
return | Scalr_UI_Request |
\Scalr::logException($e); Scalr_UI_Response::getInstance()->data(array('errorDB' => true)); Scalr_UI_Response::getInstance()->debugException($e); Scalr_UI_Response::getInstance()->failure($e instanceof \Scalr\Exception\MysqlConnectionException ? 'Database connection issue' : 'Database error'); Scalr_UI_Response::getInstance()->sendResponse(); }; try { $startTime = microtime(true); require __DIR__ . '/src/prepend.inc.php'; $prependTime = microtime(true); // public controller for link like /public/*; don't check CSRF $publicController = !strncmp('public', $path, strlen('public')); $session = Scalr_Session::getInstance(); $time1 = microtime(true); try { $request = Scalr_UI_Request::initializeInstance(Scalr_UI_Request::REQUEST_TYPE_UI, getallheaders(), $_SERVER, $_REQUEST, $_FILES, $session->getUserId(), null); } catch (Exception $e) { if ($path == 'guest/logout') { // hack Scalr_Session::destroy(); Scalr_UI_Response::getInstance()->setRedirect('/'); Scalr_UI_Response::getInstance()->sendResponse(); exit; } $message = $e->getMessage(); if ($e->getCode() != 1) { $message = htmlspecialchars($message) . ' <a href="/guest/logout">Click here to login as another user</a>'; Scalr_UI_Response::getInstance()->debugException($e); Scalr_UI_Response::getInstance()->failure($message, true); throw new Exception(); } else {
if ($error && ($error['type'] == E_ERROR || $error['type'] == E_PARSE || $error['type'] == E_COMPILE_ERROR)) { if (!headers_sent()) { header("HTTP/1.0 500"); } } }); $path = trim(str_replace("?{$_SERVER['QUERY_STRING']}", "", $_SERVER['REQUEST_URI']), '/'); define('SCALR_NOT_CHECK_SESSION', 1); try { $t = microtime(true); require "src/prepend.inc.php"; $pTime = microtime(true) - $t; @header("X-Scalr-PTime: {$pTime}"); $session = Scalr_Session::getInstance(); try { $request = Scalr_UI_Request::initializeInstance(Scalr_UI_Request::REQUEST_TYPE_UI, $session->getUserId(), $session->getEnvironmentId()); } catch (Exception $e) { if ($path == 'guest/logout') { // hack Scalr_Session::destroy(); Scalr_UI_Response::getInstance()->setRedirect('/'); Scalr_UI_Response::getInstance()->sendResponse(); exit; } $message = $e->getMessage() . ' <a href="/guest/logout">Click here to login as another user</a>'; throw new Exception($message); } if ($session->isAuthenticated()) { $session->setEnvironmentId($request->getEnvironment()->id); } //@session_write_close();
$envId = (int) $_SERVER['HTTP_X_SCALR_ENV_ID']; $pathChunks = explode('/', $path); $version = array_shift($pathChunks); $path = '/' . $path; //if (! $envId) //throw new Exception('Environment not defined'); // TODO: how to check if needed ? $user = Scalr_Account_User::init(); $user->loadByApiAccessKey($keyId); if (!$user->getSetting(Scalr_Account_User::SETTING_API_ENABLED)) { throw new Exception("API disabled for this account"); } //Check IP whitelist $postData = isset($_POST['rawPostData']) ? $_POST['rawPostData'] : ''; $secretKey = $user->getSetting(Scalr_Account_User::SETTING_API_SECRET_KEY); $stringToSign = "{$path}:{$keyId}:{$envId}:{$postData}:{$secretKey}"; $validToken = Scalr_Util_CryptoTool::hash($stringToSign); if ($validToken != $token) { throw new Exception("Invalid authentification token"); } Scalr_UI_Request::initializeInstance(Scalr_UI_Request::REQUEST_TYPE_API, $user->id, $envId); // prepate input data $postDataConvert = array(); foreach (json_decode($postData, true) as $key => $value) { $postDataConvert[str_replace('.', '_', $key)] = $value; } Scalr_Api_Controller::handleRequest($pathChunks, $postDataConvert); } catch (Exception $e) { Scalr_UI_Response::getInstance()->failure($e->getMessage()); Scalr_UI_Response::getInstance()->sendResponse(); }
if ($error && ($error['type'] == E_ERROR || $error['type'] == E_PARSE || $error['type'] == E_COMPILE_ERROR)) { if (!headers_sent()) { header("HTTP/1.0 500"); } } }); $path = trim(str_replace("?{$_SERVER['QUERY_STRING']}", "", $_SERVER['REQUEST_URI']), '/'); try { $startTime = microtime(true); require __DIR__ . '/src/prepend.inc.php'; $prependTime = microtime(true); // public controller for link like /public/*; don't check CSRF $publicController = !strncmp('public', $path, strlen('public')); $session = Scalr_Session::getInstance(); try { $request = Scalr_UI_Request::initializeInstance(Scalr_UI_Request::REQUEST_TYPE_UI, apache_request_headers(), $_SERVER, $_REQUEST, $_FILES, $session->getUserId(), $session->getEnvironmentId()); } catch (Exception $e) { if ($path == 'guest/logout') { // hack Scalr_Session::destroy(); Scalr_UI_Response::getInstance()->setRedirect('/'); Scalr_UI_Response::getInstance()->sendResponse(); exit; } $message = $e->getMessage(); if ($e->getCode() != 1) { $message = htmlspecialchars($message) . ' <a href="/guest/logout">Click here to login as another user</a>'; Scalr_UI_Response::getInstance()->failure($message, true); throw new Exception(); } else { throw new Exception($message);
$version = array_shift($pathChunks); $path = '/' . $path; //if (! $envId) //throw new Exception('Environment not defined'); // TODO: how to check if needed ? $user = Scalr_Account_User::init(); $user->loadByApiAccessKey($keyId); if (!$user->getSetting(Scalr_Account_User::SETTING_API_ENABLED)) { throw new Exception("API disabled for this account"); } //Check IP whitelist $postData = isset($_POST['rawPostData']) ? $_POST['rawPostData'] : ''; $secretKey = $user->getSetting(Scalr_Account_User::SETTING_API_SECRET_KEY); $stringToSign = "{$path}:{$keyId}:{$envId}:{$postData}:{$secretKey}"; $validToken = Scalr_Util_CryptoTool::hash($stringToSign); if ($validToken != $token) { throw new Exception("Invalid authentification token"); } // prepate input data $postDataConvert = array(); foreach (json_decode($postData, true) as $key => $value) { $postDataConvert[str_replace('.', '_', $key)] = $value; } $request = Scalr_UI_Request::initializeInstance(Scalr_UI_Request::REQUEST_TYPE_API, apache_request_headers(), $_SERVER, $postDataConvert, $_FILES, $user->id, $envId); $request->requestApiVersion = intval(trim($version, 'v')); Scalr_Api_Controller::handleRequest($pathChunks); Scalr_UI_Response::getInstance()->sendResponse(); } catch (Exception $e) { Scalr_UI_Response::getInstance()->failure($e->getMessage()); Scalr_UI_Response::getInstance()->sendResponse(); }