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