home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / classes / imap.class.inc < prev    next >
Text File  |  2004-03-08  |  28KB  |  1,079 lines

  1. <?php
  2. /*
  3. Copyright Intermesh 2003
  4. Author: Merijn Schering <mschering@intermesh.nl>
  5. Version: 1.0 Release date: 08 July 2003
  6.  
  7. This program is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.
  11. */
  12.  
  13. class imap
  14. {
  15.     var $conn;
  16.     var $count;
  17.     var $unseen;
  18.     var $messages;
  19.     var $next_message_id;
  20.     var $message;
  21.     var $servertype;
  22.     var $connectstring;
  23.     var $mailboxes;
  24.     var $mailbox;
  25.     var $username;
  26.     var $mailbox_info;
  27.  
  28.     /*
  29.     Open connection to server and count total and new messages
  30.     */
  31.  
  32.     function open($host, $type, $port, $username, $password, $mailbox = "INBOX", $flags = 0)
  33.     {
  34.         global $GO_CONFIG;
  35.  
  36.         $this->username = $username;
  37.         $this->servertype = strtolower($type);
  38.         $this->mailbox = imap_utf7_encode($mailbox);
  39.         $this->connectstring = $host.":".$port."/".$this->servertype.$GO_CONFIG->email_connectstring_options;
  40.  
  41.         if ($flags != 0)
  42.         {
  43.             $this->conn = @imap_open("{".$this->connectstring."}".$this->mailbox, $username, $password, $flags);
  44.         }else
  45.         {
  46.             $this->conn = @imap_open("{".$this->connectstring."}".$this->mailbox, $username, $password);
  47.         }
  48.         return $this->conn;
  49.     }
  50.  
  51.     /*
  52.     Close connection with server
  53.     */
  54.  
  55.     function close()
  56.     {
  57.         unset($this->messages);
  58.         unset($this->count);
  59.         unset($this->unseen);
  60.         unset($this->next_message_id);
  61.         @imap_close($this->conn);
  62.         unset($this->conn);
  63.     }
  64.  
  65.     /*
  66.     count total and new messages on server
  67.     */
  68.     function count()
  69.     {
  70.         $status = @imap_status($this->conn, "{".$this->connectstring."}".$this->mailbox, SA_ALL);
  71.         if ($this->is_imap())
  72.         {
  73.             $this->unseen = $status->unseen;
  74.         }
  75.  
  76.         $this->count = $status->messages;
  77.         return $status;
  78.     }
  79.  
  80.  
  81.     function status($mailbox)
  82.     {
  83.         $status = imap_status($this->conn, "{".$this->connectstring."}".$mailbox, SA_UNSEEN);
  84.         return $status;
  85.     }
  86.  
  87.     /*
  88.     return true when server is an IMAP server.
  89.     */
  90.  
  91.     function is_imap()
  92.     {
  93.         if ($this->servertype == "imap")
  94.         {
  95.             return true;
  96.         }else
  97.         {
  98.             return false;
  99.         }
  100.     }
  101.  
  102.     /*
  103.     Return sorted messages into $this->messages
  104.     */
  105.  
  106.     function sort($type = SORTDATE, $reverse = "1", $first_uid=0)
  107.     {
  108.         $this->next_message_id = $first_uid;
  109.         $this->messages = imap_sort($this->conn,$type,$reverse, SE_UID);
  110.     }
  111.  
  112.     /*
  113.     Loop through messages and return array with message info needed for inbox listing.
  114.     */
  115.  
  116.     function next_message()
  117.     {
  118.         if (!is_array($this->messages))
  119.         {
  120.             $this->sort();
  121.         }
  122.  
  123.         if (!isset($this->count))
  124.         {
  125.             $this->count;
  126.         }
  127.  
  128.         if ($this->next_message_id < $this->count)
  129.         {
  130.             unset($this->message);
  131.  
  132.             $uid = $this->messages[$this->next_message_id];
  133.             $this->message["uid"] = $uid;
  134.             $this->message['number'] = imap_msgno($this->conn, $uid);
  135.  
  136.             $headerinfo = imap_header($this->conn, $this->message['number']);
  137.  
  138.             $tmp = $headerinfo->from;
  139.             $this->message["sender"] = $tmp[0]->mailbox."@".$tmp[0]->host;
  140.             $this->message["from"] = $this->message["sender"];
  141.  
  142.             if (isset($tmp[0]->personal))
  143.             {
  144.                 $tmp = imap_mime_header_decode($tmp[0]->personal);
  145.                 if (isset($tmp[0]->text))
  146.                 {
  147.                     $this->message["from"] = htmlspecialchars($tmp[0]->text);
  148.                 }
  149.             }
  150.  
  151.             if (isset($headerinfo->to))
  152.             {
  153.                 $tmp = $headerinfo->to;
  154.                 for ($x=0;$x<sizeof($tmp);$x++)
  155.                 {
  156.                     $email = '';
  157.                     if (isset($tmp[$x]->mailbox))
  158.                     {
  159.                         $host = isset($tmp[$x]->host) ? '@'.$tmp[$x]->host : '';
  160.                         $email = htmlspecialchars($tmp[$x]->mailbox.$host);
  161.                     }
  162.  
  163.                     $personal = '';
  164.                     if (isset($tmp[$x]->personal))
  165.                     {
  166.                         $decoded = imap_mime_header_decode($tmp[$x]->personal);
  167.                         $personal = htmlspecialchars($decoded[0]->text);
  168.                     }
  169.  
  170.                     if ($personal != '' || $email != '')
  171.                     {
  172.                         $this->message["to"][$x] = $personal;
  173.  
  174.                         if ($email != '')
  175.                         {
  176.                             $this->message["to"][$x] .= "<".$email. ">";
  177.                         }
  178.                     }
  179.                 }
  180.             }
  181.  
  182.             if (isset($headerinfo->Subject))
  183.             {
  184.                 $tmp = imap_mime_header_decode($headerinfo->Subject);
  185.                 $this->message["subject"] = isset($tmp[0]->text) ? $tmp[0]->text : '';
  186.             }
  187.  
  188.             if (isset($headerinfo->Unseen) && isset($headerinfo->Recent) && $this->is_imap() && (($headerinfo->Unseen == 'U') || ($headerinfo->Recent == 'N')))
  189.             {
  190.                 $this->message["new"] = "1";
  191.             }else
  192.             {
  193.                 $this->message["new"] = "0";
  194.             }
  195.             $this->message["udate"] = $headerinfo->udate;
  196.  
  197.             $header = imap_fetchheader($this->conn, $uid, FT_UID);
  198.             $content_type = $this->get_header_value("Content-Type:", $header);
  199.             if (!empty($content_type) && eregi("([^/]*)/([^ ;\n\t]*)", $content_type, $regs)) {
  200.                 $mtype = strtolower($regs[1]);
  201.                 $stype = strtolower($regs[2]);
  202.             }else
  203.             {
  204.                 $mtype = '';
  205.                 $stype = '';
  206.             }
  207.  
  208.             if ($mtype != "text" && $stype != "alternative" && $stype != "related" && !empty($mtype) && !empty($stype))
  209.             {
  210.                 $this->message["attachment"] = "1";
  211.             }
  212.  
  213.             $this->message["priority"] = $this->get_header_value("X-Priority:", $header);
  214.  
  215.             $overview = imap_fetch_overview($this->conn, $uid, FT_UID);
  216.             $this->message["flagged"] = $overview[0]->flagged;
  217.             $this->message["size"] = $overview[0]->size;
  218.             $this->message["answered"] = $overview[0]->answered;
  219.             $this->next_message_id++;
  220.             return is_array($this->message);
  221.         }else
  222.         {
  223.             return false;
  224.         }
  225.     }
  226.  
  227.  
  228.     /*
  229.         return an array with all message info.
  230.     */
  231.  
  232.     function get_message($uid, $sort = "", $reverse = "", $preferred_type = "html", $part="")
  233.     {
  234.         if ($this->conn)
  235.         {
  236.             unset($this->message);
  237.  
  238.             //determine next and previous message
  239.             $this->sort($sort);
  240.  
  241.             for ($i=0;$i<sizeof($this->messages);$i++)
  242.             {
  243.                 if ($uid == $this->messages[$i])
  244.                 {
  245.                     $this->message["next"] = ($i - 1 >= 0) ? $this->messages[$i - 1] : 0;
  246.                     $this->message["previous"] = ($i + 1 < sizeof($this->messages)) ? $this->messages[$i + 1] : 0;
  247.                     break;
  248.                 }
  249.             }
  250.  
  251.             $this->message['number'] = imap_msgno($this->conn, $uid);
  252.  
  253.             $headerinfo = imap_header($this->conn, $this->message['number']);
  254.  
  255.             $this->message["udate"] = $headerinfo->udate;
  256.             if ((($headerinfo->Unseen == 'U') || ($headerinfo->Recent == 'N')) && $this->is_imap())
  257.             {
  258.                 $this->message["new"] = "1";
  259.             }else
  260.             {
  261.                 $this->message["new"] = "0";
  262.             }
  263.  
  264.             $tmp = $headerinfo->from;
  265.             $this->message["sender"] = $tmp[0]->mailbox.'@'.$tmp[0]->host;
  266.             $this->message["from"] = $this->message["sender"];
  267.  
  268.             if (isset($tmp[0]->personal))
  269.             {
  270.                 $tmp = imap_mime_header_decode($tmp[0]->personal);
  271.                 if (isset($tmp[0]->text))
  272.                 {
  273.                     $this->message["from"] = htmlspecialchars($tmp[0]->text);
  274.                 }
  275.             }
  276.  
  277.  
  278.             if (isset($headerinfo->Subject))
  279.             {
  280.                 $tmp = imap_mime_header_decode($headerinfo->Subject);
  281.                 $this->message["subject"] = isset($tmp[0]->text) ? htmlspecialchars($tmp[0]->text) : '';
  282.             }
  283.  
  284.             if (isset($headerinfo->to))
  285.             {
  286.                 $tmp = $headerinfo->to;
  287.                 for ($x=0;$x<sizeof($tmp);$x++)
  288.                 {
  289.                     $email = '';
  290.                     if (isset($tmp[$x]->mailbox))
  291.                     {
  292.                         $host = isset($tmp[$x]->host) ? '@'.$tmp[$x]->host : '';
  293.                         $email = htmlspecialchars($tmp[$x]->mailbox.$host);
  294.                     }
  295.  
  296.                     $personal = '';
  297.                     if (isset($tmp[$x]->personal))
  298.                     {
  299.                         $decoded = imap_mime_header_decode($tmp[$x]->personal);
  300.                         $personal = htmlspecialchars($decoded[0]->text);
  301.                     }
  302.  
  303.                     if ($personal != '' || $email != '')
  304.                     {
  305.                         $this->message["to"][$x] = $personal;
  306.  
  307.                         if ($email != '')
  308.                         {
  309.                             $this->message["to"][$x] .= "<".$email. ">";
  310.                         }
  311.                     }
  312.                 }
  313.             }
  314.             if (isset($headerinfo->cc))
  315.             {
  316.                 $tmp = $headerinfo->cc;
  317.                 for ($x=0;$x<sizeof($tmp);$x++)
  318.                 {
  319.                     $email = '';
  320.                     if (isset($tmp[$x]->mailbox))
  321.                     {
  322.                         $host = isset($tmp[$x]->host) ? '@'.$tmp[$x]->host : '';
  323.                         $email = htmlspecialchars($tmp[$x]->mailbox.$host);
  324.                     }
  325.  
  326.                     $personal = '';
  327.                     if (isset($tmp[$x]->personal))
  328.                     {
  329.                         $decoded = imap_mime_header_decode($tmp[$x]->personal);
  330.                         $personal = htmlspecialchars($decoded[0]->text);
  331.                     }
  332.  
  333.                     if ($personal != '' || $email != '')
  334.                     {
  335.                         $this->message["cc"][$x] = $personal;
  336.  
  337.                         if ($email != '')
  338.                         {
  339.                             $this->message["cc"][$x] .= "<".$email. ">";
  340.                         }
  341.                     }
  342.                 }
  343.             }
  344.  
  345.             if (isset($headerinfo->bcc))
  346.             {
  347.                 $tmp = $headerinfo->bcc;
  348.                 for ($x=0;$x<sizeof($tmp);$x++)
  349.                 {
  350.                     $email = '';
  351.                     if (isset($tmp[$x]->mailbox))
  352.                     {
  353.                         $host = isset($tmp[$x]->host) ? '@'.$tmp[$x]->host : '';
  354.                         $email = htmlspecialchars($tmp[$x]->mailbox.$host);
  355.                     }
  356.  
  357.                     $personal = '';
  358.                     if (isset($tmp[$x]->personal))
  359.                     {
  360.                         $decoded = imap_mime_header_decode($tmp[$x]->personal);
  361.                         $personal = htmlspecialchars($decoded[0]->text);
  362.                     }
  363.  
  364.                     if ($personal != '' || $email != '')
  365.                     {
  366.                         $this->message["bcc"][$x] = $personal;
  367.  
  368.                         if ($email != '')
  369.                         {
  370.                             $this->message["bcc"][$x] .= "<".$email. ">";
  371.                         }
  372.                     }
  373.                 }
  374.             }
  375.  
  376.  
  377.             $this->message["parts"] = array();
  378.  
  379.             if ($part == '')
  380.             {
  381.                 $structure = imap_fetchstructure($this->conn, $uid, FT_UID);
  382.             }else
  383.             {
  384.                 $structure = imap_bodystruct($this->conn, $this->message['number'], $part);
  385.             }
  386.             $this->mail["parts"] = array();
  387.             $this->get_parts($structure, $preferred_type);
  388.  
  389.             //$this->print_structure($structure);
  390.  
  391.             $header = imap_fetchheader($this->conn, $uid, FT_UID);
  392.             $this->message["priority"] = $this->get_header_value("X-Priority:", $header);
  393.             $this->message["notification"] = $this->get_header_value("Disposition-Notification-To:", $header);
  394.             $this->message["header"] = $header;
  395.  
  396.             $overview = imap_fetch_overview($this->conn, $uid, FT_UID);
  397.             $this->message["flagged"] = $overview[0]->flagged;
  398.             $this->message["size"] = $overview[0]->size;
  399.             $this->message["answered"] = $overview[0]->answered;
  400.  
  401.             return $this->message;
  402.  
  403.         }else
  404.         {
  405.             return false;
  406.         }
  407.     }
  408.  
  409.     /*
  410.         private function to get all the parts from a message.
  411.     */
  412.  
  413.     function get_parts($mimeobj,$preferred_type = "html", $section = 0)
  414.     {
  415.         if (isset($mimeobj->type))
  416.         {
  417.             $type = $this->get_mime_type($mimeobj->type);
  418.         }else
  419.         {
  420.             $type = 'text';
  421.         }
  422.  
  423.         $full_mime_type = $type ."/".$mimeobj->subtype;
  424.          $encoding = $this->get_encoding($mimeobj->encoding);
  425.  
  426.         if (isset($mimeobj->parameters))
  427.         {
  428.             $params = $mimeobj->parameters;
  429.             for ($x=0;$x<count($params);$x++)
  430.             {
  431.                 $param = $params[$x];
  432.                 if ((strtolower($param->attribute) == 'name') && ($param->value != ''))
  433.                 {
  434.                     $name = $param->value;
  435.                     break;
  436.                 }
  437.             }
  438.         }
  439.         $name = isset($name) ? $name : '';
  440.  
  441.         if ((!isset($name) || $name == "") && isset($mimeobj->dparameters))
  442.         {
  443.             $params = $mimeobj->dparameters;
  444.             for ($x=0;$x<count($params);$x++)
  445.             {
  446.                 $param = $params[$x];
  447.                 if ((strtolower($param->attribute) == 'filename') && ($param->value != ''))
  448.                 {
  449.                     $name = $param->value;
  450.                     break;
  451.                 }
  452.             }
  453.         }
  454.         $x=0;
  455.         if (isset($mimeobj->parts))
  456.         {
  457.             for($x=0;$x<count($mimeobj->parts);$x++)
  458.             {
  459.                 if ($mimeobj->subtype == "ALTERNATIVE" && $preferred_type == "html" && isset($mimeobj->parts[$x+1])) $x++;
  460.  
  461.                 // If we are in the root of the object increment by whole integers
  462.  
  463.                 if($section == 0)
  464.                 {
  465.                     $nsection = $x + 1;
  466.                 }else if(($pos = strrpos($section, ".")) && ($mimeobj->parts[0]->type != TYPEMULTIPART || $mimeobj->parts[0]->subtype != 'RELATED'))
  467.                 {
  468.                     $subsection = (int) substr($section, $pos+1)+$x;
  469.                     if ($subsection == '')
  470.                     {
  471.                         $subsection = '0';
  472.                     }
  473.                     $nsection = substr($section, 0, $pos) .    "." . ($subsection + 1);
  474.                 }else
  475.                 {
  476.                     $nsection = $section;
  477.                 }
  478.  
  479.                 // If there are more parts to the part about to be processed reference it as a header with ".0"
  480.                 // but only if the child of this child isn't MULTIPART
  481.  
  482.                 if(isset($mimeobj->parts[$x]->parts) && count($mimeobj->parts[$x]->parts))
  483.                 {
  484.                     // Funny really, if a mime section is a inline message that has a multipart body you reference the message
  485.                     // mime section with "2" the inline message header with "2.0" and the subsections with    "2.x"
  486.                     // However if the mime section is a inline message with only 1 part then you reference the
  487.                     // mime section in the message with 2.0 and the    inline message body with 2.1
  488.  
  489.                     if(!($mimeobj->parts[$x]->type == TYPEMESSAGE && $mimeobj->parts[$x]->parts[0]->type ==    TYPEMULTIPART))
  490.                     {
  491.                         $nsection .= ".0";
  492.                     }else
  493.                     {
  494.                         $nsection .= "";
  495.                     }
  496.                 }
  497.  
  498.                 $this->get_parts($mimeobj->parts[$x],$preferred_type, $nsection);
  499.  
  500.                 if ($mimeobj->subtype == "ALTERNATIVE" && $preferred_type == "plain") $x++;
  501.  
  502.             }
  503.         }
  504.  
  505.         // If after processing the entire MIME object the $x variable is still zero then we didn't
  506.         // process a multipart mime message.
  507.  
  508.         if($x == 0 && $section == 0)
  509.         {
  510.              $section = "1";
  511.         }
  512.  
  513.         if ($type != "multipart" && $full_mime_type)
  514.         {
  515.             if (eregi('message', $full_mime_type))
  516.             {
  517.                 $section++;
  518.             }
  519.             $bytes = isset($mimeobj->bytes) ? $mimeobj->bytes : 0;
  520.             $tmp = Array(
  521.                 'number' => $section,
  522.                 'id' => $mimeobj->ifid ? $mimeobj->id : 0,
  523.                 'name' => $name,
  524.                 'mime' => $full_mime_type,
  525.                 'transfer' => $encoding,
  526.                 'disposition' => $mimeobj->ifdisposition ? $mimeobj->disposition : '',
  527.                 'size' => $bytes
  528.                 );
  529.  
  530.             array_unshift($this->message["parts"], $tmp);
  531.         }
  532.  
  533.     }
  534.  
  535.     function print_structure($mimeobj, $depth = 0, $section = 0)
  536.     {
  537.         for($y = 0; $y < $depth; $y++)
  538.         {
  539.             echo("       ");
  540.         }
  541.         echo($this->get_mime_type($mimeobj->type) . "/{$mimeobj->subtype},");
  542.         echo($this->get_encoding($mimeobj->encoding) . "(<B>$section</B>)<br>");
  543.  
  544.         $x=0;
  545.         if (isset($mimeobj->parts))
  546.         {
  547.  
  548.             for($x = 0; $x < count($mimeobj->parts); $x++)
  549.             {
  550.                     // If we are in the root of the object increment by whole    integers
  551.  
  552.                 if($section == 0)
  553.                 {
  554.                     $nsection = $x + 1;
  555.                     $subsection = 0;
  556.                     // If we are in the object and the first sub-object of our    object isn't multipart
  557.                     // then increment the postfix by ".1" otherwise    we are multipart or a message
  558.                     // and leave the section id alone to be handled by the next    code block
  559.  
  560.                     //else if(($pos = strrpos($section, ".")) && sizeof($mimeobj->parts) > 1)
  561.                 }else if(($pos = strrpos($section, ".")) && ($mimeobj->parts[0]->type != TYPEMULTIPART || $mimeobj->parts[0]->subtype != 'RELATED'))
  562.                 //}elseif($pos = strrpos($section, "."))
  563.                 {
  564.                     $subsection = (int) substr($section, $pos+1)+$x;
  565.                     if ($subsection == '')
  566.                     {
  567.                         $subsection = '0';
  568.                     }
  569.                     $nsection = substr($section, 0, $pos) .    "." . ($subsection + 1);
  570.                 }else
  571.                 {
  572.                     $nsection = $section;
  573.  
  574.                 }
  575.  
  576.                 // If there are more parts to the part about to be processed reference it as a header with ".0"
  577.                 // but only if the child of this child isn't MULTIPART
  578.  
  579.                 if(isset($mimeobj->parts[$x]->parts) && count($mimeobj->parts[$x]->parts))
  580.                 {
  581.                     // Funny really, if a mime section is a inline message that has a multipart body you reference the message
  582.                     // mime section with "2" the inline    message header with "2.0" and the subsections with    "2.x"
  583.                     // However if the mime section is a inline message with only 1 part then you reference the
  584.                     // mime section in the message with 2.0 and the    inline message body with 2.1
  585.  
  586.                     if(!($mimeobj->parts[$x]->type == TYPEMESSAGE && $mimeobj->parts[$x]->parts[0]->type ==    TYPEMULTIPART))
  587.                     {
  588.                         $nsection .= ".0";
  589.                     }else
  590.                     {
  591.                         $nsection .= "";
  592.                     }
  593.                 }
  594.  
  595.                 $this->print_structure($mimeobj->parts[$x], $depth + 1, $nsection);
  596.             }
  597.         }
  598.  
  599.         // If after processing the entire MIME object the $x variable is still zero then we didn't
  600.         // process a multipart mime message, it's just normal email so say so here.
  601.  
  602.         if($x == 0 && $section == 0)
  603.         {
  604.                 echo($this->get_mime_type($mimeobj->type) . "/{$mimeobj->subtype}, ");
  605.                 echo($this->get_encoding($mimeobj->encoding) . "(<B>1</B>) (<B>NOT MIME MULTIPART</B>)<br>");
  606.         }
  607.     }
  608.  
  609.  
  610.     /*
  611.         private function to get mimetype in text
  612.     */
  613.  
  614.     function get_encoding($encoding)
  615.     {
  616.         switch ($encoding)
  617.         {
  618.             case 0:
  619.                 $encoding = '7BIT';
  620.                 break;
  621.             case 1:
  622.                 $encoding = '8BIT';
  623.                 break;
  624.             case 2:
  625.                 $encoding = 'BINARY';
  626.                 break;
  627.             case 3:
  628.                 $encoding = 'BASE64';
  629.                 break;
  630.             case 4:
  631.                 $encoding = 'QUOTED-PRINTABLE';
  632.                 break;
  633.             case 5:
  634.                 $encoding = 'OTHER';
  635.                 break;
  636.             default:
  637.                 $encoding = 'none';
  638.                 break;
  639.         }
  640.  
  641.         return $encoding;
  642.     }
  643.  
  644.     /*
  645.         private function to get encoding in text
  646.     */
  647.  
  648.     function get_mime_type($type)
  649.     {
  650.         switch ($type)
  651.         {
  652.             case 0:
  653.                 $mime_type = 'text';
  654.                 break;
  655.             case 1:
  656.                 $mime_type = 'multipart';
  657.                 break;
  658.             case 2:
  659.                 $mime_type = 'message';
  660.                 break;
  661.             case 3:
  662.                 $mime_type = 'application';
  663.                 break;
  664.             case 4:
  665.                 $mime_type = 'audio';
  666.                 break;
  667.             case 5:
  668.                 $mime_type = 'image';
  669.                 break;
  670.             case 6:
  671.                 $mime_type = 'video';
  672.                 break;
  673.             case 7:
  674.                 $mime_type = 'other';
  675.                 break;
  676.             default:
  677.                 $mime_type = 'unknown';
  678.         }
  679.         return $mime_type;
  680.     }
  681.  
  682.  
  683.     function get_header_value($fieldname, $header)
  684.     {
  685.         $resu = '';
  686.         $header = eregi_replace("\t", " ", $header);
  687.         $results = array();
  688.         if (eregi("$fieldname (.*)", $header, $results)) {
  689.             $fieldval = $results[1];
  690.             for ($b=0;$b<=strlen($fieldval);$b++) {
  691.                 $curr = substr($fieldval, $b, 1);
  692.                 $next = substr($fieldval, $b + 1, 1);
  693.                 if ($curr == "\n" && $next != " ") {
  694.                     break;
  695.                 }
  696.                 if ($curr == "\t") { $curr = " "; }
  697.                 if ($curr == "\n") { $curr = ""; }
  698.                 $resu .= $curr;
  699.             }
  700.         }
  701.         $resu = eregi_replace("\([^\)]*\)", "", $resu);
  702.         return trim($resu);
  703.     }
  704.  
  705.     function delete($messages)
  706.     {
  707.         for ($i=0;$i<count($messages);$i++)
  708.         {
  709.             @imap_delete($this->conn,$messages[$i], FT_UID);
  710.         }
  711.         @imap_expunge($this->conn);
  712.     }
  713.  
  714.     function view_part($uid, $part_no, $transfer)
  715.     {
  716.         $text = imap_fetchbody($this->conn, $uid, $part_no, FT_UID);
  717.         if ($transfer == 'BASE64')
  718.             $str = imap_base64($text);
  719.         elseif($transfer == 'QUOTED-PRINTABLE')
  720.             $str = quoted_printable_decode($text);
  721.         else
  722.             $str = $text;
  723.  
  724.         return ($str);
  725.     }
  726.  
  727.  
  728.     function f($name)
  729.     {
  730.         $value = isset($this->message[$name]) ? $this->message[$name] : false;
  731.         return $value;
  732.     }
  733.  
  734.     /*
  735.     function get_mailboxes($mailbox_root)
  736.     {
  737.             $this->mailboxes=array();
  738.             $list = imap_getmailboxes($this->conn,"{".$this->connectstring."}",$mailbox_root.'*');
  739.             if(is_array($list))
  740.             {
  741.                 while (list($key, $val) = each($list))
  742.                 {
  743.                     $this->mailboxes[] = str_replace("{".$this->connectstring."}","",imap_utf7_decode($val->name));
  744.  
  745.                 }
  746.                 reset($list);
  747.             }
  748.  
  749.             return $this->mailboxes;
  750.     }
  751.     */
  752.     /*
  753.     function get_mailboxes($mailbox_root)
  754.     {
  755.         $this->mailboxes=array();
  756.         $list = imap_getmailboxes($this->conn,"{".$this->connectstring."}",$mailbox_root.'*');
  757.         if(is_array($list))
  758.         {
  759.             while (list($key, $val) = each($list))
  760.             {
  761.                 $mailbox['name'] = str_replace("{".$this->connectstring."}","",imap_utf7_decode($val->name));
  762.                 if (isset($val->delimiter) && strlen($val->delimiter) > 0)
  763.                 {
  764.                     $mailbox['delimiter'] = $val->delimiter;
  765.                 }else
  766.                 {
  767.                     $mailbox['delimiter'] = '/';
  768.                 }
  769.                 $mailbox['attributes'] = $val->attributes;
  770.                 $this->mailboxes[] = $mailbox;
  771.  
  772.             }
  773.             reset($list);
  774.         }
  775.  
  776.         return $this->mailboxes;
  777.     }
  778.  
  779.     function get_mailboxes($mailbox_root, $name_only=false)
  780.     {
  781.         $this->mailboxes = array();
  782.         $list = imap_getmailboxes($this->conn, "{".$this->connectstring."}", $mailbox_root.'%');
  783.         if(is_array($list))
  784.         {
  785.             foreach($list as $value)
  786.             {
  787.                 if(substr($value->name, -1) != $value->delimiter && strlen($value->delimiter) > 0)
  788.                 {
  789.                     if ($name_only)
  790.                     {
  791.                         $this->mailboxes[]= str_replace("{".$this->connectstring."}","",imap_utf7_decode($value->name));
  792.                     }else
  793.                     {
  794.                         $mailbox['name'] = str_replace("{".$this->connectstring."}","",imap_utf7_decode($value->name));
  795.                         $mailbox['delimiter'] = $value->delimiter;
  796.                         $mailbox['attributes'] = $value->attributes;
  797.                         $this->mailboxes[] = $mailbox;
  798.                         if(!($mailbox['attributes']&LATT_NOINFERIORS) && $mailbox['name'] != $mailbox_root)// && $mailbox['name'] != "INBOX")
  799.                         {
  800.                             $this->mailboxes = array_merge($this->mailboxes, $this->get_mailboxes($mailbox['name'].$mailbox['delimiter']));
  801.                         }
  802.                     }
  803.                 }
  804.             }
  805.         }
  806.  
  807.         return $this->mailboxes;
  808.     }
  809.  
  810.  */
  811.      function get_mailbox_delimiter()
  812.      {
  813.         $list = imap_getmailboxes($this->conn, "{".$this->connectstring."}", '%');
  814.         if(is_array($list))
  815.         {
  816.             $folder = array_shift($list);
  817.             if (strlen($folder['delimiter']) > 0)
  818.             {
  819.                 return $folder['delimiter'];
  820.             }
  821.         }
  822.         return false;
  823.     }
  824.      function get_mailboxes($name_only=false)
  825.     {
  826.         $this->mailboxes = array();
  827.         $list = imap_getmailboxes($this->conn, "{".$this->connectstring."}", '*');
  828.         if(is_array($list))
  829.         {
  830.             foreach($list as $value)
  831.             {
  832.                 if(substr($value->name, -1) != $value->delimiter && strlen($value->delimiter) > 0)
  833.                 {
  834.                     if ($name_only)
  835.                     {
  836.                         $this->mailboxes[]= str_replace("{".$this->connectstring."}","",imap_utf7_decode($value->name));
  837.                     }else
  838.                     {
  839.                         $mailbox['name'] = str_replace("{".$this->connectstring."}","",imap_utf7_decode($value->name));
  840.                         $mailbox['delimiter'] = $value->delimiter;
  841.                         $mailbox['attributes'] = $value->attributes;
  842.                         $this->mailboxes[] = $mailbox;
  843.                     }
  844.                 }
  845.             }
  846.         }
  847.         return $this->mailboxes;
  848.       }
  849.  
  850.       function get_subscribed($name_only=false)
  851.     {
  852.         $this->mailboxes = array();
  853.         $list = imap_getsubscribed($this->conn, "{".$this->connectstring."}", '*');
  854.         if(is_array($list))
  855.         {
  856.             foreach($list as $value)
  857.             {
  858.                 if(substr($value->name, -1) != $value->delimiter && strlen($value->delimiter) > 0)
  859.                 {
  860.                     if ($name_only)
  861.                     {
  862.                         $this->mailboxes[]= str_replace("{".$this->connectstring."}","",imap_utf7_decode($value->name));
  863.                     }else
  864.                     {
  865.                         $mailbox['name'] = str_replace("{".$this->connectstring."}","",imap_utf7_decode($value->name));
  866.                         $mailbox['delimiter'] = $value->delimiter;
  867.                         $mailbox['attributes'] = $value->attributes;
  868.                         $this->mailboxes[] = $mailbox;
  869.                     }
  870.                 }
  871.             }
  872.         }
  873.         return $this->mailboxes;
  874.       }
  875.     /*
  876.     function get_subscribed($mailbox_root)
  877.     {
  878.         $this->mailboxes=array();
  879.  
  880.         $list = imap_getsubscribed($this->conn,"{".$this->connectstring."}",$mailbox_root.'*');
  881.         if(is_array($list))
  882.         {
  883.             while (list($key, $val) = each($list))
  884.             {
  885.                 $mailbox['name'] = str_replace("{".$this->connectstring."}","",imap_utf7_decode($val->name));
  886.                 if (isset($val->delimiter) && strlen($val->delimiter) > 0)
  887.                 {
  888.                     $mailbox['delimiter'] = $val->delimiter;
  889.                 }else
  890.                 {
  891.                     $mailbox['delimiter'] = '/';
  892.                 }
  893.                 $mailbox['attributes'] = $val->attributes;
  894.                 $this->mailboxes[] = $mailbox;
  895.             }
  896.             reset($list);
  897.  
  898.         }
  899.         return $this->mailboxes;
  900.     }
  901.  
  902.     function get_subscribed($mailbox_root, $name_only=false)
  903.     {
  904.         $this->mailboxes = array();
  905.         $list = imap_getsubscribed($this->conn, "{".$this->connectstring."}", $mailbox_root.'%');
  906.         if(is_array($list))
  907.         {
  908.             foreach($list as $value)
  909.             {
  910.                 if(substr($value->name, -1) != $value->delimiter && strlen($value->delimiter) > 0)
  911.                 {
  912.                     if ($name_only)
  913.                     {
  914.                         $this->mailboxes[] = str_replace("{".$this->connectstring."}","",imap_utf7_decode($value->name));
  915.                     }else
  916.                     {
  917.                         $mailbox['name'] = str_replace("{".$this->connectstring."}","",imap_utf7_decode($value->name));
  918.                         $mailbox['delimiter'] = $value->delimiter;
  919.                         $mailbox['attributes'] = $value->attributes;
  920.                         $this->mailboxes[] = $mailbox;
  921.                         if(!($mailbox['attributes']&LATT_NOINFERIORS) && $mailbox['name'] != $mailbox_root)// && $mailbox['name'] != "INBOX")
  922.                         {
  923.                             $this->mailboxes = array_merge($this->mailboxes, $this->get_subscribed($mailbox['name'].$mailbox['delimiter']));
  924.                         }
  925.                     }
  926.                 }
  927.             }
  928.         }
  929.         return $this->mailboxes;
  930.     } */
  931.  
  932.     function is_subscribed($name, $mailbox_root)
  933.     {
  934.         $this->get_subscribed($mailbox_root);
  935.         for ($i=0;$i<count($this->mailboxes);$i++)
  936.         {
  937.             if ($this->mailboxes[$i]['name']==$name)
  938.             {
  939.                 return true;
  940.             }
  941.         }
  942.         return false;
  943.     }
  944.  
  945.     function subscribe($name)
  946.     {
  947.         return imap_subscribe($this->conn, "{".$this->connectstring."}".imap_utf7_encode($name));
  948.     }
  949.  
  950.     function unsubscribe($name)
  951.     {
  952.         return imap_unsubscribe($this->conn, "{".$this->connectstring."}".imap_utf7_encode($name));
  953.     }
  954.  
  955.     function delete_folder($name, $mailbox_root)
  956.     {
  957.         if ($this->is_subscribed($name, $mailbox_root))
  958.         {
  959.             if ($this->unsubscribe($name))
  960.             {
  961.                 return imap_deletemailbox($this->conn,"{".$this->connectstring."}".imap_utf7_encode($name));
  962.             }
  963.             return false;
  964.         }else
  965.         {
  966.             return imap_deletemailbox($this->conn,"{".$this->connectstring."}".imap_utf7_encode($name));
  967.         }
  968.     }
  969.  
  970.     function create_folder($name)
  971.     {
  972.         if (imap_createmailbox($this->conn,"{".$this->connectstring."}".imap_utf7_encode($name)))
  973.         {
  974.             return $this->subscribe($name);
  975.         }
  976.     }
  977.  
  978.     function rename_folder($old_name, $new_name)
  979.     {
  980.         if ($this->unsubscribe($old_name))
  981.         {
  982.             if (imap_renamemailbox($this->conn,"{".$this->connectstring."}".$old_name,"{".$this->connectstring."}".$new_name))
  983.             {
  984.                 return $this->subscribe($new_name);
  985.             }
  986.         }
  987.         return false;
  988.     }
  989.  
  990.     function move($folder, $messages)
  991.     {
  992.         $messageset = implode (",",$messages);
  993.         if (imap_mail_move($this->conn,$messageset,$folder, CP_UID))
  994.         {
  995.             imap_expunge($this->conn);
  996.             return true;
  997.         }
  998.         return false;
  999.     }
  1000.  
  1001.     function get_quota()
  1002.     {
  1003.         return imap_get_quota($this->conn,"user.".$this->username);
  1004.     }
  1005.  
  1006.     function append_message($mailbox, $body, $flags = "")
  1007.     {
  1008.         if(@imap_append($this->conn, "{".$this->connectstring."}".$mailbox, $body, $flags))
  1009.         {
  1010.             return true;
  1011.         }else
  1012.         {
  1013.             return false;
  1014.         }
  1015.     }
  1016.  
  1017.     function set_message_flag($mailbox = "INBOX", $uid_array, $flags, $action = "")
  1018.     {
  1019.         if($this->reopen($mailbox))
  1020.         {
  1021.             $msgno_set = implode(",",$uid_array);
  1022.  
  1023.             if ($action == "reset")
  1024.             {
  1025.                 if(imap_clearflag_full($this->conn, $msgno_set, $flags, ST_UID))
  1026.                 {
  1027.                     return true;
  1028.                 }else
  1029.                 {
  1030.                     return false;
  1031.                 }
  1032.             }else
  1033.             {
  1034.                 if(imap_setflag_full($this->conn, $msgno_set, $flags, ST_UID))
  1035.                 {
  1036.                     return true;
  1037.                 }else
  1038.                 {
  1039.                     return false;
  1040.                 }
  1041.             }
  1042.         }else
  1043.         {
  1044.             return false;
  1045.         }
  1046.     }
  1047.  
  1048.     function reopen($mailbox = "INBOX", $flags = "")
  1049.     {
  1050.         if(imap_reopen($this->conn, "{".$this->connectstring."}".$mailbox, $flags))
  1051.         {
  1052.             return true;
  1053.         }else
  1054.         {
  1055.             return false;
  1056.         }
  1057.     }
  1058.  
  1059.     function mailbox_info()
  1060.     {
  1061.         $info = imap_mailboxmsginfo($this->conn);
  1062.         if ($info)
  1063.         {
  1064.             $this->mailbox_info = array();
  1065.             $this->mailbox_info["date"]        = $info->Date;
  1066.             $this->mailbox_info["driver"]        = $info->Driver;
  1067.             $this->mailbox_info["mailbox"]        = $info->Mailbox;
  1068.             $this->mailbox_info["nmsgs"]        = $info->Nmsgs;
  1069.             $this->mailbox_info["recent"]        = $info->Recent;
  1070.             $this->mailbox_info["size"]        = $info->Size;
  1071.             return true;
  1072.         }else
  1073.         {
  1074.             return false;
  1075.         }
  1076.     }
  1077. }
  1078. ?>
  1079.