Page 1 of 1

[fixed] plugin prefilter

Posted: Sat Jul 21, 2012 8:50 pm
by Jean le Chauve
I made a plugin who obfuscate email adresses.
It work with 1.10.3 (name : prefilter.protect_email.php)

Code: Select all

<?php
function smarty_cms_prefilter_protect_email($tpl_output, &$smarty)
{
    $result = explode(':', $smarty->_current_file);
	if (count($result) > 0)
	{
		if ($result[0] == 'content' || $result[0] == 'evaluated template' || $result[0] == 'globalcontent')
		{
		$patterns = array ('#(<a).+(href="mailto:)([a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4})"(.+)(</a>)#iU',
                   '#[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}#i');
		$replace = array ('$3', '{mailto address="$0" encode="javascript"}');
		$tpl_output = preg_replace($patterns, $replace, $tpl_output);
		}
	}
    return $tpl_output;
}

function smarty_cms_about_prefilter_protect_email() {
	?>
	<p>Author: Jean le Chauve<jeanlechauve@gmail.com></p>
	<p>Version: 1.0</p>
	<p>
	Change History:<br/>
	None
	</p>
	<?php
}
?>
So, I modified it for 1.11-beta4 (name : prefilter.protect_email.php)

Code: Select all

<?php
function smarty_prefilter_protect_email($tpl_output, &$template)
{
    $smarty = $template->smarty;
	$result = explode(':', $smarty->_current_file);
	if (count($result) > 0)
	{
		if( startswith($result[0],'tmp_') ) $result[0] = 'template';
		if ($result[0] == 'content' || $result[0] == 'template' || $result[0] == 'globalcontent')
		{
		$patterns = array ('#(<a).+(href="mailto:)([a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4})"(.+)(</a>)#iU',
                   '#[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}#i');
		$replace = array ('$3', '{mailto address="$0" encode="javascript"}');
		$tpl_output = preg_replace($patterns, $replace, $tpl_output);
		}
	}
    return $tpl_output;
}
?>
It does'nt work >:(
Can you help me ? What'is wrong ?

Re: plugin prefilter

Posted: Sun Jul 22, 2012 4:06 pm
by calguy1000
What's not working. Not enough information to be able to help here.

Is your plugin being called, and just the 'private member' of the smarty class that you're accessing isn't available any more.

1.11 is due to be released TODAY so please provide information as soon as possible.

Re: plugin prefilter

Posted: Sun Jul 22, 2012 6:05 pm
by Jean le Chauve
Thank's for your interest.

The plugin prefilter.protect_email.php is not loaded. I tested it with :

Code: Select all

function smarty_prefilter_protect_email($tpl_output, &$template)
{
    $smarty = $template->smarty;
	$result = explode(':', $smarty->_current_file);
	print_r($result);
	return $tpl_output;
}
For test, I added the replacement code in the end of file prefilter.precompilefunc.php

Code: Select all

function smarty_prefilter_precompilefunc($tpl_output, &$template)
{
  $smarty = $template->smarty;
	$result = explode(':', $smarty->_current_file);
	if (count($result) > 1)
	{
	  if( startswith($result[0],'tmp_') ) $result[0] = 'template';

		switch ($result[0])
		{
		case 'stylesheet':
		  Events::SendEvent('Core','StylesheetPreCompile',array('stylesheet'=>&$tpl_output));
		  break;

			case "content":
			
				Events::SendEvent('Core', 'ContentPreCompile', array('content' => &$tpl_output));
				break;

		case 'tpl_top':
		case 'tpl_body':
		case 'tpl_head':
		case "template":
		  Events::SendEvent('Core', 'TemplatePreCompile', array('template' => &$tpl_output,'type'=>$result[0]));
		  break;

			case "globalcontent":
				Events::SendEvent('Core', 'GlobalContentPreCompile', array('global_content' => &$tpl_output));
				break;
			default:
				break;
		}

	}
	$patterns = array ('#(<a).+(href="mailto:)([a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4})"(.+)(</a>)#iU',
                   '#[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}#i');
	$replace = array ('$3', '{mailto address="$0" encode="javascript"}');
	$tpl_output = preg_replace($patterns, $replace, $tpl_output);
	Events::SendEvent('Core', 'SmartyPreCompile', array('content' => &$tpl_output));
	
	return $tpl_output;
}
?>
and the email replacement work.
How can I make the application loading my prefilter plugin ?

Re: plugin prefilter

Posted: Sun Jul 22, 2012 6:06 pm
by calguy1000
put a die() statement in at the top of the function... clear the cache, and see if it actually gets called. If it does not, then I'll take a quick look.

Re: plugin prefilter

Posted: Sun Jul 22, 2012 6:16 pm
by Jean le Chauve
I clear cache and put : die('not loaded');
Nothing happend

Re: plugin prefilter

Posted: Sun Jul 22, 2012 6:27 pm
by calguy1000
Okay, I'll dig in.

Release has been put off by a week now anyways.

Re: plugin prefilter

Posted: Sun Jul 22, 2012 6:44 pm
by Jean le Chauve
Thank you :)

Re: plugin prefilter

Posted: Mon Jul 23, 2012 3:33 pm
by calguy1000
Fixed in SVN.

Re: plugin prefilter

Posted: Mon Jul 23, 2012 7:06 pm
by Jean le Chauve
Thank you very much :)
Here is the version for svn 8193 :
name : prefilter.protect_email.php

Code: Select all

<?php
function smarty_prefilter_protect_email($tpl_output, &$template)
{
	$smarty = $template->smarty;
        $result = explode(':', $smarty->_current_file);
	$patterns = array ('#(<a).+(href="mailto:)([a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4})"(.+)(</a>)#iU',
                   '#[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}#i');
        $replace = array ('$3', '{mailto address="$0" text="email" encode="javascript"}');
	if (count($result) > 1)
	{
	  if (startswith($result[0],'tmp_')) $result[0] = 'template';
		switch ($result[0])
		{
		case 'stylesheet':
			break;
		case "content":
			$tpl_output = preg_replace($patterns, $replace, $tpl_output);
			break;
		case 'tpl_top':
		case 'tpl_body':
		case 'tpl_head':
		case "template":
			$tpl_output = preg_replace($patterns, $replace, $tpl_output);
			break;
		case "globalcontent":
			$tpl_output = preg_replace($patterns, $replace, $tpl_output);
			break;
		default:
			break;
		}
	}
	
    $tpl_output = preg_replace($patterns, $replace, $tpl_output);
    return $tpl_output;
}
?>
enjoy :)