<?php if (!defined('PmWiki')) exit();
/**
* jjscms: write a static copy of any page on access 
* optionally integrates jjstemplate to allow for dynamic elements in both, public and private version of the site 
* @author Jens Schiffler <jens döt schiffler ät gmx döt de>
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License
* @version 1.1
* 
* new in 1.1: added $PublishPrivateSection and $PublishPublicSection to global statement in jjsCmsObCallback 
*/
## Configuration Variables
##
## $PublishDir: Directory where the static (public) version of the pages will be written to
SDV($PublishDir, '../../pmwiki/published');
## $PublishUrl: URL prefix to be used in all local links in public version of the page.
## Should either point to place where the server is serving $PublishDir, or to a 
## skript that takes <Group>.<Page>.html as parameter
SDV($PublishUrl, 'index.php?showcmspage=');
## $PublishAttachUrl: Point to pmwikis shared upload dir  
SDV($PublishAttachUrl,'pmwiki/pics/');
## $PublishPubUrl: Point to pmwikis shared pub dir
SDV($PublishPubUrl, 'pmwiki/pub/');

## Anything enclosed by <!-- $PublishPrivateSection --> <!-- /$PublishPrivateSection -->
## in your template will only show up in the private (editable) version of the page.
SDV($PublishPrivateSection, 'dontpublish');
## Anything enclosed by <!-- $PublishPublicSection --> <!-- /$PublishPublicSection -->
## in your template will only show up in the public (static) version of the page.
SDV($PublishPublicSection, 'dontedit');

function jjsCmsObCallback($buffer) 
{
	// post-processing: rewrite urls, write copy of rendered page to file in PublishDir
  global $pagename, $PublishUrl, $PublishAttachUrl, $PublishPubUrl, $PublishDir, $ScriptUrl, $PicsUrl, $PubDirUrl, $PublishPrivateSection, $PublishPublicSection, $PCache;
	
	$time = $PCache[$pagename]['time'];
	$filename = $PublishDir . '/' . $pagename . '.html';
	$stat = stat($filename);
	
	# Attention - cache will always be updated, no matter if the page has changed or not  
	# to do: reliable test if cached version is up to date
	# 
	if (
		true
		||		
		false === $stat
		||
		$stat['mtime'] < $time
	)
	{
		
		
		$x = $buffer;

		// remove private sections  
		$x =	preg_replace(
					'/\<\!\-\- ' . $PublishPrivateSection . ' \-\-\>.*\<\!\-\- \/' . $PublishPrivateSection . ' \-\-\>/Us',
					'',
					$x
		);

		// completely remove vspace p elements
		$x =	preg_replace(
					'/\<p class\=([\'"])vspace\1\>\<\/p\>/Us',
					'',
					$x
		);


		// rewrite simple links
		$x = preg_replace(
					'/([\'"])' . str_replace('/','\/',$ScriptUrl) . '\?n\=([^#]+)\1/Us',
					'\1' . $PublishUrl . '\2.html\1',
				$x);
			
		// rewrite anchored links #
		$x = preg_replace(
					'/([\'"])' . str_replace('/','\/',$ScriptUrl) . '\?n\=([^#]+)\#([^\1]+)\1/Us',
					'\1' . $PublishUrl . '\2.html#\3\1',
				$x);


		$x = preg_replace(	
					'/([\'"])' . str_replace('/','\/', $PicsUrl) . '\/([^\1]+)\1/Us',
					'\1'. $PublishAttachUrl . '\2\1',
				$x);

		$x = preg_replace(	
					'/([\'"])' . str_replace('/','\/', $PubDirUrl) . '\/(.*)[\'"]/Us',
					'\1' . $PublishPubUrl . '\2\1',
				$x);


		$x .= '<!-- ' . $time . ' ! ' . $stat['mtime'] .  ' -->'	;


		$fh = fopen($filename,'w');

		fwrite($fh, $x);

		fclose($fh);


		#	$buffer .= '<!-- last published at ' . now() . '-->';

	}

	// remove public only sections
  $x = preg_replace(
		'/\<\!\-\- ' . $PublishPublicSection . ' \-\-\>.*\<\!\-\- \/' . $PublishPublicSection . ' \-\-\>/Us',
		'', $buffer);

		
	return $x;
}
 

function jjsCmsHandleBrowse($pagename)
{
	global $originalHandleBrowse, $jjsSession, $jjsUseTemplate;
	ob_start('jjsCmsObCallback');
	$originalHandleBrowse($pagename);
	
	if ($jjsUseTemplate)
	{
		$jjsTemplateSrc = ob_get_contents();	
		ob_end_clean();
	
		// post-post-process output 		
		
		$smarty = new c_template();
		
		$smarty->setSrc($jjsTemplateSrc);
		
		if (isset($jjsSession))
		{
			$smarty->assign_by_ref('session', $jjsSession);
		}
		
		$smarty->display('var:smarty_tpl_src');
		#echo $jjsTemplateSrc;
	}
	else
	{
		ob_end_flush();
	}	
}

$originalHandleBrowse = $HandleActions['browse'];
$HandleActions['browse'] = 'jjsCmsHandleBrowse';

?>