]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RedirectTo.php
PHP's closing tag \?\> within // cvs log comments caused the trailing comments to...
[SourceForge/phpwiki.git] / lib / plugin / RedirectTo.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RedirectTo.php,v 1.10 2003-02-24 00:40:09 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:
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  *
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.10 $");
56     }
57
58     function getDefaultArguments() {
59         return array( 'href' => '',
60                       // 'type' => 'Temp' // or 'Permanent' // so far ignored
61                       'page' => false,
62                       );
63     }
64
65     function run($dbi, $argstr, $request) {
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(fmt(_("%s is only allowed in locked pages."),
82                                            _("Redirect to an external url")));
83             }
84         }
85         else if ($page) {
86             $url = WikiURL($page,
87                            array('redirectfrom' => $request->getArg('pagename')),
88                            'abs_path');
89         }
90         else {
91             return $this->error(fmt("%s or %s parameter missing",
92                                     "'href'", "'page'"));
93         }
94
95         if ($page == $request->getArg('pagename')) {
96             return $this->error(fmt("Recursive redirect to self: '%s'", $url));
97         }
98
99         if ($request->getArg('action') != 'browse')
100             return $this->disabled("(action != 'browse')");
101
102         $redirectfrom = $request->getArg('redirectfrom');
103         if ($redirectfrom !== false) {
104             if ($redirectfrom)
105                 return $this->disabled(_("Double redirect not allowed."));
106             else {
107                 // Got here by following the "Redirected from ..." link
108                 // on a browse page.
109                 return $this->disabled(_("Viewing redirecting page."));
110             }
111         }
112         
113         return $request->redirect($url);
114     }
115 };
116
117 // $Log: not supported by cvs2svn $
118 // Revision 1.9  2003/02/21 22:59:00  dairiki
119 // Add new argument $basepage to WikiPlugin::run() and WikiPluginLoader::expandPI().
120 // Plugins need to know what page they were invoked from so that they can handle
121 // relative page links (like [/Subpage]) correctly.  ($request->getArg('pagename')
122 // is not always the right page to use --- think included pages...)
123 //
124 // Many plugins don't need the $basepage, in which case, I think they can just ignore
125 // the extra argument.  (I don't think PHP will generate any warnings.)
126 //
127 //
128 // Also: deleted <?plugin-head? > code.  It's not needed any more, now that
129 // we always cache output.
130 //
131 // The FrameInclude plugin seems broken now, though I'm not convinced it's
132 // my fault.  If it is, sorry...   (I'll try to look at it a bit more
133 // within a few days, to see if I can figure out the problem.)
134 //
135 // Revision 1.8  2003/02/16 19:49:18  dairiki
136 // When redirecting to a page, use an absolute URL.
137 // This fixes a bug when redirecting from a sub-page (since,
138 // in that case the redirect happens before the <base> element gets
139 // sent.)
140 //
141 // Revision 1.7  2003/02/15 23:32:56  dairiki
142 // Usability improvements for the RedirectTo plugin.
143 //
144 // (Mostly this applies when using RedirectTo with a page=OtherPage
145 // argument to redirect to another page in the same wiki.)
146 //
147 // (Most of these ideas are stolen verbatim from UseModWiki.)
148 //
149 //  o Multiple redirects (PageOne -> PageTwo -> PageThree) not allowed.
150 //
151 //  o Redirects are not activated except when action == 'browse'.
152 //
153 //  o When redirections are disabled, (hopefully understandable)
154 //    diagnostics are displayed.
155 //
156 //  o A link to the redirecting page is displayed after the title
157 //    of the target page.  If the user follows this link, redirects
158 //    are disabled.  This allows for easy editing of the redirecting
159 //    page.
160 //
161 // FIXME: Stylesheets, and perhaps templates other than the defaults
162 // will probably have to be updated before this works well in other
163 // styles and/or themes.
164 //
165 // Revision 1.6  2003/01/18 22:01:44  carstenklapp
166 // Code cleanup:
167 // Reformatting & tabs to spaces;
168 // Added copyleft, getVersion, getDescription, rcs_id.
169 //
170
171 // For emacs users
172 // Local Variables:
173 // mode: php
174 // tab-width: 8
175 // c-basic-offset: 4
176 // c-hanging-comment-ender-p: nil
177 // indent-tabs-mode: nil
178 // End:
179 ?>