Template.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class Template extends Base
  5. {
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. }
  10. public function index()
  11. {
  12. $param = input();
  13. $path = $param['path'];
  14. $path = str_replace('\\','',$path);
  15. $path = str_replace('/','',$path);
  16. if(empty($path)){
  17. $path = '.@template';
  18. }
  19. if(substr($path,0,10) != ".@template") { $path = ".@template"; }
  20. if(count( explode(".@",$path) ) > 2) {
  21. $this->error(lang('illegal_request'));
  22. return;
  23. }
  24. $uppath = substr($path,0,strrpos($path,"@"));
  25. $ischild = 0;
  26. if ($path !=".@template"){
  27. $ischild = 1;
  28. }
  29. $config = config('maccms.site');
  30. if($param['current']==1){
  31. $path = '.@template@' . $config['template_dir'] .'@' . $config['html_dir'] ;
  32. $ischild = 0;
  33. $pp = str_replace('@','/',$path);
  34. $filters = $pp.'/*';
  35. }
  36. elseif($param['label']==1){
  37. $path = '.@template@' . $config['template_dir'] .'@' . $config['html_dir'] ;
  38. $ischild = 0;
  39. $pp = str_replace('@','/',$path);
  40. $filters = $pp.'/label/*';
  41. }
  42. elseif($param['ads']==1){
  43. $path = '.@template@' . $config['template_dir'] .'@' . $config['html_dir'] ;
  44. $ischild = 0;
  45. $pp = str_replace('@','/',$path);
  46. $filters = $pp.'/ads/*';
  47. }
  48. else{
  49. $pp = str_replace('@','/',$path);
  50. $filters = $pp.'/*';
  51. }
  52. $this->assign('curpath',$path);
  53. $this->assign('uppath',$uppath);
  54. $this->assign('ischild',$ischild);
  55. $num_path = 0;
  56. $num_file = 0;
  57. $sum_size = 0;
  58. $files = [];
  59. if(is_dir($pp)) {
  60. $farr = glob($filters);
  61. if ($farr) {
  62. foreach ($farr as $f) {
  63. if(is_dir($f)) {
  64. $num_path++;
  65. $tmp_path = str_replace('./template/', '.@template/', $f);
  66. $tmp_path = str_replace('/', '@', $tmp_path);
  67. $tmp_name = str_replace($path . '@', '', $tmp_path);
  68. $ftime = filemtime($f);
  69. $files[] = ['isfile' => 0, 'name' => $tmp_name, 'path' => $tmp_path, 'note'=>lang('dir'), 'time' => $ftime];
  70. }
  71. elseif(is_file($f)) {
  72. $num_file++;
  73. $fsize = filesize($f);
  74. $sum_size += $fsize;
  75. $fsize = mac_format_size($fsize);
  76. $ftime = filemtime($f);
  77. $tmp_path = mac_convert_encoding($f, "UTF-8", "GB2312");
  78. $path_info = @pathinfo($f);
  79. $tmp_path = $path_info['dirname'];
  80. $tmp_name = $path_info['basename'];
  81. $files[] = ['isfile' => 1, 'name' => $tmp_name, 'path' => $tmp_path, 'fullname'=> $tmp_path.'/'.$tmp_name, 'size' => $fsize,'note'=>lang('file'), 'time' => $ftime];
  82. }
  83. }
  84. }
  85. }
  86. $this->assign('sum_size',mac_format_size($sum_size));
  87. $this->assign('num_file',$num_file);
  88. $this->assign('num_path',$num_path);
  89. $this->assign('files',$files);
  90. $this->assign('title',lang('admin/template/title'));
  91. return $this->fetch('admin@template/index');
  92. }
  93. public function ads()
  94. {
  95. $adsdir = $GLOBALS['config']['site']['ads_dir'];
  96. if(empty($adsdir)){
  97. $adsdir='ads';
  98. }
  99. $path = './template/'.$GLOBALS['config']['site']['template_dir'].'/'.$adsdir ;
  100. if(!file_exists($path)){
  101. mac_mkdirss($path);
  102. }
  103. $filters = $path.'/*.js';
  104. $num_file=0;
  105. $sum_size=0;
  106. $farr = glob($filters);
  107. if ($farr) {
  108. foreach ($farr as $f) {
  109. if(is_file($f)) {
  110. $num_file++;
  111. $fsize = filesize($f);
  112. $sum_size += $fsize;
  113. $fsize = mac_format_size($fsize);
  114. $ftime = filemtime($f);
  115. $tmp_path = mac_convert_encoding($f, "UTF-8", "GB2312");
  116. $path_info = @pathinfo($f);
  117. $tmp_path = $path_info['dirname'];
  118. $tmp_name = $path_info['basename'];
  119. $files[] = ['isfile' => 1, 'name' => $tmp_name, 'path' => $tmp_path, 'fullname'=> $tmp_path.'/'.$tmp_name, 'size' => $fsize,'note'=>lang('file'), 'time' => $ftime];
  120. }
  121. }
  122. }
  123. $this->assign('curpath',$path);
  124. $this->assign('sum_size',mac_format_size($sum_size));
  125. $this->assign('num_file',$num_file);
  126. $this->assign('files',$files);
  127. $this->assign('title',lang('admin/template/ads/title'));
  128. return $this->fetch('admin@template/ads');
  129. }
  130. public function info()
  131. {
  132. $param = input();
  133. $fname = $param['fname'];
  134. $fpath = $param['fpath'];
  135. if( empty($fpath)){
  136. $this->error(lang('param_err').'1');
  137. return;
  138. }
  139. $fpath = str_replace('@','/',$fpath);
  140. $fullname = $fpath .'/' .$fname;
  141. $fullname = str_replace('\\','/',$fullname);
  142. if( (substr($fullname,0,10) != "./template") || count( explode("./",$fullname) ) > 2) {
  143. $this->error(lang('param_err').'2');
  144. return;
  145. }
  146. $path = pathinfo($fullname);
  147. if(!empty($fname)) {
  148. $extarr = array('html', 'htm', 'js', 'xml');
  149. if (!in_array($path['extension'], $extarr)) {
  150. $this->error(lang('admin/template/ext_safe_tip'));
  151. return;
  152. }
  153. }
  154. $filter = '<\?|php|eval|server|assert|get|post|request|cookie|session|input|env|config|call|global|dump|print|phpinfo|fputs|fopen|global|chr|strtr|pack|system|gzuncompress|shell|base64|file|proc|preg|call|ini|{:|{$|{~|{-|{+|{/';
  155. $this->assign('filter',$filter);
  156. if (Request()->isPost()) {
  157. $validate = \think\Loader::validate('Token');
  158. if(!$validate->check($param)){
  159. return $this->error($validate->getError());
  160. }
  161. $validate = \think\Loader::validate('Template');
  162. if(!$validate->check($param)){
  163. return $this->error($validate->getError());
  164. }
  165. $fcontent = $param['fcontent'];
  166. $r = mac_reg_replace($fcontent,$filter,"*");
  167. if($fcontent !== $r){
  168. $this->error(lang('admin/template/php_safe_tip'));
  169. return;
  170. }
  171. $res = @fwrite(fopen($fullname,'wb'),$fcontent);
  172. if($res===false){
  173. return $this->error(lang('save_err'));
  174. }
  175. return $this->success(lang('save_ok'));
  176. }
  177. $fcontent = @file_get_contents($fullname);
  178. $fcontent = str_replace('</textarea>','<&#47textarea>',$fcontent);
  179. $this->assign('fname',$fname);
  180. $this->assign('fpath',$fpath);
  181. $this->assign('fcontent',$fcontent);
  182. return $this->fetch('admin@template/info');
  183. }
  184. public function del()
  185. {
  186. $param = input();
  187. $fname = $param['fname'];
  188. if(!empty($fname)){
  189. if(!is_array($fname)){
  190. $fname = [$fname];
  191. }
  192. foreach($fname as $a){
  193. $a = str_replace('\\','/',$a);
  194. if( (substr($a,0,10) != "./template") || count( explode("./",$a) ) > 2) {
  195. }
  196. else{
  197. $a = mac_convert_encoding($a,"UTF-8","GB2312");
  198. if(file_exists($a)){ @unlink($a); }
  199. }
  200. }
  201. }
  202. return $this->success(lang('del_ok'));
  203. }
  204. public function wizard()
  205. {
  206. $this->assign('title',lang('admin/template/wizard/title'));
  207. return $this->fetch('admin@template/wizard');
  208. }
  209. }