<?
set_time_limit(60);

class module extends controller {

    var $params = array("login", "password", "protocol", "server", "port");
    var $isNeedSession = false;
    var $isNeedMysql = false;

    function run() {
    
//    sleep(2);
/*            return array("status" => "data",
                         "data" => array(array("from"=>"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "subj"=>"ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt") ),
                         "total" => 1);
*/
        if(@$conn = imap_open('{'.$this->server.":".$this->port.$this->protocol.'}INBOX', $this->login, $this->password)) {
            $total = imap_num_msg($conn);

            if($total > 0) {
                $cnt = ($total > 20 ? 20 : $total) + 1;

                $res = array();
                for ($i=1; $i<$cnt; $i++) {
                    $header = imap_headerinfo($conn, $i);
                    $res[] = array("from" => $header->fromaddress,
                                   "subj" => $header->subject);
                }
                return array("status" => "data",
                             "data"   => $res,
                             "total"  => $total);
            } else {
                return array("status" => "empty");
            } 
            
        } else {
            return array("status" => "error");
        }
    }

}
?>