3 require_once('include/datetime.php');
4 require_once('include/zot.php');
10 * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
11 * and add the entries to the gcontact (Global Contact) table, or update existing entries
12 * if anything (name or photo) has changed.
13 * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
15 * Once the global contact is stored add (if necessary) the contact linkage which associates
16 * the given uid, cid to the global contact entry. There can be many uid/cid combinations
17 * pointing to the same global contact id.
24 function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
28 if((! $url) || (! $uid)) {
29 $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
44 $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos' : '?fields=displayName,urls,photos') ;
46 logger('poco_load: ' . $url, LOGGER_DEBUG);
50 logger('poco_load: returns ' . $s, LOGGER_DATA);
52 logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
54 if(($a->get_curl_code() > 299) || (! $s))
59 logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
61 if(! isset($j->entry))
65 foreach($j->entry as $entry) {
73 $name = $entry->displayName;
75 if(isset($entry->urls)) {
76 foreach($entry->urls as $url) {
77 if($url->type == 'profile') {
78 $profile_url = $url->value;
81 if($url->type == 'webfinger') {
82 $connect_url = str_replace('acct:' , '', $url->value);
87 if(isset($entry->photos)) {
88 foreach($entry->photos as $photo) {
89 if($photo->type == 'profile') {
90 $profile_photo = $photo->value;
96 if((! $name) || (! $profile_url) || (! $profile_photo))
99 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
100 dbesc(normalise_link($profile_url))
106 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo) {
107 q("update gcontact set `name` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s'
108 where `nurl` = '%s' limit 1",
110 dbesc($profile_photo),
113 dbesc(normalise_link($profile_url))
118 q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`,`connect`)
119 values ( '%s', '%s', '%s', '%s','%s') ",
122 dbesc(normalise_link($profile_url)),
123 dbesc($profile_photo),
126 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
127 dbesc(normalise_link($profile_url))
135 $r = q("select * from glink where `cid` = %d and `uid` = %d and `gcid` = %d and `zcid` = %d limit 1",
142 q("insert into glink ( `cid`,`uid`,`gcid`,`zcid`, `updated`) values (%d,%d,%d,%d, '%s') ",
147 dbesc(datetime_convert())
151 q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d and zcid = %d limit 1",
152 dbesc(datetime_convert()),
161 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
163 q("delete from glink where `cid` = %d and `uid` = %d and `zcid` = %d and `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
172 function count_common_friends($uid,$cid) {
174 $r = q("SELECT count(*) as `total`
175 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
176 where `glink`.`cid` = %d and `glink`.`uid` = %d
177 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
184 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
186 return $r[0]['total'];
192 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
195 $sql_extra = " order by rand() ";
197 $sql_extra = " order by `gcontact`.`name` asc ";
199 $r = q("SELECT `gcontact`.*
200 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
201 where `glink`.`cid` = %d and `glink`.`uid` = %d
202 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d )
203 $sql_extra limit %d, %d",
217 function count_common_friends_zcid($uid,$zcid) {
219 $r = q("SELECT count(*) as `total`
220 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
221 where `glink`.`zcid` = %d
222 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
228 return $r[0]['total'];
233 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
236 $sql_extra = " order by rand() ";
238 $sql_extra = " order by `gcontact`.`name` asc ";
240 $r = q("SELECT `gcontact`.*
241 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
242 where `glink`.`zcid` = %d
243 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
244 $sql_extra limit %d, %d",
256 function count_all_friends($uid,$cid) {
258 $r = q("SELECT count(*) as `total`
259 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
260 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
266 return $r[0]['total'];
272 function all_friends($uid,$cid,$start = 0, $limit = 80) {
274 $r = q("SELECT `gcontact`.*
275 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
276 where `glink`.`cid` = %d and `glink`.`uid` = %d
277 order by `gcontact`.`name` asc LIMIT %d, %d ",
289 function suggestion_query($uid, $start = 0, $limit = 80) {
294 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
295 left join glink on glink.gcid = gcontact.id
296 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
297 and not gcontact.name in ( select name from contact where uid = %d )
298 and not gcontact.id in ( select gcid from gcign where uid = %d )
299 group by glink.gcid order by total desc limit %d, %d ",
308 if(count($r) && count($r) >= ($limit -1))
311 $r2 = q("SELECT gcontact.* from gcontact
312 left join glink on glink.gcid = gcontact.id
313 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
314 and not gcontact.name in ( select name from contact where uid = %d )
315 and not gcontact.id in ( select gcid from gcign where uid = %d )
316 order by rand() limit %d, %d ",
325 return array_merge($r,$r2);
329 function update_suggestions() {
335 poco_load(0,0,0,$a->get_baseurl() . '/poco');
337 $done[] = $a->get_baseurl() . '/poco';
339 if(strlen(get_config('system','directory_submit_url'))) {
340 $x = fetch_url('http://dir.friendica.com/pubsites');
342 $j = json_decode($x);
344 foreach($j->entries as $entry) {
345 $url = $entry->url . '/poco';
346 if(! in_array($url,$done))
347 poco_load(0,0,0,$entry->url . '/poco');
353 $r = q("select distinct(poco) as poco from contact where network = '%s'",
359 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
360 if(! in_array($base,$done))
361 poco_load(0,0,0,$base);