style.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. pc_base::load_app_class('admin', 'admin', 0);
  4. class style extends admin {
  5. //模板文件夹
  6. private $filepath;
  7. public function __construct() {
  8. $this->filepath = PC_PATH.'templates'.DIRECTORY_SEPARATOR;
  9. parent::__construct();
  10. }
  11. public function init() {
  12. pc_base::load_app_func('global', 'admin');
  13. $list = template_list('', 1);
  14. $big_menu = array('javascript:window.top.art.dialog({id:\'import\',iframe:\'?m=template&c=style&a=import\', title:\''.L('import_style').'\', width:\'500\', height:\'250\', lock:true}, function(){var d = window.top.art.dialog({id:\'import\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'import\'}).close()});void(0);', L('import_style'));
  15. include $this->admin_tpl('style_list');
  16. }
  17. public function disable() {
  18. $style = isset($_GET['style']) && trim($_GET['style']) ? trim($_GET['style']) : showmessage(L('illegal_operation'), HTTP_REFERER);
  19. if (!preg_match('/([a-z0-9_\-]+)/i',$style)) {
  20. showmessage(L('illegal_parameters'), HTTP_REFERER);
  21. }
  22. $filepath = $this->filepath.$style.DIRECTORY_SEPARATOR.'config.php';
  23. if (file_exists($filepath)) {
  24. $arr = include $filepath;
  25. if (!isset($arr['disable'])) {
  26. $arr['disable'] = 1;
  27. } else {
  28. if ($arr['disable'] ==1 ) {
  29. $arr['disable'] = 0;
  30. } else {
  31. $arr['disable'] = 1;
  32. }
  33. }
  34. if (is_writable($filepath)) {
  35. file_put_contents($filepath, '<?php return '.var_export($arr, true).';?>');
  36. } else {
  37. showmessage(L('file_does_not_writable'), HTTP_REFERER);
  38. }
  39. } else {
  40. $arr = array('name'=>$style,'disable'=>1, 'dirname'=>$style);
  41. file_put_contents($filepath, '<?php return '.var_export($arr, true).';?>');
  42. }
  43. showmessage(L('operation_success'), HTTP_REFERER);
  44. }
  45. public function export() {
  46. $style = isset($_GET['style']) && trim($_GET['style']) ? trim($_GET['style']) : showmessage(L('illegal_operation'), HTTP_REFERER);
  47. if (!preg_match('/([a-z0-9_\-]+)/i',$style)) {
  48. showmessage(L('illegal_parameters'), HTTP_REFERER);
  49. }
  50. $filepath = $this->filepath.$style.DIRECTORY_SEPARATOR.'config.php';
  51. if (file_exists($filepath)) {
  52. $arr = include $filepath;
  53. if (pc_base::load_config('system', 'charset') == 'gbk') {
  54. $arr = array_iconv($arr);
  55. }
  56. $data = base64_encode(json_encode($arr));
  57. header("Content-type: application/octet-stream");
  58. header("Content-Disposition: attachment; filename=pc_template_".$style.'.txt');
  59. echo $data;
  60. } else {
  61. showmessage(L('file_does_not_exists'), HTTP_REFERER);
  62. }
  63. }
  64. public function import() {
  65. if (isset($_POST['dosubmit'])) {
  66. $type = isset($_POST['type']) && trim($_POST['type']) ? trim($_POST['type']) : showmessage(L('illegal_operation'), HTTP_REFERER);
  67. if ($type == 1) {
  68. $filename = $_FILES['file']['tmp_name'];
  69. if (strtolower(substr($_FILES['file']['name'], -3, 3)) != 'txt') {
  70. showmessage(L('only_allowed_to_upload_txt_files'), HTTP_REFERER);
  71. }
  72. $code = json_decode(base64_decode(file_get_contents($filename)), true);
  73. if (!preg_match('/([a-z0-9_\-]+)/i',$code['dirname'])) {
  74. showmessage(L('illegal_parameters'), HTTP_REFERER);
  75. }
  76. @unlink($filename);
  77. } elseif ($type == 2) {
  78. $code = isset($_POST['code']) && trim($_POST['code']) ? json_decode(base64_decode(trim($_POST['code'])),true) : showmessage(L('illegal_operation'), HTTP_REFERER);
  79. if (!isset($code['dirname']) && !preg_match('/([a-z0-9_\-]+)/i',$code['dirname'])) {
  80. showmessage(L('illegal_parameters'), HTTP_REFERER);
  81. }
  82. }
  83. if (pc_base::load_config('system', 'charset') == 'gbk') {
  84. $code = array_iconv($code, 'utf-8', 'gbk');
  85. }
  86. //echo $this->filepath.$code['dirname'].DIRECTORY_SEPARATOR.'config.php';
  87. if (!file_exists($this->filepath.$code['dirname'].DIRECTORY_SEPARATOR.'config.php')) {
  88. @mkdir($this->filepath.$code['dirname'].DIRECTORY_SEPARATOR, 0755, true);
  89. if (@is_writable($this->filepath.$code['dirname'].DIRECTORY_SEPARATOR)) {
  90. @file_put_contents($this->filepath.$code['dirname'].DIRECTORY_SEPARATOR.'config.php', '<?php return '.var_export($code, true).';?>');
  91. showmessage(L('operation_success'), HTTP_REFERER, '', 'import');
  92. } else {
  93. showmessage(L('template_directory_not_write'), HTTP_REFERER);
  94. }
  95. } else {
  96. showmessage(L('file_exists'), HTTP_REFERER);
  97. }
  98. } else {
  99. $show_header = true;
  100. include $this->admin_tpl('style_import');
  101. }
  102. }
  103. public function updatename() {
  104. $name = isset($_POST['name']) ? $_POST['name'] : showmessage(L('illegal_operation'), HTTP_REFERER);
  105. if (is_array($name)) {
  106. foreach ($name as $key=>$val) {
  107. $filepath = $this->filepath.$key.DIRECTORY_SEPARATOR.'config.php';
  108. if (file_exists($filepath)) {
  109. $arr = include $filepath;
  110. $arr['name'] = $val;
  111. } else {
  112. $arr = array('name'=>$val,'disable'=>0, 'dirname'=>$key);
  113. }
  114. @file_put_contents($filepath, '<?php return '.var_export($arr, true).';?>');
  115. }
  116. showmessage(L('operation_success'), HTTP_REFERER);
  117. } else {
  118. showmessage(L('illegal_parameters'), HTTP_REFERER);
  119. }
  120. }
  121. }