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' : '?fields=displayName,urls,photos,updated,network') ;
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';
73 $name = $entry->displayName;
75 if(isset($entry->urls)) {
76 foreach($entry->urls as $url) {
77 if($url->type == 'profile') {
78 $profile_url = $url->value;
81 if($url->type == 'webfinger') {
82 $connect_url = str_replace('acct:' , '', $url->value);
87 if(isset($entry->photos)) {
88 foreach($entry->photos as $photo) {
89 if($photo->type == 'profile') {
90 $profile_photo = $photo->value;
96 if(isset($entry->updated))
97 $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
99 if(isset($entry->network))
100 $network = $entry->network;
102 poco_check($profile_url, $name, $network, $profile_photo, $connect_url, $updated, $cid, $uid, $zcid);
105 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
107 q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
115 function poco_check($profile_url, $name, $network, $profile_photo, $connect_url, $updated, $cid = 0, $uid = 0, $zcid = 0) {
118 if (($profile_url == "") OR ($name == "") OR ($profile_photo == ""))
121 logger("profile-check URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
123 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
124 dbesc(normalise_link($profile_url))
130 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
131 q("update gcontact set `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s', `updated` = '%s'
132 where `nurl` = '%s'",
135 dbesc($profile_photo),
139 dbesc(normalise_link($profile_url))
143 q("insert into `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `updated`)
144 values ('%s', '%s', '%s', '%s', '%s','%s', '%s')",
148 dbesc(normalise_link($profile_url)),
149 dbesc($profile_photo),
153 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
154 dbesc(normalise_link($profile_url))
163 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
170 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
175 dbesc(datetime_convert())
178 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
179 dbesc(datetime_convert()),
187 // For unknown reasons there are sometimes duplicates
188 q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
189 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
190 dbesc(normalise_link($profile_url)),
197 function count_common_friends($uid,$cid) {
199 $r = q("SELECT count(*) as `total`
200 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
201 where `glink`.`cid` = %d and `glink`.`uid` = %d
202 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
209 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
211 return $r[0]['total'];
217 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
220 $sql_extra = " order by rand() ";
222 $sql_extra = " order by `gcontact`.`name` asc ";
224 $r = q("SELECT `gcontact`.*
225 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
226 where `glink`.`cid` = %d and `glink`.`uid` = %d
227 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d )
228 $sql_extra limit %d, %d",
242 function count_common_friends_zcid($uid,$zcid) {
244 $r = q("SELECT count(*) as `total`
245 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
246 where `glink`.`zcid` = %d
247 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
253 return $r[0]['total'];
258 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
261 $sql_extra = " order by rand() ";
263 $sql_extra = " order by `gcontact`.`name` asc ";
265 $r = q("SELECT `gcontact`.*
266 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
267 where `glink`.`zcid` = %d
268 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
269 $sql_extra limit %d, %d",
281 function count_all_friends($uid,$cid) {
283 $r = q("SELECT count(*) as `total`
284 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
285 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
291 return $r[0]['total'];
297 function all_friends($uid,$cid,$start = 0, $limit = 80) {
299 $r = q("SELECT `gcontact`.*
300 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
301 where `glink`.`cid` = %d and `glink`.`uid` = %d
302 order by `gcontact`.`name` asc LIMIT %d, %d ",
314 function suggestion_query($uid, $start = 0, $limit = 80) {
319 $network = array(NETWORK_DFRN);
321 if (get_config('system','diaspora_enabled'))
322 $network[] = NETWORK_DIASPORA;
324 if (!get_config('system','ostatus_disabled'))
325 $network[] = NETWORK_OSTATUS;
327 $sql_network = implode("', '", $network);
328 //$sql_network = "'".$sql_network."', ''";
329 $sql_network = "'".$sql_network."'";
331 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
332 INNER JOIN glink on glink.gcid = gcontact.id
333 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
334 and not gcontact.name in ( select name from contact where uid = %d )
335 and not gcontact.id in ( select gcid from gcign where uid = %d )
336 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
337 AND `gcontact`.`network` IN (%s)
338 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
348 if(count($r) && count($r) >= ($limit -1))
351 $r2 = q("SELECT gcontact.* from gcontact
352 INNER JOIN glink on glink.gcid = gcontact.id
353 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
354 and not gcontact.name in ( select name from contact where uid = %d )
355 and not gcontact.id in ( select gcid from gcign where uid = %d )
356 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
357 AND `gcontact`.`network` IN (%s)
358 order by rand() limit %d, %d ",
368 foreach ($r2 AS $suggestion)
369 $list[$suggestion["nurl"]] = $suggestion;
371 foreach ($r AS $suggestion)
372 $list[$suggestion["nurl"]] = $suggestion;
377 function update_suggestions() {
383 poco_load(0,0,0,$a->get_baseurl() . '/poco');
385 $done[] = $a->get_baseurl() . '/poco';
387 if(strlen(get_config('system','directory_submit_url'))) {
388 $x = fetch_url('http://dir.friendica.com/pubsites');
390 $j = json_decode($x);
392 foreach($j->entries as $entry) {
393 $url = $entry->url . '/poco';
394 if(! in_array($url,$done))
395 poco_load(0,0,0,$entry->url . '/poco');
401 $r = q("select distinct(poco) as poco from contact where network = '%s'",
407 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
408 if(! in_array($base,$done))
409 poco_load(0,0,0,$base);