| Conditions | 32 |
| Paths | 80 |
| Total Lines | 120 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | 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 |
||
| 95 | public function searchAddressBook($ab, $ab_dir, $query, $excludeGABGroups) { |
||
| 96 | // Prefer resolving the email_address. This allows the user |
||
| 97 | // to resolve recipients with a display name that matches a EX |
||
| 98 | // user with an alternative (external) email address. |
||
| 99 | $searchstr = empty($query['email_address']) ? $query['display_name'] : $query['email_address']; |
||
| 100 | // If the address_type is 'EX' then we are resolving something which must be found in |
||
| 101 | // the GAB as an exact match. So add the flag EMS_AB_ADDRESS_LOOKUP to ensure we will not |
||
| 102 | // get multiple results when multiple items have a partial match. |
||
| 103 | $flags = $query['address_type'] === 'EX' ? EMS_AB_ADDRESS_LOOKUP : 0; |
||
| 104 | |||
| 105 | try { |
||
| 106 | // First, try an addressbook lookup |
||
| 107 | $rows = mapi_ab_resolvename($ab, [[PR_DISPLAY_NAME => $searchstr]], $flags); |
||
| 108 | $this->searchContactsFolders($ab, $ab_dir, $searchstr, $rows); |
||
| 109 | } |
||
| 110 | catch (MAPIException $e) { |
||
| 111 | if ($e->getCode() == MAPI_E_AMBIGUOUS_RECIP) { |
||
| 112 | $ab_entryid = mapi_ab_getdefaultdir($ab); |
||
| 113 | $ab_dir = mapi_ab_openentry($ab, $ab_entryid); |
||
| 114 | // Ambiguous, show possibilities: |
||
| 115 | $table = mapi_folder_getcontentstable($ab_dir, MAPI_DEFERRED_ERRORS); |
||
| 116 | $restriction = $this->getAmbigiousContactRestriction($searchstr, $excludeGABGroups, PR_ACCOUNT); |
||
| 117 | |||
| 118 | mapi_table_restrict($table, $restriction, TBL_BATCH); |
||
| 119 | mapi_table_sort($table, [PR_DISPLAY_NAME => TABLE_SORT_ASCEND], TBL_BATCH); |
||
| 120 | |||
| 121 | $rows = mapi_table_queryallrows($table, [PR_ACCOUNT, PR_ADDRTYPE, PR_DISPLAY_NAME, PR_ENTRYID, PR_SEARCH_KEY, PR_OBJECT_TYPE, PR_SMTP_ADDRESS, PR_DISPLAY_TYPE_EX, PR_EMAIL_ADDRESS, PR_OBJECT_TYPE, PR_DISPLAY_TYPE]); |
||
| 122 | |||
| 123 | $rows = array_merge($rows, $this->getAmbigiousContactResolveResults($ab, $searchstr, $excludeGABGroups)); |
||
| 124 | } |
||
| 125 | elseif ($e->getCode() == MAPI_E_NOT_FOUND) { |
||
| 126 | $rows = []; |
||
| 127 | if ($query['address_type'] === 'SMTP') { |
||
| 128 | // If we still can't find anything, and we were searching for a SMTP user |
||
| 129 | // we can generate a oneoff entry which contains the information of the user. |
||
| 130 | if (!empty($query['email_address'])) { |
||
| 131 | $rows[] = [ |
||
| 132 | PR_ACCOUNT => $query['email_address'], PR_ADDRTYPE => 'SMTP', PR_EMAIL_ADDRESS => $query['email_address'], |
||
| 133 | PR_DISPLAY_NAME => $query['display_name'], PR_DISPLAY_TYPE_EX => DT_REMOTE_MAILUSER, PR_DISPLAY_TYPE => DT_MAILUSER, |
||
| 134 | PR_SMTP_ADDRESS => $query['email_address'], PR_OBJECT_TYPE => MAPI_MAILUSER, |
||
| 135 | PR_ENTRYID => mapi_createoneoff($query['display_name'], 'SMTP', $query['email_address']), |
||
| 136 | ]; |
||
| 137 | } |
||
| 138 | // Check also the user's contacts folders |
||
| 139 | else { |
||
| 140 | $this->searchContactsFolders($ab, $ab_dir, $searchstr, $rows); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | else { |
||
| 145 | // all other errors should be propagated to higher level exception handlers |
||
| 146 | throw $e; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | $items = []; |
||
| 151 | if ($rows) { |
||
| 152 | foreach ($rows as $user_data) { |
||
| 153 | $item = []; |
||
| 154 | |||
| 155 | if (!isset($user_data[PR_ACCOUNT])) { |
||
| 156 | $abitem = mapi_ab_openentry($ab, $user_data[PR_ENTRYID]); |
||
| 157 | $user_data = mapi_getprops($abitem, [PR_ACCOUNT, PR_ADDRTYPE, PR_DISPLAY_NAME, PR_DISPLAY_TYPE_EX, PR_ENTRYID, PR_SEARCH_KEY, PR_EMAIL_ADDRESS, PR_SMTP_ADDRESS, PR_OBJECT_TYPE, PR_DISPLAY_TYPE]); |
||
| 158 | } |
||
| 159 | |||
| 160 | if ($excludeGABGroups && $user_data[PR_OBJECT_TYPE] === MAPI_DISTLIST) { |
||
| 161 | // exclude groups from result |
||
| 162 | continue; |
||
| 163 | } |
||
| 164 | |||
| 165 | $item = []; |
||
| 166 | $item['object_type'] = isset($user_data[PR_OBJECT_TYPE]) ? $user_data[PR_OBJECT_TYPE] : MAPI_MAILUSER; |
||
| 167 | $item['entryid'] = isset($user_data[PR_ENTRYID]) ? bin2hex($user_data[PR_ENTRYID]) : ''; |
||
| 168 | $item['display_name'] = isset($user_data[PR_DISPLAY_NAME]) ? $user_data[PR_DISPLAY_NAME] : ''; |
||
| 169 | $item['display_type'] = isset($user_data[PR_DISPLAY_TYPE]) ? $user_data[PR_DISPLAY_TYPE] : DT_MAILUSER; |
||
| 170 | |||
| 171 | // Test whether the GUID in the entryid is from the Contact Provider |
||
| 172 | if ($GLOBALS['entryid']->hasContactProviderGUID($item['entryid'])) { |
||
| 173 | // The properties for a Distribution List differs from the other objects |
||
| 174 | if ($item['object_type'] == MAPI_DISTLIST) { |
||
| 175 | $item['address_type'] = 'MAPIPDL'; |
||
| 176 | // The email_address is empty for DistList, using display name for resolving |
||
| 177 | $item['email_address'] = $item['display_name']; |
||
| 178 | $item['smtp_address'] = isset($item['smtp_address']) ? $item['smtp_address'] : ''; |
||
| 179 | } |
||
| 180 | else { |
||
| 181 | $item['address_type'] = 'EX'; |
||
| 182 | if (isset($user_data['address_type']) && $user_data['address_type'] === 'EX') { |
||
| 183 | $item['email_address'] = isset($user_data[PR_EMAIL_ADDRESS]) ? $user_data[PR_EMAIL_ADDRESS] : ''; |
||
| 184 | } |
||
| 185 | else { |
||
| 186 | // Fake being an EX account, since it's actually an SMTP addrtype the email address is in a different property. |
||
| 187 | $item['smtp_address'] = isset($user_data[PR_EMAIL_ADDRESS]) ? $user_data[PR_EMAIL_ADDRESS] : ''; |
||
| 188 | // Keep the old scenario happy. |
||
| 189 | $item['email_address'] = isset($user_data[PR_EMAIL_ADDRESS]) ? $user_data[PR_EMAIL_ADDRESS] : ''; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | // It can be considered a GAB entry |
||
| 193 | } |
||
| 194 | else { |
||
| 195 | $item['user_name'] = isset($user_data[PR_ACCOUNT]) ? $user_data[PR_ACCOUNT] : $item['display_name']; |
||
| 196 | $item['display_type_ex'] = isset($user_data[PR_DISPLAY_TYPE_EX]) ? $user_data[PR_DISPLAY_TYPE_EX] : MAPI_MAILUSER; |
||
| 197 | $item['email_address'] = isset($user_data[PR_EMAIL_ADDRESS]) ? $user_data[PR_EMAIL_ADDRESS] : ''; |
||
| 198 | $item['smtp_address'] = isset($user_data[PR_SMTP_ADDRESS]) ? $user_data[PR_SMTP_ADDRESS] : $item['email_address']; |
||
| 199 | $item['address_type'] = isset($user_data[PR_ADDRTYPE]) ? $user_data[PR_ADDRTYPE] : 'SMTP'; |
||
| 200 | } |
||
| 201 | |||
| 202 | if (isset($user_data[PR_SEARCH_KEY])) { |
||
| 203 | $item['search_key'] = bin2hex($user_data[PR_SEARCH_KEY]); |
||
| 204 | } |
||
| 205 | else { |
||
| 206 | $emailAddress = isset($item['smtp_address']) ? $item['smtp_address'] : $item['email_address']; |
||
| 207 | $item['search_key'] = bin2hex(strtoupper($item['address_type'] . ':' . $emailAddress)) . '00'; |
||
| 208 | } |
||
| 209 | |||
| 210 | array_push($items, $item); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | return $items; |
||
| 215 | } |
||
| 412 |