I am very much aware that PHP is a server side language and therefore should not allow the php file to be downloaded. However, on direct visit in Chrome to the PHP file, it downloads an obfuscated version of the file, something I’d like to prevent. Is there a way to stop the file from being served up?
The code is below.
The system works to perfection inside WordPress, but if I open up Chrome (and I presume others) and visit the update.php file directly, it downloads.
Notably, I tried to echo an HTML page but it messes up the way the system works. I’m hoping there’s some sort of .htaccess trick for this.
if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'version':
echo '1.1';
break;
case 'info':
$obj = new stdClass();
$obj->slug = 'plugin.php';
$obj->plugin_name = 'plugin.php';
$obj->new_version = '1.1';
$obj->requires = '3.0';
$obj->tested = '3.3.1';
$obj->downloaded = 12540;
$obj->last_updated = '2012-01-12';
$obj->sections = array(
'description' => 'The new version of the Auto-Update plugin',
'another_section' => 'This is another section',
'changelog' => 'Some new features'
);
$obj->download_link = 'http://localhost/update.php';
echo serialize($obj);
case 'license':
echo 'false';
break;
}
} else {
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
readfile('update.zip');
}
Your code, if it does not receive
$_POST['action'], then sendsupdate.zipto the browser in theelsecase.What you are seeing when visit the file not via a POST is not obfuscated PHP. Rather, it is sending the contents of the file
update.zipto the browser for download. But since the code doesn’t supply a filename hint in the headers, it doesn’t come asupdate.zipand instead probably looks like a .php file with the same name as your script.If you want
update.zipto look like a zip file, add afilenameinto theContent-Dispositionoutput header:Now, if you don’t want it sending
update.zipat all, remove the entireelse {}block from the bottom, and replace it with something likeUpdate: To restrict access only to referrals by
wp_autoupdate.phpConsult
$_SERVER['HTTP_REFERRER'], but know that the value of this can be spoofed. This cannot be used with 100% reliability.To achieve 100% reliability, you would probably need to modify
wp_autoupdate.phpto set a session variable which is then checked byupdate.php, ensuring the request came from the right place.