Permalink
Browse files

Fix non-root server

  • Loading branch information...
1 parent 2fda6bc commit f6c8ec6e90bbb7da88ac7329ffd95ef44e293248 @cdujeu cdujeu committed May 25, 2016
@@ -29,17 +29,18 @@ class Base
{
/**
+ * @param string $base
* @param string $route
*/
- public static function handleRoute($route){
+ public static function handleRoute($base, $route){
if($route === "/api") {
- $server = new Rest\RestServer("/api");
+ $server = new Rest\RestServer($base.$route);
}else if($route === "/user"){
$_GET["get_action"] = "user_access_point";
- $server = new Server();
+ $server = new Server($base);
}else{
- $server = new Server();
+ $server = new Server($base);
}
$server->registerCatchAll();
@@ -35,7 +35,7 @@
class DAVServer
{
- public static function handleRoute($baseURI){
+ public static function handleRoute($baseURI, $davRoute){
ConfService::init();
ConfService::start();
@@ -53,6 +53,7 @@ public static function handleRoute($baseURI){
});
include AJXP_BIN_FOLDER.'/sabredav/lib/Sabre/autoload.php';
+ $baseURI = $baseURI . $davRoute;
$requestUri = $_SERVER["REQUEST_URI"];
if (substr($requestUri, 0, strlen($baseURI)) != $baseURI)
{
@@ -31,23 +31,23 @@ class ApiRouter
/**
* @var array
*/
- private $config;
+ private $base;
/**
* @var array
* "cacheOptions" => ["cacheFile" => "path", "cacheDisabled" => true],
*/
private $cacheOptions;
- private $v2Base = "/api/v2";
- private $v1Base = "/api";
+ private $v2Base = "/v2";
+ private $v1Base = "";
/**
- * SimpleRestResourceRouter constructor.
- * @param array $config
+ * ApiRouter constructor.
+ * @param string $base
* @param array $cacheOptions
*/
- public function __construct($config, $cacheOptions = []){
- $this->config = $config;
+ public function __construct($base, $cacheOptions = []){
+ $this->base = $base;
$this->cacheOptions = array_merge([
"cacheDisabled" => AJXP_SKIP_CACHE,
"cacheFile" => AJXP_DATA_PATH."/cache/plugins_api2routes.php"
@@ -56,21 +56,20 @@ public function __construct($config, $cacheOptions = []){
/**
- * @param array $configObject
* @param \FastRoute\RouteCollector $r
*/
- public function configureRoutes($base, $configObject, \FastRoute\RouteCollector &$r){
+ public function configureRoutes(\FastRoute\RouteCollector &$r){
$configObject = json_decode(file_get_contents(AJXP_INSTALL_PATH . "/" . AJXP_DOCS_FOLDER . "/api2.json"), true);
foreach ($configObject["paths"] as $path => $methods){
foreach($methods as $method => $apiData){
$path = str_replace("{path}", "{path:.+}", $path);
- $r->addRoute(strtoupper($method), $base . $path , $apiData);
+ $r->addRoute(strtoupper($method), $this->base . $this->v2Base . $path , $apiData);
}
}
// Legacy V1 API
- $r->addRoute("GET", $this->v1Base."/{repository_id}/{action}[{optional:.+}]", ["api-v1" => true]);
- $r->addRoute("POST", $this->v1Base."/{repository_id}/{action}[{optional:.+}]", ["api-v1" => true]);
+ $r->addRoute("GET", $this->base . $this->v1Base."/{repository_id}/{action}[{optional:.+}]", ["api-v1" => true]);
+ $r->addRoute("POST", $this->base . $this->v1Base."/{repository_id}/{action}[{optional:.+}]", ["api-v1" => true]);
}
@@ -88,7 +87,7 @@ public function route(ServerRequestInterface &$request, ResponseInterface &$resp
$dispatcher = \FastRoute\cachedDispatcher(function(\FastRoute\RouteCollector $r) {
- $this->configureRoutes($this->v2Base, $this->config, $r);
+ $this->configureRoutes($r);
}, $this->cacheOptions);
@@ -111,13 +110,15 @@ public function route(ServerRequestInterface &$request, ResponseInterface &$resp
$request = $request
->withAttribute("action", $vars["action"])
->withAttribute("repository_id", $vars["repository_id"])
+ ->withAttribute("rest_base", $this->base.$this->v1Base)
->withAttribute("rest_path", $vars["optional"])
->withAttribute("api", "v1");
}else{
$repoId = $this->findRepositoryInParameters($request, $vars);
$request = $request
->withAttribute("action", $apiData["x-pydio-action"])
->withAttribute("repository_id", $repoId)
+ ->withAttribute("rest_base", $this->base.$this->v2Base)
->withAttribute("api", "v2")
->withParsedBody(array_merge($request->getParsedBody(), $vars));
}
@@ -29,10 +29,16 @@
class RestApiMiddleware extends \Pydio\Core\Http\Middleware\SapiMiddleware
{
+ protected $base;
+
+ public function __construct($base)
+ {
+ $this->base = $base;
+ }
protected function parseRequestRouteAndParams(ServerRequestInterface &$request, ResponseInterface &$response){
- $router = new ApiRouter([]);
+ $router = new ApiRouter($this->base);
if(!$router->route($request, $response)){
throw new PydioException("Could not find any endpoint for this URI");
}
@@ -28,17 +28,18 @@
class RestServer extends \Pydio\Core\Http\Server
{
+
public function __construct($base)
{
- parent::__construct();
+ parent::__construct($base);
ConfService::currentContextIsRestAPI($base);
}
protected function stackMiddleWares()
{
$this->middleWares->push(array("Pydio\\Core\\Controller\\Controller", "registryActionMiddleware"));
$this->middleWares->push(array("Pydio\\Core\\Http\\Rest\\RestAuthMiddleware", "handleRequest"));
- $this->topMiddleware = new RestApiMiddleware();
+ $this->topMiddleware = new RestApiMiddleware($this->base);
$this->middleWares->push(array($this->topMiddleware, "handleRequest"));
}
}
@@ -54,15 +54,22 @@ class Server
protected $topMiddleware;
/**
+ * @var string
+ */
+ protected $base;
+
+ /**
* @var \SplStack
*/
protected static $middleWareInstance;
- public function __construct(){
+ public function __construct($base){
$this->middleWares = new \SplStack();
$this->middleWares->setIteratorMode(\SplDoublyLinkedList::IT_MODE_LIFO | \SplDoublyLinkedList::IT_MODE_KEEP);
+ $this->base = $base;
+
$this->stackMiddleWares();
self::$middleWareInstance = &$this->middleWares;
@@ -23,6 +23,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Pydio\Core\Exception\PydioException;
+use Pydio\Core\Utils\Utils;
use Zend\Diactoros\ServerRequestFactory;
defined('AJXP_EXEC') or die('Access not allowed');
@@ -72,7 +73,7 @@ public function configureRoutes($base, \FastRoute\RouteCollector &$r){
$methods = $data["methods"] == "*" ? $allMethods : $data["methods"];
foreach($data["routes"] as $route){
$data["short"] = $short;
- $r->addRoute($methods, $route, $data);
+ $r->addRoute($methods, $this->base.$route, $data);
}
}
@@ -99,6 +100,8 @@ public function getURIForRequest(ServerRequestInterface $request){
public function route(){
$request = ServerRequestFactory::fromGlobals();
+ $this->base = rtrim(dirname($request->getServerParams()["SCRIPT_NAME"]), "/");
+
$dispatcher = \FastRoute\cachedDispatcher(function(\FastRoute\RouteCollector $r) {
$this->configureRoutes($this->base, $r);
}, $this->cacheOptions);
@@ -113,7 +116,7 @@ public function route(){
if(isSet($data["path"])){
require_once (AJXP_INSTALL_PATH."/".$data["path"]);
}
- call_user_func(array($data["class"], $data["method"]), $data["short"], $routeInfo[2]);
+ call_user_func(array($data["class"], $data["method"]), $this->base, $data["short"], $routeInfo[2]);
break;
case \FastRoute\Dispatcher::NOT_FOUND:
default:
@@ -158,7 +158,7 @@ protected function parseSpecificContributions(&$contribNode)
/**************************/
/* PUBLIC LINKS ROUTER
/**************************/
- public static function publicRoute($route, $params){
+ public static function publicRoute($serverBase, $route, $params){
if(isSet($params["hash"])){
@@ -182,7 +182,7 @@ public static function publicRoute($route, $params){
$h = $_GET['minisite_session'];
\Pydio\Core\Services\SessionService::setSessionName("AjaXplorer_Shared".str_replace(".","_",$h));
- $base->handleRoute("/");
+ $base->handleRoute($serverBase, "/");
}else{
@@ -143,7 +143,7 @@ public function remoteRepositoryById($repositoryId, &$repoObject){
}
- public static function startServer($route){
+ public static function startServer($base, $route){
$pServ = PluginsService::getInstance();
ConfService::$useSession = false;

0 comments on commit f6c8ec6

Please sign in to comment.