]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
7ac9a50c315ca2c4fee0ef0203ba49b68132f2c9
[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 = notags(trim($_POST['url']));
14
15         if($url) {
16                 $links = lrdd($url);
17                 if(count($links)) {
18                         foreach($links as $link) {
19                                 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
20                                         $dfrn = $link['@attributes']['href'];
21                                 if($link['@attributes']['rel'] === 'salmon')
22                                         $notify = $link['@attributes']['href'];
23                                 if($link['@attributes']['rel'] === NAMESPACE_FEED)
24                                         $poll = $link['@attributes']['href'];
25                                 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
26                                         $hcard = $link['@attributes']['href'];
27                                 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
28                                         $profile = $link['@attributes']['href'];
29
30                         }
31                 }
32         }       
33
34         // If we find a DFRN site, send our subscriber to the other person's
35         // dfrn_request page and all the other details will get sorted.
36
37         if(strlen($dfrn)) {
38                 $ret = scrape_dfrn($dfrn);
39                 if(is_array($ret) && x($ret,'dfrn-request')) {
40                         if(strlen($a->path))
41                                 $myaddr = urlencode($a->get_baseurl() . '/profile/' . $a->user['nickname']);
42                         else
43                                 $myaddr = urlencode($a->user['nickname'] . '@' . $a->get_hostname());
44  
45                         goaway($ret['dfrn-request'] . "&address=$myaddr");
46                 
47                         // NOTREACHED
48                 }
49         }
50
51         if($hcard) {
52                 $vcard = scrape_vcard($hcard);
53         }
54
55         if(! $profile)
56                 $profile = $url;
57
58         // do we have enough information?
59
60         if(! x($vcard,'fn'))
61                 if(x($vcard,'nick'))
62                         $vcard['fn'] = $vcard['nick'];
63
64         if(! ((x($vcard['fn'])) && ($poll) && ($notify) && ($profile))) {
65                 notice( t('The profile address specified does not provide adequate information.') . EOL);
66                 goaway($_SESSION['return_url']);
67         } 
68
69         if(! x($vcard,'photo'))
70                 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ; 
71
72         // check if we already have a contact
73
74         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
75                 intval(local_user()),
76                 dbesc($poll)
77         );                      
78         if($r) {
79                 // update contact
80                 if($r[0]['rel'] == REL_VIP) {
81                         q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
82                                 intval(REL_BUD),
83                                 intval($r[0]['id']),
84                                 intval(local_user())
85                         );
86                 }
87         }
88         else {
89                 // create contact record 
90                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, 
91                         `blocked`, `readonly`, `pending` )
92                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 0 ) ",
93                         intval(local_user()),
94                         dbesc(datetime_convert()),
95                         dbesc($profile),
96                         dbesc($notify),
97                         dbesc($poll),
98                         dbesc($vcard['fn']),
99                         dbesc($vcard['nick']),
100                         dbesc($vcard['photo']),
101                         dbesc('stat'),
102                         intval(REL_FAN)
103                 );
104         }
105         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
106                 dbesc($profile),
107                 intval(local_user())
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         $photo_failure = false;
121
122         $filename = basename($vcard['photo']);
123         $img_str = fetch_url($vcard['photo'],true);
124         $img = new Photo($img_str);
125         if($img->is_valid()) {
126
127                 $img->scaleImageSquare(175);
128                                         
129                 $hash = photo_new_resource();
130
131                 $r = $img->store(local_user(), $contact_id, $hash, $filename, t('Contact Photos'), 4 );
132
133                 if($r === false)
134                         $photo_failure = true;
135
136                 $img->scaleImage(80);
137
138                 $r = $img->store(local_user(), $contact_id, $hash, $filename, t('Contact Photos'), 5 );
139
140                 if($r === false)
141                         $photo_failure = true;
142
143                 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
144                 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
145         }
146         else
147                 $photo_failure = true;
148
149         if($photo_failure) {
150                 $photo = $a->get_baseurl() . '/images/default-profile.jpg';
151                 $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
152         }
153
154         $r = q("UPDATE `contact` SET `photo` = '%s', 
155                         `thumb` = '%s', 
156                         `name-date` = '%s', 
157                         `uri-date` = '%s', 
158                         `avatar-date` = '%s'
159                         WHERE `id` = %d LIMIT 1
160                 ",
161                         dbesc($photo),
162                         dbesc($thumb),
163                         dbesc(datetime_convert()),
164                         dbesc(datetime_convert()),
165                         dbesc(datetime_convert()),
166                         intval($contact_id)
167                 );                      
168
169
170         // pull feed and consume it, which should subscribe to the hub.
171
172
173         // create a follow slap
174
175         $tpl = load_view_file('view/follow_slap.tpl');
176         $slap = replace_macros($tpl, array(
177                 '$name' => $a->user['username'],
178                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
179                 '$photo' => $a->contact['photo'],
180                 '$thumb' => $a->contact['thumb'],
181                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
182                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
183                 '$title' => '',
184                 '$type' => 'text',
185                 '$content' => t('following'),
186                 '$nick' => $a->user['nickname'],
187                 '$verb' => ACTIVITY_FOLLOW
188         ));
189
190         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
191                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
192                         intval(local_user())
193         );
194
195         require_once('include/salmon.php');
196         slapper($r[0],$contact,$slap);
197
198         goaway($_SESSION['return_url']);
199         // NOTREACHED
200 }