| Conditions | 4 |
| Paths | 182 |
| Total Lines | 301 |
| Code Lines | 202 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 12 | ||
| Bugs | 3 | 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 |
||
| 67 | public function GABUsers($action, $actionType) { |
||
| 68 | $searchstring = $action['restriction']['searchstring'] ?? ''; |
||
| 69 | $hide_users = $action['restriction']['hide_users'] ?? false; |
||
| 70 | $hide_groups = $action['restriction']['hide_groups'] ?? false; |
||
| 71 | $hide_companies = $action['restriction']['hide_companies'] ?? false; |
||
| 72 | $items = []; |
||
| 73 | $data = []; |
||
| 74 | $this->sort = []; |
||
| 75 | $map = []; |
||
| 76 | $map['fileas'] = $this->properties['account']; |
||
| 77 | $sortingDir = $action["sort"][0]["direction"] ?? 'ASC'; |
||
| 78 | $sortingField = $this->getSortingField($action, $map, $sortingDir); |
||
| 79 | $folderType = $action['folderType']; |
||
| 80 | $sharedStore = null; |
||
| 81 | $isSharedFolder = $folderType === 'sharedcontacts' && isset($action["sharedFolder"]["store_entryid"]); |
||
| 82 | |||
| 83 | if (($folderType !== 'gab' || ENABLE_FULL_GAB) || !empty($searchstring)) { |
||
| 84 | $table = null; |
||
| 85 | $ab = $GLOBALS['mapisession']->getAddressbook(false, true); |
||
| 86 | $entryid = !empty($action['entryid']) ? hex2bin((string) $action['entryid']) : mapi_ab_getdefaultdir($ab); |
||
| 87 | |||
| 88 | try { |
||
| 89 | $dir = mapi_ab_openentry($ab, $entryid); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @TODO: 'All Address Lists' on IABContainer gives MAPI_E_INVALID_PARAMETER, |
||
| 93 | * as it contains subfolders only. When #7344 is fixed, MAPI will return error here, |
||
| 94 | * handle it here and return false. |
||
| 95 | */ |
||
| 96 | $table = mapi_folder_getcontentstable($dir, MAPI_DEFERRED_ERRORS); |
||
| 97 | } |
||
| 98 | catch (MAPIException) { |
||
| 99 | // for the shared and public contact folders open the store |
||
| 100 | // and get the contents of the folder |
||
| 101 | if ($isSharedFolder) { |
||
| 102 | $sharedStore = $GLOBALS["mapisession"]->openMessageStore( |
||
| 103 | hex2bin((string) $action["sharedFolder"]["store_entryid"]) |
||
| 104 | ); |
||
| 105 | $sharedContactsFolder = mapi_msgstore_openentry($sharedStore, $entryid); |
||
| 106 | $table = mapi_folder_getcontentstable($sharedContactsFolder, MAPI_DEFERRED_ERRORS); |
||
| 107 | $this->properties = $GLOBALS['properties']->getContactProperties(); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | if ($table) { |
||
| 112 | $restriction = $this->getRestriction($searchstring, $hide_users, $hide_groups, $hide_companies); |
||
| 113 | // Only add restriction when it is used |
||
| 114 | if (!empty($restriction)) { |
||
| 115 | mapi_table_restrict($table, $restriction, TBL_BATCH); |
||
| 116 | } |
||
| 117 | // Only sort when asked for |
||
| 118 | if (!empty($this->sort)) { |
||
| 119 | mapi_table_sort($table, $this->sort, TBL_BATCH); |
||
| 120 | } |
||
| 121 | |||
| 122 | $rowCount = mapi_table_getrowcount($table); |
||
| 123 | |||
| 124 | if (is_int(MAX_GAB_RESULTS) && MAX_GAB_RESULTS > 0 && $rowCount > MAX_GAB_RESULTS) { |
||
| 125 | // Create a response that contains an error message that there are too much results |
||
| 126 | $data['error'] = ['code' => 'listexceederror', 'max_gab_users' => MAX_GAB_RESULTS]; |
||
| 127 | $rows = mapi_table_queryrows($table, $this->properties, 0, MAX_GAB_RESULTS); |
||
| 128 | $rowCount = MAX_GAB_RESULTS; |
||
| 129 | } |
||
| 130 | else { |
||
| 131 | $rows = mapi_table_queryallrows($table, $this->properties); |
||
| 132 | } |
||
| 133 | |||
| 134 | for ($i = 0, $len = $rowCount; $i < $len; ++$i) { |
||
| 135 | // Use array_shift to so we won't double memory usage! |
||
| 136 | $user_data = array_shift($rows); |
||
| 137 | $abprovidertype = 0; |
||
| 138 | $item = []; |
||
| 139 | $entryid = bin2hex((string) $user_data[$this->properties['entryid']]); |
||
| 140 | $item['entryid'] = $entryid; |
||
| 141 | $item['display_name'] = $user_data[$this->properties['display_name']] ?? ""; |
||
| 142 | $item['object_type'] = $user_data[$this->properties['object_type']] ?? ""; |
||
| 143 | $item['display_type'] = $user_data[PR_DISPLAY_TYPE] ?? ""; |
||
| 144 | $item['title'] = $user_data[PR_TITLE] ?? ""; |
||
| 145 | $item['company_name'] = $user_data[PR_COMPANY_NAME] ?? ""; |
||
| 146 | |||
| 147 | // Test whether the GUID in the entryid is from the Contact Provider |
||
| 148 | if ($GLOBALS['entryid']->hasContactProviderGUID(bin2hex((string) $user_data[$this->properties['entryid']]))) { |
||
| 149 | // Use the original_display_name property to fill in the fileas column |
||
| 150 | $item['fileas'] = $user_data[$this->properties['original_display_name']] ?? $item['display_name']; |
||
| 151 | $item['address_type'] = $user_data[$this->properties['address_type']] ?? 'SMTP'; |
||
| 152 | // necessary to display the proper icon |
||
| 153 | if ($item['address_type'] === 'MAPIPDL') { |
||
| 154 | $item['display_type_ex'] = DTE_FLAG_ACL_CAPABLE | DT_MAILUSER | DT_DISTLIST; |
||
| 155 | } |
||
| 156 | |||
| 157 | $item['email_address'] = match ($user_data[PR_DISPLAY_TYPE]) { |
||
| 158 | DT_PRIVATE_DISTLIST => '', |
||
| 159 | default => $user_data[$this->properties['email_address']], |
||
| 160 | }; |
||
| 161 | } |
||
| 162 | elseif ($isSharedFolder && $sharedStore) { |
||
| 163 | // do not display private items |
||
| 164 | if (isset($user_data[$this->properties['private']]) && $user_data[$this->properties['private']] === true) { |
||
| 165 | continue; |
||
| 166 | } |
||
| 167 | // do not display items without an email address |
||
| 168 | // a shared contact is either a single contact item or a distribution list |
||
| 169 | $isContact = strcasecmp((string) $user_data[$this->properties['message_class']], 'IPM.Contact') === 0; |
||
| 170 | if ($isContact && |
||
| 171 | empty($user_data[$this->properties["email_address_1"]]) && |
||
| 172 | empty($user_data[$this->properties["email_address_2"]]) && |
||
| 173 | empty($user_data[$this->properties["email_address_3"]])) { |
||
| 174 | continue; |
||
| 175 | } |
||
| 176 | $item['display_type_ex'] = $isContact ? DT_MAILUSER : DT_MAILUSER | DT_PRIVATE_DISTLIST; |
||
| 177 | $item['display_type'] = $isContact ? DT_MAILUSER : DT_DISTLIST; |
||
| 178 | $item['fileas'] = $item['display_name']; |
||
| 179 | $item['surname'] = $user_data[PR_SURNAME] ?? ''; |
||
| 180 | $item['given_name'] = $user_data[$this->properties['given_name']] ?? ''; |
||
| 181 | $item['object_type'] = $user_data[PR_ICON_INDEX] == 512 ? MAPI_MAILUSER : MAPI_DISTLIST; |
||
| 182 | $item['store_entryid'] = $action["sharedFolder"]["store_entryid"]; |
||
| 183 | $item['is_shared'] = true; |
||
| 184 | if (!empty($user_data[$this->properties["email_address_type_1"]])) { |
||
| 185 | $abprovidertype |= 1; |
||
| 186 | } |
||
| 187 | if (!empty($user_data[$this->properties["email_address_type_2"]])) { |
||
| 188 | $abprovidertype |= 2; |
||
| 189 | } |
||
| 190 | if (!empty($user_data[$this->properties["email_address_type_3"]])) { |
||
| 191 | $abprovidertype |= 4; |
||
| 192 | } |
||
| 193 | |||
| 194 | switch ($abprovidertype) { |
||
| 195 | case 1: |
||
| 196 | case 3: |
||
| 197 | case 5: |
||
| 198 | case 7: |
||
| 199 | $item['entryid'] .= '01'; |
||
| 200 | $item['address_type'] = $user_data[$this->properties["email_address_type_1"]]; |
||
| 201 | $item['email_address'] = $user_data[$this->properties["email_address_1"]]; |
||
| 202 | break; |
||
| 203 | |||
| 204 | case 2: |
||
| 205 | case 6: |
||
| 206 | $item['entryid'] .= '02'; |
||
| 207 | $item['address_type'] = $user_data[$this->properties["email_address_type_2"]]; |
||
| 208 | $item['email_address'] = $user_data[$this->properties["email_address_2"]]; |
||
| 209 | break; |
||
| 210 | |||
| 211 | case 4: |
||
| 212 | $item['entryid'] .= '03'; |
||
| 213 | $item['address_type'] = $user_data[$this->properties["email_address_type_3"]]; |
||
| 214 | $item['email_address'] = $user_data[$this->properties["email_address_3"]]; |
||
| 215 | break; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | else { |
||
| 219 | // If display_type_ex is not set we can overwrite it with display_type |
||
| 220 | $item['display_type_ex'] = $user_data[PR_DISPLAY_TYPE_EX] ?? $user_data[PR_DISPLAY_TYPE]; |
||
| 221 | $item['fileas'] = $item['display_name']; |
||
| 222 | $item['mobile_telephone_number'] = $user_data[PR_MOBILE_TELEPHONE_NUMBER] ?? ''; |
||
| 223 | $item['home_telephone_number'] = $user_data[PR_HOME_TELEPHONE_NUMBER] ?? ''; |
||
| 224 | $item['pager_telephone_number'] = $user_data[PR_PAGER_TELEPHONE_NUMBER] ?? ''; |
||
| 225 | $item['surname'] = $user_data[PR_SURNAME] ?? ''; |
||
| 226 | $item['given_name'] = $user_data[$this->properties['given_name']] ?? ''; |
||
| 227 | |||
| 228 | switch ($user_data[PR_DISPLAY_TYPE]) { |
||
| 229 | case DT_ORGANIZATION: |
||
| 230 | $item['email_address'] = $user_data[$this->properties['account']]; |
||
| 231 | $item['address_type'] = 'EX'; |
||
| 232 | // The account property is used to fill in the fileas column |
||
| 233 | $item['fileas'] = $user_data[$this->properties['account']]; |
||
| 234 | break; |
||
| 235 | |||
| 236 | case DT_DISTLIST: |
||
| 237 | // The account property is used to fill in the fileas column, private dislist does not have that |
||
| 238 | $item['fileas'] = $user_data[$this->properties['account']]; |
||
| 239 | |||
| 240 | // no break |
||
| 241 | case DT_PRIVATE_DISTLIST: |
||
| 242 | $item['email_address'] = $user_data[$this->properties['email_address']] ?? $user_data[$this->properties['account']]; |
||
| 243 | // FIXME: shouldn't be needed, but atm this gives us an undefined offset error which makes the unittests fail. |
||
| 244 | if ($item['email_address'] !== 'Everyone' && isset($user_data[$this->properties['smtp_address']])) { |
||
| 245 | $item['smtp_address'] = $user_data[$this->properties['smtp_address']]; |
||
| 246 | } |
||
| 247 | $item['address_type'] = 'EX'; |
||
| 248 | break; |
||
| 249 | |||
| 250 | case DT_MAILUSER: |
||
| 251 | // The account property is used to fill in the fileas column, remote mailuser does not have that |
||
| 252 | $item['fileas'] = $user_data[$this->properties['account']]; |
||
| 253 | |||
| 254 | // no break |
||
| 255 | case DT_REMOTE_MAILUSER: |
||
| 256 | default: |
||
| 257 | $item['email_address'] = $user_data[$this->properties['email_address']]; |
||
| 258 | $item['smtp_address'] = $user_data[$this->properties['smtp_address']]; |
||
| 259 | |||
| 260 | $item['address_type'] = $user_data[$this->properties['address_type']] ?? 'SMTP'; |
||
| 261 | $item['department_name'] = $user_data[$this->properties['department_name']] ?? ''; |
||
| 262 | $item['office_telephone_number'] = $user_data[$this->properties['office_telephone_number']] ?? ''; |
||
| 263 | $item['office_location'] = $user_data[$this->properties['office_location']] ?? ''; |
||
| 264 | $item['primary_fax_number'] = $user_data[$this->properties['primary_fax_number']] ?? ''; |
||
| 265 | break; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | // Create a nice full_name prop ("Lastname, Firstname Middlename") |
||
| 270 | if (isset($user_data[$this->properties['surname']])) { |
||
| 271 | $item['full_name'] = $user_data[$this->properties['surname']]; |
||
| 272 | } |
||
| 273 | else { |
||
| 274 | $item['full_name'] = ''; |
||
| 275 | } |
||
| 276 | if ((isset($user_data[$this->properties['given_name']]) || isset($user_data[$this->properties['middle_name']])) && !empty($item['full_name'])) { |
||
| 277 | $item['full_name'] .= ', '; |
||
| 278 | } |
||
| 279 | if (isset($user_data[$this->properties['given_name']])) { |
||
| 280 | $item['full_name'] .= $user_data[$this->properties['given_name']]; |
||
| 281 | } |
||
| 282 | if (isset($user_data[$this->properties['middle_name']])) { |
||
| 283 | $item['full_name'] .= ' ' . $user_data[$this->properties['middle_name']]; |
||
| 284 | } |
||
| 285 | if (empty($item['full_name'])) { |
||
| 286 | $item['full_name'] = $item['display_name']; |
||
| 287 | } |
||
| 288 | |||
| 289 | if (!empty($user_data[$this->properties['search_key']])) { |
||
| 290 | $item['search_key'] = bin2hex((string) $user_data[$this->properties['search_key']]); |
||
| 291 | } |
||
| 292 | else { |
||
| 293 | // contacts folders are not returning search keys, this should be fixed in Gromox |
||
| 294 | // meanwhile this is a workaround, check ZCP-10814 |
||
| 295 | // if search key is not passed then we will generate it |
||
| 296 | $email_address = ''; |
||
| 297 | if (!empty($item['smtp_address'])) { |
||
| 298 | $email_address = $item['smtp_address']; |
||
| 299 | } |
||
| 300 | elseif (!empty($item['email_address'])) { |
||
| 301 | $email_address = $item['email_address']; |
||
| 302 | } |
||
| 303 | |||
| 304 | if (!empty($email_address)) { |
||
| 305 | $item['search_key'] = bin2hex(strtoupper($item['address_type'] . ':' . $email_address)) . '00'; |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | array_push($items, ['props' => $item]); |
||
| 310 | if ($isSharedFolder && $sharedStore) { |
||
| 311 | switch ($abprovidertype) { |
||
| 312 | case 3: |
||
| 313 | $item['address_type'] = $user_data[$this->properties["email_address_type_2"]]; |
||
| 314 | $item['email_address'] = $user_data[$this->properties["email_address_2"]]; |
||
| 315 | $item['search_key'] = bin2hex(strtoupper($item['address_type'] . ':' . $item['email_address'])) . '00'; |
||
| 316 | $item['entryid'] = $entryid . '02'; |
||
| 317 | array_push($items, ['props' => $item]); |
||
| 318 | break; |
||
| 319 | |||
| 320 | case 5: |
||
| 321 | case 6: |
||
| 322 | $item['address_type'] = $user_data[$this->properties["email_address_type_3"]]; |
||
| 323 | $item['email_address'] = $user_data[$this->properties["email_address_3"]]; |
||
| 324 | $item['search_key'] = bin2hex(strtoupper($item['address_type'] . ':' . $item['email_address'])) . '00'; |
||
| 325 | $item['entryid'] = $entryid . '03'; |
||
| 326 | array_push($items, ['props' => $item]); |
||
| 327 | break; |
||
| 328 | |||
| 329 | case 7: |
||
| 330 | $item['address_type'] = $user_data[$this->properties["email_address_type_2"]]; |
||
| 331 | $item['email_address'] = $user_data[$this->properties["email_address_2"]]; |
||
| 332 | $item['search_key'] = bin2hex(strtoupper($item['address_type'] . ':' . $item['email_address'])) . '00'; |
||
| 333 | $item['entryid'] = $entryid . '02'; |
||
| 334 | array_push($items, ['props' => $item]); |
||
| 335 | $item['address_type'] = $user_data[$this->properties["email_address_type_3"]]; |
||
| 336 | $item['email_address'] = $user_data[$this->properties["email_address_3"]]; |
||
| 337 | $item['search_key'] = bin2hex(strtoupper($item['address_type'] . ':' . $item['email_address'])) . '00'; |
||
| 338 | $item['entryid'] = $entryid . '03'; |
||
| 339 | array_push($items, ['props' => $item]); |
||
| 340 | break; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | function sorter($direction, $key) { |
||
| 346 | return fn ($a, $b) => $direction == 'ASC' ? |
||
| 347 | strcasecmp($a['props'][$key] ?? '', $b['props'][$key] ?? '') : |
||
| 348 | strcasecmp($b['props'][$key] ?? '', $a['props'][$key] ?? ''); |
||
| 349 | } |
||
| 350 | usort($items, sorter($sortingDir, $sortingField)); |
||
| 351 | |||
| 352 | // todo: fix paging stuff |
||
| 353 | $data['page']['start'] = 0; |
||
| 354 | $data['page']['rowcount'] = $rowCount; |
||
| 355 | $data['page']['totalrowcount'] = $data['page']['rowcount']; |
||
| 356 | $data = array_merge($data, ['item' => $items]); |
||
| 357 | } |
||
| 358 | } |
||
| 359 | else { |
||
| 360 | // Provide clue that full GAB is disabled. |
||
| 361 | $data = array_merge($data, ['disable_full_gab' => !ENABLE_FULL_GAB]); |
||
| 362 | } |
||
| 363 | |||
| 364 | $this->addActionData('list', $data); |
||
| 365 | $GLOBALS['bus']->addData($this->getResponseData()); |
||
| 366 | |||
| 367 | return true; |
||
| 368 | } |
||
| 1163 |