PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/api/manyou/Service/SearchHelper.php

https://github.com/kuaileshike/upload
PHP | 860 lines | 762 code | 92 blank | 6 comment | 133 complexity | 4935bc7e40cff73192099e2502bdcd0f MD5 | raw file
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: SearchHelper.php 29915 2012-05-03 01:22:40Z zhouxiaobo $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class Cloud_Service_SearchHelper {
  12. protected static $_instance;
  13. public static function getInstance() {
  14. if (!(self::$_instance instanceof self)) {
  15. self::$_instance = new self();
  16. }
  17. return self::$_instance;
  18. }
  19. public function __construct() {
  20. }
  21. public function getTables($table) {
  22. if(!in_array($table, array('post', 'thread'))) {
  23. return false;
  24. }
  25. $infos = getglobal($table.'table_info');
  26. if ($infos) {
  27. $tables = array();
  28. foreach($infos as $id => $row) {
  29. $tables[$id] = $id;
  30. }
  31. } else {
  32. $tables = array(0);
  33. }
  34. return $tables;
  35. }
  36. private function _convertForum($row) {
  37. $result = array();
  38. $map = array(
  39. 'fid' => 'fId',
  40. 'fup' => 'pId',
  41. 'name' => 'fName',
  42. 'type' => 'type',
  43. 'displayorder' => 'displayOrder',
  44. );
  45. foreach($row as $k => $v) {
  46. if (array_key_exists($k, $map)) {
  47. $result[$map[$k]] = $v;
  48. continue;
  49. }
  50. if ($k == 'status') {
  51. $isGroup = false;
  52. switch ($v) {
  53. case '0' :
  54. $displayStatus = 'hidden';
  55. break;
  56. case '1' :
  57. $displayStatus = 'normal';
  58. break;
  59. case '2' :
  60. $displayStatus = 'some';
  61. break;
  62. case '3' :
  63. $displayStatus = 'normal';
  64. $isGroup = true;
  65. break;
  66. default :
  67. $displayStatus = 'unknown';
  68. }
  69. $result['displayStatus'] = $displayStatus;
  70. $result['isGroup'] = $isGroup;
  71. }
  72. }
  73. $formula = dunserialize($row['formulaperm']);
  74. $result['isFormula'] = $formula[1] ? TRUE : FALSE;
  75. $result['sign'] = md5(serialize($result));
  76. return $result;
  77. }
  78. public function getForums($fIds = array()) {
  79. if($fIds) {
  80. $forums = C::t('forum_forum')->fetch_all_info_by_fids($fIds);
  81. } else {
  82. $forums = C::t('forum_forum')->fetch_all_info_by_fids(0, 0, 0, 0, 0, 0, 1);
  83. }
  84. $result = array();
  85. $result['totalNum'] = count($forums);
  86. foreach($forums as $forum) {
  87. $result['data'][$forum['fid']] = self::_convertForum($forum);
  88. }
  89. if (!$fIds) {
  90. $result['sign'] = md5(serialize($result['data']));
  91. }
  92. return $result;
  93. }
  94. public function getUserGroupPermissions($userGroupIds) {
  95. global $_G;
  96. $userGroups = array();
  97. if ($_G['setting']['verify']['enabled']) {
  98. foreach ($userGroupIds as $groupId) {
  99. if (dintval($groupId) > 65500) {
  100. $vGid = $groupId - 65500;
  101. if ($_G['setting']['verify'][$vGid]['available']) {
  102. $key = array_search($groupId, $userGroupIds);
  103. unset($userGroupIds[$key]);
  104. $userGroups['v' . $vGid] = array();
  105. }
  106. }
  107. }
  108. }
  109. $fields = array(
  110. 'groupid' => 'userGroupId',
  111. 'grouptitle' => 'userGroupName',
  112. 'readaccess' => 'readPermission',
  113. 'allowvisit' => 'allowVisit',
  114. 'allowsearch' => 'searchLevel',
  115. );
  116. $userGroup= C::t('common_usergroup')->fetch_all((array)$userGroupIds);
  117. $userGroupField = C::t('common_usergroup_field')->fetch_all_fields((array)$userGroupIds, array('readaccess', 'allowsearch'));
  118. foreach(array_merge($userGroup, $userGroupField) as $row) {
  119. foreach($row as $k => $v) {
  120. if (array_key_exists($k, $fields)) {
  121. if ($k == 'allowsearch') {
  122. $userGroups[$row['groupid']]['allowSearchAlbum'] = ($v & 8) ? true : false;
  123. $userGroups[$row['groupid']]['allowSearchBlog'] = ($v & 4) ? true : false;
  124. $userGroups[$row['groupid']]['allowSearchForum'] = ($v & 2) ? true : false;
  125. $userGroups[$row['groupid']]['allowSearchPortal'] = ($v & 1) ? true : false;
  126. $userGroups[$row['groupid']]['allowFulltextSearch'] = ($v & 32) ? true : false;
  127. } else {
  128. $userGroups[$row['groupid']][$fields[$k]] = $v;
  129. }
  130. }
  131. $userGroups[$row['groupid']]['forbidForumIds'] = array();
  132. $userGroups[$row['groupid']]['allowForumIds'] = array();
  133. $userGroups[$row['groupid']]['specifyAllowForumIds'] = array();
  134. }
  135. }
  136. $fIds = array();
  137. foreach(C::t('forum_forum')->fetch_all_by_status('1') as $row) {
  138. $fIds[$row['fid']] = $row['fid'];
  139. }
  140. $fieldForums = array();
  141. foreach(C::t('forum_forumfield')->fetch_all_by_fid($fIds) as $row) {
  142. $fieldForums[$row['fid']] = $row;
  143. }
  144. foreach($fIds as $fId) {
  145. $row = $fieldForums[$fId];
  146. $allowViewGroupIds = array();
  147. if ($row['viewperm']) {
  148. $allowViewGroupIds = explode("\t", $row['viewperm']);
  149. }
  150. foreach($userGroups as $gid => $_v) {
  151. if ($row['password']) {
  152. $userGroups[$gid]['forbidForumIds'][] = $fId;
  153. continue;
  154. }
  155. $perm = dunserialize($row['formulaperm']);
  156. if(is_array($perm)) {
  157. $spviewperm = explode("\t", $row['spviewperm']);
  158. if (in_array($gid, $spviewperm)) {
  159. $userGroups[$gid]['allowForumIds'][] = $fId;
  160. $userGroups[$gid]['specifyAllowForumIds'][] = $fId;
  161. continue;
  162. }
  163. if ($perm[0] || $perm[1] || $perm['users']) {
  164. $userGroups[$gid]['forbidForumIds'][] = $fId;
  165. continue;
  166. }
  167. }
  168. if (!$allowViewGroupIds) {
  169. $userGroups[$gid]['allowForumIds'][] = $fId;
  170. } elseif (!in_array($gid, $allowViewGroupIds)) {
  171. $userGroups[$gid]['forbidForumIds'][] = $fId;
  172. } elseif (in_array($gid, $allowViewGroupIds)) {
  173. $userGroups[$gid]['allowForumIds'][] = $fId;
  174. $userGroups[$gid]['specifyAllowForumIds'][] = $fId;
  175. }
  176. }
  177. }
  178. foreach ($userGroups as $groupId => $v) {
  179. if (substr($groupId, 0, 1) == 'v') {
  180. $verifyKey = 65500 + dintval(substr($groupId, 1));
  181. $v['userGroupId'] = $verifyKey;
  182. $userGroups[$verifyKey] = $v;
  183. unset($userGroups[$groupId]);
  184. }
  185. }
  186. foreach($userGroups as $k => $v) {
  187. ksort($v);
  188. $userGroups[$k]['sign'] = md5(serialize($v));
  189. }
  190. return $userGroups;
  191. }
  192. public function getGuestPerm($gfIds = array()) {
  193. $perm = self::getUserGroupPermissions(array(7));
  194. $guestPerm = $perm[7];
  195. if ($gfIds) {
  196. foreach(C::t('forum_forumfield')->fetch_all_by_fid($gfIds) as $row) {
  197. if ($row['gviewperm'] == 1) {
  198. $guestPerm['allowForumIds'][] = $row['fid'];
  199. } else {
  200. $guestPerm['forbidForumIds'][] = $row['fid'];
  201. }
  202. }
  203. }
  204. return $guestPerm;
  205. }
  206. public function convertThread($row) {
  207. $result = array();
  208. $map = array(
  209. 'tid' => 'tId',
  210. 'fid' => 'fId',
  211. 'authorid' => 'authorId',
  212. 'author' => 'authorName',
  213. 'special' => 'specialType',
  214. 'price' => 'price',
  215. 'subject' => 'subject',
  216. 'readperm' => 'readPermission',
  217. 'lastposter' => 'lastPoster',
  218. 'views' => 'viewNum',
  219. 'replies' => 'replyNum',
  220. 'displayorder' => 'stickLevel',
  221. 'highlight' => 'isHighlight',
  222. 'digest' => 'digestLevel',
  223. 'rate' => 'rate',
  224. 'attachment' => 'isAttached',
  225. 'moderated' => 'isModerated',
  226. 'closed' => 'isClosed',
  227. 'supe_pushstatus' => 'supeSitePushStatus',
  228. 'recommends' => 'recommendTimes',
  229. 'recommend_add' => 'recommendSupportTimes',
  230. 'recommend_sub' => 'recommendOpposeTimes',
  231. 'heats' => 'heats',
  232. 'pid' => 'pId',
  233. 'isgroup' => 'isGroup',
  234. 'posttableid' => 'postTableId',
  235. 'favtimes' => 'favoriteTimes',
  236. 'sharetimes'=> 'shareTimes',
  237. 'icon' => 'icon',
  238. );
  239. $map2 = array(
  240. 'dateline' => 'createdTime',
  241. 'lastpost' => 'lastPostedTime',
  242. );
  243. foreach($row as $k => $v) {
  244. if (array_key_exists($k, $map)) {
  245. if ($k == 'special') {
  246. switch($v) {
  247. case 1:
  248. $v = 'poll';
  249. break;
  250. case 2:
  251. $v = 'trade';
  252. break;
  253. case 3:
  254. $v = 'reward';
  255. break;
  256. case 4:
  257. $v = 'activity';
  258. break;
  259. case 5:
  260. $v = 'debate';
  261. break;
  262. case 127:
  263. $v = 'plugin';
  264. break;
  265. default:
  266. $v = 'normal';
  267. }
  268. }
  269. if ($k == 'displayorder') {
  270. if ($v >= 0) {
  271. $result['displayStatus'] = 'normal';
  272. } elseif ($v == -1) {
  273. $result['displayStatus'] = 'recycled';
  274. } elseif ($v == -2) {
  275. $result['displayStatus'] = 'unapproved';
  276. } elseif ($v == -3) {
  277. $result['displayStatus'] = 'ignored';
  278. } elseif ($v == -4) {
  279. $result['displayStatus'] = 'draft';
  280. } else {
  281. $result['displayStatus'] = 'unknown';
  282. }
  283. switch($v) {
  284. case 1:
  285. $v = 'board';
  286. break;
  287. case 2:
  288. $v = 'group';
  289. break;
  290. case 3:
  291. $v = 'global';
  292. break;
  293. case 0:
  294. default:
  295. $v = 'none';
  296. }
  297. }
  298. if (in_array($k, array('highlight', 'moderated', 'closed', 'isgroup'))) {
  299. $v = $v ? true : false;
  300. }
  301. $result[$map[$k]] = $v;
  302. } elseif (array_key_exists($k, $map2)) {
  303. $result[$map2[$k]] = dgmdate($v, 'Y-m-d H:i:s', 8);
  304. }
  305. }
  306. return $result;
  307. }
  308. public function preGetThreads($tableid, $tIds) {
  309. $result = array();
  310. if($tIds) {
  311. foreach(C::t('forum_thread')->fetch_all_by_tid($tIds, 0, 0, $tableid) as $thread) {
  312. $result[$thread['tid']] = self::convertThread($thread);
  313. }
  314. }
  315. return $result;
  316. }
  317. public function getThreadPosts($tIds) {
  318. $result = array();
  319. foreach($tIds as $postTableId => $_tIds) {
  320. foreach(C::t('forum_post')->fetch_all_by_tid($postTableId, $_tIds, true, '', 0, 0, 1) as $post) {
  321. $result[$post['tid']] = self::convertPost($post);
  322. }
  323. }
  324. return $result;
  325. }
  326. public function getThreads($tIds, $isReturnPostId = true) {
  327. global $_G;
  328. $tables = array();
  329. $infos = $_G['setting']['threadtable_info'];
  330. if ($infos) {
  331. foreach($infos as $id => $row) {
  332. $tables[] = $id;
  333. }
  334. } else {
  335. $tables = array('forum_thread');
  336. }
  337. $tableNum = count($tables);
  338. $res = $data = $_tableInfo = array();
  339. for($i = 0; $i < $tableNum; $i++) {
  340. $_threads = self::preGetThreads($tables[$i], $tIds);
  341. if ($_threads) {
  342. if (!$data) {
  343. $data = $_threads;
  344. } else {
  345. $data = $data + $_threads;
  346. }
  347. if (count($data) == count($tIds)) {
  348. break;
  349. }
  350. }
  351. }
  352. if ($isReturnPostId) {
  353. $threadIds = array();
  354. foreach($data as $tId => $thread) {
  355. $postTableId = $thread['postTableId'];
  356. $threadIds[$postTableId][] = $tId;
  357. }
  358. $threadPosts = self::getThreadPosts($threadIds);
  359. foreach($data as $tId => $thread) {
  360. $data[$tId]['pId'] = $threadPosts[$tId]['pId'];
  361. }
  362. }
  363. return $data;
  364. }
  365. public function convertPost($row) {
  366. $result = array();
  367. $map = array('pid' => 'pId',
  368. 'tid' => 'tId',
  369. 'fid' => 'fId',
  370. 'authorid' => 'authorId',
  371. 'author' => 'authorName',
  372. 'useip' => 'authorIp',
  373. 'anonymous' => 'isAnonymous',
  374. 'subject' => 'subject',
  375. 'message' => 'content',
  376. 'invisible' => 'displayStatus',
  377. 'htmlon' => 'isHtml',
  378. 'attachment' => 'isAttached',
  379. 'rate' => 'rate',
  380. 'ratetimes' => 'rateTimes',
  381. 'dateline' => 'createdTime',
  382. 'first' => 'isThread',
  383. );
  384. $map2 = array(
  385. 'bbcodeoff' => 'isBbcode',
  386. 'smileyoff' => 'isSmiley',
  387. 'parseurloff' => 'isParseUrl',
  388. );
  389. foreach($row as $k => $v) {
  390. if (array_key_exists($k, $map)) {
  391. if ($k == 'invisible') {
  392. switch($v) {
  393. case 0:
  394. $v = 'normal';
  395. break;
  396. case -1:
  397. $v = 'recycled';
  398. break;
  399. case -2:
  400. $v = 'unapproved';
  401. break;
  402. case -3:
  403. $v = 'ignored';
  404. break;
  405. case -4:
  406. $v = 'draft';
  407. break;
  408. default:
  409. $v = 'unkonwn';
  410. }
  411. }
  412. if ($k == 'dateline') {
  413. $result[$map[$k]] = dgmdate($v, 'Y-m-d H:i:s', 8);
  414. continue;
  415. }
  416. if (in_array($k, array('htmlon', 'attachment', 'first', 'anonymous'))) {
  417. $v = $v ? true : false;
  418. }
  419. $result[$map[$k]] = $v;
  420. } elseif (array_key_exists($k, $map2)) {
  421. $result[$map2[$k]] = $v ? false : true;
  422. }
  423. }
  424. $result['isWarned'] = $result['isBanned'] = false;
  425. if ($row['status'] & 1) {
  426. $result['isBanned'] = true;
  427. }
  428. if ($row['status'] & 2) {
  429. $result['isWarned'] = true;
  430. }
  431. $attachInfo = array();
  432. if ($result['isAttached']) {
  433. $attachIndex = C::t('forum_attachment')->fetch_all_by_id('pid', $row['pid']);
  434. $attachment = C::t('forum_attachment_n')->fetch_all_by_id('pid:'.$row['pid'], 'pid', $row['pid'], 'aid');
  435. $attachMap = array(
  436. 'aid' => 'aId',
  437. 'tid' => 'tId',
  438. 'pid' => 'pId',
  439. 'uid' => 'uId',
  440. 'dateline' => 'uploadedTime',
  441. 'filename' => 'fileName',
  442. 'filesize' => 'fileSize',
  443. 'attachment' => 'filePath',
  444. 'remote' => 'isRemote',
  445. 'description' => 'description',
  446. 'readperm' => 'readPerm',
  447. 'price' => 'price',
  448. 'isimage' => 'isImage',
  449. 'width' => 'width',
  450. 'thumb' => 'isThumb',
  451. 'picid' => 'picId',
  452. );
  453. foreach ($attachment as $k => $v) {
  454. $attachTemp = array();
  455. foreach ($v as $key => $val) {
  456. if ($key == 'dateline') {
  457. $attachTemp[$attachMap[$key]] = dgmdate($val, 'Y-m-d H:i:s', 8);
  458. continue;
  459. }
  460. $attachTemp[$attachMap[$key]] = $val;
  461. }
  462. $attachInfo[$k] = $attachTemp;
  463. $attachInfo[$k]['downloadTimes'] = $attachIndex[$k]['downloads'];
  464. }
  465. }
  466. $result['attachInfo'] = $attachInfo;
  467. return $result;
  468. }
  469. public function convertNav($row) {
  470. $map = array( 'id' => 'id',
  471. 'name' => 'name',
  472. 'title' => 'title',
  473. 'url' => 'url',
  474. 'type' => 'provider',
  475. 'navtype' => 'navType',
  476. 'available' => 'available',
  477. 'displayorder' => 'displayOrder',
  478. 'target' => 'linkTarget',
  479. 'highlight' => 'highlight',
  480. 'level' => 'userGroupLevel',
  481. 'subtype' => 'subLayout',
  482. 'subcols' => 'subColNum',
  483. 'subname' => 'subName',
  484. 'suburl' => 'subUrl',
  485. );
  486. foreach($row as $k => $v) {
  487. if (array_key_exists($k, $map)) {
  488. if (in_array($k, array('available'))) {
  489. $v = $v > 0 ? true : false;
  490. }
  491. if ($k == 'subtype') {
  492. if ($v == 1) {
  493. $v = 'parallel';
  494. } else {
  495. $v = 'menu';
  496. }
  497. }
  498. if ($k == 'type') {
  499. switch($v) {
  500. case '1':
  501. $v = 'user';
  502. break;
  503. case '0':
  504. default:
  505. $v = 'system';
  506. break;
  507. }
  508. }
  509. if ($k == 'navtype') {
  510. switch($v) {
  511. case 1:
  512. $v = 'footer';
  513. break;
  514. case 2:
  515. $v = 'space';
  516. break;
  517. case 3:
  518. $v = 'my';
  519. break;
  520. case 0:
  521. $v = 'header';
  522. break;
  523. }
  524. }
  525. $result[$map[$k]] = $v;
  526. }
  527. }
  528. return $result;
  529. }
  530. public function convertPoll($row) {
  531. $map = array('polloptionid' => 'id',
  532. 'tid' => null,
  533. 'votes' => 'votes',
  534. 'displayorder' => 'displayOrder',
  535. 'polloption' => 'label',
  536. 'voterids' => 'voterIds',
  537. );
  538. $result = array();
  539. foreach($row as $k => $v) {
  540. $field = $map[$k];
  541. if ($field !== null) {
  542. $result[$field] = $v;
  543. }
  544. }
  545. return $result;
  546. }
  547. public function getPollInfo($tIds) {
  548. $result = array();
  549. foreach(C::t('forum_polloption')->fetch_all_by_tid($tIds) as $row) {
  550. $result[$row['tid']][$row['polloptionid']] = self::convertPoll($row);
  551. }
  552. return $result;
  553. }
  554. public function getThreadSort($tIds) {
  555. global $_G;
  556. $optionvar = array();
  557. foreach(C::t('forum_typeoptionvar')->fetch_all_by_tid_optionid($tIds) as $row) {
  558. if(!isset($_G['cache']['threadsort_option_'.$row['sortid']])) {
  559. loadcache(array('threadsort_option_'.$row['sortid']));
  560. }
  561. $title = $_G['cache']['threadsort_option_'.$row['sortid']][$row['optionid']]['title'];
  562. $type = $_G['cache']['threadsort_option_'.$row['sortid']][$row['optionid']]['type'];
  563. if($title && !in_array($type, array('image'))) {
  564. $optionvar[$row['tid']][$title] = $row['value'];
  565. }
  566. }
  567. return $optionvar;
  568. }
  569. public function allowSearchForum() {
  570. C::t('common_usergroup_field')->update_allowsearch();
  571. require_once libfile('function/cache');
  572. updatecache('usergroups');
  573. }
  574. public function myThreadLog($opt, $data) {
  575. global $_G;
  576. $cloudAppService = Cloud::loadClass('Service_App');
  577. if(!$cloudAppService->getCloudAppStatus('search')) return;
  578. $data['action'] = $opt;
  579. $data['dateline'] = time();
  580. C::t('forum_threadlog')->insert($data, false, true);
  581. }
  582. public function myPostLog($opt, $data) {
  583. global $_G;
  584. $cloudAppService = Cloud::loadClass('Service_App');
  585. if(!$cloudAppService->getCloudAppStatus('search')) return;
  586. $data['action'] = $opt;
  587. $data['dateline'] = time();
  588. C::t('forum_postlog')->insert($data, false, true);
  589. }
  590. public function getRelatedThreadsTao($keyword, $page, $tpp, $excludeForumIds = '', $cache = false) {
  591. global $_G;
  592. $sId = $_G['setting']['my_siteid'];
  593. $result = array();
  594. if($sId) {
  595. if($cache === true) {
  596. $kname = 'search_recommend_thread_'.$keyword.'_'.$page.'_'.$excludeForumIds;
  597. loadcache($kname);
  598. }
  599. if(isset($_G['cache'][$kname]['ts']) && (TIMESTAMP - $_G['cache'][$kname]['ts'] <= 21600)) {
  600. $result = $_G['cache'][$kname]['result'];
  601. } else {
  602. $apiUrl = 'http://api.discuz.qq.com/search/discuz/tao?';
  603. $params = array(
  604. 'sId' => $sId,
  605. 'q' => $keyword,
  606. 'tpp' => $tpp,
  607. 'excludeForumIds' => $excludeForumIds,
  608. 'page' => $page ? $page : 1,
  609. 'clientIp' => $_G['clientip']
  610. );
  611. $utilService = Cloud::loadClass('Service_Util');
  612. $response = dfsockopen($apiUrl.$utilService->generateSiteSignUrl($params), 0, '', '', false, $_G['setting']['cloud_api_ip']);
  613. require_once libfile('class/xml');
  614. $result = (array) xml2array($response);
  615. if($cache === true && isset($result['status']) && $result['status'] == 0) {
  616. save_syscache($kname, array('ts' => TIMESTAMP, 'result' => $result));
  617. }
  618. if($result['status'] != 0) {
  619. $result = null;
  620. }
  621. }
  622. }
  623. return $result;
  624. }
  625. public function getRelatedThreads($fId, $cache = false) {
  626. global $_G;
  627. $sId = $_G['setting']['my_siteid'];
  628. $result = array();
  629. if($sId) {
  630. if($cache === true) {
  631. $kname = 'search_recommend_fidthread_'.$fId;
  632. loadcache($kname);
  633. }
  634. if(isset($_G['cache'][$kname]['ts']) && (TIMESTAMP - $_G['cache'][$kname]['ts'] <= 21600)) {
  635. $result = $_G['cache'][$kname]['result'];
  636. } else {
  637. $apiUrl = 'http://api.discuz.qq.com/search/discuz/forumRelated?';
  638. $params = array(
  639. 'sId' => $sId,
  640. 'fId' => $fId,
  641. 'clientIp' => $_G['clientip']
  642. );
  643. $utilService = Cloud::loadClass('Service_Util');
  644. $response = dfsockopen($apiUrl.$utilService->generateSiteSignUrl($params), 0, '', '', false, $_G['setting']['cloud_api_ip']);
  645. require_once libfile('class/xml');
  646. $result = (array) xml2array($response);
  647. if($cache === true && isset($result['status']) && $result['status'] == 0) {
  648. save_syscache($kname, array('ts' => TIMESTAMP, 'result' => $result));
  649. }
  650. if($result['status'] != 0) {
  651. $result = null;
  652. }
  653. }
  654. }
  655. return $result;
  656. }
  657. public function getRecWords($needNum = 14, $format = 'num', $fid = 0) {
  658. global $_G;
  659. $sId = $_G['setting']['my_siteid'];
  660. $data = array();
  661. if($sId) {
  662. $fid = $fid ? $fid : 0;
  663. $kname = 'search_recommend_words_' . $fid;
  664. loadcache($kname);
  665. $cacheLife = isset($_G['setting']['my_search_data']['recwords_lifetime']) ? intval($_G['setting']['my_search_data']['recwords_lifetime']) : 21600;
  666. $cloudSettingTime = isset($_G['setting']['my_search_data']['set_forbidden_recwords_time']) ? intval($_G['setting']['my_search_data']['set_forbidden_recwords_time']) : 0;
  667. $cacheSettingTime = isset($_G['cache'][$kname]['setting_ts']) ? intval($_G['cache'][$kname]['setting_ts']) : 0;
  668. if((!$cloudSettingTime || $cloudSettingTime == $cacheSettingTime) && isset($_G['cache'][$kname]['ts']) && (TIMESTAMP - $_G['cache'][$kname]['ts'] <= $cacheLife)) {
  669. $data = $_G['cache'][$kname]['result'];
  670. } else {
  671. $apiUrl = 'http://api.discuz.qq.com/search/recwords/get';
  672. $params = array(
  673. 's_id' => $sId,
  674. 'f_id' => $fid,
  675. 'need_random' => false,
  676. 'need_num' => $needNum,
  677. 'version' => $format == 'num' ? 1 : 2, // 1:返回数字下标的结果集、2:返回关联数组形式的结果集
  678. );
  679. $utilService = Cloud::loadClass('Service_Util');
  680. $response = dfsockopen($apiUrl, 0, $utilService->generateSiteSignUrl($params), '', false, $_G['setting']['cloud_api_ip']);
  681. $result = (array) unserialize($response);
  682. if(isset($result['status']) && $result['status'] === 0) {
  683. $data = $result['result'];
  684. if($cloudSettingTime) {
  685. save_syscache($kname, array('ts' => TIMESTAMP, 'setting_ts' => $cloudSettingTime, 'result' => $data));
  686. } else {
  687. save_syscache($kname, array('ts' => TIMESTAMP, 'result' => $data));
  688. }
  689. }
  690. }
  691. }
  692. return $data;
  693. }
  694. public function makeSearchSignUrl() {
  695. global $_G;
  696. $url = '';
  697. $params = array();
  698. $mySearchData = $_G['setting']['my_search_data'];
  699. $mySiteId = $_G['setting']['my_siteid'];
  700. $mySiteKey = $_G['setting']['my_sitekey'];
  701. $cloudAppService = Cloud::loadClass('Service_App');
  702. if($mySearchData['status'] && $cloudAppService->getCloudAppStatus('search') && $mySiteId) {
  703. $myExtGroupIds = array();
  704. $_extGroupIds = explode("\t", $_G['member']['extgroupids']);
  705. foreach($_extGroupIds as $v) {
  706. if($v) {
  707. $myExtGroupIds[] = $v;
  708. }
  709. }
  710. $myExtGroupIdsStr = implode(',', $myExtGroupIds);
  711. $params = array(
  712. 'sId' => $mySiteId,
  713. 'ts' => time(),
  714. 'cuId' => $_G['uid'],
  715. 'cuName' => $_G['username'],
  716. 'gId' => intval($_G['groupid']),
  717. 'agId' => intval($_G['adminid']),
  718. 'egIds' => $myExtGroupIdsStr,
  719. 'fmSign' => '',
  720. );
  721. $groupIds = array($params['gId']);
  722. if($params['agId']) {
  723. $groupIds[] = $params['agId'];
  724. }
  725. if($myExtGroupIds) {
  726. $groupIds = array_merge($groupIds, $myExtGroupIds);
  727. }
  728. $groupIds = array_unique($groupIds);
  729. foreach($groupIds as $v) {
  730. $key = 'ugSign' . $v;
  731. $params[$key] = '';
  732. }
  733. $extraParams = array();
  734. if (isset($_G['setting']['verify']['enabled']) && $_G['setting']['verify']['enabled']) {
  735. $verifyGroups = C::t('common_member_verify')->fetch($_G['uid']);
  736. $extraParams['ext_vgIds'] = 0;
  737. foreach ($verifyGroups as $k => $v) {
  738. if ($k != 'uid') {
  739. $position = dintval(substr($k, strlen('verify')));
  740. $extraParams['ext_vgIds'] = setstatus($position, dintval($v), $extraParams['ext_vgIds']);
  741. }
  742. }
  743. }
  744. if ($_G['cookie']['ffids' . $_G['uid']]) {
  745. $ext_ffids = str_replace('D', ',', authcode($_G['cookie']['ffids' . $_G['uid']], 'DECODE'));
  746. $extraParams['ext_ffids'] = $ext_ffids;
  747. }
  748. if (!empty($extraParams)) {
  749. ksort($extraParams);
  750. $params = array_merge($params, $extraParams);
  751. }
  752. $params['sign'] = md5(implode('|', $params) . '|' . $mySiteKey);
  753. if ($cloudAppService->getCloudAppStatus('connect') && $_G['member']['conopenid']) {
  754. $connectService = Cloud::loadClass('Service_Connect');
  755. $connectService->connectMergeMember();
  756. $params['openid'] = $_G['member']['conopenid'];
  757. }
  758. $params['charset'] = $_G['charset'];
  759. if($mySearchData['domain']) {
  760. $domain = $mySearchData['domain'];
  761. } else {
  762. $domain = 'search.discuz.qq.com';
  763. }
  764. $url = 'http://' . $domain . '/f/discuz';
  765. }
  766. return !empty($url) ? array('url' => $url, 'params' => $params) : array();
  767. }
  768. }