xref: /dokuwiki/index.php (revision 94214f97e4a1d5491e048d798c1d5cf63e52fd24)
1<?php
2/**
3 * Forwarder/Router to doku.php
4 *
5 * In normal usage, this script simply redirects to doku.php. However it can also be used as a routing
6 * script with PHP's builtin webserver. It takes care of .htaccess compatible rewriting, directory/file
7 * access permission checking and passing on static files.
8 *
9 * Usage example:
10 *
11 *   php -S localhost:8000 index.php
12 *
13 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
14 * @author     Andreas Gohr <andi@splitbrain.org>
15 */
16if(php_sapi_name() != 'cli-server') {
17    header("Location: doku.php");
18    exit;
19}
20
21# ROUTER starts below
22
23# avoid path traversal
24$_SERVER['SCRIPT_NAME'] = str_replace('/../', '/', $_SERVER['SCRIPT_NAME']);
25
26# routing aka. rewriting
27if(preg_match('/^\/_media\/(.*)/', $_SERVER['SCRIPT_NAME'], $m)) {
28    # media dispatcher
29    $_GET['media'] = $m[1];
30    require $_SERVER['DOCUMENT_ROOT'] . '/lib/exe/fetch.php';
31
32} else if(preg_match('/^\/_detail\/(.*)/', $_SERVER['SCRIPT_NAME'], $m)) {
33    # image detail view
34    $_GET['media'] = $m[1];
35    require $_SERVER['DOCUMENT_ROOT'] . '/lib/exe/detail.php';
36
37} else if(preg_match('/^\/_media\/(.*)/', $_SERVER['SCRIPT_NAME'], $m)) {
38    # exports
39    $_GET['do'] = 'export_' . $m[1];
40    $_GET['id'] = $m[2];
41    require $_SERVER['DOCUMENT_ROOT'] . '/doku.php';
42
43} elseif($_SERVER['SCRIPT_NAME'] == '/index.php') {
44    # 404s are automatically mapped to index.php
45    if(isset($_SERVER['PATH_INFO'])) {
46        $_GET['id'] = $_SERVER['PATH_INFO'];
47    }
48    require $_SERVER['DOCUMENT_ROOT'] . '/doku.php';
49
50} else if(file_exists($_SERVER['DOCUMENT_ROOT'] . $_SERVER['SCRIPT_NAME'])) {
51    # existing files
52
53    # access limitiations
54    if(preg_match('/\/([\._]ht|README$|VERSION$|COPYING$)/', $_SERVER['SCRIPT_NAME']) or
55        preg_match('/^\/(data|conf|bin|inc)\//', $_SERVER['SCRIPT_NAME'])
56    ) {
57        die('Access denied');
58    }
59
60    if(substr($_SERVER['SCRIPT_NAME'], -4) == '.php') {
61        # php scripts
62        require $_SERVER['DOCUMENT_ROOT'] . $_SERVER['SCRIPT_NAME'];
63    } else {
64        # static files
65        return false;
66    }
67}
68# 404
69