. * * @package LibreNMS * @link http://librenms.org * @copyright 2018 Tony Murray * @author Tony Murray */ use LibreNMS\Authentication\LegacyAuth; $query = ''; $where = []; $params = []; if (!LegacyAuth::user()->hasGlobalRead()) { $query .= ' LEFT JOIN `devices_perms` USING (`device_id`)'; $where = '`devices_perms`.`user_id`=?'; $params[] = LegacyAuth::id(); } if (!empty($_REQUEST['search'])) { $where[] = '(`hostname` LIKE ? OR `sysName` LIKE ?)'; $search = '%' . mres($_REQUEST['search']) . '%'; $params[] = $search; $params[] = $search; } if (!empty($where)) { $query .= ' WHERE '; $query .= implode(' AND ', $where); } $total = dbFetchCell("SELECT COUNT(*) FROM `devices` $query", $params); $more = false; if (!empty($_REQUEST['limit'])) { $limit = (int) $_REQUEST['limit']; $page = isset($_REQUEST['page']) ? (int) $_REQUEST['page'] : 1; $offset = ($page - 1) * $limit; $query .= " LIMIT $offset, $limit"; } else { $offset = 0; } $sql = "SELECT `device_id`, `hostname`, `sysName` FROM `devices` $query"; $devices = array_map(function ($device) { return [ 'id' => $device['device_id'], 'text' => format_hostname($device), ]; }, dbFetchRows($sql, $params)); $more = ($offset + count($devices)) < $total; return [$devices, $more];