]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
Allow to set a photo from albums as profile photo
[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                                         if(! $vcard['photo']) {
117                                                 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
118                                                 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
119                                                         $vcard['photo'] = $rawmedia[0]['attribs']['']['url'];
120                                         }
121                                 }
122                         }
123                         if((! $vcard['photo']) && strlen($email))
124                                 $vcard['photo'] = gravatar_img($email);
125                         
126                         $network = 'feed';
127                         $priority = 2;
128                 }
129         }
130
131         logger('follow: poll=' . $poll . ' notify=' . $notify . ' profile=' . $profile . ' vcard=' . print_r($vcard,true));
132
133         // do we have enough information?
134         
135         if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
136                 notice( t('The profile address specified does not provide adequate information.') . EOL);
137                 goaway($_SESSION['return_url']);
138         }
139
140         if(! $notify) {
141                 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
142         }
143
144         if(! x($vcard,'photo'))
145                 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ; 
146
147         // check if we already have a contact
148         // the poll url is more reliable than the profile url, as we may have
149         // indirect links or webfinger links
150
151         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
152                 intval(local_user()),
153                 dbesc($poll)
154         );                      
155
156         if(count($r)) {
157                 // update contact
158                 if($r[0]['rel'] == REL_VIP) {
159                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
160                                 intval(REL_BUD),
161                                 intval($r[0]['id']),
162                                 intval(local_user())
163                         );
164                 }
165         }
166         else {
167                 // create contact record 
168                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
169                         `blocked`, `readonly`, `pending` )
170                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, 0, 0, 0 ) ",
171                         intval(local_user()),
172                         dbesc(datetime_convert()),
173                         dbesc($profile),
174                         dbesc($notify),
175                         dbesc($poll),
176                         dbesc($vcard['fn']),
177                         dbesc($vcard['nick']),
178                         dbesc($vcard['photo']),
179                         dbesc($network),
180                         intval(REL_FAN),
181                         intval($priority)
182                 );
183         }
184
185         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
186                 dbesc($profile),
187                 intval(local_user())
188         );
189
190         if(! count($r)) {
191                 notice( t('Unable to retrieve contact information.') . EOL);
192                 goaway($_SESSION['return_url']);
193                 // NOTREACHED
194         }
195
196         $contact = $r[0];
197         $contact_id  = $r[0]['id'];
198
199         require_once("Photo.php");
200
201         $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
202
203         $r = q("UPDATE `contact` SET `photo` = '%s', 
204                         `thumb` = '%s',
205                         `micro` = '%s', 
206                         `name-date` = '%s', 
207                         `uri-date` = '%s', 
208                         `avatar-date` = '%s'
209                         WHERE `id` = %d LIMIT 1
210                 ",
211                         dbesc($photos[0]),
212                         dbesc($photos[1]),
213                         dbesc($photos[2]),
214                         dbesc(datetime_convert()),
215                         dbesc(datetime_convert()),
216                         dbesc(datetime_convert()),
217                         intval($contact_id)
218                 );                      
219
220
221         // pull feed and consume it, which should subscribe to the hub.
222
223         $php_path = ((x($a->config,'php_path') && strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
224         //proc_close(proc_open("\"$php_path\" \"include/poller.php\" \"$contact_id\" &", array(), $foo));
225         proc_run($php_path,"include/poller.php","$contact_id");
226
227         // create a follow slap
228
229         $tpl = load_view_file('view/follow_slap.tpl');
230         $slap = replace_macros($tpl, array(
231                 '$name' => $a->user['username'],
232                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
233                 '$photo' => $a->contact['photo'],
234                 '$thumb' => $a->contact['thumb'],
235                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
236                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
237                 '$title' => '',
238                 '$type' => 'text',
239                 '$content' => t('following'),
240                 '$nick' => $a->user['nickname'],
241                 '$verb' => ACTIVITY_FOLLOW,
242                 '$ostat_follow' => ''
243         ));
244
245         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
246                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
247                         intval(local_user())
248         );
249
250
251         if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
252                 require_once('include/salmon.php');
253                 slapper($r[0],$contact['notify'],$slap);
254         }
255
256         goaway($_SESSION['return_url']);
257         // NOTREACHED
258 }