function gs_user_external_numbers_get($user) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $user_id = $db->executeGetOne('SELECT `id` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\''); if ($user_id < 1) { return new GsError('Unknown user.'); } # get external numbers # switch (GS_EXTERNAL_NUMBERS_BACKEND) { case 'ldap': //ldapsearch -x -D 'cn=root,dc=example,dc=com' -w secret -b 'ou=People,dc=example,dc=com' '(uid=demo2)' telephoneNumber $ldap = gs_ldap_connect(); if (!$ldap) { return new GsError('Could not connect to LDAP server.'); } $ldap_user = $user; if (gs_get_conf('GS_LVM_USER_6_DIGIT_INT')) { $ldap_user = preg_replace('/^0+/', '', $ldap_user); # if the usernames in your LDAP are integers without a # leading "0" } $userArr = gs_ldap_get_first($ldap, GS_LDAP_SEARCHBASE, GS_LDAP_PROP_USER . '=' . $ldap_user, array(GS_EXTERNAL_NUMBERS_LDAP_PROP)); if (isGsError($userArr)) { return new GsError($userArr->getMsg()); } if (!is_array($userArr)) { //return new GsError( 'User "'. GS_LDAP_PROP_USER .'='. $user .','. GS_LDAP_SEARCHBASE .'" not in LDAP.' ); $numbers = array(); } else { foreach ($userArr as $key => $arr) { if (strCaseCmp($key, GS_EXTERNAL_NUMBERS_LDAP_PROP) == 0) { $numbers = $arr; sort($numbers); break; } } } gs_ldap_disconnect($ldap); break; case 'db': default: $rs = $db->execute('SELECT `number` FROM `users_external_numbers` WHERE `user_id`=' . $user_id . ' ORDER BY `number`'); if (!$rs) { return new GsError('Failed to get external numbers.'); } $numbers = array(); while ($r = $rs->fetchRow()) { $numbers[] = $r['number']; } break; } return $numbers; }
function gs_ldap_user_search($user) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } $GS_LDAP_HOST = gs_get_conf('GS_LDAP_HOST'); if (in_array($GS_LDAP_HOST, array(null, false, '', '0.0.0.0'), true)) { return new GsError('LDAP not configured.'); } if (!preg_match('/^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$/', $GS_LDAP_HOST)) { $tmp = getHostByName($GS_LDAP_HOST); if ($tmp == $GS_LDAP_HOST) { return new GsError('Failed to look up LDAP server.'); } $GS_LDAP_HOST = $tmp; } $tmp = @ip2long($GS_LDAP_HOST); if (in_array($tmp, array(false, null, -1, 0), true)) { return new GsError('LDAP not configured (bad IP address).'); } if (!($ldap_conn = gs_ldap_connect($GS_LDAP_HOST))) { return new GsError('Could not connect to LDAP server.'); } $req_props = array(); $GS_LDAP_PROP_FIRSTNAME = trim(gs_get_conf('GS_LDAP_PROP_FIRSTNAME')); $GS_LDAP_PROP_LASTNAME = trim(gs_get_conf('GS_LDAP_PROP_LASTNAME')); $GS_LDAP_PROP_EMAIL = trim(gs_get_conf('GS_LDAP_PROP_EMAIL')); $GS_LDAP_PROP_PHONE = trim(gs_get_conf('GS_LDAP_PROP_PHONE')); if ($GS_LDAP_PROP_FIRSTNAME != '') { $req_props[] = $GS_LDAP_PROP_FIRSTNAME; } if ($GS_LDAP_PROP_LASTNAME != '') { $req_props[] = $GS_LDAP_PROP_LASTNAME; } if ($GS_LDAP_PROP_EMAIL != '') { $req_props[] = $GS_LDAP_PROP_EMAIL; } if ($GS_LDAP_PROP_PHONE != '') { $req_props[] = $GS_LDAP_PROP_PHONE; } $users_arr = gs_ldap_get_list($ldap_conn, gs_get_conf('GS_LDAP_SEARCHBASE'), gs_get_conf('GS_LDAP_PROP_USER') . '=' . $user, $req_props, 2); //print_r($users_arr); @gs_ldap_disconnect($ldap_conn); if (isGsError($users_arr)) { return $users_arr; } if (!is_array($users_arr) || count($users_arr) < 1) { return new GsError('User "' . $user . '" not found in LDAP.'); } if (count($users_arr) > 1) { return new GsError('LDAP search did not return a unique user for "' . $user . '".'); } $user_arr = $users_arr[0]; unset($users_arr); $user_info = array('fn' => null, 'ln' => null, 'email' => null, 'exten' => null); if (array_key_exists($GS_LDAP_PROP_FIRSTNAME, $user_arr)) { $user_info['fn'] = @$user_arr[$GS_LDAP_PROP_FIRSTNAME][0]; } if (array_key_exists($GS_LDAP_PROP_LASTNAME, $user_arr)) { $user_info['ln'] = @$user_arr[$GS_LDAP_PROP_LASTNAME][0]; } if (array_key_exists($GS_LDAP_PROP_EMAIL, $user_arr)) { $user_info['email'] = @$user_arr[$GS_LDAP_PROP_EMAIL][0]; } if (array_key_exists($GS_LDAP_PROP_PHONE, $user_arr)) { require_once GS_DIR . 'inc/canonization.php'; $phone = @$user_arr[$GS_LDAP_PROP_PHONE][0]; $phone = preg_replace('/[^0-9+#*]/', '', $phone); $cpn = new CanonicalPhoneNumber($phone); if ($cpn->in_prv_branch) { $user_info['exten'] = $cpn->extn; } unset($cpn); } unset($user_arr); gs_log(GS_LOG_DEBUG, 'Found user "' . $user . '" (' . trim($user_info['fn'] . ' ' . $user_info['ln']) . ') in LDAP'); return $user_info; }