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
127 // To-Do: It could also be a Redmatrix server
129 // $network = NETWORK_DFRN;
131 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid, $uid, $zcid);
133 // Update the Friendica contacts. Diaspora is doing it via a message. (See include/diaspora.php)
134 if (($location != "") OR ($about != "") OR ($keywords != "") OR ($gender != ""))
135 q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
136 WHERE `nurl` = '%s' AND NOT `self` AND `network` = '%s'",
141 dbesc(normalise_link($profile_url)),
142 dbesc(NETWORK_DFRN));
144 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
146 q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
154 function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid = 0, $uid = 0, $zcid = 0) {
160 // 1: Profiles on this server
161 // 2: Contacts of profiles on this server
162 // 3: Contacts of contacts of profiles on this server
167 if ($profile_url == "")
170 // Don't store the statusnet connector as network
171 // We can't simply set this to NETWORK_OSTATUS since the connector could have fetched posts from friendica as well
172 if ($network == NETWORK_STATUSNET)
175 // The global contacts should contain the original picture, not the cached one
176 if (($generation != 1) AND stristr(normalise_link($profile_photo), normalise_link($a->get_baseurl()."/photo/")))
179 $r = q("SELECT `network` FROM `contact` WHERE `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1",
180 dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
183 $network = $r[0]["network"];
185 if (($network == "") OR ($network == NETWORK_OSTATUS)) {
186 $r = q("SELECT `network`, `url` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1",
187 dbesc($profile_url), dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
190 $network = $r[0]["network"];
191 $profile_url = $r[0]["url"];
195 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
196 dbesc(normalise_link($profile_url))
198 if(count($x) AND ($network == "") AND ($x[0]["network"] != NETWORK_STATUSNET))
199 $network = $x[0]["network"];
201 if (($network == "") OR ($name == "") OR ($profile_photo == "")) {
202 require_once("include/Scrape.php");
204 $data = probe_url($profile_url);
205 $network = $data["network"];
206 $name = $data["name"];
207 $profile_url = $data["url"];
208 $profile_photo = $data["photo"];
211 if (count($x) AND ($x[0]["network"] == "") AND ($network != "")) {
212 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
214 dbesc(normalise_link($profile_url))
218 if (($name == "") OR ($profile_photo == ""))
221 if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
224 logger("profile-check generation: ".$generation." Network: ".$network." URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
229 if (($location == "") AND ($x[0]['location'] != ""))
230 $location = $x[0]['location'];
232 if (($about == "") AND ($x[0]['about'] != ""))
233 $about = $x[0]['about'];
235 if (($gender == "") AND ($x[0]['gender'] != ""))
236 $gender = $x[0]['gender'];
238 if (($keywords == "") AND ($x[0]['keywords'] != ""))
239 $keywords = $x[0]['keywords'];
241 if (($generation == 0) AND ($x[0]['generation'] > 0))
242 $generation = $x[0]['generation'];
244 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
245 q("UPDATE `gcontact` SET `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s',
246 `updated` = '%s', `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s', `generation` = %d
247 WHERE (`generation` >= %d OR `generation` = 0) AND `nurl` = '%s'",
250 dbesc($profile_photo),
260 dbesc(normalise_link($profile_url))
264 q("INSERT INTO `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `updated`, `location`, `about`, `keywords`, `gender`, `generation`)
265 VALUES ('%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', %d)",
269 dbesc(normalise_link($profile_url)),
270 dbesc($profile_photo),
279 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
280 dbesc(normalise_link($profile_url))
289 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
296 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
301 dbesc(datetime_convert())
304 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
305 dbesc(datetime_convert()),
313 // For unknown reasons there are sometimes duplicates
314 q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
315 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
316 dbesc(normalise_link($profile_url)),
323 function poco_contact_from_body($body, $created, $cid, $uid) {
324 preg_replace_callback("/\[share(.*?)\].*?\[\/share\]/ism",
325 function ($match) use ($created, $cid, $uid){
326 return(sub_poco_from_share($match, $created, $cid, $uid));
330 function sub_poco_from_share($share, $created, $cid, $uid) {
332 preg_match("/profile='(.*?)'/ism", $share[1], $matches);
333 if ($matches[1] != "")
334 $profile = $matches[1];
336 preg_match('/profile="(.*?)"/ism', $share[1], $matches);
337 if ($matches[1] != "")
338 $profile = $matches[1];
343 logger("prepare poco_check for profile ".$profile, LOGGER_DEBUG);
344 poco_check($profile, "", "", "", "", "", "", "", "", $created, 3, $cid, $uid);
347 function poco_store($item) {
350 if ($item['private'])
353 // Or is it from a network where we don't store the global contacts?
354 if (!in_array($item["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, NETWORK_STATUSNET, "")))
357 // Is it a global copy?
358 $store_gcontact = ($item["uid"] == 0);
360 // Is it a comment on a global copy?
361 if (!$store_gcontact AND ($item["uri"] != $item["parent-uri"])) {
362 $q = q("SELECT `id` FROM `item` WHERE `uri`='%s' AND `uid` = 0", $item["parent-uri"]);
363 $store_gcontact = count($q);
366 if (!$store_gcontact)
369 // "3" means: We don't know this contact directly (Maybe a reshared item)
372 $profile_url = $item["author-link"];
374 // Is it a user from our server?
375 $q = q("SELECT `id` FROM `contact` WHERE `self` AND `nurl` = '%s' LIMIT 1",
376 dbesc(normalise_link($item["author-link"])));
378 logger("Our user (generation 1): ".$item["author-link"], LOGGER_DEBUG);
380 $network = NETWORK_DFRN;
381 } else { // Is it a contact from a user on our server?
382 $q = q("SELECT `network`, `url` FROM `contact` WHERE `uid` != 0 AND `network` != ''
383 AND (`nurl` = '%s' OR `alias` IN ('%s', '%s')) AND `network` != '%s' LIMIT 1",
384 dbesc(normalise_link($item["author-link"])),
385 dbesc(normalise_link($item["author-link"])),
386 dbesc($item["author-link"]),
387 dbesc(NETWORK_STATUSNET));
390 $network = $q[0]["network"];
391 $profile_url = $q[0]["url"];
392 logger("Known contact (generation 2): ".$profile_url, LOGGER_DEBUG);
396 if ($generation == 3)
397 logger("Unknown contact (generation 3): ".$item["author-link"], LOGGER_DEBUG);
399 poco_check($profile_url, $item["author-name"], $network, $item["author-avatar"], "", "", "", "", "", $item["received"], $generation, $item["contact-id"], $item["uid"]);
401 // Maybe its a body with a shared item? Then extract a global contact from it.
402 poco_contact_from_body($item["body"], $item["received"], $item["contact-id"], $item["uid"]);
405 function count_common_friends($uid,$cid) {
407 $r = q("SELECT count(*) as `total`
408 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
409 where `glink`.`cid` = %d and `glink`.`uid` = %d
410 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
417 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
419 return $r[0]['total'];
425 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
428 $sql_extra = " order by rand() ";
430 $sql_extra = " order by `gcontact`.`name` asc ";
432 $r = q("SELECT `gcontact`.*
433 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
434 where `glink`.`cid` = %d and `glink`.`uid` = %d
435 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d )
436 $sql_extra limit %d, %d",
450 function count_common_friends_zcid($uid,$zcid) {
452 $r = q("SELECT count(*) as `total`
453 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
454 where `glink`.`zcid` = %d
455 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
461 return $r[0]['total'];
466 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
469 $sql_extra = " order by rand() ";
471 $sql_extra = " order by `gcontact`.`name` asc ";
473 $r = q("SELECT `gcontact`.*
474 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
475 where `glink`.`zcid` = %d
476 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
477 $sql_extra limit %d, %d",
489 function count_all_friends($uid,$cid) {
491 $r = q("SELECT count(*) as `total`
492 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
493 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
499 return $r[0]['total'];
505 function all_friends($uid,$cid,$start = 0, $limit = 80) {
507 $r = q("SELECT `gcontact`.*
508 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
509 where `glink`.`cid` = %d and `glink`.`uid` = %d
510 order by `gcontact`.`name` asc LIMIT %d, %d ",
522 function suggestion_query($uid, $start = 0, $limit = 80) {
527 $network = array(NETWORK_DFRN);
529 if (get_config('system','diaspora_enabled'))
530 $network[] = NETWORK_DIASPORA;
532 if (!get_config('system','ostatus_disabled'))
533 $network[] = NETWORK_OSTATUS;
535 $sql_network = implode("', '", $network);
536 //$sql_network = "'".$sql_network."', ''";
537 $sql_network = "'".$sql_network."'";
539 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
540 INNER JOIN glink on glink.gcid = gcontact.id
541 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
542 and not gcontact.name in ( select name from contact where uid = %d )
543 and not gcontact.id in ( select gcid from gcign where uid = %d )
544 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
545 AND `gcontact`.`network` IN (%s)
546 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
556 if(count($r) && count($r) >= ($limit -1))
559 $r2 = q("SELECT gcontact.* from gcontact
560 INNER JOIN glink on glink.gcid = gcontact.id
561 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
562 and not gcontact.name in ( select name from contact where uid = %d )
563 and not gcontact.id in ( select gcid from gcign where uid = %d )
564 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
565 AND `gcontact`.`network` IN (%s)
566 order by rand() limit %d, %d ",
576 foreach ($r2 AS $suggestion)
577 $list[$suggestion["nurl"]] = $suggestion;
579 foreach ($r AS $suggestion)
580 $list[$suggestion["nurl"]] = $suggestion;
585 function update_suggestions() {
591 poco_load(0,0,0,$a->get_baseurl() . '/poco');
593 $done[] = $a->get_baseurl() . '/poco';
595 if(strlen(get_config('system','directory_submit_url'))) {
596 $x = fetch_url('http://dir.friendica.com/pubsites');
598 $j = json_decode($x);
600 foreach($j->entries as $entry) {
601 $url = $entry->url . '/poco';
602 if(! in_array($url,$done))
603 poco_load(0,0,0,$entry->url . '/poco');
609 $r = q("select distinct(poco) as poco from contact where network = '%s'",
615 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
616 if(! in_array($base,$done))
617 poco_load(0,0,0,$base);