ItemController.class.php 20 KB

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