197 lines
6.6 KiB
PHP
Raw Normal View History

2015-01-04 19:58:37 +00:00
<?php
/* Copyright (C) 2014 Daniel Preussker <f0o@devilcode.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
2015-07-13 20:10:26 +02:00
*
2015-01-04 19:58:37 +00:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
2015-07-13 20:10:26 +02:00
*
2015-01-04 19:58:37 +00:00
* You should have received a copy of the GNU General Public License
2015-07-13 20:10:26 +02:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2015-01-04 19:58:37 +00:00
/**
* Package Search
* @author Daniel Preussker <f0o@devilcode.org>
* @copyright 2014 f0o, LibreNMS
* @license GPL
* @package LibreNMS
* @subpackage Search
*/
use LibreNMS\Authentication\LegacyAuth;
2015-01-04 19:58:37 +00:00
print_optionbar_start(28);
?>
<form method="post" action="" class="form-inline" role="form">
2015-07-13 20:10:26 +02:00
<div class="form-group">
<label for="package">Package</label>
2016-02-17 13:59:58 +00:00
<input type="text" name="package" id="package" size=20 value="<?php echo($_POST['package']); ?>" class="form-control input-sm" placeholder="Any" />
2015-07-13 20:10:26 +02:00
</div>
<div class="form-group">
<label for="version">Version</label>
2016-02-17 13:59:58 +00:00
<input type="text" name="version" id="version" size=20 value="<?php echo($_POST['version']); ?>" class="form-control input-sm" placeholder="Any" />
2015-07-13 20:10:26 +02:00
</div>
<div class="form-group">
<label for="version">Arch</label>
2016-02-17 13:59:58 +00:00
<input type="text" name="arch" id="arch" size=20 value="<?php echo($_POST['arch']); ?>" class="form-control input-sm" placeholder="Any" />
2015-07-13 20:10:26 +02:00
</div>
<button type="submit" class="btn btn-default input-sm">Search</button>
2015-01-04 19:58:37 +00:00
</form>
<?php
print_optionbar_end();
if (isset($_POST['results_amount']) && $_POST['results_amount'] > 0) {
2016-02-17 13:59:58 +00:00
$results = $_POST['results'];
} else {
2015-07-13 20:10:26 +02:00
$results = 50;
2015-01-04 19:58:37 +00:00
}
?>
<form method="post" action="search/search=packages/" id="result_form">
2015-07-13 20:10:26 +02:00
<table class="table table-hover table-condensed table-striped">
<tr>
<td colspan="3"><strong>Packages</strong></td>
<td><select name="results" id="results" class="form-control input-sm" onChange="updateResults(this);">
<?php
$result_options = array('10','50','100','250','500','1000','5000');
foreach ($result_options as $option) {
echo "<option value='$option'";
if ($results == $option) {
echo " selected";
2015-07-13 20:10:26 +02:00
}
echo ">$option</option>";
}
2015-07-13 20:10:26 +02:00
?>
</select></td>
</tr>
2015-01-04 19:58:37 +00:00
<?php
$count_query = "SELECT COUNT(*) FROM ( ";
$full_query = "";
$query = 'SELECT packages.name FROM packages,devices ';
2015-01-04 20:42:59 +00:00
$param = array();
if (!LegacyAuth::user()->hasGlobalRead()) {
$query .= " LEFT JOIN `devices_perms` AS `DP` ON `devices`.`device_id` = `DP`.`device_id`";
$sql_where .= " AND `DP`.`user_id`=?";
$param[] = LegacyAuth::id();
}
2016-02-17 13:59:58 +00:00
$query .= " WHERE packages.device_id = devices.device_id AND packages.name LIKE '%".mres($_POST['package'])."%' $sql_where GROUP BY packages.name";
$where = '';
2015-01-04 20:42:59 +00:00
$ver = "";
$opt = "";
2015-01-04 19:58:37 +00:00
if (!empty($_POST['arch'])) {
2015-07-13 20:10:26 +02:00
$where .= ' AND packages.arch = ?';
2016-02-17 13:59:58 +00:00
$param[] = mres($_POST['arch']);
2015-01-04 19:58:37 +00:00
}
if (is_numeric($_REQUEST['device_id'])) {
2015-07-13 20:10:26 +02:00
$where .= " AND packages.device_id = ?";
$param[] = $_REQUEST['device_id'];
2015-01-04 19:58:37 +00:00
}
$count_query .= $query." ) sub";
$query .= $where." ORDER BY packages.name, packages.arch, packages.version";
$count = dbFetchCell($count_query, $param);
2015-01-04 19:58:37 +00:00
if (!isset($_POST['page_number']) && $_POST['page_number'] < 1) {
2015-07-13 20:10:26 +02:00
$page_number = 1;
} else {
2016-02-17 13:59:58 +00:00
$page_number = $_POST['page_number'];
2015-01-04 19:58:37 +00:00
}
$start = ($page_number - 1) * $results;
$full_query = $full_query . $query . " LIMIT $start,$results";
?>
2015-07-13 20:10:26 +02:00
<tr>
<th>Package</th>
<th>Version</th>
<th>Arch</th>
<th>Device</th>
</tr>
2015-01-04 19:58:37 +00:00
<?php
$ordered = array();
foreach (dbFetchRows($full_query, $param) as $entry) {
$tmp = dbFetchRows("SELECT packages.*,devices.hostname FROM packages,devices WHERE packages.device_id=devices.device_id AND packages.name = ?", array($entry['name']));
foreach ($tmp as $entry) {
if (!is_array($ordered[$entry['name']])) {
2015-07-13 20:10:26 +02:00
$ordered[$entry['name']] = array( $entry );
} else {
2015-07-13 20:10:26 +02:00
$ordered[$entry['name']][] = $entry;
}
}
2015-01-04 19:58:37 +00:00
}
if (!empty($_POST['version'])) {
list($opt, $ver) = explode(" ", $_POST['version']);
2015-01-04 19:58:37 +00:00
}
foreach ($ordered as $name => $entry) {
2015-07-13 20:10:26 +02:00
$vers = array();
$arch = array();
$devs = array();
foreach ($entry as $variation) {
$variation['version'] = str_replace(":", ".", $variation['version']);
if (!in_array($variation['version'], $vers) && (empty($ver) || version_compare($variation['version'], $ver, $opt))) {
2015-07-13 20:10:26 +02:00
$vers[] = $variation['version'];
}
if (!in_array($variation['arch'], $arch)) {
2015-07-13 20:10:26 +02:00
$arch[] = $variation['arch'];
}
if (!in_array($variation['hostname'], $devs)) {
2015-07-13 20:10:26 +02:00
unset($variation['version']);
$devs[] = generate_device_link($variation);
}
}
if (sizeof($arch) > 0 && sizeof($vers) > 0) {
2015-07-13 20:10:26 +02:00
?>
<tr>
<td><a href="<?php echo(generate_url(array('page'=>'packages','name'=>$name))); ?>"><?php echo $name; ?></a></td>
<td><?php echo implode('<br/>', $vers); ?></td>
<td><?php echo implode('<br/>', $arch); ?></td>
<td><?php echo implode('<br/>', $devs); ?></td>
2015-07-13 20:10:26 +02:00
</tr>
<?php
}
2015-01-04 19:58:37 +00:00
}
if ((int) ($count / $results) > 0 && $count != $results) {
2015-07-13 20:10:26 +02:00
?>
<tr>
<td colspan="6" align="center"><?php echo generate_pagination($count, $results, $page_number); ?></td>
2015-07-13 20:10:26 +02:00
</tr>
<?php
2015-01-04 19:58:37 +00:00
}
?>
2015-07-13 20:10:26 +02:00
</table>
<input type="hidden" name="page_number" id="page_number" value="<?php echo $page_number; ?>">
<input type="hidden" name="results_amount" id="results_amount" value="<?php echo $results; ?>">
2016-02-17 13:59:58 +00:00
<input type="hidden" name="package" id="results_packages" value="<?php echo $_POST['package']; ?>">
<input type="hidden" name="version" id="results_version" value="<?php echo $_POST['version']; ?>">
<input type="hidden" name="arch" id="results_arch" value="<?php echo $_POST['arch']; ?>">
2015-01-04 19:58:37 +00:00
</form>
<script type="text/javascript">
function updateResults(results) {
$('#results_amount').val(results.value);
$('#page_number').val(1);
$('#result_form').submit();
}
function changePage(page,e) {
e.preventDefault();
$('#page_number').val(page);
$('#result_form').submit();
}
</script>