ItemController.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 || $item['is_del'] == 1) {
  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 is_del = 0 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' and is_del = 0")->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]' and is_del = 0")->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]' and is_del = 0")->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,is_del")->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. foreach ($items as $key => $value) {
  159. //如果项目已标识为删除
  160. if ($value['is_del'] == 1) {
  161. unset($items[$key]);
  162. }
  163. }
  164. $items = array_values($items);
  165. //读取需要置顶的项目
  166. $top_items = D("ItemTop")->where("uid = '$login_user[uid]'")->select();
  167. if ($top_items) {
  168. $top_item_ids = array() ;
  169. foreach ($top_items as $key => $value) {
  170. $top_item_ids[] = $value['item_id'];
  171. }
  172. foreach ($items as $key => $value) {
  173. $items[$key]['top'] = 0 ;
  174. if (in_array($value['item_id'], $top_item_ids) ) {
  175. $items[$key]['top'] = 1 ;
  176. $tmp = $items[$key] ;
  177. unset($items[$key]);
  178. array_unshift($items,$tmp) ;
  179. }
  180. }
  181. }
  182. $items = $items ? array_values($items) : array();
  183. $this->sendResult($items);
  184. }
  185. //项目详情
  186. public function detail(){
  187. $login_user = $this->checkLogin();
  188. $item_id = I("item_id/d");
  189. $uid = $login_user['uid'] ;
  190. if(!$this->checkItemCreator($uid , $item_id)){
  191. $this->sendError(10303);
  192. return ;
  193. }
  194. $items = D("Item")->where("item_id = '$item_id' ")->find();
  195. $items = $items ? $items : array();
  196. $this->sendResult($items);
  197. }
  198. //更新项目信息
  199. public function update(){
  200. $login_user = $this->checkLogin();
  201. $item_id = I("item_id/d");
  202. $item_name = I("item_name");
  203. $item_description = I("item_description");
  204. $item_domain = I("item_domain");
  205. $password = I("password");
  206. $uid = $login_user['uid'] ;
  207. if(!$this->checkItemCreator($uid , $item_id)){
  208. $this->sendError(10303);
  209. return ;
  210. }
  211. if ($item_domain) {
  212. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  213. //echo '个性域名只能是字母或数字的组合';exit;
  214. $this->sendError(10305);
  215. return false;
  216. }
  217. $item = D("Item")->where("item_domain = '%s' and item_id !='%s' ",array($item_domain,$item_id))->find();
  218. if ($item) {
  219. //个性域名已经存在
  220. $this->sendError(10304);
  221. return false;
  222. }
  223. }
  224. $save_data = array(
  225. "item_name" => $item_name ,
  226. "item_description" => $item_description ,
  227. "item_domain" => $item_domain ,
  228. "password" => $password ,
  229. );
  230. $items = D("Item")->where("item_id = '$item_id' ")->save($save_data);
  231. $items = $items ? $items : array();
  232. $this->sendResult($items);
  233. }
  234. //转让项目
  235. public function attorn(){
  236. $login_user = $this->checkLogin();
  237. $username = I("username");
  238. $item_id = I("item_id/d");
  239. $password = I("password");
  240. $item = D("Item")->where("item_id = '$item_id' ")->find();
  241. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  242. $this->sendError(10303);
  243. return ;
  244. }
  245. if(! D("User")-> checkLogin($item['username'],$password)){
  246. $this->sendError(10208);
  247. return ;
  248. }
  249. $member = D("User")->where(" username = '%s' ",array($username))->find();
  250. if (!$member) {
  251. $this->sendError(10209);
  252. return ;
  253. }
  254. $data['username'] = $member['username'] ;
  255. $data['uid'] = $member['uid'] ;
  256. $id = D("Item")->where(" item_id = '$item_id' ")->save($data);
  257. $return = D("Item")->where("item_id = '$item_id' ")->find();
  258. if (!$return) {
  259. $this->sendError(10101);
  260. }
  261. $this->sendResult($return);
  262. }
  263. //删除项目
  264. public function delete(){
  265. $login_user = $this->checkLogin();
  266. $item_id = I("item_id/d");
  267. $password = I("password");
  268. $item = D("Item")->where("item_id = '$item_id' ")->find();
  269. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  270. $this->sendError(10303);
  271. return ;
  272. }
  273. if(! D("User")-> checkLogin($item['username'],$password)){
  274. $this->sendError(10208);
  275. return ;
  276. }
  277. $return = D("Item")->soft_delete_item($item_id);
  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. //转到Open控制器的updateItem方法
  336. R('Open/updateItem');
  337. }
  338. //置顶项目
  339. public function top(){
  340. $login_user = $this->checkLogin();
  341. $item_id = I("item_id/d");
  342. $action = I("action");
  343. if ($action == 'top') {
  344. $ret = D("ItemTop")->add(array("item_id"=>$item_id,"uid"=>$login_user['uid'],"addtime"=>time()));
  345. }
  346. elseif ($action == 'cancel') {
  347. $ret = D("ItemTop")->where(" uid = '$login_user[uid]' and item_id = '$item_id' ")->delete();
  348. }
  349. if ($ret) {
  350. $this->sendResult(array());
  351. }else{
  352. $this->sendError(10101);
  353. }
  354. }
  355. //验证访问密码
  356. public function pwd(){
  357. $item_id = I("item_id/d");
  358. $password = I("password");
  359. $v_code = I("v_code");
  360. $refer_url = I('refer_url');
  361. //检查用户输错密码的次数。如果超过一定次数,则需要验证 验证码
  362. $key= 'item_pwd_fail_times_'.$item_id;
  363. if(!D("VerifyCode")->_check_times($key,10)){
  364. if (!$v_code || $v_code != session('v_code')) {
  365. $this->sendError(10206,L('verification_code_are_incorrect'));
  366. return;
  367. }
  368. }
  369. session('v_code',null) ;
  370. $item = D("Item")->where("item_id = '$item_id' ")->find();
  371. if ($item['password'] == $password) {
  372. session("visit_item_".$item_id , 1 );
  373. $this->sendResult(array("refer_url"=>base64_decode($refer_url)));
  374. }else{
  375. D("VerifyCode")->_ins_times($key);//输错密码则设置输错次数
  376. if(D("VerifyCode")->_check_times($key,10)){
  377. $error_code = 10307 ;
  378. }else{
  379. $error_code = 10308 ;
  380. }
  381. $this->sendError($error_code,L('access_password_are_incorrect'));
  382. }
  383. }
  384. public function itemList(){
  385. $login_user = $this->checkLogin();
  386. $items = D("Item")->where("uid = '$login_user[uid]' ")->select();
  387. $items = $items ? $items : array();
  388. $this->sendResult($items);
  389. }
  390. //新建项目
  391. public function add(){
  392. $login_user = $this->checkLogin();
  393. $item_name = I("item_name");
  394. $item_domain = I("item_domain") ? I("item_domain") : '';
  395. $copy_item_id = I("copy_item_id");
  396. $password = I("password");
  397. $item_description = I("item_description");
  398. $item_type = I("item_type");
  399. if ($item_domain) {
  400. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  401. //echo '个性域名只能是字母或数字的组合';exit;
  402. $this->sendError(10305);
  403. return false;
  404. }
  405. $item = D("Item")->where("item_domain = '%s' ",array($item_domain))->find();
  406. if ($item) {
  407. //个性域名已经存在
  408. $this->sendError(10304);
  409. return false;
  410. }
  411. }
  412. //如果是复制项目
  413. if ($copy_item_id > 0) {
  414. if (!$this->checkItemPermn($login_user['uid'] , $copy_item_id)) {
  415. $this->sendError(10103);
  416. return;
  417. }
  418. $ret = D("Item")->copy($copy_item_id,$login_user['uid'],$item_name,$item_description,$password,$item_domain);
  419. if ($ret) {
  420. $this->sendResult(array());
  421. }else{
  422. $this->sendError(10101);
  423. }
  424. return ;
  425. }
  426. $insert = array(
  427. "uid" => $login_user['uid'] ,
  428. "username" => $login_user['username'] ,
  429. "item_name" => $item_name ,
  430. "password" => $password ,
  431. "item_description" => $item_description ,
  432. "item_domain" => $item_domain ,
  433. "item_type" => $item_type ,
  434. "addtime" =>time()
  435. );
  436. $item_id = D("Item")->add($insert);
  437. if ($item_id) {
  438. //如果是单页应用,则新建一个默认页
  439. if ($item_type == 2 ) {
  440. $insert = array(
  441. 'author_uid' => $login_user['uid'] ,
  442. 'author_username' => $login_user['username'],
  443. "page_title" => $item_name ,
  444. "item_id" => $item_id ,
  445. "cat_id" => 0 ,
  446. "page_content" => '欢迎使用showdoc。点击右上方的编辑按钮进行编辑吧!' ,
  447. "addtime" =>time()
  448. );
  449. $page_id = D("Page")->add($insert);
  450. }
  451. $this->sendResult(array());
  452. }else{
  453. $this->sendError(10101);
  454. }
  455. }
  456. }