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