ItemController.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. "item_name"=>$item['item_name'] ,
  112. "default_page_id"=>(string)$default_page_id ,
  113. "default_cat_id2"=>$default_cat_id2 ,
  114. "default_cat_id3"=>$default_cat_id3 ,
  115. "unread_count"=>$unread_count ,
  116. "item_type"=>1 ,
  117. "menu"=>$menu ,
  118. "is_login"=>$is_login,
  119. "ItemPermn"=>$ItemPermn ,
  120. "ItemCreator"=>$ItemCreator ,
  121. );
  122. $this->sendResult($return);
  123. }
  124. //展示单页项目
  125. private function _show_single_page_item($item){
  126. $item_id = $item['item_id'];
  127. $current_page_id = I("page_id/d");
  128. $login_user = session("login_user");
  129. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  130. $is_login = $uid > 0 ? true :false;
  131. //获取页面
  132. $page = D("Page")->where(" item_id = '$item_id' ")->find();
  133. $domain = $item['item_domain'] ? $item['item_domain'] : $item['item_id'];
  134. $share_url = get_domain().__APP__.'/'.$domain;
  135. $ItemPermn = $this->checkItemPermn($uid , $item_id) ;
  136. $ItemCreator = $this->checkItemCreator($uid , $item_id);
  137. $menu = array() ;
  138. $menu['pages'] = $page ;
  139. $return = array(
  140. "item_id"=>$item_id ,
  141. "item_domain"=>$item['item_domain'] ,
  142. "is_archived"=>$item['is_archived'] ,
  143. "item_name"=>$item['item_name'] ,
  144. "current_page_id"=>$current_page_id ,
  145. "unread_count"=>$unread_count ,
  146. "item_type"=>2 ,
  147. "menu"=>$menu ,
  148. "is_login"=>$is_login,
  149. "ItemPermn"=>$ItemPermn ,
  150. "ItemCreator"=>$ItemCreator ,
  151. );
  152. $this->sendResult($return);
  153. }
  154. //我的项目列表
  155. public function myList(){
  156. $login_user = $this->checkLogin();
  157. $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();
  158. //读取需要置顶的项目
  159. $top_items = D("ItemTop")->where("uid = '$login_user[uid]'")->select();
  160. if ($top_items) {
  161. $top_item_ids = array() ;
  162. foreach ($top_items as $key => $value) {
  163. $top_item_ids[] = $value['item_id'];
  164. }
  165. foreach ($items as $key => $value) {
  166. $items[$key]['top'] = 0 ;
  167. if (in_array($value['item_id'], $top_item_ids) ) {
  168. $items[$key]['top'] = 1 ;
  169. $tmp = $items[$key] ;
  170. unset($items[$key]);
  171. array_unshift($items,$tmp) ;
  172. }
  173. }
  174. $items = array_values($items);
  175. }
  176. $items = $items ? $items : array();
  177. $this->sendResult($items);
  178. }
  179. //项目详情
  180. public function detail(){
  181. $login_user = $this->checkLogin();
  182. $item_id = I("item_id/d");
  183. $uid = $login_user['uid'] ;
  184. if(!$this->checkItemCreator($uid , $item_id)){
  185. $this->sendError(10303);
  186. return ;
  187. }
  188. $items = D("Item")->where("item_id = '$item_id' ")->find();
  189. $items = $items ? $items : array();
  190. $this->sendResult($items);
  191. }
  192. //更新项目信息
  193. public function update(){
  194. $login_user = $this->checkLogin();
  195. $item_id = I("item_id/d");
  196. $item_name = I("item_name");
  197. $item_description = I("item_description");
  198. $item_domain = I("item_domain");
  199. $password = I("password");
  200. $uid = $login_user['uid'] ;
  201. if(!$this->checkItemCreator($uid , $item_id)){
  202. $this->sendError(10303);
  203. return ;
  204. }
  205. if ($item_domain) {
  206. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  207. //echo '个性域名只能是字母或数字的组合';exit;
  208. $this->sendError(10305);
  209. return false;
  210. }
  211. $item = D("Item")->where("item_domain = '%s' and item_id !='%s' ",array($item_domain,$item_id))->find();
  212. if ($item) {
  213. //个性域名已经存在
  214. $this->sendError(10304);
  215. return false;
  216. }
  217. }
  218. $save_data = array(
  219. "item_name" => $item_name ,
  220. "item_description" => $item_description ,
  221. "item_domain" => $item_domain ,
  222. "password" => $password ,
  223. );
  224. $items = D("Item")->where("item_id = '$item_id' ")->save($save_data);
  225. $items = $items ? $items : array();
  226. $this->sendResult($items);
  227. }
  228. //转让项目
  229. public function attorn(){
  230. $login_user = $this->checkLogin();
  231. $username = I("username");
  232. $item_id = I("item_id/d");
  233. $password = I("password");
  234. $item = D("Item")->where("item_id = '$item_id' ")->find();
  235. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  236. $this->sendError(10303);
  237. return ;
  238. }
  239. if(! D("User")-> checkLogin($item['username'],$password)){
  240. $this->sendError(10208);
  241. return ;
  242. }
  243. $member = D("User")->where(" username = '%s' ",array($username))->find();
  244. if (!$member) {
  245. $this->sendError(10209);
  246. return ;
  247. }
  248. $data['username'] = $member['username'] ;
  249. $data['uid'] = $member['uid'] ;
  250. $id = D("Item")->where(" item_id = '$item_id' ")->save($data);
  251. $return = D("Item")->where("item_id = '$item_id' ")->find();
  252. if (!$return) {
  253. $this->sendError(10101);
  254. }
  255. $this->sendResult($return);
  256. }
  257. //删除项目
  258. public function delete(){
  259. $login_user = $this->checkLogin();
  260. $item_id = I("item_id/d");
  261. $password = I("password");
  262. $item = D("Item")->where("item_id = '$item_id' ")->find();
  263. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  264. $this->sendError(10303);
  265. return ;
  266. }
  267. if(! D("User")-> checkLogin($item['username'],$password)){
  268. $this->sendError(10208);
  269. return ;
  270. }
  271. D("Page")->where("item_id = '$item_id' ")->delete();
  272. D("Catalog")->where("item_id = '$item_id' ")->delete();
  273. D("PageHistory")->where("item_id = '$item_id' ")->delete();
  274. D("ItemMember")->where("item_id = '$item_id' ")->delete();
  275. $return = D("Item")->where("item_id = '$item_id' ")->delete();
  276. if (!$return) {
  277. $this->sendError(10101);
  278. }else{
  279. }
  280. $this->sendResult($return);
  281. }
  282. //归档项目
  283. public function archive(){
  284. $login_user = $this->checkLogin();
  285. $item_id = I("item_id/d");
  286. $password = I("password");
  287. $item = D("Item")->where("item_id = '$item_id' ")->find();
  288. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  289. $this->sendError(10303);
  290. return ;
  291. }
  292. if(! D("User")-> checkLogin($item['username'],$password)){
  293. $this->sendError(10208);
  294. return ;
  295. }
  296. $return = D("Item")->where("item_id = '$item_id' ")->save(array("is_archived"=>1));
  297. if (!$return) {
  298. $this->sendError(10101);
  299. }else{
  300. $this->sendResult($return);
  301. }
  302. }
  303. public function getKey(){
  304. $login_user = $this->checkLogin();
  305. $item_id = I("item_id/d");
  306. $item = D("Item")->where("item_id = '$item_id' ")->find();
  307. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  308. $this->sendError(10303);
  309. return ;
  310. }
  311. $item_token = D("ItemToken")->getTokenByItemId($item_id);
  312. if (!$item_token) {
  313. $this->sendError(10101);
  314. }
  315. $this->sendResult($item_token);
  316. }
  317. public function resetKey(){
  318. $login_user = $this->checkLogin();
  319. $item_id = I("item_id/d");
  320. $item = D("Item")->where("item_id = '$item_id' ")->find();
  321. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  322. $this->sendError(10303);
  323. return ;
  324. }
  325. $ret = D("ItemToken")->where("item_id = '$item_id' ")->delete();
  326. if ($ret) {
  327. $this->getKey();
  328. }else{
  329. $this->sendError(10101);
  330. }
  331. }
  332. public function updateByApi(){
  333. //转到Open控制器的updateItem方法
  334. R('Open/updateItem');
  335. }
  336. //置顶项目
  337. public function top(){
  338. $login_user = $this->checkLogin();
  339. $item_id = I("item_id/d");
  340. $action = I("action");
  341. if ($action == 'top') {
  342. $ret = D("ItemTop")->add(array("item_id"=>$item_id,"uid"=>$login_user['uid'],"addtime"=>time()));
  343. }
  344. elseif ($action == 'cancel') {
  345. $ret = D("ItemTop")->where(" uid = '$login_user[uid]' and item_id = '$item_id' ")->delete();
  346. }
  347. if ($ret) {
  348. $this->sendResult(array());
  349. }else{
  350. $this->sendError(10101);
  351. }
  352. }
  353. //验证访问密码
  354. public function pwd(){
  355. $item_id = I("item_id/d");
  356. $password = I("password");
  357. $v_code = I("v_code");
  358. $refer_url = I('refer_url');
  359. //检查用户输错密码的次数。如果超过一定次数,则需要验证 验证码
  360. $key= 'item_pwd_fail_times_'.$item_id;
  361. if(!D("VerifyCode")->_check_times($key,10)){
  362. if (!$v_code || $v_code != session('v_code')) {
  363. $this->sendError(10206,L('verification_code_are_incorrect'));
  364. return;
  365. }
  366. }
  367. $item = D("Item")->where("item_id = '$item_id' ")->find();
  368. if ($item['password'] == $password) {
  369. session("visit_item_".$item_id , 1 );
  370. $this->sendResult(array("refer_url"=>base64_decode($refer_url)));
  371. }else{
  372. D("VerifyCode")->_ins_times($key);//输错密码则设置输错次数
  373. if(D("VerifyCode")->_check_times($key,10)){
  374. $error_code = 10307 ;
  375. }else{
  376. $error_code = 10308 ;
  377. }
  378. $this->sendError($error_code,L('access_password_are_incorrect'));
  379. }
  380. }
  381. public function itemList(){
  382. $login_user = $this->checkLogin();
  383. $items = D("Item")->where("uid = '$login_user[uid]' ")->select();
  384. $items = $items ? $items : array();
  385. $this->sendResult($items);
  386. }
  387. //新建项目
  388. public function add(){
  389. $login_user = $this->checkLogin();
  390. $item_name = I("item_name");
  391. $item_domain = I("item_domain") ? I("item_domain") : '';
  392. $copy_item_id = I("copy_item_id");
  393. $password = I("password");
  394. $item_description = I("item_description");
  395. $item_type = I("item_type");
  396. if ($item_domain) {
  397. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  398. //echo '个性域名只能是字母或数字的组合';exit;
  399. $this->sendError(10305);
  400. return false;
  401. }
  402. $item = D("Item")->where("item_domain = '%s' ",array($item_domain))->find();
  403. if ($item) {
  404. //个性域名已经存在
  405. $this->sendError(10304);
  406. return false;
  407. }
  408. }
  409. //如果是复制项目
  410. if ($copy_item_id > 0) {
  411. if (!$this->checkItemPermn($login_user['uid'] , $copy_item_id)) {
  412. $this->sendError(10103);
  413. return;
  414. }
  415. $ret = D("Item")->copy($copy_item_id,$login_user['uid'],$item_name,$item_description,$password,$item_domain);
  416. if ($ret) {
  417. $this->sendResult(array());
  418. }else{
  419. $this->sendError(10101);
  420. }
  421. return ;
  422. }
  423. $insert = array(
  424. "uid" => $login_user['uid'] ,
  425. "username" => $login_user['username'] ,
  426. "item_name" => $item_name ,
  427. "password" => $password ,
  428. "item_description" => $item_description ,
  429. "item_domain" => $item_domain ,
  430. "item_type" => $item_type ,
  431. "addtime" =>time()
  432. );
  433. $item_id = D("Item")->add($insert);
  434. if ($item_id) {
  435. //如果是单页应用,则新建一个默认页
  436. if ($item_type == 2 ) {
  437. $insert = array(
  438. 'author_uid' => $login_user['uid'] ,
  439. 'author_username' => $login_user['username'],
  440. "page_title" => $item_name ,
  441. "item_id" => $item_id ,
  442. "cat_id" => 0 ,
  443. "page_content" => '欢迎使用showdoc。点击右上方的编辑按钮进行编辑吧!' ,
  444. "addtime" =>time()
  445. );
  446. $page_id = D("Page")->add($insert);
  447. }
  448. $this->sendResult(array());
  449. }else{
  450. $this->sendError(10101);
  451. }
  452. }
  453. }