ItemController.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. $share_url = get_domain().__APP__.'/uid/'.$login_user['uid'];
  9. $this->assign("login_user" , $login_user);
  10. $this->assign("share_url" , $share_url);
  11. $this->display();
  12. }
  13. //我公开的项目列表
  14. public function showByUid(){
  15. $login_user = $this->checkLogin(false); //如果用户有登录,则赋值给$login_user
  16. $uid = I("uid/d");
  17. $show_user = D("User")->where(" uid = '$uid' ")->find();
  18. if ($show_user) {
  19. $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();
  20. $this->assign("items" , $items);
  21. $this->assign("show_user" , $show_user);
  22. $this->assign("login_user" , $login_user);
  23. }
  24. if (LANG_SET == 'en-us') {
  25. $help_url = "https://www.showdoc.cc/help-en";
  26. }
  27. else{
  28. $help_url = "https://www.showdoc.cc/help";
  29. }
  30. $this->assign("help_url" , $help_url);
  31. $this->display();
  32. }
  33. //新建项目
  34. public function add(){
  35. $login_user = $this->checkLogin();
  36. $item_id = I("item_id/d");
  37. if (!IS_POST) {
  38. $item = D("Item")->where("item_id = '$item_id' ")->find();
  39. $this->assign("item" , $item);
  40. $this->display ();
  41. }else{
  42. $item_name = I("item_name");
  43. $item_domain = I("item_domain") ? I("item_domain") : '';
  44. $copy_item_id = I("copy_item_id");
  45. $password = I("password");
  46. $item_description = I("item_description");
  47. $item_type = I("item_type");
  48. if ($item_domain) {
  49. $item = D("Item")->where("item_domain = '%s' and item_id !='%s' ",array($item_domain,$item_id))->find();
  50. if ($item) {
  51. //个性域名已经存在
  52. $this->message(L('domain_already_exists'));
  53. return false;
  54. }
  55. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  56. //echo '个性域名只能是字母或数字的组合';exit;
  57. $this->message(L('item_domain_illegal'));
  58. return false;
  59. }
  60. }
  61. //如果是复制项目
  62. if ($copy_item_id > 0) {
  63. if (!$this->checkItemPermn($login_user['uid'] , $copy_item_id)) {
  64. $this->message(L('no_permissions'));
  65. return;
  66. }
  67. $ret = D("Item")->copy($copy_item_id,$login_user['uid'],$item_name,$item_description,$password,$item_domain);
  68. if ($ret) {
  69. $this->message(L('operation_succeeded'),U('Home/Item/index'));
  70. }else{
  71. $this->message(L('operation_failed'),U('Home/Item/index'));
  72. }
  73. return ;
  74. }
  75. if ($item_id > 0 ) {
  76. $data = array(
  77. "item_name" => $item_name ,
  78. "item_domain" => $item_domain ,
  79. "password" => $password ,
  80. "item_description" => $item_description ,
  81. );
  82. $ret = D("Item")->where("item_id = '$item_id' ")->save($data);
  83. }else{
  84. $insert = array(
  85. "uid" => $login_user['uid'] ,
  86. "username" => $login_user['username'] ,
  87. "item_name" => $item_name ,
  88. "password" => $password ,
  89. "item_description" => $item_description ,
  90. "item_domain" => $item_domain ,
  91. "item_type" => $item_type ,
  92. "addtime" =>time()
  93. );
  94. $item_id = D("Item")->add($insert);
  95. }
  96. if ($item_id) {
  97. //如果是单页应用,则新建一个默认页
  98. if ($item_type == 2 ) {
  99. $insert = array(
  100. 'author_uid' => $login_user['uid'] ,
  101. 'author_username' => $login_user['username'],
  102. "page_title" => $item_name ,
  103. "item_id" => $item_id ,
  104. "cat_id" => 0 ,
  105. "page_content" => '欢迎使用showdoc。点击右上方的编辑按钮进行编辑吧!' ,
  106. "addtime" =>time()
  107. );
  108. D("Page")->add($insert);
  109. }
  110. $this->message(L('operation_succeeded'),U('Home/Item/index'));
  111. }else{
  112. $this->message(L('operation_failed'),U('Home/Item/index'));
  113. }
  114. }
  115. }
  116. //根据项目类型展示项目
  117. public function show(){
  118. $this->checkLogin(false);
  119. $item_id = I("item_id/d");
  120. $item_domain = I("item_domain/s");
  121. $current_page_id = I("page_id/d");
  122. //判断个性域名
  123. if ($item_domain) {
  124. $item = D("Item")->where("item_domain = '%s'",array($item_domain))->find();
  125. if ($item['item_id']) {
  126. $item_id = $item['item_id'] ;
  127. }
  128. }
  129. $login_user = session("login_user");
  130. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  131. $this->checkItemVisit($uid , $item_id);
  132. $item = D("Item")->where("item_id = '$item_id' ")->find();
  133. if ($item['item_type'] == 1 ) {
  134. $this->_show_regular_item($item);
  135. }
  136. elseif ($item['item_type'] == 2 ) {
  137. $this->_show_single_page_item($item);
  138. }else{
  139. $this->_show_regular_item($item);
  140. }
  141. }
  142. //展示常规项目
  143. private function _show_regular_item($item){
  144. $item_id = $item['item_id'];
  145. $current_page_id = I("page_id/d");
  146. $keyword = I("keyword");
  147. $login_user = session("login_user");
  148. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  149. //是否有搜索词
  150. if ($keyword) {
  151. $keyword = \SQLite3::escapeString($keyword) ;
  152. $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();
  153. }else{
  154. //获取所有父目录id为0的页面
  155. $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();
  156. //获取所有二级目录
  157. $catalogs = D("Catalog")->where("item_id = '$item_id' and level = 2 ")->order(" `s_number` asc ")->select();
  158. if ($catalogs) {
  159. foreach ($catalogs as $key => &$catalog) {
  160. //该二级目录下的所有子页面
  161. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->field("page_id,author_uid,cat_id,page_title,addtime")->select();
  162. $catalog['pages'] = $temp ? $temp: array();
  163. //该二级目录下的所有子目录
  164. $temp = D("catalog")->where("parent_cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  165. $catalog['catalogs'] = $temp ? $temp: array();
  166. if($catalog['catalogs']){
  167. //获取所有三级目录的子页面
  168. foreach ($catalog['catalogs'] as $key3 => &$catalog3) {
  169. //该二级目录下的所有子页面
  170. $temp = D("Page")->where("cat_id = '$catalog3[cat_id]' ")->order(" `s_number` asc ")->field("page_id,author_uid,cat_id,page_title,addtime")->select();
  171. $catalog3['pages'] = $temp ? $temp: array();
  172. }
  173. }
  174. }
  175. }
  176. }
  177. $domain = $item['item_domain'] ? $item['item_domain'] : $item['item_id'];
  178. $share_url = get_domain().__APP__.'/'.$domain;
  179. $ItemPermn = $this->checkItemPermn($uid , $item_id) ;
  180. $ItemCreator = $this->checkItemCreator($uid , $item_id);
  181. if (LANG_SET == 'en-us') {
  182. $help_url = "https://www.showdoc.cc/help-en";
  183. }
  184. else{
  185. $help_url = "https://www.showdoc.cc/help";
  186. }
  187. $this->assign("help_url" , $help_url);
  188. $this->assign("current_page_id" , $current_page_id);
  189. $this->assign("keyword" , $keyword);
  190. $this->assign("ItemPermn" , $ItemPermn);
  191. $this->assign("ItemCreator" , $ItemCreator);
  192. $this->assign("share_url" , $share_url);
  193. $this->assign("catalogs" , $catalogs);
  194. $this->assign("pages" , $pages);
  195. $this->assign("item" , $item);
  196. $this->assign("login_user" , $login_user);
  197. $this->display("show_regular");
  198. }
  199. //展示单页项目
  200. private function _show_single_page_item($item){
  201. $item_id = $item['item_id'];
  202. $current_page_id = I("page_id/d");
  203. $login_user = session("login_user");
  204. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  205. //获取页面
  206. $page = D("Page")->where(" item_id = '$item_id' ")->find();
  207. $domain = $item['item_domain'] ? $item['item_domain'] : $item['item_id'];
  208. $share_url = get_domain().__APP__.'/'.$domain;
  209. $ItemPermn = $this->checkItemPermn($uid , $item_id) ;
  210. $ItemCreator = $this->checkItemCreator($uid , $item_id);
  211. $this->assign("current_page_id" , $current_page_id);
  212. $this->assign("ItemPermn" , $ItemPermn);
  213. $this->assign("ItemCreator" , $ItemCreator);
  214. $this->assign("share_url" , $share_url);
  215. $this->assign("catalogs" , $catalogs);
  216. $this->assign("page" , $page);
  217. $this->assign("item" , $item);
  218. $this->assign("login_user" , $login_user);
  219. $this->display("show_single_page");
  220. }
  221. //删除项目
  222. public function delete(){
  223. $item_id = I("item_id");
  224. $login_user = $this->checkLogin();
  225. if (!$this->checkItemCreator($login_user['uid'] , $item_id)) {
  226. $this->message(L('no_permissions'));
  227. return;
  228. }
  229. $this->assign("item_id" , $item_id);
  230. $this->display();
  231. }
  232. //删除项目
  233. public function ajaxDelete(){
  234. $login_user = $this->checkLogin();
  235. $item_id = I("item_id/d");
  236. $password = I("password");
  237. $item = D("Item")->where("item_id = '$item_id' ")->find();
  238. if(! D("User")-> checkLogin($item['username'],$password)){
  239. $return['error_code'] = 10102 ;
  240. $return['error_message'] = L('incorrect_password') ;
  241. $this->sendResult($return);
  242. return ;
  243. }
  244. D("Page")->where("item_id = '$item_id' ")->delete();
  245. D("Catalog")->where("item_id = '$item_id' ")->delete();
  246. D("PageHistory")->where("item_id = '$item_id' ")->delete();
  247. D("ItemMember")->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 export(){
  288. $login_user = $this->checkLogin();
  289. $item_id = I("item_id/d");
  290. $uid = $login_user['uid'] ;
  291. $this->checkItemPermn($uid , $item_id) ;
  292. $item = D("Item")->where("item_id = '$item_id' ")->find();
  293. //对于单页项目,直接导出。对于普通项目,则让其选择目录
  294. if ($item['item_type'] == 2 ) {
  295. $url = 'server/index.php?s=/api/export/word&item_id='.$item_id ;
  296. header("location:{$url}");
  297. }else{
  298. $this->assign("item_id",$item_id);
  299. $this->display();
  300. }
  301. }
  302. public function itemList(){
  303. $login_user = $this->checkLogin();
  304. $items = D("Item")->where("uid = '$login_user[uid]' ")->select();
  305. $items = $items ? $items : array();
  306. $this->sendResult($items);
  307. }
  308. public function setting(){
  309. $login_user = $this->checkLogin();
  310. $item_id = I("item_id/d");
  311. $uid = $login_user['uid'] ;
  312. $this->checkItemPermn($uid , $item_id) ;
  313. $this->assign("item_id",$item_id);
  314. $this->display();
  315. }
  316. }