예제 #1
0
 /**
  * Compiles the opening tag for a function
  * @param  \Box\Brainy\Compiler\TemplateCompiler $compiler A compiler reference
  * @param  array|null  $args     Arguments
  * @param  array|null  $params   Parameters
  * @return mixed
  */
 public static function compileOpen(\Box\Brainy\Compiler\TemplateCompiler $compiler, $args, $params)
 {
     $var = self::getRequiredArg($args, 'var');
     $value = self::getRequiredArg($args, 'value');
     $scopeRaw = self::getOptionalArg($args, 'scope', self::getDefaultScope());
     switch (\Box\Brainy\Compiler\Decompile::decompileString($scopeRaw)) {
         case 'local':
             $scope = Brainy::SCOPE_LOCAL;
             break;
         case 'parent':
             $scope = Brainy::SCOPE_PARENT;
             break;
         case 'root':
             $scope = Brainy::SCOPE_ROOT;
             break;
         case 'global':
             $scope = Brainy::SCOPE_GLOBAL;
             break;
         default:
             $compiler->trigger_template_error('illegal value for "scope" attribute: ' . $scopeRaw, $compiler->lex->taglineno);
     }
     // implement Smarty2's behaviour of variables assigned by reference
     if ($compiler->template->smarty instanceof SmartyBC && Brainy::$assignment_compat === Brainy::ASSIGN_COMPAT) {
         $output = "if (isset(\$_smarty_tpl->tpl_vars[{$var}])) {\n";
         $output .= "  \$_smarty_tpl->tpl_vars[{$var}] = clone \$_smarty_tpl->tpl_vars[{$var}];\n";
         $output .= "  \$_smarty_tpl->tpl_vars[{$var}]->value = {$value};\n";
         $output .= "  \$_smarty_tpl->tpl_vars[{$var}]->scope = {$scope};\n";
         $output .= "} else {\$_smarty_tpl->assign({$var}, {$value}, {$scope});}\n";
     } else {
         $output = "\$_smarty_tpl->assign({$var}, {$value}, {$scope});\n";
     }
     return $output;
 }
예제 #2
0
/**
 * Smarty escape modifier plugin
 *
 * Type:     modifier<br>
 * Name:     escape<br>
 * Purpose:  escape string for output
 *
 * @link   http://www.smarty.net/docsv2/en/language.modifier.escape
 * @author Rodney Rehm
 * @param  array $params parameters
 * @return string with compiled code
 */
function smarty_modifiercompiler_escape($params, $compiler)
{
    try {
        $esc_type = isset($params[1]) ? \Box\Brainy\Compiler\Decompile::decompileString($params[1]) : 'html';
        $double_encode = isset($params[3]) ? $params[3] : 'true';
        switch ($esc_type) {
            case 'html':
                return 'htmlspecialchars(' . $params[0] . ', ENT_QUOTES, \'UTF-8\', ' . $double_encode . ')';
            case 'url':
                return 'rawurlencode(' . $params[0] . ')';
            case 'urlpathinfo':
                return 'str_replace("%2F", "/", rawurlencode(' . $params[0] . '))';
            case 'quotes':
                // escape unescaped single quotes
                return 'preg_replace("%(?<!\\\\\\\\)\'%", "\\\'",' . $params[0] . ')';
            case 'javascript':
                // escape quotes and backslashes, newlines, etc.
                return 'strtr(' . $params[0] . ', array("\\\\" => "\\\\\\\\", "\'" => "\\\\\'", "\\"" => "\\\\\\"", "\\r" => "\\\\r", "\\n" => "\\\\n", "</" => "<\\/" ))';
        }
    } catch (\Box\Brainy\Exceptions\SmartyException $e) {
        // pass through to regular plugin fallback
    }
    // could not optimize |escape call, so fallback to regular plugin
    $compiler->template->required_plugins['compiled']['escape']['modifier']['file'] = BRAINY_PLUGINS_DIR . 'modifier.escape.php';
    $compiler->template->required_plugins['compiled']['escape']['modifier']['function'] = 'smarty_modifier_escape';
    return 'smarty_modifier_escape(' . join(', ', $params) . ')';
}
예제 #3
0
 /**
  * Returns the scope, given the raw scope string
  * @param  string        $raw
  * @param  int|null|void $default The default scope, or null
  * @return int
  */
 public static function getScope($args, $default = null)
 {
     $scopeRaw = self::getOptionalArg($args, 'scope', self::getDefaultScope($default));
     switch (\Box\Brainy\Compiler\Decompile::decompileString($scopeRaw)) {
         case 'local':
             return Brainy::SCOPE_LOCAL;
         case 'parent':
             return Brainy::SCOPE_PARENT;
         case 'root':
             return Brainy::SCOPE_ROOT;
         case 'global':
             return Brainy::SCOPE_GLOBAL;
         default:
             $compiler->trigger_template_error('illegal value for "scope" attribute: ' . $scopeRaw, $compiler->lex->taglineno);
     }
 }