Extension:RawCodeFile

The RawCodeFile extension allows to embed a source code in a downloadable form, between two

bugs
BUG: probably allows php remote injection!!!

code
<?php if(!defined('MEDIAWIKI')) { echo("This is an extension to the MediaWiki package and cannot be run standalone.\n"); die(-1); } else { //Avoid unstubbing $wgParser on setHook too early on modern (1.12+) MW versions, as per r35980 if(defined('MW_SUPPORTS_PARSERFIRSTCALLINIT')) { $wgHooks['ParserFirstCallInit'][]='efRawFile_Setup'; $wgHooks['ParserFirstCallInit'][]='efCodeExtensionInit'; }   else { // Otherwise do things the old fashioned way $wgExtensionFunctions[]='efRawFile_Setup'; $wgExtensionFunctions[]='efCodeExtensionInit'; }   $wgHooks['LanguageGetMagic'][]='efRawFile_Magic'; $wgHooks['RawPageViewBeforeOutput'][]='fnRawFile_Strip'; $wgExtensionCredits['parserhook'][]=array('name'=>'RawCodeFile','path'=>__FILE__,'author'=>'Philippe Teuwen, Michael Peeters, Paul Grinberg, Tuxun','version'=>'0.1a','url'=>'http://www.mediawiki.org/wiki/Extension:RawCodeFile','description'=>'Allows syntax highlighting using GeSHi '.'Downloads a RAW copy of data in a file '.'Useful e.g. to download a script or a patch '.'It also allows what is called Literate Programming'); function efRawFile_Setup { global $wgParser; $wgParser->setFunctionHook('file','efRawFile_Render'); $wgParser->setFunctionHook('filelink','efRawFile_Render'); $wgParser->setFunctionHook('fileanchor','efRawFile_Empty'); $wgParser->setHook('file','efRawFile_FileTagRender'); $wgParser->setFunctionHook('code','efRawFile_Render'); return true; }   function efRawFile_Magic(&$magicWords,$langCode) { $magicWords['file']=array(0,'file'); $magicWords['code']=array(0,'code'); $magicWords['filelink']=array(0,'filelink'); $magicWords['fileanchor']=array(0,'fileanchor'); return true; }   function efRawFile_Render(&$parser,$filename=,$titleText=) { if($titleText=='') $title=$parser->mTitle; else $title=Title::newFromText($titleText); //Don't expand templates or we'll lose our anchors return $title->getFullURL('action=raw&anchor='.urlencode($filename)); }   function efRawFile_Empty(&$parser,$filename='') { return ''; }   function efRawFile_FileTagRender($input,$args,$parser,$frame='') { $title=$parser->mTitle; if(isset($args['title'])) { if($args['title']!='') { $title=Title::newFromText($parser->recursiveTagParse($args['name'],$frame)); //$title = $args['title']; }       }        //cf l304 //$title = Title::newFromText($parser->recursiveTagParse( $args['name'], $frame )); //We expand templates, so tag cannot be mixed with anchors $link=$title->getFullURL('action=raw&templates=expand'); if($args['name']!='') $link.='&name='.urlencode($parser->recursiveTagParse($args['name'],$frame)); if($args['anchor']!='') $link.='&anchor='.urlencode($parser->recursiveTagParse($args['anchor'],$frame)); if($args['tag']!='') $link.='&tag='.urlencode($parser->recursiveTagParse($args['tag'],$frame)); return $parser->recursiveTagParse("[$link ".$args['name']."]",$frame); }   function fnRawFile_Strip_Error($msg,$out,&$text) { $text=$msg; if($out!='') $text.="\nCandidate match: $out"; return true; }   function fnRawFile_Strip(&$rawPage,&$text) { $filename=$_GET['name']; $anchor=$_GET['anchor']; // for backward compatibility, accept also URLs with parameter 'file' if($anchor=='') $anchor=$_GET['file']; $tag=$_GET['tag']; // Either anchor or name must be specified if($filename=='') $filename=$anchor; if($filename=='') return true; // Uncomment the following line to avoid output buffering and gzipping: // wfResetOutputBuffers; header("Content-disposition: attachment;filename={$filename}"); header("Content-type: application/octet-stream"); header("Content-Transfer-Encoding: binary"); header("Expires: 0"); header("Pragma: no-cache"); header("Cache-Control: no-store"); $maskedtext=preg_replace_callback('! .*? !s',function($m) {           return ereg_replace(".","X",$m[0]);        },$text); //echo $maskedtext; if(($anchor!='')&&preg_match_all('/|(<[^>]+ class *= *"([^"]*\w)?'.$anchor.'(\w[^"]*)?"[^>]*>)/i',$maskedtext,$matches,PREG_OFFSET_CAPTURE)) $offsets=$matches[0]; elseif(preg_match_all('//i',$maskedtext,$matches,PREG_OFFSET_CAPTURE)) $offsets=array($matches[0][0]); elseif(preg_match_all('/]*)? name *= *"'.$filename.'"[^>]*>/i',$maskedtext,$matches,PREG_OFFSET_CAPTURE)) $offsets=array($matches[0][0]); elseif(preg_match_all('/

parameters
please only use double quotes as this: parametername="parametervalue".

Single quotes WILL NOT WORK AS EXPECTED!


 * lang="cpp","java","perl","html4" etc
 * name
 * line

History
When I tested Mw1.25a, I searched how to get downloads link on embeded codes, to simply share some snippet of it like Arduino sketch, or shell scripts.

I originally found Extension:Code which did syntax highlighting between two "code" tags, and Extension:RawFile which allowed to create the download link on two "file" tags.

Extension:Code was (and still is) using Geshi 1.0.8 to do syntax highlighting, and RawFile used "MagicWord" system to parse add the link.