574578b2192b99fd060bb9b30c4e04c25f1032c0
[atutor.git] / mods / wiki / plugins / jump.php
1 <?php
2
3 /*
4    This plugin adds a page redirection feature. ewiki instantly switches
5    to another page, when one of the following markup snippets is found:
6
7       [jump:AnotherPage]
8       [goto:SwitchToHere]
9    or
10       [jump:WardsWiki:WelcomeVisitors]
11       [jump:Google:ErfurtWiki:MarioSalzer]
12       [jump:http://www.heise.de/]
13
14    One can also use [redirect:] or [location:]. Page switching only occours
15    with the "view" action. Sending a HTTP redirect is the default, but in
16    place redirects are also possible.
17    There exists a loop protection, which limits redirects to 5 (for browsers
18    that cannot detect this themselfes).
19 */
20
21 #-- config 
22 define("EWIKI_JUMP_HTTP", 1);       #-- issue a HTTP redirect, or jump in place
23 define("EWIKI_UP_REDIRECT_COUNT", "redir");
24
25 #-- text
26 $ewiki_t["en"]["REDIRECTION_LOOP"] = "<h2>Redirection loop detected<h2>\nOperation stopped, because we're traped in an infinite redirection loop with page \$id.";
27
28 #-- plugin glue 
29 $ewiki_plugins["handler"][] = "ewiki_handler_jump";
30 $ewiki_config["interwiki"]["jump"] = "";
31 $ewiki_config["interwiki"]["goto"] = "";
32
33
34 function ewiki_handler_jump(&$id, &$data, &$action) {
35
36    global $ewiki_config;
37
38    static $redirect_count = 5;
39    $jump_markup = array("jump", "goto", "redirect", "location");
40
41    #-- we only care about "view" action
42    if ($action != "view") {
43       return;
44    }
45
46    #-- escape from loop
47    if (isset($_REQUEST[EWIKI_UP_REDIRECT_COUNT])) {
48       $redirect_count = $_REQUEST[EWIKI_UP_REDIRECT_COUNT];
49    }
50    if ($redirect_count-- <= 0) {
51       return(ewiki_t("REDIRECTION_LOOP", array("id"=>$id)));
52    }
53
54    #-- search for [jump:...]
55    if ($links = explode("\n", trim($data["refs"])))
56    foreach ($links as $link) {
57
58       if (strlen($link) && strpos($link, ":")
59       && in_array(strtolower(strtok($link, ":")), $jump_markup)
60       && ($dest = trim(strtok("\n"))) )
61       {
62          #-- URL
63          $url = "";
64          if (strpos($dest, "://")) {
65             $url = $dest;
66          }
67          #-- InterWiki:Link
68          else {
69             $url = ewiki_interwiki($dest, $uu, $uu2);
70          }
71
72          #-- Location:
73          if (EWIKI_JUMP_HTTP && EWIKI_HTTP_HEADERS && !headers_sent()) {
74
75             #-- simple PageLink
76             if (empty($url)) {
77                $url = ewiki_script("", $dest,
78                   array(EWIKI_UP_REDIRECT_COUNT=>$redirect_count),
79                   0, 0, ewiki_script_url()
80                );
81                $url .= defined("SID") ? EWIKI_ADDPARAMDELIM.SID : "";
82             }
83             header("Location: $url");
84             die();
85
86          }
87          #-- show page as usual, what will reveal dest URL
88          elseif ($url) {
89             return("");
90             # the rendering kernel will just show up the [jump:]!
91             # (without the jump: of course)
92          }
93          #-- it's simply about another WikiPage
94          else {
95
96             #-- we'll just restart ewiki
97             $data = array();
98             $id = $dest;
99             return(ewiki_page("view/".$id));
100          }
101       }
102    }#-search
103 }
104
105
106 ?>