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