Skip to content

Commit

Permalink
Adapt existing code to PSR-4 #39
Browse files Browse the repository at this point in the history
- Bulk changes to adapt to PSR-2 Smarty Plugins
  • Loading branch information
alombarte committed Oct 12, 2015
1 parent cc5a522 commit 71b520f
Show file tree
Hide file tree
Showing 19 changed files with 636 additions and 788 deletions.
150 changes: 70 additions & 80 deletions src/Smarty-sifo-plugins/block.t.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

/**
* block.t.php - Smarty simulation of gettext block plugin
* block.t.php - Smarty simulation of gettext block plugin.
*
* Modified to use PHP Array intead of gettext. In Windows gettext is not supported.
*/
Expand All @@ -15,22 +16,22 @@
*/
function smarty_gettext_strarg($str)
{
$tr = array();
$p = 0;
$tr = array();
$p = 0;

for ($i=1; $i < func_num_args(); $i++) {
$arg = func_get_arg($i);
for ($i = 1; $i < func_num_args(); ++$i) {
$arg = func_get_arg($i);

if (is_array($arg)) {
foreach ($arg as $aarg) {
$tr['%'.++$p] = $aarg;
}
} else {
$tr['%'.++$p] = $arg;
}
}
if (is_array($arg)) {
foreach ($arg as $aarg) {
$tr['%'.++$p] = $aarg;
}
} else {
$tr['%'.++$p] = $arg;
}
}

return strtr($str, $tr);
return strtr($str, $tr);
}

/**
Expand All @@ -53,79 +54,68 @@ function smarty_gettext_strarg($str)
*/
function smarty_block_t($params, $text, &$smarty)
{
$text = stripslashes($text);
$text = stripslashes($text);

// set escape mode (Escape use is not available when your have to set to true the escape_html flag)
if( ( $smarty == null ) || ( !$smarty->escape_html ) )
{
// In the don't protected configuration, the expectd behaviour is escapeing html:
$escape = ( isset($params['escape'] ) )? $params['escape'] : "html";
unset($params['escape']);
// set escape mode (Escape use is not available when your have to set to true the escape_html flag)
if (($smarty == null) || (!$smarty->escape_html)) {
// In the don't protected configuration, the expectd behaviour is escapeing html:
$escape = (isset($params['escape'])) ? $params['escape'] : 'html';
unset($params['escape']);

if( isset( $params['escapevar'] ) )
{
// Option not available when escape_html is false. You could use $var|escape
unset( $params['escapevar'] );
}
}
else
{
$escape = false;
if ( isset($params['escape'] ) )
{
// (escape) Escape param is not available when escape_html is set to true.
unset($params['escape']);
}
if (isset($params['escapevar'])) {
// Option not available when escape_html is false. You could use $var|escape
unset($params['escapevar']);
}
} else {
$escape = false;
if (isset($params['escape'])) {
// (escape) Escape param is not available when escape_html is set to true.
unset($params['escape']);
}

// set escape_var mode
if ( ( !isset( $params['escapevar'] ) ) || ( $params['escapevar']!='no') )
{
foreach( $params as &$param )
{
$param = htmlspecialchars($param, ENT_QUOTES, SMARTY_RESOURCE_CHAR_SET );
}
}
elseif( isset( $params['escapevar'] ) )
{
unset( $params['escapevar'] );
}
}
// set escape_var mode
if ((!isset($params['escapevar'])) || ($params['escapevar'] != 'no')) {
foreach ($params as &$param) {
$param = htmlspecialchars($param, ENT_QUOTES, SMARTY_RESOURCE_CHAR_SET);
}
} elseif (isset($params['escapevar'])) {
unset($params['escapevar']);
}
}

// set plural version
if (isset($params['plural'])) {
$plural = $params['plural'];
unset($params['plural']);
// set plural version
if (isset($params['plural'])) {
$plural = $params['plural'];
unset($params['plural']);

// set count
if (isset($params['count']) && ( 1 != $params['count'] ) ) {
$text = $plural;
unset($params['count']);
}
}
// set count
if (isset($params['count']) && (1 != $params['count'])) {
$text = $plural;
unset($params['count']);
}
}

$text = \Sifo\I18N::getTranslation( $text );
$text = \Sifo\I18N::getTranslation($text);

// run strarg if there are parameters
if (count($params)) {
$text = smarty_gettext_strarg($text, $params);
}
// run strarg if there are parameters
if (count($params)) {
$text = smarty_gettext_strarg($text, $params);
}

switch ($escape) {
case 'html':
$text = nl2br(htmlspecialchars($text));
break;
case 'javascript':
case 'js':
// javascript escape
$text = str_replace('\'', '\\\'', stripslashes($text));
break;
case 'url':
// url escape
$text = urlencode($text);
break;
}
switch ($escape) {
case 'html' :
$text = nl2br(htmlspecialchars($text));
break;
case 'javascript':
case 'js':
// javascript escape
$text = str_replace('\'', '\\\'', stripslashes($text));
break;
case 'url':
// url escape
$text = urlencode($text);
break;
}

return $text;
return $text;
}

?>
109 changes: 52 additions & 57 deletions src/Smarty-sifo-plugins/function.email_obfuscator.php
Original file line number Diff line number Diff line change
@@ -1,77 +1,72 @@
<?php

/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
* Smarty plugin.
*/

