3 require_once('include/datetime.php');
8 * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
9 * and add the entries to the gcontact (Global Contact) table, or update existing entries
10 * if anything (name or photo) has changed.
11 * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
13 * Once the global contact is stored add (if necessary) the contact linkage which associates
14 * the given uid, cid to the global contact entry. There can be many uid/cid combinations
15 * pointing to the same global contact id.
22 function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
26 if((! $url) || (! $uid)) {
27 $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
42 $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos' : '?fields=displayName,urls,photos') ;
44 logger('poco_load: ' . $url, LOGGER_DEBUG);
48 logger('poco_load: returns ' . $s, LOGGER_DATA);
50 logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
52 if(($a->get_curl_code() > 299) || (! $s))
57 logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
59 if(! isset($j->entry))
63 foreach($j->entry as $entry) {
71 $name = $entry->displayName;
73 if(isset($entry->urls)) {
74 foreach($entry->urls as $url) {
75 if($url->type == 'profile') {
76 $profile_url = $url->value;
79 if($url->type == 'webfinger') {
80 $connect_url = str_replace('acct:' , '', $url->value);
85 if(isset($entry->photos)) {
86 foreach($entry->photos as $photo) {
87 if($photo->type == 'profile') {
88 $profile_photo = $photo->value;
94 if((! $name) || (! $profile_url) || (! $profile_photo))
97 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
98 dbesc(normalise_link($profile_url))
104 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo) {
105 q("update gcontact set `name` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s'
106 where `nurl` = '%s'",
108 dbesc($profile_photo),
111 dbesc(normalise_link($profile_url))
116 q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`,`connect`)
117 values ( '%s', '%s', '%s', '%s','%s') ",
120 dbesc(normalise_link($profile_url)),
121 dbesc($profile_photo),
124 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
125 dbesc(normalise_link($profile_url))
133 $r = q("select * from glink where `cid` = %d and `uid` = %d and `gcid` = %d and `zcid` = %d limit 1",
140 q("insert into glink ( `cid`,`uid`,`gcid`,`zcid`, `updated`) values (%d,%d,%d,%d, '%s') ",
145 dbesc(datetime_convert())
149 q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d and zcid = %d",
150 dbesc(datetime_convert()),
159 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
161 q("delete from glink where `cid` = %d and `uid` = %d and `zcid` = %d and `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
170 function count_common_friends($uid,$cid) {
172 $r = q("SELECT count(*) as `total`
173 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
174 where `glink`.`cid` = %d and `glink`.`uid` = %d
175 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
182 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
184 return $r[0]['total'];
190 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
193 $sql_extra = " order by rand() ";
195 $sql_extra = " order by `gcontact`.`name` asc ";
197 $r = q("SELECT `gcontact`.*
198 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
199 where `glink`.`cid` = %d and `glink`.`uid` = %d
200 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d )
201 $sql_extra limit %d, %d",
215 function count_common_friends_zcid($uid,$zcid) {
217 $r = q("SELECT count(*) as `total`
218 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
219 where `glink`.`zcid` = %d
220 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
226 return $r[0]['total'];
231 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
234 $sql_extra = " order by rand() ";
236 $sql_extra = " order by `gcontact`.`name` asc ";
238 $r = q("SELECT `gcontact`.*
239 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
240 where `glink`.`zcid` = %d
241 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
242 $sql_extra limit %d, %d",
254 function count_all_friends($uid,$cid) {
256 $r = q("SELECT count(*) as `total`
257 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
258 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
264 return $r[0]['total'];
270 function all_friends($uid,$cid,$start = 0, $limit = 80) {
272 $r = q("SELECT `gcontact`.*
273 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
274 where `glink`.`cid` = %d and `glink`.`uid` = %d
275 order by `gcontact`.`name` asc LIMIT %d, %d ",
287 function suggestion_query($uid, $start = 0, $limit = 80) {
292 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
293 INNER JOIN glink on glink.gcid = gcontact.id
294 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
295 and not gcontact.name in ( select name from contact where uid = %d )
296 and not gcontact.id in ( select gcid from gcign where uid = %d )
297 group by glink.gcid order by total desc limit %d, %d ",
306 if(count($r) && count($r) >= ($limit -1))
309 $r2 = q("SELECT gcontact.* from gcontact
310 INNER JOIN glink on glink.gcid = gcontact.id
311 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
312 and not gcontact.name in ( select name from contact where uid = %d )
313 and not gcontact.id in ( select gcid from gcign where uid = %d )
314 order by rand() limit %d, %d ",
323 return array_merge($r,$r2);
327 function update_suggestions() {
333 poco_load(0,0,0,$a->get_baseurl() . '/poco');
335 $done[] = $a->get_baseurl() . '/poco';
337 if(strlen(get_config('system','directory_submit_url'))) {
338 $x = fetch_url('http://dir.friendica.com/pubsites');
340 $j = json_decode($x);
342 foreach($j->entries as $entry) {
343 $url = $entry->url . '/poco';
344 if(! in_array($url,$done))
345 poco_load(0,0,0,$entry->url . '/poco');
351 $r = q("select distinct(poco) as poco from contact where network = '%s'",
357 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
358 if(! in_array($base,$done))
359 poco_load(0,0,0,$base);