How to check PHP filecode syntax in PHP? - php

I am in a need of running the PHP parser for PHP code inside PHP. I am on Windows, and I have tried
system("C:\\Program Files (x86)\\PHP\\php.exe -l \"C:/Program Files (x86)/Apache/htdocs/a.php\"", $output);
var_dump($output);
without luck. The parameter -l should check for correct PHP syntax, and throw errors if some problems exist. Basically, I want to do something similar to this picture:
(source: mpsoftware.dk)
That is, to be able to detect errors in code.

This seems to work:
<?php
$file = dirname(__FILE__) . '/test.php';
//$output will be filled with output from the command
//$ret will be the return code
exec('php -l ' . escapeshellarg($file), $output, $ret);
//return code should be zero if it was ok
if ($ret != 0) {
echo 'error:';
var_dump($output);
} else {
echo 'ok';
}

The runkit extension provides a function to do that:
runkit_lint_file()
If you can not use runkit then you only other way is to do what you already tried:
echo passthru("php -l ".__FILE__);
Try fixing your path with realpath() if it does not work.

Related

Cannot print, echo or var_dump in Codeignitor [PHP]

I am new to Codeignitor, but I have some years of experience with PHP.
Although my Environment in set to development in index.php file on the root of the project,
if($some_stament=="true") {
$relativePath = $w->getRelativePath();
$sep = strlen($relativePath)===0?'':'/';
$command = str_replace("'", "", $previewSyncCommand[$site].' '.escapeshellarg($relativePath).$sep.$fromName.' '.$fullToPath);
var_dump($command);
$return_var=-1;
exec($command, $outputArray, $return_var);
$output = implode("\n",$outputArray);
$returnMessage = RsyncWrapper::$rsyncStatus[$return_var];
}
or
print_r($bar_variable);
echo $foo_bar;
print('example to print to screen text');
do not work, in fact break further execution of the if() statement, even if I don't call exit() or die();
The strangest part is that, if I remove all the dumps, prints or echos, the code run perfectly!
Does anyone have a ideia?

Executing the content of the shell file in php

Firstly, We have a shell script say ifelsesh.sh having the following code
a=20
b=20
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
Now, we want to execute this file by php say execSh.php as
<?php
// $contents = file_get_contents('ifelsesh.sh');
try
{
exec('ifelsesh.sh',$output);
echo $output;
}
catch(Exception $e)
{
echo $e;
}
when we run the file through the command line works fine
>php execSh.php
but when we run the execSh.php file through the browser nothing works,why and let us know the exact reason to sort it out.Thanks in advance.
Make sure in php.ini function shell_exec, exec are enabled.
You can use the exec to execute the shell script through PHP , make sure exec is enable on your machine
<?php
exec(dirname(__FILE__) . '/ifelsesh.sh');
?>

shell_exec in PHP returns empty string

shell_exec and exec are not returning any content. I can't figure out what's wrong.
Here's some code:
echo 'test: ';
$output = shell_exec('whoami');
var_export($output, TRUE);
echo PHP_EOL . '<br>' . PHP_EOL;
And here's the source of the output
test 2:
<br>
I do not have control over the host, but I believe they're running SuPHP. According to phpinfo, safe_mode is off. Running whoami from SSH outputs the expected value.
I'm at a loss. Any idea how to debug this?
You're never printing the $output variable. The var_export() call returns the content of the variable when you call it with a true second parameter, it does not print it directly.
If you want the output from a shell command read back into PHP, you're probably going to need popen(). For example:
if( ($fp = popen("some shell command", "r")) ) {
while( !feof($fp) ) {
echo fread($fp, 1024);
flush(); // input will be buffered
}
fclose($fp);
}

PHP exec() function windows 7 IIS 7.5

I am trying to run an a command line util using exec() but it doesnt return anything. I have read on php.net that the permissions on cmd.exe need to be set to allow the iis user to run it. I have not been able to do this using any method I can think of. cacls icacls and the standard security screen dont work. I am logging the output to a mysql database.
my code looks like this:
$Ret = array();
$err = "";
exec("dir", $Ret, $err);
in the db I get array for $Ret
Either something is wrong with the command, I doubt it, or I need to set the permissions somehow.
Please help.
Change the line:
exec("dir", $Ret, $err);
to:
$r = exec("dir", $Ret, $err);
echo $r;
You need to capture the return value of exec into a variable then render that.
Or try running this:
<?php
$a = array();
$e = "";
echo "exec'ing<br/>";
$r = exec("dir", $a, $e);
echo "var_dumping<br/>";
var_dump($a);
echo "=====================\n";
var_dump($e);
echo "=====================\n";
var_dump($r);
?>
If you're running this in a browser with xdebug turned off then you might need to view-source to see the results.

Why the statement exec("php -l $file", $error, $exit) set $exit = 5 while there's no error?

The file exists. I'm just validating the syntax of the file with this statement.
exec("php -l $file", $error, $exit);
It is supposed to set $exit = 0 if there's no error. In other words, the syntax in the file is right. However in my case, it sets $exit as 5 and $error as empty array. I wonder how this is the case. Thanks in advance.
Also, I'm using MAMP. PHP5.3. $file is the hash string of a file content. $code is the string of the file content gotten by file_get_contents() function. I don't think $translatedFile and $error matter in my question.
function validateSyntax($code,$translatedFile, &$error){
$translatedFile = $this->getTranslatedLanguageFile($translatedFile);
$file = 'cache/'.md5(time());
file_put_contents( $file, $code);
exec("php -l $file",$error,$exit);
foreach($error as $k=>$v){
$error[$k] = str_replace($file, $translatedFile, $v);
}
unlink($file);
if($retcode==0)return true;
return false;
}
You are quoting around all of the parameters, rather than just using quotes around the command and passing the last two arguments. You are most likely looking for
exec("php -l $file", $error, $exit);

Categories