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