]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RedirectTo.php
Replace tabs by spaces; remove EOL spaces
[SourceForge/phpwiki.git] / lib / plugin / RedirectTo.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  * Copyright 2002 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with PhpWiki; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * Redirect to another page or external uri. Kind of PageAlias.
25  * Usage:
26  * <?plugin RedirectTo href="http://www.internet-technology.de/fourwins_de.htm" ?>
27  *      or  <?plugin RedirectTo page=AnotherPage ?>
28  * at the VERY FIRST LINE in the content! Otherwise it will be ignored.
29  *
30  * Author:  Reini Urban <rurban@x-ray.at>
31  *
32  * BUGS/COMMENTS:
33  * Todo: fix with USE_PATH_INFO = false
34  *
35  * This plugin could probably result in a lot of confusion, especially when
36  * redirecting to external sites.  (Perhaps it can even be used for dastardly
37  * purposes?)  Maybe it should be disabled by default.
38  *
39  * It would be nice, when redirecting to another wiki page, to (as
40  * UseModWiki does) add a note to the top of the target page saying
41  * something like "(Redirected from SomeRedirectingPage)".
42  */
43 class WikiPlugin_RedirectTo
44 extends WikiPlugin
45 {
46     function getName() {
47         return _("RedirectTo");
48     }
49
50     function getDescription() {
51         return _("Redirects to another URL or page.");
52     }
53
54     function getVersion() {
55         return preg_replace("/[Revision: $]/", '',
56                             "\$Revision$");
57     }
58
59     function getDefaultArguments() {
60         return array( 'href' => '',
61                       'page' => false,
62                       );
63     }
64
65     function run($dbi, $argstr, &$request, $basepage) {
66         $args = ($this->getArgs($argstr, $request));
67
68         $href = $args['href'];
69         $page = $args['page'];
70         if ($href) {
71             /*
72              * Use quotes on the href argument value, like:
73              *   <?plugin RedirectTo href="http://funky.com/a b \" c.htm" ?>
74              *
75              * Do we want some checking on href to avoid malicious
76              * uses of the plugin? Like stripping tags or hexcode.
77              */
78             $url = preg_replace('/%\d\d/','',strip_tags($href));
79             $thispage = $request->getPage();
80             if (! $thispage->get('locked')) {
81                 return $this->disabled(_("Redirect to an external URL is only allowed in locked pages."));
82             }
83         }
84         else if ($page) {
85             $url = WikiURL($page,
86                            array('redirectfrom' => $request->getArg('pagename')),
87                            'abs_path');
88         }
89         else {
90             return $this->error(_("'href' or 'page' parameter missing."));
91         }
92
93         if ($page == $request->getArg('pagename')) {
94             return $this->error(fmt("Recursive redirect to self: '%s'", $url));
95         }
96
97         if ($request->getArg('action') != 'browse')
98             return $this->disabled("(action != 'browse')");
99
100         $redirectfrom = $request->getArg('redirectfrom');
101         if ($redirectfrom !== false) {
102             if ($redirectfrom)
103                 return $this->disabled(_("Double redirect not allowed."));
104             else {
105                 // Got here by following the "Redirected from ..." link
106                 // on a browse page.
107                 return $this->disabled(_("Viewing redirecting page."));
108             }
109         }
110
111         return $request->redirect($url);
112     }
113 };
114
115 // For emacs users
116 // Local Variables:
117 // mode: php
118 // tab-width: 8
119 // c-basic-offset: 4
120 // c-hanging-comment-ender-p: nil
121 // indent-tabs-mode: nil
122 // End:
123 ?>