3 require_once('include/datetime.php');
9 * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
10 * and add the entries to the gcontact (Global Contact) table, or update existing entries
11 * if anything (name or photo) has changed.
12 * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
14 * Once the global contact is stored add (if necessary) the contact linkage which associates
15 * the given uid, cid to the global contact entry. There can be many uid/cid combinations
16 * pointing to the same global contact id.
23 function poco_load($cid,$uid = 0,$url = null) {
27 if((! $url) || (! $uid)) {
28 $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
43 $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos' : '?fields=displayName,urls,photos') ;
45 logger('poco_load: ' . $url, LOGGER_DEBUG);
49 logger('poco_load: returns ' . $s, LOGGER_DATA);
51 logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
53 if(($a->get_curl_code() > 299) || (! $s))
59 logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
61 if(! isset($j->entry))
65 foreach($j->entry as $entry) {
73 $name = $entry->displayName;
75 foreach($entry->urls as $url) {
76 if($url->type == 'profile') {
77 $profile_url = $url->value;
80 if($url->type == 'webfinger') {
81 $connect_url = str_replace('acct:' , '', $url->value);
86 foreach($entry->photos as $photo) {
87 if($photo->type == 'profile') {
88 $profile_photo = $photo->value;
93 if((! $name) || (! $profile_url) || (! $profile_photo))
96 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
97 dbesc(normalise_link($profile_url))
103 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo) {
104 q("update gcontact set `name` = '%s', `photo` = '%s', `connect` = '%s'
105 where `nurl` = '%s' limit 1",
107 dbesc($profile_photo),
109 dbesc(normalise_link($profile_url))
114 q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`,`connect`)
115 values ( '%s', '%s', '%s', '%s','%s') ",
118 dbesc(normalise_link($profile_url)),
119 dbesc($profile_photo),
122 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
123 dbesc(normalise_link($profile_url))
131 $r = q("select * from glink where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
137 q("insert into glink ( `cid`,`uid`,`gcid`,`updated`) values (%d,%d,%d,'%s') ",
141 dbesc(datetime_convert())
145 q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
146 dbesc(datetime_convert()),
154 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
156 q("delete from glink where `cid` = %d and `uid` = %d and `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
164 function count_common_friends($uid,$cid) {
166 $r = q("SELECT count(*) as `total`
167 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
168 where `glink`.`cid` = %d and `glink`.`uid` = %d
169 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d ) ",
177 return $r[0]['total'];
183 function common_friends($uid,$cid) {
185 $r = q("SELECT `gcontact`.*
186 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
187 where `glink`.`cid` = %d and `glink`.`uid` = %d
188 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d )
189 order by `gcontact`.`name` asc ",
200 function count_all_friends($uid,$cid) {
202 $r = q("SELECT count(*) as `total`
203 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
204 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
210 return $r[0]['total'];
216 function all_friends($uid,$cid,$start = 0, $limit = 80) {
218 $r = q("SELECT `gcontact`.*
219 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
220 where `glink`.`cid` = %d and `glink`.`uid` = %d
221 order by `gcontact`.`name` asc LIMIT %d, %d ",
233 function suggestion_query($uid, $start = 0, $limit = 80) {
238 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
239 left join glink on glink.gcid = gcontact.id
240 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
241 and not gcontact.name in ( select name from contact where uid = %d )
242 and not gcontact.id in ( select gcid from gcign where uid = %d )
243 group by glink.gcid order by total desc limit %d, %d ",
252 if(count($r) && count($r) >= ($limit -1))
255 $r2 = q("SELECT gcontact.* from gcontact
256 left join glink on glink.gcid = gcontact.id
257 where glink.uid = 0 and glink.cid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
258 and not gcontact.name in ( select name from contact where uid = %d )
259 and not gcontact.id in ( select gcid from gcign where uid = %d )
260 order by rand() limit %d, %d ",
269 return array_merge($r,$r2);
273 function update_suggestions() {
279 poco_load(0,0,$a->get_baseurl() . '/poco');
281 $done[] = $a->get_baseurl() . '/poco';
283 if(strlen(get_config('system','directory_submit_url'))) {
284 $x = fetch_url('http://dir.friendica.com/pubsites');
286 $j = json_decode($x);
288 foreach($j->entries as $entry) {
289 $url = $entry->url . '/poco';
290 if(! in_array($url,$done))
291 poco_load(0,0,$entry->url . '/poco');
297 $r = q("select distinct(poco) as poco from contact where network = '%s'",
303 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
304 if(! in_array($base,$done))
305 poco_load(0,0,$base);