]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RedirectTo.php
Usability improvements for the RedirectTo plugin.
[SourceForge/phpwiki.git] / lib / plugin / RedirectTo.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RedirectTo.php,v 1.7 2003-02-15 23:32:56 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  * Redirect to another page or external uri. Kind of PageAlias.
25  * Usage:
26  * <?plugin-head RedirectTo href="http://www.internet-technology.de/fourwins_de.htm" ?>
27  *      or  <?plugin-head 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  *
34  * This plugin could probably result in a lot of confusion, especially when
35  * redirecting to external sites.  (Perhaps it can even be used for dastardly
36  * purposes?)  Maybe it should be disabled by default.
37  *
38  * It would be nice, when redirecting to another wiki page, to (as
39  * UseModWiki does) add a note to the top of the target page saying
40  * something like "(Redirected from SomeRedirectingPage)".
41  */
42 class WikiPlugin_RedirectTo
43 extends WikiPlugin
44 {
45     function getName() {
46         return _("RedirectTo");
47     }
48
49     function getDescription() {
50         return _("Redirects to another url or page.");
51     }
52
53     function getVersion() {
54         return preg_replace("/[Revision: $]/", '',
55                             "\$Revision: 1.7 $");
56     }
57
58     function getDefaultArguments() {
59         return array( 'href' => '',
60                       // 'type' => 'Temp' // or 'Permanent' // so far ignored
61                       'page' => false,
62                       'args' => false,  // pass more args to the page. TestMe!
63                       );
64     }
65
66     function run($dbi, $argstr, $request) {
67         $args = ($this->getArgs($argstr, $request));
68
69         $href = $args['href'];
70         $page = $args['page'];
71         if ($href) {
72             /*
73              * Use quotes on the href argument value, like:
74              *   <?plugin RedirectTo href="http://funky.com/a b \" c.htm" ?>
75              *
76              * Do we want some checking on href to avoid malicious
77              * uses of the plugin? Like stripping tags or hexcode.
78              */
79             $url = preg_replace('/%\d\d/','',strip_tags($href));
80             $thispage = $request->getPage();
81             if (! $thispage->get('locked')) {
82                 return $this->disabled(fmt(_("%s is only allowed in locked pages."),
83                                            _("Redirect to an external url")));
84             }
85         }
86         else if ($page) {
87             $url = $request->getURLtoSelf(array_merge(array('pagename' => $page,
88                                                             'redirectfrom' => $request->getArg('pagename')),
89                                                       SplitQueryArgs($args['args'])));
90         }
91         else {
92             return $this->error(fmt("%s or %s parameter missing",
93                                     "'href'", "'page'"));
94         }
95
96         if ($page == $request->getArg('pagename')) {
97             return $this->error(fmt("Recursive redirect to self: '%s'", $url));
98         }
99
100         if ($request->getArg('action') != 'browse')
101             return $this->disabled("(action != 'browse')");
102
103         $redirectfrom = $request->getArg('redirectfrom');
104         if ($redirectfrom !== false) {
105             if ($redirectfrom)
106                 return $this->disabled(_("Double redirect not allowed."));
107             else {
108                 // Got here by following the "Redirected from ..." link
109                 // on a browse page.
110                 return $this->disabled(_("Viewing redirecting page."));
111             }
112         }
113         
114         
115         return $request->redirect($url);
116     }
117 };
118
119 // $Log: not supported by cvs2svn $
120 // Revision 1.6  2003/01/18 22:01:44  carstenklapp
121 // Code cleanup:
122 // Reformatting & tabs to spaces;
123 // Added copyleft, getVersion, getDescription, rcs_id.
124 //
125
126 // For emacs users
127 // Local Variables:
128 // mode: php
129 // tab-width: 8
130 // c-basic-offset: 4
131 // c-hanging-comment-ender-p: nil
132 // indent-tabs-mode: nil
133 // End:
134 ?>