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