include_once('block.t.php');
include_once 'block.t.php';

/**
* Smarty email obfuscator plugin
* Smarty email obfuscator plugin.
*
* Type: function.
* Name: email_obfuscator.
* Purpose: email obfuscation for protecting emails.
*
* @author Nino Dafonte <[email protected]>.
*
* @param string Email to obfuscate (received automatically).
* @param string Text to show instead of the email.
* @return string JavaScript to show the obfuscated email.
*/
*
* @return string JavaScript to show the obfuscated email.
*/
function smarty_function_email_obfuscator($params, &$smarty)
{

// email address to obfuscate.
$email = ( isset( $params['email'] ) ) ? $params['email'] : '';
// optional text to show instead the email
$linktext = ( isset( $params['text'] ) ) ? $params['text'] : '';
// style information via class.
$style_class = ( isset( $params['class'] ) ) ? ' class=\"' . $params['class'] . '\" ' : '';
// style information via id.
$style_id = ( isset( $params['id'] ) ) ? ' id=\"' . $params['id'] . '\" ' : '';
// email address to obfuscate.
$email = (isset($params['email'])) ? $params['email'] : '';
// optional text to show instead the email
$linktext = (isset($params['text'])) ? $params['text'] : '';
// style information via class.
$style_class = (isset($params['class'])) ? ' class=\"'.$params['class'].'\" ' : '';
// style information via id.
$style_id = (isset($params['id'])) ? ' id=\"'.$params['id'].'\" ' : '';

// Getting the extra params for the case of %1, %2, etc in the linktext. Using ; like separator.
$extra_params = array();
if (isset($params['extra'])) {
$extra_params = explode(';', $params['extra']);
}

// Getting the extra params for the case of %1, %2, etc in the linktext. Using ; like separator.
$extra_params = array();
if ( isset( $params['extra'] ) ) {
$extra_params = explode( ';', $params['extra'] );
}
// Translating linktext
$textbefore = '';
$textafter = '';
if (!empty($linktext)) {
$temp = smarty_block_t($extra_params, $linktext, $this, null);
// If the email is inside the text string
$email_position = strpos($temp, $email);
if ($email_position) {
// If the email is inside the string we make the link only in the email address
$textbefore = substr($temp, 0, $email_position);
$textafter = substr($temp, strpos($temp, $email) + strlen($email));
$linktext = '';
} else {
// Else the link is all the string
$linktext = $temp;
}
}

// Translating linktext
$textbefore = '';
$textafter = '';
if ( !empty( $linktext ) )
{
$temp = smarty_block_t( $extra_params, $linktext, $this, null);
// If the email is inside the text string
$email_position = strpos( $temp, $email );
if ( $email_position )
{
// If the email is inside the string we make the link only in the email address
$textbefore = substr( $temp, 0, $email_position );
$textafter = substr( $temp, strpos( $temp, $email ) + strlen( $email ) );
$linktext = '';
}
else
{
// Else the link is all the string
$linktext = $temp;
}
}
$character_set = '+-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
$key = str_shuffle($character_set);
$cipher_text = '';
$id = 'e'.rand(1, 999999999);
for ($i = 0; $i < strlen($email); $i += 1) {
$cipher_text .= $key[ strpos($character_set, $email[ $i ]) ];
}
$script = 'var namex="'.$linktext.'";var a="'.$key.'";var b=a.split("").sort().join("");var c="'.$cipher_text.'";var d="";';
$script .= 'for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));var linktext=(namex.length == 0)?linktext=d:linktext=namex;var textbefore="'.$textbefore.'";var textafter="'.$textafter.'";';
$script .= 'document.getElementById("'.$id.'").innerHTML=textbefore+"<a '.$style_id.$style_class.' href=\\"mailto:"+d+"\\">"+linktext+"<\/a>"+textafter';
$script = 'eval("'.str_replace(array('\\', '"'), array('\\\\', '\"'), $script).'")';
$script = '<script type="text/javascript">/*<![CDATA[*/'.$script.'/*]]>*/</script>';

$character_set = '+-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
$key = str_shuffle( $character_set );
$cipher_text = '';
$id = 'e'.rand( 1, 999999999 );
for ( $i = 0; $i < strlen( $email ); $i += 1 )
{
$cipher_text.= $key[ strpos( $character_set, $email[ $i ] ) ];
}
$script = 'var namex="' . $linktext . '";var a="' . $key . '";var b=a.split("").sort().join("");var c="' . $cipher_text . '";var d="";';
$script .= 'for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));var linktext=(namex.length == 0)?linktext=d:linktext=namex;var textbefore="' . $textbefore . '";var textafter="' . $textafter . '";';
$script .= 'document.getElementById("' . $id . '").innerHTML=textbefore+"<a ' . $style_id . $style_class . ' href=\\"mailto:"+d+"\\">"+linktext+"<\/a>"+textafter';
$script = "eval(\"".str_replace(array("\\",'"'),array("\\\\",'\"'), $script)."\")";
$script = '<script type="text/javascript">/*<![CDATA[*/' . $script . '/*]]>*/</script>';
return '<span id="' . $id . '">[javascript protected email address]</span>' . $script;
return '<span id="'.$id.'">[javascript protected email address]</span>'.$script;
}
?>
Loading

0 comments on commit 71b520f

Please sign in to comment.