]> git.mxchange.org Git - friendica.git/blob - include/follow.php
Merge branch 'master', remote-tracking branch 'remotes/upstream/master'
[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         $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
98
99         if($ret['network'] === NETWORK_MAIL) {
100                 $writeable = 1;
101                 
102         }
103         if($ret['network'] === NETWORK_DIASPORA)
104                 $writeable = 1;
105
106         // check if we already have a contact
107         // the poll url is more reliable than the profile url, as we may have
108         // indirect links or webfinger links
109
110         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
111                 intval($uid),
112                 dbesc($ret['poll'])
113         );                      
114
115
116         if(count($r)) {
117                 // update contact
118                 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
119                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
120                                 intval(CONTACT_IS_FRIEND),
121                                 intval($r[0]['id']),
122                                 intval($uid)
123                         );
124                 }
125         }
126         else {
127
128                 $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
129                 if($ret['network'] === NETWORK_DIASPORA)
130                         $new_relation = CONTACT_IS_FOLLOWER;
131
132                 // create contact record 
133                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
134                         `writable`, `hidden`, `blocked`, `readonly`, `pending` )
135                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0 ) ",
136                         intval($uid),
137                         dbesc(datetime_convert()),
138                         dbesc($ret['url']),
139                         dbesc(normalise_link($ret['url'])),
140                         dbesc($ret['addr']),
141                         dbesc($ret['alias']),
142                         dbesc($ret['batch']),
143                         dbesc($ret['notify']),
144                         dbesc($ret['poll']),
145                         dbesc($ret['poco']),
146                         dbesc($ret['name']),
147                         dbesc($ret['nick']),
148                         dbesc($ret['photo']),
149                         dbesc($ret['network']),
150                         dbesc($ret['pubkey']),
151                         intval($new_relation),
152                         intval($ret['priority']),
153                         intval($writeable),
154                         intval($hidden)
155                 );
156         }
157
158         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
159                 dbesc($ret['url']),
160                 intval($uid)
161         );
162
163         if(! count($r)) {
164                 $result['message'] .=  t('Unable to retrieve contact information.') . EOL;
165                 return $result;
166         }
167
168         $contact = $r[0];
169         $contact_id  = $r[0]['id'];
170
171
172         $g = q("select def_gid from user where uid = %d limit 1",
173                 intval($uid)
174         );
175         if($g && intval($g[0]['def_gid'])) {
176                 require_once('include/group.php');
177                 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
178         }
179
180         require_once("Photo.php");
181
182         $photos = import_profile_photo($ret['photo'],$uid,$contact_id);
183
184         $r = q("UPDATE `contact` SET `photo` = '%s', 
185                         `thumb` = '%s',
186                         `micro` = '%s', 
187                         `name-date` = '%s', 
188                         `uri-date` = '%s', 
189                         `avatar-date` = '%s'
190                         WHERE `id` = %d LIMIT 1
191                 ",
192                         dbesc($photos[0]),
193                         dbesc($photos[1]),
194                         dbesc($photos[2]),
195                         dbesc(datetime_convert()),
196                         dbesc(datetime_convert()),
197                         dbesc(datetime_convert()),
198                         intval($contact_id)
199                 );                      
200
201
202         // pull feed and consume it, which should subscribe to the hub.
203
204         proc_run('php',"include/poller.php","$contact_id");
205
206         // create a follow slap
207
208         $tpl = get_markup_template('follow_slap.tpl');
209         $slap = replace_macros($tpl, array(
210                 '$name' => $a->user['username'],
211                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
212                 '$photo' => $a->contact['photo'],
213                 '$thumb' => $a->contact['thumb'],
214                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
215                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
216                 '$title' => '',
217                 '$type' => 'text',
218                 '$content' => t('following'),
219                 '$nick' => $a->user['nickname'],
220                 '$verb' => ACTIVITY_FOLLOW,
221                 '$ostat_follow' => ''
222         ));
223
224         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
225                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
226                         intval($uid)
227         );
228
229         if(count($r)) {
230                 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
231                         require_once('include/salmon.php');
232                         slapper($r[0],$contact['notify'],$slap);
233                 }
234                 if($contact['network'] == NETWORK_DIASPORA) {
235                         require_once('include/diaspora.php');
236                         $ret = diaspora_share($a->user,$contact);
237                         logger('mod_follow: diaspora_share returns: ' . $ret);
238                 }
239         }
240
241         $result['success'] = true;
242         return $result;
243 }