control.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * The control file of editor of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL (http://zpl.pub/page/zplv12.html)
  7. * @author Yidong Wang <yidong@cnezsoft.com>
  8. * @package editor
  9. * @version $Id$
  10. * @link https://www.zentao.net
  11. */
  12. class editor extends control
  13. {
  14. /**
  15. * Construct
  16. *
  17. * @param string $module
  18. * @param string $method
  19. * @access public
  20. * @return void
  21. */
  22. public function __construct($module = '', $method = '')
  23. {
  24. parent::__construct($module, $method);
  25. if($this->app->getMethodName() != 'turnon' and empty($this->config->global->editor)) $this->locate($this->createLink('dev', 'editor'));
  26. }
  27. /**
  28. * Show module files and edit them.
  29. *
  30. * @access public
  31. * @return void
  32. * @param string $type
  33. */
  34. public function index($type = 'editor')
  35. {
  36. $this->app->loadLang('dev');
  37. $this->view->title = $this->lang->editor->common;
  38. $this->view->tab = $type;
  39. $this->view->moduleTree = $this->loadModel('dev')->getTree($type, 'module');
  40. $this->display();
  41. }
  42. /**
  43. * Show files and methods of the module.
  44. *
  45. * @param string $moduleDir
  46. * @access public
  47. * @return void
  48. */
  49. public function extend($moduleDir = '')
  50. {
  51. if(!isset($this->lang->{$moduleDir}->common)) $this->app->loadLang($moduleDir);
  52. $moduleFiles = $this->editor->getModuleFiles($moduleDir);
  53. $this->view->module = $moduleDir;
  54. $this->view->tree = $this->editor->printTree($moduleFiles);
  55. $this->display();
  56. }
  57. /**
  58. * Edit extend.
  59. *
  60. * @param string $filePath
  61. * @param string $action
  62. * @param string $isExtends
  63. * @access public
  64. * @return void
  65. */
  66. public function edit($filePath = '', $action = '', $isExtends = '')
  67. {
  68. $this->view->safeFilePath = $filePath;
  69. $fileContent = '';
  70. $extension = 'php';
  71. if($filePath)
  72. {
  73. $filePath = helper::safe64Decode($filePath);
  74. $realPath = realpath($filePath);
  75. if($realPath) $filePath = $realPath;
  76. if(strpos(strtolower($filePath), strtolower($this->app->getBasePath())) !== 0) return $this->send(array('result' => 'fail', 'message' => $this->lang->editor->editFileError));
  77. if($action == 'extendOther' and file_exists($filePath)) $this->view->showContent = file_get_contents($filePath);
  78. if(($action == 'edit' or $action == 'override') && !file_exists($filePath)) $filePath = '';
  79. if($action == 'extendControl' and empty($isExtends))
  80. {
  81. $okUrl = $this->editor->getExtendLink($filePath, 'extendControl', 'yes');
  82. $cancelUrl = $this->editor->getExtendLink($filePath, 'extendControl', 'no');
  83. return print(js::confirm($this->lang->editor->extendConfirm, $okUrl, $cancelUrl));
  84. }
  85. $fileContent = $this->editorZen->buildContentByAction($filePath, $action, $isExtends);
  86. $fileName = basename($filePath);
  87. if(strpos($fileName, '.') !== false) $extension = substr($fileName, strpos($fileName, '.') + 1);
  88. if(strtolower($action) == 'newjs') $extension = 'js';
  89. if(strtolower($action) == 'newcss') $extension = 'css';
  90. }
  91. $this->view->fileContent = $fileContent;
  92. $this->view->filePath = $filePath;
  93. $this->view->fileExtension = $extension;
  94. $this->view->action = $action;
  95. $this->display();
  96. }
  97. /**
  98. * Set Page name.
  99. *
  100. * @param string $filePath
  101. * @access public
  102. * @return void
  103. */
  104. public function newPage($filePath)
  105. {
  106. $filePath = helper::safe64Decode($filePath);
  107. if($_POST)
  108. {
  109. $saveFilePath = $this->editor->getSavePath($filePath, 'newMethod');
  110. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => implode(dao::getError())));
  111. $extendLink = $this->editor->getExtendLink($saveFilePath, 'newPage');
  112. if(file_exists($saveFilePath) and !$this->post->override) return $this->send(array('result' => 'success', 'callback' => "zui.Modal.confirm('{$this->lang->editor->repeatPage}').then((res) => {if(res) loadPage('{$extendLink}');});"));
  113. return $this->send(array('result' => 'success', 'callback' => "openInEditWin('{$extendLink}')"));
  114. }
  115. $this->view->filePath = $filePath;
  116. $this->display();
  117. }
  118. /**
  119. * Save file to extension.
  120. *
  121. * @param string $filePath
  122. * @access public
  123. * @return void
  124. * @param string $action
  125. */
  126. public function save($filePath = '', $action = '')
  127. {
  128. if($filePath and $_POST)
  129. {
  130. $filePath = helper::safe64Decode($filePath);
  131. if(strpos(strtolower($filePath), strtolower($this->app->getBasePath())) !== 0) return $this->send(array('result' => 'fail', 'message' => $this->lang->editor->editFileError));
  132. $fileName = empty($_POST['fileName']) ? '' : trim($this->post->fileName);
  133. if($action != 'edit' and empty($fileName)) return $this->send(array('result' => 'fail', 'message' => $this->lang->editor->emptyFileName));
  134. if($action != 'edit' and $action != 'newPage')
  135. {
  136. $filePath = $this->editor->getSavePath($filePath, $action);
  137. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => implode(dao::getError())));
  138. }
  139. if($action != 'edit' and $action != 'newPage' and file_exists($filePath) and !$this->post->override) return $this->send(array('result' => 'fail', 'message' => $this->lang->editor->repeatFile));
  140. $result = $this->editor->save($filePath);
  141. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => implode(dao::getError())));
  142. return $this->send(array('result' => 'success', 'load' => inlink('edit', "filePath=" . helper::safe64Encode($filePath) . "&action=edit"), 'callback' => 'reloadExtendWin()'));
  143. }
  144. }
  145. /**
  146. * Delete extension file.
  147. *
  148. * @param string $filePath
  149. * @access public
  150. * @return void
  151. */
  152. public function delete($filePath = '')
  153. {
  154. $filePath = helper::safe64Decode($filePath);
  155. $extensionPath = $this->app->getExtensionRoot() . 'custom' . DS;
  156. if(strpos(strtolower($filePath), strtolower($extensionPath)) !== 0) return $this->send(array('result' => 'fail', 'message' => $this->lang->editor->notDelete));
  157. if(file_exists($filePath) and unlink($filePath)) return $this->send(array('load' => true));
  158. return $this->send(array('result' => 'fail', 'message' => $this->lang->editor->notDelete));
  159. }
  160. /**
  161. * Switch editor feature.
  162. *
  163. * @param string $status 1|0
  164. * @access public
  165. * @return void
  166. */
  167. public function turnon($status)
  168. {
  169. $this->loadModel('setting')->setItem('system.common.global.editor', $status);
  170. $link = empty($status) ? $this->createLink('dev', 'editor') : $this->createLink('editor', 'index');
  171. return $this->send(array('result' => 'success', 'load' => $link));
  172. }
  173. }