PageController.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class PageController extends BaseController {
  5. //展示某个项目的单个页面
  6. public function index(){
  7. import("Vendor.Parsedown.Parsedown");
  8. $page_id = I("page_id/d");
  9. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  10. $login_user = $this->checkLogin(false);
  11. if (!$this->checkItemVisit($login_user['uid'] , $page['item_id'])) {
  12. $this->message(L('no_permissions'));
  13. return;
  14. }
  15. $ItemPermn = $this->checkItemPermn($login_user['uid'] , $page['item_id']) ;
  16. $ItemCreator = $this->checkItemCreator($login_user['uid'],$page['item_id']);
  17. $page['page_md_content'] = $page['page_content'];
  18. //$Parsedown = new \Parsedown();
  19. //$page['page_content'] = $Parsedown->text(htmlspecialchars_decode($page['page_content']));
  20. $this->assign("page" , $page);
  21. $this->display();
  22. }
  23. //展示单个页面
  24. public function single(){
  25. import("Vendor.Parsedown.Parsedown");
  26. $page_id = I("page_id/d");
  27. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  28. $login_user = $this->checkLogin(false);
  29. if (!$this->checkItemVisit($login_user['uid'] , $page['item_id'],$_SERVER['REQUEST_URI'])) {
  30. $this->message(L('no_permissions'));
  31. return;
  32. }
  33. $ItemPermn = $this->checkItemPermn($login_user['uid'] , $page['item_id']) ;
  34. $ItemCreator = $this->checkItemCreator($login_user['uid'],$page['item_id']);
  35. $page['page_md_content'] = $page['page_content'];
  36. //$page['page_html_content'] = $Parsedown->text(htmlspecialchars_decode($page['page_content']));
  37. $this->assign("page" , $page);
  38. $this->assign("login_user" , $login_user);
  39. $this->display();
  40. }
  41. //返回单个页面的源markdown代码
  42. public function md(){
  43. $page_id = I("page_id/d");
  44. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  45. echo $page['page_content'];
  46. }
  47. //编辑页面
  48. public function edit(){
  49. $login_user = $this->checkLogin();
  50. $page_id = I("page_id/d");
  51. $item_id = I("item_id/d");
  52. $page_history_id = I("page_history_id/d");
  53. $copy_page_id = I("copy_page_id/d");
  54. if ($page_id > 0 ) {
  55. if ($page_history_id) {
  56. $page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
  57. $page_content = gzuncompress(base64_decode($page['page_content']));
  58. $page['page_content'] = $page_content ? $page_content : $page['page_content'] ;
  59. }else{
  60. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  61. }
  62. $default_cat_id = $page['cat_id'];
  63. }
  64. //如果是复制接口
  65. elseif ($copy_page_id) {
  66. $copy_page = D("Page")->where(" page_id = '$copy_page_id' ")->find();
  67. $page['page_title'] = $copy_page['page_title']."-copy";
  68. $page['page_content'] = $copy_page['page_content'];
  69. $page['item_id'] = $copy_page['item_id'];
  70. $default_cat_id = $copy_page['cat_id'];
  71. }else{
  72. //查找用户上一次设置的目录
  73. $last_page = D("Page")->where(" author_uid ='$login_user[uid]' and $item_id = '$item_id' ")->order(" addtime desc ")->limit(1)->find();
  74. $default_cat_id = $last_page['cat_id'];
  75. }
  76. $item_id = $page['item_id'] ?$page['item_id'] :$item_id;
  77. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  78. $this->message(L('no_permissions'));
  79. return;
  80. }
  81. $Catalog = D("Catalog")->where(" cat_id = '$default_cat_id' ")->find();
  82. if ($Catalog['parent_cat_id']) {
  83. $default_second_cat_id = $Catalog['parent_cat_id'];
  84. $default_child_cat_id = $default_cat_id;
  85. }else{
  86. $default_second_cat_id = $default_cat_id;
  87. }
  88. $this->assign("api_doc_templ" , 'MdTemplate/api-doc.'.LANG_SET);
  89. $this->assign("database_doc_templ" , 'MdTemplate/database.'.LANG_SET);
  90. $this->assign("page" , $page);
  91. $this->assign("item_id" , $item_id);
  92. $this->assign("default_second_cat_id" , $default_second_cat_id);
  93. $this->assign("default_child_cat_id" , $default_child_cat_id);
  94. $this->display();
  95. }
  96. //保存
  97. public function save(){
  98. $login_user = $this->checkLogin();
  99. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  100. $page_title = I("page_title") ?I("page_title") : L("default_title");
  101. $page_comments = I("page_comments") ?I("page_comments") :'';
  102. $page_content = I("page_content");
  103. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  104. $item_id = I("item_id/d")? I("item_id/d") : 0;
  105. $s_number = I("s_number/d")? I("s_number/d") : 99;
  106. $login_user = $this->checkLogin();
  107. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  108. $this->message(L('no_permissions'));
  109. return;
  110. }
  111. $data['page_title'] = $page_title ;
  112. $data['page_content'] = $page_content ;
  113. $data['page_comments'] = $page_comments ;
  114. $data['s_number'] = $s_number ;
  115. $data['item_id'] = $item_id ;
  116. $data['cat_id'] = $cat_id ;
  117. $data['addtime'] = time();
  118. $data['author_uid'] = $login_user['uid'] ;
  119. $data['author_username'] = $login_user['username'];
  120. if ($page_id > 0 ) {
  121. //在保存前先把当前页面的版本存档
  122. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  123. $insert_history = array(
  124. 'page_id'=>$page['page_id'],
  125. 'item_id'=>$page['item_id'],
  126. 'cat_id'=>$page['cat_id'],
  127. 'page_title'=>$page['page_title'],
  128. 'page_comments'=>$page['page_comments'],
  129. 'page_content'=>base64_encode( gzcompress($page['page_content'], 9)),
  130. 's_number'=>$page['s_number'],
  131. 'addtime'=>$page['addtime'],
  132. 'author_uid'=>$page['author_uid'],
  133. 'author_username'=>$page['author_username'],
  134. );
  135. D("PageHistory")->add($insert_history);
  136. $ret = D("Page")->where(" page_id = '$page_id' ")->save($data);
  137. //统计该page_id有多少历史版本了
  138. $Count = D("PageHistory")->where(" page_id = '$page_id' ")->Count();
  139. if ($Count > 20 ) {
  140. //每个单页面只保留最多20个历史版本
  141. $ret = D("PageHistory")->where(" page_id = '$page_id' ")->limit("20")->order("page_history_id desc")->select();
  142. D("PageHistory")->where(" page_id = '$page_id' and page_history_id < ".$ret[19]['page_history_id'] )->delete();
  143. }
  144. //如果是单页项目,则将页面标题设置为项目名
  145. $item_array = D("Item")->where(" item_id = '$item_id' ")->find();
  146. if ($item_array['item_type'] == 2 ) {
  147. D("Item")->where(" item_id = '$item_id' ")->save(array("last_update_time"=>time(),"item_name"=>$page_title));
  148. }else{
  149. D("Item")->where(" item_id = '$item_id' ")->save(array("last_update_time"=>time()));
  150. }
  151. $return = D("Page")->where(" page_id = '$page_id' ")->find();
  152. }else{
  153. $page_id = D("Page")->add($data);
  154. //更新项目时间
  155. D("Item")->where(" item_id = '$item_id' ")->save(array("last_update_time"=>time()));
  156. $return = D("Page")->where(" page_id = '$page_id' ")->find();
  157. }
  158. if (!$return) {
  159. $return['error_code'] = 10103 ;
  160. $return['error_message'] = 'request fail' ;
  161. }
  162. $this->sendResult($return);
  163. }
  164. //删除页面
  165. public function delete(){
  166. $page_id = I("page_id/d")? I("page_id/d") : 0;
  167. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  168. $login_user = $this->checkLogin();
  169. if (!$this->checkItemCreator($login_user['uid'] , $page['item_id']) && $login_user['uid'] != $page['author_uid']) {
  170. $this->message(L('no_permissions_to_delete_page',array("author_username"=>$page['author_username'])));
  171. return;
  172. }
  173. if ($page) {
  174. $ret = D("Page")->where(" page_id = '$page_id' ")->delete();
  175. //更新项目时间
  176. D("Item")->where(" item_id = '$page[item_id]' ")->save(array("last_update_time"=>time()));
  177. }
  178. if ($ret) {
  179. $this->message(L('delete_succeeded'),U("Home/item/show?item_id={$page['item_id']}"));
  180. }else{
  181. $this->message(L('delete_failed'),U("Home/item/show?item_id={$page['item_id']}"));
  182. }
  183. }
  184. //历史版本
  185. public function history(){
  186. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  187. $this->assign("page_id" , $page_id);
  188. $PageHistory = D("PageHistory")->where("page_id = '$page_id' ")->order(" addtime desc")->limit(10)->select();
  189. if ($PageHistory) {
  190. foreach ($PageHistory as $key => &$value) {
  191. $page_content = gzuncompress(base64_decode($value['page_content']));
  192. $value['page_content'] = $page_content ? $page_content : $value['page_content'] ;
  193. $value['addtime'] = date("Y-m-d H:i:s" , $value['addtime']);
  194. }
  195. }
  196. $this->assign("PageHistory" , $PageHistory);
  197. $this->display();
  198. }
  199. //上传图片
  200. public function uploadImg(){
  201. $qiniu_config = C('UPLOAD_SITEIMG_QINIU') ;
  202. if ($_FILES['editormd-image-file']['name'] == 'blob') {
  203. $_FILES['editormd-image-file']['name'] .= '.jpg';
  204. }
  205. if (strstr(strtolower($_FILES['editormd-image-file']['name']), ".php") ) {
  206. return false;
  207. }
  208. if (!empty($qiniu_config['driverConfig']['secrectKey'])) {
  209. //上传到七牛
  210. $Upload = new \Think\Upload(C('UPLOAD_SITEIMG_QINIU'));
  211. $info = $Upload->upload($_FILES);
  212. $url = $info['editormd-image-file']['url'] ;
  213. echo json_encode(array("url"=>$url,"success"=>1));
  214. }else{
  215. $upload = new \Think\Upload();// 实例化上传类
  216. $upload->maxSize = 3145728 ;// 设置附件上传大小
  217. $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  218. $upload->rootPath = './Public/Uploads/';// 设置附件上传目录
  219. $upload->savePath = '';// 设置附件上传子目录
  220. $info = $upload->upload() ;
  221. if(!$info) {// 上传错误提示错误信息
  222. $this->error($upload->getError());
  223. return;
  224. }else{// 上传成功 获取上传文件信息
  225. $url = get_domain().__ROOT__.substr($upload->rootPath,1).$info['editormd-image-file']['savepath'].$info['editormd-image-file']['savename'] ;
  226. echo json_encode(array("url"=>$url,"success"=>1));
  227. }
  228. }
  229. }
  230. public function diff(){
  231. $login_user = $this->checkLogin();
  232. $page_history_id = I("page_history_id/d");
  233. $page_id = I("page_id/d");
  234. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  235. $cur_page_content = $page['page_content'];
  236. $item_id = $page['item_id'] ?$page['item_id'] :$item_id;
  237. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  238. $this->message(L('no_permissions'));
  239. return;
  240. }
  241. $page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
  242. $page_content = gzuncompress(base64_decode($page['page_content']));
  243. $history_page_content = $page_content ? $page_content : $page['page_content'] ;
  244. $this->assign("cur_page_content" , $cur_page_content);
  245. $this->assign("history_page_content" , $history_page_content);
  246. $this->display();
  247. }
  248. }