ItemController.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 = '%s' and item_id !='%s' ",array($item_domain,$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 = '%s'",array($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. $keyword = \SQLite3::escapeString($keyword) ;
  154. $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();
  155. }else{
  156. //获取所有父目录id为0的页面
  157. $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();
  158. //获取所有二级目录
  159. $catalogs = D("Catalog")->where("item_id = '$item_id' and level = 2 ")->order(" `s_number` asc ")->select();
  160. if ($catalogs) {
  161. foreach ($catalogs as $key => &$catalog) {
  162. //该二级目录下的所有子页面
  163. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->field("page_id,author_uid,cat_id,page_title,addtime")->select();
  164. $catalog['pages'] = $temp ? $temp: array();
  165. //该二级目录下的所有子目录
  166. $temp = D("catalog")->where("parent_cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  167. $catalog['catalogs'] = $temp ? $temp: array();
  168. if($catalog['catalogs']){
  169. //获取所有三级目录的子页面
  170. foreach ($catalog['catalogs'] as $key3 => &$catalog3) {
  171. //该二级目录下的所有子页面
  172. $temp = D("Page")->where("cat_id = '$catalog3[cat_id]' ")->order(" `s_number` asc ")->field("page_id,author_uid,cat_id,page_title,addtime")->select();
  173. $catalog3['pages'] = $temp ? $temp: array();
  174. }
  175. }
  176. }
  177. }
  178. }
  179. $domain = $item['item_domain'] ? $item['item_domain'] : $item['item_id'];
  180. $share_url = get_domain().__APP__.'/'.$domain;
  181. $ItemPermn = $this->checkItemPermn($uid , $item_id) ;
  182. $ItemCreator = $this->checkItemCreator($uid , $item_id);
  183. if (LANG_SET == 'en-us') {
  184. $help_url = "https://www.showdoc.cc/help-en";
  185. }
  186. else{
  187. $help_url = "https://www.showdoc.cc/help";
  188. }
  189. $this->assign("help_url" , $help_url);
  190. $this->assign("current_page_id" , $current_page_id);
  191. $this->assign("keyword" , $keyword);
  192. $this->assign("ItemPermn" , $ItemPermn);
  193. $this->assign("ItemCreator" , $ItemCreator);
  194. $this->assign("share_url" , $share_url);
  195. $this->assign("catalogs" , $catalogs);
  196. $this->assign("pages" , $pages);
  197. $this->assign("item" , $item);
  198. $this->assign("login_user" , $login_user);
  199. $this->display("show_regular");
  200. }
  201. //展示单页项目
  202. private function _show_single_page_item($item){
  203. $item_id = $item['item_id'];
  204. $current_page_id = I("page_id/d");
  205. $login_user = session("login_user");
  206. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  207. //获取页面
  208. $page = D("Page")->where(" item_id = '$item_id' ")->find();
  209. $domain = $item['item_domain'] ? $item['item_domain'] : $item['item_id'];
  210. $share_url = get_domain().__APP__.'/'.$domain;
  211. $ItemPermn = $this->checkItemPermn($uid , $item_id) ;
  212. $ItemCreator = $this->checkItemCreator($uid , $item_id);
  213. $this->assign("current_page_id" , $current_page_id);
  214. $this->assign("ItemPermn" , $ItemPermn);
  215. $this->assign("ItemCreator" , $ItemCreator);
  216. $this->assign("share_url" , $share_url);
  217. $this->assign("catalogs" , $catalogs);
  218. $this->assign("page" , $page);
  219. $this->assign("item" , $item);
  220. $this->assign("login_user" , $login_user);
  221. $this->display("show_single_page");
  222. }
  223. //删除项目
  224. public function delete(){
  225. $item_id = I("item_id");
  226. $login_user = $this->checkLogin();
  227. if (!$this->checkItemCreator($login_user['uid'] , $item_id)) {
  228. $this->message(L('no_permissions'));
  229. return;
  230. }
  231. $this->assign("item_id" , $item_id);
  232. $this->display();
  233. }
  234. //删除项目
  235. public function ajaxDelete(){
  236. $login_user = $this->checkLogin();
  237. $item_id = I("item_id/d");
  238. $password = I("password");
  239. $item = D("Item")->where("item_id = '$item_id' ")->find();
  240. if(! D("User")-> checkLogin($item['username'],$password)){
  241. $return['error_code'] = 10102 ;
  242. $return['error_message'] = L('incorrect_password') ;
  243. $this->sendResult($return);
  244. return ;
  245. }
  246. D("Page")->where("item_id = '$item_id' ")->delete();
  247. D("Catalog")->where("item_id = '$item_id' ")->delete();
  248. D("PageHistory")->where("item_id = '$item_id' ")->delete();
  249. D("ItemMember")->where("item_id = '$item_id' ")->delete();
  250. $return = D("Item")->where("item_id = '$item_id' ")->delete();
  251. if (!$return) {
  252. $return['error_code'] = 10103 ;
  253. $return['error_message'] = 'request fail' ;
  254. }
  255. $this->sendResult($return);
  256. }
  257. //输入访问密码
  258. public function pwd(){
  259. $item_id = I("item_id/d");
  260. $CloseVerify = C('CloseVerify');
  261. $refer_url = I('refer_url');
  262. //var_dump(urldecode($refer_url));
  263. $this->assign('CloseVerify',$CloseVerify);
  264. $this->assign('refer_url',$refer_url);
  265. if (!IS_POST) {
  266. $this->assign("item_id" , $item_id);
  267. $this->display ();
  268. }else{
  269. $password = I("password");
  270. $v_code = I("v_code");
  271. if ( $CloseVerify || ( $v_code && $v_code == session('v_code') )) {
  272. $item = D("Item")->where("item_id = '$item_id' ")->find();
  273. if ($item['password'] == $password) {
  274. session("visit_item_".$item_id , 1 );
  275. if ($refer_url) {
  276. header("location:".base64_decode($refer_url));
  277. }else{
  278. header("location:".U("Home/Item/show").'&item_id='.$item_id);
  279. }
  280. }else{
  281. $this->message(L('access_password_are_incorrect'));
  282. }
  283. }else{
  284. $this->message(L('verification_code_are_incorrect'));
  285. }
  286. }
  287. }
  288. //导出word
  289. public function word(){
  290. import("Vendor.Parsedown.Parsedown");
  291. $Parsedown = new \Parsedown();
  292. $item_id = I("item_id/d");
  293. $login_user = $this->checkLogin();
  294. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  295. $this->message(L('no_permissions'));
  296. return;
  297. }
  298. $item = D("Item")->where("item_id = '$item_id' ")->find();
  299. //获取所有父目录id为0的页面
  300. $pages = D("Page")->where("cat_id = '0' and item_id = '$item_id' ")->order(" `s_number` asc ")->select();
  301. //获取所有二级目录
  302. $catalogs = D("Catalog")->where("item_id = '$item_id' and level = 2 ")->order(" `s_number` asc ")->select();
  303. if ($catalogs) {
  304. foreach ($catalogs as $key => &$catalog) {
  305. //该二级目录下的所有子页面
  306. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  307. $catalog['pages'] = $temp ? $temp: array();
  308. //该二级目录下的所有子目录
  309. $temp = D("catalog")->where("parent_cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  310. $catalog['catalogs'] = $temp ? $temp: array();
  311. if($catalog['catalogs']){
  312. //获取所有三级目录的子页面
  313. foreach ($catalog['catalogs'] as $key3 => &$catalog3) {
  314. //该二级目录下的所有子页面
  315. $temp = D("Page")->where("cat_id = '$catalog3[cat_id]' ")->order(" `s_number` asc ")->select();
  316. $catalog3['pages'] = $temp ? $temp: array();
  317. }
  318. }
  319. }
  320. }
  321. $data = '';
  322. $parent = 1;
  323. if ($pages) {
  324. foreach ($pages as $key => $value) {
  325. $data .= "<h1>{$parent}、{$value['page_title']}</h1>";
  326. $data .= '<div style="margin-left:20px;">';
  327. $data .= htmlspecialchars_decode($Parsedown->text($value['page_content']));
  328. $data .= '</div>';
  329. $parent ++;
  330. }
  331. }
  332. //var_export($catalogs);
  333. if ($catalogs) {
  334. foreach ($catalogs as $key => $value) {
  335. $data .= "<h1>{$parent}、{$value['cat_name']}</h1>";
  336. $data .= '<div style="margin-left:20px;">';
  337. $child = 1 ;
  338. if ($value['pages']) {
  339. foreach ($value['pages'] as $page) {
  340. $data .= "<h2>{$parent}.{$child}、{$page['page_title']}</h2>";
  341. $data .= '<div style="margin-left:20px;">';
  342. $data .= htmlspecialchars_decode($Parsedown->text($page['page_content']));
  343. $data .= '</div>';
  344. $child ++;
  345. }
  346. }
  347. if ($value['catalogs']) {
  348. $parent2 = 1 ;
  349. foreach ($value['catalogs'] as $key3 => $value3) {
  350. $data .= "<h2>{$parent}.{$parent2}、{$value3['cat_name']}</h2>";
  351. $data .= '<div style="margin-left:20px;">';
  352. $child2 = 1 ;
  353. if ($value3['pages']) {
  354. foreach ($value3['pages'] as $page3) {
  355. $data .= "<h3>{$parent}.{$parent2}.{$child2}、{$page3['page_title']}</h3>";
  356. $data .= '<div style="margin-left:30px;">';
  357. $data .= htmlspecialchars_decode($Parsedown->text($page3['page_content']));
  358. $data .= '</div>';
  359. $child2 ++;
  360. }
  361. }
  362. $data .= '</div>';
  363. $parent2 ++;
  364. }
  365. }
  366. $data .= '</div>';
  367. $parent ++;
  368. }
  369. }
  370. output_word($data,$item['item_name']);
  371. }
  372. public function itemList(){
  373. $login_user = $this->checkLogin();
  374. $items = D("Item")->where("uid = '$login_user[uid]' ")->select();
  375. $items = $items ? $items : array();
  376. $this->sendResult($items);
  377. }
  378. public function setting(){
  379. $login_user = $this->checkLogin();
  380. $item_id = I("item_id/d");
  381. $uid = $login_user['uid'] ;
  382. $this->checkItemPermn($uid , $item_id) ;
  383. $this->assign("item_id",$item_id);
  384. $this->display();
  385. }
  386. }