<?php
/**
* PmWiki module to embed a dynamic content 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 management of <pre> tags and allows several arguments in the pmWiki makrup.
* Version 2.00 , on 2008-05-10, by Skrol29 : The content is insterted in the pmWiki page, but it is not processed by pmWiki Makups |+| Compatibility with TBS 3.3.0. |+| [var..pmwiki_url] |+| compatibility with $EnablePathInfo=1
* Version 2.01 , on 2010-02-02, by Skrol29 : works with TBS >= 3.5.0
* 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 --
// ----------------------

$RecipeInfo['TbsDynamicContent']['Version'] = '2.01';

if (defined('PmWiki')) {
	
	// 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')");
	$MarkupTable['tbscontent']['seq'] = 'C'; // Change the order of execution, this markup will be processed as the last one, but before the markups for adding skins.
	unset($MarkupRules); // it seems to not be done correctly in DisableMarkup() because the varibale is not declared as global. 

	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;

	}

}

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

// Name of the class is a keyword used for Plug-In authentication. So it's 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() {

		global $RecipeInfo, $NoHTMLCache;
		$this->Version = $RecipeInfo['TbsDynamicContent']['Version'];

		$NoHTMLCache = 1; // Avoid pmWiki cache for next pages.

		// Compatibility with TBS >= 3.3.0
		$v = $this->TBS->Version;
		if (version_compare('3.3.0',$v)>0) {
			$this->OldGetPart = true; // use the old GetPart() function
		} elseif (version_compare('3.5.0',$v)>0) {
			$this->OldGetPart = true; // use the old GetPart() function syntax but write it now
			$GLOBALS['_TbsHook'] =& $this->TBS;
			function tbs_Html_GetPart(&$Txt,$Tag,$WithTags=false,$CancelIfEmpty=false) {
				return $GLOBALS['_TbsHook']->f_Xml_GetPart($Txt,$Tag,$WithTags,$CancelIfEmpty);
			}
		} else {
			$this->OldGetPart = false; // do not use the old GetPart() function
		}

		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_url') {
			$IsSupported = true;
			$p = (isset($PrmLst['page'])) ? trim($PrmLst['page']) : '';
			if ($p=='') $p = $GLOBALS['pagename'];
			$Value = PageVar($p,'$PageUrl');
			return;
		}
		if ($Name=='pmwiki_page') {
			// Compatibility with plugin version 1.X: returns the full URL with all GET parameters
			$IsSupported = true;
			$Value = basename($_SERVER['PHP_SELF']);
			$q = trim(''.$_SERVER['QUERY_STRING']);
			if ($q!=='') $Value .= '?'.$q;
			return;
		}
	}

	function AfterShow(&$Render) {

		$Render = TBS_NOTHING;
		$TBS =& $this->TBS;

		if ($this->OldGetPart) {
			$body = tbs_Html_GetPart($TBS->Source,'body');
			$before = '';
			if ($body=='') {
				// It is not a full HTML content
				$html = $TBS->Source;
			} else {
				// It is a full HTML content
				$html = $body;
				$p = false;
				if ($p===false) $p = strpos($TBS->Source,'<BODY ');
				if ($p===false) $p = strpos($TBS->Source,'<BODY>');
				if ($p===false) $p = strpos($TBS->Source,'<body ');
				if ($p===false) $p = strpos($TBS->Source,'<body>');
				if ($p!==false) {
					$x = substr($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";
				}
			}
			$GLOBALS['_TbsContentForPmWiki'] = $before.$html;
		} else {
			$GLOBALS['_TbsContentForPmWiki'] = $TBS->f_Xml_GetPart( $TBS->Source , '(script)+(style)+body' , true );
		}
	}

}

?>