function gs_user_external_number_del($user, $number) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } if (!preg_match('/^[\\d]+$/', $number)) { return new GsError('Number must be numeric.'); } # 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.'); } switch (GS_EXTERNAL_NUMBERS_BACKEND) { case 'ldap': $ldap = gs_ldap_connect(); if (!$ldap) { return new GsError('Could not connect to LDAP server.'); } # check if number exists (to return proper err msg) # /* $numbers = gs_user_external_numbers_get( $user ); if (isGsError($numbers)) return new GsError( $numbers->getMsg() ); if (! is_array($numbers)) return new GsError( 'Failed to get numbers from LDAP' ); if (! in_array($number, $numbers, true)) return new GsError( 'No such number.' ); */ # find ldap user name # if (GS_LDAP_PROP_UID === GS_LDAP_PROP_USER) { $ldap_uid = $user; if (gs_get_conf('GS_LVM_USER_6_DIGIT_INT')) { $user = preg_replace('/^0+/', '', $user); # if the usernames in your LDAP are integers without # a leading "0" } } else { if (gs_get_conf('GS_LVM_USER_6_DIGIT_INT')) { $user = preg_replace('/^0+/', '', $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 . '=' . $user, array(GS_LDAP_PROP_UID)); if (isGsError($userArr)) { return new GsError($userArr->getMsg()); } if (!is_array($userArr)) { return new GsError('Could not find user by "' . GS_LDAP_PROP_USER . '=' . $user . '" in search base "' . GS_LDAP_SEARCHBASE . '" in LDAP.'); } $ldap_uid = @$userArr[strToLower(GS_LDAP_PROP_UID)][0]; if (strLen($ldap_uid) < 1) { return new GsError('Could not find user by "' . GS_LDAP_PROP_USER . '=' . $user . '" in search base "' . GS_LDAP_SEARCHBASE . '" in LDAP.'); } } $dn = GS_LDAP_PROP_UID . '=' . $ldap_uid . ',' . GS_LDAP_SEARCHBASE; # delete number # $ok = @ldap_mod_del($ldap, $dn, array(GS_EXTERNAL_NUMBERS_LDAP_PROP => $number)); if (!$ok) { if (@ldap_errNo($ldap) == 16) { // err #16 is: "No such attribute" return new GsError('No such number.'); } return new GsError('Failed to delete number for LDAP user "' . $dn . '". - ' . gs_get_ldap_error($ldap)); } break; case 'db': default: # check if number exists (to return proper err msg) # $num = (int) $db->executeGetOne('SELECT COUNT(*) FROM `users_external_numbers` WHERE `user_id`=' . $user_id . ' AND `number`=\'' . $db->escape($number) . '\''); if ($num < 1) { return new GsError('No such number.'); } # delete number # $ok = $db->execute('DELETE FROM `users_external_numbers` WHERE `user_id`=' . $user_id . ' AND `number`=\'' . $db->escape($number) . '\''); if (!$ok) { return new GsError('Failed to delete external number.'); } break; } return true; }
function _gui_monitor_which_peers_lvm($sudo_user) { $kks = @_get_kostenstellen_lvm($sudo_user); if ($kks === false || !is_array($kks)) { return false; } $kostenstelle_prop = 'lvmkostenstelle'; $limit = 100; $filter = ''; foreach ($kks as $ks) { $filter .= '(' . $kostenstelle_prop . '=' . subStr($ks, 0, 2) . '*)'; } $filter = '(|' . $filter . ')'; //echo $filter, "<br />\n"; $ldap = gs_ldap_connect(); $matches = gs_ldap_get_list($ldap, GS_LDAP_SEARCHBASE, $filter, array(GS_LDAP_PROP_USER), (int) $limit); if (isGsError($matches)) { return false; } if (!is_array($matches)) { return false; } /* echo "<pre>"; print_r($matches); echo "</pre>"; */ $lc_GS_LDAP_PROP_USER = strToLower(GS_LDAP_PROP_USER); $peers = array(); foreach ($matches as $match) { if (!is_array($match[$lc_GS_LDAP_PROP_USER])) { continue; } foreach ($match[$lc_GS_LDAP_PROP_USER] as $mm) { //if (gs_get_conf('GS_LVM_USER_6_DIGIT_INT')) { // this check is not really needed as this is a custom function anyway $mm = str_pad($mm, 6, '0', STR_PAD_LEFT); # without leading "0" in their LDAP database //} $peers[] = $mm; } } /* echo "<pre>"; print_r($peers); echo "</pre>"; */ return $peers; }
function gs_user_external_number_add($user, $number) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } if (!preg_match('/^[\\d]+$/', $number)) { return new GsError('Number must be numeric.'); } # 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.'); } # add number # switch (GS_EXTERNAL_NUMBERS_BACKEND) { case 'ldap': $ldap = gs_ldap_connect(); if (!$ldap) { return new GsError('Could not connect to LDAP server.'); } # find ldap user name # if (GS_LDAP_PROP_UID === GS_LDAP_PROP_USER) { $ldap_uid = $user; if (gs_get_conf('GS_LVM_USER_6_DIGIT_INT')) { $user = preg_replace('/^0+/', '', $user); # if the usernames in your LDAP are integers without # a leading "0" } } else { if (gs_get_conf('GS_LVM_USER_6_DIGIT_INT')) { $user = preg_replace('/^0+/', '', $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 . '=' . $user, array(GS_LDAP_PROP_UID)); if (isGsError($userArr)) { return new GsError($userArr->getMsg()); } if (!is_array($userArr)) { return new GsError('Could not find user by "' . GS_LDAP_PROP_USER . '=' . $user . '" in search base "' . GS_LDAP_SEARCHBASE . '" in LDAP.'); } $ldap_uid = @$userArr[strToLower(GS_LDAP_PROP_UID)][0]; if (strLen($ldap_uid) < 1) { return new GsError('Could not find user by "' . GS_LDAP_PROP_USER . '=' . $user . '" in search base "' . GS_LDAP_SEARCHBASE . '" in LDAP.'); } } $dn = GS_LDAP_PROP_UID . '=' . $ldap_uid . ',' . GS_LDAP_SEARCHBASE; $ok = @ldap_mod_add($ldap, $dn, array(GS_EXTERNAL_NUMBERS_LDAP_PROP => $number)); if (!$ok && @ldap_errNo($ldap) != 20) { // err #20 is: "Type or value exists" return new GsError('Failed to add number to LDAP user "' . $dn . '". - ' . gs_get_ldap_error($ldap)); return false; } break; case 'db': default: $ok = $db->execute('REPLACE INTO `users_external_numbers` (`user_id`, `number`) VALUES (' . $user_id . ', \'' . $db->escape($number) . '\')'); if (!$ok) { return new GsError('Failed to add external number.'); } break; } return true; }
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; }
function _gs_legacy_user_map_lvm($user) { global $dispatcher_errors_html; if (!$user) { return false; } if (GS_LDAP_PROP_UID === GS_LDAP_PROP_USER) { return $user; } $ldap = gs_ldap_connect(); $u = gs_ldap_get_first($ldap, GS_LDAP_SEARCHBASE, '(' . GS_LDAP_PROP_UID . '=' . $user . ')', array(GS_LDAP_PROP_USER)); if (isGsError($u)) { //echo $u->$msg; $dispatcher_errors_html[] = sPrintF(htmlEnt(__('Failed to get user "%s" from LDAP server.')), $user); return false; } if (!is_array($u)) { $dispatcher_errors_html[] = sPrintF(htmlEnt(__('User "%s" not found in LDAP database.')), $user); return false; } $lc_GS_LDAP_PROP_USER = strToLower(GS_LDAP_PROP_USER); if (!isset($u[$lc_GS_LDAP_PROP_USER])) { return false; } if (!isset($u[$lc_GS_LDAP_PROP_USER][0])) { return false; } $ret = $u[$lc_GS_LDAP_PROP_USER][0]; //if (gs_get_conf('GS_LVM_USER_6_DIGIT_INT')) { // this check is not really needed as this is a custom function anyway $ret = str_pad($ret, 6, '0', STR_PAD_LEFT); //} return $ret; }
echo __('Could not connect to LDAP server.'); $results = array(); } else { $results = gs_ldap_get_list($ldap, GS_LDAP_SEARCHBASE, '(' . GS_LDAP_PROP_PHONE . '=' . $number_filter . ')', array(GS_LDAP_PROP_PHONE, GS_LDAP_PROP_LASTNAME, GS_LDAP_PROP_FIRSTNAME), $per_page + 1); } $has_more = !isGsError($results) && count($results) > $per_page; if ($has_more) { unset($results[count($results) - 1]); } } else { # search by name $number = ''; $search_url = '&name=' . urlEncode($name); $name_filter = str_replace(array('?'), array('*'), str_replace(array('(', ')', '\\', ""), array('\\28', '\\29', '\\5c', '\\00'), $name)) . '*'; $name_filter = preg_replace('/[*]+/', '*', $name_filter); $ldap = gs_ldap_connect(); if (!$ldap) { echo __('Could not connect to LDAP server.'); $results = array(); } else { $results = gs_ldap_get_list($ldap, GS_LDAP_SEARCHBASE, '(|(' . GS_LDAP_PROP_LASTNAME . '=' . $name_filter . ')' . '(' . GS_LDAP_PROP_FIRSTNAME . '=' . $name_filter . '))', array(GS_LDAP_PROP_LASTNAME, GS_LDAP_PROP_FIRSTNAME, GS_LDAP_PROP_PHONE), $per_page + 1); } $has_more = !isGsError($results) && count($results) > $per_page; if ($has_more) { unset($results[count($results) - 1]); } } ?> <table cellspacing="1" class="phonebook"> <thead>