]> git.mxchange.org Git - friendica.git/blob - include/follow.php
Merge pull request #2758 from annando/1609-sql-charset
[friendica.git] / include / follow.php
1 <?php
2 require_once("include/Scrape.php");
3 require_once("include/socgraph.php");
4 require_once('include/group.php');
5
6 function update_contact($id) {
7         /*
8         Warning: Never ever fetch the public key via probe_url and write it into the contacts.
9         This will reliably kill your communication with Friendica contacts.
10         */
11
12         $r = q("SELECT `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `network` FROM `contact` WHERE `id` = %d", intval($id));
13         if (!$r)
14                 return false;
15
16         $ret = probe_url($r[0]["url"]);
17
18         // If probe_url fails the network code will be different
19         if ($ret["network"] != $r[0]["network"])
20                 return false;
21
22         $update = false;
23
24         // make sure to not overwrite existing values with blank entries
25         foreach ($ret AS $key => $val) {
26                 if (isset($r[0][$key]) AND ($r[0][$key] != "") AND ($val == ""))
27                         $ret[$key] = $r[0][$key];
28
29                 if (isset($r[0][$key]) AND ($ret[$key] != $r[0][$key]))
30                         $update = true;
31         }
32
33         if (!$update)
34                 return true;
35
36         q("UPDATE `contact` SET `url` = '%s', `nurl` = '%s', `addr` = '%s', `alias` = '%s', `batch` = '%s', `notify` = '%s', `poll` = '%s', `poco` = '%s' WHERE `id` = %d",
37                 dbesc($ret['url']),
38                 dbesc(normalise_link($ret['url'])),
39                 dbesc($ret['addr']),
40                 dbesc($ret['alias']),
41                 dbesc($ret['batch']),
42                 dbesc($ret['notify']),
43                 dbesc($ret['poll']),
44                 dbesc($ret['poco']),
45                 intval($id)
46         );
47
48         // Update the corresponding gcontact entry
49         poco_last_updated($ret["url"]);
50
51         return true;
52 }
53
54 //
55 // Takes a $uid and a url/handle and adds a new contact
56 // Currently if the contact is DFRN, interactive needs to be true, to redirect to the
57 // dfrn_request page.
58
59 // Otherwise this can be used to bulk add statusnet contacts, twitter contacts, etc.
60 // Returns an array
61 //  $return['success'] boolean true if successful
62 //  $return['message'] error text if success is false.
63
64
65
66 function new_contact($uid,$url,$interactive = false) {
67
68         $result = array('cid' => -1, 'success' => false,'message' => '');
69
70         $a = get_app();
71
72         // remove ajax junk, e.g. Twitter
73
74         $url = str_replace('/#!/','/',$url);
75
76         if(! allowed_url($url)) {
77                 $result['message'] = t('Disallowed profile URL.');
78                 return $result;
79         }
80
81         if(! $url) {
82                 $result['message'] = t('Connect URL missing.');
83                 return $result;
84         }
85
86         $arr = array('url' => $url, 'contact' => array());
87
88         call_hooks('follow', $arr);
89
90         if(x($arr['contact'],'name'))
91                 $ret = $arr['contact'];
92         else
93                 $ret = probe_url($url);
94
95         if($ret['network'] === NETWORK_DFRN) {
96                 if($interactive) {
97                         if(strlen($a->path))
98                                 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
99                         else
100                                 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
101
102                         goaway($ret['request'] . "&addr=$myaddr");
103
104                         // NOTREACHED
105                 }
106         }
107         else {
108                 if(get_config('system','dfrn_only')) {
109                         $result['message'] = t('This site is not configured to allow communications with other networks.') . EOL;
110                         $result['message'] != t('No compatible communication protocols or feeds were discovered.') . EOL;
111                         return $result;
112                 }
113         }
114
115
116
117
118
119
120         // This extra param just confuses things, remove it
121         if($ret['network'] === NETWORK_DIASPORA)
122                 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
123
124
125         // do we have enough information?
126
127         if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
128                 $result['message'] .=  t('The profile address specified does not provide adequate information.') . EOL;
129                 if(! x($ret,'poll'))
130                         $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
131                 if(! x($ret,'name'))
132                         $result['message'] .=  t('An author or name was not found.') . EOL;
133                 if(! x($ret,'url'))
134                         $result['message'] .=  t('No browser URL could be matched to this address.') . EOL;
135                 if(strpos($url,'@') !== false) {
136                         $result['message'] .=  t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
137                         $result['message'] .=  t('Use mailto: in front of address to force email check.') . EOL;
138                 }
139                 return $result;
140         }
141
142         if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
143                 $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
144                 $ret['notify'] = '';
145         }
146
147
148
149
150
151
152         if(! $ret['notify']) {
153                 $result['message'] .=  t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
154         }
155
156         $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
157
158         $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
159
160         $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
161
162         if(in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA)))
163                 $writeable = 1;
164
165         // check if we already have a contact
166         // the poll url is more reliable than the profile url, as we may have
167         // indirect links or webfinger links
168
169         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1",
170                 intval($uid),
171                 dbesc($ret['poll']),
172                 dbesc(normalise_link($ret['poll'])),
173                 dbesc($ret['network'])
174         );
175
176         if(!count($r))
177                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` = '%s' LIMIT 1",
178                         intval($uid), dbesc(normalise_link($url)), dbesc($ret['network'])
179         );
180
181         if(count($r)) {
182                 // update contact
183                 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
184                         q("UPDATE `contact` SET `rel` = %d , `subhub` = %d, `readonly` = 0 WHERE `id` = %d AND `uid` = %d",
185                                 intval(CONTACT_IS_FRIEND),
186                                 intval($subhub),
187                                 intval($r[0]['id']),
188                                 intval($uid)
189                         );
190                 }
191         } else {
192
193
194                 // check service class limits
195
196                 $r = q("select count(*) as total from contact where uid = %d and pending = 0 and self = 0",
197                         intval($uid)
198                 );
199                 if(count($r))
200                         $total_contacts = $r[0]['total'];
201
202                 if(! service_class_allows($uid,'total_contacts',$total_contacts)) {
203                         $result['message'] .= upgrade_message();
204                         return $result;
205                 }
206
207                 $r = q("select count(network) as total from contact where uid = %d and network = '%s' and pending = 0 and self = 0",
208                         intval($uid),
209                         dbesc($network)
210                 );
211                 if(count($r))
212                         $total_network = $r[0]['total'];
213
214                 if(! service_class_allows($uid,'total_contacts_' . $network,$total_network)) {
215                         $result['message'] .= upgrade_message();
216                         return $result;
217                 }
218
219                 $new_relation = ((in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA))) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
220
221                 // create contact record
222                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `network`, `pubkey`, `rel`, `priority`,
223                         `writable`, `hidden`, `blocked`, `readonly`, `pending`, `subhub` )
224                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0, %d ) ",
225                         intval($uid),
226                         dbesc(datetime_convert()),
227                         dbesc($ret['url']),
228                         dbesc(normalise_link($ret['url'])),
229                         dbesc($ret['addr']),
230                         dbesc($ret['alias']),
231                         dbesc($ret['batch']),
232                         dbesc($ret['notify']),
233                         dbesc($ret['poll']),
234                         dbesc($ret['poco']),
235                         dbesc($ret['name']),
236                         dbesc($ret['nick']),
237                         dbesc($ret['network']),
238                         dbesc($ret['pubkey']),
239                         intval($new_relation),
240                         intval($ret['priority']),
241                         intval($writeable),
242                         intval($hidden),
243                         intval($subhub)
244                 );
245         }
246
247         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
248                 dbesc($ret['url']),
249                 dbesc($ret['network']),
250                 intval($uid)
251         );
252
253         if(! count($r)) {
254                 $result['message'] .=  t('Unable to retrieve contact information.') . EOL;
255                 return $result;
256         }
257
258         $contact = $r[0];
259         $contact_id  = $r[0]['id'];
260         $result['cid'] = $contact_id;
261
262         $def_gid = get_default_group($uid, $contact["network"]);
263         if (intval($def_gid))
264                 group_add_member($uid, '', $contact_id, $def_gid);
265
266         require_once("include/Photo.php");
267
268         // Update the avatar
269         update_contact_avatar($ret['photo'],$uid,$contact_id);
270
271         // pull feed and consume it, which should subscribe to the hub.
272
273         proc_run(PRIORITY_MEDIUM, "include/onepoll.php", $contact_id, "force");
274
275         // create a follow slap
276
277         $tpl = get_markup_template('follow_slap.tpl');
278         $slap = replace_macros($tpl, array(
279                 '$name' => $a->user['username'],
280                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
281                 '$photo' => $a->contact['photo'],
282                 '$thumb' => $a->contact['thumb'],
283                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
284                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . get_guid(32),
285                 '$title' => '',
286                 '$type' => 'text',
287                 '$content' => t('following'),
288                 '$nick' => $a->user['nickname'],
289                 '$verb' => ACTIVITY_FOLLOW,
290                 '$ostat_follow' => ''
291         ));
292
293         $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
294                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
295                         intval($uid)
296         );
297
298         if(count($r)) {
299                 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
300                         require_once('include/salmon.php');
301                         slapper($r[0],$contact['notify'],$slap);
302                 }
303                 if($contact['network'] == NETWORK_DIASPORA) {
304                         require_once('include/diaspora.php');
305                         $ret = diaspora::send_share($a->user,$contact);
306                         logger('share returns: '.$ret);
307                 }
308         }
309
310         $result['success'] = true;
311         return $result;
312 }