PageController.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class PageController extends BaseController {
  5. //页面详情
  6. public function info(){
  7. $page_id = I("page_id/d");
  8. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  9. if (!$page || $page['is_del'] == 1) {
  10. sleep(1);
  11. $this->sendError(10101);
  12. return false;
  13. }
  14. $login_user = $this->checkLogin(false);
  15. if (!$this->checkItemVisit($login_user['uid'] , $page['item_id'])) {
  16. $this->sendError(10103);
  17. return;
  18. }
  19. $page = $page ? $page : array();
  20. if ($page) {
  21. //unset($page['page_content']);
  22. $page['addtime'] = date("Y-m-d H:i:s",$page['addtime']);
  23. //判断是否包含附件信息
  24. $page['attachment_count'] = D("UploadFile")->where("page_id = '$page_id' ")->count();
  25. $singlePage = M("SinglePage")->where(" page_id = '%d' ",array($page_id))->limit(1)->find();
  26. if ($singlePage) {
  27. $page['unique_key'] = $singlePage['unique_key'] ;
  28. }else{
  29. $page['unique_key'] = '' ;
  30. }
  31. }
  32. $this->sendResult($page);
  33. }
  34. //删除页面
  35. public function delete(){
  36. $page_id = I("page_id/d")? I("page_id/d") : 0;
  37. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  38. $login_user = $this->checkLogin();
  39. if (!$this->checkItemCreator($login_user['uid'] , $page['item_id']) && $login_user['uid'] != $page['author_uid']) {
  40. $this->sendError(10303);
  41. return ;
  42. }
  43. if ($page) {
  44. $ret = D("Page")->softDeletePage($page_id);
  45. //更新项目时间
  46. D("Item")->where(" item_id = '$page[item_id]' ")->save(array("last_update_time"=>time()));
  47. }
  48. if ($ret) {
  49. $this->sendResult(array());
  50. }else{
  51. $this->sendError(10101);
  52. }
  53. }
  54. //保存
  55. public function save(){
  56. $login_user = $this->checkLogin();
  57. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  58. $is_urlencode = I("is_urlencode/d") ? I("is_urlencode/d") : 0 ; //页面内容是否经过了转义
  59. $page_title = I("page_title") ?I("page_title") : L("default_title");
  60. $page_comments = I("page_comments") ?I("page_comments") :'';
  61. $page_content = I("page_content");
  62. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  63. $item_id = I("item_id/d")? I("item_id/d") : 0;
  64. $s_number = I("s_number/d")? I("s_number/d") : 99;
  65. $login_user = $this->checkLogin();
  66. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  67. $this->sendError(10103);
  68. return;
  69. }
  70. if (!$page_content) {
  71. $this->sendError(10103,"不允许保存空内容,请随便写点什么");
  72. return;
  73. }
  74. if ($is_urlencode) {
  75. $page_content = urldecode($page_content);
  76. }
  77. $data['page_title'] = $page_title ;
  78. $data['page_content'] = $page_content ;
  79. $data['page_comments'] = $page_comments ;
  80. $data['s_number'] = $s_number ;
  81. $data['item_id'] = $item_id ;
  82. $data['cat_id'] = $cat_id ;
  83. $data['addtime'] = time();
  84. $data['author_uid'] = $login_user['uid'] ;
  85. $data['author_username'] = $login_user['username'];
  86. if ($page_id > 0 ) {
  87. //在保存前先把当前页面的版本存档
  88. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  89. if (!$this->checkItemPermn($login_user['uid'] , $page['item_id'])) {
  90. $this->sendError(10103);
  91. return;
  92. }
  93. $insert_history = array(
  94. 'page_id'=>$page['page_id'],
  95. 'item_id'=>$page['item_id'],
  96. 'cat_id'=>$page['cat_id'],
  97. 'page_title'=>$page['page_title'],
  98. 'page_comments'=>$page['page_comments'],
  99. 'page_content'=>base64_encode( gzcompress($page['page_content'], 9)),
  100. 's_number'=>$page['s_number'],
  101. 'addtime'=>$page['addtime'],
  102. 'author_uid'=>$page['author_uid'],
  103. 'author_username'=>$page['author_username'],
  104. );
  105. D("PageHistory")->add($insert_history);
  106. $ret = D("Page")->where(" page_id = '$page_id' ")->save($data);
  107. //统计该page_id有多少历史版本了
  108. $Count = D("PageHistory")->where(" page_id = '$page_id' ")->Count();
  109. if ($Count > 20 ) {
  110. //每个单页面只保留最多20个历史版本
  111. $ret = D("PageHistory")->where(" page_id = '$page_id' ")->limit("20")->order("page_history_id desc")->select();
  112. D("PageHistory")->where(" page_id = '$page_id' and page_history_id < ".$ret[19]['page_history_id'] )->delete();
  113. }
  114. //如果是单页项目,则将页面标题设置为项目名
  115. $item_array = D("Item")->where(" item_id = '$item_id' ")->find();
  116. if ($item_array['item_type'] == 2 ) {
  117. D("Item")->where(" item_id = '$item_id' ")->save(array("last_update_time"=>time(),"item_name"=>$page_title));
  118. }else{
  119. D("Item")->where(" item_id = '$item_id' ")->save(array("last_update_time"=>time()));
  120. }
  121. $return = D("Page")->where(" page_id = '$page_id' ")->find();
  122. }else{
  123. $page_id = D("Page")->add($data);
  124. //更新项目时间
  125. D("Item")->where(" item_id = '$item_id' ")->save(array("last_update_time"=>time()));
  126. $return = D("Page")->where(" page_id = '$page_id' ")->find();
  127. }
  128. if (!$return) {
  129. $return['error_code'] = 10103 ;
  130. $return['error_message'] = 'request fail' ;
  131. }
  132. $this->sendResult($return);
  133. }
  134. //历史版本列表
  135. public function history(){
  136. $login_user = $this->checkLogin(false);
  137. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  138. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  139. if (!$this->checkItemVisit($login_user['uid'] , $page['item_id'])) {
  140. $this->sendError(10103);
  141. return;
  142. }
  143. $PageHistory = D("PageHistory")->where("page_id = '$page_id' ")->order(" addtime desc")->limit(10)->select();
  144. if ($PageHistory) {
  145. foreach ($PageHistory as $key => &$value) {
  146. $value['addtime'] = date("Y-m-d H:i:s" , $value['addtime']);
  147. $page_content = uncompress_string($value['page_content']);
  148. if (!empty($page_content)) {
  149. $value['page_content'] = htmlspecialchars_decode($page_content) ;
  150. }
  151. }
  152. $this->sendResult($PageHistory);
  153. }else{
  154. $this->sendResult(array());
  155. }
  156. }
  157. //返回当前页面和历史某个版本的页面以供比较
  158. public function diff(){
  159. $page_id = I("page_id/d");
  160. $page_history_id = I("page_history_id/d");
  161. if (!$page_id) {
  162. return false;
  163. }
  164. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  165. if (!$page) {
  166. sleep(1);
  167. $this->sendError(10101);
  168. return false;
  169. }
  170. $login_user = $this->checkLogin(false);
  171. if (!$this->checkItemVisit($login_user['uid'] , $page['item_id'])) {
  172. $this->sendError(10103);
  173. return;
  174. }
  175. $history_page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
  176. $page_content = uncompress_string($history_page['page_content']);
  177. $history_page['page_content'] = $page_content ? $page_content : $history_page['page_content'] ;
  178. $this->sendResult(array("page"=>$page,"history_page"=>$history_page));
  179. }
  180. //上传图片
  181. public function uploadImg(){
  182. $login_user = $this->checkLogin();
  183. $item_id = I("item_id/d") ? I("item_id/d") : 0 ;
  184. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  185. if ($_FILES['editormd-image-file']['name'] == 'blob') {
  186. $_FILES['editormd-image-file']['name'] .= '.jpg';
  187. }
  188. if (!$_FILES['editormd-image-file']) {
  189. return false;
  190. }
  191. if (strstr(strtolower($_FILES['editormd-image-file']['name']), ".php") ) {
  192. return false;
  193. }
  194. $qiniu_config = C('UPLOAD_SITEIMG_QINIU') ;
  195. if (!empty($qiniu_config['driverConfig']['secrectKey'])) {
  196. //上传到七牛
  197. $Upload = new \Think\Upload(C('UPLOAD_SITEIMG_QINIU'));
  198. $info = $Upload->upload($_FILES);
  199. $url = $info['editormd-image-file']['url'] ;
  200. if ($url) {
  201. echo json_encode(array("url"=>$url,"success"=>1));
  202. }
  203. }else{
  204. $upload = new \Think\Upload();// 实例化上传类
  205. $upload->maxSize = 1003145728 ;// 设置附件上传大小
  206. $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  207. $upload->rootPath = './../Public/Uploads/';// 设置附件上传目录
  208. $upload->savePath = '';// 设置附件上传子目录
  209. $info = $upload->uploadOne($_FILES['editormd-image-file']) ;
  210. if(!$info) {// 上传错误提示错误信息
  211. $this->error($upload->getError());
  212. return;
  213. }else{// 上传成功 获取上传文件信息
  214. $url = get_domain().__ROOT__.substr($upload->rootPath,1).$info['savepath'].$info['savename'] ;
  215. echo json_encode(array("url"=>$url,"success"=>1));
  216. }
  217. }
  218. }
  219. //上传附件
  220. public function upload(){
  221. $login_user = $this->checkLogin();
  222. $item_id = I("item_id/d") ? I("item_id/d") : 0 ;
  223. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  224. $uploadFile = $_FILES['file'] ;
  225. if (!$page_id) {
  226. $this->sendError(10103,"请至少先保存一次页面内容");
  227. return;
  228. }
  229. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  230. $this->sendError(10103);
  231. return;
  232. }
  233. if (!$uploadFile) {
  234. return false;
  235. }
  236. if (strstr(strtolower($uploadFile['name']), ".php") ) {
  237. return false;
  238. }
  239. $upload = new \Think\Upload();// 实例化上传类
  240. $upload->maxSize = 4145728000 ;// 设置附件上传大小
  241. $upload->rootPath = './../Public/Uploads/';// 设置附件上传目录
  242. $upload->savePath = '';// 设置附件上传子目录
  243. $info = $upload->uploadOne($uploadFile) ;
  244. if(!$info) {// 上传错误提示错误信息
  245. $this->error($upload->getError());
  246. return;
  247. }else{// 上传成功 获取上传文件信息
  248. $url = get_domain().__ROOT__.substr($upload->rootPath,1).$info['savepath'].$info['savename'] ;
  249. $insert = array(
  250. "uid" => $login_user['uid'],
  251. "item_id" => $item_id,
  252. "page_id" => $page_id,
  253. "display_name" => $uploadFile['name'],
  254. "file_type" => $uploadFile['type'],
  255. "file_size" => $uploadFile['size'],
  256. "real_url" => $url,
  257. "addtime" => time(),
  258. );
  259. $ret = D("UploadFile")->add($insert);
  260. echo json_encode(array("url"=>$url,"success"=>1));
  261. }
  262. }
  263. public function uploadList(){
  264. $login_user = $this->checkLogin();
  265. $item_id = I("item_id/d") ? I("item_id/d") : 0 ;
  266. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  267. if (!$page_id) {
  268. $this->sendError(10103,"请至少先保存一次页面内容");
  269. return;
  270. }
  271. $return = array() ;
  272. $files = D("UploadFile")->where("page_id = '$page_id' ")->order("addtime desc")->select();
  273. if ($files) {
  274. $item_id = $files[0]['item_id'] ;
  275. if (!$this->checkItemVisit($login_user['uid'] , $item_id)) {
  276. $this->sendError(10103);
  277. return;
  278. }
  279. foreach ($files as $key => $value) {
  280. $return[] = array(
  281. "file_id"=>$value['file_id'],
  282. "display_name"=>$value['display_name'],
  283. "url"=>$value['real_url'],
  284. "addtime"=> date("Y-m-d H:i:s" , $value['addtime'] ),
  285. );
  286. }
  287. }
  288. $this->sendResult($return);
  289. }
  290. //删除已上传文件
  291. public function deleteUploadFile(){
  292. $login_user = $this->checkLogin();
  293. $file_id = I("file_id/d") ? I("file_id/d") : 0 ;
  294. $file = D("UploadFile")->where("file_id = '$file_id' ")->find();
  295. $item_id = $file['item_id'] ;
  296. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  297. $this->sendError(10103);
  298. return;
  299. }
  300. $ret = D("Page")->deleteFile($file_id);
  301. if ($ret) {
  302. $this->sendResult(array());
  303. }else{
  304. $this->sendError(10101,"删除失败");
  305. }
  306. }
  307. //创建单页
  308. public function createSinglePage(){
  309. $page_id = I("page_id/d");
  310. $isCreateSiglePage = I("isCreateSiglePage");
  311. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  312. if (!$page || $page['is_del'] == 1) {
  313. sleep(1);
  314. $this->sendError(10101);
  315. return false;
  316. }
  317. $login_user = $this->checkLogin(false);
  318. if (!$this->checkItemPermn($login_user['uid'] , $page['item_id'])) {
  319. $this->sendError(10103);
  320. return;
  321. }
  322. D("SinglePage")->where(" page_id = '$page_id' ")->delete();
  323. $unique_key = md5(time().rand()."gbgdhbdgtfgfK3@bv45342regdhbdgtfgftghsdg");
  324. $add = array(
  325. "unique_key" => $unique_key ,
  326. "page_id" => $page_id ,
  327. );
  328. if ($isCreateSiglePage == 'true') { //这里的布尔值被转成字符串了
  329. D("SinglePage")->add($add);
  330. $this->sendResult($add);
  331. }else{
  332. $this->sendResult(array());
  333. }
  334. }
  335. //页面详情
  336. public function infoByKey(){
  337. $unique_key = I("unique_key");
  338. if (!$unique_key) {
  339. return false;
  340. }
  341. $singlePage = M("SinglePage")->where(" unique_key = '%s' ",array($unique_key))->find();
  342. $page_id = $singlePage['page_id'];
  343. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  344. if (!$page || $page['is_del'] == 1) {
  345. sleep(1);
  346. $this->sendError(10101);
  347. return false;
  348. }
  349. $login_user = $this->checkLogin(false);
  350. $page = $page ? $page : array();
  351. if ($page) {
  352. unset($page['item_id']);
  353. unset($page['cat_id']);
  354. $page['addtime'] = date("Y-m-d H:i:s",$page['addtime']);
  355. //判断是否包含附件信息
  356. $page['attachment_count'] = D("UploadFile")->where("page_id = '$page_id' ")->count();
  357. }
  358. $this->sendResult($page);
  359. }
  360. }