<?php
/* Tiki-Wiki plugin to use WikiTeX (http://wikisophia.org/)
 *
 * This plugin has been written by silhusk
 * silhusk(AT)users.sf.net - http://silhusk.isa-geek.org
 *
 * This plugin is in the public domain: you can edit and redistribute it as you like,
 * just,  please, give credits to the original author.
 * Note: WikiTeX is distributed under other conditions, it is not part of the plugin!
 *
 * Installation notes:
 * You should place this file in your tiki/lib/wiki-plugins/ directory and
 * install wikitex (e.g. in tiki/lib/extensions/wikitex/) and its requirements
 * (latex, lylipond, ... - see http://wikitex.org/wikitex/current/README)
 * If there are things you don't want to use you can comment them out in
 * wikitex/wikitex.inc.php
 * You have to fix the variables $IP and $wgScriptPath below in this file to
 * point to the directory containing the path extensions/wikitex/ (do nothing if
 * you extracted wikitex in tiki/lib/extensions/wikitex/)
 * For security you should put an index.php file and a .htaccess in wikitex/ denying
 * access, but you must allow access to wikitex/tmp/ This directory must also be
 * writeable by php.
 * For wikitex to work, your php configuration must allow you to use the php 
 * 'shell_exec' funtion. If you are in safe_mode you can substitute it by 'exec'
 * (in wikitex/wikitex.php) and eventually move the script to be executed in
 * your safe_mode_exec_dir directory.
 * Beware that using latex may cause security issues - read wikitex's doc!
 * and that wikitex mantains his own cache of images in wikitex/tmp/ (you may have
 * to delete them sometimes)
 *
 * Changelog:
 *   June 2005: initial version
 *
 */
global $wgScriptPath;

$IP = "lib/"; // [FIX this to install]
$wgScriptPath = 'lib'; // [FIX this to install]
// ----- nothing to edit below this line -----//

function wikiplugin_tex_help() {
	return tra("WikiTeX").":<br />~np~{TEX(type=> math|chess|go|plot)}".tra("text")."{TEX}~/np~";
}

// this must be outside the function, unlucky.
global $arrRend, $objRend, $arrErr, $strErr, $strRendPath;

// define a fake class for the parser
	class TParser
	{
		var $hooks;

		function __construct()
		{
			$this->hooks = array();
		}

		function TParser()
		{
			$this->__construct();
		}

		function setHook($strKey, $strVal)
		{
			$this->hooks["$strKey"] = $strVal;
		}

		function hasHook($strKey)
		{
			return array_key_exists($strKey, $this->hooks);
		}

		function getHook($strKey)
		{
			return $this->hooks["$strKey"];
		}
	}
	global $wgParser;
	$wgParser = new TParser;

require ("$IP/extensions/wikitex/wikitex.php");
voidRegister();

function wikitex_clean_data($data) {
	// replace some html tags with text equivalent
	$search = array(
	  "'&(quot|#34);'i",
	  "'&(amp|#38);'i",
	  "'&(lt|#60);'i",
	  "'&(gt|#62);'i",
	  "'&(nbsp|#160);'i",
	  "'&(iexcl|#161);'i",
	  "'&(cent|#162);'i",
	  "'&(pound|#163);'i",
	  "'&(copy|#169);'i"
	);
	$replace = array(
	  "\"",
	  "&",
	  "<",
	  ">",
	  " ",
	  chr(161),
	  chr(162),
	  chr(163),
	  chr(169),
	);
	$ret = preg_replace($search, $replace, $data);
	return $ret;
}

function wikiplugin_tex($data, $params) {
	extract ($params,EXTR_SKIP);

	if (!isset($type) || !is_string($type)) {
		return ("<b>missing type parameter for plugin</b><br/>");
	}
	
	global $wgParser;
	$ret ='void';

	if ($wgParser->hasHook($type)){
	  $data = wikitex_clean_data($data);
	  $func_name = $wgParser->getHook($type);
	  $ret = $func_name($data);
	} else {
		$ret = "<b>this type is not supported</b><br/>";
	}

	return $ret;
}

?>
