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