]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Transclude.php
Add private keyword for functions
[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 getName()
51     {
52         return _("Transclude");
53     }
54
55     function getDescription()
56     {
57         return _("Include an external web page within the body of a wiki page.");
58     }
59
60     function getDefaultArguments()
61     {
62         return array('src' => false, // the src url to include
63             'title' => _("Transcluded page"), // title of the iframe
64             'height' => 450, // height of the iframe
65             'quiet' => false // if set, iframe appears as normal content
66         );
67     }
68
69     function run($dbi, $argstr, &$request, $basepage)
70     {
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' => $title,
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::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         if ($quiet) {
118             return HTML($this->_js(), $iframe);
119         } else {
120             return HTML(HTML::p(array('class' => 'transclusion-title'),
121                     fmt("Transcluded from %s", LinkURL($src))),
122                 $this->_js(), $iframe);
123         }
124     }
125
126     /**
127      * Produce our javascript.
128      *
129      * This is used to resize the iframe to fit the content.
130      * Currently it only works if the transcluded document comes
131      * from the same server as the wiki server.
132      *
133      * @access private
134      */
135     private function _js()
136     {
137         static $seen = false;
138
139         if ($seen)
140             return '';
141         $seen = true;
142
143         return JavaScript('
144           function adjust_iframe_height(frame) {
145             var content = frame.contentDocument;
146             try {
147                 frame.height = content.height + 2 * frame.marginHeight;
148             }
149             catch (e) {
150               // Cannot get content.height unless transcluded doc
151               // is from the same server...
152               return;
153             }
154           }
155
156           window.addEventListener("resize", function() {
157             f = this.document.body.getElementsByTagName("iframe");
158             for (var i = 0; i < f.length; i++)
159               adjust_iframe_height(f[i]);
160           }, false);
161           ');
162     }
163 }
164
165 // Local Variables:
166 // mode: php
167 // tab-width: 8
168 // c-basic-offset: 4
169 // c-hanging-comment-ender-p: nil
170 // indent-tabs-mode: nil
171 // End: