예제 #1
0
파일: Core.php 프로젝트: fjpqzm/ganglia-web
 /**
  * [util function] triggers a dwoo error
  *
  * @param string $message the error message
  * @param int $level the error level, one of the PHP's E_* constants
  */
 public function triggerError($message, $level = E_USER_NOTICE)
 {
     if (!($tplIdentifier = $this->template->getResourceIdentifier())) {
         $tplIdentifier = $this->template->getResourceName();
     }
     trigger_error('Dwoo error (in ' . $tplIdentifier . ') : ' . $message, $level);
 }
 public function isCached(Dwoo_ITemplate $tpl)
 {
     return is_string($tpl->getCachedTemplate($this));
 }
예제 #3
0
 /**
  * compiles the provided string down to php code
  *
  * @param string $tpl the template to compile
  * @return string a compiled php string
  */
 public function compile(Dwoo $dwoo, Dwoo_ITemplate $template)
 {
     // init vars
     $tpl = $template->getSource();
     $ptr = 0;
     $this->dwoo = $dwoo;
     $this->template = $template;
     $this->templateSource =& $tpl;
     $this->pointer =& $ptr;
     while (true) {
         // if pointer is at the beginning, reset everything, that allows a plugin to externally reset the compiler if everything must be reparsed
         if ($ptr === 0) {
             // resets variables
             $this->usedPlugins = array();
             $this->data = array();
             $this->scope =& $this->data;
             $this->scopeTree = array();
             $this->stack = array();
             $this->line = 1;
             // add top level block
             $compiled = $this->addBlock('topLevelBlock', array(), 0);
             $this->stack[0]['buffer'] = '';
             if ($this->debug) {
                 echo 'COMPILER INIT<br />';
             }
             if ($this->debug) {
                 echo 'PROCESSING PREPROCESSORS (' . count($this->processors['pre']) . ')<br />';
             }
             // runs preprocessors
             foreach ($this->processors['pre'] as $preProc) {
                 if (is_array($preProc) && isset($preProc['autoload'])) {
                     $preProc = $this->loadProcessor($preProc['class'], $preProc['name']);
                 }
                 if (is_array($preProc) && $preProc[0] instanceof Dwoo_Processor) {
                     $tpl = call_user_func($preProc, $tpl);
                 } else {
                     $tpl = call_user_func($preProc, $this, $tpl);
                 }
             }
             unset($preProc);
             // show template source if debug
             if ($this->debug) {
                 echo '<pre>' . print_r(htmlentities($tpl), true) . '</pre><hr />';
             }
             // strips php tags if required by the security policy
             if ($this->securityPolicy !== null) {
                 $search = array('{<\\?php.*?\\?>}');
                 if (ini_get('short_open_tags')) {
                     $search = array('{<\\?.*?\\?>}', '{<%.*?%>}');
                 }
                 switch ($this->securityPolicy->getPhpHandling()) {
                     case Dwoo_Security_Policy::PHP_ALLOW:
                         break;
                     case Dwoo_Security_Policy::PHP_ENCODE:
                         $tpl = preg_replace_callback($search, array($this, 'phpTagEncodingHelper'), $tpl);
                         break;
                     case Dwoo_Security_Policy::PHP_REMOVE:
                         $tpl = preg_replace($search, '', $tpl);
                 }
             }
         }
         $pos = strpos($tpl, $this->ld, $ptr);
         if ($pos === false) {
             $this->push(substr($tpl, $ptr), 0);
             break;
         } elseif (substr($tpl, $pos - 1, 1) === '\\' && substr($tpl, $pos - 2, 1) !== '\\') {
             $this->push(substr($tpl, $ptr, $pos - $ptr - 1) . $this->ld);
             $ptr = $pos + strlen($this->ld);
         } elseif (preg_match('/^' . $this->ldr . ($this->allowLooseOpenings ? '\\s*' : '') . 'literal' . ($this->allowLooseOpenings ? '\\s*' : '') . $this->rdr . '/s', substr($tpl, $pos), $litOpen)) {
             if (!preg_match('/' . $this->ldr . ($this->allowLooseOpenings ? '\\s*' : '') . '\\/literal' . ($this->allowLooseOpenings ? '\\s*' : '') . $this->rdr . '/s', $tpl, $litClose, PREG_OFFSET_CAPTURE, $pos)) {
                 throw new Dwoo_Compilation_Exception($this, 'The {literal} blocks must be closed explicitly with {/literal}');
             }
             $endpos = $litClose[0][1];
             $this->push(substr($tpl, $ptr, $pos - $ptr) . substr($tpl, $pos + strlen($litOpen[0]), $endpos - $pos - strlen($litOpen[0])));
             $ptr = $endpos + strlen($litClose[0][0]);
         } else {
             if (substr($tpl, $pos - 2, 1) === '\\' && substr($tpl, $pos - 1, 1) === '\\') {
                 $this->push(substr($tpl, $ptr, $pos - $ptr - 1));
                 $ptr = $pos;
             }
             $this->push(substr($tpl, $ptr, $pos - $ptr));
             $ptr = $pos;
             $pos += strlen($this->ld);
             if ($this->allowLooseOpenings) {
                 while (substr($tpl, $pos, 1) === ' ') {
                     $pos += 1;
                 }
             } else {
                 if (substr($tpl, $pos, 1) === ' ' || substr($tpl, $pos, 1) === "\r" || substr($tpl, $pos, 1) === "\n" || substr($tpl, $pos, 1) === "\t") {
                     $ptr = $pos;
                     $this->push($this->ld);
                     continue;
                 }
             }
             // check that there is an end tag present
             if (strpos($tpl, $this->rd, $pos) === false) {
                 throw new Dwoo_Compilation_Exception($this, 'A template tag was not closed, started with "' . substr($tpl, $ptr, 30) . '"');
             }
             $ptr += strlen($this->ld);
             $subptr = $ptr;
             while (true) {
                 $parsed = $this->parse($tpl, $subptr, null, false, 'root', $subptr);
                 // reload loop if the compiler was reset
                 if ($ptr === 0) {
                     continue 2;
                 }
                 $len = $subptr - $ptr;
                 $this->push($parsed, substr_count(substr($tpl, $ptr, $len), "\n"));
                 $ptr += $len;
                 if ($parsed === false) {
                     break;
                 }
             }
             // adds additional line breaks between php closing and opening tags because the php parser removes those if there is just a single line break
             if (substr($this->curBlock['buffer'], -2) === '?>' && preg_match('{^(([\\r\\n])([\\r\\n]?))}', substr($tpl, $ptr, 3), $m)) {
                 if ($m[3] === '') {
                     $ptr += 1;
                     $this->push($m[1] . $m[1], 1);
                 } else {
                     $ptr += 2;
                     $this->push($m[1] . "\n", 2);
                 }
             }
         }
     }
     $compiled .= $this->removeBlock('topLevelBlock');
     if ($this->debug) {
         echo 'PROCESSING POSTPROCESSORS<br />';
     }
     foreach ($this->processors['post'] as $postProc) {
         if (is_array($postProc) && isset($postProc['autoload'])) {
             $postProc = $this->loadProcessor($postProc['class'], $postProc['name']);
         }
         if (is_array($postProc) && $postProc[0] instanceof Dwoo_Processor) {
             $compiled = call_user_func($postProc, $compiled);
         } else {
             $compiled = call_user_func($postProc, $this, $compiled);
         }
     }
     unset($postProc);
     if ($this->debug) {
         echo 'COMPILATION COMPLETE : MEM USAGE : ' . memory_get_usage() . '<br />';
     }
     $output = "<?php\n";
     // build plugin preloader
     foreach ($this->usedPlugins as $plugin => $type) {
         if ($type & Dwoo::CUSTOM_PLUGIN) {
             continue;
         }
         switch ($type) {
             case Dwoo::BLOCK_PLUGIN:
             case Dwoo::CLASS_PLUGIN:
                 $output .= "if (class_exists('Dwoo_Plugin_{$plugin}', false)===false)\n\t\$this->getLoader()->loadPlugin('{$plugin}');\n";
                 break;
             case Dwoo::FUNC_PLUGIN:
                 $output .= "if (function_exists('Dwoo_Plugin_{$plugin}')===false)\n\t\$this->getLoader()->loadPlugin('{$plugin}');\n";
                 break;
             case Dwoo::SMARTY_MODIFIER:
                 $output .= "if (function_exists('smarty_modifier_{$plugin}')===false)\n\t\$this->getLoader()->loadPlugin('{$plugin}');\n";
                 break;
             case Dwoo::SMARTY_FUNCTION:
                 $output .= "if (function_exists('smarty_function_{$plugin}')===false)\n\t\$this->getLoader()->loadPlugin('{$plugin}');\n";
                 break;
             case Dwoo::SMARTY_BLOCK:
                 $output .= "if (function_exists('smarty_block_{$plugin}')===false)\n\t\$this->getLoader()->loadPlugin('{$plugin}');\n";
                 break;
             case Dwoo::PROXY_PLUGIN:
                 $output .= $this->getDwoo()->getPluginProxy()->getPreloader($plugin);
                 break;
             default:
                 throw new Dwoo_Compilation_Exception($this, 'Type error for ' . $plugin . ' with type' . $type);
         }
     }
     $output .= $compiled . "\n?>";
     $output = preg_replace('/(?<!;|\\}|\\*\\/|\\n|\\{)(\\s*' . preg_quote(self::PHP_CLOSE, '/') . preg_quote(self::PHP_OPEN, '/') . ')/', ";\n", $output);
     $output = str_replace(self::PHP_CLOSE . self::PHP_OPEN, "\n", $output);
     // handle <?xml tag at the beginning
     $output = preg_replace('#(/\\* template body \\*/ \\?>\\s*)<\\?xml#is', '$1<?php echo \'<?xml\'; ?>', $output);
     if ($this->debug) {
         echo '<hr /><pre>';
         $lines = preg_split('{\\r\\n|\\n|<br />}', highlight_string($output, true));
         array_shift($lines);
         foreach ($lines as $i => $line) {
             echo $i + 1 . '. ' . $line . "\r\n";
         }
     }
     if ($this->debug) {
         echo '<hr /></pre></pre>';
     }
     $this->template = $this->dwoo = null;
     $tpl = null;
     return $output;
 }
예제 #4
0
 public function testTemplate(Dwoo_ITemplate $template)
 {
     $this->template = $template;
     return $template->getCompiledTemplate($this, $this->getCompiler());
 }