phpldapadmin/lib/QueryRender.php

605 lines
18 KiB
PHP
Raw Normal View History

<?php
// $Header$
/**
* @author The phpLDAPadmin development team
* @package phpLDAPadmin
*/
/**
* QueryRender class
*
* @package phpLDAPadmin
* @subpackage Templates
* @todo need to add paging
*/
class QueryRender extends PageRender {
/** CORE FUNCTIONS **/
/**
* Intialise and Render the QueryRender
*/
public function accept() {
2009-08-19 03:39:37 +00:00
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',129,0,__FILE__,__LINE__,__METHOD__,$fargs);
if (DEBUGTMP) printf('<font size=-2>%s</font><br />',__METHOD__);
if (DEBUGTMP||DEBUGTMPSUB) printf('<font size=-2>* %s [GETquery:%s]</font><br />',__METHOD__,get_request('query','REQUEST'));
if (DEBUGTMP||DEBUGTMPSUB) printf('<font size=-2>* %s [Page:%s]</font><br />',__METHOD__,get_request('page','REQUEST'));
$this->template_id = $this->getTemplateChoice();
$this->page = get_request('page','REQUEST',false,1);
if ($this->template_id) {
$templates = $this->getTemplates();
$this->template = $templates->getTemplate($this->template_id);
$this->template->accept();
$this->visitStart();
$this->visitEnd();
}
}
/**
* Get our templates applicable for this object
*/
protected function getTemplates() {
2009-08-19 03:39:37 +00:00
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',129,0,__FILE__,__LINE__,__METHOD__,$fargs);
return new Queries($this->server_id);
}
/**
* Are default queries enabled?
*/
protected function haveDefaultTemplate() {
2009-08-19 03:39:37 +00:00
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',129,0,__FILE__,__LINE__,__METHOD__,$fargs);
$server = $this->getServer();
if ($server->getValue('query','disable_default'))
return false;
else
return true;
}
protected function drawTemplateChoice() {
if (DEBUGTMP) printf('<font size=-2>%s</font><br />',__METHOD__);
$server = $this->getServer();
$this->drawTitle(_('Search'));
$this->drawSubTitle();
echo "\n";
$baseDNs = $server->getBaseDN();
echo '<center>';
echo '<form action="cmd.php" name="advanced_search_form">';
echo '<input type="hidden" name="cmd" value="query_engine" />';
printf('<input type="hidden" name="server_id" value="%s" />',$server->getIndex());
echo '<table class="forminput" border=0>';
echo '<tr><td colspan=2>&nbsp;</td></tr>';
$templates = $this->getTemplates();
if (count($templates->getTemplates())) {
echo '<tr>';
printf('<td><acronym title="%s">%s</acronym></td>',_('Run a predefined query'),_('Predefined Query'));
echo '<td>';
echo '<select name="query">';
if ($this->haveDefaultTemplate())
printf('<option value="%s" %s>%s</option>','none','',_('Custom Query'));
foreach ($templates->getTemplates() as $template)
printf('<option value="%s" %s>%s</option>',
$template->getID(),
($this->template_id == $template->getID() ? 'selected' : ''),
$template->getDescription());
echo '</select>';
echo '</td>';
echo '</tr>';
}
printf('<td><acronym title="%s">%s</acronym></td>',_('The format to show the query results'),_('Display Format'));
echo '<td>';
echo '<select name="format" style="width: 200px">';
printf('<option value="list" %s>%s</option>',
$_SESSION[APPCONFIG]->getValue('search','display') == 'list' ? 'selected' : '',_('list'));
printf('<option value="table" %s>%s</option>',
$_SESSION[APPCONFIG]->getValue('search','display') == 'table' ? 'selected' : '',_('table'));
echo '</select>';
echo '</td>';
echo '</tr>';
echo '<tr>';
printf('<td><acronym title="%s">%s</acronym></td>',_('Entries to show per page'),_('Show Results'));
echo '<td>';
echo '<select name="showresults" style="width: 200px">';
printf('<option value="na" %s>%s</option>',
'','NA');
echo '</select>';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan=2>';
printf('<div id="customquery" style="display: %s">','block');
echo '<br/>';
echo '<fieldset>';
printf('<legend>%s</legend>',_('Custom Query'));
echo '<table border=0></tr>';
printf('<td>%s</td>',_('Base DN'));
printf('<td><input type="text" name="base" value="%s" style="width: 200px" id="base" />',count($baseDNs) == 1 ? $baseDNs[0] : '');
draw_chooser_link('advanced_search_form.base');
echo '</td>';
echo '</tr>';
echo '<tr>';
printf('<td><acronym title="%s">%s</acronym></td>',_('The scope in which to search'),_('Search Scope'));
echo '<td>';
echo '<select name="scope" style="width: 200px">';
printf('<option value="sub" %s>%s</option>',
'',_('Sub (entire subtree)'));
printf('<option value="one" %s>%s</option>',
'',_('One (one level beneath base)'));
printf('<option value="base" %s>%s</option>',
'',_('Base (base dn only)'));
echo '</select>';
echo '</td>';
echo '</tr>';
echo '<tr>';
printf('<td><acronym title="%s">%s</acronym></td>',
htmlspecialchars(_('Standard LDAP search filter. Example: (&(sn=Smith)(givenName=David))')),_('Search Filter'));
printf('<td><input type="text" name="filter" id="filter" style="width: 200px" value="%s" /></td>',
'objectClass=*');
echo '</tr>';
echo '<tr>';
printf('<td><acronym title="%s">%s</acronym></td>',
_('A list of attributes to display in the results (comma-separated)'),_('Show Attributes'));
printf('<td><input type="text" name="display_attrs" style="width: 200px" value="%s" /></td>',
implode(', ',$_SESSION[APPCONFIG]->getValue('search','result_attributes')));
echo '</tr>';
echo '<tr>';
printf('<td><acronym title="%s">%s</acronym></td>',_('Order by'),_('Order by'));
printf('<td><input type="text" name="orderby" id="orderby" style="width: 200px" value="%s" /></td>','');
echo '</tr></table>';
echo '</fieldset>';
echo '</div>';
echo '</td>';
echo '</tr>';
printf('<tr><td colspan="2"><br /><center><input type="submit" value="%s" /></center></td></tr>',_('Search'));
echo '</table>';
echo '</form>';
echo '</center>';
}
private function visitStart() {
2009-08-19 03:39:37 +00:00
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',129,0,__FILE__,__LINE__,__METHOD__,$fargs);
$this->drawTitle(_('Search Results'));
$this->drawSubTitle();
echo '<br/>';
}
private function visitEnd() {
2009-08-19 03:39:37 +00:00
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',129,0,__FILE__,__LINE__,__METHOD__,$fargs);
$server = $this->getServer();
$afattrs = $this->getAFAttrs();
2009-07-06 05:19:42 +00:00
# If Mass Actions Enabled
if ($_SESSION[APPCONFIG]->getValue('mass','enabled')) {
$mass_actions = array(
'&nbsp;' => '',
2009-07-07 09:40:48 +00:00
_('delete') => 'mass_delete',
_('edit') => 'mass_edit'
2009-07-06 05:19:42 +00:00
);
}
# Display the Javascript that enables us to show/hide DV entries
echo '<script type="text/javascript" language="javascript">';
echo "
function showthis(key,item) {
select = document.getElementById(key+item);
if (select.style.display == '')
return false;
hideall(key,item);
return false;
};
function hideall(key,except) {
items = items();
for (x in items) {
if (! isNaN(x) && except != items[x]) {
item = document.getElementById(key+items[x]);
item.style.display = 'none';
item = document.getElementById('CTL'+items[x]);
item.style.background = '#E0E0E0';
} else if (! isNaN(x) && except == items[x]) {
item = document.getElementById(key+items[x]);
item.style.display = '';
item = document.getElementById('CTL'+items[x]);
item.style.background = '#F0F0F0';
}
}
}
";
echo '</script>';
echo "\n\n";
$this->drawBaseTabs();
2009-08-19 11:19:35 +00:00
$ado = $this->template->getAttrDisplayOrder();
2009-08-19 11:20:51 +00:00
$counter = 0;
2009-08-19 11:20:51 +00:00
foreach ($this->template->results as $base => $results) {
$counter++;
2009-08-19 11:19:35 +00:00
2009-08-19 11:20:51 +00:00
if (! $show = get_request('show','REQUEST'))
$show = ($counter === 1 ? $this->getAjaxRef($base) : null);
2009-08-19 11:20:51 +00:00
printf('<div id="DN%s" style="display: %s">',
$this->getAjaxRef($base), ($show == $this->getAjaxRef($base) ? '' : 'none'));
2009-08-19 11:20:51 +00:00
echo '<table class="result_box" border=0 width=100%>';
echo '<tr><td>';
echo '<br/>';
echo '<br/>';
2009-08-19 11:20:51 +00:00
$this->drawResultsTable($base,count($results));
2009-08-19 11:20:51 +00:00
echo '<br/>';
echo '<br/>';
2009-08-19 11:20:51 +00:00
switch(get_request('format','REQUEST',false,$_SESSION[APPCONFIG]->getValue('search','display'))) {
case 'list':
foreach ($results as $dn => $dndetails) {
$dndetails = array_change_key_case($dndetails);
# Temporarily set our DN, for rendering that leverages our DN (eg: JpegPhoto)
$this->template->setDN($dn);
echo '<table class="result" border=0>';
echo '<tr class="list_title">';
printf('<td class="icon"><img src="%s/%s" alt="icon" /></td>',IMGDIR,get_icon($server->getIndex(),$dn));
printf('<td colspan=2><a href="cmd.php?cmd=template_engine&amp;server_id=%s&amp;dn=%s">%s</a></td>',
$server->getIndex(),rawurlencode(dn_unescape($dn)),htmlspecialchars(get_rdn($dn)));
echo '</tr>';
printf('<tr class="list_item"><td class="blank">&nbsp;</td><td class="heading">dn</td><td class="value">%s</td></tr>',
htmlspecialchars(dn_unescape($dn)));
# Iterate over each attribute for this entry
2009-08-19 11:19:35 +00:00
foreach (explode(',',$ado) as $attr) {
# Ignore DN, we've already displayed it.
if ($attr == 'dn')
continue;
if (! isset($dndetails[$attr]))
continue;
# Set our object with our values
$afattrs[$attr]->clearValue();
if (is_array($dndetails[$attr]))
$afattrs[$attr]->initValue($dndetails[$attr]);
else
$afattrs[$attr]->initValue(array($dndetails[$attr]));
echo '<tr class="list_item">';
echo '<td class="blank">&nbsp;</td>';
echo '<td class="heading">';
$this->draw('Name',$afattrs[$attr]);
echo '</td>';
echo '<td>';
$this->draw('CurrentValues',$afattrs[$attr]);
echo '</td>';
echo '</tr>';
}
echo '</table>';
echo '<br/>';
}
2009-08-19 11:20:51 +00:00
break;
# Display the results.
2009-08-19 11:20:51 +00:00
case 'table':
if (! $results) {
echo _('Search returned no results');
2009-08-19 11:20:51 +00:00
continue;
}
2009-08-19 11:20:51 +00:00
printf('<form action="cmd.php" method="post" id="massform_%s">',$counter);
2009-07-06 05:19:42 +00:00
printf('<input type="hidden" name="server_id" value="%s" />',$server->getIndex());
2009-07-07 09:40:48 +00:00
foreach ($this->template->resultsdata[$base]['attrs'] as $attr)
printf('<input type="hidden" name="attrs[]" value="%s" />',$attr);
echo '<table class="result_table" border=0>';
echo '<thead class="fixheader">';
echo '<tr class="heading">';
echo '<td>&nbsp;</td>';
2009-07-06 05:19:42 +00:00
echo '<td>&nbsp;</td>';
2009-08-19 11:19:35 +00:00
foreach (explode(',',$ado) as $attr) {
echo '<td>';
$this->draw('Name',$afattrs[$attr]);
echo '</td>';
}
echo '</tr>';
echo '</thead>';
echo '<tbody class="scroll">';
2009-08-19 11:20:51 +00:00
$j = 0;
foreach ($results as $dn => $dndetails) {
2009-08-19 11:20:51 +00:00
$j++;
$dndetails = array_change_key_case($dndetails);
# Temporarily set our DN, for rendering that leverages our DN (eg: JpegPhoto)
$this->template->setDN($dn);
2009-08-19 11:20:51 +00:00
printf('<tr class="%s" id="tr_ma_%s_%s" onClick="var cb=document.getElementById(\'ma_%s_%s\'); cb.checked=!cb.checked;">',
$j%2 ? 'even' : 'odd',$j,$counter,$j,$counter);
2009-07-06 05:19:42 +00:00
# Is mass action enabled.
if ($_SESSION[APPCONFIG]->getValue('mass','enabled'))
2009-08-19 11:20:51 +00:00
printf('<td><input type="checkbox" id="ma_%s_%s" name="dn[]" value="%s" onclick="this.checked=!this.checked;" /></td>',$j,$counter,$dn);
$href = sprintf('cmd=template_engine&server_id=%s&dn=%s',$server->getIndex(),rawurlencode($dn));
printf('<td class="icon"><a href="cmd.php?%s"><img src="%s/%s" alt="icon" /></a></td>',
htmlspecialchars($href),
IMGDIR,get_icon($server->getIndex(),$dn));
# We'll clone our attribute factory attributes, since we need to add the values to them for rendering.
2009-08-19 11:19:35 +00:00
foreach (explode(',',$ado) as $attr) {
# If the entry is blank, we'll draw an empty box and continue.
if (! isset($dndetails[$attr])) {
echo '<td>&nbsp;</td>';
continue;
}
# Special case for DNs
if ($attr == 'dn') {
$dn_display = strlen($dn) > 40
? sprintf('<acronym title="%s">%s...</acronym>',htmlspecialchars($dn),htmlspecialchars(substr($dn,0,40)))
: htmlspecialchars($dn);
printf('<td><a href="cmd.php?%s">%s</a></td>',htmlspecialchars($href),$dn_display);
continue;
}
# Set our object with our values
$afattrs[$attr]->clearValue();
if (is_array($dndetails[$attr]))
$afattrs[$attr]->initValue($dndetails[$attr]);
else
$afattrs[$attr]->initValue(array($dndetails[$attr]));
echo '<td>';
$this->draw('CurrentValues',$afattrs[$attr]);
echo '</td>';
}
echo '</tr>';
}
2009-07-06 05:19:42 +00:00
# Is mass action enabled.
if ($_SESSION[APPCONFIG]->getValue('mass','enabled')) {
2009-08-19 11:20:51 +00:00
printf('<tr class="%s">',++$j%2 ? 'odd' : 'even');
printf('<td><input type="checkbox" name="allbox" value="1" onclick="CheckAll(1,%s);" /></td>',$counter);
2009-08-19 11:19:35 +00:00
printf('<td colspan=%s>',2+count(explode(',',$ado)));
2009-07-06 05:19:42 +00:00
echo '<select name="cmd" onChange="if (this.value) submit();" style="font-size: 12px">';
2009-08-19 11:20:51 +00:00
2009-07-06 05:19:42 +00:00
foreach ($mass_actions as $action => $display)
printf('<option value="%s">%s</option>',$display,$action);
2009-08-19 11:20:51 +00:00
2009-07-06 05:19:42 +00:00
echo '</select>';
echo '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
2009-07-06 05:19:42 +00:00
echo '</form>';
echo "\n\n";
2009-07-06 05:19:42 +00:00
2009-08-19 11:20:51 +00:00
break;
default:
printf('Have ID [%s], run this query for page [%s]',$this->template_id,$this->page);
}
echo '</td></tr>';
echo '</table>';
echo '</div>';
echo "\n\n";
}
if (get_request('format','REQUEST',false,'table') == 'table') {
echo '<script type="text/javascript" language="javascript">'."\n";
echo "
function CheckAll(setbgcolor,form) {
htmlform = document.getElementById('massform_'+form);
for (var i=0;i<htmlform.elements.length;i++) {
var e = htmlform.elements[i];
2009-07-06 05:19:42 +00:00
if (e.type == 'checkbox' && e.name != 'allbox') {
2009-08-19 11:20:51 +00:00
e.checked = htmlform.allbox.checked;
2009-07-06 05:19:42 +00:00
if (!document.layers && setbgcolor) {
var tr = document.getElementById('tr_'+e.id);
if (e.checked) {
tr.style.backgroundColor='#DDDDFF';
} else {
var id = e.id.substr(2);
tr.style.backgroundColor= id%2 ? '#F0F0F0' : '#E0E0E0';
}
}
}
}
}
";
2009-08-19 11:20:51 +00:00
echo '</script>';
}
}
public function drawSubTitle($subtitle=null) {
if (is_null($subtitle)) {
$server = $this->getServer();
$subtitle = sprintf('%s: <b>%s</b>',_('Server'),$server->getName());
if ($this->template_id) {
$subtitle .= '<br />';
$subtitle .= sprintf('%s: <b>%s</b>',('Query'),$this->template->getID() != 'none' ? $this->template->getTitle() : _('Default'));
if ($this->template->getName())
$subtitle .= sprintf(' (<b>%s</b>)',$this->template->getName(false));
}
}
parent::drawSubTitle($subtitle);
}
private function getAFattrs() {
2009-08-19 03:39:37 +00:00
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',129,0,__FILE__,__LINE__,__METHOD__,$fargs);
$attribute_factory = new AttributeFactory();
$results = array();
foreach (explode(',',$this->template->getAttrDisplayOrder()) as $attr)
$results[$attr] = $attribute_factory->newAttribute($attr,array('values'=>array()),$this->getServerID());
return $results;
}
private function getAjaxRef($dn) {
2009-08-19 03:39:37 +00:00
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',129,0,__FILE__,__LINE__,__METHOD__,$fargs);
return preg_replace('/=/','.',base64_encode($dn));
}
private function drawBaseTabs() {
# Setup the Javascript to show/hide our DIVs.
echo '<script type="text/javascript" language="javascript">';
echo 'function items() {';
echo 'var $items = new Array();';
$counter = 0;
foreach ($this->template->results as $base => $results)
printf("items[%s] = '%s';",$counter++,$this->getAjaxRef($base));
echo 'return items;';
echo '}</script>';
echo "\n\n";
echo '<table class="result_table" border=0>';
echo '<tr>';
$counter = 0;
foreach ($this->template->results as $base => $results) {
if (! $show = get_request('show','REQUEST'))
$show = ($counter++ === 0 ? $this->getAjaxRef($base) : null);
printf('<td id="CTL%s" onclick="return showthis(\'DN\',\'%s\');" style="background-color: %s;">%s</td>',
$this->getAjaxRef($base),
$this->getAjaxRef($base),
($show == $this->getAjaxRef($base) ? '#F0F0F0' : '#E0E0E0'),
$base);
}
echo '</tr>';
echo '</table>';
echo "\n\n";
}
private function drawResultsTable($base,$results) {
$server = $this->getServer();
echo '<table class="result" border=0>';
echo '<tr>';
printf('<td>%s: <b>%s</b><br/><br/><div class="execution_time">(%s %s)</div></td>',_('Entries found'),
number_format($results),$this->template->resultsdata[$base]['time'],_('seconds'));
2009-07-25 15:17:47 +00:00
if ($_SESSION[APPCONFIG]->isCommandAvailable('script','export') && $_SESSION[APPCONFIG]->isCommandAvailable('script','export_form')) {
$href = htmlspecialchars(sprintf('cmd.php?cmd=export_form&server_id=%s&scope=%s&dn=%s&filter=%s&attributes=%s',
$server->getIndex(),$this->template->resultsdata[$base]['scope'],
$base,rawurlencode($this->template->resultsdata[$base]['filter']),
rawurlencode(implode(', ',$this->template->resultsdata[$base]['attrs']))));
printf('<td style="text-align: right; width: 85%%"><small>[ <a href="%s"><img src="%s/save.png" alt="Save" /> %s</a> ]</small>',
$href,IMGDIR,_('export results'));
}
printf('<small>[ <img src="%s/rename.png" alt="rename" /> %s:',IMGDIR,_('Format'));
foreach (array('list','table') as $f) {
echo '&nbsp;';
2009-08-19 11:20:51 +00:00
if (get_request('format','REQUEST',false,$_SESSION[APPCONFIG]->getValue('search','display')) == $f) {
printf('<b>%s</b>',_($f));
} else {
2009-08-19 11:20:51 +00:00
$query_string = htmlspecialchars(sprintf('%s&format=%s&show=%s&focusbase=%s',array_to_query_string($_GET,array('format','meth')),$f,$this->getAjaxRef($base),$base));
if (isAjaxEnabled())
printf('<a href="cmd.php?%s" onclick="return displayAJ(\'BODY\',\'%s\',\'%s\');">%s</a>',
$query_string,$query_string,_('Loading Search'),_($f));
else
printf('<a href="cmd.php?%s">%s</a>',$query_string,_($f));
}
}
echo ' ]</small>';
echo '<br />';
printf('<small>%s: <b>%s</b></small>',_('Base DN'),$base);
echo '<br />';
printf('<small>%s: <b>%s</b></small>',_('Filter performed'),htmlspecialchars($this->template->resultsdata[$base]['filter']));
echo '</td>';
echo '</tr>';
echo '</table>';
}
}
?>