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