TemplateAction.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. *
  4. * Template(模板管理)
  5. *
  6. */
  7. if(!defined("Ainaphp")) exit("Access Denied");
  8. class TemplateAction extends AdminbaseAction
  9. {
  10. protected $filepath,$publicpath;
  11. function _initialize()
  12. {
  13. parent::_initialize();
  14. $this->filepath = TMPL_PATH.'Home/'.$this->sysConfig['DEFAULT_THEME'].'/';
  15. $this->publicpath = TMPL_PATH.'Home/'.$this->sysConfig['DEFAULT_THEME'].'/Public/';
  16. $this->allpath = '';
  17. }
  18. public function index()
  19. {
  20. $exts = array('html','css','js');
  21. $type= $_GET['type'] ? $_GET['type'] : 'html';
  22. if($type=='html'){
  23. $path=$this->filepath;
  24. }else{
  25. $path=$this->allpath.$type.'/';
  26. }
  27. $files = dir_list($path,$type);
  28. foreach ($files as $key=>$file){
  29. $filename = basename($file);
  30. $templates[$key]['value'] = substr($filename,0,strrpos($filename, '.'));
  31. $templates[$key]['filename'] = $filename;
  32. $templates[$key]['filepath'] = $file;
  33. $templates[$key]['filesize']=byte_format(filesize($file));
  34. $templates[$key]['filemtime']=filemtime($file);
  35. $templates[$key]['ext'] = strtolower(substr($filename,strrpos($filename, '.')-strlen($filename)));
  36. }
  37. $this->assign ( 'templates',$templates );
  38. $this->display ();
  39. }
  40. public function images(){
  41. $path = $this->publicpath.'images/'.$_GET['folder'];
  42. $this->assign ( 'Public',$this->publicpath);
  43. $uppath = explode('/',$_GET['folder']);
  44. $leve = count($uppath)-1;;unset($uppath[$leve]);
  45. if($leve>1){
  46. unset($uppath[$leve-1]);
  47. $uppath = implode('/',$uppath).'/';
  48. }else{
  49. $uppath = '';
  50. }
  51. $this->assign ( 'leve',$leve);
  52. $this->assign ( 'uppath',$uppath);
  53. if($_GET['delete']){
  54. $file=$path.$_GET['filename'];
  55. if(file_exists($file)){
  56. is_dir($file) ? dir_delete($file) : unlink($file);
  57. $this->success(L('delete_ok'));
  58. }else{
  59. $this->error(L('file_no_find'));
  60. }
  61. }
  62. $files = glob($path.'*');
  63. $folders=array();
  64. foreach($files as $key => $file) {
  65. $filename = basename($file);
  66. if(is_dir($file)){
  67. $folders[$key]['filename'] = $filename;
  68. $folders[$key]['filepath'] = $file;
  69. $folders[$key]['ext'] = 'folder';
  70. }else{
  71. $templates[$key]['filename'] = $filename;
  72. $templates[$key]['filepath'] = $file;
  73. $templates[$key]['ext'] = strtolower(substr($filename,strrpos($filename, '.')-strlen($filename)+1));
  74. if(!in_array($templates[$key]['ext'],array('gif','jpg','png','bmp'))) $templates[$key]['ico'] =1;
  75. }
  76. }
  77. $this->assign ( 'path',$path);
  78. $this->assign ( 'folders',$folders );
  79. $this->assign ( 'files',$templates );
  80. $this->display ();
  81. }
  82. public function _before_add()
  83. {
  84. if (!is_writable($this->filepath)) $this->error(L('file_no_find'));
  85. }
  86. public function edit(){
  87. $exts = array('html','css','js');
  88. $filename = $_REQUEST['file'];
  89. if($_POST['type']){
  90. $type = $_POST['type'];
  91. }else{
  92. $type = strtolower(substr($filename,strrpos($filename, '.')-strlen($filename)+1));
  93. }
  94. $path = $type=='html' ? $this->filepath : $this->allpath.$type.'/';
  95. $file = $path.$filename;
  96. if($_REQUEST['dosubmit']){
  97. if(C('TOKEN_ON') && !M()->autoCheckToken($_POST))$this->error (L('_TOKEN_ERROR_'));
  98. if($_POST['type']){
  99. $file = $path.$filename.'.'.$type;
  100. file_put_contents($file,htmlspecialchars_decode(stripslashes($_POST['content'])));
  101. $this->assign('jumpUrl',U($module_name.'/index?type='.$type));
  102. $this->success(L('add_ok'));
  103. }else{
  104. if(file_exists($file)){
  105. file_put_contents($file,htmlspecialchars_decode(stripslashes($_POST['content'])));
  106. $this->success(L('edit_ok'));
  107. }else{
  108. $this->error(L('file_no_find'));
  109. }
  110. }
  111. }else{
  112. if(file_exists($file)){
  113. $content = htmlspecialchars(file_get_contents($file));
  114. $this->assign ( 'filename',$filename );
  115. $this->assign ( 'file',$file );
  116. $this->display ();
  117. echo '<textarea id="contentbox" style="display:none;" >'.$content.'</textarea><script>$("#content").val($("#contentbox").val());</script>';
  118. }else{
  119. $this->error(L('file_no_find'));
  120. }
  121. }
  122. }
  123. public function delete(){
  124. $exts = array('html','css','js');
  125. $filename = $_REQUEST['file'];
  126. $type = strtolower(substr($filename,strrpos($filename, '.')-strlen($filename)+1));
  127. $path = $type=='html' ? $path=$this->filepath : $this->publicpath.$type.'/';
  128. $file = $path.$filename;
  129. if(file_exists($file)){
  130. unlink($file);
  131. $this->assign('jumpUrl',U($module_name.'/index?type='.$type));
  132. $this->success(L('delete_ok'));
  133. }else{
  134. $this->assign('jumpUrl',U($module_name.'/index?type='.$type));
  135. $this->error(L('file_no_find'));
  136. }
  137. }
  138. public function config(){
  139. $lang= APP_LANG ? LANG_NAME : $this->sysConfig['DEFAULT_LANG'];
  140. if($_GET['isajax']){
  141. if(empty($_POST['value'])){ echo '0';exit;}
  142. $data = F('config_'.$lang, $value='', $this->filepath);
  143. $data[$_POST['key']]=$_POST['value'];
  144. $r = F('config_'.$lang, $data, $this->filepath);
  145. echo $r ? 1 : 0;
  146. exit;
  147. }
  148. if($_POST['dosubmit']){
  149. $file= $_REQUEST['file'];
  150. unset($_POST[C('TOKEN_NAME')]);
  151. unset($_POST['dosubmit']);
  152. // strtoupper
  153. foreach($_POST as $key=>$r){
  154. if($r)$data[strtolower($key)]=$r;
  155. }
  156. $r = F('config_'.$lang, $data, $this->filepath);
  157. if($r){
  158. $this->success(L('do_ok'));
  159. }else{
  160. $this->error(L('add_error'));
  161. }
  162. }else{
  163. $data = F('config_'.$lang, $value='', $this->filepath);
  164. $this->assign ( 'list', $data );
  165. }
  166. $this->display ();
  167. }
  168. }
  169. ?>