2009-06-30 08:05:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* search.php
|
|
|
|
* Perform LDAP searches and draw the advanced/simple search forms
|
|
|
|
*
|
|
|
|
* Variables that come in as GET vars:
|
|
|
|
* - server_id
|
|
|
|
* - search (true if performing a search, empty to just draw form)
|
|
|
|
* For simple searches:
|
|
|
|
* - attribute, criterion, filter
|
|
|
|
* For advanced searches:
|
|
|
|
* - base_dn, scope, filter
|
|
|
|
*/
|
|
|
|
|
2009-06-30 08:07:14 +00:00
|
|
|
require 'common.php';
|
2009-06-30 08:05:37 +00:00
|
|
|
|
|
|
|
// try to get an available server_id if one is not provided
|
2009-06-30 08:09:20 +00:00
|
|
|
if( ! isset( $_GET['server_id'] ) )
|
2009-06-30 08:05:37 +00:00
|
|
|
$server_id = get_avail_server_id();
|
2009-06-30 08:09:20 +00:00
|
|
|
else {
|
|
|
|
$server_id = $_GET['server_id'];
|
|
|
|
check_server_id( $server_id ) or pla_error( $lang['bad_server_id'] );
|
2009-06-30 08:05:37 +00:00
|
|
|
}
|
2009-06-30 08:09:20 +00:00
|
|
|
|
2009-06-30 08:07:14 +00:00
|
|
|
$js_on_change_string ='';
|
2009-06-30 08:09:20 +00:00
|
|
|
if( isset( $_GET['form'] ) && $_GET['form'] == 'advanced' )
|
|
|
|
$js_on_change_string =
|
|
|
|
'onChange="document.forms[0].base_dn.value=servers[document.forms[0].server_id.value].getBaseDn()"';
|
2009-06-30 08:05:37 +00:00
|
|
|
|
|
|
|
// build the server drop-down html and JavaScript array (for base_dns)
|
2009-06-30 08:07:14 +00:00
|
|
|
$server_menu_html = '<select name="server_id" '.$js_on_change_string.'>';
|
|
|
|
|
|
|
|
$server_info_list =array();
|
|
|
|
|
2009-06-30 08:05:37 +00:00
|
|
|
foreach( $servers as $id => $server ) {
|
|
|
|
$base_dn = $server['base'] ? $server['base'] : try_to_get_root_dn( $id );
|
2009-06-30 08:07:14 +00:00
|
|
|
$server_info_list[$id]['id'] = $id;
|
|
|
|
$server_info_list[$id]['name'] = $server['name'];
|
|
|
|
$server_info_list[$id]['base_dn'] = $base_dn;
|
|
|
|
|
2009-06-30 08:05:37 +00:00
|
|
|
if( $server['host'] ) {
|
|
|
|
$server_menu_html .= '<option value="'.$id.'"' . ( $id==$server_id? ' selected' : '' ) . '>';
|
|
|
|
$server_menu_html .= $server['name'] . '</option>';
|
|
|
|
}
|
|
|
|
}
|
2009-06-30 08:07:14 +00:00
|
|
|
|
2009-06-30 08:05:37 +00:00
|
|
|
$server_menu_html .= '</select>';
|
|
|
|
|
2009-06-30 08:09:20 +00:00
|
|
|
$filter = isset( $_GET['filter'] ) ? $_GET['filter'] : null;
|
|
|
|
$filter = $filter;
|
|
|
|
$attr = isset( $_GET['attribute'] ) ? $_GET['attribute'] : null;
|
2009-06-30 08:05:37 +00:00
|
|
|
|
|
|
|
// grab the base dn for the search
|
|
|
|
if( isset( $_GET['base_dn'] ) )
|
|
|
|
$base_dn = $_GET['base_dn'];
|
|
|
|
elseif( '' != $servers[$server_id]['base'] )
|
|
|
|
$base_dn = $servers[$server_id]['base'];
|
|
|
|
else
|
|
|
|
$base_dn = try_to_get_root_dn( $server_id );
|
|
|
|
|
2009-06-30 08:09:20 +00:00
|
|
|
$criterion = isset( $_GET['criterion'] ) ? $_GET['criterion'] : null;
|
|
|
|
$form = isset( $_GET['form'] ) ? $_GET['form'] : null;
|
|
|
|
$scope = isset( $_GET['scope'] ) ? $_GET['scope'] : 'sub';
|
|
|
|
|
|
|
|
include 'header.php'; ?>
|
2009-06-30 08:05:37 +00:00
|
|
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<center>
|
|
|
|
|
|
|
|
<?php if( $form == 'advanced' ) {
|
|
|
|
|
|
|
|
include 'search_form_advanced.php';
|
|
|
|
|
|
|
|
} else /* Draw simple search form */ {
|
|
|
|
|
|
|
|
process_config();
|
|
|
|
include 'search_form_simple.php';
|
|
|
|
|
|
|
|
} ?>
|
|
|
|
|
|
|
|
</center>
|
|
|
|
|
|
|
|
<?php flush(); ?>
|
|
|
|
|
|
|
|
<?php
|
|
|
|
|
2009-06-30 08:09:20 +00:00
|
|
|
if( isset( $_GET['search'] ) )
|
2009-06-30 08:05:37 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if( $form == 'advanced' ) {
|
|
|
|
$search_result_attributes = isset( $_GET['display_attrs'] ) ?
|
2009-06-30 08:09:20 +00:00
|
|
|
rawurldecode( $_GET['display_attrs'] ) :
|
|
|
|
( isset( $search_result_attributes ) ?
|
|
|
|
$search_result_attributes :
|
|
|
|
"dn, cn" );
|
2009-06-30 08:05:37 +00:00
|
|
|
process_config();
|
|
|
|
}
|
|
|
|
|
|
|
|
// do we have enough authentication information for the specified server_id
|
|
|
|
if( ! have_auth_info( $server_id ) )
|
|
|
|
{
|
|
|
|
$login_url = "login_form.php?server_id=$server_id&redirect=" . rawurlencode( $_SERVER['REQUEST_URI'] );
|
|
|
|
?>
|
|
|
|
<center>
|
|
|
|
<br />
|
2009-06-30 08:09:20 +00:00
|
|
|
<?php echo $lang['you_have_not_logged_into_server']; ?><br />
|
|
|
|
<a href="<?php echo $login_url; ?>"><?php echo $lang['click_to_go_to_login_form']; ?></a>.
|
2009-06-30 08:05:37 +00:00
|
|
|
</center>
|
|
|
|
<?php
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2009-06-30 08:09:20 +00:00
|
|
|
$ds = pla_ldap_connect( $server_id );
|
|
|
|
if( ! $ds )
|
|
|
|
pla_error( $lang['could_not_connect'] );
|
2009-06-30 08:05:37 +00:00
|
|
|
|
|
|
|
if( $filter )
|
|
|
|
{
|
|
|
|
|
|
|
|
// if they are using the simple search form, build an LDAP search filter from their input
|
|
|
|
if( $form == 'simple' )
|
|
|
|
{
|
|
|
|
switch( $criterion ) {
|
|
|
|
case 'starts with':
|
2009-06-30 08:07:14 +00:00
|
|
|
// to fix bug 789113
|
|
|
|
if( $filter == "*" )
|
|
|
|
$filter = "";
|
2009-06-30 08:05:37 +00:00
|
|
|
$filter = "($attr=$filter*)";
|
|
|
|
break;
|
|
|
|
case 'contains':
|
2009-06-30 08:07:14 +00:00
|
|
|
// to fix bug 789113
|
|
|
|
if( $filter == "*" )
|
|
|
|
$filter = "($attr=*)";
|
|
|
|
else
|
|
|
|
$filter = "($attr=*$filter*)";
|
2009-06-30 08:05:37 +00:00
|
|
|
break;
|
|
|
|
case 'ends with':
|
2009-06-30 08:07:14 +00:00
|
|
|
// to fix bug 789113
|
|
|
|
if( $filter == "*" )
|
|
|
|
$filter = "";
|
2009-06-30 08:05:37 +00:00
|
|
|
$filter = "($attr=*$filter)";
|
|
|
|
break;
|
|
|
|
case 'equals':
|
|
|
|
$filter = "($attr=$filter)";
|
|
|
|
break;
|
|
|
|
case 'sounds like':
|
|
|
|
$filter = "($attr~=$filter)";
|
|
|
|
break;
|
|
|
|
default:
|
2009-06-30 08:09:20 +00:00
|
|
|
pla_error( $lang['unrecognized_criteria_option'] .
|
|
|
|
htmlspecialchars( $criterion ) .
|
|
|
|
$lang['if_you_want_to_add_criteria'] );
|
2009-06-30 08:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-30 08:09:20 +00:00
|
|
|
echo "<center>Searching...</center>\n";
|
|
|
|
flush();
|
|
|
|
|
|
|
|
// prevent script from bailing early on a long delete
|
|
|
|
@set_time_limit( 0 );
|
|
|
|
|
|
|
|
// grab the time limit set in config.php
|
|
|
|
$size_limit = isset ( $search_result_size_limit ) && is_numeric( $search_result_size_limit ) ?
|
|
|
|
$search_result_size_limit :
|
|
|
|
0;
|
|
|
|
|
2009-06-30 08:05:37 +00:00
|
|
|
$time_start = utime();
|
2009-06-30 08:09:20 +00:00
|
|
|
if( $scope == 'base' )
|
|
|
|
$results = @ldap_read( $ds, $base_dn, $filter, $search_result_attributes,
|
|
|
|
0, 0, 0, LDAP_DEREF_ALWAYS );
|
|
|
|
elseif( $scope == 'one' )
|
|
|
|
$results = @ldap_list( $ds, $base_dn, $filter, $search_result_attributes,
|
|
|
|
0, 0, 0, LDAP_DEREF_ALWAYS );
|
|
|
|
else // scope == 'sub'
|
|
|
|
$results = @ldap_search( $ds, $base_dn, $filter, $search_result_attributes,
|
|
|
|
0, 0, 0, LDAP_DEREF_ALWAYS );
|
|
|
|
|
|
|
|
$errno = @ldap_errno( $ds );
|
2009-06-30 08:05:37 +00:00
|
|
|
$time_end = utime();
|
|
|
|
$time_elapsed = round( $time_end - $time_start, 2 );
|
2009-06-30 08:09:20 +00:00
|
|
|
$count = @ldap_count_entries( $ds, $results );
|
2009-06-30 08:07:14 +00:00
|
|
|
|
2009-06-30 08:05:37 +00:00
|
|
|
?>
|
|
|
|
|
2009-06-30 08:09:20 +00:00
|
|
|
<center>
|
|
|
|
<?php echo $lang['entries_found'] . ' ' . number_format( $count ) ?>
|
|
|
|
(in <?php echo $time_elapsed; ?> seconds).
|
|
|
|
<?php
|
|
|
|
|
|
|
|
// The LDAP error code for the size limit exceeded error.
|
|
|
|
define( 'SIZE_LIMIT_EXCEEDED', 4 );
|
|
|
|
if( $errno && $errno == SIZE_LIMIT_EXCEEDED ) {
|
|
|
|
echo "<br /><small>Notice, search size limit exceeded.</small><br />\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if( $size_limit > 0 && $count > $size_limit ) {
|
|
|
|
echo "<br /><small>Showing first <b>$size_limit</b> results.</small><br />\n";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|
2009-06-30 08:05:37 +00:00
|
|
|
|
|
|
|
<?php if( $form == 'simple' ) { ?>
|
2009-06-30 08:09:20 +00:00
|
|
|
<center><small><?php echo $lang['filter_performed']; ?>
|
|
|
|
<?php echo htmlspecialchars( $filter ); ?></small></center>
|
2009-06-30 08:05:37 +00:00
|
|
|
<?php } ?>
|
|
|
|
|
|
|
|
</center>
|
|
|
|
|
|
|
|
<?php flush(); ?>
|
|
|
|
|
2009-06-30 08:09:20 +00:00
|
|
|
<?php
|
|
|
|
if( ! $results ) {
|
|
|
|
pla_error( 'Encountered an error while performing search.', ldap_error( $ds ), ldap_errno( $ds ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$friendly_attrs = process_friendly_attr_table();
|
|
|
|
$entry_id = ldap_first_entry( $ds, $results );
|
|
|
|
|
|
|
|
// Iterate over each entry
|
|
|
|
$i = 0;
|
|
|
|
while( $entry_id ) {
|
|
|
|
$i++;
|
|
|
|
// Only display the first $size_limit entries
|
|
|
|
if( $size_limit != 0 && $i > $size_limit ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dn = ldap_get_dn( $ds, $entry_id );
|
|
|
|
$encoded_dn = rawurlencode( $dn );
|
|
|
|
$rdn = get_rdn( $dn );
|
|
|
|
?>
|
|
|
|
|
|
|
|
<div class="search_result">
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
|
|
<td><img src="images/<?php echo get_icon_use_cache( $server_id, $dn ); ?>" /></td>
|
|
|
|
<td><a href="edit.php?server_id=<?php
|
|
|
|
echo $server_id; ?>&dn=<?php echo $encoded_dn; ?>"><?php echo htmlspecialchars($rdn); ?></a>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<table class="attrs">
|
|
|
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
$attrs = ldap_get_attributes( $ds, $entry_id );
|
|
|
|
$attr = ldap_first_attribute( $ds, $entry_id, $attrs );
|
|
|
|
|
|
|
|
// Always print out the DN in the attribute list
|
|
|
|
echo "<tr><td class=\"attr\" valign=\"top\">dn</td>";
|
|
|
|
echo "<td>" . htmlspecialchars($dn) . "</td></tr>\n";
|
|
|
|
|
|
|
|
// Iterate over each attribute for this entry
|
|
|
|
while( $attr ) {
|
|
|
|
|
|
|
|
if( is_attr_binary( $server_id, $attr ) )
|
|
|
|
$values = array( "(binary)" );
|
|
|
|
else
|
|
|
|
$values = ldap_get_values( $ds, $entry_id, $attr );
|
|
|
|
if( isset( $values['count'] ) )
|
|
|
|
unset( $values['count'] );
|
|
|
|
|
|
|
|
if( isset( $friendly_attrs[ strtolower( $attr ) ] ) )
|
|
|
|
$attr = "<acronym title=\"Alias for $attr\">" .
|
|
|
|
htmlspecialchars( $friendly_attrs[ strtolower($attr) ] ) .
|
|
|
|
"</acronym>";
|
|
|
|
else
|
|
|
|
$attr = htmlspecialchars( $attr );
|
|
|
|
?>
|
2009-06-30 08:05:37 +00:00
|
|
|
|
|
|
|
<tr>
|
2009-06-30 08:09:20 +00:00
|
|
|
<td class="attr" valign="top"><?php echo $attr; ?></td>
|
2009-06-30 08:05:37 +00:00
|
|
|
<td class="val">
|
2009-06-30 08:09:20 +00:00
|
|
|
<?php
|
|
|
|
foreach( $values as $value )
|
|
|
|
echo str_replace( ' ', ' ',
|
|
|
|
htmlspecialchars( $value ) ) . "<br />\n";
|
|
|
|
?>
|
2009-06-30 08:05:37 +00:00
|
|
|
</td>
|
|
|
|
</tr>
|
2009-06-30 08:09:20 +00:00
|
|
|
<?php
|
|
|
|
$attr = ldap_next_attribute( $ds, $entry_id, $attrs );
|
|
|
|
} // end while( $attr )
|
|
|
|
|
|
|
|
?>
|
2009-06-30 08:05:37 +00:00
|
|
|
|
|
|
|
</table>
|
|
|
|
|
2009-06-30 08:09:20 +00:00
|
|
|
<?php
|
2009-06-30 08:05:37 +00:00
|
|
|
|
2009-06-30 08:09:20 +00:00
|
|
|
$entry_id = ldap_next_entry( $ds, $entry_id );
|
|
|
|
|
|
|
|
// flush every 5th entry (sppeds things up a bit)
|
|
|
|
if( 0 == $i % 5 )
|
|
|
|
flush();
|
|
|
|
|
|
|
|
} // end while( $entry_id )
|
|
|
|
|
|
|
|
?>
|
|
|
|
|
|
|
|
<br /><br />
|
|
|
|
<div class="search_result"><center><span style="font-weight:normal;font-size:75%;">
|
|
|
|
<?php echo $lang['search_duration']; ?>
|
|
|
|
<b><?php echo $time_elapsed; ?></b> <?php echo $lang['seconds'];?>.</small></center></div>
|
2009-06-30 08:05:37 +00:00
|
|
|
<?php
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
|