PHP escapeshellarg() doesn't work - php

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");

Related

PHP won't echo html code to show a link on page

I have a simple echo statement that's supposed to display a link on the web page, but all it's doing is showing exactly "<a href=website.com>Link</a>" (without the quotes). To me, this should work with no problem. I thought maybe it's because the HTML for the website is in one file while the PHP is in another file.
foreach ($output as $output)
{
echo 'DATE: ' . $output['date'] . "\n";
echo 'TO: ' . $output['to'] . "\n";
echo 'FROM: ' . $output['from'] . "\n";
echo 'SUBJECT: ' . $output['subject'] . "\n";
echo "<a href=website.com>Link</a>\n\n";
}
Your loop isn't a valid one, you have $output as $output.
I also suggest printing $output before looping through it, to see if it even contains what you think it should.
In your 'a' tag, the actual URL needs to be in quotes
<a href='http://www.website.com'>

PHP Echo with System Command

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);

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.

Remove text from string php

I have a table called 'filename'. I try to output <a> tags in a loop from it like this:
<?php
while($sermon = mysql_fetch_assoc($sermonsQ)) {
echo '<a href="admin/'. $sermon ['filename'] . '">';
echo 'download</a></td>';
}
Current problem is, that $sermon['filename'] containts a leading path like path/test.mp3. But I need only the filename without the path, like test.mp3. How can I do this?
Use basename() for that. It will return the filename without the leading path:
basename($sermon ['filename'])
You can use path_info()
<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?>
also mysql_* functions are depracated so you shouldn't use them for example to PDO or mysqli
<?php
while($sermon = mysql_fetch_assoc($sermonsQ)) {
$filename = explode('/',$sermon ['filename']);
echo '<a href="admin/'. $filename[1] . '">';
echo 'download</a></td>';
}

Categories