ItemController.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class ItemController extends BaseController {
  5. //单个项目信息
  6. public function info(){
  7. $this->checkLogin(false);
  8. $item_id = I("item_id");
  9. $item_domain = I("item_domain/s");
  10. $current_page_id = I("page_id/d");
  11. if (! is_numeric($item_id)) {
  12. $item_domain = $item_id ;
  13. }
  14. //判断个性域名
  15. if ($item_domain) {
  16. $item = D("Item")->where("item_domain = '%s'",array($item_domain))->find();
  17. if ($item['item_id']) {
  18. $item_id = $item['item_id'] ;
  19. }
  20. }
  21. $login_user = session("login_user");
  22. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  23. if(!$this->checkItemVisit($uid , $item_id)){
  24. $this->sendError(10303);
  25. return ;
  26. }
  27. $item = D("Item")->where("item_id = '$item_id' ")->find();
  28. if (!$item) {
  29. sleep(1);
  30. $this->sendError(10101,'项目不存在或者已删除');
  31. return false;
  32. }
  33. if ($item['item_type'] == 1 ) {
  34. $this->_show_regular_item($item);
  35. }
  36. elseif ($item['item_type'] == 2 ) {
  37. $this->_show_single_page_item($item);
  38. }else{
  39. $this->_show_regular_item($item);
  40. }
  41. }
  42. //展示常规项目
  43. private function _show_regular_item($item){
  44. $item_id = $item['item_id'];
  45. $default_page_id = I("default_page_id/d");
  46. $keyword = I("keyword");
  47. $default_cat_id2 = $default_cat_id3 = 0 ;
  48. $login_user = session("login_user");
  49. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  50. $is_login = $uid > 0 ? true :false;
  51. //是否有搜索词
  52. if ($keyword) {
  53. $keyword = \SQLite3::escapeString($keyword) ;
  54. $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();
  55. }else{
  56. //获取所有父目录id为0的页面
  57. $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();
  58. //获取所有二级目录
  59. $catalogs = D("Catalog")->where("item_id = '$item_id' and level = 2 ")->order(" `s_number` asc ")->select();
  60. if ($catalogs) {
  61. foreach ($catalogs as $key => &$catalog) {
  62. //该二级目录下的所有子页面
  63. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->field("page_id,author_uid,cat_id,page_title,addtime")->select();
  64. $catalog['pages'] = $temp ? $temp: array();
  65. //该二级目录下的所有子目录
  66. $temp = D("catalog")->where("parent_cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  67. $catalog['catalogs'] = $temp ? $temp: array();
  68. if($catalog['catalogs']){
  69. //获取所有三级目录的子页面
  70. foreach ($catalog['catalogs'] as $key3 => &$catalog3) {
  71. //该二级目录下的所有子页面
  72. $temp = D("Page")->where("cat_id = '$catalog3[cat_id]' ")->order(" `s_number` asc ")->field("page_id,author_uid,cat_id,page_title,addtime")->select();
  73. $catalog3['pages'] = $temp ? $temp: array();
  74. }
  75. }
  76. }
  77. }
  78. }
  79. $domain = $item['item_domain'] ? $item['item_domain'] : $item['item_id'];
  80. $share_url = get_domain().__APP__.'/'.$domain;
  81. $ItemPermn = $this->checkItemPermn($uid , $item_id) ;
  82. $ItemCreator = $this->checkItemCreator($uid , $item_id);
  83. //如果带了默认展开的页面id,则获取该页面所在的二级目录和三级目录
  84. if ($default_page_id) {
  85. $page = D("Page")->where(" page_id = '$default_page_id' ")->find();
  86. if ($page) {
  87. $default_cat_id3 = $page['cat_id'] ;
  88. $cat2 = D("Catalog")->where(" cat_id = '$default_cat_id3' and parent_cat_id > 0 ")->find();
  89. if ($cat2) {
  90. $default_cat_id2 = $cat2['parent_cat_id'];
  91. }else{
  92. $default_cat_id2 = $default_cat_id3;
  93. $default_cat_id3 = 0 ;
  94. }
  95. }
  96. }
  97. if (LANG_SET == 'en-us') {
  98. $help_url = "https://www.showdoc.cc/help-en";
  99. }
  100. else{
  101. $help_url = "https://www.showdoc.cc/help";
  102. }
  103. $menu =array(
  104. "pages" => $pages ,
  105. "catalogs" => $catalogs ,
  106. ) ;
  107. $return = array(
  108. "item_id"=>$item_id ,
  109. "item_domain"=>$item['item_domain'] ,
  110. "is_archived"=>$item['is_archived'] ,
  111. "default_page_id"=>(string)$default_page_id ,
  112. "default_cat_id2"=>$default_cat_id2 ,
  113. "default_cat_id3"=>$default_cat_id3 ,
  114. "unread_count"=>$unread_count ,
  115. "item_type"=>1 ,
  116. "menu"=>$menu ,
  117. "is_login"=>$is_login,
  118. "ItemPermn"=>$ItemPermn ,
  119. "ItemCreator"=>$ItemCreator ,
  120. );
  121. $this->sendResult($return);
  122. }
  123. //展示单页项目
  124. private function _show_single_page_item($item){
  125. $item_id = $item['item_id'];
  126. $current_page_id = I("page_id/d");
  127. $login_user = session("login_user");
  128. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  129. $is_login = $uid > 0 ? true :false;
  130. //获取页面
  131. $page = D("Page")->where(" item_id = '$item_id' ")->find();
  132. $domain = $item['item_domain'] ? $item['item_domain'] : $item['item_id'];
  133. $share_url = get_domain().__APP__.'/'.$domain;
  134. $ItemPermn = $this->checkItemPermn($uid , $item_id) ;
  135. $ItemCreator = $this->checkItemCreator($uid , $item_id);
  136. $menu = array() ;
  137. $menu['pages'] = $page ;
  138. $return = array(
  139. "item_id"=>$item_id ,
  140. "item_domain"=>$item['item_domain'] ,
  141. "is_archived"=>$item['is_archived'] ,
  142. "current_page_id"=>$current_page_id ,
  143. "unread_count"=>$unread_count ,
  144. "item_type"=>2 ,
  145. "menu"=>$menu ,
  146. "is_login"=>$is_login,
  147. "ItemPermn"=>$ItemPermn ,
  148. "ItemCreator"=>$ItemCreator ,
  149. );
  150. $this->sendResult($return);
  151. }
  152. //我的项目列表
  153. public function myList(){
  154. $login_user = $this->checkLogin();
  155. $items = D("Item")->field("item_id,item_name,last_update_time,item_description")->where("uid = '$login_user[uid]' or item_id in ( select item_id from ".C('DB_PREFIX')."item_member where uid = '$login_user[uid]' ) ")->order("item_id asc")->select();
  156. //读取需要置顶的项目
  157. $top_items = D("ItemTop")->where("uid = '$login_user[uid]'")->select();
  158. if ($top_items) {
  159. $top_item_ids = array() ;
  160. foreach ($top_items as $key => $value) {
  161. $top_item_ids[] = $value['item_id'];
  162. }
  163. foreach ($items as $key => $value) {
  164. $items[$key]['top'] = 0 ;
  165. if (in_array($value['item_id'], $top_item_ids) ) {
  166. $items[$key]['top'] = 1 ;
  167. $tmp = $items[$key] ;
  168. unset($items[$key]);
  169. array_unshift($items,$tmp) ;
  170. }
  171. }
  172. $items = array_values($items);
  173. }
  174. $items = $items ? $items : array();
  175. $this->sendResult($items);
  176. }
  177. //项目详情
  178. public function detail(){
  179. $login_user = $this->checkLogin();
  180. $item_id = I("item_id/d");
  181. $uid = $login_user['uid'] ;
  182. if(!$this->checkItemCreator($uid , $item_id)){
  183. $this->sendError(10303);
  184. return ;
  185. }
  186. $items = D("Item")->where("item_id = '$item_id' ")->find();
  187. $items = $items ? $items : array();
  188. $this->sendResult($items);
  189. }
  190. //更新项目信息
  191. public function update(){
  192. $login_user = $this->checkLogin();
  193. $item_id = I("item_id/d");
  194. $item_name = I("item_name");
  195. $item_description = I("item_description");
  196. $item_domain = I("item_domain");
  197. $password = I("password");
  198. $uid = $login_user['uid'] ;
  199. if(!$this->checkItemCreator($uid , $item_id)){
  200. $this->sendError(10303);
  201. return ;
  202. }
  203. if ($item_domain) {
  204. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  205. //echo '个性域名只能是字母或数字的组合';exit;
  206. $this->sendError(10305);
  207. return false;
  208. }
  209. $item = D("Item")->where("item_domain = '%s' and item_id !='%s' ",array($item_domain,$item_id))->find();
  210. if ($item) {
  211. //个性域名已经存在
  212. $this->sendError(10304);
  213. return false;
  214. }
  215. }
  216. $save_data = array(
  217. "item_name" => $item_name ,
  218. "item_description" => $item_description ,
  219. "item_domain" => $item_domain ,
  220. "password" => $password ,
  221. );
  222. $items = D("Item")->where("item_id = '$item_id' ")->save($save_data);
  223. $items = $items ? $items : array();
  224. $this->sendResult($items);
  225. }
  226. //转让项目
  227. public function attorn(){
  228. $login_user = $this->checkLogin();
  229. $username = I("username");
  230. $item_id = I("item_id/d");
  231. $password = I("password");
  232. $item = D("Item")->where("item_id = '$item_id' ")->find();
  233. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  234. $this->sendError(10303);
  235. return ;
  236. }
  237. if(! D("User")-> checkLogin($item['username'],$password)){
  238. $this->sendError(10208);
  239. return ;
  240. }
  241. $member = D("User")->where(" username = '%s' ",array($username))->find();
  242. if (!$member) {
  243. $this->sendError(10209);
  244. return ;
  245. }
  246. $data['username'] = $member['username'] ;
  247. $data['uid'] = $member['uid'] ;
  248. $id = D("Item")->where(" item_id = '$item_id' ")->save($data);
  249. $return = D("Item")->where("item_id = '$item_id' ")->find();
  250. if (!$return) {
  251. $this->sendError(10101);
  252. }
  253. $this->sendResult($return);
  254. }
  255. //删除项目
  256. public function delete(){
  257. $login_user = $this->checkLogin();
  258. $item_id = I("item_id/d");
  259. $password = I("password");
  260. $item = D("Item")->where("item_id = '$item_id' ")->find();
  261. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  262. $this->sendError(10303);
  263. return ;
  264. }
  265. if(! D("User")-> checkLogin($item['username'],$password)){
  266. $this->sendError(10208);
  267. return ;
  268. }
  269. D("Page")->where("item_id = '$item_id' ")->delete();
  270. D("Catalog")->where("item_id = '$item_id' ")->delete();
  271. D("PageHistory")->where("item_id = '$item_id' ")->delete();
  272. D("ItemMember")->where("item_id = '$item_id' ")->delete();
  273. $return = D("Item")->where("item_id = '$item_id' ")->delete();
  274. if (!$return) {
  275. $this->sendError(10101);
  276. }else{
  277. }
  278. $this->sendResult($return);
  279. }
  280. //归档项目
  281. public function archive(){
  282. $login_user = $this->checkLogin();
  283. $item_id = I("item_id/d");
  284. $password = I("password");
  285. $item = D("Item")->where("item_id = '$item_id' ")->find();
  286. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  287. $this->sendError(10303);
  288. return ;
  289. }
  290. if(! D("User")-> checkLogin($item['username'],$password)){
  291. $this->sendError(10208);
  292. return ;
  293. }
  294. $return = D("Item")->where("item_id = '$item_id' ")->save(array("is_archived"=>1));
  295. if (!$return) {
  296. $this->sendError(10101);
  297. }else{
  298. $this->sendResult($return);
  299. }
  300. }
  301. public function getKey(){
  302. $login_user = $this->checkLogin();
  303. $item_id = I("item_id/d");
  304. $item = D("Item")->where("item_id = '$item_id' ")->find();
  305. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  306. $this->sendError(10303);
  307. return ;
  308. }
  309. $item_token = D("ItemToken")->getTokenByItemId($item_id);
  310. if (!$item_token) {
  311. $this->sendError(10101);
  312. }
  313. $this->sendResult($item_token);
  314. }
  315. public function resetKey(){
  316. $login_user = $this->checkLogin();
  317. $item_id = I("item_id/d");
  318. $item = D("Item")->where("item_id = '$item_id' ")->find();
  319. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  320. $this->sendError(10303);
  321. return ;
  322. }
  323. $ret = D("ItemToken")->where("item_id = '$item_id' ")->delete();
  324. if ($ret) {
  325. $this->getKey();
  326. }else{
  327. $this->sendError(10101);
  328. }
  329. }
  330. public function updateByApi(){
  331. //转到Open控制器的updateItem方法
  332. R('Open/updateItem');
  333. }
  334. //置顶项目
  335. public function top(){
  336. $login_user = $this->checkLogin();
  337. $item_id = I("item_id/d");
  338. $action = I("action");
  339. if ($action == 'top') {
  340. $ret = D("ItemTop")->add(array("item_id"=>$item_id,"uid"=>$login_user['uid'],"addtime"=>time()));
  341. }
  342. elseif ($action == 'cancel') {
  343. $ret = D("ItemTop")->where(" uid = '$login_user[uid]' and item_id = '$item_id' ")->delete();
  344. }
  345. if ($ret) {
  346. $this->sendResult(array());
  347. }else{
  348. $this->sendError(10101);
  349. }
  350. }
  351. //验证访问密码
  352. public function pwd(){
  353. $item_id = I("item_id/d");
  354. $password = I("password");
  355. $v_code = I("v_code");
  356. $refer_url = I('refer_url');
  357. //检查用户输错密码的次数。如果超过一定次数,则需要验证 验证码
  358. $key= 'item_pwd_fail_times_'.$item_id;
  359. if(!D("VerifyCode")->_check_times($key,10)){
  360. if (!$v_code || $v_code != session('v_code')) {
  361. $this->sendError(10206,L('verification_code_are_incorrect'));
  362. return;
  363. }
  364. }
  365. $item = D("Item")->where("item_id = '$item_id' ")->find();
  366. if ($item['password'] == $password) {
  367. session("visit_item_".$item_id , 1 );
  368. $this->sendResult(array("refer_url"=>base64_decode($refer_url)));
  369. }else{
  370. D("VerifyCode")->_ins_times($key);//输错密码则设置输错次数
  371. if(D("VerifyCode")->_check_times($key,10)){
  372. $error_code = 10307 ;
  373. }else{
  374. $error_code = 10308 ;
  375. }
  376. $this->sendError($error_code,L('access_password_are_incorrect'));
  377. }
  378. }
  379. public function itemList(){
  380. $login_user = $this->checkLogin();
  381. $items = D("Item")->where("uid = '$login_user[uid]' ")->select();
  382. $items = $items ? $items : array();
  383. $this->sendResult($items);
  384. }
  385. //新建项目
  386. public function add(){
  387. $login_user = $this->checkLogin();
  388. $item_name = I("item_name");
  389. $item_domain = I("item_domain") ? I("item_domain") : '';
  390. $copy_item_id = I("copy_item_id");
  391. $password = I("password");
  392. $item_description = I("item_description");
  393. $item_type = I("item_type");
  394. if ($item_domain) {
  395. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  396. //echo '个性域名只能是字母或数字的组合';exit;
  397. $this->sendError(10305);
  398. return false;
  399. }
  400. $item = D("Item")->where("item_domain = '%s' ",array($item_domain))->find();
  401. if ($item) {
  402. //个性域名已经存在
  403. $this->sendError(10304);
  404. return false;
  405. }
  406. }
  407. //如果是复制项目
  408. if ($copy_item_id > 0) {
  409. if (!$this->checkItemPermn($login_user['uid'] , $copy_item_id)) {
  410. $this->sendError(10103);
  411. return;
  412. }
  413. $ret = D("Item")->copy($copy_item_id,$login_user['uid'],$item_name,$item_description,$password,$item_domain);
  414. if ($ret) {
  415. $this->sendResult(array());
  416. }else{
  417. $this->sendError(10101);
  418. }
  419. return ;
  420. }
  421. $insert = array(
  422. "uid" => $login_user['uid'] ,
  423. "username" => $login_user['username'] ,
  424. "item_name" => $item_name ,
  425. "password" => $password ,
  426. "item_description" => $item_description ,
  427. "item_domain" => $item_domain ,
  428. "item_type" => $item_type ,
  429. "addtime" =>time()
  430. );
  431. $item_id = D("Item")->add($insert);
  432. if ($item_id) {
  433. //如果是单页应用,则新建一个默认页
  434. if ($item_type == 2 ) {
  435. $insert = array(
  436. 'author_uid' => $login_user['uid'] ,
  437. 'author_username' => $login_user['username'],
  438. "page_title" => $item_name ,
  439. "item_id" => $item_id ,
  440. "cat_id" => 0 ,
  441. "page_content" => '欢迎使用showdoc。点击右上方的编辑按钮进行编辑吧!' ,
  442. "addtime" =>time()
  443. );
  444. $page_id = D("Page")->add($insert);
  445. }
  446. $this->sendResult(array());
  447. }else{
  448. $this->sendError(10101);
  449. }
  450. }
  451. }