]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RedirectTo.php
A bug fix and code cleanup
[SourceForge/phpwiki.git] / lib / plugin / RedirectTo.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RedirectTo.php,v 1.3 2002-09-14 18:28:54 dairiki 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  * RedirectTo:
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  * Actually, it seems that this plugin can be invoked from anywhere on a page.
33  * (Not just the first line.)
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 getDefaultArguments() {
55         return array( 'href' => '',
56                       // 'type' => 'Temp' // or 'Permanent' // s far ignored
57                       'page' => false,
58                       'args' => false,  // pass more args to the page. TestMe!
59                       );
60     }
61
62     function run($dbi, $argstr, $request) {
63         $args = ($this->getArgs($argstr, $request));
64         $href = $args['href'];
65         $page = $args['page'];
66         if (!$href and !$page)
67             return $this->error(sprintf(_("%s or %s parameter missing"), 'href', 'page'));
68         if ($href) {
69             /*
70              * I don't think this hack is needed. 
71              * Just use quotes on the href argument value, like:
72              *
73              *   <?plugin RedirectTo href="http://funky.com/a b \" c.htm" ?>
74              */
75             // // FIXME: unmunged url hack
76             // $url = preg_replace('/href=(.*)\Z/','$1',$argstr);
77             $url = $href;
78             //
79             // FIXME: may want some checking on href to avoid malicious
80             // uses of the plugin?
81         }
82         else {
83             $url = $request->getURLtoSelf(array_merge(array('pagename' => $page), 
84                                                       SplitQueryArgs($args['args'])));
85         }
86         if ($page == $request->getArg('pagename')) {
87             return $this->error(sprintf(_("Recursive redirect to self: '%s'"), $url));
88         }
89         return $request->redirect($url);
90     }
91 };
92
93 // For emacs users
94 // Local Variables:
95 // mode: php
96 // tab-width: 8
97 // c-basic-offset: 4
98 // c-hanging-comment-ender-p: nil
99 // indent-tabs-mode: nil
100 // End:
101 ?>