]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Transclude.php
Code cleanup:
[SourceForge/phpwiki.git] / lib / plugin / Transclude.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Transclude.php,v 1.4 2003-01-18 22:08:01 carstenklapp Exp $');
3 /**
4  Copyright 1999, 2000, 2001, 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  * 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.4 $");
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) {
70         global $Theme;
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         // FIXME: Better recursion detection.
79         // FIXME: Currently this doesnt work at all.
80         if ($src == $request->getURLtoSelf() ) {
81             return $this->error(fmt("recursive inclusion of url %s", $src));
82         }
83
84         if (! IsSafeURL($src)) {
85             return $this->error(_("Bad url in src: remove all of <, >, \""));
86         }
87
88         $params = array('src' => $src,
89                         'width' => "100%",
90                         'height' => $height,
91                         'marginwidth' => 0,
92                         'marginheight' => 0,
93                         'class' => 'transclude',
94                         "onload" => "adjust_iframe_height(this);");
95
96         $noframe_msg[]
97             = _("Cannot transclude document since your browser does not support <iframe>s.)");
98         $noframe_msg[] = '  ';
99         $noframe_msg[] = fmt("Click %s to view the transcluded page",
100                              HTML::a(array('href' => $src), _("here")));
101
102         $noframe_msg = HTML::div(array('class' => 'transclusion'),
103                                  HTML::p(array(), $noframe_msg));
104
105         $iframe = HTML::div(HTML::iframe($params, $noframe_msg));
106
107         /* This doesn't work very well...  maybe because CSS screws up NS4 anyway...
108         $iframe = new HtmlElement('ilayer', array('src' => $src), $iframe);
109         */
110
111         return HTML(HTML::p(array('class' => 'transclusion-title'),
112                             fmt("Transcluded from %s", LinkURL($src))),
113                     $this->_js(), $iframe);
114     }
115
116     /**
117      * Produce our javascript.
118      *
119      * This is used to resize the iframe to fit the content.
120      * Currently it only works if the transcluded document comes
121      * from the same server as the wiki server.
122      *
123      * @access private
124      */
125     function _js() {
126         static $seen = false;
127
128         if ($seen)
129             return '';
130         $seen = true;
131
132         $script = '
133           function adjust_iframe_height(frame) {
134             var content = frame.contentDocument;
135             try {
136                 frame.height = content.height + 2 * frame.marginHeight;
137             }
138             catch (e) {
139               // Can not get content.height unless transcluded doc
140               // is from the same server...
141               return;
142             }
143           }
144
145           window.addEventListener("resize", function() {
146             f = this.document.body.getElementsByTagName("iframe");
147             for (var i = 0; i < f.length; i++)
148               adjust_iframe_height(f[i]);
149           }, false);
150           ';
151
152         return HTML::script(array('language' => 'JavaScript',
153                                   'type'     => 'text/javascript'),
154                             new RawXml("<!-- //\n$script\n// -->"));
155     }
156 };
157
158 // $Log: not supported by cvs2svn $
159
160 // (c-file-style: "gnu")
161 // Local Variables:
162 // mode: php
163 // tab-width: 8
164 // c-basic-offset: 4
165 // c-hanging-comment-ender-p: nil
166 // indent-tabs-mode: nil
167 // End:
168 ?>