1<?php
2/**
3 * Redirector - Page With Regex in redirectURLs.txt
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10if(!defined('DOKU_INC')) die();
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'action.php');
13
14class action_plugin_redirector extends DokuWiki_Action_Plugin {
15
16    var $redirectFileName = 'redirectURLs.txt';
17
18    function register(Doku_Event_Handler $controller) {
19        $controller->register_hook('ACTION_HEADERS_SEND', 'BEFORE',  $this, '_redirector');
20    }
21
22    function _redirector(Doku_Event &$event, $param) {
23        global $INFO, $ACT, $conf, $ID;
24
25        if ( $INFO['exists'] ){ return; }
26        if ( !($ACT == 'notfound' || $ACT == 'show' || substr($ACT,0,7) == 'export_') ) { return; }
27
28        if ( file_exists(tpl_incdir() . $this->redirectFileName) ) {
29            // Look for the redirect file in template directory
30            $this->redirectFileName = tpl_incdir() . $this->redirectFileName;
31        } else if ( file_exists(DOKU_INC . 'conf/' . $this->redirectFileName) ) {
32            // Look for the redirect file in template directory
33            $this->redirectFileName = DOKU_INC . 'conf/' . $this->redirectFileName;
34        } else if ( file_exists(dirname(__FILE__) . '/' . $this->redirectFileName) ) {
35            // Look for the redirect file in plugin directery
36            $this->redirectFileName = dirname(__FILE__) . '/' . $this->redirectFileName;
37        } else {
38            // Nothing be done
39            return;
40        }
41
42        $redirectURLs = confToHash($this->redirectFileName);
43        $checkID = strpos($_SERVER["REQUEST_URI"], '?') === false ? $_SERVER["REQUEST_URI"] : substr($_SERVER["REQUEST_URI"], 0, strpos($_SERVER["REQUEST_URI"], '?'));
44        if ( substr($checkID, 0, 1)  != '/' ) $checkID = '/' . $checkID;
45
46        $return = preg_replace( array_keys($redirectURLs), array_values($redirectURLs), strtolower($checkID));
47        if ( substr($return , -1)  == '/' ) $return  .= $conf['start'];
48
49        if ( $return == strtolower($checkID) ) {
50            return;
51        }
52
53        # referer must be set - and its not a bot.
54        if ( $this->getConf('doLog') && !empty($_SERVER['HTTP_REFERER']) && !preg_match('/(?i)bot/', $_SERVER['HTTP_USER_AGENT'])) { dbglog("Redirecting: '{$checkID}' to '{$return}'"); }
55
56        if ( !empty($_GET) ) {
57            unset($_GET['id']);
58
59            $params = '';
60            foreach( $_GET as $key => $value ) {
61                if ( !empty($params) ) {
62                    $params .= '&';
63                }
64                $params .= urlencode($key) . "=" . urlencode($value);
65            }
66
67            if ( !empty($params) ) {
68                $return .= '?' . $params;
69            }
70        }
71
72        if ( $return != $_SERVER['REQUEST_URI'] ) {
73            send_redirect($return);
74            exit;
75        }
76    }
77
78}
79