From bec9690ff519d33d7ef3f0c8f8f8cf72b4ef059d Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Mon, 20 May 2013 14:52:36 -0400
Subject: [PATCH] Improve some options description

---
 program/include/rcmail_output_html.php |   69 +++++++++++++---------------------
 1 files changed, 26 insertions(+), 43 deletions(-)

diff --git a/program/include/rcmail_output_html.php b/program/include/rcmail_output_html.php
index f2bdd95..02eef2f 100644
--- a/program/include/rcmail_output_html.php
+++ b/program/include/rcmail_output_html.php
@@ -731,7 +731,6 @@
     /**
      * Determines if a given condition is met
      *
-     * @todo   Get rid off eval() once I understand what this does.
      * @todo   Extend this to allow real conditions, not just "set"
      * @param  string Condition statement
      * @return boolean True if condition is met, False if not
@@ -760,14 +759,15 @@
 
 
     /**
-     * Parses expression and replaces variables
+     * Parse & evaluate a given expression and return its result.
      *
-     * @param  string Expression statement
-     * @return string Expression value
+     * @param string Expression statement
+     *
+     * @return mixed Expression result
      */
-    protected function parse_expression($expression)
+    protected function eval_expression ($expression)
     {
-        return preg_replace(
+        $expression = preg_replace(
             array(
                 '/session:([a-z0-9_]+)/i',
                 '/config:([a-z0-9_]+)(:([a-z0-9_]+))?/i',
@@ -779,45 +779,29 @@
             ),
             array(
                 "\$_SESSION['\\1']",
-                "\$this->app->config->get('\\1',rcube_utils::get_boolean('\\3'))",
-                "\$this->env['\\1']",
+                "\$app->config->get('\\1',rcube_utils::get_boolean('\\3'))",
+                "\$env['\\1']",
                 "rcube_utils::get_input_value('\\1', rcube_utils::INPUT_GPC)",
                 "\$_COOKIE['\\1']",
-                "\$this->browser->{'\\1'}",
+                "\$browser->{'\\1'}",
                 $this->template_name,
             ),
-            $expression);
-    }
+            $expression
+        );
 
-    protected function eval_expression ($expression) {
-        return preg_replace_callback(
-            array(
-                '/session:([a-z0-9_]+)/i',
-                '/config:([a-z0-9_]+)(:([a-z0-9_]+))?/i',
-                '/env:([a-z0-9_]+)/i',
-                '/request:([a-z0-9_]+)/i',
-                '/cookie:([a-z0-9_]+)/i',
-                '/browser:([a-z0-9_]+)/i',
-                '/template:name/i',
-            ),
-            function($match) {
-                if(preg_match('/session:([a-z0-9_]+)/i', $match, $matches)) {
-                    return $_SESSION[$matches[1]];
-                } else if(preg_match('/config:([a-z0-9_]+)(:([a-z0-9_]+))?/i', $match, $matches)) {
-                    return $this->app->config->get($matches[1],rcube_utils::get_boolean($matches[3]));
-                } else if(preg_match('/env:([a-z0-9_]+)/i', $match, $matches)) {
-                    return $this->env[$matches[1]];
-                } else if(preg_match('/request:([a-z0-9_]+)/i', $match, $matches)) {
-                    return rcube_utils::get_input_value($matches[1], rcube_utils::INPUT_GPC);
-                } else if(preg_match('/cookie:([a-z0-9_]+)/i', $match, $matches)) {
-                    return $_COOKIE[$matches[1]];
-                } else if(preg_match('/browser:([a-z0-9_]+)/i', $match, $matches)) {
-                    return $this->browser->{$matches[1]};
-                } else if(preg_match('/template:name/i', $match, $matches)) {
-                    return $this->template_name;
-                }
-            },
-            $expression);
+        $fn = create_function('$app,$browser,$env', "return ($expression);");
+        if (!$fn) {
+            rcube::raise_error(array(
+                'code' => 505,
+                'type' => 'php',
+                'file' => __FILE__,
+                'line' => __LINE__,
+                'message' => "Expression parse error on: ($expression)"), true, false);
+
+            return null;
+        }
+
+        return $fn($this->app, $this->browser, $this->env);
     }
 
 
@@ -870,7 +854,7 @@
             // show a label
             case 'label':
                 if ($attrib['expression'])
-                    $attrib['name'] = eval("return " . $this->parse_expression($attrib['expression']) .";");
+                    $attrib['name'] = $this->eval_expression($attrib['expression']);
 
                 if ($attrib['name'] || $attrib['command']) {
                     // @FIXME: 'noshow' is useless, remove?
@@ -1002,8 +986,7 @@
 
             // return code for a specified eval expression
             case 'exp':
-                $value = $this->parse_expression($attrib['expression']);
-                return html::quote( $this->eval_expression($attrib['expression']) );
+                return html::quote($this->eval_expression($attrib['expression']));
 
             // return variable
             case 'var':

--
Gitblit v1.9.1