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