1<?php
2/**
3* GOTO Plugin: Easily redirect to other pages in your wiki.
4*
5* @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6* @author     Allen Ormond <aormond atgmaildotcom>
7 *@author    Myron Turner <turnermm02@shaw.ca>
8*/
9
10if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'syntax.php');
13
14class  syntax_plugin_goto extends DokuWiki_Syntax_Plugin {
15
16	function getType(){
17		return('substition');
18		}
19
20		function connectTo($mode) {
21			$this->Lexer->addSpecialPattern('(?i)~~GOTO>.+?~~',$mode,'plugin_goto');
22			}
23
24			function getSort() {
25				return 249;
26			}
27
28			function handle($match, $state, $pos, Doku_Handler $handler){
29                 global $INPUT;
30				 $userid = $INPUT->server->str('REMOTE_USER');
31                 $is_usr = false;
32                 $is_extern = false;
33				$seconds = $this->getConf('seconds');        //Default number of seconds to wait before redirect.
34				$minSeconds = $this->getConf('minSeconds');      //Minimum number of seconds allowed before redirect.
35
36				/* $message is the redirection message that is displayed. %d will be replaced with a link
37				*  to the destination. %s will be replaced with the number of seconds before redirect. */
38				$message = "<strong>".$this->getLang('redirect')."</strong>";
39				$matches = explode("?", substr($match,7,-2));
40                if($matches[0] == 'user') {
41                   $matches[0] = "user";  // wildcard replaced in javascript   goto_redirect()
42                    $is_usr = 'user';
43					$seconds = 3;
44                }
45                else if (preg_match("#^\s*https?:\/\/#",$matches[0])) {
46                    $is_extern ='extern';
47                }
48
49				if (is_numeric($matches[1])){
50                    $seconds = $matches[1];
51                }
52               else if(!is_numeric($matches[1]) && $is_extern) {
53                     if (is_numeric($matches[2])){
54                       $seconds = $matches[2];
55                   }
56                    $matches[0] .= '?' . $matches[1];
57                }
58				if ($seconds < $minSeconds){ $seconds = $minSeconds; }//Check that seconds is greater than $minSeconds.
59				$message = str_replace("%D","%d",$message);//Make %d case insensitive.
60				$message = str_replace("%S","%s",$message);//Make %s case insensitive.
61				return array($matches[0], $seconds, $message,$is_usr,$is_extern);
62			}
63
64             function render($mode, Doku_Renderer $renderer, $data) {
65                if($mode != 'xhtml') return false;
66                global $updateVersion;
67                global $ACT;
68                $internal_link = false;
69                if(!$data[3]) {
70                    if(!$data[4]) {
71                        $message = str_replace("%d",$renderer->internallink($data[0], $data[0],'', true),$data[2]);
72                        $internal_link = true;
73                    }
74                    else {
75                       $message = str_replace("%d",$data[0],$data[2]);
76                       $url = $data[0];
77                    }
78                    $message = str_replace("%s",$data[1],$message);
79                    $renderer->doc .= $message;
80                }
81                if(!$data[4]) {
82                    $urlArr = explode('#', $data[0], 2);
83                    $url = wl($urlArr[0]);
84                    if (count($urlArr) > 1) {
85					$url .= '#'.cleanID($urlArr[1]);
86                    }
87                }
88				if ($ACT != 'preview') {
89                    if(!$data[3] && !$data[4]) {
90                        $tm= $data[1]*1000;
91                        if($updateVersion > 50) {
92                            $renderer->doc .= '<span id = "goto_go">'.$url .';'.$tm.'</span>';
93                        }
94                        else {
95                            $renderer->doc .= "<script>var goto_tm= setTimeout(function(){goto_redirect('$url', '$data[4]');},$tm);</script>";
96                        }
97                    }
98					else{
99				        $tm =($data[1]*1000);
100                        if($data[3] == 'user') $data[4] = 'user';
101			            $renderer->doc .= "<script>var goto_tm= setTimeout(function(){goto_redirect('$url', '$data[4]');},$tm);</script>";
102					}
103				}
104                 return true;
105			}
106		}
107