]> git.mxchange.org Git - friendica.git/blob - include/socgraph.php
Merge branch 'master' of github.com:annando/friendica
[friendica.git] / include / socgraph.php
1 <?php
2
3 require_once('include/datetime.php');
4
5
6 /*
7  * poco_load
8  *
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
13  *
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. 
17  *
18  */
19  
20
21
22
23 function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
24         $a = get_app();
25
26         if($cid) {
27                 if((! $url) || (! $uid)) {
28                         $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
29                                 intval($cid)
30                         );
31                         if(count($r)) {
32                                 $url = $r[0]['poco'];
33                                 $uid = $r[0]['uid'];
34                         }
35                 }
36                 if(! $uid)
37                         return;
38         }
39
40         if(! $url)
41                 return;
42
43         $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos' : '?fields=displayName,urls,photos') ;
44
45         logger('poco_load: ' . $url, LOGGER_DEBUG);
46
47         $s = fetch_url($url);
48
49         logger('poco_load: returns ' . $s, LOGGER_DATA);
50
51         logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
52
53         if(($a->get_curl_code() > 299) || (! $s))
54                 return;
55
56         $j = json_decode($s);
57
58         logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
59
60         if(! isset($j->entry))
61                 return;
62
63         $total = 0;
64         foreach($j->entry as $entry) {
65
66                 $total ++;
67                 $profile_url = '';
68                 $profile_photo = '';
69                 $connect_url = '';
70                 $name = '';
71
72                 $name = $entry->displayName;
73
74                 foreach($entry->urls as $url) {
75                         if($url->type == 'profile') {
76                                 $profile_url = $url->value;
77                                 continue;
78                         }
79                         if($url->type == 'webfinger') {
80                                 $connect_url = str_replace('acct:' , '', $url->value);
81                                 continue;
82                         }
83                 } 
84                 foreach($entry->photos as $photo) {
85                         if($photo->type == 'profile') {
86                                 $profile_photo = $photo->value;
87                                 continue;
88                         }
89                 }
90
91                 if((! $name) || (! $profile_url) || (! $profile_photo))
92                         continue; 
93                  
94                 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
95                         dbesc(normalise_link($profile_url))
96                 );
97
98                 if(count($x)) {
99                         $gcid = $x[0]['id'];
100
101                         if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo) {
102                                 q("update gcontact set `name` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s' 
103                                         where `nurl` = '%s' limit 1",
104                                         dbesc($name),
105                                         dbesc($profile_photo),
106                                         dbesc($connect_url),
107                                         dbesc($profile_url),
108                                         dbesc(normalise_link($profile_url))
109                                 );
110                         }
111                 }
112                 else {
113                         q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`,`connect`)
114                                 values ( '%s', '%s', '%s', '%s','%s') ",
115                                 dbesc($name),
116                                 dbesc($profile_url),
117                                 dbesc(normalise_link($profile_url)),
118                                 dbesc($profile_photo),
119                                 dbesc($connect_url)
120                         );
121                         $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
122                                 dbesc(normalise_link($profile_url))
123                         );
124                         if(count($x))
125                                 $gcid = $x[0]['id'];
126                 }
127                 if(! $gcid)
128                         return;
129
130                 $r = q("select * from glink where `cid` = %d and `uid` = %d and `gcid` = %d and `zcid` = %d limit 1",
131                         intval($cid),
132                         intval($uid),
133                         intval($gcid),
134                         intval($zcid)
135                 );
136                 if(! count($r)) {
137                         q("insert into glink ( `cid`,`uid`,`gcid`,`zcid`, `updated`) values (%d,%d,%d,%d, '%s') ",
138                                 intval($cid),
139                                 intval($uid),
140                                 intval($gcid),
141                                 intval($zcid),
142                                 dbesc(datetime_convert())
143                         );
144                 }
145                 else {
146                         q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d and zcid = %d limit 1",
147                                 dbesc(datetime_convert()),
148                                 intval($cid),
149                                 intval($uid),
150                                 intval($gcid),
151                                 intval($zcid)
152                         );
153                 }
154
155         }
156         logger("poco_load: loaded $total entries",LOGGER_DEBUG);
157
158         q("delete from glink where `cid` = %d and `uid` = %d and `zcid` = %d and `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
159                 intval($cid),
160                 intval($uid),
161                 intval($zcid)
162         );
163
164 }
165
166
167 function count_common_friends($uid,$cid) {
168
169         $r = q("SELECT count(*) as `total`
170                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
171                 where `glink`.`cid` = %d and `glink`.`uid` = %d
172                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
173                 intval($cid),
174                 intval($uid),
175                 intval($uid),
176                 intval($cid)
177         );
178
179 //      logger("count_common_friends: $uid $cid {$r[0]['total']}"); 
180         if(count($r))
181                 return $r[0]['total'];
182         return 0;
183
184 }
185
186
187 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
188
189         if($shuffle)
190                 $sql_extra = " order by rand() ";
191         else
192                 $sql_extra = " order by `gcontact`.`name` asc "; 
193
194         $r = q("SELECT `gcontact`.* 
195                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
196                 where `glink`.`cid` = %d and `glink`.`uid` = %d
197                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) 
198                 $sql_extra limit %d, %d",
199                 intval($cid),
200                 intval($uid),
201                 intval($uid),
202                 intval($cid),
203                 intval($start),
204                 intval($limit)
205         );
206
207         return $r;
208
209 }
210
211
212 function count_common_friends_zcid($uid,$zcid) {
213
214         $r = q("SELECT count(*) as `total` 
215                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
216                 where `glink`.`zcid` = %d
217                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
218                 intval($zcid),
219                 intval($uid)
220         );
221
222         if(count($r))
223                 return $r[0]['total'];
224         return 0;
225
226 }
227
228 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle) {
229
230         if($shuffle)
231                 $sql_extra = " order by rand() ";
232         else
233                 $sql_extra = " order by `gcontact`.`name` asc "; 
234
235         $r = q("SELECT `gcontact`.* 
236                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
237                 where `glink`.`zcid` = %d
238                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) 
239                 $sql_extra limit %d, %d",
240                 intval($zcid),
241                 intval($uid),
242                 intval($start),
243                 intval($limit)
244         );
245
246         return $r;
247
248 }
249
250
251 function count_all_friends($uid,$cid) {
252
253         $r = q("SELECT count(*) as `total`
254                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
255                 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
256                 intval($cid),
257                 intval($uid)
258         );
259
260         if(count($r))
261                 return $r[0]['total'];
262         return 0;
263
264 }
265
266
267 function all_friends($uid,$cid,$start = 0, $limit = 80) {
268
269         $r = q("SELECT `gcontact`.* 
270                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
271                 where `glink`.`cid` = %d and `glink`.`uid` = %d 
272                 order by `gcontact`.`name` asc LIMIT %d, %d ",
273                 intval($cid),
274                 intval($uid),
275                 intval($start),
276                 intval($limit)
277         );
278
279         return $r;
280 }
281
282
283
284 function suggestion_query($uid, $start = 0, $limit = 80) {
285
286         if(! $uid)
287                 return array();
288
289         $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact 
290                 left join glink on glink.gcid = gcontact.id 
291                 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
292                 and not gcontact.name in ( select name from contact where uid = %d )
293                 and not gcontact.id in ( select gcid from gcign where uid = %d )
294                 group by glink.gcid order by total desc limit %d, %d ",
295                 intval($uid),
296                 intval($uid),
297                 intval($uid),
298                 intval($uid),
299                 intval($start),
300                 intval($limit)
301         );
302
303         if(count($r) && count($r) >= ($limit -1))
304                 return $r;
305
306         $r2 = q("SELECT gcontact.* from gcontact 
307                 left join glink on glink.gcid = gcontact.id 
308                 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
309                 and not gcontact.name in ( select name from contact where uid = %d )
310                 and not gcontact.id in ( select gcid from gcign where uid = %d )
311                 order by rand() limit %d, %d ",
312                 intval($uid),
313                 intval($uid),
314                 intval($uid),
315                 intval($start),
316                 intval($limit)
317         );
318
319
320         return array_merge($r,$r2);
321
322 }
323
324 function update_suggestions() {
325
326         $a = get_app();
327
328         $done = array();
329
330         poco_load(0,0,0,$a->get_baseurl() . '/poco');
331
332         $done[] = $a->get_baseurl() . '/poco';
333
334         if(strlen(get_config('system','directory_submit_url'))) {
335                 $x = fetch_url('http://dir.friendica.com/pubsites');
336                 if($x) {
337                         $j = json_decode($x);
338                         if($j->entries) {
339                                 foreach($j->entries as $entry) {
340                                         $url = $entry->url . '/poco';
341                                         if(! in_array($url,$done))
342                                                 poco_load(0,0,0,$entry->url . '/poco');
343                                 }
344                         }
345                 }
346         }
347
348         $r = q("select distinct(poco) as poco from contact where network = '%s'",
349                 dbesc(NETWORK_DFRN)
350         );
351
352         if(count($r)) {
353                 foreach($r as $rr) {
354                         $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
355                         if(! in_array($base,$done))
356                                 poco_load(0,0,0,$base);
357                 }
358         }
359 }