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 poco_check($profile_url, $name, $profile_photo, $connect_url, $updated, $cid, $uid, $zcid);
101 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
103 q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
111 function poco_check($profile_url, $name, $profile_photo, $connect_url, $updated, $cid = 0, $uid = 0, $zcid = 0) {
114 if (($profile_url == "") OR ($name == "") OR ($profile_photo == ""))
117 logger("profile-check URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
119 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
120 dbesc(normalise_link($profile_url))
126 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
127 q("update gcontact set `name` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s', `updated` = '%s'
128 where `nurl` = '%s'",
130 dbesc($profile_photo),
134 dbesc(normalise_link($profile_url))
138 q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`,`connect`, `updated`)
139 values ('%s', '%s', '%s', '%s','%s', '%s')",
142 dbesc(normalise_link($profile_url)),
143 dbesc($profile_photo),
147 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
148 dbesc(normalise_link($profile_url))
157 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
164 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
169 dbesc(datetime_convert())
172 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
173 dbesc(datetime_convert()),
181 // For unknown reasons there are sometimes duplicates
182 q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
183 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
184 dbesc(normalise_link($profile_url)),
191 function count_common_friends($uid,$cid) {
193 $r = q("SELECT count(*) as `total`
194 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
195 where `glink`.`cid` = %d and `glink`.`uid` = %d
196 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
203 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
205 return $r[0]['total'];
211 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
214 $sql_extra = " order by rand() ";
216 $sql_extra = " order by `gcontact`.`name` asc ";
218 $r = q("SELECT `gcontact`.*
219 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
220 where `glink`.`cid` = %d and `glink`.`uid` = %d
221 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d )
222 $sql_extra limit %d, %d",
236 function count_common_friends_zcid($uid,$zcid) {
238 $r = q("SELECT count(*) as `total`
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 ) ",
247 return $r[0]['total'];
252 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
255 $sql_extra = " order by rand() ";
257 $sql_extra = " order by `gcontact`.`name` asc ";
259 $r = q("SELECT `gcontact`.*
260 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
261 where `glink`.`zcid` = %d
262 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
263 $sql_extra limit %d, %d",
275 function count_all_friends($uid,$cid) {
277 $r = q("SELECT count(*) as `total`
278 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
279 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
285 return $r[0]['total'];
291 function all_friends($uid,$cid,$start = 0, $limit = 80) {
293 $r = q("SELECT `gcontact`.*
294 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
295 where `glink`.`cid` = %d and `glink`.`uid` = %d
296 order by `gcontact`.`name` asc LIMIT %d, %d ",
308 function suggestion_query($uid, $start = 0, $limit = 80) {
313 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
314 INNER JOIN glink on glink.gcid = gcontact.id
315 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
316 and not gcontact.name in ( select name from contact where uid = %d )
317 and not gcontact.id in ( select gcid from gcign where uid = %d )
318 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
319 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
328 if(count($r) && count($r) >= ($limit -1))
331 $r2 = q("SELECT gcontact.* from gcontact
332 INNER JOIN glink on glink.gcid = gcontact.id
333 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
334 and not gcontact.name in ( select name from contact where uid = %d )
335 and not gcontact.id in ( select gcid from gcign where uid = %d )
336 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
337 order by rand() limit %d, %d ",
346 foreach ($r2 AS $suggestion)
347 $list[$suggestion["nurl"]] = $suggestion;
349 foreach ($r AS $suggestion)
350 $list[$suggestion["nurl"]] = $suggestion;
355 function update_suggestions() {
361 poco_load(0,0,0,$a->get_baseurl() . '/poco');
363 $done[] = $a->get_baseurl() . '/poco';
365 if(strlen(get_config('system','directory_submit_url'))) {
366 $x = fetch_url('http://dir.friendica.com/pubsites');
368 $j = json_decode($x);
370 foreach($j->entries as $entry) {
371 $url = $entry->url . '/poco';
372 if(! in_array($url,$done))
373 poco_load(0,0,0,$entry->url . '/poco');
379 $r = q("select distinct(poco) as poco from contact where network = '%s'",
385 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
386 if(! in_array($base,$done))
387 poco_load(0,0,0,$base);