1<?php
2
3/*
4 * LibreNMS
5 *
6 * Copyright (c) 2017 Aldemir Akpinar <https://github.com/aldemira/>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.  Please see LICENSE.txt at the top level of
12 * the source code distribution for details.
13 */
14
15if (! Auth::user()->hasGlobalAdmin()) {
16    $status = ['status' => 1, 'message' => 'You need to be admin'];
17} else {
18    if (isset($_POST['viewtype'])) {
19        if ($_POST['viewtype'] == 'fulllist') {
20            $deps_query = 'SELECT a.device_id as id, a.hostname as hostname, a.sysName as sysName, GROUP_CONCAT(b.hostname) as parent, GROUP_CONCAT(b.device_id) as parentid FROM devices as a LEFT JOIN device_relationships a1 ON a.device_id=a1.child_device_id LEFT JOIN devices b ON b.device_id = a1.parent_device_id GROUP BY a.device_id, a.hostname, a.sysName';
21
22            if (isset($_POST['searchPhrase']) && ! empty($_POST['searchPhrase'])) {
23                $deps_query .= ' HAVING parent LIKE ? OR hostname LIKE ? OR sysName LIKE ? ';
24                $count_query = 'SELECT COUNT(*) FROM (' . $deps_query . ') AS rowcount';
25            } else {
26                $count_query = 'SELECT COUNT(device_id) AS rowcount FROM devices';
27            }
28
29            // if format is set we're trying to pull the Bootgrid table data
30            if (isset($_POST['format'])) {
31                $order_by = '';
32                if (isset($_POST['sort']) && is_array($_REQUEST['sort'])) {
33                    foreach ($_REQUEST['sort'] as $key => $value) {
34                        $order_by .= " $key $value";
35                    }
36                } else {
37                    $order_by = ' a.hostname';
38                }
39
40                $deps_query .= ' ORDER BY ' . $order_by;
41
42                if (is_numeric($_POST['rowCount']) && is_numeric($_POST['current'])) {
43                    $rows = $_POST['rowCount'];
44                    $current = $_POST['current'];
45                    if ($rows > 0) {
46                        $deps_query .= ' LIMIT ' . $rows * ($current - 1) . ', ' . $rows;
47                    }
48                }
49            } else {
50                $deps_query .= ' ORDER BY a.hostname';
51            }
52
53            if (isset($_POST['format']) && ! empty($_POST['searchPhrase'])) {
54                $searchphrase = '%' . $_POST['searchPhrase'] . '%';
55                $search_arr = [$searchphrase, $searchphrase, $searchphrase];
56                $device_deps = dbFetchRows($deps_query, $search_arr);
57                $rec_count = dbFetchCell($count_query, $search_arr);
58            } else {
59                $device_deps = dbFetchRows($deps_query);
60                $rec_count = dbFetchCell($count_query);
61            }
62
63            if (isset($_POST['format'])) {
64                $res_arr = [];
65                foreach ($device_deps as $myrow) {
66                    if ($myrow['parent'] == null || $myrow['parent'] == '') {
67                        $parent = 'None';
68                    } else {
69                        $parent = $myrow['parent'];
70                    }
71
72                    $hostname = format_hostname($myrow);
73                    $sysname = ($hostname == $myrow['sysName']) ? $myrow['hostname'] : $myrow['sysName'];
74                    array_push($res_arr, ['deviceid' => $myrow['id'], 'hostname' => $hostname, 'sysname' => $sysname, 'parent' => $parent, 'parentid' => $myrow['parentid']]);
75                }
76                $status = ['current' => $_POST['current'], 'rowCount' => $_POST['rowCount'], 'rows' => $res_arr, 'total' => $rec_count];
77            } else {
78                $status = ['status' => 0, 'deps' => $device_deps];
79            }
80        } else {
81            // Get childs from parent id(s)
82            if ($_POST['viewtype'] == 'fromparent') {
83                if ($_POST['parent_ids'] == 0) {
84                    $device_deps = dbFetchRows('SELECT `device_id`,`hostname` from `devices` as a LEFT JOIN `device_relationships` as b ON b.`child_device_id` =  a.`device_id` WHERE b.`child_device_id` is null ORDER BY `hostname`');
85                } else {
86                    $parents = implode(',', $_POST['parent_ids']);
87                    $device_deps = dbFetchRows('SELECT  a.device_id as device_id, a.hostname as hostname, GROUP_CONCAT(b.hostname) as parent, GROUP_CONCAT(b.device_id) as parentid FROM devices as a LEFT JOIN device_relationships a1 ON a.device_id=a1.child_device_id LEFT JOIN devices b ON b.device_id=a1.parent_device_id GROUP BY a.device_id, a.hostname HAVING parentid = ?', [$parents]);
88                }
89
90                $status = ['status' => 0, 'deps' => $device_deps];
91            }
92        }
93    } else {
94        // Find devices by child.
95        if (! is_numeric($_POST['device_id'])) {
96            $status = ['status' => 1, 'message' => 'Wrong device id!'];
97        } else {
98            $deps_query = 'SELECT `device_id`, `hostname` FROM `devices` AS a INNER JOIN `device_relationships` AS b ON a.`device_id` = b.`parent_device_id` WHERE ';
99            // device_id == 0 is the case where we have no parents.
100            if ($_POST['device_id'] == 0) {
101                $device_deps = dbFetchRows($deps_query . ' b.`parent_device_id` is null OR b.`parent_device_id` = 0 ');
102            } else {
103                $device_deps = dbFetchRows($deps_query . ' b.`child_device_id` = ?', [$_POST['device_id']]);
104            }
105            $status = ['status' => 0, 'deps' => $device_deps];
106        }
107    }
108}
109
110header('Content-Type: application/json');
111echo json_encode($status, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
112