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,updated') ;
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) {
70 $updated = '0000-00-00 00:00:00';
72 $name = $entry->displayName;
74 if(isset($entry->urls)) {
75 foreach($entry->urls as $url) {
76 if($url->type == 'profile') {
77 $profile_url = $url->value;
80 if($url->type == 'webfinger') {
81 $connect_url = str_replace('acct:' , '', $url->value);
86 if(isset($entry->photos)) {
87 foreach($entry->photos as $photo) {
88 if($photo->type == 'profile') {
89 $profile_photo = $photo->value;
95 if(isset($entry->updated))
96 $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
98 if((! $name) || (! $profile_url) || (! $profile_photo))
101 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
102 dbesc(normalise_link($profile_url))
108 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
109 q("update gcontact set `name` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s', `updated` = '%s'
110 where `nurl` = '%s'",
112 dbesc($profile_photo),
116 dbesc(normalise_link($profile_url))
120 q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`,`connect`, `updated`)
121 values ( '%s', '%s', '%s', '%s','%s') ",
124 dbesc(normalise_link($profile_url)),
125 dbesc($profile_photo),
129 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
130 dbesc(normalise_link($profile_url))
138 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
145 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
150 dbesc(datetime_convert())
154 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
155 dbesc(datetime_convert()),
163 // For unknown reasons there are sometimes duplicates
164 q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
165 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
166 dbesc(normalise_link($profile_url)),
171 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
173 q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
182 function count_common_friends($uid,$cid) {
184 $r = q("SELECT count(*) as `total`
185 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
186 where `glink`.`cid` = %d and `glink`.`uid` = %d
187 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
194 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
196 return $r[0]['total'];
202 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
205 $sql_extra = " order by rand() ";
207 $sql_extra = " order by `gcontact`.`name` asc ";
209 $r = q("SELECT `gcontact`.*
210 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
211 where `glink`.`cid` = %d and `glink`.`uid` = %d
212 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d )
213 $sql_extra limit %d, %d",
227 function count_common_friends_zcid($uid,$zcid) {
229 $r = q("SELECT count(*) as `total`
230 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
231 where `glink`.`zcid` = %d
232 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
238 return $r[0]['total'];
243 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
246 $sql_extra = " order by rand() ";
248 $sql_extra = " order by `gcontact`.`name` asc ";
250 $r = q("SELECT `gcontact`.*
251 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
252 where `glink`.`zcid` = %d
253 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
254 $sql_extra limit %d, %d",
266 function count_all_friends($uid,$cid) {
268 $r = q("SELECT count(*) as `total`
269 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
270 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
276 return $r[0]['total'];
282 function all_friends($uid,$cid,$start = 0, $limit = 80) {
284 $r = q("SELECT `gcontact`.*
285 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
286 where `glink`.`cid` = %d and `glink`.`uid` = %d
287 order by `gcontact`.`name` asc LIMIT %d, %d ",
299 function suggestion_query($uid, $start = 0, $limit = 80) {
304 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
305 INNER JOIN glink on glink.gcid = gcontact.id
306 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
307 and not gcontact.name in ( select name from contact where uid = %d )
308 and not gcontact.id in ( select gcid from gcign where uid = %d )
309 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
310 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
319 if(count($r) && count($r) >= ($limit -1))
322 $r2 = q("SELECT gcontact.* from gcontact
323 INNER JOIN glink on glink.gcid = gcontact.id
324 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
325 and not gcontact.name in ( select name from contact where uid = %d )
326 and not gcontact.id in ( select gcid from gcign where uid = %d )
327 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
328 order by rand() limit %d, %d ",
337 foreach ($r2 AS $suggestion)
338 $list[$suggestion["nurl"]] = $suggestion;
340 foreach ($r AS $suggestion)
341 $list[$suggestion["nurl"]] = $suggestion;
346 function update_suggestions() {
352 poco_load(0,0,0,$a->get_baseurl() . '/poco');
354 $done[] = $a->get_baseurl() . '/poco';
356 if(strlen(get_config('system','directory_submit_url'))) {
357 $x = fetch_url('http://dir.friendica.com/pubsites');
359 $j = json_decode($x);
361 foreach($j->entries as $entry) {
362 $url = $entry->url . '/poco';
363 if(! in_array($url,$done))
364 poco_load(0,0,0,$entry->url . '/poco');
370 $r = q("select distinct(poco) as poco from contact where network = '%s'",
376 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
377 if(! in_array($base,$done))
378 poco_load(0,0,0,$base);