phpldapadmin/htdocs/search.php

397 lines
13 KiB
PHP
Raw Normal View History

2009-06-30 09:22:30 +00:00
<?php
2009-06-30 10:46:00 +00:00
// $Header: /cvsroot/phpldapadmin/phpldapadmin/htdocs/search.php,v 1.78 2007/12/15 07:50:30 wurley Exp $
2009-06-30 08:05:37 +00:00
2009-06-30 09:29:51 +00:00
/**
2009-06-30 08:05:37 +00:00
* Perform LDAP searches and draw the advanced/simple search forms
*
* Variables that come in as GET vars:
* - 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 09:29:51 +00:00
*
* @package phpLDAPadmin
* @todo Search is probably broken, since base_dn is now an array
2009-06-30 10:46:00 +00:00
* @todo Order doesnt Order properly - probably case related.
2009-06-30 09:29:51 +00:00
*/
/**
2009-06-30 08:05:37 +00:00
*/
2009-06-30 10:26:08 +00:00
define('SIZE_LIMIT_EXCEEDED',4);
2009-06-30 08:09:20 +00:00
2009-06-30 10:26:08 +00:00
# Output format, table or list?
$result_formats = array('list','table');
2009-06-30 09:22:30 +00:00
2009-06-30 10:46:00 +00:00
# Our incoming variables
$entry['format'] = get_request('format','GET','false',$_SESSION['plaConfig']->GetValue('search','display'));
$entry['form'] = get_request('form','GET',false,get_request('form','SESSION'));
$entry['orderby']['raw'] = get_request('orderby','GET');
$entry['orderby']['string'] = rawurldecode($entry['orderby']['raw']);
$entry['orderby']['array'] = $entry['orderby']['raw'] ? explode(',',preg_replace('/\s+/','',$entry['orderby']['string'])) : true;
$entry['display']['raw'] = get_request('display_attrs','GET');
$entry['display']['string'] = rawurldecode($entry['display']['raw']);
$entry['display']['array'] = $entry['display']['raw'] ? explode(',',preg_replace('/\s+/','',$entry['display']['string'])) : array();
$entry['filter']['string'] = get_request('filter','GET');
$entry['filter']['clean'] = $entry['filter']['string'] ? clean_search_vals($entry['filter']['string']) : null;
$entry['attr'] = get_request('attribute','GET');
$entry['base_dn']['string'] = get_request('base_dn','GET');
$entry['criterion'] = get_request('criterion','GET');
$entry['scope'] = get_request('scope','GET','false','sub');
$entry['search'] = get_request('search','GET');
$entry['predefined'] = get_request('predefined','GET');
$entry['page'] = get_request('page','GET',false,0);
if (! in_array($entry['format'],$result_formats))
$entry['format'] = 'list';
$_SESSION['form'] = $entry['form'];
2009-06-30 08:05:37 +00:00
2009-06-30 10:26:08 +00:00
# build the server drop-down html and JavaScript array (for base_dns)
2009-06-30 09:29:51 +00:00
$js_on_change_string = '';
2009-06-30 10:46:00 +00:00
if ($entry['form'] == 'advanced')
$js_on_change_string = 'onChange="document.forms[0].base_dn.value=servers[document.forms[0].server_id.value].base_dn"';
2009-06-30 09:29:51 +00:00
if (isset($ldapserver)) {
2009-06-30 10:26:08 +00:00
$server_menu_html = server_select_list($ldapserver->server_id,true,'server_id',$js_on_change_string);
$server_info_list = server_info_list();
2009-06-30 08:05:37 +00:00
}
2009-06-30 08:07:14 +00:00
2009-06-30 10:26:08 +00:00
# grab the base dn for the search
2009-06-30 10:46:00 +00:00
if ($entry['base_dn']['string']) {
$entry['base_dn']['invalid'] = false;
$entry['base_dn']['exist'] = false;
2009-06-30 09:29:51 +00:00
2009-06-30 10:46:00 +00:00
if (! is_dn_string($entry['base_dn']['string']))
$entry['base_dn']['invalid'] = true;
2009-06-30 09:29:51 +00:00
2009-06-30 10:46:00 +00:00
elseif (! $ldapserver->dnExists($entry['base_dn']['string']))
$entry['base_dn']['exist'] = true;
2009-06-30 09:29:51 +00:00
2009-06-30 10:46:00 +00:00
$base_dns = array($entry['base_dn']['string']);
2009-06-30 10:26:08 +00:00
2009-06-30 09:29:51 +00:00
} else
if (isset($ldapserver))
$base_dns = $ldapserver->getBaseDN();
2009-06-30 10:46:00 +00:00
printf('<script type="text/javascript" src="%ssearch_util.js"></script>','../htdocs/'.JSDIR);
echo '<center>';
2009-06-30 10:26:08 +00:00
2009-06-30 10:46:00 +00:00
$entry['command']['as'] = $_SESSION['plaConfig']->isCommandAvailable('search','advanced_search');
$entry['command']['ps'] = $_SESSION['plaConfig']->isCommandAvailable('search','predefined_search');
$entry['command']['ss'] = $_SESSION['plaConfig']->isCommandAvailable('search','simple_search');
2009-06-30 08:09:20 +00:00
2009-06-30 10:46:00 +00:00
if ($entry['form'] == 'advanced') {
if ($entry['command']['as'])
require LIBDIR.'search_form_advanced.php';
2009-06-30 08:05:37 +00:00
2009-06-30 10:46:00 +00:00
} elseif ($entry['form'] == 'predefined') {
if ($entry['command']['ps'])
require LIBDIR.'search_form_predefined.php';
2009-06-30 08:05:37 +00:00
2009-06-30 10:46:00 +00:00
} elseif ($entry['form'] == 'simple') {
# Draw simple search form
if ($entry['command']['ss'])
require LIBDIR.'search_form_simple.php';
} elseif ($entry['command']['as']) {
2009-06-30 09:29:51 +00:00
require LIBDIR.'search_form_advanced.php';
2009-06-30 08:05:37 +00:00
2009-06-30 10:46:00 +00:00
} elseif ($entry['command']['ss']) {
require LIBDIR.'search_form_simple.php';
} elseif ($entry['command']['ps']) {
2009-06-30 09:29:51 +00:00
require LIBDIR.'search_form_predefined.php';
2009-06-30 09:22:30 +00:00
2009-06-30 10:46:00 +00:00
} else {
echo _('Searching is not permitted');
}
2009-06-30 08:05:37 +00:00
2009-06-30 10:26:08 +00:00
echo '</center>';
2009-06-30 10:46:00 +00:00
echo '<br />';
if ($entry['search']) {
if ($entry['form'] == 'advanced') {
if (! $_SESSION['plaConfig']->isCommandAvailable('search','advanced_search'))
pla_error(sprintf('%s%s %s',_('This operation is not permitted by the configuration'),_(':'),_('advanced search')));
} elseif ($entry['form'] == 'predefined') {
if (! $_SESSION['plaConfig']->isCommandAvailable('search','predefined_search'))
pla_error(sprintf('%s%s %s',_('This operation is not permitted by the configuration'),_(':'),_('predefined search')));
2009-06-30 08:05:37 +00:00
2009-06-30 10:46:00 +00:00
} elseif ($entry['form'] == 'simple') {
if (! $_SESSION['plaConfig']->isCommandAvailable('search','simple_search'))
pla_error(sprintf('%s%s %s',_('This operation is not permitted by the configuration'),_(':'),_('simple search')));
}
2009-06-30 09:22:30 +00:00
2009-06-30 10:46:00 +00:00
if ($entry['form'] == 'advanced') {
if ($entry['display'])
$search_result_attributes = $entry['display']['array'];
2009-06-30 10:26:08 +00:00
else
2009-06-30 10:46:00 +00:00
$search_result_attributes = $_SESSION['plaConfig']->GetValue('search','result_attributes');
2009-06-30 09:29:51 +00:00
} else {
2009-06-30 10:46:00 +00:00
$search_result_attributes = $_SESSION['plaConfig']->GetValue('search','result_attributes');
2009-06-30 09:29:51 +00:00
}
2009-06-30 08:05:37 +00:00
2009-06-30 10:26:08 +00:00
# do we have enough authentication information for the specified server_id
2009-06-30 10:46:00 +00:00
# @todo: redirect isnt working
2009-06-30 10:26:08 +00:00
if (! $ldapserver->haveAuthInfo()) {
2009-06-30 10:46:00 +00:00
$login_url = sprintf('cmd.php?cmd=login_form&amp;server_id=%s&amp;redirect=%s',
2009-06-30 10:26:08 +00:00
$ldapserver->server_id,rawurlencode($_SERVER['REQUEST_URI']));
2009-06-30 10:46:00 +00:00
system_message(array(
'title'=>_('Search'),
'body'=>_('You have not logged into the selected server yet, so you cannot perform searches on it.'),
'type'=>'warn'),
'cmd.php?cmd=login_form');
2009-06-30 08:05:37 +00:00
}
2009-06-30 10:46:00 +00:00
if (is_numeric($entry['predefined'])) {
$query = get_cleaned_up_predefined_search($entry['predefined']);
2009-06-30 09:22:30 +00:00
2009-06-30 10:46:00 +00:00
$search_result_attributes = explode(', ',$query['attributes']);
$search_attributes_display = $search_result_attributes;
$search_attributes = $search_result_attributes;
2009-06-30 09:22:30 +00:00
2009-06-30 10:46:00 +00:00
$entry['filter']['clean'] = $query['filter'];
$entry['scope'] = $query['scope'];
2009-06-30 10:26:08 +00:00
2009-06-30 10:46:00 +00:00
if (! trim($query['base']))
$query['base'] = $ldapserver->getBaseDN();
2009-06-30 10:26:08 +00:00
2009-06-30 10:46:00 +00:00
elseif (is_array($query['base']))
$base_dns = $query['base'];
2009-06-30 10:26:08 +00:00
2009-06-30 10:46:00 +00:00
else
$base_dns = array($query['base']);
2009-06-30 09:29:51 +00:00
} else {
2009-06-30 10:46:00 +00:00
$entry['predefined'] = '';
2009-06-30 09:22:30 +00:00
}
2009-06-30 09:29:51 +00:00
2009-06-30 10:46:00 +00:00
if ($entry['filter']['clean']) {
2009-06-30 10:26:08 +00:00
# if they are using the simple search form, build an LDAP search filter from their input
2009-06-30 10:46:00 +00:00
if ($entry['form'] == 'simple' & ! is_numeric($entry['predefined'])) {
2009-06-30 09:29:51 +00:00
2009-06-30 10:46:00 +00:00
switch ($entry['criterion']) {
2009-06-30 08:05:37 +00:00
case 'starts with':
2009-06-30 10:46:00 +00:00
if ($entry['filter']['clean'] == '*')
$entry['filter']['clean'] = '';
2009-06-30 09:29:51 +00:00
2009-06-30 10:46:00 +00:00
$entry['filter']['clean'] = sprintf('(%s=%s*)',$entry['attr'],$entry['filter']['clean']);
2009-06-30 08:05:37 +00:00
break;
2009-06-30 09:29:51 +00:00
2009-06-30 08:05:37 +00:00
case 'contains':
2009-06-30 10:46:00 +00:00
if ($entry['filter']['clean'] == '*')
$entry['filter']['clean'] = sprintf('(%s=*)',$entry['attr']);
2009-06-30 08:07:14 +00:00
else
2009-06-30 10:46:00 +00:00
$entry['filter']['clean'] = sprintf('(%s=*%s*)',$entry['attr'],$entry['filter']['clean']);
2009-06-30 09:29:51 +00:00
2009-06-30 08:05:37 +00:00
break;
2009-06-30 09:29:51 +00:00
2009-06-30 08:05:37 +00:00
case 'ends with':
2009-06-30 10:46:00 +00:00
if ($entry['filter']['clean'] == '*')
$entry['filter']['clean'] = '';
2009-06-30 09:29:51 +00:00
2009-06-30 10:46:00 +00:00
$entry['filter']['clean'] = sprintf('(%s=*%s)',$entry['attr'],$entry['filter']['clean']);
2009-06-30 08:05:37 +00:00
break;
2009-06-30 09:29:51 +00:00
2009-06-30 08:05:37 +00:00
case 'equals':
2009-06-30 10:46:00 +00:00
$entry['filter']['clean'] = sprintf('(%s=%s)',$entry['attr'],$entry['filter']['clean']);
2009-06-30 08:05:37 +00:00
break;
2009-06-30 09:29:51 +00:00
2009-06-30 08:05:37 +00:00
case 'sounds like':
2009-06-30 10:46:00 +00:00
$entry['filter']['clean'] = sprintf('(%s~=%s)',$entry['attr'],$entry['filter']['clean']);
2009-06-30 08:05:37 +00:00
break;
2009-06-30 09:29:51 +00:00
2009-06-30 08:05:37 +00:00
default:
2009-06-30 10:46:00 +00:00
system_message(array(
'title'=>_('Unrecognized criteria option: ').htmlspecialchars($entry['criterion']),
'body'=>_('If you want to add your own criteria to the list. Be sure to edit search.php to handle them. Quitting.'),
'type'=>'warn'),
'cmd.php?cmd=search');
2009-06-30 08:05:37 +00:00
}
}
2009-06-30 09:29:51 +00:00
2009-06-30 10:26:08 +00:00
# prevent script from bailing early on a long delete
@set_time_limit(0);
2009-06-30 08:09:20 +00:00
2009-06-30 10:46:00 +00:00
$size_limit = $_SESSION['plaConfig']->GetValue('search','size_limit');
2009-06-30 10:26:08 +00:00
# Sanity check
if ($size_limit < 1)
2009-06-30 09:29:51 +00:00
$size_limit = 1;
2009-06-30 09:22:30 +00:00
2009-06-30 08:05:37 +00:00
$time_start = utime();
2009-06-30 09:29:51 +00:00
$time_elapsed = 0;
foreach ($base_dns as $base_dn) {
2009-06-30 10:26:08 +00:00
if (! $ldapserver->dnExists($base_dn)) {
if (DEBUG_ENABLED)
debug_log('BaseDN [%s] skipped as it doesnt exist in [%s].',64,
2009-06-30 09:40:37 +00:00
$base_dn,$ldapserver->server_id);
2009-06-30 09:29:51 +00:00
continue;
2009-06-30 10:26:08 +00:00
2009-06-30 09:29:51 +00:00
} else {
2009-06-30 10:26:08 +00:00
if (DEBUG_ENABLED)
debug_log('Search with base DN [%s]',64,$base_dn);
2009-06-30 09:29:51 +00:00
}
2009-06-30 10:46:00 +00:00
$results = $ldapserver->search(null,dn_escape($base_dn),$entry['filter']['clean'],$search_result_attributes,
$entry['scope'],$entry['orderby']['array'],$_SESSION['plaConfig']->GetValue('deref','search'));
2009-06-30 09:29:51 +00:00
2009-06-30 10:26:08 +00:00
if ((! $results) && $ldapserver->errno())
pla_error(_('Encountered an error while performing search.'),$ldapserver->error(),$ldapserver->errno());
2009-06-30 09:29:51 +00:00
2009-06-30 10:26:08 +00:00
$errno = $ldapserver->errno();
2009-06-30 09:29:51 +00:00
2009-06-30 10:26:08 +00:00
$time_end = utime();
$time_elapsed += round($time_end-$time_start,2);
$count = count($results);
2009-06-30 09:29:51 +00:00
2009-06-30 10:46:00 +00:00
$start_entry = $entry['page'] * $size_limit;
2009-06-30 10:26:08 +00:00
$end_entry = min($start_entry+$size_limit+1,$count+1);
2009-06-30 08:05:37 +00:00
2009-06-30 10:46:00 +00:00
# Search Results Table
echo '<table class="search_result" border=0>';
2009-06-30 09:22:30 +00:00
2009-06-30 10:46:00 +00:00
echo '<tr class="header">';
printf('<td>%s%s <b>%s</b> <small>(%s %s)</small></td>',_('Entries found'),_(':'),
number_format($count),$time_elapsed,_('seconds'));
2009-06-30 09:29:51 +00:00
2009-06-30 10:46:00 +00:00
if ($_SESSION['plaConfig']->isCommandAvailable('export')) {
$href = htmlspecialchars(sprintf('cmd.php?cmd=export_form&server_id=%s&scope=%s&dn=%s&filter=%s&attributes=%s',
$ldapserver->server_id,$entry['scope'],$base_dn,rawurlencode($entry['filter']['clean']),rawurlencode(join(', ',$search_result_attributes))));
printf('<td style="text-align: right"><small>[ <a href="%s"><img src="images/save.png" alt="Save" /> %s</a> ]',
$href,_('export results'));
}
printf('[ <img src="images/rename.png" alt="rename" /> %s%s',_('Format'),_(':'));
2009-06-30 09:29:51 +00:00
2009-06-30 10:26:08 +00:00
foreach ($result_formats as $f) {
echo '&nbsp;';
2009-06-30 09:29:51 +00:00
2009-06-30 10:46:00 +00:00
if ($entry['format'] == $f) {
2009-06-30 10:26:08 +00:00
printf('<b>%s</b>',_($f));
2009-06-30 09:29:51 +00:00
2009-06-30 10:26:08 +00:00
} else {
2009-06-30 10:46:00 +00:00
$query_string = array_to_query_string($_GET,array('format','cmd'));
$query_string .= sprintf('&amp;format=%s',$f);
printf('<a href="cmd.php?cmd=search&amp;%s">%s</a>',$query_string,_($f));
2009-06-30 10:26:08 +00:00
}
2009-06-30 09:29:51 +00:00
}
2009-06-30 10:46:00 +00:00
echo ' ]</small>';
2009-06-30 10:26:08 +00:00
2009-06-30 10:46:00 +00:00
if ($_SESSION['plaConfig']->isCommandAvailable('schema')) {
echo '<br />';
printf('<small>%s%s <b>%s</b></small>',_('Base DN'),_(':'),htmlspecialchars($base_dn));
if ($entry['form'] == 'simple' || $entry['form'] == 'predefined') {
echo '<br />';
printf('<small>%s%s <b>%s</b></small>',_('Filter performed'),_(':'),htmlspecialchars($entry['filter']['clean']));
}
}
2009-06-30 09:22:30 +00:00
2009-06-30 10:46:00 +00:00
echo '</td>';
echo '</tr>';
echo '</table>';
2009-06-30 09:22:30 +00:00
2009-06-30 10:26:08 +00:00
# The LDAP error code for the size limit exceeded error.
2009-06-30 10:46:00 +00:00
# @todo: test this with system_message
2009-06-30 10:26:08 +00:00
if ($errno && $errno == SIZE_LIMIT_EXCEEDED)
printf('<br /><center><small style="color:red; white-space: nowrap">%s</small></center><br />',_('Notice, search size limit exceeded.'));
2009-06-30 09:22:30 +00:00
2009-06-30 10:26:08 +00:00
# Draw the paging links
$pager_html = '';
$total_pages = $count / $size_limit;
$results_per_page = $size_limit;
2009-06-30 09:29:51 +00:00
2009-06-30 10:26:08 +00:00
if ($count > $size_limit) {
printf(_('Showing results %s through %s.').'<br />',
'<b>'.number_format($start_entry+1).'</b>','<b>'.number_format($end_entry-1).'</b>');
2009-06-30 09:29:51 +00:00
2009-06-30 10:46:00 +00:00
if ($entry['page'] != 0) {
$query_string = array_to_query_string($_GET,array('page','cmd'));
$query_string .= '&page=' . ($entry['page']-1);
$pager_html .= sprintf('<a title="'._('Page %d').'" href="cmd.php?cmd=search&amp;%s">&lsaquo;&lsaquo;</a>',$entry['page'],$query_string);
2009-06-30 09:29:51 +00:00
2009-06-30 10:26:08 +00:00
} else {
2009-06-30 10:46:00 +00:00
$pager_html .= '&lsaquo;&lsaquo;';
2009-06-30 10:26:08 +00:00
}
$pager_html .= '&nbsp;&nbsp;';
2009-06-30 09:29:51 +00:00
2009-06-30 10:46:00 +00:00
/* For large search results where we page beyond the first 20 pages,
* print elipsis instead of making the pager be super wide. */
2009-06-30 10:26:08 +00:00
$elipsis_printed = false;
for ($i=0;$i<$count;$i+=$size_limit) {
$page_num = $i/$size_limit;
2009-06-30 09:22:30 +00:00
2009-06-30 10:46:00 +00:00
if ($count > $size_limit * 20 && abs($page_num-$entry['page']) > 10) {
2009-06-30 09:29:51 +00:00
2009-06-30 10:26:08 +00:00
if (! $elipsis_printed) {
$pager_html .= '...&nbsp;&nbsp;';
$elipsis_printed = true;
}
2009-06-30 09:29:51 +00:00
2009-06-30 10:46:00 +00:00
} elseif ($entry['page'] == $page_num) {
2009-06-30 10:26:08 +00:00
$pager_html .= '<b>' . ($page_num + 1) . '</b>';
$pager_html .= '&nbsp;&nbsp;';
$elipsis_printed = false;
} else {
2009-06-30 10:46:00 +00:00
$query_string = array_to_query_string($_GET,array('page','cmd'));
2009-06-30 10:26:08 +00:00
$query_string .= '&page=' . $page_num;
2009-06-30 10:46:00 +00:00
$pager_html .= "<a href=\"cmd.php?cmd=search&amp;$query_string\">" . ($page_num+1) . "</a>";
2009-06-30 10:26:08 +00:00
$pager_html .= '&nbsp;&nbsp;';
$elipsis_printed = false;
2009-06-30 09:29:51 +00:00
}
2009-06-30 10:26:08 +00:00
}
2009-06-30 09:29:51 +00:00
2009-06-30 10:46:00 +00:00
if ($entry['page']+1 < $total_pages) {
$query_string = array_to_query_string($_GET,array('page','cmd'));
$query_string .= '&page=' . ($entry['page']+1);
$pager_html .= "<a title=\"" . sprintf(_('Page %d'),($entry['page']+2))."\" href=\"cmd.php?cmd=search&amp;$query_string\">&rsaquo;&rsaquo;</a>";
2009-06-30 09:29:51 +00:00
2009-06-30 09:22:30 +00:00
} else {
2009-06-30 10:26:08 +00:00
$pager_html .= "&rsaquo;&rsaquo;";
2009-06-30 08:09:20 +00:00
}
2009-06-30 09:29:51 +00:00
2009-06-30 09:22:30 +00:00
}
2009-06-30 08:09:20 +00:00
2009-06-30 10:26:08 +00:00
if (0 == $count)
printf('<center><h2>%s</h2></center>',_('The search found no results.'));
else {
if (trim($pager_html))
printf('<center>%s</center>',$pager_html);
echo '<br />';
2009-06-30 10:46:00 +00:00
if ($entry['format'] == 'list')
2009-06-30 10:26:08 +00:00
require LIBDIR.'search_results_list.php';
2009-06-30 10:46:00 +00:00
elseif ($entry['format'] == 'table')
2009-06-30 10:26:08 +00:00
require LIBDIR.'search_results_table.php';
else
2009-06-30 10:46:00 +00:00
pla_error(sprintf(_('Unrecognized search result format: %s'),htmlspecialchars($entry['format'])));
2009-06-30 10:26:08 +00:00
echo '<br />';
if (trim($pager_html))
printf('<center>%s</center>',$pager_html);
}
2009-06-30 09:29:51 +00:00
}
2009-06-30 08:09:20 +00:00
2009-06-30 10:26:08 +00:00
printf('<br /><br /><div class="search_result"><center><small><span style="font-weight:normal;font-size:75%%;">%s <b>%s</b> %s.</span></small></center></div>',
_('Search performed by phpLDAPadmin in'),$time_elapsed,_('seconds'));
2009-06-30 08:05:37 +00:00
2009-06-30 10:26:08 +00:00
}
}
?>