function download_attachment($mbox, $msgno)
 {
     include_once "class.message_components.inc.php";
     $msg = new message_components($mbox);
     $msg->fetch_structure($msgno);
     $array_parts_attachments = array();
     //$array_parts_attachments['names'] = '';
     //print_r($msg->fname[$msgno]);
     if (count($msg->fname[$msgno]) > 0) {
         $i = 0;
         foreach ($msg->fname[$msgno] as $index => $fname) {
             $array_parts_attachments[$i]['pid'] = $msg->pid[$msgno][$index];
             $array_parts_attachments[$i]['name'] = $this->decode_mimeheader($fname);
             //$array_parts_attachments[$i]['name'] = $this->flat_mime_decode($fname);
             $array_parts_attachments[$i]['name'] = $array_parts_attachments[$i]['name'] ? $array_parts_attachments[$i]['name'] : "attachment.bin";
             $array_parts_attachments[$i]['encoding'] = $msg->encoding[$msgno][$index];
             //$array_parts_attachments['names'] .= $array_parts_attachments[$i]['name'] . ', ';
             $array_parts_attachments[$i]['fsize'] = $msg->fsize[$msgno][$index];
             ++$i;
         }
     }
     //$array_parts_attachments['names'] = substr($array_parts_attachments['names'],0,(strlen($array_parts_attachments['names']) - 2));
     return $array_parts_attachments;
 }
 function quickSearchMail($params)
 {
     require_once dirname(__FILE__) . '/../../prototype/api/controller.php';
     set_time_limit(270);
     //Aumenta o tempo limit da requisição, em algumas buscas o imap demora para retornar o resultado.
     $return = array();
     $return['folder'] = $params['folder'];
     if (!is_array($params['folder'])) {
         $params['folder'] = array($params['folder']);
     }
     if (!isset($params['sortType'])) {
         $params['sortType'] = 'SORTDATE_REVERSE';
     }
     $params['search'] = mb_convert_encoding($params['search'], 'UTF-8', mb_detect_encoding($params['search'] . 'x', 'UTF-8, ISO-8859-1'));
     $i = 0;
     if (!isset($params['page'])) {
         $params['page'] = 0;
     }
     $end = $this->prefs['max_email_per_page'] * ((int) $params['page'] + 1);
     $ini = $end - $this->prefs['max_email_per_page'];
     $count = 0;
     if (!preg_match('/KEYWORD/i', $params['search'])) {
         $search = $this->parseCriteriaSearchMail($params['search']);
     } else {
         $search = $params['search'];
     }
     foreach ($params['folder'] as $folder) {
         $imap = $this->open_mbox($folder);
         $msgIds = imap_sort($imap, SORTDATE, 1, SE_UID, $search, 'UTF-8');
         $count += count($msgIds);
         foreach ($msgIds as $ii => $v) {
             $msg = imap_headerinfo($imap, imap_msgno($imap, $v));
             $return['msgs'][$i]['from'] = '';
             if (isset($msg->from[0])) {
                 $from = self::formatMailObject($msg->from[0]);
                 $return['msgs'][$i]['from'] = mb_convert_encoding($from['name'] ? $from['name'] : $from['email'], 'UTF-8');
             } else {
                 $return['msgs'][$i]['from'] = '';
             }
             $return['msgs'][$i]['subject'] = ' ';
             $subject = imap_mime_header_decode($msg->subject);
             foreach ($subject as $tmp) {
                 $return['msgs'][$i]['subject'] .= mb_convert_encoding($tmp->text, 'UTF-8', 'UTF-8 , ISO-8859-1');
             }
             if ($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_followupflags_and_labels'] == "2") {
                 $filter = array('AND', array('=', 'folderName', $folder), array('=', 'messageNumber', $v));
                 $followupflagged = Controller::find(array('concept' => 'followupflagged', 'folder' => $folder), false, array('filter' => $filter, 'criteria' => array('deepness' => '2')));
                 if (isset($followupflagged[0]['followupflagId'])) {
                     $followupflag = Controller::read(array('concept' => 'followupflag', 'id' => $followupflagged[0]['followupflagId']));
                     $followupflagged[0]['followupflag'] = $followupflag;
                     $return['msgs'][$i]['followupflagged'] = $followupflagged[0];
                 }
                 $labeleds = Controller::find(array('concept' => 'labeled'), false, array('filter' => $filter, 'criteria' => array('deepness' => '2')));
                 if (is_array($labeleds)) {
                     foreach ($labeleds as $e) {
                         $labels = Controller::read(array('concept' => 'label', 'id' => $e['labelId']));
                         $return['msgs'][$i]['labels'][$e['labelId']] = $labels;
                     }
                 }
             }
             // os comandos abaixos foram retirados pois deixam a busca lenta.
             // $mimeBody = imap_body( $this->mbox, $v  , FT_UID|FT_PEEK  );
             // $return['msgs'][$i]['flag'] .= ( preg_match('/((Content-Disposition:(.)*([\r\n\s]*filename))|(Content-Type:(.)*([\r\n\s]*name)))/i', $mimeBody) ) ? 'T': '';         //
             $return['msgs'][$i]['flag'] = ' ';
             $return['msgs'][$i]['flag'] .= $msg->Unseen ? $msg->Unseen : '';
             $return['msgs'][$i]['flag'] .= $msg->Recent ? $msg->Recent : '';
             $return['msgs'][$i]['flag'] .= $msg->Draft ? $msg->Draft : '';
             $return['msgs'][$i]['flag'] .= $msg->Answered ? $msg->Answered : '';
             $return['msgs'][$i]['flag'] .= $msg->Deleted ? $msg->Deleted : '';
             $msgComponents = new message_components($this->mbox);
             $msgComponents->fetch_structure($v);
             $return['msgs'][$i]['flag'] .= count($msgComponents->fname[$v]) > 0 ? 'T' : '';
             $header = imap_fetchheader($imap, $v, FT_UID);
             // Necessario para recuperar se a mensagem é importante ou não.
             $importante = array();
             if ($msg->Flagged != 'F') {
                 $return['msgs'][$i]['flag'] .= preg_match('/importance\\s{0,}:\\s{0,}(high).*/i', $header, $importante) === 0 ? '' : 'F';
             } else {
                 $return['msgs'][$i]['flag'] .= $msg->Flagged ? $msg->Flagged : '';
             }
             $return['msgs'][$i]['udate'] = gmdate("d/m/Y", $msg->udate + $this->functions->CalculateDateOffset());
             $return['msgs'][$i]['udatecomp'] = substr($return['msgs'][$i]['udate'], -4) . "-" . substr($return['msgs'][$i]['udate'], 3, 2) . "-" . substr($return['msgs'][$i]['udate'], 0, 2);
             $return['msgs'][$i]['date'] = $msg->udate;
             $return['msgs'][$i]['size'] = $msg->Size;
             $return['msgs'][$i]['boxname'] = $folder;
             $return['msgs'][$i]['uid'] = $v;
             ++$i;
         }
     }
     $return['num_msgs'] = $count;
     if (!isset($return['msgs'])) {
         $return['msgs'] = array();
     }
     define('SORTBOX', 69);
     define('SORTWHO', 2);
     define('SORTBOX_REVERSE', 69);
     define('SORTWHO_REVERSE', 2);
     define('SORTDATE_REVERSE', 0);
     define('SORTSUBJECT_REVERSE', 3);
     define('SORTSIZE_REVERSE', 6);
     switch (constant($params['sortType'])) {
         case 0:
             $sA = 'date';
             break;
         case 2:
             $sA = 'from';
             break;
         case 69:
             $sA = 'boxname';
             break;
         case 3:
             $sA = 'subject';
             break;
         case 6:
             $sA = 'size';
             break;
     }
     if ($params['sortType'] !== 'SORTDATE_REVERSE') {
         if (strpos($params['sortType'], 'REVERSE') !== false) {
             $return['msgs'] = $this->array_msort($return['msgs'], array($sA => SORT_DESC));
         } else {
             $return['msgs'] = $this->array_msort($return['msgs'], array($sA => SORT_ASC));
         }
     }
     $k = -1;
     $nMsgs = array();
     foreach ($return['msgs'] as $v) {
         ++$k;
         if ($k < $ini || $k >= $end) {
             continue;
         }
         $nMsgs[] = $v;
     }
     $return['msgs'] = $nMsgs;
     $return = json_encode($return);
     $return = base64_encode($return);
     return $return;
 }