]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RedirectTo.php
Activated Revision substitution for Subversion
[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                       // 'type' => 'Temp' // or 'Permanent' // so far ignored
62                       'page' => false,
63                       );
64     }
65
66     function run($dbi, $argstr, &$request, $basepage) {
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 = WikiURL($page,
88                            array('redirectfrom' => $request->getArg('pagename')),
89                            'abs_path');
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         return $request->redirect($url);
115     }
116 };
117
118 // $Log: not supported by cvs2svn $
119 // Revision 1.12  2004/02/03 09:45:39  rurban
120 // LDAP cleanup, start of new Pref classes
121 //
122 // Revision 1.11  2003/11/22 17:54:50  carstenklapp
123 // Minor internal change: Removed redundant call to gettext within
124 // fmt(). (locale make: RedirectTo.php:81: warning: keyword nested in
125 // keyword arg)
126 //
127 // Revision 1.10  2003/02/24 00:40:09  carstenklapp
128 // PHP's closing tag \?\> within // cvs log comments caused the trailing comments to display as literal text on the PluginManager page.
129 //
130 // Revision 1.9  2003/02/21 22:59:00  dairiki
131 // Add new argument $basepage to WikiPlugin::run() and WikiPluginLoader::expandPI().
132 // Plugins need to know what page they were invoked from so that they can handle
133 // relative page links (like [/Subpage]) correctly.  ($request->getArg('pagename')
134 // is not always the right page to use --- think included pages...)
135 //
136 // Many plugins don't need the $basepage, in which case, I think they can just ignore
137 // the extra argument.  (I don't think PHP will generate any warnings.)
138 //
139 //
140 // Also: deleted <?plugin-head? > code.  It's not needed any more, now that
141 // we always cache output.
142 //
143 // The FrameInclude plugin seems broken now, though I'm not convinced it's
144 // my fault.  If it is, sorry...   (I'll try to look at it a bit more
145 // within a few days, to see if I can figure out the problem.)
146 //
147 // Revision 1.8  2003/02/16 19:49:18  dairiki
148 // When redirecting to a page, use an absolute URL.
149 // This fixes a bug when redirecting from a sub-page (since,
150 // in that case the redirect happens before the <base> element gets
151 // sent.)
152 //
153 // Revision 1.7  2003/02/15 23:32:56  dairiki
154 // Usability improvements for the RedirectTo plugin.
155 //
156 // (Mostly this applies when using RedirectTo with a page=OtherPage
157 // argument to redirect to another page in the same wiki.)
158 //
159 // (Most of these ideas are stolen verbatim from UseModWiki.)
160 //
161 //  o Multiple redirects (PageOne -> PageTwo -> PageThree) not allowed.
162 //
163 //  o Redirects are not activated except when action == 'browse'.
164 //
165 //  o When redirections are disabled, (hopefully understandable)
166 //    diagnostics are displayed.
167 //
168 //  o A link to the redirecting page is displayed after the title
169 //    of the target page.  If the user follows this link, redirects
170 //    are disabled.  This allows for easy editing of the redirecting
171 //    page.
172 //
173 // FIXME: Stylesheets, and perhaps templates other than the defaults
174 // will probably have to be updated before this works well in other
175 // styles and/or themes.
176 //
177 // Revision 1.6  2003/01/18 22:01:44  carstenklapp
178 // Code cleanup:
179 // Reformatting & tabs to spaces;
180 // Added copyleft, getVersion, getDescription, rcs_id.
181 //
182
183 // For emacs users
184 // Local Variables:
185 // mode: php
186 // tab-width: 8
187 // c-basic-offset: 4
188 // c-hanging-comment-ender-p: nil
189 // indent-tabs-mode: nil
190 // End:
191 ?>