[PHP] CodeIgniter加上phpcms的模板机制 1 →→→→→进入此内容的聊天室

来自 , 2020-06-07, 写在 PHP, 查看 161 次.
URL http://www.code666.cn/view/e22cb9d6
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3.  *  模板解析缓存
  4.  */
  5. final class template_cache {
  6.    
  7.     public $cache_path;
  8.     public function __construct()
  9.     {
  10.         //$CI =& get_instance();
  11.         $this->cache_path = APPPATH.'views';
  12.     }
  13.    
  14.     /**
  15.      * 编译模板
  16.      *
  17.      * @param $module    模块名称
  18.      * @param $template    模板文件名
  19.      * @param $istag    是否为标签模板
  20.      * @return unknown
  21.      */
  22.    
  23.     public function template_compile($module, $template, $style = 'default') {
  24.        
  25.         $tplfile= APPPATH.'views'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.php';
  26.        
  27.         if (! file_exists ( $tplfile )) {
  28.             show_error($tplfile ,  500 ,  'Template does not exist(1)');
  29.         }
  30.        
  31.         $content = @file_get_contents ( $tplfile );
  32.  
  33.         $filepath = $this->cache_path.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR;
  34.        
  35.        
  36.         if(!is_dir($filepath)) {
  37.             mkdir($filepath, 0777, true);
  38.         }
  39.         $compiledtplfile = $filepath.$template.'.php';
  40.         $content = $this->template_parse($content);
  41.         $strlen = file_put_contents ( $compiledtplfile, $content );
  42.         chmod ( $compiledtplfile, 0777 );
  43.         return $strlen;
  44.     }
  45.    
  46.     /**
  47.      * 更新模板缓存
  48.      *
  49.      * @param $tplfile    模板原文件路径
  50.      * @param $compiledtplfile    编译完成后,写入文件名
  51.      * @return $strlen 长度
  52.      */
  53.     public function template_refresh($tplfile, $compiledtplfile) {
  54.         $str = @file_get_contents ($tplfile);
  55.         $str = $this->template_parse ($str);
  56.         $strlen = file_put_contents ($compiledtplfile, $str );
  57.         chmod ($compiledtplfile, 0777);
  58.         return $strlen;
  59.     }
  60.    
  61.  
  62.     /**
  63.      * 解析模板
  64.      *
  65.      * @param $str    模板内容
  66.      * @return ture
  67.      */
  68.     public function template_parse($str) {
  69.         $str = preg_replace ( "/\{template\s+(.+)\}/", "<?php include template(\\1); ?>", $str );
  70.         $str = preg_replace ( "/\{include\s+(.+)\}/", "<?php include \\1; ?>", $str );
  71.         $str = preg_replace ( "/\{view\s+(.+)\}/", "<?php \$this->load->view(\\1); ?>", $str );
  72.         $str = preg_replace ( "/\{php\s+(.+)\}/", "<?php \\1?>", $str );
  73.         //alex fix
  74.         $str = preg_replace ( "/\{{if\s+(.+?)\}}/", "``if \\1``", $str );
  75.         $str = preg_replace ( "/\{{else\}}/", "``else``", $str );
  76.         $str = preg_replace ( "/\{{\/if\}}/", "``/if``", $str );
  77.        
  78.         $str = preg_replace ( "/\{if\s+(.+?)\}/", "<?php if(\\1) { ?>", $str );
  79.         $str = preg_replace ( "/\{else\}/", "<?php } else { ?>", $str );
  80.         $str = preg_replace ( "/\{elseif\s+(.+?)\}/", "<?php } elseif (\\1) { ?>", $str );
  81.         $str = preg_replace ( "/\{\/if\}/", "<?php } ?>", $str );
  82.        
  83.         //for 循环
  84.         $str = preg_replace("/\{for\s+(.+?)\}/","<?php for(\\1) { ?>",$str);
  85.         $str = preg_replace("/\{\/for\}/","<?php } ?>",$str);
  86.         //++ --
  87.         $str = preg_replace("/\{\+\+(.+?)\}/","<?php ++\\1; ?>",$str);
  88.         $str = preg_replace("/\{\-\-(.+?)\}/","<?php ++\\1; ?>",$str);
  89.         $str = preg_replace("/\{(.+?)\+\+\}/","<?php \\1++; ?>",$str);
  90.         $str = preg_replace("/\{(.+?)\-\-\}/","<?php \\1--; ?>",$str);
  91.         //alex fix
  92.         $str = preg_replace ( "/\``if\s+(.+?)\``/", "{{if \\1}}", $str );
  93.         $str = preg_replace ( "/\``else``/", "{{else}}", $str );
  94.         $str = preg_replace ( "/\``\/if\``/", "{{/if}}", $str );
  95.        
  96.         $str = preg_replace ( "/\{loop\s+(\S+)\s+(\S+)\}/", "<?php \$n=1;if(is_array(\\1)) foreach(\\1 AS \\2) { ?>", $str );
  97.         $str = preg_replace ( "/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/", "<?php \$n=1; if(is_array(\\1)) foreach(\\1 AS \\2 => \\3) { ?>", $str );
  98.         $str = preg_replace ( "/\{\/loop\}/", "<?php \$n++;}unset(\$n); ?>", $str );
  99.         $str = preg_replace ( "/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<?php echo \\1;?>", $str );
  100.         $str = preg_replace ( "/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<?php echo \\1;?>", $str );
  101.         $str = preg_replace ( "/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/", "<?php echo \\1;?>", $str );
  102.         $str = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/es", "\$this->addquote('<?php echo \\1;?>')",$str);
  103.         $str = preg_replace ( "/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s", "<?php echo \\1;?>", $str );
  104.         $str = preg_replace("/\{pc:(\w+)\s+([^}]+)\}/ie", "self::pc_tag('$1','$2', '$0')", $str);
  105.         $str = preg_replace("/\{\/pc\}/ie", "self::end_pc_tag()", $str);
  106.         $str = "<?php defined('BASEPATH') or exit('No direct script access allowed.'); ?>" . $str;
  107.         return $str;
  108.     }
  109.  
  110.     /**
  111.      * 转义 // 为 /
  112.      *
  113.      * @param $var    转义的字符
  114.      * @return 转义后的字符
  115.      */
  116.     public function addquote($var) {
  117.         return str_replace ( "\\\"", "\"", preg_replace ( "/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var ) );
  118.     }
  119.    
  120.     /**
  121.      * 解析PC标签
  122.      * @param string $op 操作方式
  123.      * @param string $data 参数
  124.      * @param string $html 匹配到的所有的HTML代码
  125.      */
  126.     public static function pc_tag($op, $data, $html) {
  127.         preg_match_all("/([a-z]+)\=[\"]?([^\"]+)[\"]?/i", stripslashes($data), $matches, PREG_SET_ORDER);
  128.         $arr = array('action','num','cache','page', 'pagesize', 'urlrule', 'return', 'start','setpages');
  129.         $tools = array('json', 'xml', 'block', 'get');
  130.         $datas = array();
  131.         $tag_id = md5(stripslashes($html));
  132.         //可视化条件
  133.         $str_datas = 'op='.$op.'&tag_md5='.$tag_id;
  134.         foreach ($matches as $v) {
  135.             $str_datas .= $str_datas ? "&$v[1]=".($op == 'block' && strpos($v[2], '$') === 0 ? $v[2] : urlencode($v[2])) : "$v[1]=".(strpos($v[2], '$') === 0 ? $v[2] : urlencode($v[2]));
  136.             if(in_array($v[1], $arr)) {
  137.                 $$v[1] = $v[2];
  138.                 continue;
  139.             }
  140.             $datas[$v[1]] = $v[2];
  141.         }
  142.         $str = '';
  143.         $setpages = isset($setpages) && intval($setpages) ? intval($setpages) : 10;
  144.         $num = isset($num) && intval($num) ? intval($num) : 20;
  145.         $cache = isset($cache) && intval($cache) ? intval($cache) : 0;
  146.         $return = isset($return) && trim($return) ? trim($return) : 'data';
  147.         if (!isset($urlrule)) $urlrule = '';
  148.         if (!empty($cache) && !isset($page)) {
  149.             $str .= '$tag_cache_name = md5(implode(\'&\','.self::arr_to_html($datas).').\''.$tag_id.'\');if(!$'.$return.' = tpl_cache($tag_cache_name,'.$cache.')){';
  150.         }
  151.         if (in_array($op,$tools)) {
  152.             switch ($op) {
  153.                 case 'json':
  154.                         if (isset($datas['url']) && !empty($datas['url'])) {
  155.                             $str .= '$json = @file_get_contents(\''.$datas['url'].'\');';
  156.                             $str .= '$'.$return.' = json_decode($json, true);';
  157.                         }
  158.                     break;
  159.                    
  160.                 case 'block':
  161.                     $str .= '$block_tag = pc_base::load_app_class(\'block_tag\', \'block\');';
  162.                     $str .= 'echo $block_tag->pc_tag('.self::arr_to_html($datas).');';
  163.                     break;
  164.             }
  165.         } else {
  166.             if (!isset($action) || empty($action)) return false;
  167.             if ( file_exists(APPPATH.'libraries'.DIRECTORY_SEPARATOR.$op.'_tag.php')) {
  168.                 $str .= 'if(!isset($CI))$CI =& get_instance();$CI->load->library("'.$op.'_tag");if (method_exists($CI->'.$op.'_tag, \''.$action.'\')) {';    
  169.                 if (isset($start) && intval($start)) {
  170.                     $datas['limit'] = intval($start).','.$num;
  171.                 } else {
  172.                     $datas['limit'] = $num;
  173.                 }
  174.                 if (isset($page)) {
  175.                     $str .= '$pagesize = '.$num.';';
  176.                     $str .= '$page = intval('.$page.') ? intval('.$page.') : 1;if($page<=0){$page=1;}';
  177.                     $str .= '$offset = ($page - 1) * $pagesize;$urlrule="'.$urlrule.'";';
  178.                     $datas['limit'] = '$offset.",".$pagesize';
  179.                     $datas['action'] = $action;
  180.                     $str .= '$'.$op.'_total = $CI->'.$op.'_tag->count('.self::arr_to_html($datas).');';
  181.                    
  182.                     $str .= 'if($'.$op.'_total>$pagesize){ $pages = pages($'.$op.'_total, $page, $pagesize, $urlrule); } else { $pages="" ;}';
  183.                 }
  184.                 $str .= '$'.$return.' = $CI->'.$op.'_tag->'.$action.'('.self::arr_to_html($datas).');';
  185.                 $str .= '}';
  186.             }
  187.         }
  188.         if (!empty($cache) && !isset($page)) {
  189.             $str .= 'if(!empty($'.$return.')){setcache($tag_cache_name, $'.$return.', \'tpl_data\');}';
  190.             $str .= '}';
  191.         }
  192.         return "<"."?php ".$str."?".">";
  193.     }
  194.    
  195.     /**
  196.      * PC标签结束
  197.      */
  198.     static private function end_pc_tag() {
  199.         return '<?php if(defined(\'IN_ADMIN\') && !defined(\'HTML\')) {if(isset($data))unset($data);echo \'</div>\';}?>';
  200.     }
  201.    
  202.     /**
  203.      * 转换数据为HTML代码
  204.      * @param array $data 数组
  205.      */
  206.     private static function arr_to_html($data) {
  207.         if (is_array($data)) {
  208.             $str = 'array(';
  209.             foreach ($data as $key=>$val) {
  210.                 if (is_array($val)) {
  211.                     $str .= "'$key'=>".self::arr_to_html($val).",";
  212.                 } else {
  213.                     if (strpos($val, '$')===0) {
  214.                         $str .= "'$key'=>$val,";
  215.                     } else {
  216.                         $str .= "'$key'=>'".self::new_addslashes($val)."',";
  217.                     }
  218.                 }
  219.             }
  220.             return $str.')';
  221.         }
  222.         return false;
  223.     }
  224.    
  225.     /**
  226.      * 返回经addslashes处理过的字符串或数组
  227.      * @param $string 需要处理的字符串或数组
  228.      * @return mixed
  229.      */
  230.     function new_addslashes($string){
  231.         if(!is_array($string)) return addslashes($string);
  232.         foreach($string as $key => $val) $string[$key] = new_addslashes($val);
  233.         return $string;
  234.     }
  235. }
  236.  

回复 "CodeIgniter加上phpcms的模板机制 1"

这儿你可以回复上面这条便签

captcha