PHP Echo with System Command - php

i need to call a variable ($id) within a php system command. I'm automating a solr curl delete command, and it works with a static id, but i need to echo the $id so it deletes the correct document.
code is:
<?php
echo '<pre>';
$last_line = system('curl http://localhost:8983/solr/imagedb/update?commit=true --data \'<delete><query>id\:<?php echo $id; ?></query></delete>\' -H \'Content-type:text/xml; charset=utf-8\'', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
The code shows no error but the document is not deleted so it is not grabbing the $id variable. Its an echo within a php system command. How to make it work?

You can't use <?php echo $id; ?> inside a string. That only works when you're out of PHP execution mode. Use string concatenation.
$last_line = system('curl http://localhost:8983/solr/imagedb/update?commit=true --data \'<delete><query>id\:' . $id . '</query></delete>\' -H \'Content-type:text/xml; charset=utf-8\'', $retval);

Remove php tags and enclose a variable in single quotes in your system call. Now your variable along with php tags is passed as a string.
so update this line:
$last_line = system('curl http://localhost:8983/solr/imagedb/update?commit=true --data \'<delete><query>id\:<?php echo $id; ?></query></delete>\' -H \'Content-type:text/xml; charset=utf-8\'', $retval);
to:
$last_line = system('curl http://localhost:8983/solr/imagedb/update?commit=true --data \'<delete><query>id\:'.$id.'</query></delete>\' -H \'Content-type:text/xml; charset=utf-8\'', $retval);

Related

PHP escapeshellarg() doesn't work

On the first shell_exec() I encrypt a message and on the second shell_exec() I want to decrypt my cypher. Encrypt and decrypt functions work well. It seems that first shell_exec() returns a data type that can't be used as a variable in the second shell_exec(). What can I do?
<?php
$plaintext = "unpuzzle";
echo '<p>'.$plaintext.'</p>';
$criptotext = shell_exec('java DES '. escapeshellarg($plaintext) .' 1');
echo '<p>Criptotext: ' . $criptotext . '</p>';
$plaintext = shell_exec('java DES '. escapeshellarg($criptotext) .' 2');
echo '<p>Plaintext: ' . $plaintext . '</p>';
?>
shell_exec returns the output from the command, which includes the newline at the end. You need to remove the newline before using this as the cryptotext.
$criptotext = rtrim(shell_exec('java DES ' . escapeshellarg($plaintext) . ' 1'), "\r\n");

Problems while invoking swi-prolog from PHP

I'm trying to execute a swi-prolog file from PHP, but when i try to run this code, nothing is executed. This my code:
<HTML>
<HEAD>
<TITLE>Calling SWI-Prolog from PHP (short)</TITLE>
</HEAD>
<body>
<H1>Calling SWI-Prolog from PHP (short)</H1>
<?
$cmd = "nice -n15 /C:/Program Files/swipl/bin/swipl-win.exe -f test.pl -g test,halt";
?>
<P>
<PRE>
<?
system( $cmd );
echo "\n";
$output = exec( $cmd );
echo $output;
echo "\n";
exec( $cmd, $output );
print_r( $output );
echo "\n";
$output = shell_exec( $cmd );
echo $output;
echo "\n";
?>
</PRE>
</P>
</body>
</HTML>
When I run the php file from my server, it only shows the string Calling SWI-Prolog from PHP (short).
Activate error reporting and you probably will see some info showing what went wrong.
Add this at the beginning of the file:
<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);
?>
When done debugging you should remove the lines. You should never print error output in a productive environment.

PHP exec returns "array" instead of the result of my command

I'm trying to build a web terminal emulator on my local webserver (openwrt), every time I execute the command, the result always says "array", for instance when I execute uptime it doesn't give me the up time of my webserver but instead it gives me array. This is my script:
<?php
..............................
if($_POST['command'])
{
$command = $_POST['command'];
exec("$command 2>&1 &", $output);
echo $command;
}
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "Command:<br><input type=\"text\" autofocus name=\"command\" size=\"15\" value=\"\"/><br>";
echo '<br>Result :<br><pre>
<div id="show" style="font-size: 11px; word-wrap: break-word; width:550px;height:200px;border:0px solid #000;text-align:left; overflow-y: scroll;">
'.$output.'</div>';
echo '<input type="submit" name="kill" value="Kill Command" />';
echo "</form></div>";
.........................
?>
I want the $output to return the result of my command.
Right now, the $output always returns array no matter what type of command I execute, how should I fix this?
It is supposed to return an array as it is written in the documentation.
If you want to print the array line by line use a code like this:
<?php
exec("$command 2>&1 &", $output);
foreach ($output as $line) {
echo "$line\n";
}
Option 1: You can use implode to combine the array result to a string.
exec("$command 2>&1 &", $output);
echo implode(PHP_EOL, $output);
Option 2: You can use shell_exec which return the complete output as a string.
echo shell_exec("$command 2>&1 &");

Getting console output in Laravel

I need to capture the output of a console command to be sent by email as well when requested. How can I do this?
How do I get the output generated from the following $this->info() calls?
$r = processData();
$this->info("\nSubmitted data:");
$this->info("SubmissionId: " . $r['submission_id']);
$this->info("Status: " . $r['status']);
Decided to just replace the $this->info() calls with a simple echo command and output buffer control. Looks good enough in the console and catches the data requested for emailing.
Example:
$r = processData();
if ($this->option('email-results'))
ob_start();
echo "\nSubmitted data:";
echo "\nSubmissionId: " . $r['submission_id'];
echo "\nStatus: " . $r['status'];
if ($this->option('email-results')) {
mail(
$this->option('email-results'),
'Results on ' . $start_time->toDateTimeString(),
ob_get_contents()
);
ob_end_flush();
}
an Artisan method could help:
\Illuminate\Support\Facades\Artisan::output()

Php - System function - /usr/bin/echo Hello : No such file or directory

I'm a newbie to PhP.
When I run a linux terminal command using System function in php, I'm getting errors in error_log.
Here is my code:
if(isset($_POST['submit_button']))
{
$name=$_POST['User_name']; // here $name contains 'John'
echo '<pre>';
$command="/usr/bin/echo $name";
$command1="'".$command."'";
$last_line = system($command1, $retval);
echo '</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
}
When I run this code, I'm getting the following errors:
in browser - giving code : 127
in /var/log/httpd/error_log file - sh: /usr/bin/echo John: No such file or directory
Am I missing anything?
Thanks in advance.
Try with only echo, and not with /usr/bin/echo
Change your command variable declaration as follow :
$command="echo $name";
$command="/usr/bin/echo $name";
You just want to add the value of the $name variable to the path right? If so just write this as
$command="/usr/bin/$name";
php will parse variables in strings between double qoutes, see string.parsing
Finally, i solved like this error with the help of escapeshellarg()
Here is Updated code..
if(isset($_POST['submit_button']))
{
$name=$_POST['User_name']; // here $name contains 'John'
echo '<pre>';
$last_line = system('/usr/bin/echo'.escapeshellarg($name));
// here escapeshellarg() does the trick
echo '</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
}
Thank you all for your immediate response.

Categories