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