Iframe

Aus TransArte

Wechseln zu: Navigation, Suche

you shouldn't add an iframe in a mediawiki page. But when there is no other way here is a small extension. One reason may be to include some dynamic stuff like an email programm horde etc.

Inhaltsverzeichnis

[bearbeiten] Extension for 1.8

    • I want to keep an iframe of a discussionpage in the home page, to see entries, and then a link to write my own entries. Since I'm no way near proficient, I gathered to keep the frame of the printable version (no already existing frames) would be most convenient, i.e. <iframe src="wikidir/index.php?title=Diskussion:Forum&printable=yes" scrolling="yes" width="300px" height="300px" frameborder="0"></iframe>. however, what happens is that the printable page opens over the whole window. What to do?

The below program crashed and it appears it's because the hook takes three parameters, not one. Here is my version. It's mostly the same, but the usage has changed. Now, the IFRAME works just like it does in HTML. This isn't 100% secure, as I'm sure we could clean the parameters before assigning them. Use at your own risk!

Example: <iframe src="http://www.heise.de" width=100% height=adjust name=myframe></iframe>

Also, a new "feature" is added. This script checks for the global variable $iframe_allowed_hosts. If it exists, then only hostnames (ie, www.google.com) in that array are allowed. An simple message is returned if the user attempts to use an unauthorized host. Don't count on this being foolproof or completely secure, but it's something! In your LocalSettings.php file, set the following, for example:

$iframe_allowed_hosts = array("www.google.com", "www.mysite.com");
<?php
$wgExtensionFunctions[] = "iframeS";

function iframeS(){
        global $wgParser ;
        $wgParser->setHook ( "iframe" , 'iframeR' ) ;
}

function iframeR( $input, $param, &$parser ){
        global $iframe_allowed_hosts;
        $width = (isset($param["width"]))? $param["width"] : 200;
        $height = (isset($param["height"]))? $param["height"] : 200;
        $scrolling = (isset($param["scrolling"]))? $param["scrolling"] : "yes";
        $url = (isset($param["src"]))? $param["src"] : "";
        $url = "http://". str_replace("http://", "", $url);
        $name = (isset($param["name"]))? "name='${param['name']}' " : "";
        $style = (isset($param['style'])) ? "style='${param['style']}' " : 'style="border:solid 0px; margin:none;"';
        $frameborder = (isset($param['frameborder'])) ? $param['frameborder'] : '0';

        // Attempt to parse the URL
        $parsedurl = parse_url($url);   // returns false if the url is malformed
        if (!$parsedurl) { return "[ Invalid IFRAME url ]"; }
        if (isset($iframe_allowed_hosts)) {
                if (!in_array(strtolower($parsedurl['host']),$iframe_allowed_hosts)) {
                        return "[ ${parsedurl['host']} is not an authorized iframe site ]";
                }
        }

//here you may add some other variables, but be very very careful!

        $script = ""; $onload ="";
// if you do not want autoresize script, just remove following if statement
if(strtoupper($height)=="ADJUST"){
        $onload = "onload='adjustHeight(this)'";
        $script = "<script>
        function adjustHeight(obj){
                var doc = window.frames[obj.name].document;
                obj.style.height = (doc.body.offsetHeight+15)+'px';
                doc.body.style.borderWidth = '0px';
        }
        </script>
        ";
        $height=400;
       }

        $ret = "$script<"."iframe $onload frameborder='$frameborder' $name "; //remove the space between < and iframe
        $ret .= "width='$width' height='$height' scrolling='$scrolling' src='$url' $style></iframe >" ;
        return $ret ;
}
?>

[bearbeiten] extension

first the extension:

save the following code as iframe.php in the extension folder of mediawiki

 warning .. only the tag <?php .... php?> works for me... as it is written
 below, the php crashes the system (Linux/Apache/php) ostlund@fy.chalmers.se
<?php
$wgExtensionFunctions[] = "iframeS";

function iframeS(){
	global $wgParser ;
	$wgParser->setHook ( "iframe" , 'iframeR' ) ;
}

function iframeR( $data ){
	$param = array();
	$info = explode("\n", $data);
	if(is_array($info)){
		foreach($info as $lin){
			$line = explode("=",$lin, 2);
			if(count($line)==2){
				$param[trim($line[0])] = trim($line[1]);
			}
		}
	}
	$width = (isset($param["width"]))? $param["width"] : 200;
	$height = (isset($param["height"]))? $param["height"] : 200;
	$url = (isset($param["url"]))? $param["url"] : "";
	$url = "http://". str_replace("http://", "", $url);
	$name = (isset($param["name"]))? $param["name"] : "";
//here you may add some other variables, but be very very careful!

	$script = ""; $onload ="";
// if you do not want autoresize script, just remove following if statement
if(strtoupper($height)=="ADJUST"){
	$onload = "onload='adjustHeight(this)'";
	$script = "<script>
	function adjustHeight(obj){
		var doc = window.frames[obj.name].document;
		obj.style.height = (doc.body.offsetHeight+15)+'px';
		doc.body.style.borderWidth = '0px';
	}
	</script>
	";
	$height=400;
       }
       if (empty($iFrameCount)) //iFrameCount is never really used??? 
         $iFrameCount = ;
	$ret  = "$script<"."iframe $onload id='$name$iFrameCount' frameborder='0' name='$name' "; //remove the space between < and iframe
	$ret .= "width='$width' height='$height' src='$url' style='border:solid 0px; margin:none;'></iframe >" ;
	return $ret ;
}
?>

