Skip to content
This repository
tree: ad9c4e0c24
Fetching contributors…

Cannot retrieve contributors at this time

file 141 lines (124 sloc) 5.424 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
<?php

/*
* Mashape PHP library.
*
* Copyright (C) 2011 Mashape, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* The author of this software is Mashape, Inc.
* For any question or feedback please contact us at: support@mashape.com
*
*/

require_once(dirname(__FILE__) . "/../IMethodHandler.php");
require_once(dirname(__FILE__) . "/../../net/httpUtils.php");
require_once(dirname(__FILE__) . "/../../init/init.php");
require_once(dirname(__FILE__) . "/../../configuration/restConfigurationLoader.php");
require_once(dirname(__FILE__) . "/helpers/callHelper.php");
require_once(dirname(__FILE__) . "/helpers/routeHelper.php");
require_once(dirname(__FILE__) . "/../discover/helpers/updateHtaccess.php");

define("METHOD", "_method");
define("TOKEN", "_token");
define("ROUTE", "_route");
define("LANGUAGE", "_language");
define("VERSION", "_version");
define("QUERY_PARAM_TOKEN", "token");
define("QUERY_PARAM_METHOD", "method");
define("QUERY_PARAM_SERVERKEY", "serverkey");
define("QUERY_PARAM_LANGUAGE", "language");
define("QUERY_PARAM_VERSION", "version");
define("MASHAPE_TOKEN_VALIDATION_URL", "https://api.mashape.com/validateToken");

class Call implements IMethodHandler {

public function handle($instance, $serverKey, $parameters, $httpRequestMethod) {
// If the request comes from local, reload the configuration
$this->reloadConfiguration($instance, $serverKey);

$methodName = null;
$method = null;

$this->findMethod($parameters, $methodName, $method, $serverKey, $httpRequestMethod);

if (strtolower($method->getHttp()) != strtolower($httpRequestMethod)) {
throw new MashapeException(EXCEPTION_INVALID_HTTPMETHOD, EXCEPTION_INVALID_HTTPMETHOD_CODE);
}

unset($parameters[METHOD]); // Remove the method name from the params
$token = (isset($parameters[TOKEN])) ? $parameters[TOKEN] : null;
unset($parameters[TOKEN]); // remove the token parameter

$language = (isset($parameters[LANGUAGE])) ? $parameters[LANGUAGE] : null;
unset($parameters[LANGUAGE]); // remove the language parameter
$version = (isset($parameters[VERSION])) ? $parameters[VERSION] : null;
unset($parameters[VERSION]); // remove the version parameter

//Validate Request
if (self::validateRequest($serverKey, $token, $methodName, $language, $version)) {
return doCall($method, $parameters, $instance, $serverKey);
} else {
throw new MashapeException(EXCEPTION_AUTH_INVALID, EXCEPTION_AUTH_INVALID_CODE);
}
}

private function findMethod(&$parameters, &$methodName, &$method, $serverKey, $httpRequestMethod) {
$methodName = (isset($parameters[METHOD])) ? $parameters[METHOD] : null;
$method = null;
if (empty($methodName)) {
// Find route
$requestUri = (isset($_SERVER["REQUEST_URI"])) ? $_SERVER["REQUEST_URI"] : null;

$method = findRoute($requestUri, $parameters, $httpRequestMethod, $serverKey);
if (!empty($method)) {
$methodName = $method->getName();
}
} else {
$method = RESTConfigurationLoader::getMethod($methodName, $serverKey);
}
if (empty($method)) {
throw new MashapeException(sprintf(EXCEPTION_METHOD_NOTFOUND, $methodName), EXCEPTION_METHOD_NOTFOUND_CODE);
}
}

private function reloadConfiguration($instance, $serverKey) {
if (HttpUtils::isLocal()) {
// Update the .htaccess file with the new route settings
updateHtaccess($instance);

// Update the configuration
RESTConfigurationLoader::reloadConfiguration($serverKey);
}
}

private function validateRequest($serverKey, $token, $method, $language, $version) {
// If the request comes from the local computer, then don't require authorization,
// otherwise check the headers
if (HttpUtils::isLocal()) {
return true;
} else {
if (empty($serverKey)) {
throw new MashapeException(EXCEPTION_EMPTY_SERVERKEY, EXCEPTION_XML_CODE);
}
$url = MASHAPE_TOKEN_VALIDATION_URL . "?" . QUERY_PARAM_TOKEN . "=" . $token . "&" . QUERY_PARAM_SERVERKEY . "=" . $serverKey . "&" . QUERY_PARAM_METHOD . "=" . $method . "&" . QUERY_PARAM_LANGUAGE . "=" . $language . "&" . QUERY_PARAM_VERSION . "=" . $version;
$response = HttpUtils::makeHttpRequest($url);
if (empty($response)) {
throw new MashapeException(EXCEPTION_EMPTY_REQUEST, EXCEPTION_SYSTEM_ERROR_CODE);
}
$validationResponse = json_decode($response);
if (empty($validationResponse)) {
throw new MashapeException(EXCEPTION_JSONDECODE_REQUEST, EXCEPTION_SYSTEM_ERROR_CODE);
}
if (!empty($validationResponse->errors)) {
$error = $validationResponse->errors[0];
throw new MashapeException($error->message, $error->code);
}
$authorization = $validationResponse->authorized;
$GLOBALS[UID] = $validationResponse->uid;
if ($authorization == true) {
return true;
} else {
return false;
}
}
}

}
?>
Something went wrong with that request. Please try again.