Issues (2963)

includes/html/pages/search/packages.inc.php (1 issue)

1
<?php
2
/* Copyright (C) 2014 Daniel Preussker <[email protected]>
3
 * This program is free software: you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License as published by
5
 * the Free Software Foundation, either version 3 of the License, or
6
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
 */
16
17
/**
18
 * Package Search
19
 *
20
 * @author Daniel Preussker <[email protected]>
21
 * @copyright 2014 f0o, LibreNMS
22
 * @license GPL
23
 */
24
print_optionbar_start(28);
25
?>
26
<form method="post" action="" class="form-inline" role="form">
27
    <?php echo csrf_field() ?>
28
    <div class="form-group">
29
        <label for="package">Package</label>
30
        <input type="text" name="package" id="package" size=20 value="<?php echo $_POST['package']; ?>" class="form-control input-sm" placeholder="Any" />
31
    </div>
32
    <div class="form-group">
33
        <label for="version">Version</label>
34
        <input type="text" name="version" id="version" size=20 value="<?php echo $_POST['version']; ?>" class="form-control input-sm" placeholder="Any" />
35
    </div>
36
    <div class="form-group">
37
        <label for="version">Arch</label>
38
        <input type="text" name="arch" id="arch" size=20 value="<?php echo $_POST['arch']; ?>" class="form-control input-sm" placeholder="Any" />
39
    </div>
40
    <button type="submit" class="btn btn-default input-sm">Search</button>
41
</form>
42
<?php
43
print_optionbar_end();
44
45
if (isset($_POST['results_amount']) && $_POST['results_amount'] > 0) {
46
    $results = $_POST['results'];
47
} else {
48
    $results = 50;
49
}
50
51
?>
52
<form method="post" action="search/search=packages/" id="result_form">
53
    <?php echo csrf_field() ?>
54
    <table class="table table-hover table-condensed table-striped">
55
        <tr>
56
            <td colspan="3"><strong>Packages</strong></td>
57
            <td><select name="results" id="results" class="form-control input-sm" onChange="updateResults(this);">
58
                <?php
59
                $result_options = ['10', '50', '100', '250', '500', '1000', '5000'];
60
                foreach ($result_options as $option) {
61
                    echo "<option value='$option'";
62
                    if ($results == $option) {
63
                        echo ' selected';
64
                    }
65
                    echo ">$option</option>";
66
                }
67
                ?>
68
            </select></td>
69
        </tr>
70
<?php
71
72
$count_query = 'SELECT COUNT(*) FROM ( ';
73
$full_query = '';
74
$query = 'SELECT packages.name FROM packages,devices ';
75
$param = [];
76
77
if (! Auth::user()->hasGlobalRead()) {
78
    $device_ids = Permissions::devicesForUser()->toArray() ?: [0];
79
    $where .= ' AND `D`.`device_id` IN ' . dbGenPlaceholders(count($device_ids));
80
    $param = array_merge($param, $device_ids);
81
}
82
83
$query .= " WHERE packages.device_id = devices.device_id AND packages.name LIKE '%" . $_POST['package'] . "%' $sql_where GROUP BY packages.name";
84
85
$where = '';
86
$ver = '';
87
$opt = '';
88
89
if (! empty($_POST['arch'])) {
90
    $where .= ' AND packages.arch = ?';
91
    $param[] = $_POST['arch'];
92
}
93
94
if (is_numeric($_REQUEST['device_id'])) {
95
    $where .= ' AND packages.device_id = ?';
96
    $param[] = $_REQUEST['device_id'];
97
}
98
99
$count_query .= $query . ' ) sub';
100
$query .= $where . ' ORDER BY packages.name, packages.arch, packages.version';
101
$count = dbFetchCell($count_query, $param);
102
103
if (! isset($_POST['page_number']) && $_POST['page_number'] < 1) {
104
    $page_number = 1;
105
} else {
106
    $page_number = $_POST['page_number'];
107
}
108
109
$start = ($page_number - 1) * $results;
110
$full_query = $full_query . $query . " LIMIT $start,$results";
111
112
?>
113
        <tr>
114
            <th>Package</th>
115
            <th>Version</th>
116
            <th>Arch</th>
117
            <th>Device</th>
118
        </tr>
119
<?php
120
121
$ordered = [];
122
foreach (dbFetchRows($full_query, $param) as $entry) {
123
    $tmp = dbFetchRows('SELECT packages.*,devices.hostname FROM packages,devices WHERE packages.device_id=devices.device_id AND packages.name = ?', [$entry['name']]);
124
    foreach ($tmp as $entry) {
0 ignored issues
show
Comprehensibility Bug introduced by
$entry is overwriting a variable from outer foreach loop.
Loading history...
125
        if (! is_array($ordered[$entry['name']])) {
126
            $ordered[$entry['name']] = [$entry];
127
        } else {
128
            $ordered[$entry['name']][] = $entry;
129
        }
130
    }
131
}
132
133
if (! empty($_POST['version'])) {
134
    [$opt, $ver] = explode(' ', $_POST['version']);
135
}
136
137
foreach ($ordered as $name => $entry) {
138
    $vers = [];
139
    $arch = [];
140
    $devs = [];
141
    foreach ($entry as $variation) {
142
        $variation['version'] = str_replace(':', '.', $variation['version']);
143
        if (! in_array($variation['version'], $vers) && (empty($ver) || version_compare($variation['version'], $ver, $opt))) {
144
            $vers[] = $variation['version'];
145
        }
146
        if (! in_array($variation['arch'], $arch)) {
147
            $arch[] = $variation['arch'];
148
        }
149
        if (! in_array($variation['hostname'], $devs)) {
150
            unset($variation['version']);
151
            $devs[] = generate_device_link($variation);
152
        }
153
    }
154
    if (sizeof($arch) > 0 && sizeof($vers) > 0) {
155
        ?>
156
        <tr>
157
            <td><a href="<?php echo \LibreNMS\Util\Url::generate(['page' => 'packages', 'name' => $name]); ?>"><?php echo $name; ?></a></td>
158
            <td><?php echo implode('<br/>', $vers); ?></td>
159
            <td><?php echo implode('<br/>', $arch); ?></td>
160
            <td><?php echo implode('<br/>', $devs); ?></td>
161
        </tr>
162
        <?php
163
    }
164
}
165
if ((int) ($count / $results) > 0 && $count != $results) {
166
    ?>
167
        <tr>
168
            <td colspan="6" align="center"><?php echo generate_pagination($count, $results, $page_number); ?></td>
169
        </tr>
170
    <?php
171
}
172
?>
173
174
    </table>
175
    <input type="hidden" name="page_number" id="page_number" value="<?php echo $page_number; ?>">
176
    <input type="hidden" name="results_amount" id="results_amount" value="<?php echo $results; ?>">
177
    <input type="hidden" name="package" id="results_packages" value="<?php echo $_POST['package']; ?>">
178
    <input type="hidden" name="version" id="results_version" value="<?php echo $_POST['version']; ?>">
179
    <input type="hidden" name="arch" id="results_arch" value="<?php echo $_POST['arch']; ?>">
180
</form>
181
<script type="text/javascript">
182
    function updateResults(results) {
183
       $('#results_amount').val(results.value);
184
       $('#page_number').val(1);
185
       $('#result_form').trigger( "submit" );
186
    }
187
188
    function changePage(page,e) {
189
        e.preventDefault();
190
        $('#page_number').val(page);
191
        $('#result_form').trigger( "submit" );
192
    }
193
</script>
194