]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Transclude.php
Wups. Removed a bit of cruft...
[SourceForge/phpwiki.git] / lib / plugin / Transclude.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Transclude.php,v 1.2 2002-09-17 02:34:18 dairiki Exp $');
3 /**
4  * Transclude:  Include an external web page within the body of a wiki page.
5  * 
6  * Usage:   
7  *  <?plugin Transclude src=http://www.internet-technology.de/fourwins_de.htm ?>
8  *
9  * @author Geoffrey T. Dairiki
10  *
11  * @see http://www.cs.tut.fi/~jkorpela/html/iframe.html
12  *
13  * KNOWN ISSUES
14  *  Will only work if the browser supports <iframe>s (which is a recent,
15  *  but standard tag)
16  *
17  *  The auto-vertical resize javascript code only works if the transcluded
18  *  page comes from the PhpWiki server.  Otherwise (due to "tainting"
19  *  security checks in JavaScript) I can't figure out how to deduce the
20  *  height of the transcluded page via JavaScript... :-/
21  *
22  *  Sometimes the auto-vertical resize code doesn't seem to make the iframe
23  *  quite big enough --- the scroll bars remain.  Not sure why.
24  */
25
26 class WikiPlugin_Transclude
27 extends WikiPlugin
28 {
29     function getName() {
30         return _("Transclude");
31     }
32
33     function getDescription() {
34       return _("Include an external web page within the body of a wiki page.");
35     }
36
37     function getDefaultArguments() {
38         return array( 'src'     => false, // the src url to include
39                       'height'  => 450 // height of the iframe
40                     );
41     }
42
43     function run($dbi, $argstr, $request) {
44         global $Theme;
45
46         $args = ($this->getArgs($argstr, $request));
47         extract($args);
48
49         if (!$src) {
50             return $this->error(fmt("%s parameter missing", "'src'"));
51         }
52         // FIXME: Better recursion detection.
53         // FIXME: Currently this doesnt work at all.
54         if ($src == $request->getURLtoSelf() ) {
55             return $this->error(fmt("recursive inclusion of url %s", $src));
56         }
57
58         if (! IsSafeURL($src)) {
59             return $this->error(_("Bad url in src: remove all of <, >, \""));
60         }
61
62         $params = array('src' => $src,
63                         'width' => "100%",
64                         'height' => $height,
65                         'marginwidth' => 0,
66                         'marginheight' => 0,
67                         'class' => 'transclude',
68                         "onload" => "adjust_iframe_height(this);");
69
70         $noframe_msg[]
71             = _("Cannot transclude document since your browser does not support <iframe>s.)");
72         $noframe_msg[] = '  ';
73         $noframe_msg[] = fmt("Click %s to view the transcluded page",
74                              HTML::a(array('href' => $src), _("here")));
75         
76         $noframe_msg = HTML::div(array('class' => 'transclusion'),
77                                  HTML::p(array(), $noframe_msg));
78
79         $iframe = HTML::div(HTML::iframe($params, $noframe_msg));
80
81         /* This doesn't work very well...  maybe because CSS screws up NS4 anyway...
82         $iframe = new HtmlElement('ilayer', array('src' => $src), $iframe);
83         */
84         
85         return HTML(HTML::p(array('class' => 'transclusion-title'),
86                             fmt("Transcluded from %s", LinkURL($src))),
87                     $this->_js(), $iframe);
88     }
89
90     /**
91      * Produce our javascript.
92      *
93      * This is used to resize the iframe to fit the content.
94      * Currently it only works if the transcluded document comes
95      * from the same server as the wiki server.
96      *
97      * @access private
98      */
99     function _js() {
100         static $seen = false;
101
102         if ($seen)
103             return '';
104         $seen = true;
105         
106         $script = '
107           function adjust_iframe_height(frame) {
108             var content = frame.contentDocument;
109             try {
110               frame.height = content.height + 2 * frame.marginHeight;
111             }
112             catch (e) {
113               // Can not get content.height unless transcluded doc
114               // is from the same server...
115               return;
116             }
117           }';
118
119         return  HTML::script(array('language' => 'JavaScript',
120                                    'type'     => 'text/javascript'),
121                              new RawXml("<!-- //\n$script\n// -->"));
122     }
123 };
124
125
126 // (c-file-style: "gnu")
127 // Local Variables:
128 // mode: php
129 // tab-width: 8
130 // c-basic-offset: 4
131 // c-hanging-comment-ender-p: nil
132 // indent-tabs-mode: nil
133 // End:   
134 ?>