ItemController.class.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. $res = [
  176. 'data_list' => $items,
  177. 'username' => $login_user['username']
  178. ];
  179. $this->sendResult($res);
  180. }
  181. //项目详情
  182. public function detail(){
  183. $login_user = $this->checkLogin();
  184. $item_id = I("item_id/d");
  185. $uid = $login_user['uid'] ;
  186. if(!$this->checkItemCreator($uid , $item_id)){
  187. $this->sendError(10303);
  188. return ;
  189. }
  190. $items = D("Item")->where("item_id = '$item_id' ")->find();
  191. $items = $items ? $items : array();
  192. $this->sendResult($items);
  193. }
  194. //更新项目信息
  195. public function update(){
  196. $login_user = $this->checkLogin();
  197. $item_id = I("item_id/d");
  198. $item_name = I("item_name");
  199. $item_description = I("item_description");
  200. $item_domain = I("item_domain");
  201. $password = I("password");
  202. $uid = $login_user['uid'] ;
  203. if(!$this->checkItemCreator($uid , $item_id)){
  204. $this->sendError(10303);
  205. return ;
  206. }
  207. if ($item_domain) {
  208. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  209. //echo '个性域名只能是字母或数字的组合';exit;
  210. $this->sendError(10305);
  211. return false;
  212. }
  213. $item = D("Item")->where("item_domain = '%s' and item_id !='%s' ",array($item_domain,$item_id))->find();
  214. if ($item) {
  215. //个性域名已经存在
  216. $this->sendError(10304);
  217. return false;
  218. }
  219. }
  220. $save_data = array(
  221. "item_name" => $item_name ,
  222. "item_description" => $item_description ,
  223. "item_domain" => $item_domain ,
  224. "password" => $password ,
  225. );
  226. $items = D("Item")->where("item_id = '$item_id' ")->save($save_data);
  227. $items = $items ? $items : array();
  228. $this->sendResult($items);
  229. }
  230. //转让项目
  231. public function attorn(){
  232. $login_user = $this->checkLogin();
  233. $username = I("username");
  234. $item_id = I("item_id/d");
  235. $password = I("password");
  236. $item = D("Item")->where("item_id = '$item_id' ")->find();
  237. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  238. $this->sendError(10303);
  239. return ;
  240. }
  241. if(! D("User")-> checkLogin($item['username'],$password)){
  242. $this->sendError(10208);
  243. return ;
  244. }
  245. $member = D("User")->where(" username = '%s' ",array($username))->find();
  246. if (!$member) {
  247. $this->sendError(10209);
  248. return ;
  249. }
  250. $data['username'] = $member['username'] ;
  251. $data['uid'] = $member['uid'] ;
  252. $id = D("Item")->where(" item_id = '$item_id' ")->save($data);
  253. $return = D("Item")->where("item_id = '$item_id' ")->find();
  254. if (!$return) {
  255. $this->sendError(10101);
  256. }
  257. $this->sendResult($return);
  258. }
  259. //删除项目
  260. public function delete(){
  261. $login_user = $this->checkLogin();
  262. $item_id = I("item_id/d");
  263. $password = I("password");
  264. $item = D("Item")->where("item_id = '$item_id' ")->find();
  265. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  266. $this->sendError(10303);
  267. return ;
  268. }
  269. if(! D("User")-> checkLogin($item['username'],$password)){
  270. $this->sendError(10208);
  271. return ;
  272. }
  273. D("Page")->where("item_id = '$item_id' ")->delete();
  274. D("Catalog")->where("item_id = '$item_id' ")->delete();
  275. D("PageHistory")->where("item_id = '$item_id' ")->delete();
  276. D("ItemMember")->where("item_id = '$item_id' ")->delete();
  277. $return = D("Item")->where("item_id = '$item_id' ")->delete();
  278. if (!$return) {
  279. $this->sendError(10101);
  280. }else{
  281. }
  282. $this->sendResult($return);
  283. }
  284. //归档项目
  285. public function archive(){
  286. $login_user = $this->checkLogin();
  287. $item_id = I("item_id/d");
  288. $password = I("password");
  289. $item = D("Item")->where("item_id = '$item_id' ")->find();
  290. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  291. $this->sendError(10303);
  292. return ;
  293. }
  294. if(! D("User")-> checkLogin($item['username'],$password)){
  295. $this->sendError(10208);
  296. return ;
  297. }
  298. $return = D("Item")->where("item_id = '$item_id' ")->save(array("is_archived"=>1));
  299. if (!$return) {
  300. $this->sendError(10101);
  301. }else{
  302. $this->sendResult($return);
  303. }
  304. }
  305. public function getKey(){
  306. $login_user = $this->checkLogin();
  307. $item_id = I("item_id/d");
  308. $item = D("Item")->where("item_id = '$item_id' ")->find();
  309. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  310. $this->sendError(10303);
  311. return ;
  312. }
  313. $item_token = D("ItemToken")->getTokenByItemId($item_id);
  314. if (!$item_token) {
  315. $this->sendError(10101);
  316. }
  317. $this->sendResult($item_token);
  318. }
  319. public function resetKey(){
  320. $login_user = $this->checkLogin();
  321. $item_id = I("item_id/d");
  322. $item = D("Item")->where("item_id = '$item_id' ")->find();
  323. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  324. $this->sendError(10303);
  325. return ;
  326. }
  327. $ret = D("ItemToken")->where("item_id = '$item_id' ")->delete();
  328. if ($ret) {
  329. $this->getKey();
  330. }else{
  331. $this->sendError(10101);
  332. }
  333. }
  334. public function updateByApi(){
  335. $api_key = I("api_key");
  336. $api_token = I("api_token");
  337. $cat_name = I("cat_name");
  338. $cat_name_sub = I("cat_name_sub");
  339. $page_title = I("page_title");
  340. $page_content = I("page_content");
  341. $s_number = I("s_number") ? I("s_number") : 99;
  342. $ret = D("ItemToken")->getTokenByKey($api_key);
  343. if ($ret && $ret['api_token'] == $api_token) {
  344. $item_id = $ret['item_id'] ;
  345. D("ItemToken")->setLastTime($item_id);
  346. }else{
  347. $this->sendError(10306);
  348. return false;
  349. }
  350. //如果传送了二级目录
  351. if ($cat_name) {
  352. $cat_name_array = D("Catalog")->where(" item_id = '$item_id' and level = 2 and cat_name = '%s' ",array($cat_name))->find();
  353. //如果不存在则新建
  354. if (!$cat_name_array) {
  355. $add_data = array(
  356. "cat_name" => $cat_name,
  357. "item_id" => $item_id,
  358. "addtime" => time(),
  359. "level" => 2
  360. );
  361. D("Catalog")->add($add_data);
  362. $cat_name_array = D("Catalog")->where(" item_id = '$item_id' and level = 2 and cat_name = '%s' ",array($cat_name))->find();
  363. }
  364. }
  365. //如果传送了三级目录
  366. if ($cat_name_sub) {
  367. $cat_name_sub_array = D("Catalog")->where(" item_id = '$item_id' and level = 3 and cat_name = '%s' and parent_cat_id = '%s' ",array($cat_name_sub,$cat_name_array['cat_id']))->find();
  368. //如果不存在则新建
  369. if (!$cat_name_sub_array) {
  370. $add_data = array(
  371. "cat_name" => $cat_name_sub,
  372. "item_id" => $item_id,
  373. "parent_cat_id" => $cat_name_array['cat_id'],
  374. "addtime" => time(),
  375. "level" => 3
  376. );
  377. D("Catalog")->add($add_data);
  378. $cat_name_sub_array = D("Catalog")->where(" item_id = '$item_id' and level = 3 and cat_name = '%s' and parent_cat_id = '%s' ",array($cat_name_sub,$cat_name_array['cat_id']))->find();
  379. }
  380. }
  381. //目录id
  382. $cat_id = 0 ;
  383. if ($cat_name_array && $cat_name_array['cat_id'] > 0 ) {
  384. $cat_id = $cat_name_array['cat_id'] ;
  385. }
  386. if ($cat_name_sub_array && $cat_name_sub_array['cat_id'] > 0 ) {
  387. $cat_id = $cat_name_sub_array['cat_id'] ;
  388. }
  389. if ($page_content) {
  390. $page_array = D("Page")->where(" item_id = '$item_id' and cat_id = '$cat_id' and page_title ='%s' ",array($page_title))->find();
  391. //如果不存在则新建
  392. if (!$page_array) {
  393. $add_data = array(
  394. "author_username" => "from_api",
  395. "item_id" => $item_id,
  396. "cat_id" => $cat_id,
  397. "page_title" => $page_title,
  398. "page_content" => $page_content,
  399. "s_number" => $s_number,
  400. "addtime" => time(),
  401. );
  402. $page_id = D("Page")->add($add_data);
  403. }else{
  404. $page_id = $page_array['page_id'] ;
  405. $update_data = array(
  406. "author_username" => "from_api",
  407. "item_id" => $item_id,
  408. "cat_id" => $cat_id,
  409. "page_title" => $page_title,
  410. "page_content" => $page_content,
  411. "s_number" => $s_number,
  412. );
  413. D("Page")->where(" page_id = '$page_id' ")->save($update_data);
  414. }
  415. }
  416. if ($page_id) {
  417. $ret = D("Page")->where(" page_id = '$page_id' ")->find();
  418. $this->sendResult($ret);
  419. }else{
  420. $this->sendError(10101);
  421. }
  422. }
  423. //置顶项目
  424. public function top(){
  425. $login_user = $this->checkLogin();
  426. $item_id = I("item_id/d");
  427. $action = I("action");
  428. if ($action == 'top') {
  429. $ret = D("ItemTop")->add(array("item_id"=>$item_id,"uid"=>$login_user['uid'],"addtime"=>time()));
  430. }
  431. elseif ($action == 'cancel') {
  432. $ret = D("ItemTop")->where(" uid = '$login_user[uid]' and item_id = '$item_id' ")->delete();
  433. }
  434. if ($ret) {
  435. $this->sendResult(array());
  436. }else{
  437. $this->sendError(10101);
  438. }
  439. }
  440. //验证访问密码
  441. public function pwd(){
  442. $item_id = I("item_id/d");
  443. $password = I("password");
  444. $v_code = I("v_code");
  445. $refer_url = I('refer_url');
  446. //检查用户输错密码的次数。如果超过一定次数,则需要验证 验证码
  447. $key= 'item_pwd_fail_times_'.$item_id;
  448. if(!D("VerifyCode")->_check_times($key,10)){
  449. if (!$v_code || $v_code != session('v_code')) {
  450. $this->sendError(10206,L('verification_code_are_incorrect'));
  451. return;
  452. }
  453. }
  454. $item = D("Item")->where("item_id = '$item_id' ")->find();
  455. if ($item['password'] == $password) {
  456. session("visit_item_".$item_id , 1 );
  457. $this->sendResult(array("refer_url"=>base64_decode($refer_url)));
  458. }else{
  459. D("VerifyCode")->_ins_times($key);//输错密码则设置输错次数
  460. if(D("VerifyCode")->_check_times($key,10)){
  461. $error_code = 10307 ;
  462. }else{
  463. $error_code = 10308 ;
  464. }
  465. $this->sendError($error_code,L('access_password_are_incorrect'));
  466. }
  467. }
  468. public function itemList(){
  469. $login_user = $this->checkLogin();
  470. $items = D("Item")->where("uid = '$login_user[uid]' ")->select();
  471. $items = $items ? $items : array();
  472. $this->sendResult($items);
  473. }
  474. //新建项目
  475. public function add(){
  476. $login_user = $this->checkLogin();
  477. $item_name = I("item_name");
  478. $item_domain = I("item_domain") ? I("item_domain") : '';
  479. $copy_item_id = I("copy_item_id");
  480. $password = I("password");
  481. $item_description = I("item_description");
  482. $item_type = I("item_type");
  483. if ($item_domain) {
  484. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  485. //echo '个性域名只能是字母或数字的组合';exit;
  486. $this->sendError(10305);
  487. return false;
  488. }
  489. $item = D("Item")->where("item_domain = '%s' ",array($item_domain))->find();
  490. if ($item) {
  491. //个性域名已经存在
  492. $this->sendError(10304);
  493. return false;
  494. }
  495. }
  496. //如果是复制项目
  497. if ($copy_item_id > 0) {
  498. if (!$this->checkItemPermn($login_user['uid'] , $copy_item_id)) {
  499. $this->sendError(10103);
  500. return;
  501. }
  502. $ret = D("Item")->copy($copy_item_id,$login_user['uid'],$item_name,$item_description,$password,$item_domain);
  503. if ($ret) {
  504. $this->sendResult(array());
  505. }else{
  506. $this->sendError(10101);
  507. }
  508. return ;
  509. }
  510. $insert = array(
  511. "uid" => $login_user['uid'] ,
  512. "username" => $login_user['username'] ,
  513. "item_name" => $item_name ,
  514. "password" => $password ,
  515. "item_description" => $item_description ,
  516. "item_domain" => $item_domain ,
  517. "item_type" => $item_type ,
  518. "addtime" =>time()
  519. );
  520. $item_id = D("Item")->add($insert);
  521. if ($item_id) {
  522. //如果是单页应用,则新建一个默认页
  523. if ($item_type == 2 ) {
  524. $insert = array(
  525. 'author_uid' => $login_user['uid'] ,
  526. 'author_username' => $login_user['username'],
  527. "page_title" => $item_name ,
  528. "item_id" => $item_id ,
  529. "cat_id" => 0 ,
  530. "page_content" => '点击右上方的编辑按钮进行编辑吧!' ,
  531. "addtime" =>time()
  532. );
  533. $page_id = D("Page")->add($insert);
  534. }
  535. $this->sendResult(array());
  536. }else{
  537. $this->sendError(10101);
  538. }
  539. }
  540. }