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