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';
82 $network = NETWORK_DFRN;
84 $name = $entry->displayName;
86 if(isset($entry->urls)) {
87 foreach($entry->urls as $url) {
88 if($url->type == 'profile') {
89 $profile_url = $url->value;
92 if($url->type == 'webfinger') {
93 $connect_url = str_replace('acct:' , '', $url->value);
98 if(isset($entry->photos)) {
99 foreach($entry->photos as $photo) {
100 if($photo->type == 'profile') {
101 $profile_photo = $photo->value;
107 if(isset($entry->updated))
108 $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
110 if(isset($entry->network))
111 $network = $entry->network;
113 if(isset($entry->currentLocation))
114 $location = $entry->currentLocation;
116 if(isset($entry->aboutMe))
117 $about = html2bbcode($entry->aboutMe);
119 if(isset($entry->gender))
120 $gender = $entry->gender;
122 if(isset($entry->generation) AND ($entry->generation > 0))
123 $generation = ++$entry->generation;
125 if(isset($entry->tags))
126 foreach($entry->tags as $tag)
127 $keywords = implode(", ", $tag);
129 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid, $uid, $zcid);
131 // Update the Friendica contacts. Diaspora is doing it via a message. (See include/diaspora.php)
132 if (($location != "") OR ($about != "") OR ($keywords != "") OR ($gender != ""))
133 q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
134 WHERE `nurl` = '%s' AND NOT `self` AND `network` = '%s'",
139 dbesc(normalise_link($profile_url)),
140 dbesc(NETWORK_DFRN));
142 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
144 q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
152 function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid = 0, $uid = 0, $zcid = 0) {
156 // 1: Profiles on this server
157 // 2: Contacts of profiles on this server
158 // 3: Contacts of contacts of profiles on this server
163 if ($profile_url == "")
166 $r = q("SELECT `network` FROM `contact` WHERE `nurl` = '%s' AND `network` != '' LIMIT 1",
167 dbesc(normalise_link($profile_url))
170 $network = $r[0]["network"];
172 if ($network == "") {
173 $r = q("SELECT `network`, `url` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `network` != '' LIMIT 1",
174 dbesc($profile_url), dbesc(normalise_link($profile_url))
177 $network = $r[0]["network"];
178 $profile_url = $r[0]["url"];
182 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
183 dbesc(normalise_link($profile_url))
185 if(count($x) AND ($network == ""))
186 $network = $x[0]["network"];
188 if (($network == "") OR ($name == "") OR ($profile_photo == "")) {
189 require_once("include/Scrape.php");
191 $data = probe_url($profile_url, PROBE_DIASPORA);
192 $network = $data["network"];
193 $name = $data["name"];
194 $profile_photo = $data["photo"];
197 if (count($x) AND ($x[0]["network"] == "") AND ($network != "")) {
198 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
200 dbesc(normalise_link($profile_url))
204 if (($name == "") OR ($profile_photo == ""))
207 if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_STATUSNET)))
210 logger("profile-check generation: ".$generation." Network: ".$network." URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
215 if (($location == "") AND ($x[0]['location'] != ""))
216 $location = $x[0]['location'];
218 if (($about == "") AND ($x[0]['about'] != ""))
219 $about = $x[0]['about'];
221 if (($gender == "") AND ($x[0]['gender'] != ""))
222 $gender = $x[0]['gender'];
224 if (($keywords == "") AND ($x[0]['keywords'] != ""))
225 $keywords = $x[0]['keywords'];
227 if (($generation == 0) AND ($x[0]['generation'] > 0))
228 $generation = $x[0]['generation'];
230 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
231 q("UPDATE `gcontact` SET `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s',
232 `updated` = '%s', `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s', `generation` = %d
233 WHERE (`generation` >= %d OR `generation` = 0) AND `nurl` = '%s'",
236 dbesc($profile_photo),
246 dbesc(normalise_link($profile_url))
250 q("INSERT INTO `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `updated`, `location`, `about`, `keywords`, `gender`, `generation`)
251 VALUES ('%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', %d)",
255 dbesc(normalise_link($profile_url)),
256 dbesc($profile_photo),
265 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
266 dbesc(normalise_link($profile_url))
275 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
282 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
287 dbesc(datetime_convert())
290 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
291 dbesc(datetime_convert()),
299 // For unknown reasons there are sometimes duplicates
300 q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
301 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
302 dbesc(normalise_link($profile_url)),
309 function poco_contact_from_body($body, $created, $cid, $uid) {
310 preg_replace_callback("/\[share(.*?)\].*?\[\/share\]/ism",
311 function ($match) use ($created, $cid, $uid){
312 return(sub_poco_from_share($match, $created, $cid, $uid));
316 function sub_poco_from_share($share, $created, $cid, $uid) {
318 preg_match("/profile='(.*?)'/ism", $share[1], $matches);
319 if ($matches[1] != "")
320 $profile = $matches[1];
322 preg_match('/profile="(.*?)"/ism', $share[1], $matches);
323 if ($matches[1] != "")
324 $profile = $matches[1];
329 logger("prepare poco_check for profile ".$profile, LOGGER_DEBUG);
330 poco_check($profile, "", "", "", "", "", "", "", "", $created, 3, $cid, $uid);
333 function poco_store($item) {
336 if (!$item['private'])
339 // Or is it from a network where we don't store the global contacts?
340 if (!in_array($item["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, "")))
343 // Is it a global copy?
344 $store_gcontact = ($item["uid"] == 0);
346 // Is it a comment on a global copy?
347 if (!$store_gcontact AND ($item["uri"] != $item["parent-uri"])) {
348 $q = q("SELECT `id` FROM `item` WHERE `uri`='%s' AND `uid` = 0", $item["parent-uri"]);
349 $store_gcontact = count($q);
352 if (!$store_gcontact)
355 // "3" means: We don't know this contact directly (Maybe a reshared item)
359 // Is it a user from our server?
360 $q = q("SELECT `id` FROM `contact` WHERE `self` AND `nurl` = '%s' LIMIT 1",
361 dbesc(normalise_link($item["author-link"])));
364 $network = NETWORK_DFRN;
365 } else { // Is it a contact from a user on our server?
366 $q = q("SELECT `network` FROM `contact` WHERE `uid` != 0 AND `network` != ''
367 AND (`nurl` = '%s' OR `alias` IN ('%s', '%s')) LIMIT 1",
368 dbesc(normalise_link($item["author-link"])),
369 dbesc(normalise_link($item["author-link"])),
370 dbesc($item["author-link"]));
373 $network = $q[0]["network"];
376 poco_check($item["author-link"], $item["author-name"], $network, $item["author-avatar"], "", "", "", "", "", $item["received"], $generation, $item["contact-id"], $item["uid"]);
378 // Maybe its a body with a shared item? Then extract a global contact from it.
379 poco_contact_from_body($item["body"], $item["received"], $item["contact-id"], $item["uid"]);
382 function count_common_friends($uid,$cid) {
384 $r = q("SELECT count(*) as `total`
385 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
386 where `glink`.`cid` = %d and `glink`.`uid` = %d
387 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
394 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
396 return $r[0]['total'];
402 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
405 $sql_extra = " order by rand() ";
407 $sql_extra = " order by `gcontact`.`name` asc ";
409 $r = q("SELECT `gcontact`.*
410 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
411 where `glink`.`cid` = %d and `glink`.`uid` = %d
412 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d )
413 $sql_extra limit %d, %d",
427 function count_common_friends_zcid($uid,$zcid) {
429 $r = q("SELECT count(*) as `total`
430 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
431 where `glink`.`zcid` = %d
432 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
438 return $r[0]['total'];
443 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
446 $sql_extra = " order by rand() ";
448 $sql_extra = " order by `gcontact`.`name` asc ";
450 $r = q("SELECT `gcontact`.*
451 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
452 where `glink`.`zcid` = %d
453 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
454 $sql_extra limit %d, %d",
466 function count_all_friends($uid,$cid) {
468 $r = q("SELECT count(*) as `total`
469 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
470 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
476 return $r[0]['total'];
482 function all_friends($uid,$cid,$start = 0, $limit = 80) {
484 $r = q("SELECT `gcontact`.*
485 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
486 where `glink`.`cid` = %d and `glink`.`uid` = %d
487 order by `gcontact`.`name` asc LIMIT %d, %d ",
499 function suggestion_query($uid, $start = 0, $limit = 80) {
504 $network = array(NETWORK_DFRN);
506 if (get_config('system','diaspora_enabled'))
507 $network[] = NETWORK_DIASPORA;
509 if (!get_config('system','ostatus_disabled'))
510 $network[] = NETWORK_OSTATUS;
512 $sql_network = implode("', '", $network);
513 //$sql_network = "'".$sql_network."', ''";
514 $sql_network = "'".$sql_network."'";
516 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
517 INNER JOIN glink on glink.gcid = gcontact.id
518 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
519 and not gcontact.name in ( select name from contact where uid = %d )
520 and not gcontact.id in ( select gcid from gcign where uid = %d )
521 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
522 AND `gcontact`.`network` IN (%s)
523 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
533 if(count($r) && count($r) >= ($limit -1))
536 $r2 = q("SELECT gcontact.* from gcontact
537 INNER JOIN glink on glink.gcid = gcontact.id
538 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
539 and not gcontact.name in ( select name from contact where uid = %d )
540 and not gcontact.id in ( select gcid from gcign where uid = %d )
541 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
542 AND `gcontact`.`network` IN (%s)
543 order by rand() limit %d, %d ",
553 foreach ($r2 AS $suggestion)
554 $list[$suggestion["nurl"]] = $suggestion;
556 foreach ($r AS $suggestion)
557 $list[$suggestion["nurl"]] = $suggestion;
562 function update_suggestions() {
568 poco_load(0,0,0,$a->get_baseurl() . '/poco');
570 $done[] = $a->get_baseurl() . '/poco';
572 if(strlen(get_config('system','directory_submit_url'))) {
573 $x = fetch_url('http://dir.friendica.com/pubsites');
575 $j = json_decode($x);
577 foreach($j->entries as $entry) {
578 $url = $entry->url . '/poco';
579 if(! in_array($url,$done))
580 poco_load(0,0,0,$entry->url . '/poco');
586 $r = q("select distinct(poco) as poco from contact where network = '%s'",
592 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
593 if(! in_array($base,$done))
594 poco_load(0,0,0,$base);