Config.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | config.php 后台管理 基本配置管理
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace DeverApp\Manage;
  8. use Dever;
  9. use Dever\Security\Internal;
  10. class Config
  11. {
  12. /**
  13. * project
  14. *
  15. * @var string
  16. */
  17. private $project;
  18. /**
  19. * dir
  20. *
  21. * @var string
  22. */
  23. protected $type;
  24. /**
  25. * config
  26. *
  27. * @var array
  28. */
  29. protected $config;
  30. /**
  31. * __construct
  32. *
  33. * @return mixed
  34. */
  35. public function __construct()
  36. {
  37. $this->project = Dever::input('key');
  38. $this->type = Dever::input('type', 'config');
  39. $config = Dever::load('manage/project.get');
  40. if(isset($config[$this->project]))
  41. {
  42. $this->config = $config[$this->project];
  43. }
  44. $type = array('config', 'view', 'database', 'api');
  45. if(!in_array($this->type, $type))
  46. {
  47. Dever::out('error');
  48. }
  49. }
  50. /**
  51. * get
  52. *
  53. * @return mixed
  54. */
  55. public function get()
  56. {
  57. $dir = Dever::input('dir');
  58. $name = Dever::input('name');
  59. if($dir && $name)
  60. {
  61. $dir = $dir . '/' . $name;
  62. }
  63. else
  64. {
  65. $dir = $this->type;
  66. }
  67. Dever::setInput('dir', $dir);
  68. $result = $this->read($dir);
  69. return $result;
  70. }
  71. /**
  72. * read
  73. * @param string $dir
  74. *
  75. * @return mixed
  76. */
  77. private function read($dir)
  78. {
  79. $file = $this->dir($dir);
  80. if(is_dir($file))
  81. {
  82. $result = scandir($file);
  83. foreach($result as $k => $v)
  84. {
  85. if($v == '.' || $v == '..')
  86. {
  87. unset($result[$k]);
  88. }
  89. }
  90. return $result;
  91. }
  92. elseif(is_file($file))
  93. {
  94. Dever::$global['temp']['delete'] = '';
  95. $data_dir = $this->dir($dir, 'data/');
  96. if(is_file($data_dir))
  97. {
  98. $file = $data_dir;
  99. $name = str_replace(DEVER_PATH, '', $file);
  100. $md5 = Internal::encode($name);
  101. $delete = Dever::url('config.del?file=' . $name . '&md5=' . $md5);
  102. Dever::$global['temp']['delete'] = '<br /><a href="'.$delete.'" class="oper_6">点此删除</a>';
  103. }
  104. else
  105. {
  106. $name = str_replace(DEVER_PATH, '', $file);
  107. $md5 = Internal::encode($name);
  108. }
  109. $result[] = array('content' => file_get_contents($file), 'file' => $name, 'md5' => $md5, 'key' => $this->project);
  110. return $result;
  111. }
  112. else
  113. {
  114. Dever::out('error');
  115. }
  116. }
  117. /**
  118. * dir
  119. * @param string $dir
  120. *
  121. * @return mixed
  122. */
  123. private function dir($dir, $path = '')
  124. {
  125. if(!$this->config['path'])
  126. {
  127. }
  128. if($path)
  129. {
  130. return Dever::path(DEVER_PATH . $path, $this->config['path'] . $dir);
  131. }
  132. return DEVER_PATH . $this->config['path'] . $dir;
  133. }
  134. /**
  135. * set 将内容写入到配置文件中(备份)
  136. *
  137. * @return string
  138. */
  139. public function set()
  140. {
  141. $file = Dever::input('file');
  142. $value = Dever::input('value');
  143. $name = Dever::input('name');
  144. $md5 = Internal::decode(Dever::input('md5'));
  145. if($file == $md5)
  146. {
  147. $file = DEVER_PATH . $file;
  148. if(is_file($file))
  149. {
  150. file_put_contents($this->dir($name, 'data/'), $value);
  151. }
  152. }
  153. highlight_string($value);die;
  154. }
  155. /**
  156. * del 删除后台建立的内容
  157. *
  158. * @return mixed
  159. */
  160. public function del()
  161. {
  162. $file = Dever::input('file');
  163. $md5 = Internal::decode(Dever::input('md5'));
  164. if(strpos($file, 'data/') !== false && $file == $md5)
  165. {
  166. $file = DEVER_PATH . $file;
  167. if(is_file($file))
  168. {
  169. @unlink($file);
  170. }
  171. }
  172. Dever::out('yes');
  173. }
  174. }