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