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