]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RedirectTo.php
Allow external url redirection only on locked pages.
[SourceForge/phpwiki.git] / lib / plugin / RedirectTo.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RedirectTo.php,v 1.5 2002-10-14 16:28:13 carstenklapp Exp $');
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:   <?plugin-head RedirectTo href="http://www.internet-technology.de/fourwins_de.htm" ?>
26  *      or  <?plugin-head RedirectTo page=AnotherPage ?>
27  *          at the VERY FIRST LINE in the content! Otherwise it will be ignored.
28  * Author:  Reini Urban <rurban@x-ray.at>
29  *
30  * BUGS/COMMENTS:
31  *
32  * This plugin could probably result in a lot of confusion, especially when
33  * redirecting to external sites.  (Perhaps it can even be used for dastardly
34  * purposes?)  Maybe it should be disabled by default.
35  *
36  * It would be nice, when redirecting to another wiki page, to (as
37  * UseModWiki does) add a note to the top of the target page saying
38  * something like "(Redirected from SomeRedirectingPage)".
39  */
40 class WikiPlugin_RedirectTo
41 extends WikiPlugin
42 {
43     function getName() {
44         return _("RedirectTo");
45     }
46
47     function getDescription() {
48         return _("Redirects to another url or page.");
49     }
50
51     function getDefaultArguments() {
52         return array( 'href' => '',
53                       // 'type' => 'Temp' // or 'Permanent' // so far ignored
54                       'page' => false,
55                       'args' => false,  // pass more args to the page. TestMe!
56                       );
57     }
58
59     function run($dbi, $argstr, $request) {
60         $args = ($this->getArgs($argstr, $request));
61         $href = $args['href'];
62         $page = $args['page'];
63         if ($href) {
64             /*
65              * Use quotes on the href argument value, like:
66              *   <?plugin RedirectTo href="http://funky.com/a b \" c.htm" ?>
67              *
68              * Do we want some checking on href to avoid malicious
69              * uses of the plugin? Like stripping tags or hexcode.
70              */
71             $url = preg_replace('/%\d\d/','',strip_tags($href));
72             $thispage = $request->getPage();
73             if (! $thispage->get('locked')) {
74                 return $this->error(HTML(fmt(_("%s is only allowed in locked pages."),
75                                              _("Redirect to an external url")),
76                                          HTML::br(),
77                                          fmt("url=\"%s\"", $url)));
78             }
79         }
80         else if ($page) {
81             $url = $request->getURLtoSelf(array_merge(array('pagename' => $page,
82                                                             'redirectfrom' => $request->getArg('pagename')),
83                                                       SplitQueryArgs($args['args'])));
84         }
85         else
86             return $this->error(sprintf(_("%s or %s parameter missing"), "'href'", "'page'"));
87
88         if ($page == $request->getArg('pagename')) {
89             return $this->error(sprintf(_("Recursive redirect to self: '%s'"), $url));
90         }
91         return $request->redirect($url);
92     }
93 };
94
95 // For emacs users
96 // Local Variables:
97 // mode: php
98 // tab-width: 8
99 // c-basic-offset: 4
100 // c-hanging-comment-ender-p: nil
101 // indent-tabs-mode: nil
102 // End:
103 ?>