User:Jeblad/Qualified access/QualifiedAccess body.php

From MediaWiki.org
Jump to navigation Jump to search
<?php
/**
 * Extension to do qualified access to external services
 */

function efRunQualifiedAccess( $par ) {
	QualifiedAccess::run( $par );
}
 
class QualifiedAccess extends SpecialPage {

        function QualifiedAccess() {
                SpecialPage::SpecialPage("QualifiedAccess", '', true, 'efRunQualifiedAccess');
                wfLoadExtensionMessages('QualifiedAccess');
        }
 
        function run( $par ) {
		global $wgOut;
		global $wgRawHtml;
		global $wgRequest;
		$oldRawHtml = $wgRawHtml;
		$wgRawHtml = false;         // disable raw html if it's enabled as this could be XSS security risk
		//$this->setHeaders();

		global $efQualifiedAccessArgs;
		$efQualifiedAccessArgs = array();

		global $_REQUEST;
		$argkeys = array_keys($_REQUEST);

       		// find the position of "title" and count succeeding arguments until we find one that matches
       		// one of the patterns which belong to typical session cookie variables
       		$argTitle=-1; $argCount=0; $n=0;
       		foreach ($argkeys as $argKey) {
	       		if ($argKey=='title') $argTitle = $n;
	       		else if ($argTitle>=0) {
		       		if (preg_match('/(UserName|UserID|_session|Token)$/',$argKey)) break;
		       		++$argCount;
	       		}
	       		$n++;
       		}
       		$wikitext=''; $n=0; $i=-1;
       		foreach ($argkeys as $argKeyNr => $argKey) {
	       		$i++;
	       		if ($i<$argTitle) continue;
	       		if ($i==$argTitle) {
		       		$wikitext .= preg_replace(',^[^/]+/,','',$wgRequest->getText($argKey),1);
		   		//$wikitext = str_replace( ",", "|", $wikitext );
				$wikitext = str_replace( "_", " ", $wikitext );
				foreach (split(',', $wikitext) as $item) {
					$a = split('=', $item, 2);
					if (count($a)==1) $efQualifiedAccessArgs[] = $item;
					else $efQualifiedAccessArgs[$a[0]] = $a[1];
				}
		       		continue;
	       		}
	       		if (++$n > $argCount) break;
	       		$arg = $wgRequest->getText($argKey);
		       	if ($arg=='') {
			       	$arg = str_replace( "_", " ", $argKey );
				$efQualifiedAccessArgs[] = $arg;
		       	}
			else {
			       	$arg = str_replace( "_", " ", $arg );
				$efQualifiedAccessArgs[$argKey] = $arg;
			}
       		}

       		if ($wikitext=='' && $par!='') {
	       		// the first argument may contain parameters which are separated by comma
	       		// this is the case if [[Call,a=b]] syntax is used
			$wikitext = str_replace( "_", " ", $par );
			foreach (split(',', $wikitext) as $item) $efQualifiedAccessArgs[] = $item;
       		}
		foreach ($efQualifiedAccessArgs as $key => $value) {
			if (!preg_match('/^\d+$/',$key)) continue;
			if (preg_match('/^(doi|issn)\s*(\d.*)$/i',$value,$matches)) {
				$efQualifiedAccessArgs[$matches[1]] = $matches[2];
				$efQualifiedAccessArgs[$key] = '';
			}
		}
		if ($efQualifiedAccessArgs[0] == 'Spesial:QualifiedAccess') $efQualifiedAccessArgs[0] = '';
		$wgOut->addWikiText( wfMsg( 'qaccess-description' ) . wfMsg( 'qualifiedaccesspage' ) );
		$wgRawHtml = $oldRawHtml;
		$efQualifiedAccessArgs = null;
        }

}