function_friend.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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: function_friend.php 26635 2011-12-19 01:59:13Z zhangguosheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. function friend_list($uid, $limit, $start=0) {
  12. $list = array();
  13. $query = C::t('home_friend')->fetch_all_by_uid($uid, $start, $limit, true);
  14. foreach($query as $value) {
  15. $list[$value['fuid']] = $value;
  16. }
  17. return $list;
  18. }
  19. function friend_group_list() {
  20. global $_G;
  21. $space = array('uid' => $_G['uid']);
  22. space_merge($space, 'field_home');
  23. $groups = array();
  24. $spacegroup = empty($space['privacy']['groupname'])?array():$space['privacy']['groupname'];
  25. for($i = 0; $i < $_G['setting']['friendgroupnum']; $i++) {
  26. if($i == 0) {
  27. $groups[0] = lang('friend', 'friend_group_default');
  28. } else {
  29. if(!empty($spacegroup[$i])) {
  30. $groups[$i] = $spacegroup[$i];
  31. } else {
  32. if($i<8) {
  33. $groups[$i] = lang('friend', 'friend_group_'.$i);
  34. } else {
  35. $groups[$i] = lang('friend', 'friend_group_more', array('num'=>$i));
  36. }
  37. }
  38. }
  39. }
  40. return $groups;
  41. }
  42. function friend_check($touids, $isfull = 0) {
  43. global $_G;
  44. if(empty($_G['uid'])) return false;
  45. if(is_array($touids)) {
  46. $query = C::t('home_friend')->fetch_all_by_uid_fuid($_G['uid'], $touids);
  47. foreach($query as $value) {
  48. $touid = $value['fuid'];
  49. $var = "home_friend_{$_G['uid']}_{$touid}";
  50. $fvar = "home_friend_{$touid}_{$_G['uid']}";
  51. $_G[$var] = $_G[$fvar] = true;
  52. if($isfull) {
  53. $fvarinfo = "home_friend_info_{$touid}_{$_G['uid']}";
  54. $_G[$fvarinfo] = $value;
  55. }
  56. }
  57. if(count($query) != count($touids)) {
  58. return false;
  59. } else {
  60. return true;
  61. }
  62. } else {
  63. $touid = $touids;
  64. $var = "home_friend_{$_G['uid']}_{$touid}";
  65. $fvar = "home_friend_{$touid}_{$_G['uid']}";
  66. if(!isset($_G[$var])) {
  67. $query = C::t('home_friend')->fetch_all_by_uid_fuid($_G['uid'], $touid);
  68. $friend = $query[0];
  69. if($friend) {
  70. $_G[$var] = $_G[$fvar] = true;
  71. if($isfull) {
  72. $fvarinfo = "home_friend_info_{$touid}_{$_G['uid']}";
  73. $_G[$fvarinfo] = $friend;
  74. }
  75. } else {
  76. $_G[$var] = $_G[$fvar] = false;
  77. }
  78. }
  79. return $_G[$var];
  80. }
  81. }
  82. function friend_request_check($touid) {
  83. global $_G;
  84. $var = "home_friend_request_{$touid}";
  85. if(!isset($_G[$var])) {
  86. $result = C::t('home_friend_request')->fetch_by_uid_fuid($_G['uid'], $touid);
  87. $_G[$var] = $result?true:false;
  88. }
  89. return $_G[$var];
  90. }
  91. function friend_add($touid, $gid=0, $note='') {
  92. global $_G;
  93. if($touid == $_G['uid']) return -2;
  94. if(friend_check($touid)) return -2;
  95. include_once libfile('function/stat');
  96. $freind_request = C::t('home_friend_request')->fetch_by_uid_fuid($_G['uid'], $touid);
  97. if($freind_request) {
  98. $setarr = array(
  99. 'uid' => $_G['uid'],
  100. 'fuid' => $freind_request['fuid'],
  101. 'fusername' => addslashes($freind_request['fusername']),
  102. 'gid' => $gid,
  103. 'dateline' => $_G['timestamp']
  104. );
  105. C::t('home_friend')->insert($setarr);
  106. friend_request_delete($touid);
  107. friend_cache($_G['uid']);
  108. $setarr = array(
  109. 'uid' => $touid,
  110. 'fuid' => $_G['uid'],
  111. 'fusername' => $_G['username'],
  112. 'gid' => $freind_request['gid'],
  113. 'dateline' => $_G['timestamp']
  114. );
  115. C::t('home_friend')->insert($setarr);
  116. addfriendlog($_G['uid'], $touid);
  117. friend_cache($touid);
  118. updatestat('friend');
  119. } else {
  120. $to_freind_request = C::t('home_friend_request')->fetch_by_uid_fuid($touid, $_G['uid']);
  121. if($to_freind_request) {
  122. return -1;
  123. }
  124. $setarr = array(
  125. 'uid' => $touid,
  126. 'fuid' => $_G['uid'],
  127. 'fusername' => $_G['username'],
  128. 'gid' => $gid,
  129. 'note' => $note,
  130. 'dateline' => $_G['timestamp']
  131. );
  132. C::t('home_friend_request')->insert($setarr);
  133. updatestat('addfriend');
  134. }
  135. return 1;
  136. }
  137. function friend_make($touid, $tousername, $checkrequest=true) {
  138. global $_G;
  139. if($touid == $_G['uid']) return false;
  140. if($checkrequest) {
  141. $to_freind_request = C::t('home_friend_request')->fetch_by_uid_fuid($touid, $_G['uid']);
  142. if($to_freind_request) {
  143. C::t('home_friend_request')->delete_by_uid_fuid($touid, $_G['uid']);
  144. }
  145. $to_freind_request = C::t('home_friend_request')->fetch_by_uid_fuid($_G['uid'], $touid);
  146. if($to_freind_request) {
  147. C::t('home_friend_request')->delete_by_uid_fuid($_G['uid'], $touid);
  148. }
  149. }
  150. $insertarray = array(
  151. 'uid' => $touid,
  152. 'fuid' => $_G['uid'],
  153. 'fusername' => $_G['username'],
  154. 'dateline' => $_G['timestamp'],
  155. );
  156. C::t('home_friend')->insert($insertarray, false, true);
  157. $insertarray = array(
  158. 'uid' => $_G['uid'],
  159. 'fuid' => $touid,
  160. 'fusername' => $tousername,
  161. 'dateline' => $_G['timestamp'],
  162. );
  163. C::t('home_friend')->insert($insertarray, false, true);
  164. addfriendlog($_G['uid'], $touid);
  165. include_once libfile('function/stat');
  166. updatestat('friend');
  167. friend_cache($touid);
  168. friend_cache($_G['uid']);
  169. }
  170. function addfriendlog($uid, $touid, $action = 'add') {
  171. global $_G;
  172. if($uid && $touid) {
  173. $flog = array(
  174. 'uid' => $uid > $touid ? $uid : $touid,
  175. 'fuid' => $uid > $touid ? $touid : $uid,
  176. 'dateline' => $_G['timestamp'],
  177. 'action' => $action
  178. );
  179. DB::insert('home_friendlog', $flog, false, true);
  180. return true;
  181. }
  182. return false;
  183. }
  184. function friend_addnum($touid) {
  185. global $_G;
  186. if($_G['uid'] && $_G['uid'] != $touid) {
  187. C::t('home_friend')->update_num_by_uid_fuid(1, $_G['uid'], $touid);
  188. }
  189. }
  190. function friend_cache($touid) {
  191. global $_G;
  192. $tospace = array('uid' => $touid);
  193. space_merge($tospace, 'field_home');
  194. $filtergids = empty($tospace['privacy']['filter_gid'])?array():$tospace['privacy']['filter_gid'];
  195. $uids = array();
  196. $count = 0;
  197. $fcount = 0;
  198. $query = C::t('home_friend')->fetch_all_by_uid($touid, 0, 0, true);
  199. foreach($query as $value) {
  200. if($value['fuid'] == $touid) continue;
  201. if($fcount > 200) {
  202. $count = count($query);
  203. break;
  204. } elseif(empty($filtergids) || !in_array($value['gid'], $filtergids)) {
  205. $uids[] = $value['fuid'];
  206. $fcount++;
  207. }
  208. $count++;
  209. }
  210. C::t('common_member_field_home')->update($touid, array('feedfriend'=>implode(',', $uids)));
  211. C::t('common_member_count')->update($touid, array('friends'=>$count));
  212. }
  213. function friend_request_delete($touid) {
  214. global $_G;
  215. return C::t('home_friend_request')->delete_by_uid_fuid($_G['uid'], $touid);
  216. }
  217. function friend_delete($touid) {
  218. global $_G;
  219. if(!friend_check($touid)) return false;
  220. C::t('home_friend')->delete_by_uid_fuid_dual($_G['uid'], $touid);
  221. if(DB::affected_rows()) {
  222. addfriendlog($_G['uid'], $touid, 'delete');
  223. friend_cache($_G['uid']);
  224. friend_cache($touid);
  225. }
  226. }
  227. ?>