3 require_once('include/datetime.php');
9 * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
10 * and add the entries to the gcontact (Global Contact) table, or update existing entries
11 * if anything (name or photo) has changed.
12 * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
14 * Once the global contact is stored add (if necessary) the contact linkage which associates
15 * the given uid, cid to the global contact entry. There can be many uid/cid combinations
16 * pointing to the same global contact id.
23 function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
27 if((! $url) || (! $uid)) {
28 $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
43 $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos' : '?fields=displayName,urls,photos') ;
45 logger('poco_load: ' . $url, LOGGER_DEBUG);
49 logger('poco_load: returns ' . $s, LOGGER_DATA);
51 logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
53 if(($a->get_curl_code() > 299) || (! $s))
58 logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
60 if(! isset($j->entry))
64 foreach($j->entry as $entry) {
72 $name = $entry->displayName;
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);
84 foreach($entry->photos as $photo) {
85 if($photo->type == 'profile') {
86 $profile_photo = $photo->value;
91 if((! $name) || (! $profile_url) || (! $profile_photo))
94 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
95 dbesc(normalise_link($profile_url))
101 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo) {
102 q("update gcontact set `name` = '%s', `photo` = '%s', `connect` = '%s'
103 where `nurl` = '%s' limit 1",
105 dbesc($profile_photo),
107 dbesc(normalise_link($profile_url))
112 q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`,`connect`)
113 values ( '%s', '%s', '%s', '%s','%s') ",
116 dbesc(normalise_link($profile_url)),
117 dbesc($profile_photo),
120 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
121 dbesc(normalise_link($profile_url))
129 $r = q("select * from glink where `cid` = %d and `uid` = %d and `gcid` = %d and `zcid` = %d limit 1",
136 q("insert into glink ( `cid`,`uid`,`gcid`,`zcid`, `updated`) values (%d,%d,%d,%d, '%s') ",
141 dbesc(datetime_convert())
145 q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d and zcid = %d limit 1",
146 dbesc(datetime_convert()),
155 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
157 q("delete from glink where `cid` = %d and `uid` = %d and `zcid` = %d and `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
166 function count_common_friends($uid,$cid) {
168 $r = q("SELECT count(*) as `total`
169 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
170 where `glink`.`cid` = %d and `glink`.`uid` = %d
171 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d ) ",
179 return $r[0]['total'];
185 function common_friends($uid,$cid) {
187 $r = q("SELECT `gcontact`.*
188 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
189 where `glink`.`cid` = %d and `glink`.`uid` = %d
190 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d )
191 order by `gcontact`.`name` asc ",
203 function count_common_friends_remote($uid,$cid,$zid) {
205 $r = q("SELECT count(*) as `total`
206 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
207 where ((`glink`.cid != 0 and `glink`.`cid` = %d) or ( `glink`.`zcid` != 0 and `glink`.`zcid` = %d ))
208 and `glink`.`uid` = %d
209 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d ) ",
218 return $r[0]['total'];
223 function common_friends_remote($uid,$cid,$zid,$limit = 6) {
225 $r = q("SELECT `gcontact`.*
226 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
227 where ((`glink`.cid != 0 and `glink`.`cid` = %d) or ( `glink`.`zcid` != 0 and `glink`.`zcid` = %d ))
228 and `glink`.`uid` = %d
229 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d )
230 order by `gcontact`.`name` asc limit 0, %d",
244 function count_all_friends($uid,$cid) {
246 $r = q("SELECT count(*) as `total`
247 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
248 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
254 return $r[0]['total'];
260 function all_friends($uid,$cid,$start = 0, $limit = 80) {
262 $r = q("SELECT `gcontact`.*
263 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
264 where `glink`.`cid` = %d and `glink`.`uid` = %d
265 order by `gcontact`.`name` asc LIMIT %d, %d ",
277 function suggestion_query($uid, $start = 0, $limit = 80) {
282 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
283 left join glink on glink.gcid = gcontact.id
284 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
285 and not gcontact.name in ( select name from contact where uid = %d )
286 and not gcontact.id in ( select gcid from gcign where uid = %d )
287 group by glink.gcid order by total desc limit %d, %d ",
296 if(count($r) && count($r) >= ($limit -1))
299 $r2 = q("SELECT gcontact.* from gcontact
300 left join glink on glink.gcid = gcontact.id
301 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
302 and not gcontact.name in ( select name from contact where uid = %d )
303 and not gcontact.id in ( select gcid from gcign where uid = %d )
304 order by rand() limit %d, %d ",
313 return array_merge($r,$r2);
317 function update_suggestions() {
323 poco_load(0,0,0,$a->get_baseurl() . '/poco');
325 $done[] = $a->get_baseurl() . '/poco';
327 if(strlen(get_config('system','directory_submit_url'))) {
328 $x = fetch_url('http://dir.friendica.com/pubsites');
330 $j = json_decode($x);
332 foreach($j->entries as $entry) {
333 $url = $entry->url . '/poco';
334 if(! in_array($url,$done))
335 poco_load(0,0,0,$entry->url . '/poco');
341 $r = q("select distinct(poco) as poco from contact where network = '%s'",
347 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
348 if(! in_array($base,$done))
349 poco_load(0,0,0,$base);