]> git.mxchange.org Git - friendica.git/blob - include/socgraph.php
3e9c00633b78f2cd975da1ae93c7604502c84dc2
[friendica.git] / include / socgraph.php
1 <?php
2
3 require_once('include/datetime.php');
4
5
6 /*
7  * poco_load
8  *
9  * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
10  * and add the entries to the gcontact (Global Contact) table, or update existing entries
11  * if anything (name or photo) has changed.
12  * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
13  *
14  * Once the global contact is stored add (if necessary) the contact linkage which associates
15  * the given uid, cid to the global contact entry. There can be many uid/cid combinations
16  * pointing to the same global contact id. 
17  *
18  */
19  
20
21
22
23 function poco_load($cid,$uid = 0,$url = null) {
24         $a = get_app();
25         if((! $url) || (! $uid)) {
26                 $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
27                         intval($cid)
28                 );
29                 if(count($r)) {
30                         $url = $r[0]['poco'];
31                         $uid = $r[0]['uid'];
32                 }
33         }
34         if((! $url) || (! $uid))
35                 return;
36         $s = fetch_url($url . '/@me/@all?fields=displayName,urls,photos');
37
38         if(($a->get_curl_code() > 299) || (! $s))
39                 return;
40         $j = json_decode($s);
41         foreach($j->entry as $entry) {
42
43                 $profile_url = '';
44                 $profile_photo = '';
45                 $name = '';
46
47                 $name = $entry->displayName;
48
49                 foreach($entry->urls as $url) {
50                         if($url->type == 'profile') {
51                                 $profile_url = $url->value;
52                                 break;
53                         }
54                 } 
55                 foreach($entry->photos as $photo) {
56                         if($photo->type == 'profile') {
57                                 $profile_photo = $photo->value;
58                                 break;
59                         }
60                 }
61
62                 if((! $name) || (! $profile_url) || (! $profile_photo))
63                         continue; 
64                  
65                 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
66                         dbesc(normalise_link($profile_url))
67                 );
68                 if(count($x)) {
69                         $gcid = $x[0]['id'];
70
71                         if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo) {
72                                 q("update gcontact set `name` = '%s', `photo` = '%s' where
73                                         `nurl` = '%s' limit 1",
74                                         dbesc($name),
75                                         dbesc($profile_photo),
76                                         dbesc(normalise_link($profile_url))
77                                 );
78                         }
79                 }
80                 else {
81                         q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`)
82                                 values ( '%s', '%s', '%s', '%s') ",
83                                 dbesc($name),
84                                 dbesc($profile_url),
85                                 dbesc(normalise_link($profile_url)),
86                                 dbesc($profile_photo)
87                         );
88                         $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
89                                 dbesc(normalise_link($profile_url))
90                         );
91                         if(count($x))
92                                 $gcid = $x[0]['id'];
93                 }
94                 if(! $gcid)
95                         return;
96
97                 $r = q("select * from glink where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
98                         intval($cid),
99                         intval($uid),
100                         intval($gcid)
101                 );
102                 if(! count($r)) {
103                         q("insert into glink ( `cid`,`uid`,`gcid`,`updated`) values (%d,%d,%d,'%s') ",
104                                 intval($cid),
105                                 intval($uid),
106                                 intval($gcid),
107                                 dbesc(datetime_convert())
108                         );
109                 }
110                 else {
111                         q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
112                                 dbesc(datetime_convert()),
113                                 intval($cid),
114                                 intval($uid),
115                                 intval($gcid)
116                         );
117                 }
118
119         }
120
121         q("delete from glink where `cid` = %d and `uid` = %d and `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
122                 intval($cid),
123                 intval($uid)
124         );
125
126 }