Ignore:
Timestamp:
03/26/13 14:29:24 (11 years ago)
Author:
gustavo
Message:

Ticket #000 - Commit contendo o expresso com solr funcionando corretamente

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sandbox/expresso-solr/expressoMail1_2/inc/class.imap_functions.inc.php

    r7552 r8056  
    40054005                $return = array(); 
    40064006                $folders = $this->get_folders_list(); 
     4007                $useSolr = false;                
    40074008 
    40084009                $j = 0; 
     
    42004201 
    42014202        function search_msg( $params = false ) 
    4202         { 
    4203          
     4203        {        
    42044204                include '../prototype/api/controller.php'; 
     4205                include 'solrclient/library/Solarium/Autoloader.php'; 
     4206                Solarium_Autoloader::register(); 
     4207 
     4208                $useSolr = false; 
     4209 
    42054210                if(strpos($params['condition'],"#")===false) 
    42064211                { //local messages 
     
    42124217                } 
    42134218                 
    4214                 $params['page'] = $params['page'] * 1; 
    4215  
    4216             if( is_array($search) ) 
    4217             { 
    4218                         $search = array_unique($search); // Remove duplicated folders 
    4219                         $search_criteria = ''; 
    4220                         $search_result_number = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['search_result_number']; 
    4221                         foreach($search as $tmp) 
    4222                         { 
    4223                                 $tmp1 = explode("##",$tmp); 
    4224                                 $sum = 0; 
    4225                                 $name_box = $tmp1[0]; 
    4226                                 unset($filter); 
    4227                                 foreach($tmp1 as $index => $criteria) 
     4219                if(isset($_SESSION['phpgw_info']['expresso']['expressoMail']['expressoMail_enable_solr']) && $_SESSION['phpgw_info']['expresso']['expressoMail']['expressoMail_enable_solr'] === 'true') 
     4220                { 
     4221                        $useSolr = true; 
     4222                } 
     4223                $count = 0; 
     4224                 
     4225                if($useSolr == true) 
     4226                { 
     4227                        $params['page'] = $params['page'] * 1; 
     4228 
     4229                        if( is_array($search) ) 
     4230                        { 
     4231                                $search = array_unique($search); // Remove duplicated folders 
     4232                                $search_criteria = ''; 
     4233                                $search_result_number = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['search_result_number']; 
     4234                                 
     4235                                $solrConfig = array( 
     4236                                                'adapteroptions' => array( 
     4237                                                'host' => '127.0.0.1', 
     4238                                                'port' => 8983, 
     4239                                                'path' => '/solr/', 
     4240                                                ) 
     4241                                        ); 
     4242                                // create a client instance 
     4243                                $solariumClient = new Solarium_Client($solrConfig); 
     4244                                $solrQuery = $solariumClient->createSelect(); 
     4245                                $usernameAux = $this->username; 
     4246                                $solrSearchString = ""; 
     4247 
     4248                                foreach($search as $tmp) 
    42284249                                { 
    4229                                         if ($index != 0 && strlen($criteria) != 0) 
     4250                                        $tmp1 = explode("##",$tmp); 
     4251                                        $sum = 0; 
     4252                                        $name_box = $tmp1[0]; 
     4253                                        $filter = ""; 
     4254                                        $solrSearchString = "("; 
     4255                                         
     4256                                        foreach($tmp1 as $index => $criteria) 
    42304257                                        { 
    4231                                                 $filter_array = explode("<=>",html_entity_decode(rawurldecode($criteria))); 
    4232                                                 $filter .= " ".$filter_array[0]; 
    4233                                                 if (strlen($filter_array[1]) != 0) 
     4258                                                if ($index != 0 && strlen($criteria) != 0) 
    42344259                                                { 
    4235                                                         if ( trim($filter_array[0]) != 'BEFORE' && 
    4236                                                                  trim($filter_array[0]) != 'SINCE' && 
    4237                                                                  trim($filter_array[0]) != 'ON') 
     4260                                                        $filter_array = explode("<=>",html_entity_decode(rawurldecode($criteria))); 
     4261                                                        //$filter .= " ".$filter_array[0]; 
     4262                                                        if (strlen($filter_array[1]) != 0) 
    42384263                                                        { 
    4239                                                             $filter .= '"'.$filter_array[1].'"'; 
    4240                                                         }else if(trim($filter_array[0]) == 'BEFORE' ){ 
    4241                                                             $filter .= '"'.$this->make_search_date($filter_array[1],true).'"'; 
    4242                                                         }else{ 
    4243                                                             $filter .= '"'.$this->make_search_date($filter_array[1]).'"'; 
    4244                                                         } 
     4264                                                                 
     4265                                                                $filter_array[1] = mb_convert_encoding($filter_array[1], 'UTF-8',mb_detect_encoding($filter_array[1].'x', 'UTF-8, ISO-8859-1')); 
     4266 
     4267                                                                if ( strlen(trim($solrSearchString)) > 1 ) 
     4268                                                                { 
     4269                                                                        $solrSearchString .= " OR "; 
     4270                                                                } 
     4271                                                                 
     4272                                                                 
     4273                                                                if ( trim($filter_array[0]) == 'SUBJECT' ) 
     4274                                                                { 
     4275                                                                        $solrSearchString .= "subject:\"".trim($filter_array[1])."\""; 
     4276                                                                } 
     4277                                                                else if ( trim($filter_array[0]) == 'FROM' ) 
     4278                                                                { 
     4279                                                                        $solrSearchString .= "from:\"".trim($filter_array[1])."\""; 
     4280                                                                } 
     4281                                                                else if ( trim($filter_array[0]) == 'TO' ) 
     4282                                                                { 
     4283                                                                        $solrSearchString .= "to:\"".trim($filter_array[1])."\""; 
     4284                                                                } 
     4285                                                                else if ( trim($filter_array[0]) == 'BODY' ) 
     4286                                                                { 
     4287                                                                        $solrSearchString .= "content:\"".trim($filter_array[1])."\""; 
     4288                                                                } 
     4289                                                                else if ( trim($filter_array[0]) == 'CC' ) 
     4290                                                                { 
     4291                                                                        $solrSearchString .= "copyto:\"".trim($filter_array[1])."\""; 
     4292                                                                } 
     4293                                                        } 
    42454294                                                } 
    42464295                                        } 
     4296 
     4297                                        $solrSearchString .= ")"; 
     4298 
     4299                                        $name_box = mb_convert_encoding(utf8_decode($name_box), "UTF7-IMAP", "ISO-8859-1"); 
     4300                                        if(count(explode("/", $name_box)) > 1 ) 
     4301                                        { 
     4302                                                $arrayAux = explode("/", $name_box); 
     4303                                                $iArrayAux = count(explode("/", $name_box)); 
     4304                                                 
     4305                                                $name_box = $arrayAux[$iArrayAux-1]; 
     4306                                        } 
     4307 
     4308 
     4309                                        $solrQuery->setQuery("$solrSearchString AND user:$usernameAux AND folder:$name_box" ); 
     4310                                        $solrQuery->setFields(array('id', 'msg_no')); 
     4311                                        $solrQuery->addSort("sent_date", "desc"); 
     4312                                        // override the default row limit of 10 by setting rows to 50 
     4313                                        $solrQuery->setRows(50); 
     4314                                        // this executes the query and returns the result 
     4315                                        $resultset = $solariumClient->select($solrQuery); 
     4316                                 
     4317                                        $count += count($resultset);  
     4318 
     4319                                        //$filter = $this->remove_accents($filter); 
     4320 
     4321                                        //Este bloco tem a finalidade de transformar o login (quando numerico) das pastas compartilhadas em common name 
     4322                                        /*if ($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['uid2cn'] && substr($name_box,0,4) == 'user') 
     4323                                        { 
     4324                                                $folder_name = explode($this->imap_delimiter,$name_box); 
     4325                                                $this->ldap = new ldap_functions(); 
     4326                                                 
     4327                                                if ($cn = $this->ldap->uid2cn($folder_name[1])) 
     4328                                                { 
     4329                                                        $folder_name[1] = $cn; 
     4330                                                } 
     4331                                                $folder_name = implode($this->imap_delimiter,$folder_name); 
     4332                                        } 
     4333                                        else 
     4334                                        { 
     4335                                                $folder_name = mb_convert_encoding(utf8_decode($name_box), "UTF7-IMAP", "ISO-8859-1" ); 
     4336                                        } 
     4337                                 
     4338                                        //print_r($filter); 
     4339                                        //print("<br/>"); 
     4340                                 
     4341                                        $this->open_mbox($name_box); 
     4342 
     4343                                        if (preg_match("/^.?\bALL\b/", $filter)) 
     4344                                        {  
     4345                                                // Quick Search, note: this ALL isn't the same ALL from imap_search 
     4346                                                $all_criterias = array ("TO","SUBJECT","FROM","CC"); 
     4347                                                 
     4348                                                foreach($all_criterias as $criteria_fixed) 
     4349                                                { 
     4350                                                        $_filter = $criteria_fixed . substr($filter,4); 
     4351                                                 
     4352                                                        $search_criteria = imap_search($this->mbox, $_filter, SE_UID); 
     4353                                                 
     4354                                                        if(is_array($search_criteria)) 
     4355                                                        { 
     4356                                                                foreach($search_criteria as $new_search) 
     4357                                                                { 
     4358                                                                        $elem = $this->get_info_head_msg($new_search); 
     4359                                                                        $elem['udate']       = gmdate('d/m/Y', $elem['udate'] + $this->functions->CalculateDateOffset());  
     4360                                                                        $elem['boxname'] = mb_convert_encoding( $name_box, "ISO-8859-1", "UTF7-IMAP" ); 
     4361                                                                        $elem['uid'] = $new_search; 
     4362                                                                        // compare dates in ordering / 
     4363                                                                        $elem['udatecomp'] = substr ($elem['udate'], -4) ."-". substr ($elem['udate'], 3, 2) ."-". substr ($elem['udate'], 0, 2); 
     4364                                                                        $retorno[] = $elem;  
     4365                                                                } 
     4366                                                        } 
     4367                                                } 
     4368                                        }*/ 
     4369                                        //else 
     4370                                        //{ 
     4371                                        $imap = $this->open_mbox( $name_box ); 
     4372                                        foreach ($resultset as $document) 
     4373                                        {        
     4374                                                $v = $document['msg_no']; 
     4375                                         
     4376                                                $new_search = imap_headerinfo ( $imap,  imap_msgno($imap, $v) ); 
     4377                                                if ($new_search->message_id != $document['id']) 
     4378                                                { 
     4379                                                        continue; 
     4380                                                } 
     4381 
     4382                                                //$search_criteria = imap_search($this->mbox, $filter, SE_UID); 
     4383                                                 
     4384                                                /*if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_important_flag']) 
     4385                                                { 
     4386                                                        if((!strpos($filter,"FLAGGED") === false) || (!strpos($filter,"UNFLAGGED") === false)) 
     4387                                                        { 
     4388                                                                $num_msgs = imap_num_msg($this->mbox); 
     4389                                                                $flagged_msgs = array(); 
     4390                                                                for ($i=$num_msgs; $i>0; $i--) 
     4391                                                                { 
     4392                                                                        $iuid = @imap_uid($this->mbox,$i); 
     4393                                                                        $header = $this->get_header($iuid); 
     4394                                                                        if(trim($header->Flagged)) 
     4395                                                                        { 
     4396                                                                                $flagged_msgs[$i] = $iuid; 
     4397                                                                        } 
     4398                                                                } 
     4399                                                                 
     4400                                                                if((count($flagged_msgs) >0) && (strpos($filter,"UNFLAGGED") === false)) 
     4401                                                                { 
     4402                                                                        $arry_diff = is_array($search_criteria) ? array_diff($flagged_msgs,$search_criteria):$flagged_msgs; 
     4403                                                                        foreach($arry_diff as $msg) 
     4404                                                                        { 
     4405                                                                                $search_criteria[] = $msg; 
     4406                                                                        } 
     4407                                                                } 
     4408                                                                else if((count($flagged_msgs) >0) && (is_array($search_criteria)) && (!strpos($filter,"UNFLAGGED") === false)) 
     4409                                                                { 
     4410                                                                        $search_criteria = array_diff($search_criteria,$flagged_msgs); 
     4411                                                                } 
     4412                                                        } 
     4413                                                }*/ 
     4414 
     4415                                                //if( is_array( $search_criteria) ) 
     4416                                                //{ 
     4417                                                //      foreach($search_criteria as $new_search) 
     4418                                                //      { 
     4419                                                                 
     4420 
     4421                                                                $elem = $this->get_info_head_msg( $new_search ); 
     4422 
     4423                                                                $elem['udate']       = gmdate('d/m/Y', $elem['msg_number']->udate + $this->functions->CalculateDateOffset()); 
     4424                                                                $elem['boxname'] = mb_convert_encoding( $name_box, "ISO-8859-1", "UTF7-IMAP" );  
     4425                                                                $elem['uid'] = $new_search->Msgno; 
     4426                                                                $elem['flag'] = $elem['msg_number']->Unseen.$elem['msg_number']->Flagged.$elem['msg_number']->Answered.$elem['msg_number']->Draft; 
     4427                                                                /* compare dates in ordering */ 
     4428                                                                $elem['from'] = $elem['msg_number']->from[0]->personal ? $elem['msg_number']->from[0]->personal : $elem['msg_number']->from[0]->mailbox . "@" . $elem['msg_number']->from[0]->host; 
     4429                                                                $elem['Size'] = $elem['msg_number']->Size; 
     4430                                                                $elem['subject'] = mb_convert_encoding(self::decodeMimeString($elem['msg_number']->subject), 'UTF-8',mb_detect_encoding(self::decodeMimeString($elem['msg_number']->subject).'x', 'UTF-8, ISO-8859-1')); 
     4431                                                                $elem['udatecomp'] = substr ($elem['udate'], -4) ."-". substr ($elem['udate'], 3, 2) ."-". substr ($elem['udate'], 0, 2);   
     4432                                                                $filter = array('AND', array('=', 'folderName', $name_box), array('=','messageNumber', $new_search)); 
     4433                                                                $followupflagged = Controller::find( 
     4434                                                                                        array('concept' => 'followupflagged'), 
     4435                                                                                        false,  
     4436                                                                                        array('filter' => $filter, 'criteria' => array('deepness' => '2')) 
     4437                                                                                ); 
     4438 
     4439                                                                if(isset($followupflagged[0]['followupflagId'])) 
     4440                                                                { 
     4441                                                                        $followupflag = Controller::read( array( 'concept' => 'followupflag', 'id' => $followupflagged[0]['followupflagId'] ));      
     4442                                                                        $followupflagged[0]['followupflag'] = $followupflag; 
     4443                                                                        $elem['followupflagged'] = $followupflagged[0]; 
     4444                                                                }        
     4445 
     4446                                                                $labeleds = Controller::find( 
     4447                                                                                        array('concept' => 'labeled'), 
     4448                                                                                        false,  
     4449                                                                                        array('filter' => $filter, 'criteria' => array('deepness' => '2')) 
     4450                                                                                ); 
     4451                                                                foreach ($labeleds as $e) 
     4452                                                                { 
     4453                                                                        $labels = Controller::read( array( 'concept' => 'label', 'id' =>  $e['labelId'])); 
     4454                                                                        $elem['labels'][$e['labelId']] = $labels; 
     4455                                                                }                                                                                        
     4456                                                                $retorno[] = $elem; 
     4457                                                        //} 
     4458                                                //} 
     4459                                        } 
    42474460                                } 
     4461                        } 
     4462                        imap_close($this->mbox); 
     4463                        $num_msgs = count($retorno); 
     4464                        /* Comparison functions, descendent is ascendent with parms inverted */ 
     4465                        function SORTDATE($a, $b){ return ($a['udatecomp'] < $b['udatecomp']); } 
     4466                        function SORTDATE_REVERSE($b, $a) { return SORTDATE($a,$b); } 
     4467 
     4468                        function SORTWHO($a, $b) { return (strtoupper($a['from']) > strtoupper($b['from'])); } 
     4469                        function SORTWHO_REVERSE($b, $a) { return SORTWHO($a,$b); } 
     4470 
     4471                        function SORTSUBJECT($a, $b) { return (strtoupper($a['subject']) > strtoupper($b['subject'])); } 
     4472                        function SORTSUBJECT_REVERSE($b, $a) { return SORTSUBJECT($a,$b); } 
     4473 
     4474                        function SORTBOX($a, $b) { return ($a['boxname'] > $b['boxname']); } 
     4475                        function SORTBOX_REVERSE($b, $a) { return SORTBOX($a,$b); } 
     4476 
     4477                        function SORTSIZE($a, $b) { return ($a['size'] > $b['size']); } 
     4478                        function SORTSIZE_REVERSE($b, $a) { return SORTSIZE($a,$b); } 
     4479 
     4480                        usort( $retorno, $params['sort_type']); 
     4481                        $pageret = array_slice( $retorno, $params['page'] * $this->prefs['max_email_per_page'], $this->prefs['max_email_per_page']); 
     4482                     
     4483                        $arrayRetorno['num_msgs']       =  $num_msgs; 
     4484                        $arrayRetorno['data']           =  $pageret; 
     4485                        $arrayRetorno['currentTab'] =  $params['current_tab']; 
     4486 
     4487                        print_r(json_encode($arrayRetorno)); 
     4488                        return; 
     4489                } 
     4490                else 
     4491                { 
     4492                        $params['page'] = $params['page'] * 1; 
     4493 
     4494                        if( is_array($search) ) 
     4495                        { 
     4496                                $search = array_unique($search); // Remove duplicated folders 
     4497                                //print_r($search); 
     4498                                $search_criteria = ''; 
     4499                                $search_result_number = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['search_result_number']; 
     4500                                foreach($search as $tmp) 
     4501                                { 
     4502                                        $tmp1 = explode("##",$tmp); 
     4503                                        //print_r($tmp1); 
     4504                                        $sum = 0; 
     4505                                        $name_box = $tmp1[0]; 
     4506                                        //unset($filter); 
     4507                                        $filter = ""; 
     4508                                        foreach($tmp1 as $index => $criteria) 
     4509                                        { 
     4510                                                if ($index != 0 && strlen($criteria) != 0) 
     4511                                                { 
     4512                                                        $filter_array = explode("<=>",html_entity_decode(rawurldecode($criteria))); 
     4513                                                        $filter .= " ".$filter_array[0]; 
     4514                                                        if (strlen($filter_array[1]) != 0) 
     4515                                                        { 
     4516                                                                if ( trim($filter_array[0]) != 'BEFORE' && 
     4517                                                                                 trim($filter_array[0]) != 'SINCE' && 
     4518                                                                                 trim($filter_array[0]) != 'ON') 
     4519                                                                { 
     4520                                                                        $filter .= '"'.$filter_array[1].'"'; 
     4521                                                                } 
     4522                                                                else if(trim($filter_array[0]) == 'BEFORE' ) 
     4523                                                                { 
     4524                                                                        $filter .= '"'.$this->make_search_date($filter_array[1],true).'"'; 
     4525                                                                } 
     4526                                                                else 
     4527                                                                { 
     4528                                                                        $filter .= '"'.$this->make_search_date($filter_array[1]).'"'; 
     4529                                                                } 
     4530                                                        } 
     4531                                                } 
     4532                                        } 
    42484533                                 
    4249                                 $name_box = mb_convert_encoding(utf8_decode($name_box), "UTF7-IMAP", "ISO-8859-1" ); 
    4250                                 $filter = $this->remove_accents($filter); 
    4251  
    4252                                 //Este bloco tem a finalidade de transformar o login (quando numerico) das pastas compartilhadas em common name 
    4253                                 if ($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['uid2cn'] && substr($name_box,0,4) == 'user') 
    4254                                 { 
    4255                                         $folder_name = explode($this->imap_delimiter,$name_box); 
    4256                                         $this->ldap = new ldap_functions(); 
    4257                                          
    4258                                         if ($cn = $this->ldap->uid2cn($folder_name[1])) 
     4534                                        $name_box = mb_convert_encoding(utf8_decode($name_box), "UTF7-IMAP", "ISO-8859-1" ); 
     4535                                        $filter = $this->remove_accents($filter); 
     4536 
     4537                                        //Este bloco tem a finalidade de transformar o login (quando numerico) das pastas compartilhadas em common name 
     4538                                        if ($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['uid2cn'] && substr($name_box,0,4) == 'user') 
    42594539                                        { 
    4260                                                 $folder_name[1] = $cn; 
     4540                                                $folder_name = explode($this->imap_delimiter,$name_box); 
     4541                                                $this->ldap = new ldap_functions(); 
     4542                                                 
     4543                                                if ($cn = $this->ldap->uid2cn($folder_name[1])) 
     4544                                                { 
     4545                                                        $folder_name[1] = $cn; 
     4546                                                } 
     4547                                                $folder_name = implode($this->imap_delimiter,$folder_name); 
    42614548                                        } 
    4262                                         $folder_name = implode($this->imap_delimiter,$folder_name); 
    4263                                 } 
    4264                                 else 
    4265                                         $folder_name = mb_convert_encoding(utf8_decode($name_box), "UTF7-IMAP", "ISO-8859-1" ); 
     4549                                        else 
     4550                                        { 
     4551                                                $folder_name = mb_convert_encoding(utf8_decode($name_box), "UTF7-IMAP", "ISO-8859-1" ); 
     4552                                        } 
    42664553                                 
    4267          
    4268                                 $this->open_mbox($name_box); 
    4269  
    4270                                 if (preg_match("/^.?\bALL\b/", $filter)) 
    4271                                 {  
    4272                                         // Quick Search, note: this ALL isn't the same ALL from imap_search 
    4273                                         $all_criterias = array ("TO","SUBJECT","FROM","CC"); 
    4274                                              
    4275                                         foreach($all_criterias as $criteria_fixed) 
     4554                                        //print_r($filter); 
     4555                                        //print("<br/>"); 
     4556                                 
     4557                                        $this->open_mbox($name_box); 
     4558 
     4559                                        if (preg_match("/^.?\bALL\b/", $filter)) 
     4560                                        {  
     4561                                                // Quick Search, note: this ALL isn't the same ALL from imap_search 
     4562                                                $all_criterias = array ("TO","SUBJECT","FROM","CC"); 
     4563                                                 
     4564                                                foreach($all_criterias as $criteria_fixed) 
     4565                                                { 
     4566                                                        $_filter = $criteria_fixed . substr($filter,4); 
     4567                                                 
     4568                                                        $search_criteria = imap_search($this->mbox, $_filter, SE_UID); 
     4569                                                 
     4570                                                        if(is_array($search_criteria)) 
     4571                                                        { 
     4572                                                                foreach($search_criteria as $new_search) 
     4573                                                                { 
     4574                                                                        $elem = $this->get_info_head_msg($new_search); 
     4575 
     4576                                                                        $elem['udate']       = gmdate('d/m/Y', $elem['udate'] + $this->functions->CalculateDateOffset());  
     4577                                                                        $elem['boxname'] = mb_convert_encoding( $name_box, "ISO-8859-1", "UTF7-IMAP" ); 
     4578                                                                        $elem['uid'] = $new_search; 
     4579                                                                        /* compare dates in ordering */ 
     4580                                                                        $elem['udatecomp'] = substr ($elem['udate'], -4) ."-". substr ($elem['udate'], 3, 2) ."-". substr ($elem['udate'], 0, 2); 
     4581                                                                        $retorno[] = $elem;  
     4582                                                                } 
     4583                                                        } 
     4584                                                } 
     4585                                        } 
     4586                                        else 
    42764587                                        { 
    4277                                                 $_filter = $criteria_fixed . substr($filter,4); 
    4278                                                  
    4279                                                 $search_criteria = imap_search($this->mbox, $_filter, SE_UID); 
    4280                                                  
    4281                                                 if(is_array($search_criteria)) 
     4588                                                $search_criteria = imap_search($this->mbox, $filter, SE_UID); 
     4589                                                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_important_flag']) 
     4590                                                { 
     4591                                                        if((!strpos($filter,"FLAGGED") === false) || (!strpos($filter,"UNFLAGGED") === false)) 
     4592                                                        { 
     4593                                                                $num_msgs = imap_num_msg($this->mbox); 
     4594                                                                $flagged_msgs = array(); 
     4595                                                                for ($i=$num_msgs; $i>0; $i--) 
     4596                                                                { 
     4597                                                                        $iuid = @imap_uid($this->mbox,$i); 
     4598                                                                        $header = $this->get_header($iuid); 
     4599                                                                        if(trim($header->Flagged)) 
     4600                                                                        { 
     4601                                                                                $flagged_msgs[$i] = $iuid; 
     4602                                                                        } 
     4603                                                                } 
     4604                                                                if((count($flagged_msgs) >0) && (strpos($filter,"UNFLAGGED") === false)) 
     4605                                                                { 
     4606                                                                        $arry_diff = is_array($search_criteria) ? array_diff($flagged_msgs,$search_criteria):$flagged_msgs; 
     4607                                                                        foreach($arry_diff as $msg) 
     4608                                                                        { 
     4609                                                                                $search_criteria[] = $msg; 
     4610                                                                        } 
     4611                                                                } 
     4612                                                                else if((count($flagged_msgs) >0) && (is_array($search_criteria)) && (!strpos($filter,"UNFLAGGED") === false)) 
     4613                                                                { 
     4614                                                                        $search_criteria = array_diff($search_criteria,$flagged_msgs); 
     4615                                                                } 
     4616                                                        } 
     4617                                                } 
     4618 
     4619                                                if( is_array( $search_criteria) ) 
    42824620                                                { 
    42834621                                                        foreach($search_criteria as $new_search) 
    42844622                                                        { 
    4285                                                                 $elem = $this->get_info_head_msg($new_search); 
    4286                                                                 $elem['udate']       = gmdate('d/m/Y', $elem['udate'] + $this->functions->CalculateDateOffset());  
     4623                                                                $elem = $this->get_info_head_msg( $new_search ); 
     4624                                                                $elem['udate']       = gmdate('d/m/Y', $elem['udate'] + $this->functions->CalculateDateOffset()); 
    42874625                                                                $elem['boxname'] = mb_convert_encoding( $name_box, "ISO-8859-1", "UTF7-IMAP" );  
    42884626                                                                $elem['uid'] = $new_search; 
    4289                                                                 /* compare dates in ordering */ 
    4290                                                                 $elem['udatecomp'] = substr ($elem['udate'], -4) ."-". substr ($elem['udate'], 3, 2) ."-". substr ($elem['udate'], 0, 2); 
    4291                                                                 $retorno[] = $elem;  
     4627                                                                /* compare dates in ordering */ 
     4628                                                                $elem['udatecomp'] = substr ($elem['udate'], -4) ."-". substr ($elem['udate'], 3, 2) ."-". substr ($elem['udate'], 0, 2);   
     4629                                                                $filter = array('AND', array('=', 'folderName', $name_box), array('=','messageNumber', $new_search)); 
     4630                                                                $followupflagged = Controller::find( 
     4631                                                                                        array('concept' => 'followupflagged'), 
     4632                                                                                        false,  
     4633                                                                                        array('filter' => $filter, 'criteria' => array('deepness' => '2')) 
     4634                                                                                ); 
     4635 
     4636                                                                if(isset($followupflagged[0]['followupflagId'])) 
     4637                                                                { 
     4638                                                                        $followupflag = Controller::read( array( 'concept' => 'followupflag', 'id' => $followupflagged[0]['followupflagId'] ));      
     4639                                                                        $followupflagged[0]['followupflag'] = $followupflag; 
     4640                                                                        $elem['followupflagged'] = $followupflagged[0]; 
     4641                                                                }        
     4642 
     4643                                                                $labeleds = Controller::find( 
     4644                                                                                        array('concept' => 'labeled'), 
     4645                                                                                        false,  
     4646                                                                                        array('filter' => $filter, 'criteria' => array('deepness' => '2')) 
     4647                                                                                ); 
     4648                                                                foreach ($labeleds as $e) 
     4649                                                                { 
     4650                                                                        $labels = Controller::read( array( 'concept' => 'label', 'id' =>  $e['labelId'])); 
     4651                                                                        $elem['labels'][$e['labelId']] = $labels; 
     4652                                                                }                                                                                        
     4653                                                                $retorno[] = $elem; 
    42924654                                                        } 
    42934655                                                } 
    42944656                                        } 
    42954657                                } 
    4296                                 else{ 
    4297                                         $search_criteria = imap_search($this->mbox, $filter, SE_UID); 
    4298                                     if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_important_flag']) 
    4299                                     { 
    4300                                         if((!strpos($filter,"FLAGGED") === false) || (!strpos($filter,"UNFLAGGED") === false)) 
    4301                                         { 
    4302                                             $num_msgs = imap_num_msg($this->mbox); 
    4303                                             $flagged_msgs = array(); 
    4304                                             for ($i=$num_msgs; $i>0; $i--) 
    4305                                             { 
    4306                                                 $iuid = @imap_uid($this->mbox,$i); 
    4307                                                 $header = $this->get_header($iuid); 
    4308                                                 if(trim($header->Flagged)) 
    4309                                                 { 
    4310                                                         $flagged_msgs[$i] = $iuid; 
    4311                                                 } 
    4312                                             } 
    4313                                                 if((count($flagged_msgs) >0) && (strpos($filter,"UNFLAGGED") === false)) 
    4314                                             { 
    4315                                                     $arry_diff = is_array($search_criteria) ? array_diff($flagged_msgs,$search_criteria):$flagged_msgs; 
    4316                                                     foreach($arry_diff as $msg) 
    4317                                             { 
    4318                                                         $search_criteria[] = $msg; 
    4319                                             } 
    4320                                         } 
    4321                                                 else if((count($flagged_msgs) >0) && (is_array($search_criteria)) && (!strpos($filter,"UNFLAGGED") === false)) 
    4322                                         { 
    4323                                                     $search_criteria = array_diff($search_criteria,$flagged_msgs); 
    4324                                         } 
    4325                                     } 
    4326                                     } 
    4327  
    4328                                     if( is_array( $search_criteria) ) 
    4329                                     { 
    4330                                         foreach($search_criteria as $new_search) 
    4331                                         { 
    4332                                                                                  
    4333                                             $elem = $this->get_info_head_msg( $new_search ); 
    4334                                             $elem['udate']       = gmdate('d/m/Y', $elem['udate'] + $this->functions->CalculateDateOffset());  
    4335                                                                                         $elem['boxname'] = mb_convert_encoding( $name_box, "ISO-8859-1", "UTF7-IMAP" );  
    4336                                             $elem['uid'] = $new_search; 
    4337                                             /* compare dates in ordering */ 
    4338                                             $elem['udatecomp'] = substr ($elem['udate'], -4) ."-". substr ($elem['udate'], 3, 2) ."-". substr ($elem['udate'], 0, 2);   
    4339  
    4340                                                                                         $filter = array('AND', array('=', 'folderName', $name_box), array('=','messageNumber', $new_search)); 
    4341                                                                                         $followupflagged = Controller::find( 
    4342                                                                                                 array('concept' => 'followupflagged'), 
    4343                                                                                                 false,  
    4344                                                                                                 array('filter' => $filter, 'criteria' => array('deepness' => '2')) 
    4345                                                                                         ); 
    4346  
    4347                                                                                         if(isset($followupflagged[0]['followupflagId'])) 
    4348                                                                                         { 
    4349                                                                                                 $followupflag = Controller::read( array( 'concept' => 'followupflag', 'id' => $followupflagged[0]['followupflagId'] ));      
    4350                                                                                                 $followupflagged[0]['followupflag'] = $followupflag; 
    4351                                                                                                 $elem['followupflagged'] = $followupflagged[0]; 
    4352  
    4353                                                                                         }        
    4354                                                                                         $labeleds = Controller::find( 
    4355                                                                                                 array('concept' => 'labeled'), 
    4356                                                                                                 false,  
    4357                                                                                                 array('filter' => $filter, 'criteria' => array('deepness' => '2')) 
    4358                                                                                         ); 
    4359                                                                                         foreach ($labeleds as $e){ 
    4360                                                                                                 $labels = Controller::read( array( 'concept' => 'label', 'id' =>  $e['labelId']));      
    4361                                                                                                 $elem['labels'][$e['labelId']] = $labels; 
    4362 }                                                                                        
    4363                                             $retorno[] = $elem; 
    4364                                         } 
    4365                                     } 
    4366                                 } 
    4367                         } 
    4368                 } 
    4369                  
    4370             imap_close($this->mbox); 
    4371             $num_msgs = count($retorno); 
    4372             /* Comparison functions, descendent is ascendent with parms inverted */ 
    4373             function SORTDATE($a, $b){ return ($a['udatecomp'] < $b['udatecomp']); } 
    4374             function SORTDATE_REVERSE($b, $a) { return SORTDATE($a,$b); } 
    4375  
    4376             function SORTWHO($a, $b) { return (strtoupper($a['from']) > strtoupper($b['from'])); } 
    4377             function SORTWHO_REVERSE($b, $a) { return SORTWHO($a,$b); } 
    4378  
    4379             function SORTSUBJECT($a, $b) { return (strtoupper($a['subject']) > strtoupper($b['subject'])); } 
    4380             function SORTSUBJECT_REVERSE($b, $a) { return SORTSUBJECT($a,$b); } 
    4381  
    4382             function SORTBOX($a, $b) { return ($a['boxname'] > $b['boxname']); } 
    4383             function SORTBOX_REVERSE($b, $a) { return SORTBOX($a,$b); } 
    4384  
    4385             function SORTSIZE($a, $b) { return ($a['size'] > $b['size']); } 
    4386             function SORTSIZE_REVERSE($b, $a) { return SORTSIZE($a,$b); } 
    4387  
    4388             usort( $retorno, $params['sort_type']); 
    4389             $pageret = array_slice( $retorno, $params['page'] * $this->prefs['max_email_per_page'], $this->prefs['max_email_per_page']); 
     4658                        } 
     4659                } 
     4660 
     4661                imap_close($this->mbox); 
     4662                $num_msgs = count($retorno); 
     4663                /* Comparison functions, descendent is ascendent with parms inverted */ 
     4664                function SORTDATE($a, $b){ return ($a['udatecomp'] < $b['udatecomp']); } 
     4665                function SORTDATE_REVERSE($b, $a) { return SORTDATE($a,$b); } 
     4666 
     4667                function SORTWHO($a, $b) { return (strtoupper($a['from']) > strtoupper($b['from'])); } 
     4668                function SORTWHO_REVERSE($b, $a) { return SORTWHO($a,$b); } 
     4669 
     4670                function SORTSUBJECT($a, $b) { return (strtoupper($a['subject']) > strtoupper($b['subject'])); } 
     4671                function SORTSUBJECT_REVERSE($b, $a) { return SORTSUBJECT($a,$b); } 
     4672 
     4673                function SORTBOX($a, $b) { return ($a['boxname'] > $b['boxname']); } 
     4674                function SORTBOX_REVERSE($b, $a) { return SORTBOX($a,$b); } 
     4675 
     4676                function SORTSIZE($a, $b) { return ($a['size'] > $b['size']); } 
     4677                function SORTSIZE_REVERSE($b, $a) { return SORTSIZE($a,$b); } 
     4678 
     4679                usort( $retorno, $params['sort_type']); 
     4680                $pageret = array_slice( $retorno, $params['page'] * $this->prefs['max_email_per_page'], $this->prefs['max_email_per_page']); 
    43904681             
    4391             $arrayRetorno['num_msgs']   =  $num_msgs; 
    4392             $arrayRetorno['data']               =  $pageret; 
    4393             $arrayRetorno['currentTab'] =  $params['current_tab']; 
    4394             return ($pageret) ? $arrayRetorno : 'none'; 
     4682                $arrayRetorno['num_msgs']       =  $num_msgs; 
     4683                $arrayRetorno['data']           =  $pageret; 
     4684                $arrayRetorno['currentTab'] =  $params['current_tab'];   
     4685 
     4686                return ($pageret) ? $arrayRetorno : 'none'; 
    43954687        } 
    43964688 
     
    49215213                call_user_func_array('array_multisort', $params); 
    49225214                $ret = array(); 
    4923                 $keys = array(); 
     5215                $keys = array();$solrConfig = array( 
     5216                                                'adapteroptions' => array( 
     5217                                                'host' => '127.0.0.1', 
     5218                                                'port' => 8983, 
     5219                                                'path' => '/solr/', 
     5220                                        ) 
     5221                                ); 
     5222                                //echo("damn"); 
     5223                                // create a client instance 
     5224                                $solariumClient = new Solarium_Client($solrConfig); 
     5225                                $solrQuery = $solariumClient->createSelect(); 
     5226                                $usernameAux = $this->username; 
    49245227                $first = true; 
    49255228                foreach ($colarr as $col => $arr) { 
     
    49515254        function quickSearchMail( $params ) 
    49525255        {                
    4953                 include '../prototype/api/controller.php';                       
     5256                include '../prototype/api/controller.php'; 
     5257                include 'solrclient/library/Solarium/Autoloader.php'; 
     5258                Solarium_Autoloader::register(); 
     5259 
    49545260                set_time_limit(270); //Aumenta o tempo limit da requisição, em algumas buscas o imap demora para retornar o resultado. 
    49555261                $return = array(); 
    49565262                $return['folder'] = $params['folder']; 
     5263                $useSolr = false; 
     5264 
    49575265                if(!is_array($params['folder'])) 
    49585266                        $params['folder'] = array( $params['folder'] ); 
     
    49745282                        $search = $params['search']; 
    49755283                } 
     5284                 
     5285                if(isset($_SESSION['phpgw_info']['expresso']['expressoMail']['expressoMail_enable_solr']) && $_SESSION['phpgw_info']['expresso']['expressoMail']['expressoMail_enable_solr'] === 'true') 
     5286                { 
     5287                        $useSolr = true; 
     5288                } 
     5289                 
     5290                if($useSolr == true) 
     5291                { 
     5292                        foreach ($params['folder'] as $folder)  
     5293                        { 
     5294                                 
     5295                                $imap = $this->open_mbox( $folder ) ; 
    49765296         
     5297                                //$msgIds = imap_sort( $imap , SORTDATE , 1 , SE_UID , $search ,'UTF-8'); 
     5298                                                         
     5299                                /*include("solrclient/init.php");*/ 
     5300                         
     5301                                $solrConfig = array( 
     5302                                                'adapteroptions' => array( 
     5303                                                'host' => '127.0.0.1', 
     5304                                                'port' => 8983, 
     5305                                                'path' => '/solr/', 
     5306                                        ) 
     5307                                ); 
     5308                                // create a client instance 
     5309                                $solariumClient = new Solarium_Client($solrConfig); 
     5310                                $solrQuery = $solariumClient->createSelect(); 
     5311                                $usernameAux = $this->username; 
     5312                                $solrSearch = $params['search']; 
     5313 
     5314                                $solrFolder = $folder; 
     5315                                if(count(split("/", $folder) ) > 1 ) 
     5316                                { 
     5317                                        $folderArrayAux = split("/", $folder); 
     5318                                        $solrFolder = $folderArrayAux[1]; 
     5319                                } 
     5320                                 
     5321                                $solrQuery->setQuery("(content:\"$solrSearch\" OR subject:\"$solrSearch\" OR from:\"$solrSearch:\") AND user:$usernameAux AND folder:$solrFolder" ); 
     5322                                $solrQuery->setFields(array('id', 'msg_no')); 
     5323                                $solrQuery->addSort("sent_date", "desc"); 
     5324                                // override the default row limit of 10 by setting rows to 50 
     5325                                $solrQuery->setRows(50); 
     5326                                // this executes the query and returns the result 
     5327                                $resultset = $solariumClient->select($solrQuery); 
     5328                                 
     5329                                $count += count($resultset);   
     5330                                 
     5331                                foreach ($resultset as $document) 
     5332                                {        
     5333                                        $v = $document['msg_no']; 
     5334                                         
     5335                                        $msg = imap_headerinfo ( $imap,  imap_msgno($imap, $v) ); 
     5336                                        if ($msg->message_id != $document['id']) 
     5337                                        { 
     5338                                                continue; 
     5339                                        } 
     5340         
     5341                                        if(isset($msg->from[0])) 
     5342                                        { 
     5343                                                $from = self::formatMailObject( $msg->from[0] ); 
     5344                                                $return['msgs'][$i]['from']     = mb_convert_encoding($from['name'] ? $from['name'] : $from['email'], 'UTF-8'); 
     5345                                        } 
     5346                                        else 
     5347                                                $return['msgs'][$i]['from']     = '';  
     5348                                         
     5349                                        $return['msgs'][$i]['subject'] = ' '; 
     5350                                 
     5351                                        $subject = imap_mime_header_decode($msg->subject); 
     5352                                        foreach ($subject as $tmp) 
     5353                                                $return['msgs'][$i]['subject'] .= mb_convert_encoding($tmp->text, 'UTF-8', 'UTF-8 , ISO-8859-1'); 
     5354                                 
     5355                                        $filter = array('AND', array('=', 'folderName', $folder), array('=','messageNumber', $v)); 
     5356                                        $followupflagged = Controller::find( 
     5357                                                array('concept' => 'followupflagged' , 'folder' => $folder ), 
     5358                                                false,  
     5359                                                array('filter' => $filter, 'criteria' => array('deepness' => '2')) 
     5360                                        ); 
     5361 
     5362                                        if(isset($followupflagged[0]['followupflagId'])) 
     5363                                        { 
     5364                                                $followupflag = Controller::read( array( 'concept' => 'followupflag', 'id' => $followupflagged[0]['followupflagId'] ));      
     5365                                                $followupflagged[0]['followupflag'] = $followupflag; 
     5366                                                $return['msgs'][$i]['followupflagged'] = $followupflagged[0]; 
     5367 
     5368                                        }        
     5369                                        $labeleds = Controller::find( 
     5370                                                array('concept' => 'labeled'), 
     5371                                                false,  
     5372                                                array('filter' => $filter, 'criteria' => array('deepness' => '2')) 
     5373                                        ); 
     5374                                        if(is_array($labeleds)) 
     5375                                                foreach ($labeleds as $e){ 
     5376                                                        $labels = Controller::read( array( 'concept' => 'label', 'id' =>  $e['labelId']));      
     5377                                                        $return['msgs'][$i]['labels'][$e['labelId']] = $labels; 
     5378                                                } 
     5379                                        $mimeBody = imap_body( $this->mbox, $v  , FT_UID|FT_PEEK  );     
     5380                                        $return['msgs'][$i]['flag'] = ' '; 
     5381                                        $return['msgs'][$i]['flag'] .= $msg->Unseen ? $msg->Unseen : ''; 
     5382                                        $return['msgs'][$i]['flag'] .= $msg->Recent ? $msg->Recent : '';         
     5383                                        $return['msgs'][$i]['flag'] .= $msg->Draft ? $msg->Draft : '';   
     5384                                        $return['msgs'][$i]['flag'] .= $msg->Answered ? $msg->Answered : '';     
     5385                                        $return['msgs'][$i]['flag'] .= $msg->Deleted ? $msg->Deleted : '';       
     5386                                        $return['msgs'][$i]['flag'] .= ( preg_match('/((Content-Disposition:(.)*([\r\n\s]*filename))|(Content-Type:(.)*([\r\n\s]*name)))/i', $mimeBody) ) ? 'T': ''; 
     5387 
     5388                                        $header = imap_fetchheader( $imap, $v , FT_UID ); // Necessario para recuperar se a mensagem é importante ou não. 
     5389                                        $importante = array(); 
     5390                                 
     5391                                        if($msg->Flagged != 'F') 
     5392                                                $return['msgs'][$i]['flag'] .= ( preg_match('/importance *: *(.*)\r/i', $header , $importante) === 0 ) ? '' : 'F'; 
     5393                                        else 
     5394                                                $return['msgs'][$i]['flag'] .= $msg->Flagged ? $msg->Flagged : '';       
     5395                                         
     5396                                        $return['msgs'][$i]['udate'] = gmdate("d/m/Y",$msg->udate + $this->functions->CalculateDateOffset());  
     5397                                        $return['msgs'][$i]['udatecomp'] = substr ($return['msgs'][$i]['udate'], -4) ."-". substr ($return['msgs'][$i]['udate'], 3, 2) ."-". substr ($return['msgs'][$i]['udate'], 0, 2); 
     5398                                        $return['msgs'][$i]['date'] =   $msg->udate; 
     5399                                        $return['msgs'][$i]['size'] =  $msg->Size; 
     5400                                        $return['msgs'][$i]['boxname'] = $folder; 
     5401                                        $return['msgs'][$i]['uid'] = $v; 
     5402                                        $i++; 
     5403                                }        
     5404                        } 
     5405                } 
     5406                else 
    49775407                foreach ($params['folder'] as $folder)  
    49785408                { 
     
    50725502                        case 3 : $sA = 'subject'; break; 
    50735503                        case 6 : $sA = 'size'; break; 
    5074         }  
     5504                }  
    50755505         
    50765506                         
Note: See TracChangeset for help on using the changeset viewer.