/** * Searches for the emails on the server * * @param ContentParameter $cpo * * @return array */ public function GetMailboxSearchResults($cpo) { $searchFolder = $this->getSearchFolder(); $searchRestriction = $this->getSearchRestriction($cpo); $searchRange = explode('-', $cpo->GetSearchRange()); $searchFolderId = $cpo->GetSearchFolderid(); $searchFolders = array(); // search only in required folders if (!empty($searchFolderId)) { $searchFolderEntryId = mapi_msgstore_entryidfromsourcekey($this->store, hex2bin($searchFolderId)); $searchFolders[] = $searchFolderEntryId; } else { $tmp = mapi_getprops($this->store, array(PR_ENTRYID, PR_DISPLAY_NAME, PR_IPM_SUBTREE_ENTRYID)); $searchFolders[] = $tmp[PR_IPM_SUBTREE_ENTRYID]; } $items = array(); $flags = 0; // if subfolders are required, do a recursive search if ($cpo->GetSearchDeepTraversal()) { $flags |= SEARCH_RECURSIVE; } mapi_folder_setsearchcriteria($searchFolder, $searchRestriction, $searchFolders, $flags); $table = mapi_folder_getcontentstable($searchFolder); $searchStart = time(); // do the search and wait for all the results available while (time() - $searchStart < SEARCH_WAIT) { $searchcriteria = mapi_folder_getsearchcriteria($searchFolder); if (($searchcriteria["searchstate"] & SEARCH_REBUILD) == 0) { break; } // Search is done sleep(1); } // if the search range is set limit the result to it, otherwise return all found messages $rows = is_array($searchRange) && isset($searchRange[0]) && isset($searchRange[1]) ? mapi_table_queryrows($table, array(PR_SOURCE_KEY, PR_PARENT_SOURCE_KEY), $searchRange[0], $searchRange[1] - $searchRange[0] + 1) : mapi_table_queryrows($table, array(PR_SOURCE_KEY, PR_PARENT_SOURCE_KEY), 0, SEARCH_MAXRESULTS); $cnt = count($rows); $items['searchtotal'] = $cnt; $items["range"] = $cpo->GetSearchRange(); for ($i = 0; $i < $cnt; $i++) { $items[$i]['class'] = 'Email'; $items[$i]['longid'] = bin2hex($rows[$i][PR_PARENT_SOURCE_KEY]) . ":" . bin2hex($rows[$i][PR_SOURCE_KEY]); $items[$i]['folderid'] = bin2hex($rows[$i][PR_PARENT_SOURCE_KEY]); } return $items; }
function getSearchResultsMailbox($searchquery) { if (!is_array($searchquery)) { return array(); } foreach ($searchquery['query'] as $value) { $query = $this->_getSearchResultsMailboxTranslation($value); } // get search folder $storeProps = mapi_getprops($this->_defaultstore, array(PR_STORE_SUPPORT_MASK, PR_FINDER_ENTRYID)); if (($storeProps[PR_STORE_SUPPORT_MASK] & STORE_SEARCH_OK) != STORE_SEARCH_OK) { return array('global_search_status' => '3'); } else { // open search folders root $searchFolderEntryID = mapi_msgstore_openentry($this->_defaultstore, $storeProps[PR_FINDER_ENTRYID]); if (($hresult = mapi_last_hresult()) != 0 || $searchFolderEntryID == false) { debugLog("getSearchResultsMailbox: searchFolderEntryID is " . ($searchFolderEntryID === 'true' ? 'true' : 'false') . " mapi_last_hresult = " . sprintf("0x%08x", $hresult)); return array('global_search_status' => '3'); } } $result = mapi_table_queryallrows(mapi_folder_gethierarchytable($searchFolderEntryID), array(PR_DISPLAY_NAME, PR_ENTRYID, PR_SOURCE_KEY)); $folderName = "Z-Push Search " . $this->_devid; $found = false; foreach ($result as $array) { if (array_keys($array, $folderName)) { $found = $array[PR_ENTRYID]; $searchfoldersourcekey = $array[PR_SOURCE_KEY]; break; } } if ($found == false) { // create search folder $folder = mapi_folder_createfolder($searchFolderEntryID, $folderName, null, 0, FOLDER_SEARCH); if ($folder === false || ($hresult = mapi_last_hresult()) != 0) { debugLog("getSearchResultsMailbox: searchFolderEntryID is " . ($searchFolderEntryID === 'true' ? 'true' : 'false') . " folderName is " . $folderName . " mapi_last_hresult = " . sprintf("0x%08x", $hresult)); return array('global_search_status' => '3'); } } else { $folder = mapi_openentry($this->_session, $found); if ($folder === false || ($hresult = mapi_last_hresult()) != 0) { debugLog("getSearchResultsMailbox: _session is " . $this->_session . " found " . ($found == 'true' ? 'true' : 'false') . " mapi_last_hresult = " . sprintf("0x%08x", $hresult)); return array('global_search_status' => '3'); } } $props = mapi_getprops($folder, array(PR_SOURCE_KEY)); $searchfoldersourcekey = $props[PR_SOURCE_KEY]; if (!isset($query[PR_SOURCE_KEY]) || !($startsearchinfolderid = mapi_msgstore_entryidfromsourcekey($this->_defaultstore, $query[PR_SOURCE_KEY]))) { $tmp = mapi_getprops($this->_defaultstore, array(PR_ENTRYID, PR_DISPLAY_NAME, PR_IPM_SUBTREE_ENTRYID)); $startsearchinfolderid = $tmp[PR_IPM_SUBTREE_ENTRYID]; } $search = mapi_folder_getsearchcriteria($folder); $range = explode("-", $searchquery['range']); // if ($range[0] == 0 && // (!isset($search['restriction']) || // md5(serialize(ksort_recursive($search['restriction']))) != md5(serialize(ksort_recursive($query['query']))))) { if ($range[0] == 0 || isset($searchquery['rebuildresults']) && $searchquery['rebuildresults'] == true) { // debugLog("\n".serialize(ksort_recursive($search['restriction']))."\n".serialize(ksort_recursive($query['query']))); // debugLog("Set Search Criteria ".md5(serialize(ksort_recursive($search['restriction']))) . " ". md5(serialize(ksort_recursive($query['query'])))); if (($search['searchstate'] & SEARCH_REBUILD) == true && ($search['searchstate'] & SEARCH_RUNNING) == true) { mapi_folder_setsearchcriteria($folder, $query["query"], array($startsearchinfolderid), STOP_SEARCH); } mapi_folder_setsearchcriteria($folder, $query["query"], array($startsearchinfolderid), (isset($searchquery['deeptraversal']) && $searchquery['deeptraversal'] == true ? RECURSIVE_SEARCH : SHALLOW_SEARCH) | (isset($searchquery['rebuildresults']) && $searchquery['rebuildresults'] == true ? RESTART_SEARCH : 0)); } $i = 0; $table = mapi_folder_getcontentstable($folder); do { $search = mapi_folder_getsearchcriteria($folder); usleep(100000); $i++; } while (($search['searchstate'] & SEARCH_REBUILD) == true && ($search['searchstate'] & SEARCH_RUNNING) == true && $i < 4000 && mapi_table_getrowcount($table) <= $range[1] * 2 + 1); if ($rows = mapi_table_queryallrows($table, array(PR_ENTRYID, PR_SOURCE_KEY, PR_PARENT_SOURCE_KEY))) { foreach ($rows as $value) { $res["searchfolderid"] = bin2hex($searchfoldersourcekey); $res["uniqueid"] = bin2hex($value[PR_ENTRYID]); $res["item"] = bin2hex($value[PR_SOURCE_KEY]); $res["parent"] = bin2hex($value[PR_PARENT_SOURCE_KEY]); $result['rows'][] = $res; } $result['status'] = 1; } else { $result = array(); $result['status'] = 1; } if (($search['searchstate'] & SEARCH_REBUILD) == true && ($search['searchstate'] & SEARCH_RUNNING) == true && $i == 4000) { $result['status'] = 10; } $result['global_search_status'] = 1; return $result; }