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) {
24 require_once("include/html2bbcode.php");
29 if((! $url) || (! $uid)) {
30 $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
45 $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,generation' : '?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,generation') ;
47 logger('poco_load: ' . $url, LOGGER_DEBUG);
51 logger('poco_load: returns ' . $s, LOGGER_DATA);
53 logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
55 if(($a->get_curl_code() > 299) || (! $s))
60 logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
62 if(! isset($j->entry))
66 foreach($j->entry as $entry) {
74 $updated = '0000-00-00 00:00:00';
81 $name = $entry->displayName;
83 if(isset($entry->urls)) {
84 foreach($entry->urls as $url) {
85 if($url->type == 'profile') {
86 $profile_url = $url->value;
89 if($url->type == 'webfinger') {
90 $connect_url = str_replace('acct:' , '', $url->value);
95 if(isset($entry->photos)) {
96 foreach($entry->photos as $photo) {
97 if($photo->type == 'profile') {
98 $profile_photo = $photo->value;
104 if(isset($entry->updated))
105 $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
107 if(isset($entry->network))
108 $network = $entry->network;
110 if(isset($entry->currentLocation))
111 $location = $entry->currentLocation;
113 if(isset($entry->aboutMe))
114 $about = html2bbcode($entry->aboutMe);
116 if(isset($entry->gender))
117 $gender = $entry->gender;
119 if(isset($entry->generation) AND ($entry->generation > 0))
120 $generation = ++$entry->generation;
122 if(isset($entry->tags))
123 foreach($entry->tags as $tag)
124 $keywords = implode(", ", $tag);
126 // If you query a Friendica server for its profiles, the network has to be Friendica
128 $network = NETWORK_DFRN;
130 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid, $uid, $zcid);
132 // Update the Friendica contacts. Diaspora is doing it via a message. (See include/diaspora.php)
133 if (($location != "") OR ($about != "") OR ($keywords != "") OR ($gender != ""))
134 q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
135 WHERE `nurl` = '%s' AND NOT `self` AND `network` = '%s'",
140 dbesc(normalise_link($profile_url)),
141 dbesc(NETWORK_DFRN));
143 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
145 q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
153 function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid = 0, $uid = 0, $zcid = 0) {
159 // 1: Profiles on this server
160 // 2: Contacts of profiles on this server
161 // 3: Contacts of contacts of profiles on this server
166 if ($profile_url == "")
169 // Don't store the statusnet connector as network
170 // We can't simply set this to NETWORK_OSTATUS since the connector could have fetched posts from friendica as well
171 if ($network == NETWORK_STATUSNET)
174 // The global contacts should contain the original picture, not the cached one
175 if (($generation != 1) AND stristr(normalise_link($profile_photo), normalise_link($a->get_baseurl()."/photo/")))
178 $r = q("SELECT `network` FROM `contact` WHERE `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1",
179 dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
182 $network = $r[0]["network"];
184 if (($network == "") OR ($network == NETWORK_OSTATUS)) {
185 $r = q("SELECT `network`, `url` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1",
186 dbesc($profile_url), dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
189 $network = $r[0]["network"];
190 $profile_url = $r[0]["url"];
194 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
195 dbesc(normalise_link($profile_url))
197 if(count($x) AND ($network == "") AND ($x[0]["network"] != NETWORK_STATUSNET))
198 $network = $x[0]["network"];
200 if (($network == "") OR ($name == "") OR ($profile_photo == "")) {
201 require_once("include/Scrape.php");
203 $data = probe_url($profile_url);
204 $network = $data["network"];
205 $name = $data["name"];
206 $profile_url = $data["url"];
207 $profile_photo = $data["photo"];
210 if (count($x) AND ($x[0]["network"] == "") AND ($network != "")) {
211 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
213 dbesc(normalise_link($profile_url))
217 if (($name == "") OR ($profile_photo == ""))
220 if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
223 logger("profile-check generation: ".$generation." Network: ".$network." URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
228 if (($location == "") AND ($x[0]['location'] != ""))
229 $location = $x[0]['location'];
231 if (($about == "") AND ($x[0]['about'] != ""))
232 $about = $x[0]['about'];
234 if (($gender == "") AND ($x[0]['gender'] != ""))
235 $gender = $x[0]['gender'];
237 if (($keywords == "") AND ($x[0]['keywords'] != ""))
238 $keywords = $x[0]['keywords'];
240 if (($generation == 0) AND ($x[0]['generation'] > 0))
241 $generation = $x[0]['generation'];
243 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
244 q("UPDATE `gcontact` SET `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s',
245 `updated` = '%s', `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s', `generation` = %d
246 WHERE (`generation` >= %d OR `generation` = 0) AND `nurl` = '%s'",
249 dbesc($profile_photo),
259 dbesc(normalise_link($profile_url))
263 q("INSERT INTO `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `updated`, `location`, `about`, `keywords`, `gender`, `generation`)
264 VALUES ('%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', %d)",
268 dbesc(normalise_link($profile_url)),
269 dbesc($profile_photo),
278 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
279 dbesc(normalise_link($profile_url))
288 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
295 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
300 dbesc(datetime_convert())
303 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
304 dbesc(datetime_convert()),
312 // For unknown reasons there are sometimes duplicates
313 q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
314 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
315 dbesc(normalise_link($profile_url)),
322 function poco_contact_from_body($body, $created, $cid, $uid) {
323 preg_replace_callback("/\[share(.*?)\].*?\[\/share\]/ism",
324 function ($match) use ($created, $cid, $uid){
325 return(sub_poco_from_share($match, $created, $cid, $uid));
329 function sub_poco_from_share($share, $created, $cid, $uid) {
331 preg_match("/profile='(.*?)'/ism", $share[1], $matches);
332 if ($matches[1] != "")
333 $profile = $matches[1];
335 preg_match('/profile="(.*?)"/ism', $share[1], $matches);
336 if ($matches[1] != "")
337 $profile = $matches[1];
342 logger("prepare poco_check for profile ".$profile, LOGGER_DEBUG);
343 poco_check($profile, "", "", "", "", "", "", "", "", $created, 3, $cid, $uid);
346 function poco_store($item) {
349 if ($item['private'])
352 // Or is it from a network where we don't store the global contacts?
353 if (!in_array($item["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, NETWORK_STATUSNET, "")))
356 // Is it a global copy?
357 $store_gcontact = ($item["uid"] == 0);
359 // Is it a comment on a global copy?
360 if (!$store_gcontact AND ($item["uri"] != $item["parent-uri"])) {
361 $q = q("SELECT `id` FROM `item` WHERE `uri`='%s' AND `uid` = 0", $item["parent-uri"]);
362 $store_gcontact = count($q);
365 if (!$store_gcontact)
368 // "3" means: We don't know this contact directly (Maybe a reshared item)
371 $profile_url = $item["author-link"];
373 // Is it a user from our server?
374 $q = q("SELECT `id` FROM `contact` WHERE `self` AND `nurl` = '%s' LIMIT 1",
375 dbesc(normalise_link($item["author-link"])));
377 logger("Our user (generation 1): ".$item["author-link"], LOGGER_DEBUG);
379 $network = NETWORK_DFRN;
380 } else { // Is it a contact from a user on our server?
381 $q = q("SELECT `network`, `url` FROM `contact` WHERE `uid` != 0 AND `network` != ''
382 AND (`nurl` = '%s' OR `alias` IN ('%s', '%s')) AND `network` != '%s' LIMIT 1",
383 dbesc(normalise_link($item["author-link"])),
384 dbesc(normalise_link($item["author-link"])),
385 dbesc($item["author-link"]),
386 dbesc(NETWORK_STATUSNET));
389 $network = $q[0]["network"];
390 $profile_url = $q[0]["url"];
391 logger("Known contact (generation 2): ".$profile_url, LOGGER_DEBUG);
395 if ($generation == 3)
396 logger("Unknown contact (generation 3): ".$item["author-link"], LOGGER_DEBUG);
398 poco_check($profile_url, $item["author-name"], $network, $item["author-avatar"], "", "", "", "", "", $item["received"], $generation, $item["contact-id"], $item["uid"]);
400 // Maybe its a body with a shared item? Then extract a global contact from it.
401 poco_contact_from_body($item["body"], $item["received"], $item["contact-id"], $item["uid"]);
404 function count_common_friends($uid,$cid) {
406 $r = q("SELECT count(*) as `total`
407 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
408 where `glink`.`cid` = %d and `glink`.`uid` = %d
409 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
416 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
418 return $r[0]['total'];
424 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
427 $sql_extra = " order by rand() ";
429 $sql_extra = " order by `gcontact`.`name` asc ";
431 $r = q("SELECT `gcontact`.*
432 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
433 where `glink`.`cid` = %d and `glink`.`uid` = %d
434 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d )
435 $sql_extra limit %d, %d",
449 function count_common_friends_zcid($uid,$zcid) {
451 $r = q("SELECT count(*) as `total`
452 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
453 where `glink`.`zcid` = %d
454 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
460 return $r[0]['total'];
465 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
468 $sql_extra = " order by rand() ";
470 $sql_extra = " order by `gcontact`.`name` asc ";
472 $r = q("SELECT `gcontact`.*
473 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
474 where `glink`.`zcid` = %d
475 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
476 $sql_extra limit %d, %d",
488 function count_all_friends($uid,$cid) {
490 $r = q("SELECT count(*) as `total`
491 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
492 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
498 return $r[0]['total'];
504 function all_friends($uid,$cid,$start = 0, $limit = 80) {
506 $r = q("SELECT `gcontact`.*
507 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
508 where `glink`.`cid` = %d and `glink`.`uid` = %d
509 order by `gcontact`.`name` asc LIMIT %d, %d ",
521 function suggestion_query($uid, $start = 0, $limit = 80) {
526 $network = array(NETWORK_DFRN);
528 if (get_config('system','diaspora_enabled'))
529 $network[] = NETWORK_DIASPORA;
531 if (!get_config('system','ostatus_disabled'))
532 $network[] = NETWORK_OSTATUS;
534 $sql_network = implode("', '", $network);
535 //$sql_network = "'".$sql_network."', ''";
536 $sql_network = "'".$sql_network."'";
538 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
539 INNER JOIN glink on glink.gcid = gcontact.id
540 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
541 and not gcontact.name in ( select name from contact where uid = %d )
542 and not gcontact.id in ( select gcid from gcign where uid = %d )
543 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
544 AND `gcontact`.`network` IN (%s)
545 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
555 if(count($r) && count($r) >= ($limit -1))
558 $r2 = q("SELECT gcontact.* from gcontact
559 INNER JOIN glink on glink.gcid = gcontact.id
560 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
561 and not gcontact.name in ( select name from contact where uid = %d )
562 and not gcontact.id in ( select gcid from gcign where uid = %d )
563 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
564 AND `gcontact`.`network` IN (%s)
565 order by rand() limit %d, %d ",
575 foreach ($r2 AS $suggestion)
576 $list[$suggestion["nurl"]] = $suggestion;
578 foreach ($r AS $suggestion)
579 $list[$suggestion["nurl"]] = $suggestion;
584 function update_suggestions() {
590 poco_load(0,0,0,$a->get_baseurl() . '/poco');
592 $done[] = $a->get_baseurl() . '/poco';
594 if(strlen(get_config('system','directory_submit_url'))) {
595 $x = fetch_url('http://dir.friendica.com/pubsites');
597 $j = json_decode($x);
599 foreach($j->entries as $entry) {
600 $url = $entry->url . '/poco';
601 if(! in_array($url,$done))
602 poco_load(0,0,0,$entry->url . '/poco');
608 $r = q("select distinct(poco) as poco from contact where network = '%s'",
614 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
615 if(! in_array($base,$done))
616 poco_load(0,0,0,$base);