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 $keywords = implode(", ", $entry->tags);
118 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $cid, $uid, $zcid);
120 // Update the Friendica contacts. Diaspora is doing it via a message. (See include/diaspora.php)
121 if (($location != "") OR ($about != "") OR ($keywords != "") OR ($gender != ""))
122 q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
123 WHERE `nurl` = '%s' AND NOT `self` AND `network` = '%s'",
128 dbesc(normalise_link($profile_url)),
129 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 if (($network == "") OR ($name == "") OR ($profile_photo == "")) {
149 require_once("include/Scrape.php");
151 $data = probe_url($profile_url, PROBE_DIASPORA);
152 $network = $data["network"];
153 $name = $data["name"];
154 $profile_photo = $data["photo"];
157 if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_STATUSNET)))
160 if (($name == "") OR ($profile_photo == ""))
163 logger("profile-check URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
165 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
166 dbesc(normalise_link($profile_url))
172 if (($location == "") AND ($x[0]['location'] != ""))
173 $location = $x[0]['location'];
175 if (($about == "") AND ($x[0]['about'] != ""))
176 $about = $x[0]['about'];
178 if (($gender == "") AND ($x[0]['gender'] != ""))
179 $gender = $x[0]['gender'];
181 if (($keywords == "") AND ($x[0]['keywords'] != ""))
182 $keywords = $x[0]['keywords'];
184 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
185 q("update gcontact set `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s',
186 `updated` = '%s', `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
187 where `nurl` = '%s'",
190 dbesc($profile_photo),
198 dbesc(normalise_link($profile_url))
202 q("insert into `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `updated`, `location`, `about`, `keywords`, `gender`)
203 values ('%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s')",
207 dbesc(normalise_link($profile_url)),
208 dbesc($profile_photo),
216 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
217 dbesc(normalise_link($profile_url))
226 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
233 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
238 dbesc(datetime_convert())
241 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
242 dbesc(datetime_convert()),
250 // For unknown reasons there are sometimes duplicates
251 q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
252 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
253 dbesc(normalise_link($profile_url)),
260 function poco_contact_from_body($body, $created, $cid, $uid) {
261 preg_replace_callback("/\[share(.*?)\].*?\[\/share\]/ism",
262 function ($match) use ($created, $cid, $uid){
263 return(sub_poco_from_share($match, $created, $cid, $uid));
267 function sub_poco_from_share($share, $created, $cid, $uid) {
269 preg_match("/profile='(.*?)'/ism", $share[1], $matches);
270 if ($matches[1] != "")
271 $profile = $matches[1];
273 preg_match('/profile="(.*?)"/ism', $share[1], $matches);
274 if ($matches[1] != "")
275 $profile = $matches[1];
280 logger("prepare poco_check for profile ".$profile, LOGGER_DEBUG);
281 poco_check($profile, "", "", "", "", "", "", "", "", $created, $cid, $uid);
284 function count_common_friends($uid,$cid) {
286 $r = q("SELECT count(*) as `total`
287 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
288 where `glink`.`cid` = %d and `glink`.`uid` = %d
289 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
296 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
298 return $r[0]['total'];
304 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
307 $sql_extra = " order by rand() ";
309 $sql_extra = " order by `gcontact`.`name` asc ";
311 $r = q("SELECT `gcontact`.*
312 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
313 where `glink`.`cid` = %d and `glink`.`uid` = %d
314 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d )
315 $sql_extra limit %d, %d",
329 function count_common_friends_zcid($uid,$zcid) {
331 $r = q("SELECT count(*) as `total`
332 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
333 where `glink`.`zcid` = %d
334 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
340 return $r[0]['total'];
345 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
348 $sql_extra = " order by rand() ";
350 $sql_extra = " order by `gcontact`.`name` asc ";
352 $r = q("SELECT `gcontact`.*
353 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
354 where `glink`.`zcid` = %d
355 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
356 $sql_extra limit %d, %d",
368 function count_all_friends($uid,$cid) {
370 $r = q("SELECT count(*) as `total`
371 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
372 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
378 return $r[0]['total'];
384 function all_friends($uid,$cid,$start = 0, $limit = 80) {
386 $r = q("SELECT `gcontact`.*
387 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
388 where `glink`.`cid` = %d and `glink`.`uid` = %d
389 order by `gcontact`.`name` asc LIMIT %d, %d ",
401 function suggestion_query($uid, $start = 0, $limit = 80) {
406 $network = array(NETWORK_DFRN);
408 if (get_config('system','diaspora_enabled'))
409 $network[] = NETWORK_DIASPORA;
411 if (!get_config('system','ostatus_disabled'))
412 $network[] = NETWORK_OSTATUS;
414 $sql_network = implode("', '", $network);
415 //$sql_network = "'".$sql_network."', ''";
416 $sql_network = "'".$sql_network."'";
418 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
419 INNER JOIN glink on glink.gcid = gcontact.id
420 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
421 and not gcontact.name in ( select name from contact where uid = %d )
422 and not gcontact.id in ( select gcid from gcign where uid = %d )
423 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
424 AND `gcontact`.`network` IN (%s)
425 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
435 if(count($r) && count($r) >= ($limit -1))
438 $r2 = q("SELECT gcontact.* from gcontact
439 INNER JOIN glink on glink.gcid = gcontact.id
440 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
441 and not gcontact.name in ( select name from contact where uid = %d )
442 and not gcontact.id in ( select gcid from gcign where uid = %d )
443 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
444 AND `gcontact`.`network` IN (%s)
445 order by rand() limit %d, %d ",
455 foreach ($r2 AS $suggestion)
456 $list[$suggestion["nurl"]] = $suggestion;
458 foreach ($r AS $suggestion)
459 $list[$suggestion["nurl"]] = $suggestion;
464 function update_suggestions() {
470 poco_load(0,0,0,$a->get_baseurl() . '/poco');
472 $done[] = $a->get_baseurl() . '/poco';
474 if(strlen(get_config('system','directory_submit_url'))) {
475 $x = fetch_url('http://dir.friendica.com/pubsites');
477 $j = json_decode($x);
479 foreach($j->entries as $entry) {
480 $url = $entry->url . '/poco';
481 if(! in_array($url,$done))
482 poco_load(0,0,0,$entry->url . '/poco');
488 $r = q("select distinct(poco) as poco from contact where network = '%s'",
494 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
495 if(! in_array($base,$done))
496 poco_load(0,0,0,$base);