<?php

/** // #!/usr/local/bin/php
 * @file pop_killer.cli.php
 * @brief Ce script permet de supprimer tous les messages d'un compte pop.
 *
 * @see http://abcdrfc.free.fr/rfc-vf/rfc1939.html
 * @author hughes monget
 * @see http://monget.com/
 */

set_time_limit(0);

/**
 * CONST
 */

define('ENUM_SERVER',            'ENUM_SERVER');
define('ENUM_LOGIN',             'ENUM_LOGIN');
define('ENUM_PASSWORD',          'ENUM_PASSWORD');

define('INT_BUFFER_LENGTH',      1024);
define('INT_POP_PORT_NUMBER',    110);

define('STR_OK',                 '+OK');
define('STR_ERROR',              '+ERR');



/**
 * FUNCTIONS
 */

function write($s)
{
    
$ss preg_replace_callback('/^(PASS )(.*)$/'create_function('$a''return ($a[1].str_repeat(\'*\', strlen($a[2])));'), $s);
    print(
$ss);
    return 
$s;
}


function 
add_account(&$arr_arr_str_account_list$str_server$str_login$str_password)
{
    
$arr_arr_str_account_list[] = array
    (
        
'ENUM_SERVER'   => $str_server,
        
'ENUM_LOGIN'    => $str_login,
        
'ENUM_PASSWORD' => $str_password
    
);
}


function 
kill_account($arr_arr_str_account_list)
{
    foreach (
$arr_arr_str_account_list as $arr_str_account)
    {
        
$res_server fsockopen($arr_str_account[ENUM_SERVER], INT_POP_PORT_NUMBER$int_error_number$str_error_message);
        if (!
$res_server)
        {
            print(
'Connexion problem "' $arr_str_account[ENUM_SERVER] . '" : ' $str_error_message ' (' strval($int_error_number) . ')' PHP_EOL);
        }
        else
        {
            print 
fgets($res_serverINT_BUFFER_LENGTH);
            
fwrite($res_serverwrite(sprintf("USER %s\r\n"$arr_str_account[ENUM_LOGIN])));
            print 
fgets($res_serverINT_BUFFER_LENGTH);
            
fwrite($res_serverwrite(sprintf("PASS %s\r\n"$arr_str_account[ENUM_PASSWORD])));
            
$str_answer write(fgets($res_serverINT_BUFFER_LENGTH));

            if (
strpos($str_answerSTR_OK) !== FALSE)
            {
                
//$int_message_count = intval(preg_replace('#^.*?([[:digit:]]+).*$#', '\\1', $str_answer));
                // Récupération du nombre de messages.
                
fwrite($res_serverwrite("STAT\r\n"));
                
$str_answer write(fgets($res_serverINT_BUFFER_LENGTH));

                if (
preg_match('#[[:digit:]]+#'$str_answer$arr_str_match))
                {
                    
$int_message_count intval($arr_str_match[0]);
                    if (
$int_message_count 0)
                    {
                        for (
$ii 1$ii <= $int_message_count$ii++)
                        {
                            
fwrite($res_serverwrite(sprintf("DELE %d\r\n"$ii)));
                            
$str_answer write(fgets($res_serverINT_BUFFER_LENGTH));
                        }
                    }
                }
            }
            
fwrite($res_serverwrite("QUIT\r\n"));
            
fclose($res_server);
        }
    }
}


/**
 * MAIN
 */
function main()
{
    
$arr_arr_str_liste = array();
    
add_account($arr_arr_str_account_list'pop.laposte.net''mister.kludge''toto2001');
    
kill_account($arr_arr_str_account_list);
}

$str_serveur  = (empty($_REQUEST['serveur']))  ? '' trim($_REQUEST['serveur']);
$str_compte   = (empty($_REQUEST['compte']))   ? '' trim($_REQUEST['compte']);
$str_password = (empty($_REQUEST['password'])) ? '' trim($_REQUEST['password']);


?>
<!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>
        <title>Supprimer tous les messages d'un compte POP</title>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <style type="text/css">
        <!--
        table
        {
            border-collapse: collapse;
        }
        td
        {
            border: 1px solid #000;
            padding: 5px 5px;
        }
        .texte
        {
            width: 300px;
        }
        -->
        </style>
    </head>
    <body>
        <form method="get" action="">
            <table>
                <tr>
                    <td>
                        <label for="serveur">Serveur POP</label>
                    </td>
                    <td>
                        <input type="text" class="texte" name="serveur" id="serveur" value="<?php echo htmlspecialchars($str_serveur?>" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="compte">Nom du compte</label>
                    </td>
                    <td>
                        <input type="text" class="texte" name="compte" id="compte" value="<?php echo htmlspecialchars($str_compte?>" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="password">Password</label>
                    </td>
                    <td>
                        <input type="password" class="texte" name="password" id="password" value="<?php echo $str_password ?>" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="submit" value="Supprimer" />
                    </td>
                </tr>
            </table>
        </form>
<?php

if ($str_serveur && $str_compte && $str_password)
{
    echo 
'<pre>';
    
$arr_arr_str_liste = array();
    
add_account($arr_arr_str_account_list$str_serveur$str_compte$str_password);
    
kill_account($arr_arr_str_account_list);
    echo 
'</pre>';

}

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

?>
    </body>
</html>