]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
email cc recipients (front end)
[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         $email_conversant = false;
16
17         if($url) {
18                 $links = lrdd($url);
19                 if(count($links)) {
20                         foreach($links as $link) {
21                                 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
22                                         $dfrn = $link['@attributes']['href'];
23                                 if($link['@attributes']['rel'] === 'salmon')
24                                         $notify = $link['@attributes']['href'];
25                                 if($link['@attributes']['rel'] === NAMESPACE_FEED)
26                                         $poll = $link['@attributes']['href'];
27                                 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
28                                         $hcard = $link['@attributes']['href'];
29                                 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
30                                         $profile = $link['@attributes']['href'];
31
32                         }
33                 }
34                 else {
35                         if((strpos($orig_url,'@')) && validate_email($orig_url)) {
36                                 $email_conversant = true;
37                         }
38                 }
39         }       
40
41         // If we find a DFRN site, send our subscriber to the other person's
42         // dfrn_request page and all the other details will get sorted.
43
44         if(strlen($dfrn)) {
45                 $ret = scrape_dfrn($dfrn);
46                 if(is_array($ret) && x($ret,'dfrn-request')) {
47                         if(strlen($a->path))
48                                 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
49                         else
50                                 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
51  
52                         goaway($ret['dfrn-request'] . "&addr=$myaddr");
53                 
54                         // NOTREACHED
55                 }
56         }
57
58         $network  = 'stat';
59         $priority = 0;
60
61         if($hcard) {
62                 $vcard = scrape_vcard($hcard);
63
64                 // Google doesn't use absolute url in profile photos
65
66                 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
67                         $h = parse_url($hcard);
68                         if($h)
69                                 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
70                 }
71         }
72
73         if(! $profile)
74                 $profile = $url;
75
76
77         if(! x($vcard,'fn'))
78                 if(x($vcard,'nick'))
79                         $vcard['fn'] = $vcard['nick'];
80
81         if((! isset($vcard)) && (! $poll)) {
82
83                 $ret = scrape_feed($url);
84
85                 if(count($ret) && ($ret['feed_atom'] || $ret['feed_rss'])) {
86                         $poll = ((x($ret,'feed_atom')) ? $ret['feed_atom'] : $ret['feed_rss']);
87                         $vcard = array();
88                         require_once('simplepie/simplepie.inc');
89                     $feed = new SimplePie();
90                         $xml = fetch_url($poll);
91
92                 $feed->set_raw_data($xml);
93
94                     $feed->init();
95
96                         $vcard['photo'] = $feed->get_image_url();
97                         $author = $feed->get_author();
98                         if($author) {                   
99                                 $vcard['fn'] = trim($author->get_name());
100                                 $vcard['nick'] = strtolower($vcard['fn']);
101                                 if(strpos($vcard['nick'],' '))
102                                         $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
103                                 $email = $author->get_email();
104                         }
105                         else {
106                                 $item = $feed->get_item(0);
107                                 if($item) {
108                                         $author = $item->get_author();
109                                         if($author) {                   
110                                                 $vcard['fn'] = trim($author->get_name());
111                                                 $vcard['nick'] = strtolower($vcard['fn']);
112                                                 if(strpos($vcard['nick'],' '))
113                                                         $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
114                                                 $email = $author->get_email();
115                                         }
116                                 }
117                         }
118                         if((! $vcard['photo']) && strlen($email))
119                                 $vcard['photo'] = gravatar_img($email);
120                         $network = 'feed';
121                         $priority = 2;
122                 }
123         }
124
125         logger('follow: poll=' . $poll . ' notify=' . $notify . ' profile=' . $profile . ' vcard=' . print_r($vcard,true));
126
127         // do we have enough information?
128         
129         if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
130                 notice( t('The profile address specified does not provide adequate information.') . EOL);
131                 goaway($_SESSION['return_url']);
132         }
133
134         if(! $notify) {
135                 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
136         }
137
138         if(! x($vcard,'photo'))
139                 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ; 
140
141         // check if we already have a contact
142         // the poll url is more reliable than the profile url, as we may have
143         // indirect links or webfinger links
144
145         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
146                 intval(local_user()),
147                 dbesc($poll)
148         );                      
149
150         if(count($r)) {
151                 // update contact
152                 if($r[0]['rel'] == REL_VIP) {
153                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
154                                 intval(REL_BUD),
155                                 intval($r[0]['id']),
156                                 intval(local_user())
157                         );
158                 }
159         }
160         else {
161                 // create contact record 
162                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
163                         `blocked`, `readonly`, `pending` )
164                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 0 ) ",
165                         intval(local_user()),
166                         dbesc(datetime_convert()),
167                         dbesc($profile),
168                         dbesc($notify),
169                         dbesc($poll),
170                         dbesc($vcard['fn']),
171                         dbesc($vcard['nick']),
172                         dbesc($vcard['photo']),
173                         dbesc($network),
174                         intval(REL_FAN),
175                         intval($priority)
176                 );
177         }
178
179         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
180                 dbesc($profile),
181                 intval(local_user())
182         );
183
184         if(! count($r)) {
185                 notice( t('Unable to retrieve contact information.') . EOL);
186                 goaway($_SESSION['return_url']);
187                 // NOTREACHED
188         }
189
190         $contact = $r[0];
191         $contact_id  = $r[0]['id'];
192
193         require_once("Photo.php");
194
195         $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
196
197         $r = q("UPDATE `contact` SET `photo` = '%s', 
198                         `thumb` = '%s',
199                         `micro` = '%s', 
200                         `name-date` = '%s', 
201                         `uri-date` = '%s', 
202                         `avatar-date` = '%s'
203                         WHERE `id` = %d LIMIT 1
204                 ",
205                         dbesc($photos[0]),
206                         dbesc($photos[1]),
207                         dbesc($photos[2]),
208                         dbesc(datetime_convert()),
209                         dbesc(datetime_convert()),
210                         dbesc(datetime_convert()),
211                         intval($contact_id)
212                 );                      
213
214
215         // pull feed and consume it, which should subscribe to the hub.
216
217         $php_path = ((x($a->config,'php_path') && strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
218         //proc_close(proc_open("\"$php_path\" \"include/poller.php\" \"$contact_id\" &", array(), $foo));
219         proc_run($php_path,"include/poller.php","$contact_id");
220
221         // create a follow slap
222
223         $tpl = load_view_file('view/follow_slap.tpl');
224         $slap = replace_macros($tpl, array(
225                 '$name' => $a->user['username'],
226                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
227                 '$photo' => $a->contact['photo'],
228                 '$thumb' => $a->contact['thumb'],
229                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
230                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
231                 '$title' => '',
232                 '$type' => 'text',
233                 '$content' => t('following'),
234                 '$nick' => $a->user['nickname'],
235                 '$verb' => ACTIVITY_FOLLOW,
236                 '$ostat_follow' => ''
237         ));
238
239         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
240                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
241                         intval(local_user())
242         );
243
244
245         if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
246                 require_once('include/salmon.php');
247                 slapper($r[0],$contact['notify'],$slap);
248         }
249
250         goaway($_SESSION['return_url']);
251         // NOTREACHED
252 }