PageController.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. $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']."-copy";
  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(L('no_permissions'));
  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("api_doc_templ" , 'MdTemplate/api-doc.'.LANG_SET);
  68. $this->assign("database_doc_templ" , 'MdTemplate/database.'.LANG_SET);
  69. $this->assign("page" , $page);
  70. $this->assign("item_id" , $item_id);
  71. $this->assign("default_second_cat_id" , $default_second_cat_id);
  72. $this->assign("default_child_cat_id" , $default_child_cat_id);
  73. $this->display();
  74. }
  75. //跳转到HTTP接口测试页面
  76. public function http_api(){
  77. $this->display();
  78. }
  79. //处理HTTP测试请求,返回请求接口后的数据
  80. public function ajaxHttpApi(){
  81. $url=I('url');
  82. $method=I('method');
  83. $params=I('params');
  84. if($method=='get'){
  85. $url=$url."?".$params;
  86. $return=$this->http_get($url);
  87. }else{
  88. $return=$this->http_post($url, $params);
  89. }
  90. echo $return;
  91. }
  92. /**
  93. * GET 请求
  94. *
  95. * @param string $url
  96. */
  97. private function http_get($url) {
  98. $oCurl = curl_init ();
  99. if (stripos ( $url, "https://" ) !== FALSE) {
  100. curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );
  101. curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, FALSE );
  102. }
  103. curl_setopt ( $oCurl, CURLOPT_URL, $url );
  104. curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 );
  105. $sContent = curl_exec ( $oCurl );
  106. curl_close ( $oCurl );
  107. return $sContent;
  108. }
  109. /**
  110. * POST 请求
  111. *
  112. * @param string $url
  113. * @param array $param
  114. * @return string content
  115. */
  116. private function http_post($url, $param) {
  117. $oCurl = curl_init ();
  118. if (stripos ( $url, "https://" ) !== FALSE) {
  119. curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );
  120. curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, false );
  121. }
  122. if (is_string ( $param )) {
  123. $strPOST = $param;
  124. } else {
  125. $aPOST = array ();
  126. foreach ( $param as $key => $val ) {
  127. $aPOST [] = $key . "=" . urlencode ( $val );
  128. }
  129. $strPOST = join ( "&", $aPOST );
  130. }
  131. curl_setopt ( $oCurl, CURLOPT_URL, $url );
  132. curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 );
  133. curl_setopt ( $oCurl, CURLOPT_POST, true );
  134. curl_setopt ( $oCurl, CURLOPT_POSTFIELDS, $strPOST );
  135. $sContent = curl_exec ( $oCurl );
  136. curl_close ( $oCurl );
  137. return $sContent;
  138. }
  139. //保存
  140. public function save(){
  141. $login_user = $this->checkLogin();
  142. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  143. $page_title = I("page_title") ?I("page_title") : L("default_title");
  144. $page_comments = I("page_comments") ?I("page_comments") :'';
  145. $page_content = I("page_content");
  146. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  147. $item_id = I("item_id/d")? I("item_id/d") : 0;
  148. $s_number = I("s_number/d")? I("s_number/d") : 99;
  149. $login_user = $this->checkLogin();
  150. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  151. $this->message(L('no_permissions'));
  152. return;
  153. }
  154. $data['page_title'] = $page_title ;
  155. $data['page_content'] = $page_content ;
  156. $data['page_comments'] = $page_comments ;
  157. $data['s_number'] = $s_number ;
  158. $data['item_id'] = $item_id ;
  159. $data['cat_id'] = $cat_id ;
  160. $data['addtime'] = time();
  161. $data['author_uid'] = $login_user['uid'] ;
  162. $data['author_username'] = $login_user['username'];
  163. if ($page_id > 0 ) {
  164. //在保存前先把当前页面的版本存档
  165. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  166. $insert_history = array(
  167. 'page_id'=>$page['page_id'],
  168. 'item_id'=>$page['item_id'],
  169. 'cat_id'=>$page['cat_id'],
  170. 'page_title'=>$page['page_title'],
  171. 'page_comments'=>$page['page_comments'],
  172. 'page_content'=>base64_encode( gzcompress($page['page_content'], 9)),
  173. 's_number'=>$page['s_number'],
  174. 'addtime'=>$page['addtime'],
  175. 'author_uid'=>$page['author_uid'],
  176. 'author_username'=>$page['author_username'],
  177. );
  178. D("PageHistory")->add($insert_history);
  179. $ret = D("Page")->where(" page_id = '$page_id' ")->save($data);
  180. //统计该page_id有多少历史版本了
  181. $Count = D("PageHistory")->where(" page_id = '$page_id' ")->Count();
  182. if ($Count > 20 ) {
  183. //每个单页面只保留最多20个历史版本
  184. $ret = D("PageHistory")->where(" page_id = '$page_id' ")->limit("20")->order("page_history_id desc")->select();
  185. D("PageHistory")->where(" page_id = '$page_id' and page_history_id < ".$ret[19]['page_history_id'] )->delete();
  186. }
  187. //更新项目时间
  188. D("Item")->where(" item_id = '$item_id' ")->save(array("last_update_time"=>time()));
  189. $return = D("Page")->where(" page_id = '$page_id' ")->find();
  190. }else{
  191. $page_id = D("Page")->add($data);
  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. }
  196. if (!$return) {
  197. $return['error_code'] = 10103 ;
  198. $return['error_message'] = 'request fail' ;
  199. }
  200. $this->sendResult($return);
  201. }
  202. //删除页面
  203. public function delete(){
  204. $page_id = I("page_id/d")? I("page_id/d") : 0;
  205. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  206. $login_user = $this->checkLogin();
  207. if (!$this->checkItemCreator($login_user['uid'] , $page['item_id']) && $login_user['uid'] != $page['author_uid']) {
  208. $this->message(L('no_permissions_to_delete_page',array("author_username"=>$page['author_username'])));
  209. return;
  210. }
  211. if ($page) {
  212. $ret = D("Page")->where(" page_id = '$page_id' ")->delete();
  213. //更新项目时间
  214. D("Item")->where(" item_id = '$page[item_id]' ")->save(array("last_update_time"=>time()));
  215. }
  216. if ($ret) {
  217. $this->message(L('delete_succeeded'),U("Home/item/show?item_id={$page['item_id']}"));
  218. }else{
  219. $this->message(L('delete_failed'),U("Home/item/show?item_id={$page['item_id']}"));
  220. }
  221. }
  222. //历史版本
  223. public function history(){
  224. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  225. $this->assign("page_id" , $page_id);
  226. $PageHistory = D("PageHistory")->where("page_id = '$page_id' ")->order(" addtime desc")->limit(10)->select();
  227. if ($PageHistory) {
  228. foreach ($PageHistory as $key => &$value) {
  229. $page_content = gzuncompress(base64_decode($value['page_content']));
  230. $value['page_content'] = $page_content ? $page_content : $value['page_content'] ;
  231. $value['addtime'] = date("Y-m-d H:i:s" , $value['addtime']);
  232. }
  233. }
  234. $this->assign("PageHistory" , $PageHistory);
  235. $this->display();
  236. }
  237. //上传图片
  238. public function uploadImg(){
  239. $qiniu_config = C('UPLOAD_SITEIMG_QINIU') ;
  240. if (!empty($qiniu_config['driverConfig']['secrectKey'])) {
  241. //上传到七牛
  242. $Upload = new \Think\Upload(C('UPLOAD_SITEIMG_QINIU'));
  243. $info = $Upload->upload($_FILES);
  244. $url = $info['editormd-image-file']['url'] ;
  245. echo json_encode(array("url"=>$url,"success"=>1));
  246. }else{
  247. $upload = new \Think\Upload();// 实例化上传类
  248. $upload->maxSize = 3145728 ;// 设置附件上传大小
  249. $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  250. $upload->rootPath = './Public/Uploads/';// 设置附件上传目录
  251. $upload->savePath = '';// 设置附件上传子目录
  252. $info = $upload->upload() ;
  253. if(!$info) {// 上传错误提示错误信息
  254. $this->error($upload->getError());
  255. return;
  256. }else{// 上传成功 获取上传文件信息
  257. $url = get_domain().__ROOT__.substr($upload->rootPath,1).$info['editormd-image-file']['savepath'].$info['editormd-image-file']['savename'] ;
  258. echo json_encode(array("url"=>$url,"success"=>1));
  259. }
  260. }
  261. }
  262. }