Beispiel #1
0
 protected function function_check($code)
 {
     $preg = '#(\\W|\\s)' . implode('(\\W|\\s)|(\\W|\\s)', self::$black_list) . '(\\W|\\s)#';
     if (count(self::$black_list) && preg_match($preg, $code, $match)) {
         $line = 0;
         $rows = explode("\n", $this->tpl['source']);
         while (!strpos($rows[$line], $code)) {
             $line++;
         }
         $e = new RainTpl_SyntaxException('Unallowed syntax in ' . $this->tpl['tpl_filename'] . ' template');
         throw $e->setTemplateFile($this->tpl['tpl_filename'])->setTag($code)->setTemplateLine($line);
     }
 }
Beispiel #2
0
 /**
  * Check if function is in black list (sandbox)
  *
  * @access protected
  * @param  string $code
  * @throws RainTpl_SyntaxException
  * @return void
  */
 protected function function_check($code)
 {
     $preg = '#(\\W|\\s)' . implode('(\\W|\\s)|(\\W|\\s)', self::$black_list) . '(\\W|\\s)#';
     // check if the function is in the black list (or not in white list)
     if (count(self::$black_list) && preg_match($preg, $code, $match)) {
         // find the line of the error
         $line = 0;
         $rows = explode("\n", $this->tpl['source']);
         while (!strpos($rows[$line], $code)) {
             $line++;
         }
         // stop the execution of the script
         $e = new RainTpl_SyntaxException('Unallowed syntax in ' . $this->tpl['tpl_filename'] . ' template');
         throw $e->setTemplateFile($this->tpl['tpl_filename'])->setTag($code)->setTemplateLine($line);
     }
 }
