home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / Net / IMAP.php < prev    next >
Encoding:
PHP Script  |  2005-12-02  |  50.1 KB  |  1,840 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar>       |
  17. // +----------------------------------------------------------------------+
  18.  
  19.  
  20. require_once 'Net/IMAPProtocol.php';
  21.  
  22.  
  23. /**
  24.  * Provides an implementation of the IMAP protocol using PEAR's
  25.  * Net_Socket:: class.
  26.  *
  27.  * @package Net_IMAP
  28.  * @author  Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar>
  29.  */
  30. class Net_IMAP extends Net_IMAPProtocol {
  31.  
  32.     /**
  33.      * Constructor
  34.      *
  35.      * Instantiates a new Net_SMTP object, overriding any defaults
  36.      * with parameters that are passed in.
  37.      *
  38.      * @param string The server to connect to.
  39.      * @param int The port to connect to.
  40.      * @param string The value to give when sending EHLO or HELO.
  41.      */
  42.  
  43.     function Net_IMAP($host = 'localhost', $port = 143)
  44.     {
  45.         $this->Net_IMAPProtocol();
  46.         $ret = $this->connect( $host , $port );
  47.     }
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.      /**
  55.      * Attempt to connect to the IMAP server located at $host $port
  56.      * @param string $host The IMAP server
  57.      * @param string $port The IMAP port
  58.      *
  59.      *          It is only useful in a very few circunstances
  60.      *          because the contructor already makes this job
  61.      * @return true on success or PEAR_Error
  62.      *
  63.      * @access public
  64.      * @since  1.0
  65.      */
  66.     function connect($host, $port)
  67.     {
  68.         $ret=$this->cmdConnect($host,$port);
  69.         if($ret === true ){
  70.             return $ret;
  71.         }
  72.         if(empty($ret)){
  73.             return new PEAR_Error("Unexpected response on connection");
  74.         }
  75.         if(PEAR::isError($ret) ){
  76.             return $ret;
  77.         }
  78.         if(isset(    $ret["RESPONSE"]["CODE"] ) ){
  79.             if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  80.                 return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  81.             }
  82.         }
  83.         return $ret;
  84.     }
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.     /**
  96.      * Attempt to authenticate to the IMAP server.
  97.      * @param string $user The userid to authenticate as.
  98.      * @param string $pass The password to authenticate with.
  99.      * @param string $useauthenticate true: authenticate using
  100.      *        the IMAP AUTHENTICATE command. false: authenticate using
  101.      *        the IMAP AUTHENTICATE command. 'string': authenticate using
  102.      *        the IMAP AUTHENTICATE command but using the authMethod in 'string'
  103.      * @param boolean $selectMailbox automaticaly select inbox on login (false does not)
  104.      *
  105.      * @return true on success or PEAR_Error
  106.      *
  107.      * @access public
  108.      * @since  1.0
  109.      */
  110.  
  111.     function login($user, $pass, $useauthenticate = true, $selectMailbox=true)
  112.     {
  113.         if ( $useauthenticate ){
  114.             //$useauthenticate is a string if the user hardcodes an AUTHMethod
  115.             // (the user calls $imap->login("user","password","CRAM-MD5"); for example!
  116.  
  117.             $method = is_string( $useauthenticate ) ? $useauthenticate : null;
  118.  
  119.             //Try the selected Auth method
  120.             if ( PEAR::isError( $ret = $this->cmdAuthenticate( $user , $pass , $method  ) ) ) {
  121.                 // Verify the methods that we have in common with the server
  122.                 if(is_array($this->_serverAuthMethods)){
  123.                     $commonMethods=array_intersect ($this->supportedAuthMethods, $this->_serverAuthMethods );
  124.                 }else{
  125.                     $this->_serverAuthMethods=null;
  126.                 }
  127.                 if($this->_serverAuthMethods == null  || count($commonMethods) == 0 || $this->supportedAuthMethods == null ){
  128.                     // The server does not have any auth method, so I try LOGIN
  129.                     if ( PEAR::isError( $ret = $this->cmdLogin( $user, $pass ) ) ) {
  130.                         return $ret;
  131.                     }
  132.                 }else{
  133.                     return $ret;
  134.                 }
  135.             }
  136.             if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  137.                 return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  138.             }
  139.         }else{
  140.             //The user request "PLAIN"  auth, we use the login command
  141.             if ( PEAR::isError( $ret = $this->cmdLogin( $user, $pass ) ) ) {
  142.                 return $ret;
  143.             }
  144.             if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  145.                 return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  146.             }
  147.         }
  148.  
  149.         if($selectMailbox){
  150.             //Select INBOX
  151.             if ( PEAR::isError( $ret=$this->cmdSelect( $this->getCurrentMailbox() ) ) ) {
  152.                 return $ret;
  153.             }
  154.         }
  155.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  156.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  157.         }
  158.         return true;
  159.     }
  160.  
  161.  
  162.  
  163.  
  164.     /*
  165.     * Disconnect function. Sends the QUIT command
  166.     * and closes the socket.
  167.     *
  168.     * @return bool Success/Failure
  169.     */
  170.     function disconnect($expungeOnExit = false)
  171.     {
  172.         if($expungeOnExit){
  173.             $ret=$this->cmdExpunge();
  174.             if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  175.                 $ret=$this->cmdLogout();
  176.                 return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  177.             }
  178.         }
  179.         $ret=$this->cmdLogout();
  180.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  181.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  182.         }
  183.         return true;
  184.     }
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.      /*
  193.     * Changes  the default/current mailbox th $mailbox
  194.     *
  195.     *
  196.     * @return bool Success/Pear_Error Failure
  197.     */
  198.     function selectMailbox($mailbox)
  199.     {
  200.         $ret=$this->cmdSelect($mailbox);
  201.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  202.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  203.         }
  204.         return true;
  205.     }
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.      /*
  214.     * Checks  the mailbox $mailbox
  215.     *
  216.     *
  217.     * @return bool Success/Pear_Error Failure
  218.     */
  219.     function examineMailbox($mailbox)
  220.     {
  221.         $ret=$this->cmdExamine($mailbox);
  222.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  223.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  224.         }
  225.  
  226.         //$ret_aux["EXISTS"]=$ret["PARSED"]["EXISTS"];
  227.         //$ret_aux["RECENT"]=$ret["PARSED"]["RECENT"];
  228.         return $ret;
  229.     }
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.     /*
  239.     * Returns the raw headers of the specified message.
  240.     *
  241.     * @param  $msg_id Message number
  242.     * @return mixed   Either raw headers or false on error
  243.     */
  244.     function getRawHeaders($msg_id)
  245.     {
  246.         $ret=$this->cmdFetch($msg_id, "BODY[HEADER]");
  247.         if(strtoupper( $ret["RESPONSE"]["CODE"]) != "OK" ){
  248.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  249.         }
  250.         $ret=$ret["PARSED"][0]["EXT"]["BODY[HEADER]"]["CONTENT"];
  251.         return $ret;
  252.     }
  253.  
  254.  
  255.  
  256.  
  257.     /*
  258.     * Returns the  headers of the specified message in an
  259.     * associative array. Array keys are the header names, array
  260.     * values are the header values. In the case of multiple headers
  261.     * having the same names, eg Received:, the array value will be
  262.     * an indexed array of all the header values.
  263.     *
  264.     * @param  $msg_id Message number
  265.     * @return mixed   Either array of headers or false on error
  266.     */
  267.     function getParsedHeaders($msg_id)
  268.     {
  269.             $ret=$this->getRawHeaders($msg_id);
  270.  
  271.             $raw_headers = rtrim($ret);
  272.             $raw_headers = preg_replace("/\r\n[ \t]+/", ' ', $raw_headers); // Unfold headers
  273.             $raw_headers = explode("\r\n", $raw_headers);
  274.             foreach ($raw_headers as $value) {
  275.                 $name  = substr($value, 0, $pos = strpos($value, ':'));
  276.                 $value = ltrim(substr($value, $pos + 1));
  277.                 if (isset($headers[$name]) AND is_array($headers[$name])) {
  278.                     $headers[$name][] = $value;
  279.                 } elseif (isset($headers[$name])) {
  280.                     $headers[$name] = array($headers[$name], $value);
  281.                 } else {
  282.                     $headers[$name] = $value;
  283.                 }
  284.             }
  285.  
  286.             return $headers;
  287.     }
  288.  
  289.  
  290.  
  291.  
  292.  
  293.     /*
  294.     * Returns an array containing the message ID, the size and the UID
  295.     * of each message selected.
  296.     * message selection can be a valid IMAP command, a number or an array of
  297.     * messages
  298.     *
  299.     * @param  $msg_id Message number
  300.     * @return mixed   Either array of message data or PearError on error
  301.     */
  302.  
  303.     function getMessagesList($msg_id = null)
  304.     {
  305.         if( $msg_id != null){
  306.             if(is_array($msg_id)){
  307.                 $message_set=$this->_getSearchListFromArray($msg_id);
  308.             }else{
  309.                 $message_set=$msg_id;
  310.             }
  311.         }else{
  312.             $message_set="1:*";
  313.         }
  314.         $ret=$this->cmdFetch($message_set,"(RFC822.SIZE UID)");
  315.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  316.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  317.         }
  318.         foreach($ret["PARSED"] as $msg){
  319.             $ret_aux[]=array("msg_id"=>$msg["NRO"],"size" => $msg["EXT"]["RFC822.SIZE"],"uidl"=> $msg["EXT"]["UID"]);
  320.         }
  321.         return $ret_aux;
  322.     }
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.     function getSummary($msg_id = null)
  334.     {
  335.        if( $msg_id != null){
  336.             if(is_array($msg_id)){
  337.                 $message_set=$this->_getSearchListFromArray($msg_id);
  338.             }else{
  339.                 $message_set=$msg_id;
  340.             }
  341.         }else{
  342.             $message_set="1:*";
  343.         }
  344.         $ret=$this->cmdFetch($message_set,"(RFC822.SIZE UID FLAGS ENVELOPE INTERNALDATE)");
  345.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  346.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  347.         }
  348.  
  349.  
  350.         if(isset( $ret["PARSED"] ) ){
  351.             for($i=0; $i<count($ret["PARSED"]) ; $i++){
  352.                 $a=$ret["PARSED"][$i]['EXT']['ENVELOPE'];
  353.                 $a['MSG_NUM']=$ret["PARSED"][$i]['NRO'];
  354.                 $a['UID']=$ret["PARSED"][$i]['EXT']['UID'];
  355.                 $a['FLAGS']=$ret["PARSED"][$i]['EXT']['FLAGS'];
  356.                 $a['INTERNALDATE']=$ret["PARSED"][$i]['EXT']['INTERNALDATE'];
  357.                 $a['SIZE']=$ret["PARSED"][$i]['EXT']['RFC822.SIZE'];
  358.                 $env[]=$a;
  359.                 $a=null;
  360.             }
  361.             return $env;
  362.         }
  363.  
  364.         //return $ret;
  365.     }
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.     /*
  374.     * Returns the body of the message with given message number.
  375.     *
  376.     * @param  $msg_id Message number
  377.     * @return mixed   Either message body or false on error
  378.     */
  379.     function getBody($msg_id)
  380.     {
  381.         $ret=$this->cmdFetch($msg_id,"BODY[TEXT]");
  382.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  383.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  384.         }
  385.         $ret=$ret["PARSED"][0]["EXT"]["BODY[TEXT]"]["CONTENT"];
  386.         //$ret=$resp["PARSED"][0]["EXT"]["RFC822"]["CONTENT"];
  387.         return $ret;
  388.     }
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.     /*
  398.     * Returns the entire message with given message number.
  399.     *
  400.     * @param  $msg_id Message number
  401.     * @return mixed   Either entire message or false on error
  402.     */
  403.     function getMessages($msg_id = null, $indexIsMessageNumber=true)
  404.     {
  405.         //$resp=$this->cmdFetch($msg_id,"(BODY[TEXT] BODY[HEADER])");
  406.         if( $msg_id != null){
  407.             if(is_array($msg_id)){
  408.                 $message_set=$this->_getSearchListFromArray($msg_id);
  409.             }else{
  410.                 $message_set=$msg_id;
  411.             }
  412.         }else{
  413.             $message_set="1:*";
  414.         }
  415.  
  416.         $ret=$this->cmdFetch($message_set,"RFC822");
  417.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  418.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  419.         }
  420.         if(isset($ret["PARSED"])){
  421.             foreach($ret["PARSED"] as $msg){
  422.                 if(isset($msg["EXT"]["RFC822"]["CONTENT"])){
  423.                     if($indexIsMessageNumber){
  424.                         $ret_aux[$msg["NRO"]]=$msg["EXT"]["RFC822"]["CONTENT"];
  425.                     }else{
  426.                         $ret_aux[]=$msg["EXT"]["RFC822"]["CONTENT"];
  427.                     }
  428.         }
  429.             }
  430.             return $ret_aux;
  431.        }
  432.        return array();
  433.     }
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.     /*
  446.     * Returns number of messages in this mailbox
  447.     *
  448.     * @param  string $mailbox  the mailbox
  449.     * @return mixed Either number of messages or Pear_Error on error
  450.     */
  451.     function getNumberOfMessages($mailbox = '')
  452.     {
  453.         if ( $mailbox == '' || $mailbox == null ){
  454.             $mailbox=$this->getCurrentMailbox();
  455.         }
  456.         $ret=$this->cmdStatus( $mailbox , "MESSAGES" );
  457.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  458.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  459.         }
  460.         if( isset($ret["PARSED"]["STATUS"]["ATTRIBUTES"]["MESSAGES"] ) ){
  461.             if( !is_numeric( $ret["PARSED"]["STATUS"]["ATTRIBUTES"]["MESSAGES"] ) ){
  462.                 // if this array does not exists means that there is no messages in the mailbox
  463.                 return 0;
  464.             }else{
  465.                 return $ret["PARSED"]["STATUS"]["ATTRIBUTES"]["MESSAGES"];
  466.             }
  467.  
  468.         }
  469.         return 0;
  470.     }
  471.  
  472.  
  473.     /*
  474.     * Returns number of UnSeen messages in this mailbox
  475.     *
  476.     * @param  string $mailbox  the mailbox
  477.     * @return mixed Either number of messages or Pear_Error on error
  478.     */
  479.     function getNumberOfUnSeenMessages($mailbox = '')
  480.     {
  481.         if ( $mailbox == '' ){
  482.             $mailbox=$this->getCurrentMailbox();
  483.         }
  484.         $ret=$this->cmdStatus( $mailbox , "UNSEEN" );
  485.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  486.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  487.         }
  488.         if( isset($ret["PARSED"]["STATUS"]["ATTRIBUTES"]["UNSEEN"] ) ){
  489.             if( !is_numeric( $ret["PARSED"]["STATUS"]["ATTRIBUTES"]["UNSEEN"] ) ){
  490.                 // if this array does not exists means that there is no messages in the mailbox
  491.                 return 0;
  492.             }else{
  493.                 return $ret["PARSED"]["STATUS"]["ATTRIBUTES"]["UNSEEN"];
  494.             }
  495.  
  496.         }
  497.         return 0;
  498.     }
  499.  
  500.     /*
  501.     * Returns number of UnSeen messages in this mailbox
  502.     *
  503.     * @param  string $mailbox  the mailbox
  504.     * @return mixed Either number of messages or Pear_Error on error
  505.     */
  506.     function getNumberOfRecentMessages($mailbox = '')
  507.     {
  508.         if ( $mailbox == '' ){
  509.             $mailbox=$this->getCurrentMailbox();
  510.         }
  511.         $ret=$this->cmdStatus( $mailbox , "RECENT" );
  512.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  513.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  514.         }
  515.         if( isset($ret["PARSED"]["STATUS"]["ATTRIBUTES"]["RECENT"] ) ){
  516.             if( !is_numeric( $ret["PARSED"]["STATUS"]["ATTRIBUTES"]["RECENT"] ) ){
  517.                 // if this array does not exists means that there is no messages in the mailbox
  518.                 return 0;
  519.             }else{
  520.                 return $ret["PARSED"]["STATUS"]["ATTRIBUTES"]["RECENT"];
  521.             }
  522.  
  523.         }
  524.         return 0;
  525.     }
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.     /*
  534.     * Returns an array containing the message envelope
  535.     *
  536.     * @return mixed Either the envelopes or Pear_Error on error
  537.     */
  538.     function getEnvelope($mailbox = '', $msg_id = null)
  539.     {
  540.         if ( $mailbox == '' ){
  541.             $mailbox=$this->getCurrentMailbox();
  542.         }
  543.  
  544.         if( $msg_id != null){
  545.             if(is_array($msg_id)){
  546.                 $message_set=$this->_getSearchListFromArray($msg_id);
  547.             }else{
  548.                 $message_set=$msg_id;
  549.             }
  550.         }else{
  551.             $message_set="1:*";
  552.         }
  553.  
  554.  
  555.         $ret=$this->cmdFetch($message_set,"ENVELOPE");
  556.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  557.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  558.         }
  559.  
  560.         if(isset( $ret["PARSED"] ) ){
  561.             for($i=0; $i<count($ret["PARSED"]) ; $i++){
  562.                 $a=$ret["PARSED"][$i]['EXT']['ENVELOPE'];
  563.                 $a['MSG_NUM']=$ret["PARSED"][$i]['NRO'];
  564.                 $env[]=$a;
  565.             }
  566.             return $env;
  567.         }
  568.         return new PEAR_Error('Error, undefined number of messages');
  569.  
  570.  
  571.     }
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.     /*
  582.     * Returns the sum of all the sizes of messages in $mailbox
  583.     *           WARNING!!!  The method's performance is not good
  584.     *                       if you have a lot of messages in the mailbox
  585.     *                       Use with care!
  586.     * @return mixed Either size of maildrop or false on error
  587.     */
  588.     function getMailboxSize($mailbox = '')
  589.     {
  590.  
  591.         if ( $mailbox != '' && $mailbox != $this->getCurrentMailbox() ){
  592.             // store the actual selected mailbox name
  593.             $mailbox_aux = $this->getCurrentMailbox();
  594.             if ( PEAR::isError( $ret = $this->selectMailbox( $mailbox ) ) ) {
  595.                 return $ret;
  596.             }
  597.         }
  598.  
  599.         $ret=$this->cmdFetch("1:*","RFC822.SIZE");
  600.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  601.                 // Restore the default mailbox if it was changed
  602.                 if ( $mailbox != '' && $mailbox != $this->getCurrentMailbox() ){
  603.                     if ( PEAR::isError( $ret = $this->selectMailbox( $mailbox_aux ) ) ) {
  604.                         return $ret;
  605.                     }
  606.                 }
  607.                 // return 0 because the server says that there is no message in the mailbox
  608.                 return 0;
  609.         }
  610.  
  611.         $sum=0;
  612.  
  613.         if(!isset($ret["PARSED"]) ){
  614.             // if the server does not return a "PARSED"  part
  615.             // we think that it does not suppoprt select or has no messages in it.
  616.             return 0;
  617.         }
  618.         foreach($ret["PARSED"] as $msgSize){
  619.             if( isset($msgSize["EXT"]["RFC822.SIZE"]) ){
  620.                 $sum+= $msgSize["EXT"]["RFC822.SIZE"];
  621.             }
  622.         }
  623.  
  624.         if ( $mailbox != '' && $mailbox != $this->getCurrentMailbox() ){
  625.             // re-select the  mailbox
  626.             if ( PEAR::isError( $ret = $this->selectMailbox( $mailbox_aux ) ) ) {
  627.                 return $ret;
  628.             }
  629.         }
  630.  
  631.         return $sum;
  632.     }
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.     /*
  648.     * Marks a message for deletion. Only will be deleted if the
  649.     * disconnect() method is called with auto-expunge on true or expunge()
  650.     * method is called.
  651.     *
  652.     * @param  $msg_id Message to delete
  653.     * @return bool Success/Failure
  654.     */
  655.     function deleteMessages($msg_id = null)
  656.     {
  657.         /* As said in RFC2060...
  658.         C: A003 STORE 2:4 +FLAGS (\Deleted)
  659.                 S: * 2 FETCH FLAGS (\Deleted \Seen)
  660.                 S: * 3 FETCH FLAGS (\Deleted)
  661.                 S: * 4 FETCH FLAGS (\Deleted \Flagged \Seen)
  662.                 S: A003 OK STORE completed
  663.         */
  664.         //Called without parammeters deletes all the messages in the mailbox
  665.         // You can also provide an array of numbers to delete those emails
  666.         if( $msg_id != null){
  667.             if(is_array($msg_id)){
  668.                 $message_set=$this->_getSearchListFromArray($msg_id);
  669.             }else{
  670.                 $message_set=$msg_id;
  671.             }
  672.         }else{
  673.             $message_set="1:*";
  674.         }
  675.  
  676.  
  677.         $dataitem="+FLAGS.SILENT";
  678.         $value="\Deleted";
  679.         $ret=$this->cmdStore($message_set,$dataitem,$value);
  680.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  681.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  682.         }
  683.         return true;
  684.     }
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.     /**
  695.     * Copies mail from one folder to another
  696.     *
  697.     * @param string $dest_mailbox     mailbox name to copy sessages to
  698.     * @param mixed $message_set       the messages that I want to copy (all by default) it also
  699.     *                                  can be an array
  700.     * @param string $source_mailbox   mailbox name from where the messages are copied
  701.     *
  702.     * @return mixed true on Success/PearError on Failure
  703.     * @since 1.0
  704.     */
  705.     function copyMessages($dest_mailbox, $msg_id = null , $source_mailbox = null )
  706.     {
  707.  
  708.         if($source_mailbox == null){
  709.             $source_mailbox = $this->getCurrentMailbox();
  710.         }else{
  711.             if ( PEAR::isError( $ret = $this->selectMailbox( $source_mailbox  ) ) ) {
  712.                 return $ret;
  713.             }
  714.         }
  715.         //Called without parammeters copies all messages in the mailbox
  716.         // You can also provide an array of numbers to copy those emails
  717.         if( $msg_id != null){
  718.             if(is_array($msg_id)){
  719.                 $message_set=$this->_getSearchListFromArray($msg_id);
  720.             }else{
  721.                 $message_set=$msg_id;
  722.             }
  723.         }else{
  724.             $message_set="1:*";
  725.         }
  726.  
  727.  
  728.  
  729.         if ( PEAR::isError( $ret = $this->cmdCopy($message_set, $dest_mailbox ) ) ) {
  730.             return $ret;
  731.         }
  732.         return true;
  733.     }
  734.  
  735.  
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743.  
  744.  
  745.  
  746.  
  747.     /**
  748.     * Appends a mail to  a mailbox
  749.     *
  750.     * @param string $rfc_message    the message to append in RFC822 format
  751.     * @param string $mailbox        mailbox name to append to
  752.     *
  753.     * @return mixed true on Success/PearError on Failure
  754.     * @since 1.0
  755.     */
  756.     function appendMessage($rfc_message, $mailbox = null )
  757.     {
  758.         if($mailbox == null){
  759.             $mailbox = $this->getCurrentMailbox();
  760.         }
  761.         $ret=$this->cmdAppend($mailbox,$rfc_message);
  762.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  763.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  764.         }
  765.         return true;
  766.     }
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.     /******************************************************************
  783.     **                                                               **
  784.     **           MAILBOX RELATED METHODS                             **
  785.     **                                                               **
  786.     ******************************************************************/
  787.  
  788.  
  789.  
  790.  
  791.  
  792.     /**
  793.     * Gets the HierachyDelimiter character used to create subfolders  cyrus users "."
  794.     *   and wu-imapd uses "/"
  795.     *
  796.     * $param  string  the mailbox to get the hierarchy from
  797.     * @return string  the hierarchy delimiter
  798.     *
  799.     * @access public
  800.     * @since  1.0
  801.     */
  802.     function getHierarchyDelimiter( $mailbox = '' )
  803.     {
  804.  
  805.         /* RFC2060 says: "the command LIST "" "" means get the hierachy delimiter:
  806.                     An empty ("" string) mailbox name argument is a special request to
  807.             return the hierarchy delimiter and the root name of the name given
  808.             in the reference.  The value returned as the root MAY be null if
  809.             the reference is non-rooted or is null.  In all cases, the
  810.             hierarchy delimiter is returned.  This permits a client to get the
  811.             hierarchy delimiter even when no mailboxes by that name currently
  812.             exist."
  813.         */
  814.         if( PEAR::isError( $ret = $this->cmdList( $mailbox , '' )  ) ){
  815.             return $ret;
  816.         }
  817.         if(isset($ret["PARSED"][0]["EXT"]["LIST"]["HIERACHY_DELIMITER"]) ){
  818.             return $ret["PARSED"][0]["EXT"]["LIST"]["HIERACHY_DELIMITER"];
  819.         }
  820.         return new PEAR_Error( 'the IMAP Server does not support HIERACHY_DELIMITER!' );
  821.     }
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.     /**
  831.     * Returns an array containing the names of the selected mailboxes
  832.     *
  833.     * @param string $mailbox_base         base mailbox to start the search
  834.     *                   $mailbox_base     if $mailbox_base == ''     then $mailbox_base is the curent selected mailbox
  835.     * @param string $restriction_search   false or 0 means return all mailboxes  true or 1 return only the mailbox that contains that exact name
  836.                                             2  return all mailboxes in that hierarchy level
  837.     * @param string $returnAttributes     true means return an assoc array containing mailbox names and mailbox attributes
  838.                                           false - the default - means return an array of mailboxes
  839.     *
  840.     * @return mixed true on Success/PearError on Failure
  841.     * @since 1.0
  842.     */
  843.  
  844.  
  845.  
  846.     function getMailboxes($reference = ''  , $restriction_search = 0, $returnAttributes=false )
  847.     {
  848.  
  849.         if ( is_bool($restriction_search) ){
  850.             $restriction_search = (int) $restriction_search;
  851.         }
  852.  
  853.         if ( is_int( $restriction_search ) ){
  854.             switch ( $restriction_search ) {
  855.                 case 0:
  856.                     $mailbox = "*";
  857.                     break;
  858.                 case 1:
  859.                     $mailbox = $reference;
  860.                     $reference = '%';
  861.                     break;
  862.                 case 2:
  863.                     $mailbox = "%";
  864.                     break;
  865.             }
  866.          }else{
  867.             if ( is_string( $restriction_search ) ){
  868.                 $mailbox = $restriction_search;
  869.             }else {
  870.                 return new PEAR_Error("UPS... you ");
  871.             }
  872.         }
  873.  
  874.         if( PEAR::isError( $ret = $this->cmdList($reference, $mailbox) ) ){
  875.             return $ret;
  876.         }
  877.  
  878.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  879.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  880.         }
  881.         $ret_aux=array();
  882.         if( isset($ret["PARSED"]) ){
  883.             foreach( $ret["PARSED"] as $mbox ){
  884.  
  885.             //If the folder has the \NoSelect atribute we don't put in the list
  886.             // it solves a bug in wu-imap that crash the IMAP server if we select that mailbox
  887.                 if( isset($mbox["EXT"]["LIST"]["NAME_ATTRIBUTES"]) ){
  888.                     if( !in_array('\NoSelect',$mbox["EXT"]["LIST"]["NAME_ATTRIBUTES"]) ){
  889.                         if( $returnAttributes){
  890.                             $ret_aux[]=array(   'MAILBOX' => $mbox["EXT"]["LIST"]["MAILBOX_NAME"],
  891.                                                 'ATTRIBUTES' => $mbox["EXT"]["LIST"]["NAME_ATTRIBUTES"] ,
  892.                                                 'HIERACHY_DELIMITER' => $mbox["EXT"]["LIST"]["HIERACHY_DELIMITER"] ) ;
  893.                         }else{
  894.                             $ret_aux[]=$mbox["EXT"]["LIST"]["MAILBOX_NAME"];
  895.                         }
  896.                     }
  897.                 }
  898.             }
  899.         }
  900.         return $ret_aux;
  901.     }
  902.  
  903.  
  904.  
  905.  
  906.  
  907.     /**
  908.     * check if the mailbox name exists
  909.     *
  910.     * @param string $mailbox     mailbox name to check existance
  911.     *
  912.     * @return boolean true on Success/false on Failure
  913.     * @since 1.0
  914.     */
  915.  
  916.     function mailboxExist($mailbox)
  917.     {
  918.         // true means do an exact match
  919.         if( PEAR::isError( $ret = $this->getMailboxes( $mailbox , true ) ) ){
  920.             return $ret;
  921.         }
  922.         if( count( $ret ) > 0 ){
  923.             return true;
  924.         }
  925.         return false;
  926.     }
  927.  
  928.  
  929.  
  930.  
  931.  
  932.  
  933.  
  934.     /**
  935.     * Creates the mailbox $mailbox
  936.     *
  937.     * @param string $mailbox     mailbox name to create
  938.     *
  939.     * @return mixed true on Success/PearError on Failure
  940.     * @since 1.0
  941.     */
  942.     function createMailbox($mailbox)
  943.     {
  944.         $ret=$this->cmdCreate($mailbox);
  945.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  946.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  947.         }
  948.         return true;
  949.     }
  950.  
  951.  
  952.  
  953.  
  954.     /**
  955.     * Deletes the mailbox $mailbox
  956.     *
  957.     * @param string $mailbox     mailbox name to delete
  958.     *
  959.     * @return mixed true on Success/PearError on Failure
  960.     * @since 1.0
  961.     */
  962.     function deleteMailbox($mailbox)
  963.     {
  964.     // TODO verificar que el mailbox se encuentra vacio y, sino borrar los mensajes antes~!!!!!!
  965.         $ret=$this->cmdDelete($mailbox);
  966.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  967.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  968.         }
  969.         return true;
  970.     }
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.     /**
  981.     * Renames the mailbox $mailbox
  982.     *
  983.     * @param string $mailbox     mailbox name to rename
  984.     *
  985.     * @return mixed true on Success/PearError on Failure
  986.     * @since 1.0
  987.     */
  988.     function renameMailbox($oldmailbox, $newmailbox)
  989.     {
  990.         $ret=$this->cmdRename($oldmailbox,$newmailbox);
  991.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  992.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  993.         }
  994.         return true;
  995.     }
  996.  
  997.  
  998.  
  999.  
  1000.  
  1001.  
  1002.  
  1003.  
  1004.  
  1005.     /******************************************************************
  1006.     **                                                               **
  1007.     **           SUBSCRIPTION METHODS                                **
  1008.     **                                                               **
  1009.     ******************************************************************/
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015.     /**
  1016.     * Subscribes to the selected mailbox
  1017.     *
  1018.     * @param string $mailbox     mailbox name to subscribe
  1019.     *
  1020.     * @return mixed true on Success/PearError on Failure
  1021.     * @since 1.0
  1022.     */
  1023.     function subscribeMailbox($mailbox = null )
  1024.     {
  1025.         if($mailbox == null){
  1026.             $mailbox = $this->getCurrentMailbox();
  1027.         }
  1028.         $ret=$this->cmdSubscribe($mailbox);
  1029.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  1030.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  1031.         }
  1032.         return true;
  1033.     }
  1034.  
  1035.  
  1036.  
  1037.  
  1038.  
  1039.     /**
  1040.     * Removes the subscription to a mailbox
  1041.     *
  1042.     * @param string $mailbox     mailbox name to unsubscribe
  1043.     *
  1044.     * @return mixed true on Success/PearError on Failure
  1045.     * @since 1.0
  1046.     */
  1047.     function unsubscribeMailbox($mailbox = null)
  1048.     {
  1049.         if($mailbox == null){
  1050.             $mailbox = $this->getCurrentMailbox();
  1051.         }
  1052.         $ret=$this->cmdUnsubscribe($mailbox);
  1053.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  1054.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  1055.         }
  1056.         return true;
  1057.     }
  1058.  
  1059.  
  1060.  
  1061.     /**
  1062.     * Lists the subscription to mailboxes
  1063.     *
  1064.     * @param string $mailbox_base     mailbox name start the search (see to getMailboxes() )
  1065.     * @param string $mailbox_name     mailbox name filter the search (see to getMailboxes() )
  1066.     *
  1067.     * @return mixed true on Success/PearError on Failure
  1068.     * @since 1.0
  1069.     */
  1070.  
  1071.     function listsubscribedMailboxes($reference = ''  , $restriction_search = 0, $returnAttributes = false)
  1072.     {
  1073.         if ( is_bool($restriction_search) ){
  1074.             $restriction_search = (int) $restriction_search;
  1075.         }
  1076.  
  1077.         if ( is_int( $restriction_search ) ){
  1078.             switch ( $restriction_search ) {
  1079.                 case 0:
  1080.                     $mailbox = "*";
  1081.                     break;
  1082.                 case 1:
  1083.                     $mailbox = $reference;
  1084.                     $reference = '%';
  1085.                     break;
  1086.                 case 2:
  1087.                     $mailbox = "%";
  1088.                     break;
  1089.             }
  1090.          }else{
  1091.             if ( is_string( $restriction_search ) ){
  1092.                 $mailbox = $restriction_search;
  1093.             }else {
  1094.                 return new PEAR_Error("UPS... you ");
  1095.             }
  1096.         }
  1097.  
  1098.         if( PEAR::isError( $ret=$this->cmdLsub($reference, $mailbox) ) ){
  1099.             return $ret;
  1100.         }
  1101.         //$ret=$this->cmdLsub($mailbox_base, $mailbox_name);
  1102.  
  1103.  
  1104.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  1105.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  1106.         }
  1107.  
  1108.         $ret_aux=array();
  1109.         if( isset($ret["PARSED"]) ){
  1110.             foreach( $ret["PARSED"] as $mbox ){
  1111.                 if( isset($mbox["EXT"]["LSUB"]["MAILBOX_NAME"]) ){
  1112.                     if( $returnAttributes){
  1113.                             $ret_aux[]=array(
  1114.                                         'MAILBOX' => $mbox["EXT"]["LSUB"]["MAILBOX_NAME"],
  1115.                                         'ATTRIBUTES' => $mbox["EXT"]["LSUB"]["NAME_ATTRIBUTES"],
  1116.                                         'HIERACHY_DELIMITER' =>  $mbox["EXT"]["LSUB"]["HIERACHY_DELIMITER"]
  1117.                                         ) ;
  1118.                         }else{
  1119.                             $ret_aux[]=$mbox["EXT"]["LSUB"]["MAILBOX_NAME"];
  1120.  
  1121.                         }
  1122.                 }
  1123.             }
  1124.         }
  1125.         return $ret_aux;
  1126.     }
  1127.  
  1128.  
  1129.  
  1130.  
  1131.  
  1132.  
  1133.  
  1134.  
  1135.  
  1136.  
  1137.  
  1138.  
  1139.     /******************************************************************
  1140.     **                                                               **
  1141.     **           FLAGS METHODS                                       **
  1142.     **                                                               **
  1143.     ******************************************************************/
  1144.  
  1145.  
  1146.  
  1147.  
  1148.     /**
  1149.     * Lists the flags of the selected messages
  1150.     *
  1151.     * @param mixes $msg_id  the message list
  1152.     *
  1153.     * @return mixed array on Success/PearError on Failure
  1154.     * @since 1.0
  1155.     */
  1156.     function getFlags( $msg_id = null )
  1157.     {
  1158.       // You can also provide an array of numbers to those emails
  1159.         if( $msg_id != null){
  1160.             if(is_array($msg_id)){
  1161.                 $message_set=$this->_getSearchListFromArray($msg_id);
  1162.             }else{
  1163.                 $message_set=$msg_id;
  1164.             }
  1165.         }else{
  1166.             $message_set="1:*";
  1167.         }
  1168.  
  1169.  
  1170.         $ret=$this->cmdFetch($message_set,"FLAGS");
  1171.         if(strtoupper($ret["RESPONSE"]["CODE"]) != "OK"){
  1172.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  1173.         }
  1174.         $flags=array();
  1175.         if(isset($ret["PARSED"])){
  1176.             foreach($ret["PARSED"] as $msg_flags){
  1177.                 if(isset($msg_flags["EXT"]["FLAGS"])){
  1178.                     $flags[]=$msg_flags["EXT"]["FLAGS"];
  1179.                 }
  1180.             }
  1181.         }
  1182.         return $flags;
  1183.     }
  1184.  
  1185.  
  1186.  
  1187.  
  1188.    /**
  1189.     * check the Seen flag
  1190.     *
  1191.     * @param mixes $message_nro the message to check
  1192.     *
  1193.     * @return mixed true or false if the flag is sert PearError on Failure
  1194.     * @since 1.0
  1195.     */
  1196.     function isSeen($message_nro)
  1197.     {
  1198.         return $this->hasFlag( $message_nro, "\\Seen" );
  1199.     }
  1200.  
  1201.  
  1202.  
  1203.  
  1204.    /**
  1205.     * check the Answered flag
  1206.     *
  1207.     * @param mixes $message_nro the message to check
  1208.     *
  1209.     * @return mixed true or false if the flag is sert PearError on Failure
  1210.     * @since 1.0
  1211.     */
  1212.     function isAnswered($message_nro)
  1213.     {
  1214.         return $this->hasFlag( $message_nro, "\\Answered" );
  1215.     }
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.    /**
  1222.     * check the flagged flag
  1223.     *
  1224.     * @param mixes $message_nro the message to check
  1225.     *
  1226.     * @return mixed true or false if the flag is sert PearError on Failure
  1227.     * @since 1.0
  1228.     */
  1229.     function isFlagged($message_nro)
  1230.     {
  1231.         return $this->hasFlag( $message_nro, "\\Flagged" );
  1232.     }
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.    /**
  1240.     * check the Draft flag
  1241.     *
  1242.     * @param mixes $message_nro the message to check
  1243.     *
  1244.     * @return mixed true or false if the flag is sert PearError on Failure
  1245.     * @since 1.0
  1246.     */
  1247.     function isDraft($message_nro)
  1248.     {
  1249.         return $this->hasFlag( $message_nro, "\\Draft" );
  1250.     }
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.    /**
  1259.     * check the Deleted flag
  1260.     *
  1261.     * @param mixes $message_nro the message to check
  1262.     *
  1263.     * @return mixed true or false if the flag is sert PearError on Failure
  1264.     * @since 1.0
  1265.     */
  1266.     function isDeleted($message_nro)
  1267.     {
  1268.         return $this->hasFlag( $message_nro, "\\Deleted" );
  1269.     }
  1270.  
  1271.  
  1272.  
  1273.  
  1274.  
  1275.  
  1276.     function hasFlag($message_nro,$flag)
  1277.     {
  1278.         if ( PEAR::isError( $resp = $this->getFlags( $message_nro ) ) ) {
  1279.             return $resp;
  1280.         }
  1281.         if(isset($resp[0]) ){
  1282.             if( is_array( $resp[0] ) ){
  1283.                 if( in_array( $flag , $resp[0] ) )
  1284.                     return true;
  1285.             }
  1286.         }
  1287.         return false;
  1288.     }
  1289.  
  1290.  
  1291.  
  1292.  
  1293.  
  1294.     /******************************************************************
  1295.     **                                                               **
  1296.     **           MISC METHODS                                        **
  1297.     **                                                               **
  1298.     ******************************************************************/
  1299.  
  1300.  
  1301.  
  1302.  
  1303.  
  1304.  
  1305.     /*
  1306.     * expunge function. Sends the EXPUNGE command
  1307.     *
  1308.     *
  1309.     * @return bool Success/Failure
  1310.     */
  1311.     function expunge()
  1312.     {
  1313.         $ret = $this->cmdExpunge();
  1314.         if( strtoupper( $ret["RESPONSE"]["CODE"]) != "OK" ){
  1315.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  1316.         }
  1317.         return true;
  1318.     }
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.  
  1325.  
  1326.     /*
  1327.     * search function. Sends the SEARCH command
  1328.     *
  1329.     *
  1330.     * @return bool Success/Failure
  1331.     */
  1332.     function search($search_list,$uidSearch=false)
  1333.     {
  1334.         if($uidSearch){
  1335.             $ret = $this->cmdUidSearch($search_list);
  1336.         }else{
  1337.             $ret = $this->cmdSearch($search_list);
  1338.         }
  1339.  
  1340.         if( strtoupper( $ret["RESPONSE"]["CODE"]) != "OK" ){
  1341.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  1342.         }
  1343.         return $ret["PARSED"]["SEARCH"]["SEARCH_LIST"];
  1344.     }
  1345.  
  1346.  
  1347.  
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.     /******************************************************************
  1354.     **                                                               **
  1355.     **           QUOTA METHODS                                       **
  1356.     **                                                               **
  1357.     ******************************************************************/
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.      /**
  1365.      * Returns STORAGE quota details
  1366.      * @param string $mailbox_name Mailbox to get quota info.
  1367.      * @return assoc array contaning the quota info  on success or PEAR_Error
  1368.      *
  1369.      * @access public
  1370.      * @since  1.0
  1371.      */
  1372.     function getStorageQuota($mailbox_name = null )
  1373.     {
  1374.        if($mailbox_name == null){
  1375.             $mailbox_name = $this->getCurrentMailbox();
  1376.         }
  1377.  
  1378.  
  1379.         if ( PEAR::isError( $ret = $this->cmdGetQuota($mailbox_name) ) ) {
  1380.             return new PEAR_Error($ret->getMessage());
  1381.         }
  1382.  
  1383.         if( strtoupper( $ret["RESPONSE"]["CODE"]) != "OK" ){
  1384.             // if the error is that the user does not have quota set return  an array
  1385.             // and not pear error
  1386.             if( substr(strtoupper($ret["RESPONSE"]["STR_CODE"]),0,5)  == "QUOTA" ){
  1387.                 return array('USED'=>'NOT SET', 'QMAX'=>'NOT SET');
  1388.             }
  1389.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  1390.         }
  1391.  
  1392.         if( isset( $ret['PARSED']['EXT']['QUOTA']['STORAGE'] ) ){
  1393.             return $ret['PARSED']['EXT']['QUOTA']['STORAGE'];
  1394.         }
  1395.         return array('USED'=>'NOT SET', 'QMAX'=>'NOT SET');
  1396.    }
  1397.  
  1398.  
  1399.  
  1400.      /**
  1401.      * Returns MESSAGES quota details
  1402.      * @param string $mailbox_name Mailbox to get quota info.
  1403.      * @return assoc array contaning the quota info  on success or PEAR_Error
  1404.      *
  1405.      * @access public
  1406.      * @since  1.0
  1407.      */
  1408.     function getMessagesQuota($mailbox_name = null )
  1409.     {
  1410.        if($mailbox_name == null){
  1411.             $mailbox_name = $this->getCurrentMailbox();
  1412.         }
  1413.  
  1414.         if ( PEAR::isError( $ret = $this->cmdGetQuota($mailbox_name) ) ) {
  1415.             return new PEAR_Error($ret->getMessage());
  1416.         }
  1417.  
  1418.         if( strtoupper( $ret["RESPONSE"]["CODE"]) != "OK" ){
  1419.             // if the error is that the user does not have quota set return  an array
  1420.             // and not pear error
  1421.             if( substr(strtoupper($ret["RESPONSE"]["STR_CODE"]),0,5)  == "QUOTA" ){
  1422.                 return array('USED'=>'NOT SET', 'QMAX'=>'NOT SET');
  1423.             }
  1424.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  1425.         }
  1426.  
  1427.         if( isset( $ret['PARSED']['EXT']['QUOTA']['MESSAGES'] ) ){
  1428.             return $ret['PARSED']['EXT']['QUOTA']['MESSAGES'];
  1429.         }
  1430.         return array('USED'=>'NOT SET', 'QMAX'=>'NOT SET');
  1431.    }
  1432.  
  1433.  
  1434.  
  1435.  
  1436.      /**
  1437.      * sets STORAGE quota details
  1438.      * @param string $mailbox_name Mailbox to get quota info.
  1439.      * @return true on success or PEAR_Error
  1440.      *
  1441.      * @access public
  1442.      * @since  1.0
  1443.      */
  1444.     function setStorageQuota($mailbox_name, $quota)
  1445.     {
  1446.         if ( PEAR::isError( $ret = $this->cmdSetQuota($mailbox_name,$quota) ) ) {
  1447.             return new PEAR_Error($ret->getMessage());
  1448.         }
  1449.         if( strtoupper( $ret["RESPONSE"]["CODE"]) != "OK" ){
  1450.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  1451.         }
  1452.         return true;
  1453.     }
  1454.  
  1455.  
  1456.  
  1457.  
  1458.      /**
  1459.      * sets MESSAGES quota details
  1460.      * @param string $mailbox_name Mailbox to get quota info.
  1461.      * @return true on success or PEAR_Error
  1462.      *
  1463.      * @access public
  1464.      * @since  1.0
  1465.      */
  1466.     function setMessagesQuota($mailbox_name, $quota)
  1467.     {
  1468.         if ( PEAR::isError( $ret = $this->cmdSetQuota($mailbox_name,'',$quota) ) ) {
  1469.             return new PEAR_Error($ret->getMessage());
  1470.         }
  1471.         if( strtoupper( $ret["RESPONSE"]["CODE"]) != "OK" ){
  1472.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  1473.         }
  1474.         return true;
  1475.     }
  1476.  
  1477.  
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.     /******************************************************************
  1486.     **                                                               **
  1487.     **           ACL METHODS                                         **
  1488.     **                                                               **
  1489.     ******************************************************************/
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496.     /**
  1497.      * get the Access Control List details
  1498.      * @param string $mailbox_name Mailbox to get ACL info.
  1499.      * @return string on success or PEAR_Error
  1500.      *
  1501.      * @access public
  1502.      * @since  1.0
  1503.      */
  1504.     function getACL($mailbox_name = null )
  1505.     {
  1506.        if($mailbox_name == null){
  1507.             $mailbox_name = $this->getCurrentMailbox();
  1508.         }
  1509.         if ( PEAR::isError( $ret = $this->cmdGetACL($mailbox_name) ) ) {
  1510.             return new PEAR_Error($ret->getMessage());
  1511.         }
  1512.  
  1513.         if( strtoupper( $ret["RESPONSE"]["CODE"]) != "OK" ){
  1514.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  1515.         }
  1516.  
  1517.         if( isset($ret['PARSED']['USERS']) ){
  1518.         return $ret['PARSED']['USERS'];
  1519.         }else{
  1520.             return false;
  1521.         }
  1522.     }
  1523.  
  1524.  
  1525.  
  1526.  
  1527.  
  1528.  
  1529.  
  1530.  
  1531.     /**
  1532.     * Set ACL on a mailbox
  1533.     *
  1534.     * @param  string $mailbox_name  the mailbox
  1535.     * @param  string $user          user to set the ACL
  1536.     * @param  string $acl           ACL list
  1537.     * @return mixed                 True on success, or PEAR_Error on false
  1538.     *
  1539.     * @access public
  1540.     * @since  1.0
  1541.     */
  1542.     function setACL($mailbox_name, $user, $acl)
  1543.     {
  1544.         if ( PEAR::isError( $ret = $this->cmdSetACL($mailbox_name, $user, $acl) ) ) {
  1545.             return new PEAR_Error($ret->getMessage());
  1546.         }
  1547.         if( strtoupper( $ret["RESPONSE"]["CODE"]) != "OK" ){
  1548.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  1549.         }
  1550.         return true;
  1551.     }
  1552.  
  1553.  
  1554.     /**
  1555.     * deletes the ACL on a mailbox
  1556.     *
  1557.     * @param  string $mailbox_name  the mailbox
  1558.     * @param  string $user          user to set the ACL
  1559.     * @return mixed                 True on success, or PEAR_Error on false
  1560.     *
  1561.     * @access public
  1562.     * @since  1.0
  1563.     */
  1564.     function deleteACL($mailbox_name, $user)
  1565.     {
  1566.         if ( PEAR::isError( $ret = $this->cmdDeleteACL($mailbox_name, $user) ) ) {
  1567.             return new PEAR_Error($ret->getMessage());
  1568.         }
  1569.         if( strtoupper( $ret["RESPONSE"]["CODE"]) != "OK" ){
  1570.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  1571.         }
  1572.         return true;
  1573.     }
  1574.  
  1575.  
  1576.  
  1577.     /**
  1578.     * returns the rights that the user logged on has on the mailbox
  1579.     * this method can be used by any user, not only the administrator
  1580.     *
  1581.     * @param  string $mailbox_name  the mailbox to query rights
  1582.     * @return mixed                 string contailing the list of rights on success, or PEAR_Error on failure
  1583.     *
  1584.     * @access public
  1585.     * @since  1.0
  1586.     */
  1587.     function getMyRights($mailbox_name = null)
  1588.     {
  1589.  
  1590.         if($mailbox_name == null){
  1591.             $mailbox_name = $this->getCurrentMailbox();
  1592.         }
  1593.  
  1594.  
  1595.         if ( PEAR::isError( $ret = $this->cmdMyRights($mailbox_name) ) ) {
  1596.             return new PEAR_Error($ret->getMessage());
  1597.         }
  1598.         if( strtoupper( $ret["RESPONSE"]["CODE"]) != "OK" ){
  1599.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  1600.         }
  1601.  
  1602.         if(isset($ret['PARSED']['GRANTED'])){
  1603.             return $ret['PARSED']['GRANTED'];
  1604.         }
  1605.  
  1606.         return new PEAR_Error('Bogus response from server!' );
  1607.  
  1608.     }
  1609.  
  1610.  
  1611.  
  1612.  
  1613.  
  1614.  
  1615.  
  1616.  
  1617.  
  1618.     /**
  1619.     * returns an array containing the rights that a user logged on has on the mailbox
  1620.     * this method can be used by any user, not only the administrator
  1621.     *
  1622.     * @param  string $mailbox_name  the mailbox to query rights
  1623.     * @return mixed                 string contailing the list of rights on success, or PEAR_Error on failure
  1624.     *
  1625.     * @access public
  1626.     * @since  1.0
  1627.     */
  1628.     function getACLRights($user,$mailbox_name = null)
  1629.     {
  1630.  
  1631.         if($mailbox_name == null){
  1632.             $mailbox_name = $this->getCurrentMailbox();
  1633.         }
  1634.  
  1635.  
  1636.         if ( PEAR::isError( $ret = $this->cmdListRights($mailbox_name, $user) ) ) {
  1637.             return new PEAR_Error($ret->getMessage());
  1638.         }
  1639.         if( strtoupper( $ret["RESPONSE"]["CODE"]) != "OK" ){
  1640.             return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]);
  1641.         }
  1642.  
  1643.  
  1644.         if(isset($ret['PARSED']['GRANTED'])){
  1645.             return $ret['PARSED']['GRANTED'];
  1646.         }
  1647.  
  1648.         return new PEAR_Error('Bogus response from server!' );
  1649.  
  1650.     }
  1651.  
  1652.  
  1653.  
  1654.  
  1655.  
  1656.  
  1657.  
  1658.  
  1659.  
  1660.  
  1661.  
  1662.  
  1663.     /******************************************************************
  1664.     **                                                               **
  1665.     **           ANNOTATEMORE METHODS                                **
  1666.     **                                                               **
  1667.     ******************************************************************/
  1668.  
  1669.  
  1670.  
  1671.  
  1672.  
  1673.  
  1674.     function setAnnotation($entry, $values, $mailbox_name = null )
  1675.     {
  1676.         if($mailbox_name == null){
  1677.             $mailbox_name = $this->getCurrentMailbox();
  1678.         }
  1679.  
  1680.         if (PEAR::isError($ret = $this->cmdSetAnnotation($mailbox_name, $entry, $values))) {
  1681.             return new PEAR_Error($ret->getMessage());
  1682.         }
  1683.         if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
  1684.             return new PEAR_Error($ret['RESPONSE']['CODE'] . ', ' . $ret['RESPONSE']['STR_CODE']);
  1685.         }
  1686.         return true;
  1687.     }
  1688.  
  1689.  
  1690.  
  1691.  
  1692.  
  1693.  
  1694.     function deleteAnnotation($entry, $values, $mailbox_name = null )
  1695.     {
  1696.         if($mailbox_name == null){
  1697.             $mailbox_name = $this->getCurrentMailbox();
  1698.         }
  1699.  
  1700.         if (PEAR::isError($ret = $this->cmdDeleteAnnotation($mailbox_name, $entry, $values))) {
  1701.             return new PEAR_Error($ret->getMessage());
  1702.         }
  1703.         if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
  1704.             return new PEAR_Error($ret['RESPONSE']['CODE'] . ', ' . $ret['RESPONSE']['STR_CODE']);
  1705.         }
  1706.         return true;
  1707.     }
  1708.  
  1709.  
  1710.  
  1711.  
  1712.  
  1713.  
  1714.     function getAnnotation($entries, $values, $mailbox_name = null)
  1715.     {
  1716.         if($mailbox_name == null){
  1717.             $mailbox_name = $this->getCurrentMailbox();
  1718.         }
  1719.         if (!is_array($entries)) {
  1720.             $entries = array($entries);
  1721.         }
  1722.         if (!is_array($values)) {
  1723.             $values = array($values);
  1724.         }
  1725.  
  1726.         if (PEAR::isError($ret = $this->cmdGetAnnotation($mailbox_name, $entries, $values))) {
  1727.             return new PEAR_Error($ret->getMessage());
  1728.         }
  1729.         if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
  1730.             return new PEAR_Error($ret['RESPONSE']['CODE'] . ', ' . $ret['RESPONSE']['STR_CODE']);
  1731.         }
  1732.         $ret_aux = array();
  1733.         if (isset($ret['PARSED'])) {
  1734.             foreach ($ret['PARSED'] as $mbox) {
  1735.                 $rawvalues = $mbox['EXT']['ATTRIBUTES'];
  1736.                 $values = array();
  1737.                 for ($i = 0; $i < count($rawvalues); $i += 2) {
  1738.                     $values[$rawvalues[$i]] = $rawvalues[$i + 1];
  1739.                 }
  1740.                 $mbox['EXT']['ATTRIBUTES'] = $values;
  1741.                 $ret_aux[] = $mbox['EXT'];
  1742.             }
  1743.         }
  1744.         if (count($ret_aux) == 1 && $ret_aux[0]['MAILBOX'] == $mailbox_name) {
  1745.             if (count($entries) == 1 && $ret_aux[0]['ENTRY'] == $entries[0]) {
  1746.                 if (count($ret_aux[0]['ATTRIBUTES']) == 1 && count($values) == 1) {
  1747.                     $attrs = array_keys($ret_aux[0]['ATTRIBUTES']);
  1748.                     $vals = array_keys($values);
  1749.                     if ($attrs[0] == $vals[0]) {
  1750.                         return $ret_aux[0]['ATTRIBUTES'][$attrs[0]];
  1751.                     }
  1752.                 }
  1753.             }
  1754.         }
  1755.         return $ret_aux;
  1756.     }
  1757.  
  1758.  
  1759.  
  1760.  
  1761.  
  1762.  
  1763.  
  1764.  
  1765.  
  1766.     /*
  1767.     *   Transform an array to a list to be used in the cmdFetch method
  1768.     *
  1769.     */
  1770.     function _getSearchListFromArray($arr){
  1771.  
  1772.         $txt=implode(',' , $arr);
  1773.         return $txt;
  1774.     }
  1775.  
  1776.  
  1777.  
  1778.  
  1779.  
  1780.  
  1781.  
  1782.  
  1783.     /*****************************************************
  1784.         Net_POP3 Compatibility functions:
  1785.  
  1786.         Warning!!!
  1787.             Those functions could dissapear in the future
  1788.  
  1789.     *********************************************************/
  1790.  
  1791.  
  1792.  
  1793.  
  1794.     function getSize(){
  1795.         return $this->getMailboxSize();
  1796.     }
  1797.  
  1798.  
  1799.     function numMsg($mailbox = null){
  1800.         return $this->getNumberOfMessages($mailbox);
  1801.     }
  1802.  
  1803.  
  1804.  
  1805.     /*
  1806.     * Returns the entire message with given message number.
  1807.     *
  1808.     * @param  $msg_id Message number
  1809.     * @return mixed   Either entire message or false on error
  1810.     */
  1811.     function getMsg($msg_id)
  1812.     {
  1813.         $ret=$this->getMessages($msg_id,false);
  1814.         // false means that getMessages() must not use the msg number as array key
  1815.         if(isset($ret[0])){
  1816.             return $ret[0];
  1817.         }else{
  1818.             return $ret;
  1819.         }
  1820.  
  1821.     }
  1822.  
  1823.  
  1824.     function getListing($msg_id = null)
  1825.     {
  1826.         return $this->getMessagesList($msg_id);
  1827.     }
  1828.  
  1829.  
  1830.     function deleteMsg($msg_id){
  1831.         return $this->deleteMessages($msg_id);
  1832.     }
  1833.  
  1834.  
  1835.  
  1836.  
  1837.  
  1838. }
  1839. ?>
  1840.