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