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