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