<?php
/**
* PmWiki module to embed a dynamic page using the TinyButStrong template Engine.
* Version 1.00 , on 2007-02-16, by Skrol29
* Version 1.02 , on 2007-03-21, by Skrol29 : Bug when a Javascript is placed inside the BODY tags.
* Version 1.03 , on 2007-03-26, by Skrol29 : Better managment of <pre> tags and allaws several arguments in the pmWiki makrup.
* Works with TBS version 3.2.0 or higher. TinyButStrong site : www.tinybutstrong.com
* In your PmWiki page add (:TbsContent example1:) to insert the dynamic content made by TBS.
*/

// ----------------------
// --- PmWiki Cookbook --
// ----------------------
if (defined('PmWiki')) {
	
	function TbsContent($ArgLst='') {
		
		// Split arguments
		$ArgLst = explode(' ',$ArgLst);
		$iMax = count($ArgLst)-1;
		for ($i=0;$i<=$iMax;$i++) {$ArgLst[$i]=trim($ArgLst[$i]);}

		$TbsAction = $ArgLst[0];
		if ($TbsAction=='') return '(TBS for pmWiki) Error : no parameter defined.';

		global $TbsScriptToCall, $TbsScriptArgs;
		if (!isset($TbsScriptToCall)) return '(TBS for pmWiki) Error : no script defined.';
		$TbsScriptArgs = $ArgLst;

		if (is_array($TbsScriptToCall)) {
			// Allowed scripts are defined ins a PHP array
			if (isset($TbsScriptToCall['path'])) {
				$path = $TbsScriptToCall['path'];
			} else {
				$path = '';
			}
			if (isset($TbsScriptToCall[$TbsAction])) {
		  	include_once($path.$TbsScriptToCall[$TbsAction]);
			} else {
				return '(TBS for pmWiki) Error : parameter \''.$TbsAction.'\' is not allowed.';
			}
		} else {
	  	include_once($TbsScriptToCall);
	  }

	  $x = $GLOBALS['_TbsContentForPmWiki'];
	  unset($GLOBALS['_TbsContentForPmWiki']);
	  return $x;

	}
	
	// Put a tag like this into your pmWiki page: (:TbsContent action:) action is a keywork transmeted by pmWiki to your script.
	Markup('TbsContent', 'directives', '/\\(:TbsContent (.*?):\\)/e', "TbsContent('$1')");
	
	// Disabled Wikiwords. This is important for custom scripts because when Wikiwords all works with serveral upper case in there are bounded with <span class='wikiword'> </span> and this makes troubles in CSS and JavaScript.
	DisableMarkup('wikilink'); // trick that works well given by PM :)

}

// ---------------------
// --- TBS Plug-In    --
// ---------------------

// Name of the class is a keyword used for Plug-In authentication. So i'ts better to save it into a constant.
define('TBS_PMWIKI','clsTbsPmWiki');

// Put the name of the class into global variable array $_TBS_AutoInstallPlugIns to have it automatically installed for any new TBS instance.
$GLOBALS['_TBS_AutoInstallPlugIns'][] = TBS_PMWIKI;

class clsTbsPmWiki {

	function OnInstall() {
		$this->Version = '1.00';
		return array('BeforeLoadTemplate','OnSpecialVar','AfterShow');
	}

	function BeforeLoadTemplate(&$File,&$HtmlCharSet) {
		// Prepare data to found the template in the given path if any.
		if ( ($this->TBS->_LastFile=='') and (isset($GLOBALS['TbsScriptToCall']['path'])) ) {
			$this->TBS->_LastFile = $GLOBALS['TbsScriptToCall']['path'].'nofile.txt';
		}
	}

	function OnSpecialVar($Name,&$IsSupported,&$Value,&$PrmLst) {
		if ($Name=='pmwiki_page') {
			$IsSupported = true;
			$Value = basename($_SERVER['PHP_SELF']);
			$q = trim(''.$_SERVER['QUERY_STRING']);
			if ($q!=='') $Value .= '?'.$q;
		}
	}

	function AfterShow(&$Render) {
		$Render = TBS_NOTHING;
		$body = tbs_Html_GetPart($this->TBS->Source,'body');
		$before = '';
		if ($body===false) {
			// It is not a full HTML content
			$html = $this->TBS->Source;
		} else {
			// It is a full HTML content
			$html = $body;
			$p = false;
			if ($p===false) $p = strpos($this->TBS->Source,'<BODY ');
			if ($p===false) $p = strpos($this->TBS->Source,'<BODY>');
			if ($p===false) $p = strpos($this->TBS->Source,'<body ');
			if ($p===false) $p = strpos($this->TBS->Source,'<body>');
			if ($p!==false) {
				$x = substr($this->TBS->Source,0,$p);
				$before .= "\r\n".tbs_Html_GetPart($x,'script',true); // Add possible Javascripts snipets  of the Html page
				$before .= "\r\n".tbs_Html_GetPart($x,'style',true);  // Add possible local Styles of the Html page
				$before .= "\r\n";
			}
		}
		// if (!in_array('nopre',$GLOBALS['TbsScriptArgs'])) $html = '</pre>'.$html.'<pre>'; // Add tags <pre> in order to avoid the same provided in pmWiki.
		$html = $before.'</pre>'.$html.'<pre>'; // Add tags <pre> in order to avoid the same provided in pmWiki.
		$GLOBALS['_TbsContentForPmWiki'] = $html;
	}

}

?>