tagParse_class.php
资源名称:BBWPS.rar [点击查看]
上传用户:yttaitong
上传日期:2009-05-10
资源大小:128k
文件大小:8k
源码类别:
WEB源码(ASP,PHP,...)
开发平台:
PHP
- <?
- /*
- [BBWPS!] (C)2006-2010 小蜜蜂版权所有.
- This is NOT a freeware, use is subject to license terms
- 时间:2007年12月
- 描述:解析模板上的参数、并把各模块返回的XML解析成数组、根据模板上的参数要求显示到要求的模板上去
- */
- /*调用方法
- $t = new tagParse($p,"./template/default/default.html");
- $t->setInstallPath($installPath);
- $t->setModuleArray($moduleArray);
- $p = $t->parse();
- $t->__tagdParse();*/
- class tagParse{
- var $p = ""; //模板对像
- var $parsePath = ""; //解析模板路径
- var $fileString = ""; //获得模板内容
- var $moduleArray; //安装模块的配置文件
- var $installPath = ""; //平台的安装路径
- var $isopen = FALSE; //是否打开缓存
- var $limit_time_day = ""; //限制白天最大刷新时间(秒)
- var $limit_time_night = ""; //限制夜间最大刷新时间(秒)
- var $isCreateCash = FALSE; //CASH是否到了生存期
- var $cashDir =""; //设置CASH的目录
- var $cashPath = ""; //设置CASH的路径
- var $baseCheckCode = ""; //设置平台和模块间的通讯口令
- //----------------------------------------------------
- //构造函数
- //----------------------------------------------------
- function tagParse($p,$parsePath,$baseCheckCode){
- $this->p = $p;
- $this->parsePath =$parsePath;
- $this->baseCheckCode = $baseCheckCode;
- }
- //----------------------------------------------------
- //析构函数
- //----------------------------------------------------
- function __tagdParse(){
- unset($this->p,$this->parsePath,$this->fileString,$this->moduleArray,$this->installPath,$this->isopen,$this->limit_time_day,$this->limit_time_night,$this->isCreateCash,$this->cashDir,$this->cashPath);
- }
- //----------------------------------------------------
- //获得模板内容
- //----------------------------------------------------
- function getContent(){
- $this->fileString=file_get_contents($this->parsePath);
- }
- //----------------------------------------------------
- //获得安装模块的配置文件
- //----------------------------------------------------
- function setModuleArray($moduleArray){
- $this->moduleArray = $moduleArray;
- }
- //----------------------------------------------------
- //获得平台的安装路径
- //----------------------------------------------------
- function setInstallPath($installPath){
- $this->installPath = $installPath;
- }
- //----------------------------------------------------
- //获得是否开启CASH
- //----------------------------------------------------
- function setOpenCash($isopen=FALSE){
- $this->isopen = $isopen;
- }
- //----------------------------------------------------
- //设置CASH目录
- //----------------------------------------------------
- function setCashDir($dir){
- $this->cashDir = $dir;
- }
- //----------------------------------------------------
- //获得CASH的生存时间
- //----------------------------------------------------
- function setCashTime($limit_time_day,$limit_time_night){
- $this->limit_time_day = $limit_time_day;
- $this->limit_time_night = $limit_time_night;
- }
- //----------------------------------------------------
- //设置CASH的路径
- //----------------------------------------------------
- function setCashPath($cashName){
- $this->cashPath = $this->cashDir."/".md5($cashName).".php";
- }
- //----------------------------------------------------
- //判断CASH是否到了生存期
- //----------------------------------------------------
- function isCreateCash(){
- $dispersion = time() - @filemtime($this->cashPath); //当前时间同文件修改时间之差
- $current_time_hour = (int)date("G",time()); //当前的小时
- //----------------------------------------------------
- //判断白天CASH的生存时间
- //----------------------------------------------------
- if ($current_time_hour > 7 && $current_time_hour < 23) {
- if ($dispersion > $this->limit_time_day)
- $this->isCreateCash = true;
- //----------------------------------------------------
- //判断夜晚CASH的生存时间
- //----------------------------------------------------
- } else {
- if ($dispersion > $this->limit_time_night)
- $this->isCreateCash = true;
- }
- }
- //----------------------------------------------------
- //生成CASH
- //----------------------------------------------------
- function createCash($url){
- if($this->isCreateCash){
- //-----------------------------------------
- //解析XML成数组
- //-----------------------------------------
- $x=new OutParse($url);
- $a=$x->GetItems();
- $stringOfCacheContent='<?PHP'."n";
- foreach ($a as $k=>$v){
- foreach ($v as $key=>$value){
- $value=str_replace('"','"',$value);
- $value=nl2br($value);
- $stringOfCacheContent.='$a["'.$k.'"]["'.$key.'"]="'.trim($value).'";'."n";
- }
- }
- foreach ($f as $k=>$v){
- foreach ($v as $key=>$value){
- $value=str_replace('"','"',$value);
- $value=nl2br($value);
- $stringOfCacheContent.='$f["'.$k.'"]["'.$key.'"]="'.trim($value).'";'."n";
- }
- }
- $stringOfCacheContent.="?>";
- $stringOfCacheContent=preg_replace("/s[ ]+/is","",$stringOfCacheContent);
- $fp = @fopen ($this->cashPath,"w");
- @fputs($fp,$stringOfCacheContent);
- @fclose($fp);
- }
- }
- //----------------------------------------------------
- //解析模板并在模板上显示
- //----------------------------------------------------
- function parse(){
- $this->getContent();
- preg_match_all("/{module_(.+?)_category_([0-9s]+)_type_(.+?)_number_([0-9s]+)_template_(.+?)}/is",$this->fileString,$match);
- //-----------------------------------------
- //解析平台各个子模块的内容
- //-----------------------------------------
- foreach ($match[1] as $key=>$value){
- //----------------------------------------------------------
- //校验码 随机产生MD5加密次数 把加把加密次数附加在密文最后一位
- //----------------------------------------------------------
- $checkNum = rand(2,9);
- $check = MakeMd5($this->baseCheckCode,$checkNum).$checkNum;
- //-----------------------------------------
- //获得URL
- //-----------------------------------------
- $moduleArrayFlag = "bbwps_module_".$match[1][$key]; //下标
- //------------------------------------------------------------------
- //判断$this->moduleArray[$moduleArrayFlag]['dns']是否为空
- //如果为空则不进行此次请求
- //------------------------------------------------------------------
- if($this->moduleArray[$moduleArrayFlag]['idns']){
- $url = strtolower($this->moduleArray[$moduleArrayFlag]['idns']."/api/out.php?type=".$match[3][$key]."&catid=".$match[2][$key]."&number=".$match[4][$key]."&check=".$check);
- $url = str_replace("//api","/api",$url);
- }
- else{
- if($this->moduleArray[$moduleArrayFlag]['dns']){
- $url = strtolower($this->moduleArray[$moduleArrayFlag]['dns']."/api/out.php?type=".$match[3][$key]."&catid=".$match[2][$key]."&number=".$match[4][$key]."&check=".$check);
- $url = str_replace("//api","/api",$url);
- }
- else{
- continue;
- }
- }
- //-----------------------------------------
- //打开缓存
- //-----------------------------------------
- if($this->isopen){
- //-----------------------------------------
- //CASH的路径由模板变量MD5加密获得
- //-----------------------------------------
- $this->setCashPath($match[0][$key]);
- $this->isCreateCash();
- $this->createCash($url);
- include_once($this->cashPath);
- }
- //-----------------------------------------
- //关闭缓存
- //-----------------------------------------
- else {
- //-----------------------------------------
- //解析XML成数组
- //-----------------------------------------
- $x=new OutParse($url);
- $a=$x->GetItems();
- }
- //-----------------------------------------
- //把获得的数据显示在模板上
- //-----------------------------------------
- $this->p->set_var("module_category",null);
- $this->p->set_file("module_category",$match[5][$key].".html");
- $outString = substr($match[0][$key],1,-1);
- //-----------------------------------------
- //把解析出来的数组下标转换为小写
- //-----------------------------------------
- foreach ($a as $key=>$value){
- $a[$key] = array_change_key_case($value);
- }
- $this->p->set_var($outString,null);
- foreach ($a as $v){
- //-----------------------------------------
- //以后$match[5][$key]显示长度在这里实现
- //-----------------------------------------
- $this->p->set_var($v);
- $this->p->parse($outString,"module_category",true);
- }
- unset($a);
- }
- return $this->p;
- unset($x,$a,$match,$absultePath,$outString);
- }
- }
- ?>