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