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