]> git.mxchange.org Git - friendica.git/blob - include/socgraph.php
a3e20a64143e78ebd9688064ba7f08a1491a42c3
[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' 
103                                         where `nurl` = '%s' limit 1",
104                                         dbesc($name),
105                                         dbesc($profile_photo),
106                                         dbesc($connect_url),
107                                         dbesc(normalise_link($profile_url))
108                                 );
109                         }
110                 }
111                 else {
112                         q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`,`connect`)
113                                 values ( '%s', '%s', '%s', '%s','%s') ",
114                                 dbesc($name),
115                                 dbesc($profile_url),
116                                 dbesc(normalise_link($profile_url)),
117                                 dbesc($profile_photo),
118                                 dbesc($connect_url)
119                         );
120                         $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
121                                 dbesc(normalise_link($profile_url))
122                         );
123                         if(count($x))
124                                 $gcid = $x[0]['id'];
125                 }
126                 if(! $gcid)
127                         return;
128
129                 $r = q("select * from glink where `cid` = %d and `uid` = %d and `gcid` = %d and `zcid` = %d limit 1",
130                         intval($cid),
131                         intval($uid),
132                         intval($gcid),
133                         intval($zcid)
134                 );
135                 if(! count($r)) {
136                         q("insert into glink ( `cid`,`uid`,`gcid`,`zcid`, `updated`) values (%d,%d,%d,%d, '%s') ",
137                                 intval($cid),
138                                 intval($uid),
139                                 intval($gcid),
140                                 intval($zcid),
141                                 dbesc(datetime_convert())
142                         );
143                 }
144                 else {
145                         q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d and zcid = %d limit 1",
146                                 dbesc(datetime_convert()),
147                                 intval($cid),
148                                 intval($uid),
149                                 intval($gcid),
150                                 intval($zcid)
151                         );
152                 }
153
154         }
155         logger("poco_load: loaded $total entries",LOGGER_DEBUG);
156
157         q("delete from glink where `cid` = %d and `uid` = %d and `zcid` = %d and `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
158                 intval($cid),
159                 intval($uid),
160                 intval($zcid)
161         );
162
163 }
164
165
166 function count_common_friends($uid,$cid) {
167
168         $r = q("SELECT count(*) as `total`
169                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
170                 where `glink`.`cid` = %d and `glink`.`uid` = %d
171                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d ) ",
172                 intval($cid),
173                 intval($uid),
174                 intval($uid),
175                 intval($cid)
176         );
177
178         if(count($r))
179                 return $r[0]['total'];
180         return 0;
181
182 }
183
184
185 function common_friends($uid,$cid) {
186
187         $r = q("SELECT `gcontact`.* 
188                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
189                 where `glink`.`cid` = %d and `glink`.`uid` = %d
190                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d ) 
191                 order by `gcontact`.`name` asc ",
192                 intval($cid),
193                 intval($uid),
194                 intval($uid),
195                 intval($cid)
196         );
197
198         return $r;
199
200 }
201
202
203 function count_common_friends_zcid($uid,$zcid) {
204
205         $r = q("SELECT count(*) as `total` 
206                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
207                 where `glink`.`zcid` = %d
208                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 ) ",
209                 intval($zcid),
210                 intval($uid),
211         );
212
213         if(count($r))
214                 return $r[0]['total'];
215         return 0;
216
217 }
218
219 function common_friends_zcid($uid,$zcid,$limit = 6) {
220
221         $r = q("SELECT `gcontact`.* 
222                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
223                 where `glink`.`zcid` = %d
224                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 ) 
225                 order by `gcontact`.`name` asc limit 0, %d",
226                 intval($zcid),
227                 intval($uid),
228                 intval($limit)
229         );
230
231         return $r;
232
233 }
234
235
236 function count_all_friends($uid,$cid) {
237
238         $r = q("SELECT count(*) as `total`
239                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
240                 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
241                 intval($cid),
242                 intval($uid)
243         );
244
245         if(count($r))
246                 return $r[0]['total'];
247         return 0;
248
249 }
250
251
252 function all_friends($uid,$cid,$start = 0, $limit = 80) {
253
254         $r = q("SELECT `gcontact`.* 
255                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
256                 where `glink`.`cid` = %d and `glink`.`uid` = %d 
257                 order by `gcontact`.`name` asc LIMIT %d, %d ",
258                 intval($cid),
259                 intval($uid),
260                 intval($start),
261                 intval($limit)
262         );
263
264         return $r;
265 }
266
267
268
269 function suggestion_query($uid, $start = 0, $limit = 80) {
270
271         if(! $uid)
272                 return array();
273
274         $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact 
275                 left join glink on glink.gcid = gcontact.id 
276                 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
277                 and not gcontact.name in ( select name from contact where uid = %d )
278                 and not gcontact.id in ( select gcid from gcign where uid = %d )
279                 group by glink.gcid order by total desc limit %d, %d ",
280                 intval($uid),
281                 intval($uid),
282                 intval($uid),
283                 intval($uid),
284                 intval($start),
285                 intval($limit)
286         );
287
288         if(count($r) && count($r) >= ($limit -1))
289                 return $r;
290
291         $r2 = q("SELECT gcontact.* from gcontact 
292                 left join glink on glink.gcid = gcontact.id 
293                 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
294                 and not gcontact.name in ( select name from contact where uid = %d )
295                 and not gcontact.id in ( select gcid from gcign where uid = %d )
296                 order by rand() limit %d, %d ",
297                 intval($uid),
298                 intval($uid),
299                 intval($uid),
300                 intval($start),
301                 intval($limit)
302         );
303
304
305         return array_merge($r,$r2);
306
307 }
308
309 function update_suggestions() {
310
311         $a = get_app();
312
313         $done = array();
314
315         poco_load(0,0,0,$a->get_baseurl() . '/poco');
316
317         $done[] = $a->get_baseurl() . '/poco';
318
319         if(strlen(get_config('system','directory_submit_url'))) {
320                 $x = fetch_url('http://dir.friendica.com/pubsites');
321                 if($x) {
322                         $j = json_decode($x);
323                         if($j->entries) {
324                                 foreach($j->entries as $entry) {
325                                         $url = $entry->url . '/poco';
326                                         if(! in_array($url,$done))
327                                                 poco_load(0,0,0,$entry->url . '/poco');
328                                 }
329                         }
330                 }
331         }
332
333         $r = q("select distinct(poco) as poco from contact where network = '%s'",
334                 dbesc(NETWORK_DFRN)
335         );
336
337         if(count($r)) {
338                 foreach($r as $rr) {
339                         $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
340                         if(! in_array($base,$done))
341                                 poco_load(0,0,0,$base);
342                 }
343         }
344 }