| Conditions | 36 |
| Paths | 17320 |
| Total Lines | 208 |
| Code Lines | 118 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 2 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 275 | public function search($store, $entryid, $action, $actionType) { |
||
| 276 | $useSearchFolder = isset($action["use_searchfolder"]) ? $action["use_searchfolder"] : false; |
||
| 277 | if (!$useSearchFolder) { |
||
| 278 | /* |
||
| 279 | * store doesn't support search folders so we can't use this |
||
| 280 | * method instead we will pass restriction to messageList and |
||
| 281 | * it will give us the restricted results |
||
| 282 | */ |
||
| 283 | return parent::messageList($store, $entryid, $action, "list"); |
||
| 284 | } |
||
| 285 | $store_props = mapi_getprops($store, [PR_MDB_PROVIDER, PR_DEFAULT_STORE, PR_IPM_SUBTREE_ENTRYID]); |
||
| 286 | if ($store_props[PR_MDB_PROVIDER] == ZARAFA_STORE_PUBLIC_GUID) { |
||
| 287 | // public store does not support search folders |
||
| 288 | return parent::messageList($store, $entryid, $action, "search"); |
||
| 289 | } |
||
| 290 | if ($GLOBALS['entryid']->compareEntryIds(bin2hex($entryid), bin2hex(TodoList::getEntryId()))) { |
||
| 291 | // todo list do not need to perform full text index search |
||
| 292 | return parent::messageList($store, $entryid, $action, "list"); |
||
| 293 | } |
||
| 294 | |||
| 295 | $this->searchFolderList = true; // Set to indicate this is not the normal folder, but a search folder |
||
| 296 | $this->restriction = false; |
||
| 297 | |||
| 298 | // Parse Restriction |
||
| 299 | $this->parseRestriction($action); |
||
| 300 | if ($this->restriction == false) { |
||
| 301 | // if error in creating restriction then send error to client |
||
| 302 | $errorInfo = []; |
||
| 303 | $errorInfo["error_message"] = _("Error in search, please try again") . "."; |
||
| 304 | $errorInfo["original_error_message"] = "Error in parsing restrictions."; |
||
| 305 | |||
| 306 | return $this->sendSearchErrorToClient($store, $entryid, $action, $errorInfo); |
||
| 307 | } |
||
| 308 | |||
| 309 | $isSetSearchFolderEntryId = isset($action['search_folder_entryid']); |
||
| 310 | if ($isSetSearchFolderEntryId) { |
||
| 311 | $this->sessionData['searchFolderEntryId'] = $action['search_folder_entryid']; |
||
| 312 | } |
||
| 313 | |||
| 314 | if (isset($action['forceCreateSearchFolder']) && $action['forceCreateSearchFolder']) { |
||
| 315 | $isSetSearchFolderEntryId = false; |
||
| 316 | } |
||
| 317 | |||
| 318 | // create or open search folder |
||
| 319 | $searchFolder = $this->createSearchFolder($store, $isSetSearchFolderEntryId); |
||
| 320 | if ($searchFolder === false) { |
||
| 321 | if ($store_props[PR_MDB_PROVIDER] == ZARAFA_STORE_DELEGATE_GUID) { |
||
| 322 | $this->messageList($store, $entryid, $action, "search"); |
||
| 323 | return true; |
||
| 324 | } |
||
| 325 | // if error in creating search folder then send error to client |
||
| 326 | $errorInfo = []; |
||
| 327 | |||
| 328 | switch (mapi_last_hresult()) { |
||
| 329 | case MAPI_E_NO_ACCESS: |
||
| 330 | $errorInfo["error_message"] = _("Unable to perform search query, no permissions to create search folder."); |
||
| 331 | break; |
||
| 332 | |||
| 333 | case MAPI_E_NOT_FOUND: |
||
| 334 | $errorInfo["error_message"] = _("Unable to perform search query, search folder not found."); |
||
| 335 | break; |
||
| 336 | |||
| 337 | default: |
||
| 338 | $errorInfo["error_message"] = _("Unable to perform search query, store might not support searching."); |
||
| 339 | } |
||
| 340 | |||
| 341 | $errorInfo["original_error_message"] = _("Error in creating search folder."); |
||
| 342 | |||
| 343 | return $this->sendSearchErrorToClient($store, $entryid, $action, $errorInfo); |
||
| 344 | } |
||
| 345 | |||
| 346 | $subfolder_flag = 0; |
||
| 347 | $recursive = false; |
||
| 348 | if (isset($action["subfolders"]) && $action["subfolders"] == "true") { |
||
| 349 | $recursive = true; |
||
| 350 | $subfolder_flag = RECURSIVE_SEARCH; |
||
| 351 | } |
||
| 352 | |||
| 353 | if (!is_array($entryid)) { |
||
| 354 | $entryids = [$entryid]; |
||
| 355 | } |
||
| 356 | else { |
||
| 357 | $entryids = $entryid; |
||
| 358 | } |
||
| 359 | |||
| 360 | $searchFolderEntryId = $this->sessionData['searchFolderEntryId']; |
||
| 361 | |||
| 362 | // check if searchcriteria has changed |
||
| 363 | $restrictionCheck = md5(serialize($this->restriction) . $searchFolderEntryId . $subfolder_flag); |
||
| 364 | |||
| 365 | // check if there is need to set searchcriteria again |
||
| 366 | if (!isset($this->sessionData['searchCriteriaCheck']) || $restrictionCheck != $this->sessionData['searchCriteriaCheck']) { |
||
| 367 | if (!empty($this->sessionData['searchOriginalEntryids']) && |
||
| 368 | isset($action['entryid']) && |
||
| 369 | in_array($action['entryid'], $this->sessionData['searchOriginalEntryids']) |
||
| 370 | ) { |
||
| 371 | // get entryids of original folders, and use it to set new search criteria |
||
| 372 | $entryids = []; |
||
| 373 | $entryIdsCount = count($this->sessionData['searchOriginalEntryids']); |
||
| 374 | for ($index = 0; $index < $entryIdsCount; ++$index) { |
||
| 375 | $entryids[] = hex2bin($this->sessionData['searchOriginalEntryids'][$index]); |
||
| 376 | } |
||
| 377 | } |
||
| 378 | else { |
||
| 379 | // store entryids of original folders, so that can be used for re-setting the search criteria if needed |
||
| 380 | $this->sessionData['searchOriginalEntryids'] = []; |
||
| 381 | for ($index = 0, $len = count($entryids); $index < $len; ++$index) { |
||
| 382 | $this->sessionData['searchOriginalEntryids'][] = bin2hex($entryids[$index]); |
||
| 383 | } |
||
| 384 | } |
||
| 385 | // we never start the search folder because we will populate the search folder by ourselves |
||
| 386 | mapi_folder_setsearchcriteria($searchFolder, $this->restriction, $entryids, $subfolder_flag | STOP_SEARCH); |
||
| 387 | $this->sessionData['searchCriteriaCheck'] = $restrictionCheck; |
||
| 388 | } |
||
| 389 | |||
| 390 | if (isset($this->sessionData['searchCriteriaCheck']) || $restrictionCheck == $this->sessionData['searchCriteriaCheck']) { |
||
| 391 | $folderEntryid = bin2hex($entryid); |
||
| 392 | if ($this->sessionData['searchOriginalEntryids'][0] !== $folderEntryid) { |
||
| 393 | $this->sessionData['searchOriginalEntryids'][0] = $folderEntryid; |
||
| 394 | // we never start the search folder because we will populate the search folder by ourselves |
||
| 395 | mapi_folder_setsearchcriteria($searchFolder, $this->restriction, [$entryid], $subfolder_flag | STOP_SEARCH); |
||
| 396 | } |
||
| 397 | } |
||
| 398 | |||
| 399 | // Sort |
||
| 400 | $this->parseSortOrder($action); |
||
| 401 | // Initialize search patterns with default values |
||
| 402 | $search_patterns = array_fill_keys(['sender', 'sending', 'recipients', |
||
| 403 | 'subject', 'content', 'attachments', 'others', 'message_classes', |
||
| 404 | 'date_start', 'date_end', 'unread', 'has_attachments', 'categories'], |
||
| 405 | null); |
||
| 406 | $this->parsePatterns($this->restriction, $search_patterns); |
||
| 407 | if (isset($search_patterns['message_classes']) && |
||
| 408 | count($search_patterns['message_classes']) >= 7) { |
||
| 409 | $search_patterns['message_classes'] = null; |
||
| 410 | } |
||
| 411 | |||
| 412 | if ($store_props[PR_MDB_PROVIDER] == ZARAFA_STORE_DELEGATE_GUID) { |
||
| 413 | $eidObj = $GLOBALS["entryid"]->createMsgStoreEntryIdObj(hex2bin($action['store_entryid'])); |
||
| 414 | $username = $eidObj['ServerShortname']; |
||
| 415 | $session = $GLOBALS["mapisession"]->getSession(); |
||
| 416 | |||
| 417 | if ($username) { |
||
| 418 | $indexDB = new IndexSqlite($username, $session, $store); |
||
| 419 | } |
||
| 420 | } |
||
| 421 | else { |
||
| 422 | $indexDB = new IndexSqlite(); |
||
| 423 | } |
||
| 424 | |||
| 425 | $search_result = $indexDB->search(hex2bin($searchFolderEntryId), $search_patterns, $entryid, $recursive); |
||
| 426 | // Use the query search if search in index fails or is not available. |
||
| 427 | if ($search_result == false) { |
||
| 428 | // Search in the inbox instead of Top of Information Store |
||
| 429 | if (isset($store_props[PR_IPM_SUBTREE_ENTRYID]) && |
||
| 430 | $GLOBALS['entryid']->compareEntryIds(bin2hex($entryid), bin2hex($store_props[PR_IPM_SUBTREE_ENTRYID]))) { |
||
| 431 | $inbox = mapi_msgstore_getreceivefolder($store); |
||
| 432 | $inboxProps = mapi_getprops($inbox, [PR_ENTRYID]); |
||
| 433 | $entryid = $inboxProps[PR_ENTRYID]; |
||
| 434 | } |
||
| 435 | |||
| 436 | return parent::messageList($store, $entryid, $action, "search"); |
||
| 437 | } |
||
| 438 | |||
| 439 | unset($action["restriction"]); |
||
| 440 | |||
| 441 | // Get the table and merge the arrays |
||
| 442 | $table = $GLOBALS["operations"]->getTable($store, hex2bin($searchFolderEntryId), $this->properties, $this->sort, $this->start); |
||
| 443 | // Create the data array, which will be send back to the client |
||
| 444 | $data = []; |
||
| 445 | $data = array_merge($data, $table); |
||
| 446 | |||
| 447 | $this->getDelegateFolderInfo($store); |
||
| 448 | $data = $this->filterPrivateItems($data); |
||
| 449 | |||
| 450 | // remember which entryid's are send to the client |
||
| 451 | $searchResults = []; |
||
| 452 | foreach ($table["item"] as $item) { |
||
| 453 | // store entryid => last_modification_time mapping |
||
| 454 | $searchResults[$item["entryid"]] = $item["props"]["last_modification_time"]; |
||
| 455 | } |
||
| 456 | |||
| 457 | // store search results into session data |
||
| 458 | if (!isset($this->sessionData['searchResults'])) { |
||
| 459 | $this->sessionData['searchResults'] = []; |
||
| 460 | } |
||
| 461 | $this->sessionData['searchResults'][$searchFolderEntryId] = $searchResults; |
||
| 462 | |||
| 463 | $result = mapi_folder_getsearchcriteria($searchFolder); |
||
| 464 | |||
| 465 | $data["search_meta"] = []; |
||
| 466 | $data["search_meta"]["searchfolder_entryid"] = $searchFolderEntryId; |
||
| 467 | $data["search_meta"]["search_store_entryid"] = $action["store_entryid"]; |
||
| 468 | $data["search_meta"]["searchstate"] = $result["searchstate"]; |
||
| 469 | $data["search_meta"]["results"] = count($searchResults); |
||
| 470 | |||
| 471 | // Reopen the search folder, because otherwise the suggestion property will |
||
| 472 | // not have been updated |
||
| 473 | $searchFolder = $this->createSearchFolder($store, true); |
||
| 474 | $storeProps = mapi_getprops($searchFolder, [PR_EC_SUGGESTION]); |
||
| 475 | if (isset($storeProps[PR_EC_SUGGESTION])) { |
||
| 476 | $data["search_meta"]["suggestion"] = $storeProps[PR_EC_SUGGESTION]; |
||
| 477 | } |
||
| 478 | |||
| 479 | $this->addActionData("search", $data); |
||
| 480 | $GLOBALS["bus"]->addData($this->getResponseData()); |
||
| 481 | |||
| 482 | return true; |
||
| 483 | } |
||
| 485 |