chengkun
2025-04-24 5d55579fd424e024c52e62265b72e24c38a45004
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/*
* $Author :PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2021 宿迁鑫潮信息技术有限公司,并保留所有权利。
*
* 软件声明:未经授权前提下,不得用于商业运营、二次开发以及任何形式的再次发布。
 */
class template
{
    function __construct($obj)
    {
        $this->obj = $obj;
    }
    /******遍历指定目录文件夹*****/
    public function model_dir_action($path,$dir=NULL,$type=NULL)
    {
        $handle = opendir($path);
        while($file = readdir($handle))
        {
              if($file=="."||$file==".."||$file==".svn") continue;
              $newFilePath = $path.$file;
              if($type==NULL)
              {
                  if(is_dir($newFilePath))
                  {
                      $list.=$this->model_dir_action($newFilePath."/",$file);
                  }
              }
              if(is_file($newFilePath))
              {
                   if(strrchr($file,".")==".htm")
                  {
                        $list.= $file."+".$path."+".$newFilePath.",";
                  }
              }
         }
         closedir($handle);
         return $list;
 
    }
    /******模板列表,$style:当前模板风格使用目录默认为default,$file:文件夹*****/
    public function model_list_action($style="default",$path=NULL,$file=NULL)
    {    
        if($path=="")
        {
            if($file!="")
            {
                $path=TPL_PATH.$style."/".$file."/";   //获取当前目录路径
            }else{
                $path=TPL_PATH.$style."/";   //获取当前模板目录路径
            }
        }
        $list = $this->model_dir_action($path,$file);
        $lists =@ explode(",",$list);
        if(is_array($lists))
        {
            foreach($lists as $key=>$value)
            {
                $v = @explode("+",$value);
                $tp_list[$key]['name'] = $v[0];
                $tp_list[$key]['path'] = $v[1];
                $tp_list[$key]['file_path'] = str_replace(APP_PATH,"..",$v[1]);
                $tp_list[$key]['dir'] = $tp_list[$key]['file_path'];
                $tp_list[$key]['tp_name'] = "暂无说明";
                $tp_list[$key][time] = "暂未做修改";
            }
 
        }
        $temp = $this->obj->DB_select_all("admin_template");
        if(is_array($temp)){
            foreach($temp as $key=>$value)
            {
                foreach($tp_list as $k=>$v)
                {
                    if($value['name']==$v['name'] && $value['dir']==$v['dir'])
                    {
                        $tp_list[$k]['tp_name']=$value['tp_name'];
                        $tp_list[$k]['time']=date("Y-m-d H:i:s",$value['update_time']);
                    }
                }
 
            }
        }
        return $tp_list;
    }
 
    /******模板内容*****/
    public function model_modify_action($name,$style,$dir)
    {
        $list = $this->model_list_action($style,NULL,NULL);
        foreach($list as $key=>$value)
        {
                if($name==$value['name'] && $dir==$value['dir'])
                {
                    $tp_info['name'] = $value['name'];
                    $tp_info['dir'] = $dir;
                    $tp_info['path'] = $value['path'];
                    $tp_info['file_path'] = $value['file_path'];
                    $tp_info['tp_name'] = $value['tp_name'];
                    break;
                }
        }
        $fp=@fopen($tp_info['path']."/".$tp_info['name'],"r"); //只读打开模板
          $content_text=@fread($fp,filesize($tp_info['path']."/".$tp_info['name']));//读取模板中内容
        fclose($fp);
        $tp_info['content'] = htmlspecialchars($content_text);
        return $tp_info;
    }
    /******更新模板*****/
    public function model_savetp_action($post,$table,$style)
    {
        extract($post);
        $sql = "`name`='$name' and `dir`='$tp_path'";
        $tp = $this->obj->DB_select_all($table,$sql);
        $time = time();
        $value = "`name`='$name',`tp_name`='$tp_name',`update_time`='$time'";
        if(is_array($tp))
        {
            $this->obj->DB_update_all($table,$value,"`name`='$name' AND `dir`='$tp_path'");
        }else{
            $value.=",`dir`='$tp_path'";
            $this->obj->DB_insert_once($table,$value);
        }
        $content = html_entity_decode($content,ENT_QUOTES);
        //$content = str_replace("\\\"","\"",$content);
        $fp = fopen($tp_path."/".$name,"w");
        fwrite($fp,$content);
        fclose($fp); 
        $this->ACT_layer_msg("模板修改成功!",9,"index.php?m=admin_template",2,1);
    }
    /******新增模板*****/
    function model_tpaddsave_action($post,$table)
    {
        extract($post);
        $time = time();
        if($name!="")
        {
            if($dir=="")
            {
                $dir = $style;
                $file_path = $path."/";
            }else{
                $file_path = $path.$dir."/";
            }
            $tpname = $name.".htm";
            $file_list = @explode(",",$this->model_dir_action($file_path,NULL,1));
            if(is_array($file_list))
            {
                foreach($file_list as $key=>$value)
                {
                    $v = @explode("+",$value);
                    if($v[0]==$tpname)
                    {
                        $this->ACT_layer_msg("该文件已经存在!",8,"index.php?m=admin_template&c=tpadd",2,1);
                    }
                }
            }
            $dir = str_replace(APP_PATH,"..",$file_path);
            $value = "`name`='$tpname',`tp_name`='$tp_name',`update_time`='$time',`dir`='$dir'";
            $nid = $this->obj->DB_insert_once($table,$value);
            if($nid>0)
            {
                if(!is_dir($file_path)) //如果此文件夹不存在,则自动建立一个
                {
                    mkdir($file_path,0777,true);
                }
                if(!file_exists($file_path.$name.".htm")) //如果此文件不存在,则自动建立一个
                {
                    $fp = @fopen($file_path.$name.".htm","w");
                    $content = html_entity_decode($content,ENT_QUOTES);
                    //$content = str_replace("\\\"","\"",$content);
                    fwrite($fp,$content);
                    fclose($fp); 
                    $this->ACT_layer_msg("模板添加成功!",9,"index.php?m=admin_template",2,1);
                }
            }else{ 
                $this->ACT_layer_msg("模板添加失败!",8,"index.php?m=admin_template");
            }
        }else{ 
            $this->ACT_layer_msg("文件名称不得为空!",8,"index.php?m=admin_template&c=tpadd");
        }
    }
}