Permalink
Browse files

Add a top-level router

  • Loading branch information...
1 parent b06aa8e commit 7f73957c16e552dbbae1b490c231def4fdfeddcd @cdujeu cdujeu committed May 25, 2016
View
@@ -8,14 +8,6 @@ RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
-RewriteRule ^shares ./dav.php [L]
-RewriteRule ^api ./rest.php [L]
-RewriteRule ^ocs/ ./opencollab.php [L]
-RewriteRule ^ocs-provider/ ./opencollab.php [L]
-RewriteRule ^user ./index.php?get_action=user_access_point [L]
-RewriteCond %{REQUEST_URI} !^/index
-RewriteCond %{REQUEST_URI} !^/plugins
-RewriteCond %{REQUEST_URI} ^/dashboard|^/settings|^/welcome|^/ws-
RewriteRule (.*) index.php [L]
#Following lines seem to be necessary if PHP is working
@@ -1,6 +1,6 @@
<?php
/*
- * Copyright 2007-2016 Charles du Jeu <contact (at) cdujeu.me>
+ * Copyright 2007-2015 Abstrium <contact (at) pydio.com>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
@@ -17,18 +17,36 @@
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
- *
- * Description : Real RESTful API access
*/
+namespace Pydio\Core\Http;
+
use Pydio\Core\Services\ConfService;
-use Pydio\Core\Http\Rest\RestServer;
-include_once("base.conf.php");
+defined('AJXP_EXEC') or die('Access not allowed');
+
+
+class Base
+{
+
+ /**
+ * @param string $route
+ */
+ public static function handleRoute($route){
-$server = new RestServer("/api");
-$server->registerCatchAll();
+ if($route === "/api") {
+ $server = new Rest\RestServer("/api");
+ }else if($route === "/user"){
+ $_GET["get_action"] = "user_access_point";
+ $server = new Server();
+ }else{
+ $server = new Server();
+ }
+ $server->registerCatchAll();
-ConfService::init();
-ConfService::start();
+ ConfService::init();
+ ConfService::start();
+
+ $server->listen();
+ }
-$server->listen();
+}
@@ -0,0 +1,127 @@
+<?php
+/*
+ * Copyright 2007-2015 Abstrium <contact (at) pydio.com>
+ * This file is part of Pydio.
+ *
+ * Pydio 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.
+ *
+ * Pydio 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 Pydio. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * The latest code can be found at <http://pyd.io/>.
+ */
+namespace Pydio\Core\Http;
+
+use AJXP_Sabre_AuthBackendBasic;
+use AJXP_Sabre_AuthBackendDigest;
+use AJXP_Sabre_BrowserPlugin;
+use AJXP_Sabre_Collection;
+use AJXP_Sabre_RootCollection;
+use Pydio\Core\PluginFramework\PluginsService;
+use Pydio\Core\Services\ConfService;
+use Pydio\Log\Core\AJXP_Logger;
+use Sabre\DAV as DAV;
+
+defined('AJXP_EXEC') or die('Access not allowed');
+
+
+class DAVServer
+{
+ public static function handleRoute($baseURI){
+
+ ConfService::init();
+ ConfService::start();
+
+ if (!ConfService::getCoreConf("WEBDAV_ENABLE")) {
+ die('You are not allowed to access this service');
+ }
+
+ PluginsService::getInstance()->initActivePlugins();
+
+ spl_autoload_register(function ($className){
+ if (strpos($className,'AJXP_Sabre_')===0) {
+ include AJXP_BIN_FOLDER. '/sabredav/ajaxplorer/class.' . $className . '.php';
+ }
+ });
+ include AJXP_BIN_FOLDER.'/sabredav/lib/Sabre/autoload.php';
+
+ $requestUri = $_SERVER["REQUEST_URI"];
+ if (substr($requestUri, 0, strlen($baseURI)) != $baseURI)
+ {
+ $baseURI = substr($requestUri, 0, stripos($requestUri, $baseURI)) . $baseURI;
+ }
+ $end = trim(substr($requestUri, strlen($baseURI."/")));
+ $rId = null;
+ if ((!empty($end) || $end ==="0") && $end[0] != "?") {
+
+ $parts = explode("/", $end);
+ $pathBase = $parts[0];
+ $repositoryId = $pathBase;
+
+ $repository = ConfService::getRepositoryById($repositoryId);
+ if ($repository == null) {
+ $repository = ConfService::getRepositoryByAlias($repositoryId);
+ if ($repository != null) {
+ $repositoryId = $repository->getId();
+ }
+ }
+ if ($repository == null) {
+ AJXP_Logger::debug("not found, dying $repositoryId");
+ die('You are not allowed to access this service');
+ }
+
+ $rId = $repositoryId;
+ $rootDir = new AJXP_Sabre_Collection("/", $repository, null);
+ $server = new DAV\Server($rootDir);
+ $server->setBaseUri($baseURI."/".$pathBase);
+
+
+ } else {
+
+ $rootDir = new AJXP_Sabre_RootCollection("root");
+ $server = new DAV\Server($rootDir);
+ $server->setBaseUri($baseURI);
+
+ }
+
+ if((AJXP_Sabre_AuthBackendBasic::detectBasicHeader() || ConfService::getCoreConf("WEBDAV_FORCE_BASIC"))
+ && ConfService::getAuthDriverImpl()->getOptionAsBool("TRANSMIT_CLEAR_PASS")){
+ $authBackend = new AJXP_Sabre_AuthBackendBasic($rId);
+ } else {
+ $authBackend = new AJXP_Sabre_AuthBackendDigest($rId);
+ }
+ $authPlugin = new DAV\Auth\Plugin($authBackend, ConfService::getCoreConf("WEBDAV_DIGESTREALM"));
+ $server->addPlugin($authPlugin);
+
+ if (!is_dir(AJXP_DATA_PATH."/plugins/server.sabredav")) {
+ mkdir(AJXP_DATA_PATH."/plugins/server.sabredav", 0755);
+ $fp = fopen(AJXP_DATA_PATH."/plugins/server.sabredav/locks", "w");
+ fwrite($fp, "");
+ fclose($fp);
+ }
+
+ $lockBackend = new DAV\Locks\Backend\File(AJXP_DATA_PATH."/plugins/server.sabredav/locks");
+ $lockPlugin = new DAV\Locks\Plugin($lockBackend);
+ $server->addPlugin($lockPlugin);
+
+ if (ConfService::getCoreConf("WEBDAV_BROWSER_LISTING")) {
+ $browerPlugin = new AJXP_Sabre_BrowserPlugin((isSet($repository)?$repository->getDisplay():null));
+ $extPlugin = new DAV\Browser\GuessContentType();
+ $server->addPlugin($browerPlugin);
+ $server->addPlugin($extPlugin);
+ }
+ try {
+ $server->exec();
+ } catch ( \Exception $e ) {
+ AJXP_Logger::error(__CLASS__,"Exception",$e->getMessage());
+ }
+ }
+}
@@ -24,7 +24,6 @@
use Pydio\Authfront\Core\FrontendsLoader;
use Pydio\Core\Exception\PydioException;
use Pydio\Core\Exception\WorkspaceNotFoundException;
-use Pydio\Core\Http\Rest\RestServer;
use Pydio\Core\Services\AuthService;
use Pydio\Core\Services\ConfService;
@@ -0,0 +1,123 @@
+<?php
+/*
+ * Copyright 2007-2015 Abstrium <contact (at) pydio.com>
+ * This file is part of Pydio.
+ *
+ * Pydio 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.
+ *
+ * Pydio 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 Pydio. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * The latest code can be found at <http://pyd.io/>.
+ */
+namespace Pydio\Core\Http;
+
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Pydio\Core\Exception\PydioException;
+use Zend\Diactoros\ServerRequestFactory;
+
+defined('AJXP_EXEC') or die('Access not allowed');
+
+
+/**
+ * Class TopLevelRouter
+ * Creates a simple router for top level segments. Will replace the RewriteRules and others.
+ *
+ * @package Pydio\Core\Http
+ */
+class TopLevelRouter
+{
+ /**
+ * @var array
+ * "cacheOptions" => ["cacheFile" => "path", "cacheDisabled" => true],
+ */
+ private $cacheOptions;
+
+ private $base = "";
+
+ /**
+ * TopLevelRouter constructor.
+ * @param array $cacheOptions
+ */
+ public function __construct($cacheOptions = []){
+ $this->cacheOptions = array_merge([
+ "cacheDisabled" => AJXP_SKIP_CACHE,
+ "cacheFile" => AJXP_DATA_PATH."/cache/plugins_toplevel_routes.php"
+ ], $cacheOptions);
+ }
+
+
+ /**
+ * @param string $base Base URI (empty string if "/").
+ * @param \FastRoute\RouteCollector $r
+ */
+ public function configureRoutes($base, \FastRoute\RouteCollector &$r){
+
+ $allMethods = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'TRACE', 'OPTIONS', 'CONNECT', 'PATCH'];
+ $file = AJXP_DATA_PATH."/".AJXP_PLUGINS_FOLDER."/boot.conf/routes.json";
+ if(!file_exists($file)){
+ $file = AJXP_INSTALL_PATH."/".AJXP_PLUGINS_FOLDER."/core.ajaxplorer/routes.json";
+ }
+ $routes = json_decode(file_get_contents($file), true);
+ foreach ($routes as $short => $data){
+ $methods = $data["methods"] == "*" ? $allMethods : $data["methods"];
+ foreach($data["routes"] as $route){
+ $data["short"] = $short;
+ $r->addRoute($methods, $route, $data);
+ }
+ }
+
+ }
+
+ /**
+ * Simple parser to get URI
+ * @param ServerRequestInterface $request
+ * @return string
+ */
+ public function getURIForRequest(ServerRequestInterface $request){
+
+ $uri = $request->getServerParams()['REQUEST_URI'];
+ // Strip query string (?foo=bar) and decode URI
+ if (false !== $pos = strpos($uri, '?')) {
+ $uri = substr($uri, 0, $pos);
+ }
+ return rawurldecode($uri);
+ }
+
+ /**
+ * @throws PydioException
+ */
+ public function route(){
+
+ $request = ServerRequestFactory::fromGlobals();
+ $dispatcher = \FastRoute\cachedDispatcher(function(\FastRoute\RouteCollector $r) {
+ $this->configureRoutes($this->base, $r);
+ }, $this->cacheOptions);
+
+ $httpMethod = $request->getServerParams()['REQUEST_METHOD'];
+ $uri = $this->getURIForRequest($request);
+ $routeInfo = $dispatcher->dispatch($httpMethod, $uri);
+
+ switch ($routeInfo[0]) {
+ case \FastRoute\Dispatcher::FOUND:
+ $data = $routeInfo[1];
+ call_user_func(array($data["class"], $data["method"]), $data["short"]);
+ break;
+ case \FastRoute\Dispatcher::NOT_FOUND:
+ default:
+ throw new PydioException("Oups, could not find any valid route for ".$uri);
+ break;
+ }
+
+ }
+
+}
Oops, something went wrong.

0 comments on commit 7f73957

Please sign in to comment.