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