Simple Groupware PHP Cross Reference Groupware Applications

Source: /src/lib/mail/IMAP.php - 1781 lines - 59752 bytes - Summary - Text - Print

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

title

Description

title

Description

title

Description

title

title

Body