ItemController.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class ItemController extends BaseController {
  5. //项目列表页
  6. public function index(){
  7. $login_user = $this->checkLogin();
  8. $items = D("Item")->where("uid = '$login_user[uid]' or item_id in ( select item_id from ".C('DB_PREFIX')."item_member where uid = '$login_user[uid]' ) ")->select();
  9. $share_url = get_domain().__APP__.'/uid/'.$login_user['uid'];
  10. $this->assign("items" , $items);
  11. $this->assign("login_user" , $login_user);
  12. $this->assign("share_url" , $share_url);
  13. $this->display();
  14. }
  15. //我公开的项目列表
  16. public function showByUid(){
  17. $login_user = $this->checkLogin(false); //如果用户有登录,则赋值给$login_user
  18. $uid = I("uid/d");
  19. $show_user = D("User")->where(" uid = '$uid' ")->find();
  20. if ($show_user) {
  21. $items = D("Item")->where(" password = '' and ( uid = '$show_user[uid]' or item_id in ( select item_id from ".C('DB_PREFIX')."item_member where uid = '$show_user[uid]' ) ) ")->select();
  22. $this->assign("items" , $items);
  23. $this->assign("show_user" , $show_user);
  24. $this->assign("login_user" , $login_user);
  25. }
  26. if (LANG_SET == 'en-us') {
  27. $help_url = "https://www.showdoc.cc/help-en";
  28. }
  29. else{
  30. $help_url = "https://www.showdoc.cc/help";
  31. }
  32. $this->assign("help_url" , $help_url);
  33. $this->display();
  34. }
  35. //新建项目
  36. public function add(){
  37. $login_user = $this->checkLogin();
  38. $item_id = I("item_id/d");
  39. if (!IS_POST) {
  40. $item = D("Item")->where("item_id = '$item_id' ")->find();
  41. $this->assign("item" , $item);
  42. $this->display ();
  43. }else{
  44. $item_name = I("item_name");
  45. $item_domain = I("item_domain") ? I("item_domain") : '';
  46. $copy_item_id = I("copy_item_id");
  47. $password = I("password");
  48. $item_description = I("item_description");
  49. $item_type = I("item_type");
  50. if ($item_domain) {
  51. $item = D("Item")->where("item_domain = '$item_domain' and item_id !='$item_id' ")->find();
  52. if ($item) {
  53. //个性域名已经存在
  54. $this->message(L('domain_already_exists'));
  55. return false;
  56. }
  57. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  58. //echo '个性域名只能是字母或数字的组合';exit;
  59. $this->message(L('item_domain_illegal'));
  60. return false;
  61. }
  62. }
  63. //如果是复制项目
  64. if ($copy_item_id > 0) {
  65. if (!$this->checkItemPermn($login_user['uid'] , $copy_item_id)) {
  66. $this->message(L('no_permissions'));
  67. return;
  68. }
  69. $ret = D("Item")->copy($copy_item_id,$login_user['uid'],$item_name,$item_description,$password,$item_domain);
  70. if ($ret) {
  71. $this->message(L('operation_succeeded'),U('Home/Item/index'));
  72. }else{
  73. $this->message(L('operation_failed'),U('Home/Item/index'));
  74. }
  75. return ;
  76. }
  77. if ($item_id > 0 ) {
  78. $data = array(
  79. "item_name" => $item_name ,
  80. "item_domain" => $item_domain ,
  81. "password" => $password ,
  82. "item_description" => $item_description ,
  83. );
  84. $ret = D("Item")->where("item_id = '$item_id' ")->save($data);
  85. }else{
  86. $insert = array(
  87. "uid" => $login_user['uid'] ,
  88. "username" => $login_user['username'] ,
  89. "item_name" => $item_name ,
  90. "password" => $password ,
  91. "item_description" => $item_description ,
  92. "item_domain" => $item_domain ,
  93. "item_type" => $item_type ,
  94. "addtime" =>time()
  95. );
  96. $item_id = D("Item")->add($insert);
  97. }
  98. if ($item_id) {
  99. //如果是单页应用,则新建一个默认页
  100. if ($item_type == 2 ) {
  101. $insert = array(
  102. 'author_uid' => $login_user['uid'] ,
  103. 'author_username' => $login_user['username'],
  104. "page_title" => $item_name ,
  105. "item_id" => $item_id ,
  106. "cat_id" => 0 ,
  107. "page_content" => '欢迎使用showdoc。点击右上方的编辑按钮进行编辑吧!' ,
  108. "addtime" =>time()
  109. );
  110. D("Page")->add($insert);
  111. }
  112. $this->message(L('operation_succeeded'),U('Home/Item/index'));
  113. }else{
  114. $this->message(L('operation_failed'),U('Home/Item/index'));
  115. }
  116. }
  117. }
  118. //根据项目类型展示项目
  119. public function show(){
  120. $this->checkLogin(false);
  121. $item_id = I("item_id/d");
  122. $item_domain = I("item_domain/s");
  123. $current_page_id = I("page_id/d");
  124. //判断个性域名
  125. if ($item_domain) {
  126. $item = D("Item")->where("item_domain = '$item_domain' ")->find();
  127. if ($item['item_id']) {
  128. $item_id = $item['item_id'] ;
  129. }
  130. }
  131. $login_user = session("login_user");
  132. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  133. $this->checkItemVisit($uid , $item_id);
  134. $item = D("Item")->where("item_id = '$item_id' ")->find();
  135. if ($item['item_type'] == 1 ) {
  136. $this->_show_regular_item($item);
  137. }
  138. elseif ($item['item_type'] == 2 ) {
  139. $this->_show_single_page_item($item);
  140. }else{
  141. $this->_show_regular_item($item);
  142. }
  143. }
  144. //展示常规项目
  145. private function _show_regular_item($item){
  146. $item_id = $item['item_id'];
  147. $current_page_id = I("page_id/d");
  148. $keyword = I("keyword");
  149. $login_user = session("login_user");
  150. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  151. //是否有搜索词
  152. if ($keyword) {
  153. $pages = D("Page")->where("item_id = '$item_id' and ( page_title like '%{$keyword}%' or page_content like '%{$keyword}%' ) ")->order(" `s_number` asc ")->field("page_id,author_uid,cat_id,page_title,addtime")->select();
  154. }else{
  155. //获取所有父目录id为0的页面
  156. $pages = D("Page")->where("cat_id = '0' and item_id = '$item_id' ")->order(" `s_number` asc ")->field("page_id,author_uid,cat_id,page_title,addtime")->select();
  157. //获取所有二级目录
  158. $catalogs = D("Catalog")->where("item_id = '$item_id' and level = 2 ")->order(" `s_number` asc ")->select();
  159. if ($catalogs) {
  160. foreach ($catalogs as $key => &$catalog) {
  161. //该二级目录下的所有子页面
  162. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->field("page_id,author_uid,cat_id,page_title,addtime")->select();
  163. $catalog['pages'] = $temp ? $temp: array();
  164. //该二级目录下的所有子目录
  165. $temp = D("catalog")->where("parent_cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  166. $catalog['catalogs'] = $temp ? $temp: array();
  167. if($catalog['catalogs']){
  168. //获取所有三级目录的子页面
  169. foreach ($catalog['catalogs'] as $key3 => &$catalog3) {
  170. //该二级目录下的所有子页面
  171. $temp = D("Page")->where("cat_id = '$catalog3[cat_id]' ")->order(" `s_number` asc ")->field("page_id,author_uid,cat_id,page_title,addtime")->select();
  172. $catalog3['pages'] = $temp ? $temp: array();
  173. }
  174. }
  175. }
  176. }
  177. }
  178. $domain = $item['item_domain'] ? $item['item_domain'] : $item['item_id'];
  179. $share_url = get_domain().__APP__.'/'.$domain;
  180. $ItemPermn = $this->checkItemPermn($uid , $item_id) ;
  181. $ItemCreator = $this->checkItemCreator($uid , $item_id);
  182. if (LANG_SET == 'en-us') {
  183. $help_url = "https://www.showdoc.cc/help-en";
  184. }
  185. else{
  186. $help_url = "https://www.showdoc.cc/help";
  187. }
  188. $this->assign("help_url" , $help_url);
  189. $this->assign("current_page_id" , $current_page_id);
  190. $this->assign("keyword" , $keyword);
  191. $this->assign("ItemPermn" , $ItemPermn);
  192. $this->assign("ItemCreator" , $ItemCreator);
  193. $this->assign("share_url" , $share_url);
  194. $this->assign("catalogs" , $catalogs);
  195. $this->assign("pages" , $pages);
  196. $this->assign("item" , $item);
  197. $this->assign("login_user" , $login_user);
  198. $this->display("show_regular");
  199. }
  200. //展示单页项目
  201. private function _show_single_page_item($item){
  202. $item_id = $item['item_id'];
  203. $current_page_id = I("page_id/d");
  204. $login_user = session("login_user");
  205. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  206. //获取页面
  207. $page = D("Page")->where(" item_id = '$item_id' ")->find();
  208. $domain = $item['item_domain'] ? $item['item_domain'] : $item['item_id'];
  209. $share_url = get_domain().__APP__.'/'.$domain;
  210. $ItemPermn = $this->checkItemPermn($uid , $item_id) ;
  211. $ItemCreator = $this->checkItemCreator($uid , $item_id);
  212. $this->assign("current_page_id" , $current_page_id);
  213. $this->assign("ItemPermn" , $ItemPermn);
  214. $this->assign("ItemCreator" , $ItemCreator);
  215. $this->assign("share_url" , $share_url);
  216. $this->assign("catalogs" , $catalogs);
  217. $this->assign("page" , $page);
  218. $this->assign("item" , $item);
  219. $this->assign("login_user" , $login_user);
  220. $this->display("show_single_page");
  221. }
  222. //删除项目
  223. public function delete(){
  224. $item_id = I("item_id");
  225. $login_user = $this->checkLogin();
  226. if (!$this->checkItemCreator($login_user['uid'] , $item_id)) {
  227. $this->message(L('no_permissions'));
  228. return;
  229. }
  230. $this->assign("item_id" , $item_id);
  231. $this->display();
  232. }
  233. //删除项目
  234. public function ajaxDelete(){
  235. $login_user = $this->checkLogin();
  236. $item_id = I("item_id/d");
  237. $password = I("password");
  238. $item = D("Item")->where("item_id = '$item_id' ")->find();
  239. if(! D("User")-> checkLogin($item['username'],$password)){
  240. $return['error_code'] = 10102 ;
  241. $return['error_message'] = L('incorrect_password') ;
  242. $this->sendResult($return);
  243. return ;
  244. }
  245. D("Page")->where("item_id = '$item_id' ")->delete();
  246. D("Catalog")->where("item_id = '$item_id' ")->delete();
  247. D("PageHistory")->where("item_id = '$item_id' ")->delete();
  248. $return = D("Item")->where("item_id = '$item_id' ")->delete();
  249. if (!$return) {
  250. $return['error_code'] = 10103 ;
  251. $return['error_message'] = 'request fail' ;
  252. }
  253. $this->sendResult($return);
  254. }
  255. //输入访问密码
  256. public function pwd(){
  257. $item_id = I("item_id/d");
  258. $CloseVerify = C('CloseVerify');
  259. $refer_url = I('refer_url');
  260. //var_dump(urldecode($refer_url));
  261. $this->assign('CloseVerify',$CloseVerify);
  262. $this->assign('refer_url',$refer_url);
  263. if (!IS_POST) {
  264. $this->assign("item_id" , $item_id);
  265. $this->display ();
  266. }else{
  267. $password = I("password");
  268. $v_code = I("v_code");
  269. if ( $CloseVerify || ( $v_code && $v_code == session('v_code') )) {
  270. $item = D("Item")->where("item_id = '$item_id' ")->find();
  271. if ($item['password'] == $password) {
  272. session("visit_item_".$item_id , 1 );
  273. if ($refer_url) {
  274. header("location:".base64_decode($refer_url));
  275. }else{
  276. header("location:".U("Home/Item/show").'&item_id='.$item_id);
  277. }
  278. }else{
  279. $this->message(L('access_password_are_incorrect'));
  280. }
  281. }else{
  282. $this->message(L('verification_code_are_incorrect'));
  283. }
  284. }
  285. }
  286. //导出word
  287. public function word(){
  288. import("Vendor.Parsedown.Parsedown");
  289. $Parsedown = new \Parsedown();
  290. $item_id = I("item_id/d");
  291. $login_user = $this->checkLogin();
  292. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  293. $this->message(L('no_permissions'));
  294. return;
  295. }
  296. $item = D("Item")->where("item_id = '$item_id' ")->find();
  297. //获取所有父目录id为0的页面
  298. $pages = D("Page")->where("cat_id = '0' and item_id = '$item_id' ")->order(" `s_number` asc ")->select();
  299. //获取所有二级目录
  300. $catalogs = D("Catalog")->where("item_id = '$item_id' and level = 2 ")->order(" `s_number` asc ")->select();
  301. if ($catalogs) {
  302. foreach ($catalogs as $key => &$catalog) {
  303. //该二级目录下的所有子页面
  304. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  305. $catalog['pages'] = $temp ? $temp: array();
  306. //该二级目录下的所有子目录
  307. $temp = D("catalog")->where("parent_cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  308. $catalog['catalogs'] = $temp ? $temp: array();
  309. if($catalog['catalogs']){
  310. //获取所有三级目录的子页面
  311. foreach ($catalog['catalogs'] as $key3 => &$catalog3) {
  312. //该二级目录下的所有子页面
  313. $temp = D("Page")->where("cat_id = '$catalog3[cat_id]' ")->order(" `s_number` asc ")->select();
  314. $catalog3['pages'] = $temp ? $temp: array();
  315. }
  316. }
  317. }
  318. }
  319. $data = '';
  320. $parent = 1;
  321. if ($pages) {
  322. foreach ($pages as $key => $value) {
  323. $data .= "<h1>{$parent}、{$value['page_title']}</h1>";
  324. $data .= '<div style="margin-left:20px;">';
  325. $data .= htmlspecialchars_decode($Parsedown->text($value['page_content']));
  326. $data .= '</div>';
  327. $parent ++;
  328. }
  329. }
  330. //var_export($catalogs);
  331. if ($catalogs) {
  332. foreach ($catalogs as $key => $value) {
  333. $data .= "<h1>{$parent}、{$value['cat_name']}</h1>";
  334. $data .= '<div style="margin-left:20px;">';
  335. $child = 1 ;
  336. if ($value['pages']) {
  337. foreach ($value['pages'] as $page) {
  338. $data .= "<h2>{$parent}.{$child}、{$page['page_title']}</h2>";
  339. $data .= '<div style="margin-left:20px;">';
  340. $data .= htmlspecialchars_decode($Parsedown->text($page['page_content']));
  341. $data .= '</div>';
  342. $child ++;
  343. }
  344. }
  345. if ($value['catalogs']) {
  346. $parent2 = 1 ;
  347. foreach ($value['catalogs'] as $key3 => $value3) {
  348. $data .= "<h2>{$parent}.{$parent2}、{$value3['cat_name']}</h2>";
  349. $data .= '<div style="margin-left:20px;">';
  350. $child2 = 1 ;
  351. if ($value3['pages']) {
  352. foreach ($value3['pages'] as $page3) {
  353. $data .= "<h3>{$parent}.{$parent2}.{$child2}、{$page3['page_title']}</h3>";
  354. $data .= '<div style="margin-left:30px;">';
  355. $data .= htmlspecialchars_decode($Parsedown->text($page3['page_content']));
  356. $data .= '</div>';
  357. $child2 ++;
  358. }
  359. }
  360. $data .= '</div>';
  361. $parent2 ++;
  362. }
  363. }
  364. $data .= '</div>';
  365. $parent ++;
  366. }
  367. }
  368. output_word($data,$item['item_name']);
  369. }
  370. public function itemList(){
  371. $login_user = $this->checkLogin();
  372. $items = D("Item")->where("uid = '$login_user[uid]' ")->select();
  373. $items = $items ? $items : array();
  374. $this->sendResult($items);
  375. }
  376. }