file.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. pc_base::load_app_class('admin', 'admin', 0);
  4. pc_base::load_sys_class('form', '', 0);
  5. pc_base::load_sys_class('format', '', 0);
  6. class file extends admin {
  7. //模板文件夹
  8. private $filepath;
  9. //风格名
  10. private $style;
  11. //风格属性
  12. private $style_info;
  13. //是否允许在线编辑模板
  14. private $tpl_edit;
  15. public function __construct() {
  16. $this->style = isset($_GET['style']) && trim($_GET['style']) ? str_replace(array('..\\', '../', './', '.\\', '/', '\\'), '', trim($_GET['style'])) : showmessage(L('illegal_operation'), HTTP_REFERER);
  17. if (empty($this->style)) {
  18. showmessage(L('illegal_operation'), HTTP_REFERER);
  19. }
  20. $this->filepath = PC_PATH.'templates'.DIRECTORY_SEPARATOR.$this->style.DIRECTORY_SEPARATOR;
  21. if (file_exists($this->filepath.'config.php')) {
  22. $this->style_info = include $this->filepath.'config.php';
  23. if (!isset($this->style_info['name'])) $this->style_info['name'] = $this->style;
  24. }
  25. $this->tpl_edit = pc_base::load_config('system', 'tpl_edit');
  26. parent::__construct();
  27. }
  28. public function init() {
  29. $dir = isset($_GET['dir']) && trim($_GET['dir']) ? str_replace(array('..\\', '../', './', '.\\', '/', '\\'), '', trim($_GET['dir'])) : '';
  30. $filepath = $this->filepath.$dir;
  31. $list = glob($filepath.DIRECTORY_SEPARATOR.'*');
  32. if(!empty($list)) ksort($list);
  33. $local = str_replace(array(PC_PATH, DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR), array('',DIRECTORY_SEPARATOR), $filepath);
  34. if (substr($local, -1, 1) == '.') {
  35. $local = substr($local, 0, (strlen($local)-1));
  36. }
  37. $encode_local = str_replace(array('/', '\\'), '|', $local);
  38. $file_explan = $this->style_info['file_explan'];
  39. $show_header = true;
  40. $tpl_edit = $this->tpl_edit;
  41. include $this->admin_tpl('file_list');
  42. }
  43. public function updatefilename() {
  44. $file_explan = isset($_POST['file_explan']) ? $_POST['file_explan'] : '';
  45. if (!isset($this->style_info['file_explan'])) $this->style_info['file_explan'] = array();
  46. $this->style_info['file_explan'] = array_merge($this->style_info['file_explan'], $file_explan);
  47. @file_put_contents($this->filepath.'config.php', '<?php return '.var_export($this->style_info, true).';?>');
  48. showmessage(L('operation_success'), HTTP_REFERER);
  49. }
  50. public function edit_file() {
  51. if (empty($this->tpl_edit)) {
  52. showmessage(L('tpl_edit'));
  53. }
  54. $dir = isset($_GET['dir']) && trim($_GET['dir']) ? str_replace(array('..\\', '../', './', '.\\'), '', urldecode(trim($_GET['dir']))) : '';
  55. $file = isset($_GET['file']) && trim($_GET['file']) ? trim($_GET['file']) : '';
  56. if ($file) {
  57. preg_match('/^([a-zA-Z0-9])?([^.|-|_]+)/i', $file, $file_t);
  58. $file_t = $file_t[0];
  59. $file_t_v = array('header'=>array('{$SEO[\'title\']}'=>L('seo_title'), '{$SEO[\'site_title\']}'=>L('site_title'), '{$SEO[\'keyword\']}'=>L('seo_keyword'), '{$SEO[\'description\']}'=>L('seo_des')), 'category'=>array('{$catid}'=>L('cat_id'), '{$catname}'=>L('cat_name'), '{$url}'=>L('cat_url'), '{$r[catname]}'=>L('cat_name'), '{$r[url]}'=>'URL', '{$CATEGORYS}'=>L('cats')), 'list'=>array('{$catid}'=>L('cat_id'), '{$catname}'=>L('cat_name'), '{$url}'=>L('cat_url'), '{$CATEGORYS}'=>L('cats')), 'show'=> array('{$title}'=>L('title'), '{$inputtime}'=>L('inputtime'), '{$copyfrom}'=>L('comeform'), '{$content}'=>L('content'), '{$previous_page[url]}'=>L('pre_url'), '{$previous_page[title]}'=>L('pre_title'), '{$next_page[url]}'=>L('next_url'), '{$next_page[title]}'=>L('next_title')), 'page'=>array('{$CATEGORYS}'=>L('cats'), '{$content}'=>L('content')));
  60. }
  61. if (substr($file, -4, 4) != 'html') showmessage(L("can_edit_html_files"));
  62. $filepath = $this->filepath.$dir.DIRECTORY_SEPARATOR.$file;
  63. $is_write = 0;
  64. if (is_writable($filepath)) {
  65. $is_write = 1;
  66. }
  67. if ($_POST['dosubmit']) {
  68. $code = isset($_POST['code']) ? stripslashes($_POST['code']) : showmessage(L('illegal_operation'), HTTP_REFERER);
  69. $code = str_replace(array('<?','{php'),array('<?','{ php'),$code);
  70. if ($is_write == 1) {
  71. pc_base::load_app_func('global');
  72. creat_template_bak($filepath, $this->style, $dir);
  73. file_put_contents($filepath,htmlspecialchars_decode($code));
  74. showmessage(L('operation_success'), HTTP_REFERER);
  75. } else{
  76. showmessage(L("file_does_not_writable"), HTTP_REFERER);
  77. }
  78. } else {
  79. if (file_exists($filepath)) {
  80. $data = new_html_special_chars(file_get_contents($filepath));
  81. } else {
  82. showmessage(L('file_does_not_exists'));
  83. }
  84. }
  85. $show_header = true;
  86. include $this->admin_tpl('file_edit_file');
  87. }
  88. public function add_file() {
  89. if (empty($this->tpl_edit)) {
  90. showmessage(L('tpl_edit'));
  91. }
  92. $dir = isset($_GET['dir']) && trim($_GET['dir']) ? str_replace(array('..\\', '../', './', '.\\'), '', urldecode(trim($_GET['dir']))) : '';
  93. $filepath = $this->filepath.$dir.DIRECTORY_SEPARATOR;
  94. $is_write = 0;
  95. if (is_writable($filepath)) {
  96. $is_write = 1;
  97. }
  98. if (!$is_write) {
  99. showmessage('dir_not_writable');
  100. }
  101. if ($_POST['dosubmit']) {
  102. $name = isset($_POST['name']) && trim($_POST['name']) ? trim($_POST['name']) : showmessage('');
  103. if (!preg_match('/^[\w]+$/i', $name)) {
  104. showmessage(L('name_datatype_error'), HTTP_REFERER);
  105. }
  106. if ($is_write == 1) {
  107. @file_put_contents($filepath.$name.'.html','');
  108. showmessage('','','', 'add_file');
  109. } else {
  110. showmessage(L("dir_not_writable"), HTTP_REFERER);
  111. }
  112. }
  113. $show_header = $show_validator = true;
  114. include $this->admin_tpl('file_add_file');
  115. }
  116. public function public_name() {
  117. $dir = isset($_GET['dir']) && trim($_GET['dir']) ? str_replace(array('..\\', '../', './', '.\\'), '', urldecode(trim($_GET['dir']))) : '';
  118. $name = isset($_GET['name']) && trim($_GET['name']) ? (pc_base::load_config('system', 'charset') == 'gbk' ? iconv('utf-8', 'gbk', trim($_GET['name'])) : trim($_GET['name'])) : exit('0');
  119. $filepath = $this->filepath.$dir.DIRECTORY_SEPARATOR.$name.'.html';
  120. if (file_exists($filepath)) {
  121. exit('0');
  122. } else {
  123. exit('1');
  124. }
  125. }
  126. public function visualization() {
  127. $dir = isset($_GET['dir']) && trim($_GET['dir']) ? str_replace(array('..\\', '../', './', '.\\'), '', urldecode(trim($_GET['dir']))) : showmessage(L('illegal_operation'), HTTP_REFERER);
  128. $file = isset($_GET['file']) && trim($_GET['file']) ? trim($_GET['file']) : showmessage(L('illegal_operation'), HTTP_REFERER);
  129. ob_start();
  130. //include $this->admin_tpl('base_tool');
  131. include template($dir,basename($file, '.html'),$this->style);
  132. $html = ob_get_contents();
  133. ob_clean();
  134. pc_base::load_app_func('global');
  135. $html = visualization($html, $this->style, $dir, $file);
  136. echo $html;
  137. }
  138. public function public_ajax_get() {
  139. $op_tag = pc_base::load_app_class($_GET['op']."_tag", $_GET['op']);
  140. $html = $op_tag->{$_GET['action']}($_GET['html'], $_GET['value'], $_GET['id']);
  141. echo $html;
  142. }
  143. public function edit_pc_tag() {
  144. if (empty($this->tpl_edit)) {
  145. showmessage(L('tpl_edit'));
  146. }
  147. $dir = isset($_GET['dir']) && trim($_GET['dir']) ? str_replace(array('..\\', '../', './', '.\\'), '', urldecode(trim($_GET['dir']))) : showmessage(L('illegal_operation'));
  148. $file = isset($_GET['file']) && trim($_GET['file']) ? urldecode(trim($_GET['file'])) : showmessage(L('illegal_operation'));
  149. $op = isset($_GET['op']) && trim($_GET['op']) ? trim($_GET['op']) : showmessage(L('illegal_operation'));
  150. $tag_md5 = isset($_GET['tag_md5']) && trim($_GET['tag_md5']) ? trim($_GET['tag_md5']) : showmessage(L('illegal_operation'));
  151. $show_header = $show_scroll = $show_validator = true;
  152. pc_base::load_app_func('global');
  153. pc_base::load_sys_class('form', '', 0);
  154. $filepath = $this->filepath.$dir.DIRECTORY_SEPARATOR.$file;
  155. switch ($op) {
  156. case 'xml':
  157. case 'json':
  158. if ($_POST['dosubmit']) {
  159. $url = isset($_POST['url']) && trim($_POST['url']) ? trim($_POST['url']) : showmessage(L('data_address').L('empty'));
  160. $cache = isset($_POST['cache']) && trim($_POST['cache']) ? trim($_POST['cache']) : 0;
  161. $return = isset($_POST['return']) && trim($_POST['return']) ? trim($_POST['return']) : '';
  162. if (!preg_match('/http:\/\//i', $url)) {
  163. showmessage(L('data_address_reg_sg'), HTTP_REFERER);
  164. }
  165. $tag_md5_list = tag_md5($filepath);
  166. $pc_tag = creat_pc_tag($op, array('url'=>$url, 'cache'=>$cache, 'return'=>$return));
  167. if (in_array($tag_md5, $tag_md5_list[0])) {
  168. $old_pc_tag = $tag_md5_list[1][$tag_md5];
  169. }
  170. if (replace_pc_tag($filepath, $old_pc_tag, $pc_tag, $this->style, $dir)) {
  171. showmessage('<script style="text/javascript">if(!window.top.right){parent.location.reload();}art.dialog({id:"edit"}).close();</script>', '', '', 'edit');
  172. } else {
  173. showmessage(L('failure_the_document_may_not_to_write'));
  174. }
  175. }
  176. include $this->admin_tpl('pc_tag_tools_json_xml');
  177. break;
  178. case 'get':
  179. if ($_POST['dosubmit']) {
  180. $sql = isset($_POST['sql']) && trim($_POST['sql']) ? trim($_POST['sql']) : showmessage('SQL'.L('empty'));
  181. $dbsource = isset($_POST['dbsource']) && trim($_POST['dbsource']) ? trim($_POST['dbsource']) : '';
  182. $cache = isset($_POST['cache']) && intval($_POST['cache']) ? intval($_POST['cache']) : 0;
  183. $return = isset($_POST['return']) && trim($_POST['return']) ? trim($_POST['return']) : '';
  184. $tag_md5_list = tag_md5($filepath);
  185. $pc_tag = creat_pc_tag($op, array('sql'=>$sql, 'dbsource'=>$dbsource, 'cache'=>$cache, 'return'=>$return));
  186. if (in_array($tag_md5, $tag_md5_list[0])) {
  187. $old_pc_tag = $tag_md5_list[1][$tag_md5];
  188. }
  189. if (replace_pc_tag($filepath, $old_pc_tag, $pc_tag, $this->style, $dir)) {
  190. showmessage('<script style="text/javascript">if(!window.top.right){parent.location.reload();}art.dialog({id:"edit"}).close();</script>', '', '', 'edit');
  191. } else {
  192. showmessage(L('failure_the_document_may_not_to_write'));
  193. }
  194. }
  195. $dbsource_db = pc_base::load_model('dbsource_model');
  196. $r = $dbsource_db->select('', 'name');
  197. $dbsource_list = array(''=>L('please_select'));
  198. foreach ($r as $v) {
  199. $dbsource_list[$v['name']] = $v['name'];
  200. }
  201. include $this->admin_tpl('pc_tag_tools_get');
  202. break;
  203. default:
  204. if (!file_exists(PC_PATH.DIRECTORY_SEPARATOR.'modules'.DIRECTORY_SEPARATOR.$op.DIRECTORY_SEPARATOR.'classes'.DIRECTORY_SEPARATOR.$op.'_tag.class.php')) {
  205. showmessage(L('the_module_will_not_support_the_operation'));
  206. }
  207. $op_tag = pc_base::load_app_class($op."_tag", $op);
  208. if (!method_exists($op_tag, 'pc_tag')) {
  209. showmessage(L('the_module_will_not_support_the_operation'));
  210. }
  211. $html = $op_tag->pc_tag();
  212. if ($_POST['dosubmit']) {
  213. $action = isset($_POST['action']) && trim($_POST['action']) ? trim($_POST['action']) : 0;
  214. $data = array('action'=>$action);
  215. if (isset($html[$action]) && is_array($html[$action])) {
  216. foreach ($html[$action] as $key=>$val) {
  217. $val['validator']['reg_msg'] = $val['validator']['reg_msg'] ? $val['validator']['reg_msg'] : $val['name'].L('inputerror');
  218. if ($val['htmltype'] != 'checkbox') {
  219. $$key = isset($_POST[$key]) && trim($_POST[$key]) ? trim($_POST[$key]) : '';
  220. } else {
  221. $$key = isset($_POST[$key]) && $_POST[$key] ? implode(',', $_POST[$key]) : '';
  222. }
  223. if (isset($val['ajax']['id']) && !empty($val['ajax']['id'])) {
  224. $data[$val['ajax']['id']] = isset($_POST[$val['ajax']['id']]) && trim($_POST[$val['ajax']['id']]) ? trim($_POST[$val['ajax']['id']]) : '';
  225. }
  226. if (!empty($val['validator'])) {
  227. if (isset($val['validator']['min']) && strlen($$key) < $val['validator']['min']) {
  228. showmessage($val['name'].L('should').L('is_greater_than').$val['validator']['min'].L('lambda'));
  229. }
  230. if (isset($val['validator']['max']) && strlen($$key) > $val['validator']['max']) {
  231. showmessage($val['name'].L('should').L('less_than').$val['validator']['max'].L('lambda'));
  232. }
  233. if (!preg_match('/'.$val['validator']['reg'].'/'.$val['validator']['reg_param'], $$key)) {
  234. showmessage($val['name'].$val['validator']['reg_msg']);
  235. }
  236. }
  237. $data[$key] = $$key;
  238. }
  239. }
  240. $page = isset($_POST['page']) && trim($_POST['page']) ? trim($_POST['page']) : '';
  241. $num = isset($_POST['num']) && intval($_POST['num']) ? intval($_POST['num']) : 0;
  242. $return = isset($_POST['return']) && trim($_POST['return']) ? trim($_POST['return']) : '';
  243. $cache = isset($_POST['cache']) && intval($_POST['cache']) ? intval($_POST['cache']) : 0;
  244. $data['page'] = $page;
  245. $data['num'] = $num;
  246. $data['return'] = $return;
  247. $data['cache'] = $cache;
  248. $tag_md5_list = tag_md5($filepath);
  249. $pc_tag = creat_pc_tag($op, $data);
  250. if (in_array($tag_md5, $tag_md5_list[0])) {
  251. $old_pc_tag = $tag_md5_list[1][$tag_md5];
  252. }
  253. if(!file_exists($filepath)) showmessage($filepath.L('file_does_not_exists'));
  254. if (replace_pc_tag($filepath, $old_pc_tag, $pc_tag, $this->style, $dir)) {
  255. showmessage(L('operation_success').'<script style="text/javascript">top.art.dialog({id:"edit"}).close();</script>', '', '', 'edit');
  256. } else {
  257. showmessage(L('failure_the_document_may_not_to_write'));
  258. }
  259. }
  260. include $this->admin_tpl('pc_tag_modules');
  261. break;
  262. }
  263. }
  264. }