]> git.mxchange.org Git - friendica.git/blob - include/socgraph.php
"about" and "location" is now included in global contacts as well.
[friendica.git] / include / socgraph.php
1 <?php
2
3 require_once('include/datetime.php');
4
5 /*
6  * poco_load
7  *
8  * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
9  * and add the entries to the gcontact (Global Contact) table, or update existing entries
10  * if anything (name or photo) has changed.
11  * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
12  *
13  * Once the global contact is stored add (if necessary) the contact linkage which associates
14  * the given uid, cid to the global contact entry. There can be many uid/cid combinations
15  * pointing to the same global contact id.
16  *
17  */
18
19
20
21
22 function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
23         $a = get_app();
24
25         if($cid) {
26                 if((! $url) || (! $uid)) {
27                         $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
28                                 intval($cid)
29                         );
30                         if(count($r)) {
31                                 $url = $r[0]['poco'];
32                                 $uid = $r[0]['uid'];
33                         }
34                 }
35                 if(! $uid)
36                         return;
37         }
38
39         if(! $url)
40                 return;
41
42         $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation' : '?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation') ;
43
44         logger('poco_load: ' . $url, LOGGER_DEBUG);
45
46         $s = fetch_url($url);
47
48         logger('poco_load: returns ' . $s, LOGGER_DATA);
49
50         logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
51
52         if(($a->get_curl_code() > 299) || (! $s))
53                 return;
54
55         $j = json_decode($s);
56
57         logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
58
59         if(! isset($j->entry))
60                 return;
61
62         $total = 0;
63         foreach($j->entry as $entry) {
64
65                 $total ++;
66                 $profile_url = '';
67                 $profile_photo = '';
68                 $connect_url = '';
69                 $name = '';
70                 $network = '';
71                 $updated = '0000-00-00 00:00:00';
72                 $location = '';
73                 $about = '';
74
75                 $name = $entry->displayName;
76
77                 if(isset($entry->urls)) {
78                         foreach($entry->urls as $url) {
79                                 if($url->type == 'profile') {
80                                         $profile_url = $url->value;
81                                         continue;
82                                 }
83                                 if($url->type == 'webfinger') {
84                                         $connect_url = str_replace('acct:' , '', $url->value);
85                                         continue;
86                                 }
87                         }
88                 }
89                 if(isset($entry->photos)) {
90                         foreach($entry->photos as $photo) {
91                                 if($photo->type == 'profile') {
92                                         $profile_photo = $photo->value;
93                                         continue;
94                                 }
95                         }
96                 }
97
98                 if(isset($entry->updated))
99                         $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
100
101                 if(isset($entry->network))
102                         $network = $entry->network;
103
104                 if(isset($entry->currentLocation))
105                         $location = $entry->currentLocation;
106
107                 if(isset($entry->aboutMe))
108                         $about = $entry->aboutMe;
109
110                 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $connect_url, $updated, $cid, $uid, $zcid);
111
112                 if (($location != "") OR ($about != ""))
113                         q("UPDATE `contact` SET `location` = '%s', `about` = '%s' WHERE `nurl` = '%s' AND NOT `self`",
114                                 dbesc($location),
115                                 dbesc($about),
116                                 dbesc(normalise_link($profile_url)));
117
118         }
119         logger("poco_load: loaded $total entries",LOGGER_DEBUG);
120
121         q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
122                 intval($cid),
123                 intval($uid),
124                 intval($zcid)
125         );
126
127 }
128
129 function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $connect_url, $updated, $cid = 0, $uid = 0, $zcid = 0) {
130         $gcid = "";
131
132         if ($profile_url == "")
133                 return $gcid;
134
135         if (($network == "") OR ($name == "") OR ($profile_photo == "")) {
136                 require_once("include/Scrape.php");
137
138                 $data = probe_url($profile_url, PROBE_DIASPORA);
139                 $network = $data["network"];
140                 $name = $data["name"];
141                 $profile_photo = $data["photo"];
142         }
143
144         if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_STATUSNET)))
145                 return $gcid;
146
147         if (($name == "") OR ($profile_photo == ""))
148                 return $gcid;
149
150         logger("profile-check URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
151
152         $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
153                 dbesc(normalise_link($profile_url))
154         );
155
156         if(count($x)) {
157                 $gcid = $x[0]['id'];
158
159                 if (($location == "") AND ($x[0]['location'] != ""))
160                         $location = $x[0]['location'];
161
162                 if (($about == "") AND ($x[0]['about'] != ""))
163                         $about = $x[0]['about'];
164
165                 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
166                         q("update gcontact set `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s', `updated` = '%s', `location` = '%s', `about` = '%s'
167                                 where `nurl` = '%s'",
168                                 dbesc($name),
169                                 dbesc($network),
170                                 dbesc($profile_photo),
171                                 dbesc($connect_url),
172                                 dbesc($profile_url),
173                                 dbesc($updated),
174                                 dbesc($location),
175                                 dbesc($about),
176                                 dbesc(normalise_link($profile_url))
177                         );
178                 }
179         } else {
180                 q("insert into `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `updated`, `location`, `about`)
181                         values ('%s', '%s', '%s', '%s', '%s','%s', '%s')",
182                         dbesc($name),
183                         dbesc($network),
184                         dbesc($profile_url),
185                         dbesc(normalise_link($profile_url)),
186                         dbesc($profile_photo),
187                         dbesc($connect_url),
188                         dbesc($updated),
189                         dbesc($location),
190                         dbesc($about)
191                 );
192                 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
193                         dbesc(normalise_link($profile_url))
194                 );
195                 if(count($x))
196                         $gcid = $x[0]['id'];
197         }
198
199         if(! $gcid)
200                 return $gcid;
201
202         $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
203                 intval($cid),
204                 intval($uid),
205                 intval($gcid),
206                 intval($zcid)
207         );
208         if(! count($r)) {
209                 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
210                         intval($cid),
211                         intval($uid),
212                         intval($gcid),
213                         intval($zcid),
214                         dbesc(datetime_convert())
215                 );
216         } else {
217                 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
218                         dbesc(datetime_convert()),
219                         intval($cid),
220                         intval($uid),
221                         intval($gcid),
222                         intval($zcid)
223                 );
224         }
225
226         // For unknown reasons there are sometimes duplicates
227         q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
228                 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
229                 dbesc(normalise_link($profile_url)),
230                 intval($gcid)
231         );
232
233         return $gcid;
234 }
235
236 function poco_contact_from_body($body, $created, $cid, $uid) {
237         preg_replace_callback("/\[share(.*?)\].*?\[\/share\]/ism",
238                 function ($match) use ($created, $cid, $uid){
239                         return(sub_poco_from_share($match, $created, $cid, $uid));
240                 }, $body);
241 }
242
243 function sub_poco_from_share($share, $created, $cid, $uid) {
244         $profile = "";
245         preg_match("/profile='(.*?)'/ism", $share[1], $matches);
246         if ($matches[1] != "")
247                 $profile = $matches[1];
248
249         preg_match('/profile="(.*?)"/ism', $share[1], $matches);
250         if ($matches[1] != "")
251                 $profile = $matches[1];
252
253         if ($profile == "")
254                 return;
255
256         logger("prepare poco_check for profile ".$profile, LOGGER_DEBUG);
257         poco_check($profile, "", "", "", "", "", "", $created, $cid, $uid);
258 }
259
260 function count_common_friends($uid,$cid) {
261
262         $r = q("SELECT count(*) as `total`
263                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
264                 where `glink`.`cid` = %d and `glink`.`uid` = %d
265                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
266                 intval($cid),
267                 intval($uid),
268                 intval($uid),
269                 intval($cid)
270         );
271
272 //      logger("count_common_friends: $uid $cid {$r[0]['total']}"); 
273         if(count($r))
274                 return $r[0]['total'];
275         return 0;
276
277 }
278
279
280 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
281
282         if($shuffle)
283                 $sql_extra = " order by rand() ";
284         else
285                 $sql_extra = " order by `gcontact`.`name` asc ";
286
287         $r = q("SELECT `gcontact`.*
288                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
289                 where `glink`.`cid` = %d and `glink`.`uid` = %d
290                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) 
291                 $sql_extra limit %d, %d",
292                 intval($cid),
293                 intval($uid),
294                 intval($uid),
295                 intval($cid),
296                 intval($start),
297                 intval($limit)
298         );
299
300         return $r;
301
302 }
303
304
305 function count_common_friends_zcid($uid,$zcid) {
306
307         $r = q("SELECT count(*) as `total`
308                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
309                 where `glink`.`zcid` = %d
310                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
311                 intval($zcid),
312                 intval($uid)
313         );
314
315         if(count($r))
316                 return $r[0]['total'];
317         return 0;
318
319 }
320
321 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
322
323         if($shuffle)
324                 $sql_extra = " order by rand() ";
325         else
326                 $sql_extra = " order by `gcontact`.`name` asc ";
327
328         $r = q("SELECT `gcontact`.*
329                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
330                 where `glink`.`zcid` = %d
331                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) 
332                 $sql_extra limit %d, %d",
333                 intval($zcid),
334                 intval($uid),
335                 intval($start),
336                 intval($limit)
337         );
338
339         return $r;
340
341 }
342
343
344 function count_all_friends($uid,$cid) {
345
346         $r = q("SELECT count(*) as `total`
347                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
348                 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
349                 intval($cid),
350                 intval($uid)
351         );
352
353         if(count($r))
354                 return $r[0]['total'];
355         return 0;
356
357 }
358
359
360 function all_friends($uid,$cid,$start = 0, $limit = 80) {
361
362         $r = q("SELECT `gcontact`.*
363                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
364                 where `glink`.`cid` = %d and `glink`.`uid` = %d
365                 order by `gcontact`.`name` asc LIMIT %d, %d ",
366                 intval($cid),
367                 intval($uid),
368                 intval($start),
369                 intval($limit)
370         );
371
372         return $r;
373 }
374
375
376
377 function suggestion_query($uid, $start = 0, $limit = 80) {
378
379         if(! $uid)
380                 return array();
381
382         $network = array(NETWORK_DFRN);
383
384         if (get_config('system','diaspora_enabled'))
385                 $network[] = NETWORK_DIASPORA;
386
387         if (!get_config('system','ostatus_disabled'))
388                 $network[] = NETWORK_OSTATUS;
389
390         $sql_network = implode("', '", $network);
391         //$sql_network = "'".$sql_network."', ''";
392         $sql_network = "'".$sql_network."'";
393
394         $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
395                 INNER JOIN glink on glink.gcid = gcontact.id
396                 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
397                 and not gcontact.name in ( select name from contact where uid = %d )
398                 and not gcontact.id in ( select gcid from gcign where uid = %d )
399                 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
400                 AND `gcontact`.`network` IN (%s)
401                 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
402                 intval($uid),
403                 intval($uid),
404                 intval($uid),
405                 intval($uid),
406                 $sql_network,
407                 intval($start),
408                 intval($limit)
409         );
410
411         if(count($r) && count($r) >= ($limit -1))
412                 return $r;
413
414         $r2 = q("SELECT gcontact.* from gcontact
415                 INNER JOIN glink on glink.gcid = gcontact.id
416                 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
417                 and not gcontact.name in ( select name from contact where uid = %d )
418                 and not gcontact.id in ( select gcid from gcign where uid = %d )
419                 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
420                 AND `gcontact`.`network` IN (%s)
421                 order by rand() limit %d, %d ",
422                 intval($uid),
423                 intval($uid),
424                 intval($uid),
425                 $sql_network,
426                 intval($start),
427                 intval($limit)
428         );
429
430         $list = array();
431         foreach ($r2 AS $suggestion)
432                 $list[$suggestion["nurl"]] = $suggestion;
433
434         foreach ($r AS $suggestion)
435                 $list[$suggestion["nurl"]] = $suggestion;
436
437         return $list;
438 }
439
440 function update_suggestions() {
441
442         $a = get_app();
443
444         $done = array();
445
446         poco_load(0,0,0,$a->get_baseurl() . '/poco');
447
448         $done[] = $a->get_baseurl() . '/poco';
449
450         if(strlen(get_config('system','directory_submit_url'))) {
451                 $x = fetch_url('http://dir.friendica.com/pubsites');
452                 if($x) {
453                         $j = json_decode($x);
454                         if($j->entries) {
455                                 foreach($j->entries as $entry) {
456                                         $url = $entry->url . '/poco';
457                                         if(! in_array($url,$done))
458                                                 poco_load(0,0,0,$entry->url . '/poco');
459                                 }
460                         }
461                 }
462         }
463
464         $r = q("select distinct(poco) as poco from contact where network = '%s'",
465                 dbesc(NETWORK_DFRN)
466         );
467
468         if(count($r)) {
469                 foreach($r as $rr) {
470                         $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
471                         if(! in_array($base,$done))
472                                 poco_load(0,0,0,$base);
473                 }
474         }
475 }