CMS Made Simple Forums
https://forum.cmsmadesimple.org/

[fixed] plugin prefilter
https://forum.cmsmadesimple.org/viewtopic.php?f=80&t=62056
Page 1 of 1

Author:  Jean le Chauve [ Sat Jul 21, 2012 8:50 pm ]
Post subject:  [fixed] plugin prefilter

I made a plugin who obfuscate email adresses.
It work with 1.10.3 (name : prefilter.protect_email.php)
Code:
<?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&lt;jeanlechauve@gmail.com&gt;</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:
<?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 ?

Author:  calguy1000 [ Sun Jul 22, 2012 4:06 pm ]
Post subject:  Re: plugin prefilter

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.

Author:  Jean le Chauve [ Sun Jul 22, 2012 6:05 pm ]
Post subject:  Re: plugin prefilter

Thank's for your interest.

The plugin prefilter.protect_email.php is not loaded. I tested it with :
Code:
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:
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 ?

Author:  calguy1000 [ Sun Jul 22, 2012 6:06 pm ]
Post subject:  Re: plugin prefilter

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.

Author:  Jean le Chauve [ Sun Jul 22, 2012 6:16 pm ]
Post subject:  Re: plugin prefilter

I clear cache and put : die('not loaded');
Nothing happend

Author:  calguy1000 [ Sun Jul 22, 2012 6:27 pm ]
Post subject:  Re: plugin prefilter

Okay, I'll dig in.

Release has been put off by a week now anyways.

Author:  Jean le Chauve [ Sun Jul 22, 2012 6:44 pm ]
Post subject:  Re: plugin prefilter

Thank you :)

Author:  calguy1000 [ Mon Jul 23, 2012 3:33 pm ]
Post subject:  Re: plugin prefilter

Fixed in SVN.

Author:  Jean le Chauve [ Mon Jul 23, 2012 7:06 pm ]
Post subject:  Re: plugin prefilter

Thank you very much :)
Here is the version for svn 8193 :
name : prefilter.protect_email.php
Code:
<?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 :)

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/