]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
efcbdfd24475f6233d38d2dbceffe737e47fc243
[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('The profile address specified belongs to a network which has been disabled on this site.') . 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         if($ret['network'] === NETWORK_DIASPORA)
75                 $writeable = 1;
76
77         // check if we already have a contact
78         // the poll url is more reliable than the profile url, as we may have
79         // indirect links or webfinger links
80
81         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
82                 intval(local_user()),
83                 dbesc($ret['poll'])
84         );                      
85
86         if(count($r)) {
87                 // update contact
88                 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
89                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
90                                 intval(CONTACT_IS_FRIEND),
91                                 intval($r[0]['id']),
92                                 intval(local_user())
93                         );
94                 }
95         }
96         else {
97
98                 $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
99                 if($ret['network'] === NETWORK_DIASPORA)
100                         $new_relation = CONTACT_IS_FOLLOWER;
101
102                 // create contact record 
103                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `addr`, `alias`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
104                         `writable`, `blocked`, `readonly`, `pending` )
105                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
106                         intval(local_user()),
107                         dbesc(datetime_convert()),
108                         dbesc($ret['url']),
109                         dbesc($ret['addr']),
110                         dbesc($ret['alias']),
111                         dbesc($ret['notify']),
112                         dbesc($ret['poll']),
113                         dbesc($ret['name']),
114                         dbesc($ret['nick']),
115                         dbesc($ret['photo']),
116                         dbesc($ret['network']),
117                         intval($new_relation),
118                         intval($ret['priority']),
119                         intval($writeable)
120                 );
121         }
122
123         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
124                 dbesc($ret['url']),
125                 intval(local_user())
126         );
127
128         if(! count($r)) {
129                 notice( t('Unable to retrieve contact information.') . EOL);
130                 goaway($_SESSION['return_url']);
131                 // NOTREACHED
132         }
133
134         $contact = $r[0];
135         $contact_id  = $r[0]['id'];
136
137         require_once("Photo.php");
138
139         $photos = import_profile_photo($ret['photo'],local_user(),$contact_id);
140
141         $r = q("UPDATE `contact` SET `photo` = '%s', 
142                         `thumb` = '%s',
143                         `micro` = '%s', 
144                         `name-date` = '%s', 
145                         `uri-date` = '%s', 
146                         `avatar-date` = '%s'
147                         WHERE `id` = %d LIMIT 1
148                 ",
149                         dbesc($photos[0]),
150                         dbesc($photos[1]),
151                         dbesc($photos[2]),
152                         dbesc(datetime_convert()),
153                         dbesc(datetime_convert()),
154                         dbesc(datetime_convert()),
155                         intval($contact_id)
156                 );                      
157
158
159         // pull feed and consume it, which should subscribe to the hub.
160
161         proc_run('php',"include/poller.php","$contact_id");
162
163         // create a follow slap
164
165         $tpl = get_markup_template('follow_slap.tpl');
166         $slap = replace_macros($tpl, array(
167                 '$name' => $a->user['username'],
168                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
169                 '$photo' => $a->contact['photo'],
170                 '$thumb' => $a->contact['thumb'],
171                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
172                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
173                 '$title' => '',
174                 '$type' => 'text',
175                 '$content' => t('following'),
176                 '$nick' => $a->user['nickname'],
177                 '$verb' => ACTIVITY_FOLLOW,
178                 '$ostat_follow' => ''
179         ));
180
181         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
182                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
183                         intval(local_user())
184         );
185
186         if(count($r)) {
187                 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
188                         require_once('include/salmon.php');
189                         slapper($r[0],$contact['notify'],$slap);
190                 }
191                 if($contact['network'] == NETWORK_DIASPORA) {
192                         require_once('include/diaspora.php');
193                         $ret = diaspora_share($a->user,$r[0]);
194                         logger('mod_follow: diaspora_share returns: ' . $ret);
195                 }
196         }
197
198         goaway($a->get_baseurl() . '/contacts/' . $contact_id);
199 //      goaway($_SESSION['return_url']);
200         // NOTREACHED
201 }