Permalink
Browse files

New URL Routing system.

This will break *EVERYTHING*. Just wait while I fix pages to work with this system.

ATTN addon devs (they exist?):
actionLink will be deprecated in the future. Just wait until the new method to make links is made.
  • Loading branch information...
Repflez committed Mar 20, 2017
1 parent 56b53bb commit 0179f131eac018e96251eadf5dfb10b26c8a0121
Showing with 1,754 additions and 48 deletions.
  1. +27 −47 index.php
  2. +18 −0 lib/common.php
  3. +19 −1 lib/pluginsystem.php
  4. +268 −0 lib/router.php
  5. +9 −0 lib/urls.yaml
  6. +252 −0 lib/urlslugs.php
  7. +1,161 −0 lib/yaml.php
@@ -4,7 +4,7 @@
define('BLARG', 1);
// change this to change your board's default page
define('MAIN_PAGE', 'home');
define('MAIN_PAGE', 'index');
$ajaxPage = false;
if(isset($_GET['ajax']))
@@ -37,7 +37,6 @@
//Support for running pages from the terminal.
if(isset($argv)) {
$_GET = array();
$_GET["page"] = $argv[1];
$_SERVER = array();
$_SERVER["REMOTE_ADDR"] = "0.0.0.0";
@@ -50,25 +49,7 @@
//=======================
// Do the page
if (isset($_GET['page']))
$page = $_GET['page'];
else
$page = MAIN_PAGE;
if(!ctype_alnum($page))
$page = MAIN_PAGE;
if($page == MAIN_PAGE) {
if(isset($_GET['fid']) && (int)$_GET['fid'] > 0 && !isset($_GET['action']))
die(header("Location: ".actionLink("forum", (int)$_GET['fid'])));
if(isset($_GET['tid']) && (int)$_GET['tid'] > 0)
die(header("Location: ".actionLink("thread", (int)$_GET['tid'])));
if(isset($_GET['uid']) && (int)$_GET['uid'] > 0)
die(header("Location: ".actionLink("profile", (int)$_GET['uid'])));
if(isset($_GET['pid']) && (int)$_GET['pid'] > 0)
die(header("Location: ".actionLink("post", (int)$_GET['pid'])));
}
define('CURRENT_PAGE', $page);
$match = $router->match();
ob_start();
$layout_crumbs = "";
@@ -86,39 +67,38 @@
if (!$fakeerror) {
try {
try {
if(array_key_exists($page, $pluginpages)) {
$pageName = $page;
$plugin = $pluginpages[$pageName];
// Throw the 404 page if we don't have a match already.
if ($match === false)
require_once(__DIR__.'/pages/404.php');
else {
// Set up the stuff for our page loader.
$pageName = $match['target'];
$pageParams = $match['params'];
// Check first for plugin pages.
if(array_key_exists($pageName, $pluginpages)) {
// TODO: Make this cleaner than a hack.
$self = $plugins[$plugin];
$page = __DIR__.'/plugins/'.$self['dir']."/pages/".$pageName.".php";
$page_ABXD = __DIR__.'/plugins/'.$self['dir']."/page_".$pageName.".php";
$plugin_ABXD = $pluginpages[$pageName];
$self_ABXD = $plugins[$plugin];
$addonWWXD = __DIR__.'/plugins/'.$self['dir'].'/pages/'.$pageName.'.php';
$addonABXD = __DIR__.'/plugins/'.$self['dir'].'/page_'.$pageName.'.php';
if(file_exists($page))
include($page);
elseif (file_exists($page_ABXD))
include($page_ABXD);
if (file_exists($addonWWXD))
require_once($addonWWXD);
elseif (file_exists($addonABXD))
require_once($addonABXD);
else
throw new Exception(404);
unset($self);
unset($self_ABXD);
require_once(__DIR__.'/pages/404.php');
} else {
$page = __DIR__.'/pages/'.$page.'.php';
if(!file_exists($page))
throw new Exception(404);
include($page);
// Check now for core pages.
$page = $page = __DIR__.'/pages/'.$pageName.'.php';
if (file_exists($page))
require_once($page);
else
require_once(__DIR__.'/pages/404.php');
}
}
catch(Exception $e) {
if ($e->getMessage() != 404) { throw $e; }
require(__DIR__.'/pages/404.php');
}
} catch(KillException $e) {
// Nothing. Just ignore this exception.
}
@@ -69,6 +69,9 @@ function usectime() {
require_once(__DIR__."/functions.php");
require_once(__DIR__."/language.php");
require_once(__DIR__."/links.php");
require_once(__DIR__."/urlslugs.php");
require_once(__DIR__."/yaml.php");
require_once(__DIR__."/router.php");
class KillException extends Exception { }
date_default_timezone_set("GMT");
@@ -78,10 +81,25 @@ class KillException extends Exception { }
//WARNING: These things need to be kept in a certain order of execution.
// TODO: Nuke this.
$thisURL = $_SERVER['SCRIPT_NAME'];
if($q = $_SERVER['QUERY_STRING'])
$thisURL .= "?$q";
// Init the router
$router = new AltoRouter();
// Add a special regex for our purposes
$router->addMatchTypes(['s' => '[0-9A-Za-z\-]+']);
// Load the basic URLs we use by default via the YAML file
$routes = spyc_load_file(__DIR__."/urls.yaml");
// Map our routes
foreach ($routes as $route_name => $params) {
$router->map($params[0], $params[1], $params[2], $route_name);
}
require_once(__DIR__."/browsers.php");
require_once(__DIR__."/pluginsystem.php");
loadFieldLists();
@@ -31,7 +31,7 @@ class BadPluginException extends Exception { }
// TODO cache all those data so we don't have to scan directories at each run
function getPluginData($plugin, $load = true) {
global $pluginpages, $pluginbuckets, $plugintemplates, $misc, $abxd_version;
global $pluginpages, $pluginbuckets, $plugintemplates, $misc, $abxd_version, $router;
if(!is_dir(__DIR__."/../plugins/".$plugin))
throw new BadPluginException("Plugin folder is gone");
@@ -108,6 +108,24 @@ function getPluginData($plugin, $load = true) {
closedir($pdir);
}
if(file_exists($dir.'/routes.yaml')) {
$routes = spyc_load_file($dir.'/routes.yaml');
$allRoutes = $router->getRoutes();
$existingRoutes = array();
foreach ($allRoutes as $route) {
$existingRoutes[] = $route[3];
}
foreach ($routes as $route_name => $params) {
if (in_array($route_name, $existingRoutes))
throw new BadPluginException(__("Plugin route \"$route_name\" conflicts with an existing route."));
}
// Trust me, this is to avoid polluting the routes if there's a conflict that we dealt with earlier.
foreach ($routes as $route_name => $params) {
$router->map($params[0], $params[1], $params[2], $route_name);
}
}
return $plugindata;
}
Oops, something went wrong.

0 comments on commit 0179f13

Please sign in to comment.