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 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $cid, $uid, $zcid);
121 // Update the Friendica contacts. Diaspora is doing it via a message. (See include/diaspora.php)
122 if (($location != "") OR ($about != "") OR ($keywords != "") OR ($gender != ""))
123 q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
124 WHERE `nurl` = '%s' AND NOT `self` AND `network` = '%s'",
129 dbesc(normalise_link($profile_url)),
130 dbesc(NETWORK_DFRN));
132 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
134 q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
142 function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $cid = 0, $uid = 0, $zcid = 0) {
145 if ($profile_url == "")
148 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
149 dbesc(normalise_link($profile_url))
152 $network = $x[0]["network"];
154 if (($network == "") OR ($name == "") OR ($profile_photo == "")) {
155 require_once("include/Scrape.php");
157 $data = probe_url($profile_url, PROBE_DIASPORA);
158 $network = $data["network"];
159 $name = $data["name"];
160 $profile_photo = $data["photo"];
163 if (count($x) AND ($x[0]["network"] == "") AND ($network != "")) {
164 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
166 dbesc(normalise_link($profile_url))
170 if (($name == "") OR ($profile_photo == ""))
173 if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_STATUSNET)))
176 logger("profile-check URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
181 if (($location == "") AND ($x[0]['location'] != ""))
182 $location = $x[0]['location'];
184 if (($about == "") AND ($x[0]['about'] != ""))
185 $about = $x[0]['about'];
187 if (($gender == "") AND ($x[0]['gender'] != ""))
188 $gender = $x[0]['gender'];
190 if (($keywords == "") AND ($x[0]['keywords'] != ""))
191 $keywords = $x[0]['keywords'];
193 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
194 q("update gcontact set `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s',
195 `updated` = '%s', `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
196 where `nurl` = '%s'",
199 dbesc($profile_photo),
207 dbesc(normalise_link($profile_url))
211 q("insert into `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `updated`, `location`, `about`, `keywords`, `gender`)
212 values ('%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s')",
216 dbesc(normalise_link($profile_url)),
217 dbesc($profile_photo),
225 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
226 dbesc(normalise_link($profile_url))
235 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
242 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
247 dbesc(datetime_convert())
250 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
251 dbesc(datetime_convert()),
259 // For unknown reasons there are sometimes duplicates
260 q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
261 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
262 dbesc(normalise_link($profile_url)),
269 function poco_contact_from_body($body, $created, $cid, $uid) {
270 preg_replace_callback("/\[share(.*?)\].*?\[\/share\]/ism",
271 function ($match) use ($created, $cid, $uid){
272 return(sub_poco_from_share($match, $created, $cid, $uid));
276 function sub_poco_from_share($share, $created, $cid, $uid) {
278 preg_match("/profile='(.*?)'/ism", $share[1], $matches);
279 if ($matches[1] != "")
280 $profile = $matches[1];
282 preg_match('/profile="(.*?)"/ism', $share[1], $matches);
283 if ($matches[1] != "")
284 $profile = $matches[1];
289 logger("prepare poco_check for profile ".$profile, LOGGER_DEBUG);
290 poco_check($profile, "", "", "", "", "", "", "", "", $created, $cid, $uid);
293 function count_common_friends($uid,$cid) {
295 $r = q("SELECT count(*) as `total`
296 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
297 where `glink`.`cid` = %d and `glink`.`uid` = %d
298 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
305 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
307 return $r[0]['total'];
313 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
316 $sql_extra = " order by rand() ";
318 $sql_extra = " order by `gcontact`.`name` asc ";
320 $r = q("SELECT `gcontact`.*
321 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
322 where `glink`.`cid` = %d and `glink`.`uid` = %d
323 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d )
324 $sql_extra limit %d, %d",
338 function count_common_friends_zcid($uid,$zcid) {
340 $r = q("SELECT count(*) as `total`
341 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
342 where `glink`.`zcid` = %d
343 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
349 return $r[0]['total'];
354 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
357 $sql_extra = " order by rand() ";
359 $sql_extra = " order by `gcontact`.`name` asc ";
361 $r = q("SELECT `gcontact`.*
362 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
363 where `glink`.`zcid` = %d
364 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
365 $sql_extra limit %d, %d",
377 function count_all_friends($uid,$cid) {
379 $r = q("SELECT count(*) as `total`
380 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
381 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
387 return $r[0]['total'];
393 function all_friends($uid,$cid,$start = 0, $limit = 80) {
395 $r = q("SELECT `gcontact`.*
396 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
397 where `glink`.`cid` = %d and `glink`.`uid` = %d
398 order by `gcontact`.`name` asc LIMIT %d, %d ",
410 function suggestion_query($uid, $start = 0, $limit = 80) {
415 $network = array(NETWORK_DFRN);
417 if (get_config('system','diaspora_enabled'))
418 $network[] = NETWORK_DIASPORA;
420 if (!get_config('system','ostatus_disabled'))
421 $network[] = NETWORK_OSTATUS;
423 $sql_network = implode("', '", $network);
424 //$sql_network = "'".$sql_network."', ''";
425 $sql_network = "'".$sql_network."'";
427 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
428 INNER JOIN glink on glink.gcid = gcontact.id
429 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
430 and not gcontact.name in ( select name from contact where uid = %d )
431 and not gcontact.id in ( select gcid from gcign where uid = %d )
432 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
433 AND `gcontact`.`network` IN (%s)
434 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
444 if(count($r) && count($r) >= ($limit -1))
447 $r2 = q("SELECT gcontact.* from gcontact
448 INNER JOIN glink on glink.gcid = gcontact.id
449 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
450 and not gcontact.name in ( select name from contact where uid = %d )
451 and not gcontact.id in ( select gcid from gcign where uid = %d )
452 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
453 AND `gcontact`.`network` IN (%s)
454 order by rand() limit %d, %d ",
464 foreach ($r2 AS $suggestion)
465 $list[$suggestion["nurl"]] = $suggestion;
467 foreach ($r AS $suggestion)
468 $list[$suggestion["nurl"]] = $suggestion;
473 function update_suggestions() {
479 poco_load(0,0,0,$a->get_baseurl() . '/poco');
481 $done[] = $a->get_baseurl() . '/poco';
483 if(strlen(get_config('system','directory_submit_url'))) {
484 $x = fetch_url('http://dir.friendica.com/pubsites');
486 $j = json_decode($x);
488 foreach($j->entries as $entry) {
489 $url = $entry->url . '/poco';
490 if(! in_array($url,$done))
491 poco_load(0,0,0,$entry->url . '/poco');
497 $r = q("select distinct(poco) as poco from contact where network = '%s'",
503 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
504 if(! in_array($base,$done))
505 poco_load(0,0,0,$base);