Skip to content

Instantly share code, notes, and snippets.

@Rivsen
Created September 22, 2015 12:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rivsen/3300e63a3b0b6ce600e4 to your computer and use it in GitHub Desktop.
Save Rivsen/3300e63a3b0b6ce600e4 to your computer and use it in GitHub Desktop.
a php internat server route file for flarum
<?php
// Set timezone
// date_default_timezone_set("UTC");
// Directory that contains error pages
define("ERRORS", dirname(__FILE__) . "/flarum/error");
// Default index files
define("WEB_INDEX", "index.php");
define("API_INDEX", "api.php");
define("ADMIN_INDEX", "admin.php");
// Optional array of authorized client IPs for a bit of security
$config["hostsAllowed"] = array('127.0.0.1');
function logAccess($status = 200) {
file_put_contents(
"php://stdout",
sprintf("[%s] %s:%s [%s]: %sn",
date("D M j H:i:s Y"), $_SERVER["REMOTE_ADDR"],
$_SERVER["REMOTE_PORT"], $status, $_SERVER["REQUEST_URI"])
);
}
// Parse allowed host list
if (!empty($config['hostsAllowed'])) {
if (!in_array($_SERVER['REMOTE_ADDR'], $config['hostsAllowed'])) {
logAccess(403);
http_response_code(403);
include ERRORS . '/403.html';
exit;
}
}
// if requesting a directory then serve the default index
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
$ext = pathinfo($path, PATHINFO_EXTENSION);
$path_arr = explode('/', $path);
if (empty($ext)) {
// check flarum's route
switch( $path_arr[1] ) {
case 'api' :
case 'api.php' :
$path = "/" . API_INDEX;
break;
case 'admin' :
case 'admin.php' :
$path = "/" . ADMIN_INDEX;
break;
default :
$path = "/" . WEB_INDEX;
}
// if the file exists then include it and execute it
if (file_exists($_SERVER["DOCUMENT_ROOT"] . $path)) {
//return false;
require_once './'.$path;
exit;
}
} else {
// If the file exists then return false and let the server handle it
if (file_exists($_SERVER["DOCUMENT_ROOT"] . $path)) {
return false;
}
}
// default behavior
logAccess(404);
http_response_code(404);
include ERRORS . "/404.html";
#!/bin/bash
php -S localhost:8000 routes.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment