| Conditions | 4 |
| Paths | > 20000 |
| Total Lines | 376 |
| Code Lines | 227 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | 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 |
||
| 78 | function GABUsers($action, $actionType) |
||
| 79 | { |
||
| 80 | $searchstring = ''; |
||
| 81 | $hide_users = false; |
||
| 82 | $hide_groups = false; |
||
| 83 | $hide_companies = false; |
||
| 84 | |||
| 85 | if(isset($action['restriction'])) { |
||
| 86 | if(isset($action['restriction']['searchstring'])) { |
||
| 87 | // Get search string for searching in AB. |
||
| 88 | $searchstring = $action['restriction']['searchstring']; |
||
| 89 | } |
||
| 90 | |||
| 91 | if(isset($action['restriction']['hide_users'])) { |
||
| 92 | $hide_users = $action['restriction']['hide_users']; |
||
| 93 | } |
||
| 94 | if(isset($action['restriction']['hide_groups'])) { |
||
| 95 | $hide_groups = $action['restriction']['hide_groups']; |
||
| 96 | } |
||
| 97 | if(isset($action['restriction']['hide_companies'])) { |
||
| 98 | $hide_companies = $action['restriction']['hide_companies']; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | $items = array(); |
||
| 103 | |||
| 104 | $data['page'] = array(); |
||
| 105 | $data['page']['start'] = 0; |
||
| 106 | $data['page']['rowcount'] = 0; |
||
| 107 | $data['page']['totalrowcount'] = 0; |
||
| 108 | |||
| 109 | $data = array(); |
||
| 110 | |||
| 111 | $this->sort = array(); |
||
| 112 | |||
| 113 | $map = array(); |
||
| 114 | $map['fileas'] = $this->properties['account']; |
||
| 115 | |||
| 116 | // Rewrite the sort info when sorting on full name as this is a combination of multiple fields |
||
| 117 | global $sortingField; |
||
| 118 | if ( isset($action["sort"]) && is_array($action["sort"]) && count($action["sort"])===1 && isset($action["sort"][0]["field"])) { |
||
| 119 | $sortingDir = $action["sort"][0]["direction"]; |
||
| 120 | $sortingField = $action["sort"][0]["field"]; |
||
| 121 | if($action["sort"][0]["field"] === 'full_name') { |
||
| 122 | $action["sort"] = array( |
||
| 123 | array( |
||
| 124 | "field" => "surname", |
||
| 125 | "direction" => $sortingDir |
||
| 126 | ), |
||
| 127 | array( |
||
| 128 | "field" => "given_name", |
||
| 129 | "direction" => $sortingDir |
||
| 130 | ), |
||
| 131 | array( |
||
| 132 | "field" => "middle_name", |
||
| 133 | "direction" => $sortingDir |
||
| 134 | ), |
||
| 135 | array( |
||
| 136 | "field" => "display_name", |
||
| 137 | "direction" => $sortingDir |
||
| 138 | ), |
||
| 139 | ); |
||
| 140 | } |
||
| 141 | |||
| 142 | // Parse incoming sort order |
||
| 143 | $this->parseSortOrder($action, $map, true); |
||
| 144 | } |
||
| 145 | |||
| 146 | $folderType = $action['folderType']; |
||
| 147 | |||
| 148 | if (($folderType!=='gab' || ENABLE_FULL_GAB) || !empty($searchstring)) { |
||
| 149 | $ab = $GLOBALS['mapisession']->getAddressbook(false, true); |
||
| 150 | |||
| 151 | if (!empty($action['entryid'])) { |
||
| 152 | $entryid = hex2bin($action['entryid']); |
||
| 153 | } else { |
||
| 154 | $entryid = mapi_ab_getdefaultdir($ab); |
||
| 155 | } |
||
| 156 | |||
| 157 | $dir = mapi_ab_openentry($ab,$entryid); |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @TODO: 'All Address Lists' on IABContainer gives MAPI_E_INVALID_PARAMETER, |
||
| 161 | * as it contains subfolders only. When #7344 is fixed, MAPI will return error here, |
||
| 162 | * handle it here and return false. |
||
| 163 | */ |
||
| 164 | $table = mapi_folder_getcontentstable($dir, MAPI_DEFERRED_ERRORS); |
||
| 165 | |||
| 166 | $restriction = false; |
||
| 167 | $tempRestriction = false; |
||
| 168 | $userGroupRestriction = false; |
||
| 169 | |||
| 170 | if($hide_users || $hide_groups || $hide_companies) { |
||
| 171 | $userRestrictions = array(); |
||
| 172 | if ($hide_users) { |
||
| 173 | $tmp = $this->createUsersRestriction($hide_users); |
||
| 174 | if ($tmp) { |
||
| 175 | $userRestrictions[] = $tmp; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | if ($hide_groups) { |
||
| 179 | $tmp = $this->createGroupsRestriction($hide_groups); |
||
| 180 | if ($tmp) { |
||
| 181 | $userRestrictions[] = $tmp; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | if ($hide_companies) { |
||
| 185 | $tmp = $this->createCompanyRestriction($hide_companies); |
||
| 186 | if ($tmp) { |
||
| 187 | $userRestrictions[] = $tmp; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | $userGroupRestriction = Array(RES_AND, $userRestrictions); |
||
| 191 | } |
||
| 192 | |||
| 193 | if(!empty($searchstring)){ |
||
| 194 | // create restriction for search |
||
| 195 | // only return users from who the displayName or the username starts with $searchstring |
||
| 196 | // TODO: use PR_ANR for this restriction instead of PR_DISPLAY_NAME and PR_ACCOUNT |
||
| 197 | $tempRestriction = array(RES_OR, |
||
| 198 | array( |
||
| 199 | // Display name of user from GAB and contacts. |
||
| 200 | array( |
||
| 201 | RES_CONTENT, |
||
| 202 | array(FUZZYLEVEL => FL_SUBSTRING|FL_IGNORECASE, |
||
| 203 | ULPROPTAG => PR_DISPLAY_NAME, |
||
| 204 | VALUE => $searchstring |
||
| 205 | ) |
||
| 206 | ), |
||
| 207 | // fileas value of user from GAB. |
||
| 208 | array( |
||
| 209 | RES_CONTENT, |
||
| 210 | array(FUZZYLEVEL => FL_SUBSTRING|FL_IGNORECASE, |
||
| 211 | ULPROPTAG => PR_ACCOUNT, |
||
| 212 | VALUE => $searchstring |
||
| 213 | ) |
||
| 214 | ), |
||
| 215 | // smtp_address of user from GAB. |
||
| 216 | array( |
||
| 217 | RES_CONTENT, |
||
| 218 | array(FUZZYLEVEL => FL_SUBSTRING|FL_IGNORECASE, |
||
| 219 | ULPROPTAG => PR_SMTP_ADDRESS, |
||
| 220 | VALUE => $searchstring |
||
| 221 | ) |
||
| 222 | ), |
||
| 223 | // email_address of user from GAB and contacts. |
||
| 224 | array( |
||
| 225 | RES_CONTENT, |
||
| 226 | array(FUZZYLEVEL => FL_SUBSTRING|FL_IGNORECASE, |
||
| 227 | ULPROPTAG => PR_EMAIL_ADDRESS, |
||
| 228 | VALUE => $searchstring |
||
| 229 | ) |
||
| 230 | ), |
||
| 231 | // department of user from GAB. |
||
| 232 | array( |
||
| 233 | RES_CONTENT, |
||
| 234 | array(FUZZYLEVEL => FL_SUBSTRING|FL_IGNORECASE, |
||
| 235 | ULPROPTAG => PR_DEPARTMENT_NAME, |
||
| 236 | VALUE => $searchstring |
||
| 237 | ) |
||
| 238 | ), |
||
| 239 | // fileas of user from Contacts. |
||
| 240 | array( |
||
| 241 | RES_CONTENT, |
||
| 242 | array(FUZZYLEVEL => FL_SUBSTRING|FL_IGNORECASE, |
||
| 243 | ULPROPTAG => PR_ORIGINAL_DISPLAY_NAME, |
||
| 244 | VALUE => $searchstring |
||
| 245 | ) |
||
| 246 | ) |
||
| 247 | ) |
||
| 248 | ); |
||
| 249 | } |
||
| 250 | |||
| 251 | if($tempRestriction && $userGroupRestriction) { |
||
| 252 | $restriction = Array( |
||
| 253 | RES_AND, |
||
| 254 | Array( |
||
| 255 | // restriction for search/alphabet bar |
||
| 256 | $tempRestriction, |
||
| 257 | // restriction for hiding users/groups |
||
| 258 | $userGroupRestriction, |
||
| 259 | )); |
||
| 260 | } else if($tempRestriction) { |
||
| 261 | // restriction for search/alphabet bar |
||
| 262 | $restriction = $tempRestriction; |
||
| 263 | } else { |
||
| 264 | // restriction for hiding users/groups |
||
| 265 | $restriction = $userGroupRestriction; |
||
| 266 | } |
||
| 267 | |||
| 268 | // Only add restriction when it is used |
||
| 269 | if($restriction) { |
||
| 270 | mapi_table_restrict($table, $restriction, TBL_BATCH); |
||
| 271 | } |
||
| 272 | // Only sort when asked for |
||
| 273 | if ( !empty($this->sort) ) { |
||
| 274 | mapi_table_sort($table, $this->sort, TBL_BATCH); |
||
| 275 | } |
||
| 276 | |||
| 277 | $rowCount = mapi_table_getrowcount($table); |
||
| 278 | |||
| 279 | if ( is_int(MAX_GAB_RESULTS) && MAX_GAB_RESULTS > 0 && $rowCount > MAX_GAB_RESULTS ) { |
||
| 280 | // Create a response that contains an error message that there are too much results |
||
| 281 | $data['error'] = array('code' => 'listexceederror', 'max_gab_users' => MAX_GAB_RESULTS); |
||
| 282 | $rows = mapi_table_queryrows($table, $this->properties, 0, MAX_GAB_RESULTS); |
||
| 283 | $rowCount = MAX_GAB_RESULTS; |
||
| 284 | } else { |
||
| 285 | $rows = mapi_table_queryallrows($table, $this->properties); |
||
| 286 | } |
||
| 287 | |||
| 288 | $sharedStore = null; |
||
| 289 | if (isset($action["isSharedFolder"]) && $action["isSharedFolder"] === true) { |
||
| 290 | if(isset($action["sharedFolder"]) && !empty($action["sharedFolder"])) { |
||
| 291 | $sharedStoreEntryID = $action["sharedFolder"]["store_entryid"]; |
||
| 292 | $sharedStore = $GLOBALS["mapisession"]->openMessageStore(hex2bin($sharedStoreEntryID)); |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | for ($i = 0, $len = $rowCount; $i < $len; $i++) { |
||
| 297 | // Use array_shift to so we won't double memory usage! |
||
| 298 | $user_data = array_shift($rows); |
||
| 299 | $item = array(); |
||
| 300 | $item['entryid'] = bin2hex($user_data[$this->properties['entryid']]); |
||
| 301 | $item['display_name'] = isset($user_data[$this->properties['display_name']]) ? $user_data[$this->properties['display_name']] : ""; |
||
| 302 | $item['object_type'] = isset($user_data[$this->properties['object_type']]) ? $user_data[$this->properties['object_type']] : ""; |
||
| 303 | $item['display_type'] = isset($user_data[PR_DISPLAY_TYPE]) ? $user_data[PR_DISPLAY_TYPE] : ""; |
||
| 304 | $item['title'] = isset($user_data[PR_TITLE]) ? $user_data[PR_TITLE] : ""; |
||
| 305 | $item['company_name'] = isset($user_data[PR_COMPANY_NAME]) ? $user_data[PR_COMPANY_NAME] : ""; |
||
| 306 | |||
| 307 | // Test whether the GUID in the entryid is from the Contact Provider |
||
| 308 | if($GLOBALS['entryid']->hasContactProviderGUID( bin2hex($user_data[$this->properties['entryid']]) )){ |
||
| 309 | // Use the original_display_name property to fill in the fileas column |
||
| 310 | $item['fileas'] = $user_data[$this->properties['original_display_name']] ?? $item['display_name']; |
||
| 311 | $item['address_type'] = isset($user_data[$this->properties['address_type']]) ? $user_data[$this->properties['address_type']] : 'SMTP'; |
||
| 312 | |||
| 313 | if (isset($action["isSharedFolder"]) && $action["isSharedFolder"] === true) { |
||
| 314 | if(isset($action["sharedFolder"]) && !empty($action["sharedFolder"])) { |
||
| 315 | $sharedContactEntryID = $GLOBALS['entryid']->unwrapABEntryIdObj(bin2hex($user_data[$this->properties['entryid']])); |
||
| 316 | // Address book record does not have 'private' property so we need to open shared contact to |
||
| 317 | // get the value of 'private' property. |
||
| 318 | $contact = $GLOBALS['operations']->openMessage($sharedStore, hex2bin($sharedContactEntryID)); |
||
| 319 | $sharedContactProps = mapi_getprops($contact, array($this->properties['private'])); |
||
| 320 | // Don't show the private contact. |
||
| 321 | if (isset($sharedContactProps[$this->properties['private']]) && $sharedContactProps[$this->properties['private']] === true) { |
||
| 322 | continue; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | switch($user_data[PR_DISPLAY_TYPE]){ |
||
| 328 | case DT_PRIVATE_DISTLIST: |
||
| 329 | $item['email_address'] = ''; |
||
| 330 | break; |
||
| 331 | case DT_MAILUSER: |
||
| 332 | default: |
||
| 333 | $item['email_address'] = $user_data[$this->properties['email_address']]; |
||
| 334 | } |
||
| 335 | } else { |
||
| 336 | // If display_type_ex is not set we can overwrite it with display_type |
||
| 337 | $item['display_type_ex'] = isset($user_data[PR_DISPLAY_TYPE_EX])?$user_data[PR_DISPLAY_TYPE_EX]:$user_data[PR_DISPLAY_TYPE]; |
||
| 338 | $item['fileas'] = $item['display_name']; |
||
| 339 | $item['mobile_telephone_number'] = isset($user_data[PR_MOBILE_TELEPHONE_NUMBER])? $user_data[PR_MOBILE_TELEPHONE_NUMBER] : ''; |
||
| 340 | $item['home_telephone_number'] = isset($user_data[PR_HOME_TELEPHONE_NUMBER])? $user_data[PR_HOME_TELEPHONE_NUMBER] : ''; |
||
| 341 | $item['pager_telephone_number'] = isset($user_data[PR_PAGER_TELEPHONE_NUMBER])? $user_data[PR_PAGER_TELEPHONE_NUMBER] : ''; |
||
| 342 | $item['surname'] = isset($user_data[PR_SURNAME])? $user_data[PR_SURNAME] : ''; |
||
| 343 | $item['given_name'] = isset($user_data[$this->properties['given_name']])? $user_data[$this->properties['given_name']] : ''; |
||
| 344 | |||
| 345 | switch($user_data[PR_DISPLAY_TYPE]){ |
||
| 346 | case DT_ORGANIZATION: |
||
| 347 | $item['email_address'] = $user_data[$this->properties['account']]; |
||
| 348 | $item['address_type'] = 'EX'; |
||
| 349 | // The account property is used to fill in the fileas column |
||
| 350 | $item['fileas'] = $user_data[$this->properties['account']]; |
||
| 351 | break; |
||
| 352 | |||
| 353 | case DT_DISTLIST: |
||
| 354 | // The account property is used to fill in the fileas column, private dislist does not have that |
||
| 355 | $item['fileas'] = $user_data[$this->properties['account']]; |
||
| 356 | case DT_PRIVATE_DISTLIST: |
||
| 357 | $item['email_address'] = $user_data[$this->properties['account']]; |
||
| 358 | // FIXME: shouldn't be needed, but atm this gives us an undefined offset error which makes the unittests fail. |
||
| 359 | if($item['email_address'] !== 'Everyone') { |
||
| 360 | if (isset($user_data[$this->properties['smtp_address']])) { |
||
| 361 | $item['smtp_address'] = $user_data[$this->properties['smtp_address']]; |
||
| 362 | } |
||
| 363 | } |
||
| 364 | $item['address_type'] = 'EX'; |
||
| 365 | break; |
||
| 366 | |||
| 367 | case DT_MAILUSER: |
||
| 368 | // The account property is used to fill in the fileas column, remote mailuser does not have that |
||
| 369 | $item['fileas'] = $user_data[$this->properties['account']]; |
||
| 370 | case DT_REMOTE_MAILUSER: |
||
| 371 | default: |
||
| 372 | $item['email_address'] = $user_data[$this->properties['email_address']]; |
||
| 373 | $item['smtp_address'] = $user_data[$this->properties['smtp_address']]; |
||
| 374 | |||
| 375 | $item['address_type'] = isset($user_data[$this->properties['address_type']]) ? $user_data[$this->properties['address_type']] : 'SMTP'; |
||
| 376 | $item['department_name'] = isset($user_data[$this->properties['department_name']]) ? $user_data[$this->properties['department_name']] : ''; |
||
| 377 | $item['office_telephone_number'] = isset($user_data[$this->properties['office_telephone_number']]) ? $user_data[$this->properties['office_telephone_number']] : ''; |
||
| 378 | $item['office_location'] = isset($user_data[$this->properties['office_location']]) ? $user_data[$this->properties['office_location']] : ''; |
||
| 379 | $item['primary_fax_number'] = isset($user_data[$this->properties['primary_fax_number']]) ? $user_data[$this->properties['primary_fax_number']] : ''; |
||
| 380 | break; |
||
| 381 | } |
||
| 382 | } |
||
| 383 | |||
| 384 | // Create a nice full_name prop ("Lastname, Firstname Middlename") |
||
| 385 | if ( isset($user_data[$this->properties['surname']]) ){ |
||
| 386 | $item['full_name'] = $user_data[$this->properties['surname']]; |
||
| 387 | } else { |
||
| 388 | $item['full_name'] = ''; |
||
| 389 | } |
||
| 390 | if ( (isset($user_data[$this->properties['given_name']]) || isset($user_data[$this->properties['middle_name']])) && !empty($item['full_name']) ){ |
||
| 391 | $item['full_name'] .= ', '; |
||
| 392 | } |
||
| 393 | if ( isset($user_data[$this->properties['given_name']]) ){ |
||
| 394 | $item['full_name'] .= $user_data[$this->properties['given_name']]; |
||
| 395 | } |
||
| 396 | if ( isset($user_data[$this->properties['middle_name']]) ){ |
||
| 397 | $item['full_name'] .= ' ' . $user_data[$this->properties['middle_name']]; |
||
| 398 | } |
||
| 399 | if ( empty($item['full_name']) ){ |
||
| 400 | $item['full_name'] = $item['display_name']; |
||
| 401 | } |
||
| 402 | |||
| 403 | if(!empty($user_data[$this->properties['search_key']])) { |
||
| 404 | $item['search_key'] = bin2hex($user_data[$this->properties['search_key']]); |
||
| 405 | } else { |
||
| 406 | // contacts folders are not returning search keys, this should be fixed in Gromox |
||
| 407 | // meanwhile this is a workaround, check ZCP-10814 |
||
| 408 | // if search key is not passed then we will generate it |
||
| 409 | $email_address = ''; |
||
| 410 | if(!empty($item['smtp_address'])) { |
||
| 411 | $email_address = $item['smtp_address']; |
||
| 412 | } else if(!empty($item['email_address'])) { |
||
| 413 | $email_address = $item['email_address']; |
||
| 414 | } |
||
| 415 | |||
| 416 | if(!empty($email_address)) { |
||
| 417 | $item['search_key'] = bin2hex(strtoupper($item['address_type'] . ':' . $email_address)) . '00'; |
||
| 418 | } |
||
| 419 | } |
||
| 420 | |||
| 421 | array_push($items, array('props' => $item)); |
||
| 422 | } |
||
| 423 | |||
| 424 | if ( !empty($sortingField) ){ |
||
| 425 | // Sort the items here, because full_name is not a real property, so we can not use the regular sorting |
||
| 426 | // Note: This hack only works because the GAB does not work with paging! |
||
| 427 | function cmpAsc($a, $b){ |
||
| 428 | global $sortingField; |
||
| 429 | return strcasecmp($b['props'][$sortingField], $a['props'][$sortingField]); |
||
| 430 | } |
||
| 431 | function cmpDesc($a, $b){ |
||
| 432 | global $sortingField; |
||
| 433 | return strcasecmp($a['props'][$sortingField], $b['props'][$sortingField]); |
||
| 434 | } |
||
| 435 | |||
| 436 | $cmpFn = $sortingDir === 'DESC' ? 'cmpDesc' : 'cmpAsc'; |
||
| 437 | usort($items, $cmpFn); |
||
| 438 | } |
||
| 439 | |||
| 440 | // todo: fix paging stuff |
||
| 441 | $data['page']['start'] = 0; |
||
| 442 | $data['page']['rowcount'] = $rowCount; |
||
| 443 | $data['page']['totalrowcount'] = $data['page']['rowcount']; |
||
| 444 | $data = array_merge($data, array('item'=>$items)); |
||
| 445 | } else { |
||
| 446 | // Provide clue that full GAB is disabled. |
||
| 447 | $data = array_merge($data, array('disable_full_gab' => !ENABLE_FULL_GAB)); |
||
| 448 | } |
||
| 449 | |||
| 450 | $this->addActionData('list', $data); |
||
| 451 | $GLOBALS['bus']->addData($this->getResponseData()); |
||
| 452 | |||
| 453 | return true; |
||
| 454 | } |
||
| 921 |