",$content); //变量替换 $content= preg_replace("/\[!--(.+?)--\]/","{\$\\1}",$content); //万能标签替换 $content= $this->cmsinfo_replace($content); return $content; } /* 万能标签替换 * */ public function cmsinfo_replace($content){ preg_match_all("//",$content,$myvar); if(count($myvar)){ foreach($myvar[0] as $v){ $cmsinfo=$v; // $str="query("'.$parameter["sql"].'"); '; } //指定表方式 else if($parameter["table"]||$parameter["catid"]){ if(!$parameter["table"]){ //如果没有指定table,则从catid里查询表名 $catid_arr=explode(",",$parameter["catid"]); $cat=M("cat")->where(array("catid"=>$catid_arr[0]))->find(); $modelid=$cat["modelid"]; $table_name=$cat["table_name"]; $parameter["table"]="cms_".$table_name; } if($parameter["catid"]){ //如果是int形返回当前栏目和子栏目catid if(is_numeric($parameter["catid"])){ $son=getSonCat($parameter["catid"]); if(count($son)>1){ $parameter["catid"]=array("IN",$son); }else{ $parameter["catid"]=$son[0]; } } //字符串形式的话,返回array("IN","1,2,3") else{ $catid_num=explode(",",$parameter["catid"]); $parameter["catid"]=array("IN",$parameter["catid"]); } } // $where=$parameter; unset($where["sql"]); unset($where["table"]); unset($where["limit"]); unset($where["order"]); unset($where["field"]); unset($where["group"]); unset($where["having"]); unset($where["join"]); unset($where["union"]); unset($where["distinct"]); $where = var_export($where,true); $where= preg_replace("/'array\((.*)\)',/","array(\\1),", $where);//去掉数组外的单引号 $where= preg_replace("/=> '\\$(.+)',/U","=> $\\1,", $where);//如果数组里包含变更的话,去掉单引号 $where= stripslashes($where);//去掉转义字符 $str.='$mwhere='.$where.';'; preg_match_all('/ \[(\w+)\]=/',$cmsinfo,$autofield); foreach($autofield[1] as $vv){ $str.=' if(!$mwhere[\''.$vv.'\']){ unset($mwhere[\''.$vv.'\']); } '; } $str.='$result=M("'.$parameter["table"].'")'; //相关条件字段判断 if($where){ $str.='->where($mwhere)'; } if($parameter["limit"]){ $str.='->limit('.$parameter["limit"].')'; } if($parameter["order"]){ $str.='->order("'.$parameter["order"].'")'; } if($parameter["field"]){ $str.='->field("'.$parameter["field"].'")'; } if($parameter["group"]){ $str.='->group("'.$parameter["group"].'")'; } if($parameter["having"]){ $str.='->having("'.$parameter["having"].'")'; } if($parameter["join"]){ $str.='->join("'.$parameter["join"].'")'; } if($parameter["union"]){ $str.='->union("'.$parameter["union"].'")'; } if($parameter["distinct"]){ $str.='->distinct("'.$parameter["distinct"].'")'; } $str.='->select();'; } //PHP代码 $str.=' $no=1; foreach($result as $key=>$v){ if($v[id]&&$v[catid]){ $v[titleurl]=titleurl($v); } if($v[catid]){ $cat=M("cat")->find($v[catid]); $v[caturl]=caturl($cat[catid]); } ?>'; // $content=str_replace($cmsinfo,$str,$content); } } $content=str_replace("",'',$content); return $content; } //查询公共模板 public function getTempTemplate($myvar){ $template=$this->where(array("myvar"=>$myvar))->getField("content"); $template=htmlspecialchars_decode($template); return $template; } //生成template文件 public function makeTemplateFile($template_id){ $template_r=$this->find($template_id); $template_r["content"]=$this->replaceTemplate($template_r["content"]); //如果模板类型是公共模板的话文件名以myvar的值命名 if($template_r["type"]=="public"){ file_put_contents(C("CMS_TEMP_PATH").$template_r["myvar"].".html",$template_r["content"]); }else{ file_put_contents(C("CMS_TEMP_PATH").$template_id.".html",$template_r["content"]); } } //删除模板文件 public function deleteTemplateFile($template_id){ unlink(C("CMS_TEMP_PATH").$template_id.".html"); } }