115fae107Sandi<?php 215fae107Sandi/** 37aaab109SAndreas Gohr * Forwarder/Router to doku.php 47aaab109SAndreas Gohr * 57aaab109SAndreas Gohr * In normal usage, this script simply redirects to doku.php. However it can also be used as a routing 67aaab109SAndreas Gohr * script with PHP's builtin webserver. It takes care of .htaccess compatible rewriting, directory/file 77aaab109SAndreas Gohr * access permission checking and passing on static files. 87aaab109SAndreas Gohr * 97aaab109SAndreas Gohr * Usage example: 107aaab109SAndreas Gohr * 117aaab109SAndreas Gohr * php -S localhost:8000 index.php 1215fae107Sandi * 1315fae107Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 1415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1515fae107Sandi */ 16*b1f206e1SAndreas Gohrif (PHP_SAPI != 'cli-server') { 17*b1f206e1SAndreas Gohr if (!defined('DOKU_INC')) define('DOKU_INC', __DIR__ . '/'); 187f4718ddSPhy require_once(DOKU_INC . 'inc/init.php'); 197f4718ddSPhy 20aceca2ebSSchplurtz le Déboulonné send_redirect(wl($conf['start'])); 217aaab109SAndreas Gohr} 227aaab109SAndreas Gohr 237f153c56SAndreas Gohr// ROUTER starts below 247aaab109SAndreas Gohr 257f153c56SAndreas Gohr// avoid path traversal 267aaab109SAndreas Gohr$_SERVER['SCRIPT_NAME'] = str_replace('/../', '/', $_SERVER['SCRIPT_NAME']); 277aaab109SAndreas Gohr 287f153c56SAndreas Gohr// routing aka. rewriting 297aaab109SAndreas Gohrif (preg_match('/^\/_media\/(.*)/', $_SERVER['SCRIPT_NAME'], $m)) { 307f153c56SAndreas Gohr // media dispatcher 317aaab109SAndreas Gohr $_GET['media'] = $m[1]; 327aaab109SAndreas Gohr require $_SERVER['DOCUMENT_ROOT'] . '/lib/exe/fetch.php'; 337aaab109SAndreas Gohr 347aaab109SAndreas Gohr} elseif (preg_match('/^\/_detail\/(.*)/', $_SERVER['SCRIPT_NAME'], $m)) { 357f153c56SAndreas Gohr // image detail view 367aaab109SAndreas Gohr $_GET['media'] = $m[1]; 377aaab109SAndreas Gohr require $_SERVER['DOCUMENT_ROOT'] . '/lib/exe/detail.php'; 387aaab109SAndreas Gohr 3962c31ba5SRainbow Spike} elseif (preg_match('/^\/_export\/([^\/]+)\/(.*)/', $_SERVER['SCRIPT_NAME'], $m)) { 407f153c56SAndreas Gohr // exports 417aaab109SAndreas Gohr $_GET['do'] = 'export_' . $m[1]; 427aaab109SAndreas Gohr $_GET['id'] = $m[2]; 437aaab109SAndreas Gohr require $_SERVER['DOCUMENT_ROOT'] . '/doku.php'; 447aaab109SAndreas Gohr 457f153c56SAndreas Gohr} elseif ( 467f153c56SAndreas Gohr $_SERVER['SCRIPT_NAME'] !== '/index.php' && 477f153c56SAndreas Gohr file_exists($_SERVER['DOCUMENT_ROOT'] . $_SERVER['SCRIPT_NAME']) 487f153c56SAndreas Gohr) { 497f153c56SAndreas Gohr // existing files 507aaab109SAndreas Gohr 517f153c56SAndreas Gohr // access limitiations 52*b1f206e1SAndreas Gohr if ( 53*b1f206e1SAndreas Gohr preg_match('/\/([._]ht|README$|VERSION$|COPYING$)/', $_SERVER['SCRIPT_NAME']) || 5473cc470bSAndreas Gohr preg_match('/^\/(data|conf|bin|inc)\//', $_SERVER['SCRIPT_NAME']) 557aaab109SAndreas Gohr ) { 56a4d15dc0SAndreas Gohr header('HTTP/1.1 403 Forbidden'); 577aaab109SAndreas Gohr die('Access denied'); 587aaab109SAndreas Gohr } 597aaab109SAndreas Gohr 607aaab109SAndreas Gohr if (substr($_SERVER['SCRIPT_NAME'], -4) == '.php') { 617aaab109SAndreas Gohr # php scripts 627aaab109SAndreas Gohr require $_SERVER['DOCUMENT_ROOT'] . $_SERVER['SCRIPT_NAME']; 637aaab109SAndreas Gohr } else { 647aaab109SAndreas Gohr # static files 657aaab109SAndreas Gohr return false; 667aaab109SAndreas Gohr } 677f153c56SAndreas Gohr} else { 687f153c56SAndreas Gohr // treat everything else as a potential wiki page 697f153c56SAndreas Gohr // working around https://bugs.php.net/bug.php?id=61286 70d40ef125SAndreas Gohr $request_path = preg_split('/\?/', $_SERVER['REQUEST_URI'], 2)[0]; 717f153c56SAndreas Gohr if (isset($_SERVER['PATH_INFO'])) { 727f153c56SAndreas Gohr $_GET['id'] = $_SERVER['PATH_INFO']; 7309f71c98SAndreas Gohr } elseif ($request_path != '/' && $request_path != '/index.php') { 747f153c56SAndreas Gohr $_GET['id'] = $_SERVER['SCRIPT_NAME']; 757aaab109SAndreas Gohr } 767f153c56SAndreas Gohr 777f153c56SAndreas Gohr require $_SERVER['DOCUMENT_ROOT'] . '/doku.php'; 787f153c56SAndreas Gohr} 79