]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
830399ff5c51ae7903b239ed4fa80a07dcf9c840
[friendica.git] / mod / follow.php
1 <?php
2
3 require_once('Scrape.php');
4
5 function follow_post(&$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($_POST['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         $ret = probe_url($url);
26
27         if($ret['network'] === NETWORK_DFRN) {
28                 if(strlen($a->path))
29                         $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
30                 else
31                         $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
32  
33                 goaway($ret['request'] . "&addr=$myaddr");
34                 
35                 // NOTREACHED
36         }
37         else {
38                 if(get_config('system','dfrn_only')) {
39                         notice( t('This site is not configured to allow communications with other networks.') . EOL);
40                         notice( t('No compatible communication protocols or feeds were discovered.') . EOL);
41                         goaway($_SESSION['return_url']);
42                 }
43         }
44
45         // do we have enough information?
46         
47         if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
48                 notice( t('The profile address specified does not provide adequate information.') . EOL);
49                 if(! x($ret,'poll'))
50                         notice( t('No compatible communication protocols or feeds were discovered.') . EOL);
51                 if(! x($ret,'name'))
52                         notice( t('An author or name was not found.') . EOL);
53                 if(! x($ret,'url'))
54                         notice( t('No browser URL could be matched to this address.') . EOL);
55                 if(strpos($url,'@') !== false)
56                         notice('Unable to match @-style Identity Address with a known protocol or email contact');
57                 goaway($_SESSION['return_url']);
58         }
59
60         if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
61                 notice( t('Communication options with this network have been restricted.') . EOL);
62                 $ret['notify'] = '';
63         }
64
65         if(! $ret['notify']) {
66                 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
67         }
68
69         $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
70         if($ret['network'] === NETWORK_MAIL) {
71                 $writeable = 1;
72                 
73         }
74         // check if we already have a contact
75         // the poll url is more reliable than the profile url, as we may have
76         // indirect links or webfinger links
77
78         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
79                 intval(local_user()),
80                 dbesc($ret['poll'])
81         );                      
82
83         if(count($r)) {
84                 // update contact
85                 if($r[0]['rel'] == REL_VIP) {
86                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
87                                 intval(REL_BUD),
88                                 intval($r[0]['id']),
89                                 intval(local_user())
90                         );
91                 }
92         }
93         else {
94                 // create contact record 
95                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `addr`, `alias`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
96                         `writable`, `blocked`, `readonly`, `pending` )
97                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
98                         intval(local_user()),
99                         dbesc(datetime_convert()),
100                         dbesc($ret['url']),
101                         dbesc($ret['addr']),
102                         dbesc($ret['alias']),
103                         dbesc($ret['notify']),
104                         dbesc($ret['poll']),
105                         dbesc($ret['name']),
106                         dbesc($ret['nick']),
107                         dbesc($ret['photo']),
108                         dbesc($ret['network']),
109                         intval(($ret['network'] === NETWORK_MAIL) ? REL_BUD : REL_FAN),
110                         intval($ret['priority']),
111                         intval($writeable)
112                 );
113         }
114
115         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
116                 dbesc($ret['url']),
117                 intval(local_user())
118         );
119
120         if(! count($r)) {
121                 notice( t('Unable to retrieve contact information.') . EOL);
122                 goaway($_SESSION['return_url']);
123                 // NOTREACHED
124         }
125
126         $contact = $r[0];
127         $contact_id  = $r[0]['id'];
128
129         require_once("Photo.php");
130
131         $photos = import_profile_photo($ret['photo'],local_user(),$contact_id);
132
133         $r = q("UPDATE `contact` SET `photo` = '%s', 
134                         `thumb` = '%s',
135                         `micro` = '%s', 
136                         `name-date` = '%s', 
137                         `uri-date` = '%s', 
138                         `avatar-date` = '%s'
139                         WHERE `id` = %d LIMIT 1
140                 ",
141                         dbesc($photos[0]),
142                         dbesc($photos[1]),
143                         dbesc($photos[2]),
144                         dbesc(datetime_convert()),
145                         dbesc(datetime_convert()),
146                         dbesc(datetime_convert()),
147                         intval($contact_id)
148                 );                      
149
150
151         // pull feed and consume it, which should subscribe to the hub.
152
153         proc_run('php',"include/poller.php","$contact_id");
154
155         // create a follow slap
156
157         $tpl = get_markup_template('follow_slap.tpl');
158         $slap = replace_macros($tpl, array(
159                 '$name' => $a->user['username'],
160                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
161                 '$photo' => $a->contact['photo'],
162                 '$thumb' => $a->contact['thumb'],
163                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
164                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
165                 '$title' => '',
166                 '$type' => 'text',
167                 '$content' => t('following'),
168                 '$nick' => $a->user['nickname'],
169                 '$verb' => ACTIVITY_FOLLOW,
170                 '$ostat_follow' => ''
171         ));
172
173         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
174                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
175                         intval(local_user())
176         );
177
178
179         if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
180                 require_once('include/salmon.php');
181                 slapper($r[0],$contact['notify'],$slap);
182         }
183
184         goaway($_SESSION['return_url']);
185         // NOTREACHED
186 }