Beispiel #3
0
 /**
  * Compile template
  * @access protected
  */
 protected function _compile_template($code, $template_basedir, $template_filepath)
 {
     //path replace (src of img, background and href of link)
     if (static::$conf['path_replace']) {
         $code = $this->_path_replace($code, $template_basedir);
     }
     // set tags
     foreach (static::$conf['tags'] as $tag => $tag_array) {
         list($split, $match) = $tag_array;
         $tag_split[$tag] = $split;
         $tag_match[$tag] = $match;
     }
     $keys = array_keys(static::$conf['registered_tags']);
     $tag_split += array_merge($tag_split, $keys);
     //split the code with the tags regexp
     $code_split = preg_split("/" . implode("|", $tag_split) . "/", $code, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
     //variables initialization
     $parsed_code = $comment_is_open = $ignore_is_open = NULL;
     $open_if = $loop_level = 0;
     //read all parsed code
     while ($html = array_shift($code_split)) {
         //close ignore tag
         if (!$comment_is_open && preg_match($tag_match['ignore_close'], $html)) {
             $ignore_is_open = FALSE;
         } elseif ($ignore_is_open) {
             //ignore the code
         } elseif (preg_match($tag_match['noparse_close'], $html)) {
             $comment_is_open = FALSE;
         } elseif ($comment_is_open) {
             $parsed_code .= $html;
         } elseif (preg_match($tag_match['ignore'], $html)) {
             $ignore_is_open = TRUE;
         } elseif (preg_match($tag_match['noparse'], $html)) {
             $comment_is_open = TRUE;
         } elseif (preg_match($tag_match['include'], $html, $matches)) {
             //variables substitution
             $include_var = $this->_var_replace($matches[1], $loop_level);
             //dynamic include
             $parsed_code .= '<?php $tpl = new ' . get_called_class() . ';' . '$tpl_dir_temp = static::$conf[\'tpl_dir\'];' . '$tpl->assign( $this->var );' . (!$loop_level ? null : '$tpl->assign( "key", $key' . $loop_level . ' ); $tpl->assign( "value", $value' . $loop_level . ' );') . '$tpl->draw( dirname("' . $include_var . '") . ( substr("' . $include_var . '",-1,1) != "/" ? "/" : "" ) . basename("' . $include_var . '") );' . '?>';
         } elseif (preg_match($tag_match['loop'], $html, $matches)) {
             //increase the loop counter
             $loop_level++;
             // check if is a function
             if (preg_match("/.*\\(.*\\)/", $matches['variable'])) {
                 $var = $matches['variable'];
             } else {
                 //replace the variable in the loop
                 $var = $this->_var_replace($matches['variable'], $loop_level - 1, $escape = false);
             }
             //loop variables
             $counter = "\$counter{$loop_level}";
             // count iteration
             if (isset($matches['key']) && isset($matches['value'])) {
                 $key = $matches['key'];
                 $value = $matches['value'];
             } elseif (isset($matches['key'])) {
                 $key = "\$key{$loop_level}";
                 // key
                 $value = $matches['key'];
             } else {
                 $key = "\$key{$loop_level}";
                 // key
                 $value = "\$value{$loop_level}";
                 // value
             }
             //loop code
             $parsed_code .= "<?php {$counter}=-1; if( is_array({$var}) && sizeof({$var}) ) foreach( {$var} as {$key} => {$value} ){ {$counter}++; ?>";
         } elseif (preg_match($tag_match['loop_close'], $html)) {
             //iterator
             $counter = "\$counter{$loop_level}";
             //decrease the loop counter
             $loop_level--;
             //close loop code
             $parsed_code .= "<?php } ?>";
         } elseif (preg_match($tag_match['if'], $html, $matches)) {
             //increase open if counter (for intendation)
             $open_if++;
             //tag
             $tag = $matches[0];
             //condition attribute
             $condition = $matches[1];
             //variable substitution into condition (no delimiter into the condition)
             $parsed_condition = $this->_var_replace($condition, $loop_level, $escape = false);
             //if code
             $parsed_code .= "<?php if( {$parsed_condition} ){ ?>";
         } elseif (preg_match($tag_match['elseif'], $html, $matches)) {
             //tag
             $tag = $matches[0];
             //condition attribute
             $condition = $matches[1];
             //variable substitution into condition (no delimiter into the condition)
             $parsed_condition = $this->_var_replace($condition, $loop_level, $escape = false);
             //elseif code
             $parsed_code .= "<?php }elseif( {$parsed_condition} ){ ?>";
         } elseif (preg_match($tag_match['else'], $html)) {
             //else code
             $parsed_code .= '<?php }else{ ?>';
         } elseif (preg_match($tag_match['if_close'], $html)) {
             //decrease if counter
             $open_if--;
             // close if code
             $parsed_code .= '<?php } ?>';
         } elseif (preg_match($tag_match['function'], $html, $matches)) {
             // get function
             $function = $matches[1];
             // var replace
             if (isset($matches[2])) {
                 $parsed_function = $function . $this->_var_replace($matches[2], $loop_level, $escape = false, $echo = false);
             } else {
                 $parsed_function = $function . "()";
             }
             // function
             $parsed_code .= "<?php echo {$parsed_function}; ?>";
         } elseif (preg_match($tag_match['variable'], $html, $matches)) {
             //variables substitution (es. {$title})
             $parsed_code .= "<?php " . $this->_var_replace($matches[1], $loop_level, $escape = true, $echo = true) . "; ?>";
         } elseif (preg_match($tag_match['constant'], $html, $matches)) {
             $parsed_code .= "<?php echo " . $this->_con_replace($matches[1], $loop_level) . "; ?>";
         } else {
             $found = false;
             foreach (static::$conf['registered_tags'] as $tags => $array) {
                 if (preg_match_all('/' . $array['parse'] . '/', $html, $matches)) {
                     $found = true;
                     $parsed_code .= "<?php echo call_user_func( static::\$conf['registered_tags']['{$tags}']['function'], " . var_export($matches, 1) . " ); ?>";
                 }
             }
             if (!$found) {
                 $parsed_code .= $html;
             }
         }
     }
     if ($open_if > 0) {
         $e = new RainTpl_SyntaxException('Error! You need to close an {if} tag in ' . $template_filepath . ' template');
         throw $e->setTemplateFile($template_filepath);
     }
     if ($loop_level > 0) {
         $e = new RainTpl_SyntaxException('Error! You need to close the {loop} tag in ' . $template_filepath . ' template');
         throw $e->setTemplateFile($template_filepath);
     }
     return $parsed_code;
 }