Для потомков:
> php obs.php obs.php obs2.php
> php obs2.php obs.php obs3.php
<?php
define('ERR_BAD_EXTENSION', 'ERR_BAD_EXTENSION');
define('ERR_BAD_USAGE', 'ERR_BAD_USAGE');
define('ERR_EMPTY_FILE', 'ERR_EMPTY_FILE');
define('ERR_EXISTS_FILE', 'ERR_EXISTS_FILE');
define('ERR_NOT_FOUND_FILE', 'ERR_NOT_FOUND_FILE');
$messages = [
ERR_BAD_EXTENSION => 'Incorrect file extension',
ERR_BAD_USAGE => sprintf('Usage: php %s <input file> <output file>', basename(__FILE__)),
ERR_EMPTY_FILE => 'File name should be not empty',
ERR_EXISTS_FILE => 'File exists',
ERR_NOT_FOUND_FILE => 'File not found',
];
function _error($err, $subject = null)
{
echo PHP_EOL . $GLOBALS[ 'messages' ][ (string) $err ] . PHP_EOL . print_r($subject, 1);
die();
}
function _log($message, $subject = null)
{
echo PHP_EOL . (string) $message . PHP_EOL . print_r($subject, 1);
}
if (! isset($_SERVER[ 'argv' ][ 1 ])) {
_error(ERR_BAD_USAGE);
}
if (! isset($_SERVER[ 'argv' ][ 2 ])) {
_error(ERR_BAD_USAGE);
}
$from = $_SERVER[ 'argv' ][ 1 ];
$to = $_SERVER[ 'argv' ][ 2 ];
if (! $from) {
_error(ERR_EMPTY_FILE, $from);
}
if (! is_file($from)) {
_error(ERR_NOT_FOUND_FILE, $from);
}
if ('php' !== ($ext = pathinfo($from, PATHINFO_EXTENSION))) {
_error(ERR_BAD_EXTENSION, [ $from, $ext ]);
}
if (is_file($to)) {
_error(ERR_EXISTS_FILE, $to);
}
if ('php' !== ($ext = pathinfo($to, PATHINFO_EXTENSION))) {
_error(ERR_BAD_EXTENSION, [ $to, $ext ]);
}
_log('Processing...',
[
$from,
$to,
]
);
$content = ''
. 'ob_end_clean(); ?>'
. php_strip_whitespace($from)
;
$data = ''
. '<?php' . PHP_EOL
. sprintf('$a=\'%s\';', base64_encode(gzcompress($content, 9)))
. 'ob_start();'
. 'eval(gzuncompress(base64_decode($a)));'
. '$v=ob_get_clean();'
;
file_put_contents($to, $data);
_log('Done.', $to);