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