includes/template.php
changeset 189 fd0e9c7a7b28
parent 184 d74ff822acc9
child 193 ebe99e82a59a
child 195 eec079676fe7
child 201 2303ef648290
equal deleted inserted replaced
188:b4aaca42c8a4 189:fd0e9c7a7b28
   956    * @return string
   956    * @return string
   957    */
   957    */
   958   
   958   
   959   function compile_tpl_code($text)
   959   function compile_tpl_code($text)
   960   {
   960   {
       
   961     global $db, $session, $paths, $template, $plugins; // Common objects
   961     // A random seed used to salt tags
   962     // A random seed used to salt tags
   962     $seed = md5 ( microtime() . mt_rand() );
   963     $seed = md5 ( microtime() . mt_rand() );
   963     
   964     
   964     // Strip out PHP sections
   965     // Strip out PHP sections
   965     preg_match_all('/<\?php(.+?)\?>/is', $text, $php_matches);
   966     preg_match_all('/<\?php(.+?)\?>/is', $text, $php_matches);
   984     
   985     
   985     //
   986     //
   986     // Conditionals
   987     // Conditionals
   987     //
   988     //
   988     
   989     
   989     // If-else-end
   990     $keywords = array('BEGIN', 'BEGINNOT', 'IFSET', 'IFPLUGIN');
   990     $text = preg_replace('/<!-- BEGIN ([A-z0-9_-]+?) -->(.*?)<!-- BEGINELSE \\1 -->(.*?)<!-- END \\1 -->/is', '\'; if ( $this->tpl_bool[\'\\1\'] ) { echo \'\\2\'; } else { echo \'\\3\'; } echo \'', $text);
   991     $code = $plugins->setHook('template_compile_logic_keyword');
   991     
   992     foreach ( $code as $cmd )
   992     // If-end
   993     {
   993     $text = preg_replace('/<!-- BEGIN ([A-z0-9_-]+?) -->(.*?)<!-- END \\1 -->/is', '\'; if ( $this->tpl_bool[\'\\1\'] ) { echo \'\\2\'; } echo \'', $text);
   994       eval($cmd);
   994     
   995     }
   995     // If not-else-end
   996     
   996     $text = preg_replace('/<!-- BEGINNOT ([A-z0-9_-]+?) -->(.*?)<!-- BEGINELSE \\1 -->(.*?)<!-- END \\1 -->/is', '\'; if ( !$this->tpl_bool[\'\\1\'] ) { echo \'\\2\'; } else { echo \'\\3\'; } echo \'', $text);
   997     $keywords = implode('|', $keywords);
   997     
   998     
   998     // If not-end
   999     // Matches
   999     $text = preg_replace('/<!-- BEGINNOT ([A-z0-9_-]+?) -->(.*?)<!-- END \\1 -->/is', '\'; if ( !$this->tpl_bool[\'\\1\'] ) { echo \'\\2\'; } echo \'', $text);
  1000     //          1     2                               3                 4   56                       7     8
  1000     
  1001     $regexp = '/(<!-- ('. $keywords .') ([A-z0-9_-]+) -->)(.*)((<!-- BEGINELSE \\3 -->)(.*))?(<!-- END \\3 -->)/isU';
  1001     // If set-else-end
  1002     
  1002     $text = preg_replace('/<!-- IFSET ([A-z0-9_-]+?) -->(.*?)<!-- BEGINELSE \\1 -->(.*?)<!-- END \\1 -->/is', '\'; if ( isset($this->tpl_strings[\'\\1\']) ) { echo \'\\2\'; } else { echo \'\\3\'; } echo \'', $text);
  1003     /*
  1003     
  1004     The way this works is: match all blocks using the standard form with a different keyword in the block each time,
  1004     // If set-end
  1005     and replace them with appropriate PHP logic. Plugin-extensible now. :-)
  1005     $text = preg_replace('/<!-- IFSET ([A-z0-9_-]+?) -->(.*?)<!-- END \\1 -->/is', '\'; if ( isset($this->tpl_strings[\'\\1\']) ) { echo \'\\2\'; } echo \'', $text);
  1006     
  1006     
  1007     The while-loop is to bypass what is apparently a PCRE bug. It's hackish but it works. Properly written plugins should only need
  1007     // If plugin loaded-else-end
  1008     to compile templates (using this method) once for each time the template file is changed.
  1008     $text = preg_replace('/<!-- IFPLUGIN ([A-z0-9_\.-]+?) -->(.*?)<!-- BEGINELSE \\1 -->(.*?)<!-- END \\1 -->/is', '\'; if ( getConfig(\'plugin_\\1\') == \'1\' ) { echo \'\\2\'; } else { echo \'\\3\'; } echo \'', $text);
  1009     */
  1009     
  1010     while ( preg_match($regexp, $text) )
  1010     // If plugin loaded-end
  1011     {
  1011     $text = preg_replace('/<!-- IFPLUGIN ([A-z0-9_\.-]+?) -->(.*?)<!-- END \\1 -->/is', '\'; if ( getConfig(\'plugin_\\1\') == \'1\' ) { echo \'\\2\'; } echo \'', $text);
  1012       preg_match_all($regexp, $text, $matches);
       
  1013       for ( $i = 0; $i < count($matches[0]); $i++ )
       
  1014       {
       
  1015         $start_tag =& $matches[1][$i];
       
  1016         $type =& $matches[2][$i];
       
  1017         $test =& $matches[3][$i];
       
  1018         $particle_true  =& $matches[4][$i];
       
  1019         $else_tag =& $matches[6][$i];
       
  1020         $particle_else =& $matches[7][$i];
       
  1021         $end_tag =& $matches[8][$i];
       
  1022         
       
  1023         switch($type)
       
  1024         {
       
  1025           case 'BEGIN':
       
  1026             $cond = "isset(\$this->tpl_bool['$test']) && \$this->tpl_bool['$test']";
       
  1027             break;
       
  1028           case 'BEGINNOT':
       
  1029             $cond = "!isset(\$this->tpl_bool['$test']) || ( isset(\$this->tpl_bool['$test']) && !\$this->tpl_bool['$test'] )";
       
  1030             break;
       
  1031           case 'IFPLUGIN':
       
  1032             $cond = "getConfig('plugin_$test') == '1'";
       
  1033             break;
       
  1034           case 'IFSET':
       
  1035             $cond = "isset(\$this->tpl_strings['$test'])";
       
  1036             break;
       
  1037           default:
       
  1038             $code = $plugins->setHook('template_compile_logic_cond');
       
  1039             foreach ( $code as $cmd )
       
  1040             {
       
  1041               eval($cmd);
       
  1042             }
       
  1043             break;
       
  1044         }
       
  1045         
       
  1046         if ( !isset($cond) || ( isset($cond) && !is_string($cond) ) )
       
  1047           continue;
       
  1048         
       
  1049         $tag_complete = <<<TPLCODE
       
  1050         ';
       
  1051         /* START OF CONDITION: $type ($test) */
       
  1052         if ( $cond )
       
  1053         {
       
  1054           echo '$particle_true';
       
  1055         /* ELSE OF CONDITION: $type ($test) */
       
  1056         }
       
  1057         else
       
  1058         {
       
  1059           echo '$particle_else';
       
  1060         /* END OF CONDITION: $type ($test) */
       
  1061         }
       
  1062         echo '
       
  1063 TPLCODE;
       
  1064         
       
  1065         $text = str_replace_once($matches[0][$i], $tag_complete, $text);
       
  1066         
       
  1067       }
       
  1068     }
       
  1069     
       
  1070     // For debugging ;-)
       
  1071     // die("<pre>&lt;?php\n" . htmlspecialchars($text."\n\n".print_r($matches,true)) . "\n\n?&gt;</pre>");
  1012     
  1072     
  1013     //
  1073     //
  1014     // Data substitution/variables
  1074     // Data substitution/variables
  1015     //
  1075     //
  1016     
  1076     
  1026     {
  1086     {
  1027       // Substitute the random tag with the "real" PHP code
  1087       // Substitute the random tag with the "real" PHP code
  1028       $tag = "{PHP:$i:$seed}";
  1088       $tag = "{PHP:$i:$seed}";
  1029       $text = str_replace_once($tag, "'; $match echo '", $text);
  1089       $text = str_replace_once($tag, "'; $match echo '", $text);
  1030     }
  1090     }
       
  1091     
       
  1092     // echo('<pre>' . htmlspecialchars($text) . '</pre>');
  1031     
  1093     
  1032     return $text;  
  1094     return $text;  
  1033     
  1095     
  1034   }
  1096   }
  1035   
  1097