Skip to content

Instantly share code, notes, and snippets.

@db-ryan
Created February 19, 2012 00:54
Show Gist options
  • Save db-ryan/1861487 to your computer and use it in GitHub Desktop.
Save db-ryan/1861487 to your computer and use it in GitHub Desktop.
exec(php -l *); on OSX
<?php
// Get the text from the text box
$text = stripslashes($_POST["text"]);
// Open a temporary file
$directory = './temp';
$prefix = 'temp_';
$temp_filename = tempnam( $directory, $prefix );
// Make sure the temp file was created
//if ( !$temp_filename ) {
// $result['success'] = 'error';
// $result['exec'] = 'Could not open temp file. Sorry.'
// $json_result = json_encode( $result );
// print_r( $json_result );
//}
// Save the data to the temporary file
$temp_file = fopen( $temp_filename, 'w+' );
fwrite( $temp_file, $text );
fclose( $temp_file );
// Execute the text
$php_path ='/Applications/MAMP/bin/php/php5.3.6/bin/php';
$execute_string = $php_path . ' -l "' . $temp_filename . '"';
$escaped_execute_string = escapeshellcmd ( $execute_string );
exec( $escaped_execute_string, $output, &$return_value );
// Determine success or failure
if ( !empty( $output[0] ) ) {
$result['syntax_check'] = 'pass';
$result['exec'] = $output[0];
} else {
$result['syntax_check'] = 'fail';
$result['exec'] = $output[1];
}
//print_r( $result['exec'] ); exit;
// Remove the file
unlink( $temp_filename );
$json_result = json_encode( $result );
// Return the results
print_r( $json_result );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment