Fix UTF-8 saving from the API

Fix out of bounds if the email was not signed
This commit is contained in:
Némunaire 2012-02-29 15:03:47 +01:00
parent 0838f1c15b
commit 7c15c88657
2 changed files with 39 additions and 13 deletions

View File

@ -3,36 +3,38 @@
require_once("common.php"); require_once("common.php");
// Search an author (in the first arg)
if (isset($argv[1])) if (isset($argv[1]))
{
$ffrom = $argv[1]; $ffrom = $argv[1];
if (preg_match("#([^<]+) <#ui", $argv[1], $out))
$from = $out[1];
else else
$from = $argv[1]; $ffrom = "";
}
else
$from = $ffrom = "";
// Search a title for the paste (in the second arg)
if (isset($argv[2])) if (isset($argv[2]))
$subject = $argv[2]; $subject = $argv[2];
else else
$subject = ""; $subject = "";
// Receive mail content
$content = file("php://stdin"); $content = file("php://stdin");
$cnt = array(); $cnt = array();
$boundary = null; $boundary = null;
$pass = false; $pass = false;
$i = -1; $i = -1;
foreach($content as $k => $line) foreach($content as $k => $line)
{ {
// Separate body email content
if (substr($line, 0, 2 + strlen($boundary)) == "--".$boundary) if (substr($line, 0, 2 + strlen($boundary)) == "--".$boundary)
{ {
$cnt[] = ""; $cnt[] = "";
$i++; $i++;
$pass = true; $pass = true;
} }
// Don't save headers
else if (($pass || empty($boundary)) && (trim($line) == "" || !empty($cnt[$i]))) else if (($pass || empty($boundary)) && (trim($line) == "" || !empty($cnt[$i])))
{ {
if ($i < 0) if ($i < 0)
@ -42,22 +44,43 @@ foreach($content as $k => $line)
} }
$cnt[$i] .= $line; $cnt[$i] .= $line;
} }
else if (preg_match("#Content-Type: [^;]+; boundary=\"(.+)\"#", $line, $out))
// Save email part separator
else if (preg_match("#^Content-Type: [^;]+; boundary=\"(.+)\"#", $line, $out))
$boundary = $out[1]; $boundary = $out[1];
// Read From field if $ffrom is empty
else if (empty($ffrom) && preg_match("#^From: (.+)#", $line, $out))
$ffrom = $out[1];
// Read Subject field if $subject is empty
else if (empty($subject) && preg_match("#^Subject: (.+)#", $line, $out))
$subject = $out[1];
} }
// Extract username instead of email adress if it exists
if (preg_match("#([^<]+) <#ui", $ffrom, $out))
$from = $out[1];
else
$from = $ffrom;
// Create the paste
$paste = new Paste(); $paste = new Paste();
$paste->title = $subject; $paste->title = $subject;
$paste->author = $from; $paste->author = $from;
$paste->date = time(); $paste->date = time();
$paste->content = trim($cnt[$i-1]); $paste->content = utf8_encode(trim($cnt[max(0,$i-1)]));
// Save the paste and give read right to all users (if mail user is different from php one)
$link = $paste->save(); $link = $paste->save();
chmod(Paste::get_path($paste->filename), 0666); chmod(Paste::get_path($paste->filename), 0644);
// Send confirmation email
$headers = 'From: paste@p0m.fr' . "\r\n" . $headers = 'From: paste@p0m.fr' . "\r\n" .
'Content-Type: text/plain; charset="utf-8"' . "\r\n" . 'Content-Type: text/plain; charset="utf-8"' . "\r\n" .
'X-Mailer: Paste.p0m.fr'; 'X-Mailer: '.ucfirst(HTTP_URL);
mail($ffrom, "Re: ".$subject, "Bonjour,\n\nVotre paste a bien été publié à l'adresse suivante :\nhttp://".HTTP_URL."/?".$link."\n\n-- \n".HTTP_URL, $headers);
mail($ffrom, "Re: ".$subject, "Bonjour,\n\nVotre paste a bien été publié à l'adresse suivante :\nhttp://paste.p0m.fr/?".$link."\n\n-- \npaste.p0m.fr", $headers);
?> ?>

View File

@ -16,6 +16,9 @@ define("NB_CHAR", 5);
// can adjust the minimum // can adjust the minimum
define("ALLOW_NB_MIN", 5); define("ALLOW_NB_MIN", 5);
// The adress of this service
define("HTTP_URL", "paste.p0m.fr");
/********************************************* /*********************************************