PageController.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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("你无权限");
  13. return;
  14. }
  15. $Parsedown = new \Parsedown();
  16. $page['page_content'] = $Parsedown->text(htmlspecialchars_decode($page['page_content']));
  17. $this->assign("page" , $page);
  18. $this->display();
  19. }
  20. //返回单个页面的源markdown代码
  21. public function md(){
  22. $page_id = I("page_id/d");
  23. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  24. echo $page['page_content'];
  25. }
  26. //编辑页面
  27. public function edit(){
  28. $login_user = $this->checkLogin();
  29. $page_id = I("page_id/d");
  30. $item_id = I("item_id/d");
  31. $page_history_id = I("page_history_id/d");
  32. $copy_page_id = I("copy_page_id/d");
  33. if ($page_id > 0 ) {
  34. if ($page_history_id) {
  35. $page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
  36. $page_content = gzuncompress(base64_decode($page['page_content']));
  37. $page['page_content'] = $page_content ? $page_content : $page['page_content'] ;
  38. }else{
  39. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  40. }
  41. $default_cat_id = $page['cat_id'];
  42. }
  43. //如果是复制接口
  44. elseif ($copy_page_id) {
  45. $copy_page = D("Page")->where(" page_id = '$copy_page_id' ")->find();
  46. $page['page_title'] = $copy_page['page_title']."-副本";
  47. $page['page_content'] = $copy_page['page_content'];
  48. $page['item_id'] = $copy_page['item_id'];
  49. $default_cat_id = $copy_page['cat_id'];
  50. }else{
  51. //查找用户上一次设置的目录
  52. $last_page = D("Page")->where(" author_uid ='$login_user[uid]' and $item_id = '$item_id' ")->order(" addtime desc ")->limit(1)->find();
  53. $default_cat_id = $last_page['cat_id'];
  54. }
  55. $item_id = $page['item_id'] ?$page['item_id'] :$item_id;
  56. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  57. $this->message("你无权限");
  58. return;
  59. }
  60. $Catalog = D("Catalog")->where(" cat_id = '$default_cat_id' ")->find();
  61. if ($Catalog['parent_cat_id']) {
  62. $default_second_cat_id = $Catalog['parent_cat_id'];
  63. $default_child_cat_id = $default_cat_id;
  64. }else{
  65. $default_second_cat_id = $default_cat_id;
  66. }
  67. $this->assign("page" , $page);
  68. $this->assign("item_id" , $item_id);
  69. $this->assign("default_second_cat_id" , $default_second_cat_id);
  70. $this->assign("default_child_cat_id" , $default_child_cat_id);
  71. $this->display();
  72. }
  73. //跳转到HTTP接口测试页面
  74. public function http_api(){
  75. $this->display();
  76. }
  77. //处理HTTP测试请求,返回请求接口后的数据
  78. public function ajaxHttpApi(){
  79. $url=I('url');
  80. $method=I('method');
  81. $params=I('params');
  82. if($method=='get'){
  83. $url=$url."?".$params;
  84. $return=$this->http_get($url);
  85. }else{
  86. $return=$this->http_post($url, $params);
  87. }
  88. echo $return;
  89. }
  90. /**
  91. * GET 请求
  92. *
  93. * @param string $url
  94. */
  95. private function http_get($url) {
  96. $oCurl = curl_init ();
  97. if (stripos ( $url, "https://" ) !== FALSE) {
  98. curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );
  99. curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, FALSE );
  100. }
  101. curl_setopt ( $oCurl, CURLOPT_URL, $url );
  102. curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 );
  103. $sContent = curl_exec ( $oCurl );
  104. $aStatus = curl_getinfo ( $oCurl );
  105. curl_close ( $oCurl );
  106. if (intval ( $aStatus ["http_code"] ) == 200) {
  107. return $sContent;
  108. } else {
  109. return false;
  110. }
  111. }
  112. /**
  113. * POST 请求
  114. *
  115. * @param string $url
  116. * @param array $param
  117. * @return string content
  118. */
  119. private function http_post($url, $param) {
  120. $oCurl = curl_init ();
  121. if (stripos ( $url, "https://" ) !== FALSE) {
  122. curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );
  123. curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, false );
  124. }
  125. if (is_string ( $param )) {
  126. $strPOST = $param;
  127. } else {
  128. $aPOST = array ();
  129. foreach ( $param as $key => $val ) {
  130. $aPOST [] = $key . "=" . urlencode ( $val );
  131. }
  132. $strPOST = join ( "&", $aPOST );
  133. }
  134. curl_setopt ( $oCurl, CURLOPT_URL, $url );
  135. curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 );
  136. curl_setopt ( $oCurl, CURLOPT_POST, true );
  137. curl_setopt ( $oCurl, CURLOPT_POSTFIELDS, $strPOST );
  138. $sContent = curl_exec ( $oCurl );
  139. $aStatus = curl_getinfo ( $oCurl );
  140. curl_close ( $oCurl );
  141. if (intval ( $aStatus ["http_code"] ) == 200) {
  142. return $sContent;
  143. } else {
  144. return false;
  145. }
  146. }
  147. //保存
  148. public function save(){
  149. $login_user = $this->checkLogin();
  150. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  151. $page_title = I("page_title") ?I("page_title") : '默认页面';
  152. $page_content = I("page_content");
  153. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  154. $item_id = I("item_id/d")? I("item_id/d") : 0;
  155. $s_number = I("s_number/d")? I("s_number/d") : 99;
  156. $login_user = $this->checkLogin();
  157. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  158. $this->message("你无权限");
  159. return;
  160. }
  161. $data['page_title'] = $page_title ;
  162. $data['page_content'] = $page_content ;
  163. $data['s_number'] = $s_number ;
  164. $data['item_id'] = $item_id ;
  165. $data['cat_id'] = $cat_id ;
  166. $data['addtime'] = time();
  167. $data['author_uid'] = $login_user['uid'] ;
  168. $data['author_username'] = $login_user['username'];
  169. if ($page_id > 0 ) {
  170. //在保存前先把当前页面的版本存档
  171. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  172. $insert_history = array(
  173. 'page_id'=>$page['page_id'],
  174. 'item_id'=>$page['item_id'],
  175. 'cat_id'=>$page['cat_id'],
  176. 'page_title'=>$page['page_title'],
  177. 'page_content'=>base64_encode( gzcompress($page['page_content'], 9)),
  178. 's_number'=>$page['s_number'],
  179. 'addtime'=>$page['addtime'],
  180. 'author_uid'=>$page['author_uid'],
  181. 'author_username'=>$page['author_username'],
  182. );
  183. D("PageHistory")->add($insert_history);
  184. $ret = D("Page")->where(" page_id = '$page_id' ")->save($data);
  185. //统计该page_id有多少历史版本了
  186. $Count = D("PageHistory")->where(" page_id = '$page_id' ")->Count();
  187. if ($Count > 20 ) {
  188. //每个单页面只保留最多20个历史版本
  189. $ret = D("PageHistory")->where(" page_id = '$page_id' ")->limit("20")->order("page_history_id desc")->select();
  190. D("PageHistory")->where(" page_id = '$page_id' and page_history_id < ".$ret[19]['page_history_id'] )->delete();
  191. }
  192. //更新项目时间
  193. D("Item")->where(" item_id = '$item_id' ")->save(array("last_update_time"=>time()));
  194. $return = D("Page")->where(" page_id = '$page_id' ")->find();
  195. }else{
  196. $page_id = D("Page")->add($data);
  197. //更新项目时间
  198. D("Item")->where(" item_id = '$item_id' ")->save(array("last_update_time"=>time()));
  199. $return = D("Page")->where(" page_id = '$page_id' ")->find();
  200. }
  201. if (!$return) {
  202. $return['error_code'] = 10103 ;
  203. $return['error_message'] = 'request fail' ;
  204. }
  205. $this->sendResult($return);
  206. }
  207. //删除页面
  208. public function delete(){
  209. $page_id = I("page_id/d")? I("page_id/d") : 0;
  210. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  211. $login_user = $this->checkLogin();
  212. if (!$this->checkItemCreator($login_user['uid'] , $page['item_id']) && $login_user['uid'] != $page['author_uid']) {
  213. $this->message("你无权限!此页面由".$page['author_username']."创建");
  214. return;
  215. }
  216. if ($page) {
  217. $ret = D("Page")->where(" page_id = '$page_id' ")->delete();
  218. //更新项目时间
  219. D("Item")->where(" item_id = '$page[item_id]' ")->save(array("last_update_time"=>time()));
  220. }
  221. if ($ret) {
  222. $this->message("删除成功!",U("Home/item/show?item_id={$page['item_id']}"));
  223. }else{
  224. $this->message("删除失败!",U("Home/item/show?item_id={$page['item_id']}"));
  225. }
  226. }
  227. //历史版本
  228. public function history(){
  229. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  230. $this->assign("page_id" , $page_id);
  231. $PageHistory = D("PageHistory")->where("page_id = '$page_id' ")->order(" addtime desc")->limit(10)->select();
  232. if ($PageHistory) {
  233. foreach ($PageHistory as $key => &$value) {
  234. $page_content = gzuncompress(base64_decode($value['page_content']));
  235. $value['page_content'] = $page_content ? $page_content : $value['page_content'] ;
  236. $value['addtime'] = date("Y-m-d H:i:s" , $value['addtime']);
  237. }
  238. }
  239. $this->assign("PageHistory" , $PageHistory);
  240. $this->display();
  241. }
  242. //上传图片
  243. public function uploadImg(){
  244. $upload = new \Think\Upload();// 实例化上传类
  245. $upload->maxSize = 3145728 ;// 设置附件上传大小
  246. $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  247. $upload->rootPath = './Public/Uploads/';// 设置附件上传目录
  248. $upload->savePath = '';// 设置附件上传子目录
  249. $info = $upload->upload() ;
  250. if(!$info) {// 上传错误提示错误信息
  251. $this->error($upload->getError());
  252. return;
  253. }else{// 上传成功 获取上传文件信息
  254. $url = get_domain().__ROOT__.substr($upload->rootPath,1).$info['editormd-image-file']['savepath'].$info['editormd-image-file']['savename'] ;
  255. echo json_encode(array("url"=>$url,"success"=>1));
  256. }
  257. }
  258. }