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