Note that there are some explanatory texts on larger screens.

plurals
  1. POEscaping Shell echos
    primarykey
    data
    text
    <p>I tried to do my research, there is just an abundance of ways to call shell commands, and even more ways to strip harmful characters that I am coming to stackoverflow for an expert's best recommendation.</p> <p>I was hoping to find something like I've seen other languages so where sending arguments to a command are actually passed through a function, like:</p> <p>do_command("ls", "-l", $Directory);</p> <p>and it will take care of anything harmful in the $Directory variable for you. I haven't quite found this with PHP.</p> <p>This is the code I am working with:</p> <pre><code>&lt;?php session_start(); $AdminEmail = "random_email@gmail.com"; $CatalogEmails = array(""); $QuoteEmails = array(""); $PartsEmails = array(""); $Subject = $_SESSION['Email_Subject']; $Body = $_SESSION['Email_Body']; $Headers = $_SESSION['Email_Headers']; $Type = $_SESSION['Type']; msmtp($AdminEmail, $Subject, $Body, $Headers, "meyers"); if ($Type == "Catalog") { foreach ($CatalogEmails as $AdditionalEmail) { msmtp($AdditionalEmail, $Subject, $Body, $Headers, "meyers"); } } else if ($Type == "Quote") { foreach ($QuoteEmails as $AdditionalEmail) { msmtp($AdditionalEmail, $Subject, $Body, $Headers, "meyers"); } } else if ($Type == "Parts") { foreach ($PartsEmails as $AdditionalEmail) { msmtp($AdditionalEmail, $Subject, $Body, $Headers, "meyers"); } } function msmtp($To, $Subject, $Body, $Headers, $Account) { $Email = "To: $To\nSubject: $Subject\n$Headers\n\n$Body\n"; exec("echo \"$Email\" | msmtp --account=$Account $To"); } session_destroy(); ?&gt; </code></pre> <p>I know there is a built-in PHP mail function that pretty much would take care of this, but I am running multiple SMTP servers and the <code>msmtp</code> is a program I use that sends emails based on the "account" the email will be sent under. In this case it will be the "meyers" account.</p> <p>All the sessions variables contain HTML (<code>&lt;br&gt;</code>'s <code>&lt;b&gt;</code>'s, etc) with some <code>$_POST</code> vars in there as well. I use PHP 5.3 so no magic quotes.</p> <p>I know using an echo is a horrid way, which is why I am coming to stackoverflow. My goal here is that the email will go through despite any kind of crazy character they throw at me. I know the shell/bash is picky-- I assume it's much more than just escaping double quotes.</p> <p>I tried using <code>escapeshellcmd</code> <code>escapeshellarg</code> and <code>htmlentities</code>, they all escape too much or mess up the HTML in the email.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload