I have installed the command jpegoptim, and it works in command line, but when I use the php command is_executable, return false
how can I check if the command can run with php ?, may be is phpini configuration, commands configuration or something like that?
you would like to check if the command is available for the user php
so it has to be in the PATH, this you can check with e.g. the command which. and such you can call from PHP as well
<?php
function checkCommand( $command ) {
$r = shell_exec("which ".$command);
if ( $r == "" )
return "ERROR: command not found: ".$command."\n";
else
return $r;
}
print( checkCommand( "ls" ) );
print( checkCommand( "lsl" ) );
?>
beware not creating any remote code execution or path traversal with such stuff. all parameters you might include should be checked very well.
Related
I am trying to execute a C program using the shell_exec command, which needs arguments to be passed. It is working for one input, but not working for others. I tried to run the C program through terminal, it is working for all the inputs.
This is my execprog.php file. I have to give 2 inputs as command line arguments to file. /var/www/project is the path.
$query = "/var/www/project/./a.out /var/www/project/constraints.txt /var/www/project/constraints_keyword.txt /var/www/project/FIB.txt /var/www/project/ANS.txt";
echo $query;
$var = shell_exec($query);
echo $var;
<?php
$query = "/var/www/project/./a.out";
$arguments = array
(
'/var/www/project/constraints.txt',
'/var/www/project/constraints_keyword.txt',
'/var/www/project/FIB.txt',
'/var/www/project/ANS.txt'
);
$string = '';
for($i=0;$i<count($arguments);$i++)
$string.= ' %s';
$command = vsprintf("{$query}{$string}", $arguments);
$var = shell_exec($command);
echo $var;
As you it works on the terminal and not on apache then apache's php.ini file may be disabling the use of shell_exec().
See http://www.php.net/manual/en/ini.core.php#ini.disable-functions
Your apache's php.ini file may look something like
disable_functions=exec,passthru,shell_exec,system,proc_open,popen
Remove shell_exec from this list and restart the web server, although this is a security risk and I don't recommend it.
In general functions such as exec,shell_exec and system are always used to execute the external programs. Even a shell command can also be executed. If these two functions are enabled then a user can enter any command as input and execute into your server. So usually people disable in apache config as disable_functions to secure their site.
It works for me - Here is test run
Sample test c code
[akshay#gold tmp]$ cat test.c
#include<stdio.h>
int main(int args, char *argv[]) {
int i = 0;
for (i = 0; i < args; i++)
printf("Arg[%d] = %s\n",i, argv[i]);
return 0;
}
Compile
[akshay#gold tmp]$ gcc test.c
Sample php script
[akshay#gold tmp]$ cat test.php
<?php
$query = "/tmp/./a.out /var/www/project/constraints.txt /var/www/project/constraints_keyword.txt /var/www/project/FIB.txt /var/www/project/ANS.txt";
$var = shell_exec($query);
echo $var;
?>
Execution and output
[akshay#gold tmp]$ php test.php
Arg[0] = /tmp/./a.out
Arg[1] = /var/www/project/constraints.txt
Arg[2] = /var/www/project/constraints_keyword.txt
Arg[3] = /var/www/project/FIB.txt
Arg[4] = /var/www/project/ANS.txt
I login to AS400 and go to QSHELL(STRQSH). After that I pasted the following command and run it in QSHELL
system "IVIEW/PRTDISTL APPNAME(MYJOBLOG) STMF(TEST_89.PDF) HOLD(*NO)"
It works. But when I execute the above command using PHP script as the follow
$cmd = "IVIEW/PRTDISTL APPNAME(MYJOBLOG) STMF(TEST_89.PDF) HOLD(*NO)";
$cmd = 'system "' . $cmd . '"';
$output = array();
exec ($cmd, $output , $retVal);
echo "return value code: " . $retVal;
It returns error code of 255. Please help me how to fix this issue. Thanks
Try the PHP Toolkit for i5/OS. There is a older Redbook that describes it: http://www.redbooks.ibm.com/redbooks/pdfs/sg247327.pdf
An example from there:
<HTML>
<?php
/* Connect to server */
$conn = i5_connect("localhost", "PHPUSER", "MYPASSWORD");
if (!$conn)
die("<br>Connection using \"localhost\" with USERID and PASSWORD failed. Error
number =".i5_errno()." msg=".i5_errormsg())."<br>";
else
echo "<br>Connection using \"localhost\" with USERID and PASSWORD OK!<br>\n";
/* Call Retrieve Network Attributes command */
$ret = i5_command("rtvneta", array(), array("sysname" => "sysn", "lclnetid"=>"lclnet"));
if (!$ret) die("<br>rtvneta command failed. errno=".i5_errno()."
msg=".i5_errormsg());
print "<h1><b>Results of \"rtvneta\" command </b></h1><br>" ;
print "System Name : $sysn<br>" ;
print "Local Net ID : $lclnet<br>" ;
/* Close connection */
i5_close($conn);
?>
</HTML>
I see two pontential problems:
PHP runs in PASE, not in QShell. Try to manually run your call from QP2TERM.
User rights. Make sure QTMHHTTP (or whatever user runs PHP) has the apropriate rights to the program you try to call.
You can check for further information in stdout, which you should have in $output, and your PHP-server joblog as mentioned at IBM i information center (possibly needing the flag 'system -K').
I have the fallowing code
<html>
<body>
<?php
if ($_GET['run']) {
# This code will run if ?run=true is set.
echo "Hello";
exec ("chmod a+x ps.sh");
exec ("sh ps.sh");
}
?>
<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
Click Me!
Now i want to know exec ("chmod a+x ps.sh") is executing properly or not. What should i do??
Have a look at the documentation:
string exec ( string $command [, array &$output [, int &$return_var ]] )
...
return_var
If the return_var argument is present along with the output argument,
then the return status of the executed command will be written to this
variable.
So just check if the return code is not equals zero:
exec ("chmod a+x ps.sh", $output, $return);
if ($return != 0) {
// An error occured, fallback or whatever
...
}
exec(..., $output, $return);
if ($return != 0) {
// something went wrong
}
Capture the return code by supplying a variable name for the third parameter. If that variable contains 0 afterwards, all is good. If it's anything other than 0, something went wrong.
exec() accepts other parameters. The second one is output, it allows you to see the output of your command.
In the case of a chmod, a correct output is nothing.
The third argument of exec() is the return status. It should be 0 in case of success.
You should then do something like :
exec ("chmod a+x ps.sh", $out, $value);
if(!empty($out) || $value != 0)
{
#there was an error
}
Note : you don't have to initialize $out or $value beforehand, PHP will create them as you use them.
I know how to check if one instance of process is running but how do I check a particular process running with different parameters for example
/usr/local/bin/foo --config /home/config1.txt
/usr/local/bin/foo --config /home/config2.txt
Following code checks only process name, how do I check if a process is running with a particular parameter?
function is_process_running ($process_name) {
$result = array();
exec("/sbin/pidof {$process_name}", $result);
if(is_array($result) && isset($result[0]) && $result[0] >= 1) {
return true;
}
return false;
}
is_process_running('/usr/local/bin/foo --config /home/config1.txt') returns true
is_process_running('/usr/local/bin/foo --config /home/config3.txt') returns false
function is_process_running ($process_name) {
$result = array();
exec("ps -Af | grep {$process_name}", $result);
// A loop that checks for your result and also checks
// that the result isn't the grep command called
// ps -ax | grep firefox asdfasd
// returns grep --color=auto firefox asdfasd
return false;
}
Give it a try. The flag 'f' modifies the output so includes the full call.
Try this bash command to get more details about the processes:
ps ax | grep YourProcesName
I know that at least java processes would should its Paramerers
I am getting the following error when I commit to a repository:
post-commit hook failed (exit code 255) with no output.
The post commit code is as follows:
#echo off
setlocal enableextensions
set REPOS=%1
set REV=%2
set TEMPFILE=C:\TEMP\%REV%.txt
set LOGFILE=D:\svn\logs\mysite\post_commit.log
set MANTIS_PATH="D:\home\mantis"
"C:\Program Files\SlikSvn\bin\svnlook" author %REPOS% -r %REV% >> %TEMPFILE% 2>>C:\TEMP\err.txt
"C:\Program Files\SlikSvn\bin\svnlook" date %REPOS% -r %REV% >> %TEMPFILE% 2>>C:\TEMP\err.txt
"C:\Program Files\SlikSvn\bin\svnlook" changed %REPOS% -r %REV% >> %TEMPFILE% 2>>C:\TEMP\err.txt
echo revision:[%REV%] >> %TEMPFILE% 2>>C:\TEMP\err.txt
"C:\Program Files\SlikSvn\bin\svnlook" log %REPOS% -r %REV% >> %TEMPFILE% 2>>C:\TEMP\err.txt
date /T >> %LOGFILE% 2>>C:\TEMP\err.txt
time /T >> %LOGFILE% 2>>C:\TEMP\err.txt
D:\wamp\bin\php\php5.3.0\php.exe %MANTIS_PATH%\scripts\checkin.php < %TEMPFILE% >> %LOGFILE% 2>>C:\TEMP\err.txt
As you can see there is extensive error logging, but the error log never gets populated (unless I put in an obvious error like a missing path)
Both dev and live environments are Windows
The line that seems to case the error is the final one - where PHP is called. When I take this out the commit works without issue
When I run this manually on the command line it works and doesn't show any (obvious) errors. Afaik svn runs under SYSTEM by default user which is high enough privilleges to not have to worry
As you've probably made out, this is hooking up my SVN checkins with Mantis bug tracker. The interesting thing is that the PHP script is being executed, i.e. the notes are being added to bugs and the auto-closing of issues is working. It just seems to erroring for some reason.
I am using TortoiseSVN to commit, but using command line (SlikSVN) I get the same output:
svn ci -m "this is a test" test.txt
Sending test.txt
Transmitting file data .
Committed revision 1337.
Warning: post-commit hook failed (exit code 255) with no output.
Mantis' checkin.php looks like this (some general comments removed for readability):
#!/usr/bin/php -q
<?php
global $g_bypass_headers;
$g_bypass_headers = 1;
require_once( dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'core.php' );
# Make sure this script doesn't run via the webserver
if( php_sapi_name() != 'cli' ) {
echo "checkin.php is not allowed to run through the webserver.\n";
exit( 1 );
}
# Check that the username is set and exists
$t_username = config_get( 'source_control_account' );
if( is_blank( $t_username ) || ( user_get_id_by_name( $t_username ) === false ) ) {
echo "Invalid source control account ('$t_username').\n";
exit( 1 );
}
if( !defined( "STDIN" ) ) {
define( "STDIN", fopen( 'php://stdin', 'r' ) );
}
# Detect references to issues + concat all lines to have the comment log.
$t_commit_regexp = config_get( 'source_control_regexp' );
$t_commit_fixed_regexp = config_get( 'source_control_fixed_regexp' );
$t_comment = '';
$t_issues = array();
$t_fixed_issues = array();
while(( $t_line = fgets( STDIN, 1024 ) ) ) {
$t_comment .= $t_line;
if( preg_match_all( $t_commit_regexp, $t_line, $t_matches ) ) {
$t_count = count( $t_matches[0] );
for( $i = 0;$i < $t_count;++$i ) {
$t_issues[] = $t_matches[1][$i];
}
}
if( preg_match_all( $t_commit_fixed_regexp, $t_line, $t_matches ) ) {
$t_count = count( $t_matches[0] );
for( $i = 0;$i < $t_count;++$i ) {
$t_fixed_issues[] = $t_matches[1][$i];
}
}
}
# If no issues found, then no work to do.
if(( count( $t_issues ) == 0 ) && ( count( $t_fixed_issues ) == 0 ) ) {
echo "Comment does not reference any issues.\n";
exit( 0 );
}
# Login as source control user
if( !auth_attempt_script_login( $t_username ) ) {
echo "Unable to login\n";
exit( 1 );
}
# history parameters are reserved for future use.
$t_history_old_value = '';
$t_history_new_value = '';
# add note to each bug only once
$t_issues = array_unique( $t_issues );
$t_fixed_issues = array_unique( $t_fixed_issues );
# Call the custom function to register the checkin on each issue.
foreach( $t_issues as $t_issue_id ) {
if( !in_array( $t_issue_id, $t_fixed_issues ) ) {
helper_call_custom_function( 'checkin', array( $t_issue_id, $t_comment, $t_history_old_value, $t_history_new_value, false ) );
}
}
foreach( $t_fixed_issues as $t_issue_id ) {
helper_call_custom_function( 'checkin', array( $t_issue_id, $t_comment, $t_history_old_value, $t_history_new_value, true ) );
}
exit( 0 );
I usually recommend not using batch scripts for Subversion hooks. There are a ton of free, open source, scripting languages that are more powerful and easier to work with. Heck, I don't even use BASH for hook scripts.
This is especially true since hook scripts only have to run on the server and no where else. That means you only have to get it working on a single platform.
That being said, you'll need to do some debugging. Your hook script fails via the svn commit command line. That means it's not a Tortoise related issue. It's a hook script issue. Since this is a post hook script, try running the script itself from the command line and pass to it the repository and revision from the command line:
C> post-commit C:\repos\my_repo 2323
Does that work?
The problem is that you're not printing anything on STDERR, so there's nothing for Subversion to print. You're capturing STDERR in a file. Remove the 2>>C:\TEMP\err.txt, so you can see the error output. In Python, BASH, and Perl, I could capture STDERR, and still print it out, but I'm not sure how to do this in Batch script.
This would allow you to see the error output, and maybe help you determine where your script is failing. Is it failing before it calls your PHP script? Is something going on in your PHP script? Are there assumptions you're making which aren't necessarily valid?
Also, don't merely echo stuff in your PHP hook. Subversion doesn't print anything on STDOUT no matter if the hook succeeds or fails. Use fputs(STDERR, 'My Error Message'); instead of echo or print. This will send your error message to STDERR and Subversion will print it out if your hook script fails.
Hope this helps.
I was running in to this as well. Make sure your script file permissions are set to Executable.