Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
6.9 KiB

10 years ago
10 years ago
10 years ago
  1. <?php
  2. require_once('include/datetime.php');
  3. /*
  4. * poco_load
  5. *
  6. * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
  7. * and add the entries to the gcontact (Global Contact) table, or update existing entries
  8. * if anything (name or photo) has changed.
  9. * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
  10. *
  11. * Once the global contact is stored add (if necessary) the contact linkage which associates
  12. * the given uid, cid to the global contact entry. There can be many uid/cid combinations
  13. * pointing to the same global contact id.
  14. *
  15. */
  16. function poco_load($cid,$uid = 0,$url = null) {
  17. $a = get_app();
  18. if($cid) {
  19. if((! $url) || (! $uid)) {
  20. $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
  21. intval($cid)
  22. );
  23. if(count($r)) {
  24. $url = $r[0]['poco'];
  25. $uid = $r[0]['uid'];
  26. }
  27. }
  28. if(! $uid)
  29. return;
  30. }
  31. if(! $url)
  32. return;
  33. $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos' : '?fields=displayName,urls,photos') ;
  34. logger('poco_load: ' . $url, LOGGER_DEBUG);
  35. $s = fetch_url($url);
  36. logger('poco_load: returns ' . $s, LOGGER_DATA);
  37. logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
  38. if(($a->get_curl_code() > 299) || (! $s))
  39. return;
  40. $j = json_decode($s);
  41. logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
  42. if(! isset($j->entry))
  43. return;
  44. $total = 0;
  45. foreach($j->entry as $entry) {
  46. $total ++;
  47. $profile_url = '';
  48. $profile_photo = '';
  49. $connect_url = '';
  50. $name = '';
  51. $name = $entry->displayName;
  52. foreach($entry->urls as $url) {
  53. if($url->type == 'profile') {
  54. $profile_url = $url->value;
  55. continue;
  56. }
  57. if($url->type == 'webfinger') {
  58. $connect_url = str_replace('acct:' , '', $url->value);
  59. continue;
  60. }
  61. }
  62. foreach($entry->photos as $photo) {
  63. if($photo->type == 'profile') {
  64. $profile_photo = $photo->value;
  65. continue;
  66. }
  67. }
  68. if((! $name) || (! $profile_url) || (! $profile_photo))
  69. continue;
  70. $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
  71. dbesc(normalise_link($profile_url))
  72. );
  73. if(count($x)) {
  74. $gcid = $x[0]['id'];
  75. if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo) {
  76. q("update gcontact set `name` = '%s', `photo` = '%s', `connect` = '%s'
  77. where `nurl` = '%s' limit 1",
  78. dbesc($name),
  79. dbesc($profile_photo),
  80. dbesc($connect_url),
  81. dbesc(normalise_link($profile_url))
  82. );
  83. }
  84. }
  85. else {
  86. q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`,`connect`)
  87. values ( '%s', '%s', '%s', '%s','%s') ",
  88. dbesc($name),
  89. dbesc($profile_url),
  90. dbesc(normalise_link($profile_url)),
  91. dbesc($profile_photo),
  92. dbesc($connect_url)
  93. );
  94. $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
  95. dbesc(normalise_link($profile_url))
  96. );
  97. if(count($x))
  98. $gcid = $x[0]['id'];
  99. }
  100. if(! $gcid)
  101. return;
  102. $r = q("select * from glink where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
  103. intval($cid),
  104. intval($uid),
  105. intval($gcid)
  106. );
  107. if(! count($r)) {
  108. q("insert into glink ( `cid`,`uid`,`gcid`,`updated`) values (%d,%d,%d,'%s') ",
  109. intval($cid),
  110. intval($uid),
  111. intval($gcid),
  112. dbesc(datetime_convert())
  113. );
  114. }
  115. else {
  116. q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
  117. dbesc(datetime_convert()),
  118. intval($cid),
  119. intval($uid),
  120. intval($gcid)
  121. );
  122. }
  123. }
  124. logger("poco_load: loaded $total entries",LOGGER_DEBUG);
  125. q("delete from glink where `cid` = %d and `uid` = %d and `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
  126. intval($cid),
  127. intval($uid)
  128. );
  129. }
  130. function count_common_friends($uid,$cid) {
  131. $r = q("SELECT count(*) as `total`
  132. FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
  133. where `glink`.`cid` = %d and `glink`.`uid` = %d
  134. and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d ) ",
  135. intval($cid),
  136. intval($uid),
  137. intval($uid),
  138. intval($cid)
  139. );
  140. if(count($r))
  141. return $r[0]['total'];
  142. return 0;
  143. }
  144. function common_friends($uid,$cid) {
  145. $r = q("SELECT `gcontact`.*
  146. FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
  147. where `glink`.`cid` = %d and `glink`.`uid` = %d
  148. and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d )
  149. order by `gcontact`.`name` asc ",
  150. intval($cid),
  151. intval($uid),
  152. intval($uid),
  153. intval($cid)
  154. );
  155. return $r;
  156. }
  157. function count_all_friends($uid,$cid) {
  158. $r = q("SELECT count(*) as `total`
  159. FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
  160. where `glink`.`cid` = %d and `glink`.`uid` = %d ",
  161. intval($cid),
  162. intval($uid)
  163. );
  164. if(count($r))
  165. return $r[0]['total'];
  166. return 0;
  167. }
  168. function all_friends($uid,$cid,$start = 0, $limit = 80) {
  169. $r = q("SELECT `gcontact`.*
  170. FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
  171. where `glink`.`cid` = %d and `glink`.`uid` = %d
  172. order by `gcontact`.`name` asc LIMIT %d, %d ",
  173. intval($cid),
  174. intval($uid),
  175. intval($start),
  176. intval($limit)
  177. );
  178. return $r;
  179. }
  180. function suggestion_query($uid, $start = 0, $limit = 80) {
  181. if(! $uid)
  182. return array();
  183. $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
  184. left join glink on glink.gcid = gcontact.id
  185. where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
  186. and not gcontact.name in ( select name from contact where uid = %d )
  187. and not gcontact.id in ( select gcid from gcign where uid = %d )
  188. group by glink.gcid order by total desc limit %d, %d ",
  189. intval($uid),
  190. intval($uid),
  191. intval($uid),
  192. intval($uid),
  193. intval($start),
  194. intval($limit)
  195. );
  196. if(count($r) && count($r) >= ($limit -1))
  197. return $r;
  198. $r2 = q("SELECT gcontact.* from gcontact
  199. left join glink on glink.gcid = gcontact.id
  200. where glink.uid = 0 and glink.cid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
  201. and not gcontact.name in ( select name from contact where uid = %d )
  202. and not gcontact.id in ( select gcid from gcign where uid = %d )
  203. order by rand() limit %d, %d ",
  204. intval($uid),
  205. intval($uid),
  206. intval($uid),
  207. intval($start),
  208. intval($limit)
  209. );
  210. return array_merge($r,$r2);
  211. }
  212. function update_suggestions() {
  213. $a = get_app();
  214. $done = array();
  215. poco_load(0,0,$a->get_baseurl() . '/poco');
  216. $done[] = $a->get_baseurl() . '/poco';
  217. if(strlen(get_config('system','directory_submit_url'))) {
  218. $x = fetch_url('http://dir.friendica.com/pubsites');
  219. if($x) {
  220. $j = json_decode($x);
  221. if($j->entries) {
  222. foreach($j->entries as $entry) {
  223. $url = $entry->url . '/poco';
  224. if(! in_array($url,$done))
  225. poco_load(0,0,$entry->url . '/poco');
  226. }
  227. }
  228. }
  229. }
  230. $r = q("select distinct(poco) as poco from contact where network = '%s'",
  231. dbesc(NETWORK_DFRN)
  232. );
  233. if(count($r)) {
  234. foreach($r as $rr) {
  235. $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
  236. if(! in_array($base,$done))
  237. poco_load(0,0,$base);
  238. }
  239. }
  240. }