]> git.mxchange.org Git - friendica.git/blob - include/socgraph.php
name change continued, social graph tools and stuctures, fix for spanish province...
[friendica.git] / include / socgraph.php
1 <?php
2
3
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,$url = null) {
23         $a = get_app();
24         if((! $url) || (! $uid)) {
25                 $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
26                         intval($cid)
27                 );
28                 if(count($r)) {
29                         $url = $r[0]['poco'];
30                         $uid = $r[0]['uid'];
31                 }
32         }
33         if((! $url) || (! $uid))
34                 return;
35         $s = fetch_url($url . '/@me/@all?fields=displayName,urls,photos');
36
37         if(($a->get_curl_code() > 299) || (! $s))
38                 return;
39         $j = json_decode($s);
40         foreach($j->entry as $entry) {
41
42                 $profile_url = '';
43                 $profile_photo = '';
44                 $name = '';
45
46                 $name = $entry->displayName;
47
48                 foreach($entry->urls as $url) {
49                         if($url->type == 'profile') {
50                                 $profile_url = $url->value;
51                                 break;
52                         }
53                 } 
54                 foreach($entry->photos as $photo) {
55                         if($photo->type == 'profile') {
56                                 $profile_photo = $photo->value;
57                                 break;
58                         }
59                 }
60
61                 if((! $name) || (! $profile_url) || (! $profile_photo))
62                         continue; 
63                  
64                 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
65                         dbesc(normalise_link($profile_url))
66                 );
67                 if(count($x)) {
68                         $gcid = $x[0]['id'];
69
70                         if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo) {
71                                 q("update gcontact set `name` = '%s', `photo` = '%s' where
72                                         `nurl` = '%s' limit 1",
73                                         dbesc($name),
74                                         dbesc($profile_photo),
75                                         dbesc(normalise_link($profile_url))
76                                 );
77                         }
78                 }
79                 else {
80                         q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`)
81                                 values ( '%s', '%s', '%s', '%s') ",
82                                 dbesc($name),
83                                 dbesc($profile_url),
84                                 dbesc(normalise_link($profile_url)),
85                                 dbesc($profile_photo)
86                         );
87                         $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
88                                 dbesc(normalise_link($profile_url))
89                         );
90                         if(count($x))
91                                 $gcid = $x[0]['id'];
92                 }
93                 if(! $gcid)
94                         return;
95
96                 $r = q("select * from glink where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
97                         intval($cid),
98                         intval($uid),
99                         intval($gcid)
100                 );
101                 if(! count($r)) {
102                         q("insert into glink ( `cid`,`uid`,`gcid`,`updated`) values (%d,%d,%d,'%s') ",
103                                 intval($cid),
104                                 intval($uid),
105                                 intval($gcid),
106                                 dbesc(datetime_convert())
107                         );
108                 }
109                 else {
110                         q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
111                                 dbesc(datetime_convert()),
112                                 intval($cid),
113                                 intval($uid),
114                                 intval($gcid)
115                         );
116                 }
117
118         }
119
120         q("delete from gcid where `cid` = %d and `uid` = %d and `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
121                 intval($cid),
122                 intval($uid)
123         );
124
125 }