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 logger('poco_load: ' . $url, LOGGER_DATA);
45 $s = fetch_url($url . '/@me/@all?fields=displayName,urls,photos');
47 if(($a->get_curl_code() > 299) || (! $s))
50 foreach($j->entry as $entry) {
56 $name = $entry->displayName;
58 foreach($entry->urls as $url) {
59 if($url->type == 'profile') {
60 $profile_url = $url->value;
64 foreach($entry->photos as $photo) {
65 if($photo->type == 'profile') {
66 $profile_photo = $photo->value;
71 if((! $name) || (! $profile_url) || (! $profile_photo))
74 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
75 dbesc(normalise_link($profile_url))
81 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo) {
82 q("update gcontact set `name` = '%s', `photo` = '%s' where
83 `nurl` = '%s' limit 1",
85 dbesc($profile_photo),
86 dbesc(normalise_link($profile_url))
91 q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`)
92 values ( '%s', '%s', '%s', '%s') ",
95 dbesc(normalise_link($profile_url)),
98 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
99 dbesc(normalise_link($profile_url))
107 $r = q("select * from glink where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
113 q("insert into glink ( `cid`,`uid`,`gcid`,`updated`) values (%d,%d,%d,'%s') ",
117 dbesc(datetime_convert())
121 q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
122 dbesc(datetime_convert()),
131 q("delete from glink where `cid` = %d and `uid` = %d and `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
139 function count_common_friends($uid,$cid) {
141 $r = q("SELECT count(*) as `total`
142 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
143 where `glink`.`cid` = %d and `glink`.`uid` = %d
144 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d ) ",
152 return $r[0]['total'];
158 function common_friends($uid,$cid) {
160 $r = q("SELECT `gcontact`.*
161 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
162 where `glink`.`cid` = %d and `glink`.`uid` = %d
163 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d )
164 order by `gcontact`.`name` asc ",
175 function count_all_friends($uid,$cid) {
177 $r = q("SELECT count(*) as `total`
178 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
179 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
185 return $r[0]['total'];
191 function all_friends($uid,$cid,$start = 0, $limit = 80) {
193 $r = q("SELECT `gcontact`.*
194 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
195 where `glink`.`cid` = %d and `glink`.`uid` = %d
196 order by `gcontact`.`name` asc LIMIT %d, %d ",
208 function suggestion_query($uid, $start = 0, $limit = 40) {
213 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
214 left join glink on glink.gcid = gcontact.id
215 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d)
216 and not gcontact.id in ( select gcid from gcign where uid = %d )
217 group by glink.gcid order by total desc limit %d, %d ",
228 $r = q("SELECT gcontact.* from gcontact
229 left join glink on glink.gcid = gcontact.id
230 where uid = 0 and cid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d)
231 and not gcontact.id in ( select gcid from gcign where uid = %d )
232 order by rand limit %d, %d ",
242 function update_suggestions() {
248 poco_load(0,0,$a->get_baseurl() . '/poco');
250 $done[] = $a->get_baseurl() . '/poco';
252 if(strlen(get_config('system','directory_submit_url'))) {
253 $x = fetch_url('http://dir.friendica.com/pubsites');
255 $j = json_decode($x);
257 foreach($j->entries as $entry) {
258 $url = $entry->url . '/poco';
259 if(! in_array($url,$done))
260 poco_load(0,0,$entry->url . '/poco');
266 $r = q("select distinct(poco) as poco from contact where network = '%s'",
272 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
273 if(! in_array($base,$done))
274 poco_load(0,0,$base);