then you have to add the line:

include("extensions/iframe.php");

in LocalSettings.php

[bearbeiten] use it

This formatting is for the "old" version of the script and is kept for posterity's sake.

to use a iframe in a mediawiki page just add (of course you have to modify the values)

<iframe>
url=www.heise.de
width=100%
height=adjust
name=myframe
</iframe>

with height=adjust the iframe will be adjusted to the body height of the content. But this only works on local sites. External sites does not allow javacript. In this case use height=400 or so. And there may be some browsers without onload !!!

i kept the code farily simple, so you can adopt it to your needs.

[bearbeiten] Question

Could you integrate the following code so that height readjustment works for external sites too?

There is a security issue, that dissalow crossdomain scripting. I tried the script and it does no more or less than my part and it does not work. Maybe there is a way.

<script type="text/javascript">
/***********************************************
* IFrame SSI script II- © Dynamic Drive DHTML code library (h t t p:/ / www.dynamicdrive.com)
* Visit DynamicDrive.com for hundreds of original DHTML scripts
* This notice must stay intact for legal use
***********************************************/
//Input the IDs of the IFRAMES you wish to dynamically resize to match its content height:
//Separate each ID with a comma. Examples: ["myframe1", "myframe2"] or ["myframe"] or [] for none:
var iframeids=["phpBBframe"]
//Should script hide iframe from browsers that don't support this script (non IE5+/NS6+ browsers. Recommended):
var iframehide="no"
var  getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
var FFextraHeight=getFFVersion>=0.1? 16 : 4 //extra height in px to add to iframe in FireFox 1.0+ browsers
function resizeCaller() {
var dyniframe=new Array()
for (i=0; i<iframeids.length; i++){
if (document.getElementById)
resizeIframe(iframeids[i])
//reveal iframe for lower end browsers? (see var above):
if ((document.all || document.getElementById) && iframehide=="yes"){
var tempobj=document.all? document.all[iframeids[i]] : document.getElementById(iframeids[i])
tempobj.style.display="block"
}
}
}
function resizeIframe(frameid){
var currentfr=document.getElementById(frameid)
if (currentfr && !window.opera){
currentfr.style.display="block"
if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) //ns6 syntax
currentfr.height = currentfr.contentDocument.body.offsetHeight+FFextraHeight; 
else if (currentfr.Document && currentfr.Document.body.scrollHeight) //ie5+ syntax
currentfr.height = currentfr.Document.body.scrollHeight;
if (currentfr.addEventListener)
currentfr.addEventListener("load", readjustIframe, false)
else if (currentfr.attachEvent){
currentfr.detachEvent("onload", readjustIframe) // Bug fix line
currentfr.attachEvent("onload", readjustIframe)
}
}
}
function readjustIframe(loadevt) {
var crossevt=(window.event)? event : loadevt
var iframeroot=(crossevt.currentTarget)? crossevt.currentTarget : crossevt.srcElement
if (iframeroot)
resizeIframe(iframeroot.id);
}
function loadintoIframe(iframeid, url){
if (document.getElementById)
document.getElementById(iframeid).src=url
}
if (window.addEventListener)
window.addEventListener("load", resizeCaller, false)
else if (window.attachEvent)
window.attachEvent("onload", resizeCaller)
else
window.onload=resizeCaller
</script>

[bearbeiten] Anomalies

Anyone who sees the php code of this extension display at the top of every page, try this: Remove the Space between the < and ?php, and the corresponding space between the ? and the > at the end of the script. Also note the comment in the php script to remove the "." in one of the lines.

Because of the security issues with this extension, we have actually modified it to only allow particular sites of interest to our users. We do not offer the general iframe functionality for any site, only trusted ones. This might sufficiently address security issues for other prospective users of this script.

I couldn't get the current script running on the first try so I went to the former one by TransWiki. Maybe that was just sloppy copy and pasting, I dunno. If someone else runs into the same try the most recent Transwiki copy of the code. Aloha dudes from Hawaii... MakThorpe 3. Mär 2006 18:21 (CET)

Persönliche Werkzeuge