]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Transclude.php
getName should not translate
[SourceForge/phpwiki.git] / lib / plugin / Transclude.php
1 <?php
2
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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /**
24  * Transclude:  Include an external web page within the body of a wiki page.
25  *
26  * Usage:
27  *  <<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 getDescription()
51     {
52         return _("Include an external web page within the body of a wiki page.");
53     }
54
55     function getDefaultArguments()
56     {
57         return array('src' => false, // the src url to include
58             'title' => _("Transcluded page"), // title of the iframe
59             'height' => 450, // height of the iframe
60             'quiet' => false // if set, iframe appears as normal content
61         );
62     }
63
64     function run($dbi, $argstr, &$request, $basepage)
65     {
66
67         $args = ($this->getArgs($argstr, $request));
68         extract($args);
69
70         if (!$src) {
71             return $this->error(fmt("%s parameter missing", "'src'"));
72         }
73         // Expand possible interwiki link for src
74         if (strstr($src, ':')
75             and (!strstr($src, '://'))
76                 and ($intermap = getInterwikiMap())
77                     and preg_match("/^" . $intermap->getRegexp() . ":/", $src)
78         ) {
79             $link = $intermap->link($src);
80             $src = $link->getAttr('href');
81         }
82
83         // FIXME: Better recursion detection.
84         // FIXME: Currently this doesnt work at all.
85         if ($src == $request->getURLtoSelf()) {
86             return $this->error(fmt("Recursive inclusion of url %s", $src));
87         }
88         if (!IsSafeURL($src)) {
89             return $this->error(_("Bad url in src: remove all of <, >, \""));
90         }
91
92         $params = array('title' => $title,
93             'src' => $src,
94             'width' => "100%",
95             'height' => $height,
96             'marginwidth' => 0,
97             'marginheight' => 0,
98             'class' => 'transclude',
99             "onload" => "adjust_iframe_height(this);");
100
101         $noframe_msg[] = fmt("See: %s", HTML::a(array('href' => $src), $src));
102
103         $noframe_msg = HTML::div(array('class' => 'transclusion'),
104             HTML::p(array(), $noframe_msg));
105
106         $iframe = HTML::iframe($params, $noframe_msg);
107
108         /* This doesn't work very well...  maybe because CSS screws up NS4 anyway...
109         $iframe = new HtmlElement('ilayer', array('src' => $src), $iframe);
110         */
111
112         if ($quiet) {
113             return HTML($this->_js(), $iframe);
114         } else {
115             return HTML(HTML::p(array('class' => 'transclusion-title'),
116                     fmt("Transcluded from %s", LinkURL($src))),
117                 $this->_js(), $iframe);
118         }
119     }
120
121     /**
122      * Produce our javascript.
123      *
124      * This is used to resize the iframe to fit the content.
125      * Currently it only works if the transcluded document comes
126      * from the same server as the wiki server.
127      *
128      * @access private
129      */
130     private function _js()
131     {
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               // Cannot 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 // Local Variables:
161 // mode: php
162 // tab-width: 8
163 // c-basic-offset: 4
164 // c-hanging-comment-ender-p: nil
165 // indent-tabs-mode: nil
166 // End: