]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Transclude.php
Activated Id substitution for Subversion
[SourceForge/phpwiki.git] / lib / plugin / Transclude.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /**
4  Copyright 1999,2000,2001,2002,2006 $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  * Transclude:  Include an external web page within the body of a wiki page.
25  *
26  * Usage:
27  *  <?plugin Transclude
28  *           src=http://www.internet-technology.de/fourwins_de.htm
29  *  ?>
30  *
31  * @author Geoffrey T. Dairiki
32  *
33  * @see http://www.cs.tut.fi/~jkorpela/html/iframe.html
34  *
35  * KNOWN ISSUES
36  *  Will only work if the browser supports <iframe>s (which is a recent,
37  *  but standard tag)
38  *
39  *  The auto-vertical resize javascript code only works if the transcluded
40  *  page comes from the PhpWiki server.  Otherwise (due to "tainting"
41  *  security checks in JavaScript) I can't figure out how to deduce the
42  *  height of the transcluded page via JavaScript... :-/
43  *
44  *  Sometimes the auto-vertical resize code doesn't seem to make the iframe
45  *  quite big enough --- the scroll bars remain.  Not sure why.
46  */
47 class WikiPlugin_Transclude
48 extends WikiPlugin
49 {
50     function getName() {
51         return _("Transclude");
52     }
53
54     function getDescription() {
55       return _("Include an external web page within the body of a wiki page.");
56     }
57
58     function getVersion() {
59         return preg_replace("/[Revision: $]/", '',
60                             "\$Revision: 1.10 $");
61     }
62
63     function getDefaultArguments() {
64         return array( 'src'     => false, // the src url to include
65                       'height'  => 450 // height of the iframe
66                     );
67     }
68
69     function run($dbi, $argstr, &$request, $basepage) {
70         global $WikiTheme;
71
72         $args = ($this->getArgs($argstr, $request));
73         extract($args);
74
75         if (!$src) {
76             return $this->error(fmt("%s parameter missing", "'src'"));
77         }
78         // Expand possible interwiki link for src
79         if (strstr($src,':')
80             and (!strstr($src,'://'))
81             and ($intermap = getInterwikiMap()) 
82             and preg_match("/^" . $intermap->getRegexp() . ":/", $src)) 
83         {
84             $link = $intermap->link($src);
85             $src = $link->getAttr('href');
86         }
87
88         // FIXME: Better recursion detection.
89         // FIXME: Currently this doesnt work at all.
90         if ($src == $request->getURLtoSelf() ) {
91             return $this->error(fmt("recursive inclusion of url %s", $src));
92         }
93         if (! IsSafeURL($src)) {
94             return $this->error(_("Bad url in src: remove all of <, >, \""));
95         }
96
97         $params = array('title' => _("Transcluded page"),
98                         'src' => $src,
99                         'width' => "100%",
100                         'height' => $height,
101                         'marginwidth' => 0,
102                         'marginheight' => 0,
103                         'class' => 'transclude',
104                         "onload" => "adjust_iframe_height(this);");
105
106         $noframe_msg[] = fmt("See: %s", HTML::a(array('href' => $src), $src));
107
108         $noframe_msg = HTML::div(array('class' => 'transclusion'),
109                                  HTML::p(array(), $noframe_msg));
110
111         $iframe = HTML::div(HTML::iframe($params, $noframe_msg));
112
113         /* This doesn't work very well...  maybe because CSS screws up NS4 anyway...
114         $iframe = new HtmlElement('ilayer', array('src' => $src), $iframe);
115         */
116
117         return HTML(HTML::p(array('class' => 'transclusion-title'),
118                             fmt("Transcluded from %s", LinkURL($src))),
119                     $this->_js(), $iframe);
120     }
121
122     /**
123      * Produce our javascript.
124      *
125      * This is used to resize the iframe to fit the content.
126      * Currently it only works if the transcluded document comes
127      * from the same server as the wiki server.
128      *
129      * @access private
130      */
131     function _js() {
132         static $seen = false;
133
134         if ($seen)
135             return '';
136         $seen = true;
137
138         return JavaScript('
139           function adjust_iframe_height(frame) {
140             var content = frame.contentDocument;
141             try {
142                 frame.height = content.height + 2 * frame.marginHeight;
143             }
144             catch (e) {
145               // Can not get content.height unless transcluded doc
146               // is from the same server...
147               return;
148             }
149           }
150
151           window.addEventListener("resize", function() {
152             f = this.document.body.getElementsByTagName("iframe");
153             for (var i = 0; i < f.length; i++)
154               adjust_iframe_height(f[i]);
155           }, false);
156           ');
157     }
158 };
159
160 // $Log: not supported by cvs2svn $
161 // Revision 1.9  2004/06/14 11:31:39  rurban
162 // renamed global $Theme to $WikiTheme (gforge nameclash)
163 // inherit PageList default options from PageList
164 //   default sortby=pagename
165 // use options in PageList_Selectable (limit, sortby, ...)
166 // added action revert, with button at action=diff
167 // added option regex to WikiAdminSearchReplace
168 //
169 // Revision 1.8  2004/02/17 12:11:36  rurban
170 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
171 //
172 // Revision 1.7  2003/02/27 22:47:27  dairiki
173 // New functions in HtmlElement:
174 //
175 // JavaScript($js)
176 //    Helper for generating javascript.
177 //
178 // IfJavaScript($if_content, $else_content)
179 //    Helper for generating
180 //       <script>document.write('...')</script><noscript>...</noscript>
181 //    constructs.
182 //
183 // Revision 1.6  2003/02/25 05:45:34  carstenklapp
184 // Added "See: " in front of url, so for browsers that do not support
185 // <iframe> at least there is an indication to the user that this
186 // plugin is actually doing something while at the same time without
187 // being (subjectively) too disruptive to page content.
188 //
189 // Revision 1.5  2003/02/24 14:34:44  carstenklapp
190 // Added iframe title (bobby.org accessibility guidelines).
191 // Simplified output for non-iframe and non-visual browsers (as suggested
192 // by http://www.uwosh.edu/programs/accessibility/tutorial.html).
193 //
194 // Revision 1.4  2003/01/18 22:08:01  carstenklapp
195 // Code cleanup:
196 // Reformatting & tabs to spaces;
197 // Added copyleft, getVersion, getDescription, rcs_id.
198 //
199
200 // (c-file-style: "gnu")
201 // Local Variables:
202 // mode: php
203 // tab-width: 8
204 // c-basic-offset: 4
205 // c-hanging-comment-ender-p: nil
206 // indent-tabs-mode: nil
207 // End:
208 ?>