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