]> git.mxchange.org Git - friendica.git/blob - include/socgraph.php
Add missing $nav_sel in class App
[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 }
127
128
129 function count_common_friends($uid,$cid) {
130
131         $r = q("SELECT count(*) as `total`
132                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
133                 where `glink`.`cid` = %d and `glink`.`uid` = %d
134                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d ) ",
135                 intval($cid),
136                 intval($uid),
137                 intval($uid),
138                 intval($cid)
139         );
140
141         if(count($r))
142                 return $r[0]['total'];
143         return 0;
144
145 }
146
147
148 function common_friends($uid,$cid) {
149
150         $r = q("SELECT `gcontact`.* 
151                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
152                 where `glink`.`cid` = %d and `glink`.`uid` = %d
153                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d ) ",
154                 intval($cid),
155                 intval($uid),
156                 intval($uid),
157                 intval($cid)
158         );
159
160         return $r;
161
162 }
163
164
165
166 function suggestion_query($uid, $start = 0, $limit = 40) {
167
168         if(! $uid)
169                 return array();
170
171         $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact 
172                 left join glink on glink.gcid = gcontact.id 
173                 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d)
174                 and not gcontact.id in ( select gcid from gcign where uid = %d )
175                 group by glink.gcid order by total desc limit %d, %d ",
176                 intval($uid),
177                 intval($uid),
178                 intval($uid),
179                 intval($start),
180                 intval($limit)
181         );
182
183         return $r;
184
185 }