]> git.mxchange.org Git - friendica.git/blob - include/follow.php
Merge branch 'master' of git://github.com/friendica/friendica
[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('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         // This extra param just confuses things, remove it
66         if($ret['network'] === NETWORK_DIASPORA)
67                 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
68
69
70         // do we have enough information?
71         
72         if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
73                 $result['message'] .=  t('The profile address specified does not provide adequate information.') . EOL;
74                 if(! x($ret,'poll'))
75                         $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
76                 if(! x($ret,'name'))
77                         $result['message'] .=  t('An author or name was not found.') . EOL;
78                 if(! x($ret,'url'))
79                         $result['message'] .=  t('No browser URL could be matched to this address.') . EOL;
80                 if(strpos($url,'@') !== false) {
81                         $result['message'] .=  t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
82                         $result['message'] .=  t('Use mailto: in front of address to force email check.') . EOL;
83                 }
84                 return $result;
85         }
86
87         if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
88                 $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
89                 $ret['notify'] = '';
90         }
91
92         if(! $ret['notify']) {
93                 $result['message'] .=  t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
94         }
95
96         $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
97
98         $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
99
100         $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
101
102         if($ret['network'] === NETWORK_MAIL) {
103                 $writeable = 1;
104                 
105         }
106         if($ret['network'] === NETWORK_DIASPORA)
107                 $writeable = 1;
108
109         // check if we already have a contact
110         // the poll url is more reliable than the profile url, as we may have
111         // indirect links or webfinger links
112
113         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
114                 intval($uid),
115                 dbesc($ret['poll'])
116         );                      
117
118
119         if(count($r)) {
120                 // update contact
121                 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
122                         q("UPDATE `contact` SET `rel` = %d , `subhub` = %d, `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
123                                 intval(CONTACT_IS_FRIEND),
124                                 intval($subhub),
125                                 intval($r[0]['id']),
126                                 intval($uid)
127                         );
128                 }
129         }
130         else {
131
132                 $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
133                 if($ret['network'] === NETWORK_DIASPORA)
134                         $new_relation = CONTACT_IS_FOLLOWER;
135
136                 // create contact record 
137                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
138                         `writable`, `hidden`, `blocked`, `readonly`, `pending`, `subhub` )
139                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0, %d ) ",
140                         intval($uid),
141                         dbesc(datetime_convert()),
142                         dbesc($ret['url']),
143                         dbesc(normalise_link($ret['url'])),
144                         dbesc($ret['addr']),
145                         dbesc($ret['alias']),
146                         dbesc($ret['batch']),
147                         dbesc($ret['notify']),
148                         dbesc($ret['poll']),
149                         dbesc($ret['poco']),
150                         dbesc($ret['name']),
151                         dbesc($ret['nick']),
152                         dbesc($ret['photo']),
153                         dbesc($ret['network']),
154                         dbesc($ret['pubkey']),
155                         intval($new_relation),
156                         intval($ret['priority']),
157                         intval($writeable),
158                         intval($hidden),
159                         intval($subhub)
160                 );
161         }
162
163         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
164                 dbesc($ret['url']),
165                 intval($uid)
166         );
167
168         if(! count($r)) {
169                 $result['message'] .=  t('Unable to retrieve contact information.') . EOL;
170                 return $result;
171         }
172
173         $contact = $r[0];
174         $contact_id  = $r[0]['id'];
175
176
177         $g = q("select def_gid from user where uid = %d limit 1",
178                 intval($uid)
179         );
180         if($g && intval($g[0]['def_gid'])) {
181                 require_once('include/group.php');
182                 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
183         }
184
185         require_once("Photo.php");
186
187         $photos = import_profile_photo($ret['photo'],$uid,$contact_id);
188
189         $r = q("UPDATE `contact` SET `photo` = '%s', 
190                         `thumb` = '%s',
191                         `micro` = '%s', 
192                         `name-date` = '%s', 
193                         `uri-date` = '%s', 
194                         `avatar-date` = '%s'
195                         WHERE `id` = %d LIMIT 1
196                 ",
197                         dbesc($photos[0]),
198                         dbesc($photos[1]),
199                         dbesc($photos[2]),
200                         dbesc(datetime_convert()),
201                         dbesc(datetime_convert()),
202                         dbesc(datetime_convert()),
203                         intval($contact_id)
204                 );                      
205
206
207         // pull feed and consume it, which should subscribe to the hub.
208
209         proc_run('php',"include/poller.php","$contact_id");
210
211         // create a follow slap
212
213         $tpl = get_markup_template('follow_slap.tpl');
214         $slap = replace_macros($tpl, array(
215                 '$name' => $a->user['username'],
216                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
217                 '$photo' => $a->contact['photo'],
218                 '$thumb' => $a->contact['thumb'],
219                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
220                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
221                 '$title' => '',
222                 '$type' => 'text',
223                 '$content' => t('following'),
224                 '$nick' => $a->user['nickname'],
225                 '$verb' => ACTIVITY_FOLLOW,
226                 '$ostat_follow' => ''
227         ));
228
229         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
230                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
231                         intval($uid)
232         );
233
234         if(count($r)) {
235                 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
236                         require_once('include/salmon.php');
237                         slapper($r[0],$contact['notify'],$slap);
238                 }
239                 if($contact['network'] == NETWORK_DIASPORA) {
240                         require_once('include/diaspora.php');
241                         $ret = diaspora_share($a->user,$contact);
242                         logger('mod_follow: diaspora_share returns: ' . $ret);
243                 }
244         }
245
246         $result['success'] = true;
247         return $result;
248 }