ItemController.class.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. $api_key = I("api_key");
  332. $api_token = I("api_token");
  333. $cat_name = I("cat_name");
  334. $cat_name_sub = I("cat_name_sub");
  335. $page_title = I("page_title");
  336. $page_content = I("page_content");
  337. $s_number = I("s_number") ? I("s_number") : 99;
  338. $ret = D("ItemToken")->getTokenByKey($api_key);
  339. if ($ret && $ret['api_token'] == $api_token) {
  340. $item_id = $ret['item_id'] ;
  341. D("ItemToken")->setLastTime($item_id);
  342. }else{
  343. $this->sendError(10306);
  344. return false;
  345. }
  346. //如果传送了二级目录
  347. if ($cat_name) {
  348. $cat_name_array = D("Catalog")->where(" item_id = '$item_id' and level = 2 and cat_name = '%s' ",array($cat_name))->find();
  349. //如果不存在则新建
  350. if (!$cat_name_array) {
  351. $add_data = array(
  352. "cat_name" => $cat_name,
  353. "item_id" => $item_id,
  354. "addtime" => time(),
  355. "level" => 2
  356. );
  357. D("Catalog")->add($add_data);
  358. $cat_name_array = D("Catalog")->where(" item_id = '$item_id' and level = 2 and cat_name = '%s' ",array($cat_name))->find();
  359. }
  360. }
  361. //如果传送了三级目录
  362. if ($cat_name_sub) {
  363. $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();
  364. //如果不存在则新建
  365. if (!$cat_name_sub_array) {
  366. $add_data = array(
  367. "cat_name" => $cat_name_sub,
  368. "item_id" => $item_id,
  369. "parent_cat_id" => $cat_name_array['cat_id'],
  370. "addtime" => time(),
  371. "level" => 3
  372. );
  373. D("Catalog")->add($add_data);
  374. $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();
  375. }
  376. }
  377. //目录id
  378. $cat_id = 0 ;
  379. if ($cat_name_array && $cat_name_array['cat_id'] > 0 ) {
  380. $cat_id = $cat_name_array['cat_id'] ;
  381. }
  382. if ($cat_name_sub_array && $cat_name_sub_array['cat_id'] > 0 ) {
  383. $cat_id = $cat_name_sub_array['cat_id'] ;
  384. }
  385. if ($page_content) {
  386. $page_array = D("Page")->where(" item_id = '$item_id' and cat_id = '$cat_id' and page_title ='%s' ",array($page_title))->find();
  387. //如果不存在则新建
  388. if (!$page_array) {
  389. $add_data = array(
  390. "author_username" => "from_api",
  391. "item_id" => $item_id,
  392. "cat_id" => $cat_id,
  393. "page_title" => $page_title,
  394. "page_content" => $page_content,
  395. "s_number" => $s_number,
  396. "addtime" => time(),
  397. );
  398. $page_id = D("Page")->add($add_data);
  399. }else{
  400. $page_id = $page_array['page_id'] ;
  401. $update_data = array(
  402. "author_username" => "from_api",
  403. "item_id" => $item_id,
  404. "cat_id" => $cat_id,
  405. "page_title" => $page_title,
  406. "page_content" => $page_content,
  407. "s_number" => $s_number,
  408. );
  409. D("Page")->where(" page_id = '$page_id' ")->save($update_data);
  410. }
  411. }
  412. if ($page_id) {
  413. $ret = D("Page")->where(" page_id = '$page_id' ")->find();
  414. $this->sendResult($ret);
  415. }else{
  416. $this->sendError(10101);
  417. }
  418. }
  419. //置顶项目
  420. public function top(){
  421. $login_user = $this->checkLogin();
  422. $item_id = I("item_id/d");
  423. $action = I("action");
  424. if ($action == 'top') {
  425. $ret = D("ItemTop")->add(array("item_id"=>$item_id,"uid"=>$login_user['uid'],"addtime"=>time()));
  426. }
  427. elseif ($action == 'cancel') {
  428. $ret = D("ItemTop")->where(" uid = '$login_user[uid]' and item_id = '$item_id' ")->delete();
  429. }
  430. if ($ret) {
  431. $this->sendResult(array());
  432. }else{
  433. $this->sendError(10101);
  434. }
  435. }
  436. //验证访问密码
  437. public function pwd(){
  438. $item_id = I("item_id/d");
  439. $password = I("password");
  440. $v_code = I("v_code");
  441. $refer_url = I('refer_url');
  442. //检查用户输错密码的次数。如果超过一定次数,则需要验证 验证码
  443. $key= 'item_pwd_fail_times_'.$item_id;
  444. if(!D("VerifyCode")->_check_times($key,10)){
  445. if (!$v_code || $v_code != session('v_code')) {
  446. $this->sendError(10206,L('verification_code_are_incorrect'));
  447. return;
  448. }
  449. }
  450. $item = D("Item")->where("item_id = '$item_id' ")->find();
  451. if ($item['password'] == $password) {
  452. session("visit_item_".$item_id , 1 );
  453. $this->sendResult(array("refer_url"=>base64_decode($refer_url)));
  454. }else{
  455. D("VerifyCode")->_ins_times($key);//输错密码则设置输错次数
  456. if(D("VerifyCode")->_check_times($key,10)){
  457. $error_code = 10307 ;
  458. }else{
  459. $error_code = 10308 ;
  460. }
  461. $this->sendError($error_code,L('access_password_are_incorrect'));
  462. }
  463. }
  464. public function itemList(){
  465. $login_user = $this->checkLogin();
  466. $items = D("Item")->where("uid = '$login_user[uid]' ")->select();
  467. $items = $items ? $items : array();
  468. $this->sendResult($items);
  469. }
  470. //新建项目
  471. public function add(){
  472. $login_user = $this->checkLogin();
  473. $item_name = I("item_name");
  474. $item_domain = I("item_domain") ? I("item_domain") : '';
  475. $copy_item_id = I("copy_item_id");
  476. $password = I("password");
  477. $item_description = I("item_description");
  478. $item_type = I("item_type");
  479. if ($item_domain) {
  480. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  481. //echo '个性域名只能是字母或数字的组合';exit;
  482. $this->sendError(10305);
  483. return false;
  484. }
  485. $item = D("Item")->where("item_domain = '%s' ",array($item_domain))->find();
  486. if ($item) {
  487. //个性域名已经存在
  488. $this->sendError(10304);
  489. return false;
  490. }
  491. }
  492. //如果是复制项目
  493. if ($copy_item_id > 0) {
  494. if (!$this->checkItemPermn($login_user['uid'] , $copy_item_id)) {
  495. $this->sendError(10103);
  496. return;
  497. }
  498. $ret = D("Item")->copy($copy_item_id,$login_user['uid'],$item_name,$item_description,$password,$item_domain);
  499. if ($ret) {
  500. $this->sendResult(array());
  501. }else{
  502. $this->sendError(10101);
  503. }
  504. return ;
  505. }
  506. $insert = array(
  507. "uid" => $login_user['uid'] ,
  508. "username" => $login_user['username'] ,
  509. "item_name" => $item_name ,
  510. "password" => $password ,
  511. "item_description" => $item_description ,
  512. "item_domain" => $item_domain ,
  513. "item_type" => $item_type ,
  514. "addtime" =>time()
  515. );
  516. $item_id = D("Item")->add($insert);
  517. if ($item_id) {
  518. //如果是单页应用,则新建一个默认页
  519. if ($item_type == 2 ) {
  520. $insert = array(
  521. 'author_uid' => $login_user['uid'] ,
  522. 'author_username' => $login_user['username'],
  523. "page_title" => $item_name ,
  524. "item_id" => $item_id ,
  525. "cat_id" => 0 ,
  526. "page_content" => '点击右上方的编辑按钮进行编辑吧!' ,
  527. "addtime" =>time()
  528. );
  529. $page_id = D("Page")->add($insert);
  530. }
  531. $this->sendResult(array());
  532. }else{
  533. $this->sendError(10101);
  534. }
  535. }
  536. }