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,updated,network,aboutMe,currentLocation,tags,gender' : '?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender') ;
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) {
71 $updated = '0000-00-00 00:00:00';
77 $name = $entry->displayName;
79 if(isset($entry->urls)) {
80 foreach($entry->urls as $url) {
81 if($url->type == 'profile') {
82 $profile_url = $url->value;
85 if($url->type == 'webfinger') {
86 $connect_url = str_replace('acct:' , '', $url->value);
91 if(isset($entry->photos)) {
92 foreach($entry->photos as $photo) {
93 if($photo->type == 'profile') {
94 $profile_photo = $photo->value;
100 if(isset($entry->updated))
101 $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
103 if(isset($entry->network))
104 $network = $entry->network;
106 if(isset($entry->currentLocation))
107 $location = $entry->currentLocation;
109 if(isset($entry->aboutMe))
110 $about = $entry->aboutMe;
112 if(isset($entry->gender))
113 $gender = $entry->gender;
115 if(isset($entry->tags))
116 foreach($entry->tags as $tag)
117 $keywords = implode(", ", $tag);
119 if ($network != "") {
120 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $cid, $uid, $zcid);
122 // Update the Friendica contacts. Diaspora is doing it via a message. (See include/diaspora.php)
123 if (($location != "") OR ($about != "") OR ($keywords != "") OR ($gender != ""))
124 q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
125 WHERE `nurl` = '%s' AND NOT `self` AND `network` = '%s'",
130 dbesc(normalise_link($profile_url)),
131 dbesc(NETWORK_DFRN));
134 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
136 q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
144 function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $cid = 0, $uid = 0, $zcid = 0) {
147 if ($profile_url == "")
150 if (($network == "") OR ($name == "") OR ($profile_photo == "")) {
151 require_once("include/Scrape.php");
153 $data = probe_url($profile_url, PROBE_DIASPORA);
154 $network = $data["network"];
155 $name = $data["name"];
156 $profile_photo = $data["photo"];
159 if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_STATUSNET)))
162 if (($name == "") OR ($profile_photo == ""))
165 logger("profile-check URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
167 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
168 dbesc(normalise_link($profile_url))
174 if (($location == "") AND ($x[0]['location'] != ""))
175 $location = $x[0]['location'];
177 if (($about == "") AND ($x[0]['about'] != ""))
178 $about = $x[0]['about'];
180 if (($gender == "") AND ($x[0]['gender'] != ""))
181 $gender = $x[0]['gender'];
183 if (($keywords == "") AND ($x[0]['keywords'] != ""))
184 $keywords = $x[0]['keywords'];
186 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
187 q("update gcontact set `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s',
188 `updated` = '%s', `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
189 where `nurl` = '%s'",
192 dbesc($profile_photo),
200 dbesc(normalise_link($profile_url))
204 q("insert into `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `updated`, `location`, `about`, `keywords`, `gender`)
205 values ('%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s')",
209 dbesc(normalise_link($profile_url)),
210 dbesc($profile_photo),
218 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
219 dbesc(normalise_link($profile_url))
228 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
235 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
240 dbesc(datetime_convert())
243 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
244 dbesc(datetime_convert()),
252 // For unknown reasons there are sometimes duplicates
253 q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
254 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
255 dbesc(normalise_link($profile_url)),
262 function poco_contact_from_body($body, $created, $cid, $uid) {
263 preg_replace_callback("/\[share(.*?)\].*?\[\/share\]/ism",
264 function ($match) use ($created, $cid, $uid){
265 return(sub_poco_from_share($match, $created, $cid, $uid));
269 function sub_poco_from_share($share, $created, $cid, $uid) {
271 preg_match("/profile='(.*?)'/ism", $share[1], $matches);
272 if ($matches[1] != "")
273 $profile = $matches[1];
275 preg_match('/profile="(.*?)"/ism', $share[1], $matches);
276 if ($matches[1] != "")
277 $profile = $matches[1];
282 logger("prepare poco_check for profile ".$profile, LOGGER_DEBUG);
283 poco_check($profile, "", "", "", "", "", "", "", "", $created, $cid, $uid);
286 function count_common_friends($uid,$cid) {
288 $r = q("SELECT count(*) as `total`
289 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
290 where `glink`.`cid` = %d and `glink`.`uid` = %d
291 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
298 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
300 return $r[0]['total'];
306 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
309 $sql_extra = " order by rand() ";
311 $sql_extra = " order by `gcontact`.`name` asc ";
313 $r = q("SELECT `gcontact`.*
314 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
315 where `glink`.`cid` = %d and `glink`.`uid` = %d
316 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d )
317 $sql_extra limit %d, %d",
331 function count_common_friends_zcid($uid,$zcid) {
333 $r = q("SELECT count(*) as `total`
334 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
335 where `glink`.`zcid` = %d
336 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
342 return $r[0]['total'];
347 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
350 $sql_extra = " order by rand() ";
352 $sql_extra = " order by `gcontact`.`name` asc ";
354 $r = q("SELECT `gcontact`.*
355 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
356 where `glink`.`zcid` = %d
357 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
358 $sql_extra limit %d, %d",
370 function count_all_friends($uid,$cid) {
372 $r = q("SELECT count(*) as `total`
373 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
374 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
380 return $r[0]['total'];
386 function all_friends($uid,$cid,$start = 0, $limit = 80) {
388 $r = q("SELECT `gcontact`.*
389 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
390 where `glink`.`cid` = %d and `glink`.`uid` = %d
391 order by `gcontact`.`name` asc LIMIT %d, %d ",
403 function suggestion_query($uid, $start = 0, $limit = 80) {
408 $network = array(NETWORK_DFRN);
410 if (get_config('system','diaspora_enabled'))
411 $network[] = NETWORK_DIASPORA;
413 if (!get_config('system','ostatus_disabled'))
414 $network[] = NETWORK_OSTATUS;
416 $sql_network = implode("', '", $network);
417 //$sql_network = "'".$sql_network."', ''";
418 $sql_network = "'".$sql_network."'";
420 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
421 INNER JOIN glink on glink.gcid = gcontact.id
422 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
423 and not gcontact.name in ( select name from contact where uid = %d )
424 and not gcontact.id in ( select gcid from gcign where uid = %d )
425 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
426 AND `gcontact`.`network` IN (%s)
427 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
437 if(count($r) && count($r) >= ($limit -1))
440 $r2 = q("SELECT gcontact.* from gcontact
441 INNER JOIN glink on glink.gcid = gcontact.id
442 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
443 and not gcontact.name in ( select name from contact where uid = %d )
444 and not gcontact.id in ( select gcid from gcign where uid = %d )
445 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
446 AND `gcontact`.`network` IN (%s)
447 order by rand() limit %d, %d ",
457 foreach ($r2 AS $suggestion)
458 $list[$suggestion["nurl"]] = $suggestion;
460 foreach ($r AS $suggestion)
461 $list[$suggestion["nurl"]] = $suggestion;
466 function update_suggestions() {
472 poco_load(0,0,0,$a->get_baseurl() . '/poco');
474 $done[] = $a->get_baseurl() . '/poco';
476 if(strlen(get_config('system','directory_submit_url'))) {
477 $x = fetch_url('http://dir.friendica.com/pubsites');
479 $j = json_decode($x);
481 foreach($j->entries as $entry) {
482 $url = $entry->url . '/poco';
483 if(! in_array($url,$done))
484 poco_load(0,0,0,$entry->url . '/poco');
490 $r = q("select distinct(poco) as poco from contact where network = '%s'",
496 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
497 if(! in_array($base,$done))
498 poco_load(0,0,0,$base);