]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Transclude.php
New Transclude plugin.
[SourceForge/phpwiki.git] / lib / plugin / Transclude.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Transclude.php,v 1.1 2002-09-17 02:26:20 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
23 class WikiPlugin_Transclude
24 extends WikiPlugin
25 {
26     function getName() {
27         return _("Transclude");
28     }
29
30     function getDescription() {
31       return _("Include an external web page within the body of a wiki page.");
32     }
33
34     function getDefaultArguments() {
35         return array( 'src'     => false, // the src url to include
36                       'height'  => 450 // height of the iframe
37                     );
38     }
39
40     function run($dbi, $argstr, $request) {
41         global $Theme;
42
43         $args = ($this->getArgs($argstr, $request));
44         extract($args);
45
46         if (!$src) {
47             return $this->error(fmt("%s parameter missing", "'src'"));
48         }
49         // FIXME: Better recursion detection.
50         // FIXME: Currently this doesnt work at all.
51         if ($src == $request->getURLtoSelf() ) {
52             return $this->error(fmt("recursive inclusion of url %s", $src));
53         }
54
55         if (! IsSafeURL($src)) {
56             return $this->error(_("Bad url in src: remove all of <, >, \""));
57         }
58
59         $params = array('src' => $src,
60                         'width' => "100%",
61                         'height' => $height,
62                         'marginwidth' => 0,
63                         'marginheight' => 0,
64                         'scrolling' => 'no',
65                         'class' => 'transclude',
66                         "onload" => "adjust_iframe_height(this);");
67
68         $noframe_msg[]
69             = _("Cannot transclude document since your browser does not support <iframe>s.)");
70         $noframe_msg[] = '  ';
71         $noframe_msg[] = fmt("Click %s to view the transcluded page",
72                              HTML::a(array('href' => $src), _("here")));
73         
74         $noframe_msg = HTML::div(array('class' => 'transclusion'),
75                                  HTML::p(array(), $noframe_msg));
76
77         $iframe = HTML::div(HTML::iframe($params, $noframe_msg));
78
79         /* This doesn't work very well...  maybe because CSS screws up NS4 anyway...
80         $iframe = new HtmlElement('ilayer', array('src' => $src), $iframe);
81         */
82         
83         return HTML(HTML::p(array('class' => 'transclusion-title'),
84                             fmt("Transcluded from %s", LinkURL($src))),
85                     $this->_js(), $iframe);
86     }
87
88     /**
89      * Produce our javascript.
90      *
91      * This is used to resize the iframe to fit the content.
92      * Currently it only works if the transcluded document comes
93      * from the same server as the wiki server.
94      *
95      * @access private
96      */
97     function _js() {
98         static $seen = false;
99
100         if ($seen)
101             return '';
102         $seen = true;
103         
104         $script = '
105           function adjust_iframe_height(frame) {
106             var content = frame.contentDocument;
107             try {
108               frame.height = content.height + 2 * frame.marginHeight;
109             }
110             catch (e) {
111               // Can not get content.height unless transcluded doc
112               // is from the same server...
113               return;
114             }
115             frame.scrolling = "no"; // This does nothing in Galeon
116           }';
117
118         return  HTML::script(array('language' => 'JavaScript',
119                                    'type'     => 'text/javascript'),
120                              new RawXml("<!-- //\n$script\n// -->"));
121     }
122 };
123
124
125 // (c-file-style: "gnu")
126 // Local Variables:
127 // mode: php
128 // tab-width: 8
129 // c-basic-offset: 4
130 // c-hanging-comment-ender-p: nil
131 // indent-tabs-mode: nil
132 // End:   
133 ?>