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' : '?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation') ;
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';
75 $name = $entry->displayName;
77 if(isset($entry->urls)) {
78 foreach($entry->urls as $url) {
79 if($url->type == 'profile') {
80 $profile_url = $url->value;
83 if($url->type == 'webfinger') {
84 $connect_url = str_replace('acct:' , '', $url->value);
89 if(isset($entry->photos)) {
90 foreach($entry->photos as $photo) {
91 if($photo->type == 'profile') {
92 $profile_photo = $photo->value;
98 if(isset($entry->updated))
99 $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
101 if(isset($entry->network))
102 $network = $entry->network;
104 if(isset($entry->currentLocation))
105 $location = $entry->currentLocation;
107 if(isset($entry->aboutMe))
108 $about = $entry->aboutMe;
110 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $connect_url, $updated, $cid, $uid, $zcid);
112 if (($location != "") OR ($about != ""))
113 q("UPDATE `contact` SET `location` = '%s', `about` = '%s' WHERE `nurl` = '%s' AND NOT `self`",
116 dbesc(normalise_link($profile_url)));
119 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
121 q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
129 function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $connect_url, $updated, $cid = 0, $uid = 0, $zcid = 0) {
132 if ($profile_url == "")
135 if (($network == "") OR ($name == "") OR ($profile_photo == "")) {
136 require_once("include/Scrape.php");
138 $data = probe_url($profile_url, PROBE_DIASPORA);
139 $network = $data["network"];
140 $name = $data["name"];
141 $profile_photo = $data["photo"];
144 if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_STATUSNET)))
147 if (($name == "") OR ($profile_photo == ""))
150 logger("profile-check URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
152 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
153 dbesc(normalise_link($profile_url))
159 if (($location == "") AND ($x[0]['location'] != ""))
160 $location = $x[0]['location'];
162 if (($about == "") AND ($x[0]['about'] != ""))
163 $about = $x[0]['about'];
165 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
166 q("update gcontact set `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s', `updated` = '%s', `location` = '%s', `about` = '%s'
167 where `nurl` = '%s'",
170 dbesc($profile_photo),
176 dbesc(normalise_link($profile_url))
180 q("insert into `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `updated`, `location`, `about`)
181 values ('%s', '%s', '%s', '%s', '%s','%s', '%s')",
185 dbesc(normalise_link($profile_url)),
186 dbesc($profile_photo),
192 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
193 dbesc(normalise_link($profile_url))
202 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
209 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
214 dbesc(datetime_convert())
217 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
218 dbesc(datetime_convert()),
226 // For unknown reasons there are sometimes duplicates
227 q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
228 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
229 dbesc(normalise_link($profile_url)),
236 function poco_contact_from_body($body, $created, $cid, $uid) {
237 preg_replace_callback("/\[share(.*?)\].*?\[\/share\]/ism",
238 function ($match) use ($created, $cid, $uid){
239 return(sub_poco_from_share($match, $created, $cid, $uid));
243 function sub_poco_from_share($share, $created, $cid, $uid) {
245 preg_match("/profile='(.*?)'/ism", $share[1], $matches);
246 if ($matches[1] != "")
247 $profile = $matches[1];
249 preg_match('/profile="(.*?)"/ism', $share[1], $matches);
250 if ($matches[1] != "")
251 $profile = $matches[1];
256 logger("prepare poco_check for profile ".$profile, LOGGER_DEBUG);
257 poco_check($profile, "", "", "", "", "", "", $created, $cid, $uid);
260 function count_common_friends($uid,$cid) {
262 $r = q("SELECT count(*) as `total`
263 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
264 where `glink`.`cid` = %d and `glink`.`uid` = %d
265 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
272 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
274 return $r[0]['total'];
280 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
283 $sql_extra = " order by rand() ";
285 $sql_extra = " order by `gcontact`.`name` asc ";
287 $r = q("SELECT `gcontact`.*
288 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
289 where `glink`.`cid` = %d and `glink`.`uid` = %d
290 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d )
291 $sql_extra limit %d, %d",
305 function count_common_friends_zcid($uid,$zcid) {
307 $r = q("SELECT count(*) as `total`
308 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
309 where `glink`.`zcid` = %d
310 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
316 return $r[0]['total'];
321 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
324 $sql_extra = " order by rand() ";
326 $sql_extra = " order by `gcontact`.`name` asc ";
328 $r = q("SELECT `gcontact`.*
329 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
330 where `glink`.`zcid` = %d
331 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
332 $sql_extra limit %d, %d",
344 function count_all_friends($uid,$cid) {
346 $r = q("SELECT count(*) as `total`
347 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
348 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
354 return $r[0]['total'];
360 function all_friends($uid,$cid,$start = 0, $limit = 80) {
362 $r = q("SELECT `gcontact`.*
363 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
364 where `glink`.`cid` = %d and `glink`.`uid` = %d
365 order by `gcontact`.`name` asc LIMIT %d, %d ",
377 function suggestion_query($uid, $start = 0, $limit = 80) {
382 $network = array(NETWORK_DFRN);
384 if (get_config('system','diaspora_enabled'))
385 $network[] = NETWORK_DIASPORA;
387 if (!get_config('system','ostatus_disabled'))
388 $network[] = NETWORK_OSTATUS;
390 $sql_network = implode("', '", $network);
391 //$sql_network = "'".$sql_network."', ''";
392 $sql_network = "'".$sql_network."'";
394 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
395 INNER JOIN glink on glink.gcid = gcontact.id
396 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
397 and not gcontact.name in ( select name from contact where uid = %d )
398 and not gcontact.id in ( select gcid from gcign where uid = %d )
399 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
400 AND `gcontact`.`network` IN (%s)
401 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
411 if(count($r) && count($r) >= ($limit -1))
414 $r2 = q("SELECT gcontact.* from gcontact
415 INNER JOIN glink on glink.gcid = gcontact.id
416 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
417 and not gcontact.name in ( select name from contact where uid = %d )
418 and not gcontact.id in ( select gcid from gcign where uid = %d )
419 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
420 AND `gcontact`.`network` IN (%s)
421 order by rand() limit %d, %d ",
431 foreach ($r2 AS $suggestion)
432 $list[$suggestion["nurl"]] = $suggestion;
434 foreach ($r AS $suggestion)
435 $list[$suggestion["nurl"]] = $suggestion;
440 function update_suggestions() {
446 poco_load(0,0,0,$a->get_baseurl() . '/poco');
448 $done[] = $a->get_baseurl() . '/poco';
450 if(strlen(get_config('system','directory_submit_url'))) {
451 $x = fetch_url('http://dir.friendica.com/pubsites');
453 $j = json_decode($x);
455 foreach($j->entries as $entry) {
456 $url = $entry->url . '/poco';
457 if(! in_array($url,$done))
458 poco_load(0,0,0,$entry->url . '/poco');
464 $r = q("select distinct(poco) as poco from contact where network = '%s'",
470 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
471 if(! in_array($base,$done))
472 poco_load(0,0,0,$base);