<?php

/**
 * @file formater_mail.php
 * @brief Page qui reformate un mail pour écrire une réponse au format texte
 *
 * @author hughes monget
 * @see http://monget.com/
 */


define('STR_MAIL',    'mail');
define('STR_ACTION',  'action');
define('STR_QUOTE',   'quote');
define('STR_WRAP',    'wrap');
define('INT_WRAP',    70);
define('STR_CHECKED'' checked="checked"');


$bool_action = isset($_REQUEST[STR_ACTION]) && isset($_REQUEST[STR_MAIL]);
$bool_quote  TRUE;
$int_wrap    INT_WRAP;

$str_mail     '';
$str_formater '';

if (
$bool_action)
{
    
$str_mail $_REQUEST[STR_MAIL];
    if (
get_magic_quotes_gpc())
    {
        
$str_mail stripslashes($str_mail);
    }
    
$bool_quote = isset($_REQUEST[STR_QUOTE]);
    if (isset(
$_REQUEST[STR_WRAP]))
    {
        
$int_wrap intval($_REQUEST[STR_WRAP]);
        if (!(
$int_wrap 0))
        {
            
$int_wrap INT_WRAP;
        }
    }

    
$str_formater  $str_mail;
    
$str_formater  strip_tags($str_formater); // On supprime les balises html.
    
$str_formater  str_replace(array("\r\n""\r"), "\n"$str_formater); // On normalise les sauts de lignes.

    
foreach (array('’' => "'"'&#8216;' => '\'''&#8217;' => '\'''&#339;' => 'oe''&#8230;' => '...''&#8364;' => '€') as $str_entite => $str_caractere)
    {
        
$str_formater  str_replace(html_entity_decode($str_entite), $str_caractere$str_formater);
    }
    
$str_formater  str_replace('’''\''$str_formater);

    
$str_formater  preg_replace('/[\t ]+/m'' '$str_formater); // On réduit les espaces multiples.

    
$str_formater  wordwrap($str_formater$int_wrap); // On wrappe.

    
$arr_str_ligne preg_split('/\n/'$str_formater);
    
$arr_str_ligne array_map('trim'$arr_str_ligne);
    
$str_formater  implode("\n"$arr_str_ligne);
    
$str_formater  preg_replace('/\n{3,}/m'"\n\n"$str_formater); // On réduit les sauts de lignes multiples.

    
if ($bool_quote)
    {
        
$str_formater  preg_replace('/^.*$/m''> \\0'$str_formater);
    }

    
//$str_mail     = htmlspecialchars($str_mail);
    
$str_formater htmlspecialchars($str_formaterENT_QUOTES'ISO-8859-15');

    foreach (array(
'&amp;#8217;' => '\'') as $str_entite => $str_caractere)
    {
        
$str_formater  str_replace($str_entite$str_caractere$str_formater);
    }

}

$str_quote '';
if (
$bool_quote)
{
    
$str_quote STR_CHECKED;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <title>Formater un mail</title>
        <style type="text/css">
        <!--
        body *
        {
            font-family: monospace;
            font-size: 8pt;
        }

        textarea
        {
            height: 200px;
            width: 600px;
        }

        input.submit
        {
            width: 100px;
            display: block;
        }

        input#<?php print STR_WRAP?>
        {
            width: 50px;
        }

        -->
        </style>
        <script type="text/javascript">
        <!--;
            function on_load()
            {
                if (document && document.getElementById)
                {
                    var obj_pattern = document.getElementById('<?php print STR_MAIL?>');
                    if (obj_pattern && obj_pattern.focus)
                    {
                        obj_pattern.focus();
                    }
                }
            }

        //-->
        </script>
    </head>
    <body onload="on_load();">
        <form method="post">
            <input type="hidden" name="<?php echo STR_ACTION?>" value="<?php echo STR_ACTION?>" />
            <textarea id="<?php print STR_MAIL?>" name="<?php print STR_MAIL?>"><?php print $str_mail?></textarea>
            <div>
                <label for="<?php print STR_QUOTE?>">Quote:</label>
                <input id="<?php print STR_QUOTE?>" name="<?php print STR_QUOTE?>" type="checkbox" <?php print $str_quote?> />
                <label for="<?php print STR_WRAP?>">Wrap:</label>
                <input id="<?php print STR_WRAP?>" name="<?php print STR_WRAP?>" type="text" value="<?php print $int_wrap?>" />
            </div>
            <input type="submit" class="submit" value="formater" />
        </form>
<?php
if ($bool_action)
{
    
printf('<textarea onclick="this.select();">%s</textarea>'$str_formater);
}

echo 
'<hr />';
highlight_file(__FILE__);

?>
    </body>
</html>