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' : '?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender') ;
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';
80 $name = $entry->displayName;
82 if(isset($entry->urls)) {
83 foreach($entry->urls as $url) {
84 if($url->type == 'profile') {
85 $profile_url = $url->value;
88 if($url->type == 'webfinger') {
89 $connect_url = str_replace('acct:' , '', $url->value);
94 if(isset($entry->photos)) {
95 foreach($entry->photos as $photo) {
96 if($photo->type == 'profile') {
97 $profile_photo = $photo->value;
103 if(isset($entry->updated))
104 $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
106 if(isset($entry->network))
107 $network = $entry->network;
109 if(isset($entry->currentLocation))
110 $location = $entry->currentLocation;
112 if(isset($entry->aboutMe))
113 $about = html2bbcode($entry->aboutMe);
115 if(isset($entry->gender))
116 $gender = $entry->gender;
118 if(isset($entry->tags))
119 foreach($entry->tags as $tag)
120 $keywords = implode(", ", $tag);
122 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $cid, $uid, $zcid);
124 // Update the Friendica contacts. Diaspora is doing it via a message. (See include/diaspora.php)
125 if (($location != "") OR ($about != "") OR ($keywords != "") OR ($gender != ""))
126 q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
127 WHERE `nurl` = '%s' AND NOT `self` AND `network` = '%s'",
132 dbesc(normalise_link($profile_url)),
133 dbesc(NETWORK_DFRN));
135 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
137 q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
145 function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $cid = 0, $uid = 0, $zcid = 0) {
148 if ($profile_url == "")
151 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
152 dbesc(normalise_link($profile_url))
155 $network = $x[0]["network"];
157 if (($network == "") OR ($name == "") OR ($profile_photo == "")) {
158 require_once("include/Scrape.php");
160 $data = probe_url($profile_url, PROBE_DIASPORA);
161 $network = $data["network"];
162 $name = $data["name"];
163 $profile_photo = $data["photo"];
166 if (count($x) AND ($x[0]["network"] == "") AND ($network != "")) {
167 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
169 dbesc(normalise_link($profile_url))
173 if (($name == "") OR ($profile_photo == ""))
176 if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_STATUSNET)))
179 logger("profile-check URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
184 if (($location == "") AND ($x[0]['location'] != ""))
185 $location = $x[0]['location'];
187 if (($about == "") AND ($x[0]['about'] != ""))
188 $about = $x[0]['about'];
190 if (($gender == "") AND ($x[0]['gender'] != ""))
191 $gender = $x[0]['gender'];
193 if (($keywords == "") AND ($x[0]['keywords'] != ""))
194 $keywords = $x[0]['keywords'];
196 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
197 q("update gcontact set `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s',
198 `updated` = '%s', `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
199 where `nurl` = '%s'",
202 dbesc($profile_photo),
210 dbesc(normalise_link($profile_url))
214 q("insert into `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `updated`, `location`, `about`, `keywords`, `gender`)
215 values ('%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s')",
219 dbesc(normalise_link($profile_url)),
220 dbesc($profile_photo),
228 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
229 dbesc(normalise_link($profile_url))
238 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
245 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
250 dbesc(datetime_convert())
253 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
254 dbesc(datetime_convert()),
262 // For unknown reasons there are sometimes duplicates
263 q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
264 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
265 dbesc(normalise_link($profile_url)),
272 function poco_contact_from_body($body, $created, $cid, $uid) {
273 preg_replace_callback("/\[share(.*?)\].*?\[\/share\]/ism",
274 function ($match) use ($created, $cid, $uid){
275 return(sub_poco_from_share($match, $created, $cid, $uid));
279 function sub_poco_from_share($share, $created, $cid, $uid) {
281 preg_match("/profile='(.*?)'/ism", $share[1], $matches);
282 if ($matches[1] != "")
283 $profile = $matches[1];
285 preg_match('/profile="(.*?)"/ism', $share[1], $matches);
286 if ($matches[1] != "")
287 $profile = $matches[1];
292 logger("prepare poco_check for profile ".$profile, LOGGER_DEBUG);
293 poco_check($profile, "", "", "", "", "", "", "", "", $created, $cid, $uid);
296 function count_common_friends($uid,$cid) {
298 $r = q("SELECT count(*) as `total`
299 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
300 where `glink`.`cid` = %d and `glink`.`uid` = %d
301 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
308 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
310 return $r[0]['total'];
316 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
319 $sql_extra = " order by rand() ";
321 $sql_extra = " order by `gcontact`.`name` asc ";
323 $r = q("SELECT `gcontact`.*
324 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
325 where `glink`.`cid` = %d and `glink`.`uid` = %d
326 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d )
327 $sql_extra limit %d, %d",
341 function count_common_friends_zcid($uid,$zcid) {
343 $r = q("SELECT count(*) as `total`
344 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
345 where `glink`.`zcid` = %d
346 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
352 return $r[0]['total'];
357 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
360 $sql_extra = " order by rand() ";
362 $sql_extra = " order by `gcontact`.`name` asc ";
364 $r = q("SELECT `gcontact`.*
365 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
366 where `glink`.`zcid` = %d
367 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
368 $sql_extra limit %d, %d",
380 function count_all_friends($uid,$cid) {
382 $r = q("SELECT count(*) as `total`
383 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
384 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
390 return $r[0]['total'];
396 function all_friends($uid,$cid,$start = 0, $limit = 80) {
398 $r = q("SELECT `gcontact`.*
399 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
400 where `glink`.`cid` = %d and `glink`.`uid` = %d
401 order by `gcontact`.`name` asc LIMIT %d, %d ",
413 function suggestion_query($uid, $start = 0, $limit = 80) {
418 $network = array(NETWORK_DFRN);
420 if (get_config('system','diaspora_enabled'))
421 $network[] = NETWORK_DIASPORA;
423 if (!get_config('system','ostatus_disabled'))
424 $network[] = NETWORK_OSTATUS;
426 $sql_network = implode("', '", $network);
427 //$sql_network = "'".$sql_network."', ''";
428 $sql_network = "'".$sql_network."'";
430 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
431 INNER JOIN glink on glink.gcid = gcontact.id
432 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
433 and not gcontact.name in ( select name from contact where uid = %d )
434 and not gcontact.id in ( select gcid from gcign where uid = %d )
435 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
436 AND `gcontact`.`network` IN (%s)
437 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
447 if(count($r) && count($r) >= ($limit -1))
450 $r2 = q("SELECT gcontact.* from gcontact
451 INNER JOIN glink on glink.gcid = gcontact.id
452 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
453 and not gcontact.name in ( select name from contact where uid = %d )
454 and not gcontact.id in ( select gcid from gcign where uid = %d )
455 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
456 AND `gcontact`.`network` IN (%s)
457 order by rand() limit %d, %d ",
467 foreach ($r2 AS $suggestion)
468 $list[$suggestion["nurl"]] = $suggestion;
470 foreach ($r AS $suggestion)
471 $list[$suggestion["nurl"]] = $suggestion;
476 function update_suggestions() {
482 poco_load(0,0,0,$a->get_baseurl() . '/poco');
484 $done[] = $a->get_baseurl() . '/poco';
486 if(strlen(get_config('system','directory_submit_url'))) {
487 $x = fetch_url('http://dir.friendica.com/pubsites');
489 $j = json_decode($x);
491 foreach($j->entries as $entry) {
492 $url = $entry->url . '/poco';
493 if(! in_array($url,$done))
494 poco_load(0,0,0,$entry->url . '/poco');
500 $r = q("select distinct(poco) as poco from contact where network = '%s'",
506 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
507 if(! in_array($base,$done))
508 poco_load(0,0,0,$base);