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