array('regex' => $reg, 'original' => $orig))
* $reg is the regular expression to use, $orig the user's unmodified input string
*/
function listBuildFilter($post, $attributes) {
$filter = array();
for ($i = 0; $i < sizeof($attributes); $i++) {
if (eregi('^([0-9a-z_\\*\\$])+$', $_POST["filter" . strtolower($attributes[$i])])) {
$filter[$attributes[$i]]['original'] = $_POST["filter" . strtolower($attributes[$i])];
$filter[$attributes[$i]]['regex'] = $_POST["filter" . strtolower($attributes[$i])];
// replace special characters
$filter[$attributes[$i]]['regex'] = str_replace("*", "(.)*", $filter[$attributes[$i]]['regex']);
$filter[$attributes[$i]]['regex'] = str_replace('$', '[$]', $filter[$attributes[$i]]['regex']);
// add string begin and end
$filter[$attributes[$i]]['regex'] = "^" . $filter[$attributes[$i]]['regex'] . "$";
}
}
return $filter;
}
/**
* Removes all entries which do not fit to the filter.
*
* @param array $entries list of accounts
* @param array $filter attribute filter
* @return array filtered list of accounts
*/
function listFilterAccounts($entries, $filter) {
$attributes = array_keys($filter);
for ($r = 0; $r < sizeof($entries); $r++) {
for ($a = 0; $a < sizeof($attributes); $a++) {
// check if filter fits
$found = false;
for ($i = 0; $i < sizeof($entries[$r][$attributes[$a]]); $i++) {
if (eregi($filter[$attributes[$a]]['regex'], $entries[$r][$attributes[$a]][$i])) {
$found = true;
break;
}
}
if (!$found) {
// remove account and reindex array
unset($entries[$r]);
$entries = array_values($entries);
$r--;
break;
}
}
}
return $entries;
}
/**
* Sorts an account list by a given attribute
*
* @param string $sort the attribute by which to sort
* @param array $attr_array array of displayed attributes
* @param array $info the account list
* @return array sorted account list
*/
function listSort($sort, $attr_array, $info) {
/**
* Compare function used for usort-method
*
* Rows are sorted with the first attribute entry of the sort column.
* If objects have attributes with multiple values only the first is used for sorting.
*
* @param array $a first row which is compared
* @param array $b second row which is compared
* @return integer 0 if both are equal, 1 if $a is greater, -1 if $b is greater
*/
function cmp_array($a, $b) {
// sort specifies the sort column
global $sort;
global $attr_array;
// sort by first column if no attribute is given
if (!$sort) $sort = strtolower($attr_array[0]);
if ($sort != "dn") {
// sort by first attribute with name $sort
if ($a[$sort][0] == $b[$sort][0]) return 0;
elseif ($a[$sort][0] == max($a[$sort][0], $b[$sort][0])) return 1;
else return -1;
}
else {
if ($a[$sort] == $b[$sort]) return 0;
elseif ($a[$sort] == max($a[$sort], $b[$sort])) return 1;
else return -1;
}
}
if (!is_array($attr_array)) return $info;
if (!is_string($sort)) return $info;
// sort and return account list
usort($info, "cmp_array");
return $info;
}
/**
* Draws a navigation bar to switch between pages
*
* @param integer $count number of account entries
* @param integer $max_page_entries maximum number of account per page
* @param integer $page current page number
* @param string $sort sort attribute
* @param string $searchFilter LDAP search filter
* @param string $scope account type (user/group/host/domain)
* @param string $text string including the number of accounts
*/
function listDrawNavigationBar($count, $max_page_entries, $page, $sort, $searchFilter, $scope, $text) {
echo("