Esempio n. 1
0
 /**
  * Parses all REX_VARs in the given content.
  *
  * @param string $content     Content
  * @param int    $env         Environment
  * @param string $context     Context
  * @param mixed  $contextData Context data
  *
  * @return string
  */
 public static function parse($content, $env = null, $context = null, $contextData = null)
 {
     if (($env & self::ENV_INPUT) != self::ENV_INPUT) {
         $env = $env | self::ENV_OUTPUT;
     }
     self::$env = $env;
     self::$context = $context;
     self::$contextData = $contextData;
     $tokens = token_get_all($content);
     $countTokens = count($tokens);
     $content = '';
     for ($i = 0; $i < $countTokens; ++$i) {
         $token = $tokens[$i];
         if (is_string($token)) {
             $add = $token;
         } else {
             $add = $token[1];
             if (in_array($token[0], [T_INLINE_HTML, T_CONSTANT_ENCAPSED_STRING, T_STRING, T_START_HEREDOC])) {
                 $useVariables = false;
                 $stripslashes = null;
                 switch ($token[0]) {
                     case T_INLINE_HTML:
                         $format = '<?= %s ?>';
                         break;
                     case T_CONSTANT_ENCAPSED_STRING:
                         $format = $token[1][0] == '"' ? '" . %s . "' : "' . %s . '";
                         $stripslashes = $token[1][0];
                         break;
                     case T_STRING:
                         while (isset($tokens[++$i]) && (is_string($tokens[$i]) && in_array($tokens[$i], ['=', '[', ']']) || in_array($tokens[$i][0], [T_WHITESPACE, T_STRING, T_CONSTANT_ENCAPSED_STRING, T_LNUMBER, T_ISSET]))) {
                             $add .= is_string($tokens[$i]) ? $tokens[$i] : $tokens[$i][1];
                         }
                         --$i;
                         $format = '%s';
                         break;
                     case T_START_HEREDOC:
                         while (isset($tokens[++$i]) && (is_string($tokens[$i]) || $tokens[$i][0] != T_END_HEREDOC)) {
                             $add .= is_string($tokens[$i]) ? $tokens[$i] : $tokens[$i][1];
                         }
                         --$i;
                         if (preg_match("/'(.*)'/", $token[1], $match)) {
                             // nowdoc
                             $format = "\n" . $match[1] . "\n. %s . <<<'" . $match[1] . "'\n";
                         } else {
                             // heredoc
                             $format = '{%s}';
                             $useVariables = true;
                         }
                         break;
                 }
                 $add = self::replaceVars($add, $format, $useVariables, $stripslashes);
                 if ($token[0] == T_CONSTANT_ENCAPSED_STRING) {
                     $start = substr($add, 0, 5);
                     $end = substr($add, -5);
                     if ($start == '"" . ' || $start == "'' . ") {
                         $add = substr($add, 5);
                     }
                     if ($end == ' . ""' || $end == " . ''") {
                         $add = substr($add, 0, -5);
                     }
                 }
             }
         }
         $content .= $add;
     }
     return $content;
 }
Esempio n. 2
0
File: var.php Progetto: DECAF/redaxo
 /**
  * Parses all REX_VARs in the given content.
  *
  * @param string $content     Content
  * @param int    $env         Environment
  * @param string $context     Context
  * @param mixed  $contextData Context data
  *
  * @return string
  */
 public static function parse($content, $env = null, $context = null, $contextData = null)
 {
     if (($env & self::ENV_INPUT) != self::ENV_INPUT) {
         $env = $env | self::ENV_OUTPUT;
     }
     self::$env = $env;
     self::$context = $context;
     self::$contextData = $contextData;
     self::$variableIndex = 0;
     $tokens = token_get_all($content);
     $countTokens = count($tokens);
     $content = '';
     for ($i = 0; $i < $countTokens; ++$i) {
         $token = $tokens[$i];
         if (is_string($token)) {
             $content .= $token;
             continue;
         }
         if (!in_array($token[0], [T_INLINE_HTML, T_CONSTANT_ENCAPSED_STRING, T_STRING, T_START_HEREDOC])) {
             $content .= $token[1];
             continue;
         }
         $add = $token[1];
         switch ($token[0]) {
             case T_INLINE_HTML:
                 $format = '<?= %s@@@INLINE_HTML_REPLACEMENT_END@@@';
                 $add = self::replaceVars($add, $format);
                 $add = preg_replace_callback('/@@@INLINE_HTML_REPLACEMENT_END@@@(\\r?\\n?)/', function (array $match) {
                     return $match[1] ? ', "' . addcslashes($match[1], "\r\n") . '" ?>' . $match[1] : ' ?>';
                 }, $add);
                 break;
             case T_CONSTANT_ENCAPSED_STRING:
                 $format = $token[1][0] == '"' ? '" . %s . "' : "' . %s . '";
                 $add = self::replaceVars($add, $format, false, $token[1][0]);
                 $start = substr($add, 0, 5);
                 $end = substr($add, -5);
                 if ($start == '"" . ' || $start == "'' . ") {
                     $add = substr($add, 5);
                 }
                 if ($end == ' . ""' || $end == " . ''") {
                     $add = substr($add, 0, -5);
                 }
                 break;
             case T_STRING:
                 while (isset($tokens[++$i]) && (is_string($tokens[$i]) && in_array($tokens[$i], ['=', '[', ']']) || in_array($tokens[$i][0], [T_WHITESPACE, T_STRING, T_CONSTANT_ENCAPSED_STRING, T_LNUMBER, T_ISSET]))) {
                     $add .= is_string($tokens[$i]) ? $tokens[$i] : $tokens[$i][1];
                 }
                 --$i;
                 $add = self::replaceVars($add);
                 break;
             case T_START_HEREDOC:
                 while (isset($tokens[++$i]) && (is_string($tokens[$i]) || $tokens[$i][0] != T_END_HEREDOC)) {
                     $add .= is_string($tokens[$i]) ? $tokens[$i] : $tokens[$i][1];
                 }
                 --$i;
                 if (preg_match("/'(.*)'/", $token[1], $match)) {
                     // nowdoc
                     $format = "\n" . $match[1] . "\n. %s . <<<'" . $match[1] . "'\n";
                     $add = self::replaceVars($add, $format);
                 } else {
                     // heredoc
                     $add = self::replaceVars($add, '{%s}', true);
                 }
                 break;
         }
         $content .= $add;
     }
     return $content;
 }