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