Issues (2963)

app/Http/Controllers/DeviceController.php (1 issue)

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Facades\DeviceCache;
6
use App\Models\Device;
7
use App\Models\Vminfo;
8
use Carbon\Carbon;
9
use Gate;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Arr;
12
use Illuminate\Support\Facades\Auth;
13
use LibreNMS\Config;
14
use LibreNMS\Util\Debug;
15
use LibreNMS\Util\Graph;
16
use LibreNMS\Util\Url;
17
18
class DeviceController extends Controller
19
{
20
    private $tabs = [
21
        'overview' => \App\Http\Controllers\Device\Tabs\OverviewController::class,
22
        'graphs' => \App\Http\Controllers\Device\Tabs\GraphsController::class,
23
        'health' => \App\Http\Controllers\Device\Tabs\HealthController::class,
24
        'apps' => \App\Http\Controllers\Device\Tabs\AppsController::class,
25
        'processes' => \App\Http\Controllers\Device\Tabs\ProcessesController::class,
26
        'collectd' => \App\Http\Controllers\Device\Tabs\CollectdController::class,
27
        'munin' => \App\Http\Controllers\Device\Tabs\MuninController::class,
28
        'ports' => \App\Http\Controllers\Device\Tabs\PortsController::class,
29
        'port' => \App\Http\Controllers\Device\Tabs\PortController::class,
30
        'slas' => \App\Http\Controllers\Device\Tabs\SlasController::class,
31
        'wireless' => \App\Http\Controllers\Device\Tabs\WirelessController::class,
32
        'accesspoints' => \App\Http\Controllers\Device\Tabs\AccessPointsController::class,
33
        'vlans' => \App\Http\Controllers\Device\Tabs\VlansController::class,
34
        'vm' => \App\Http\Controllers\Device\Tabs\VmInfoController::class,
35
        'mef' => \App\Http\Controllers\Device\Tabs\MefController::class,
36
        'tnmsne' => \App\Http\Controllers\Device\Tabs\TnmsneController::class,
37
        'loadbalancer' => \App\Http\Controllers\Device\Tabs\LoadBalancerController::class,
38
        'routing' => \App\Http\Controllers\Device\Tabs\RoutingController::class,
39
        'pseudowires' => \App\Http\Controllers\Device\Tabs\PseudowiresController::class,
40
        'neighbours' => \App\Http\Controllers\Device\Tabs\NeighboursController::class,
41
        'stp' => \App\Http\Controllers\Device\Tabs\StpController::class,
42
        'packages' => \App\Http\Controllers\Device\Tabs\PackagesController::class,
43
        'inventory' => \App\Http\Controllers\Device\Tabs\InventoryController::class,
44
        'services' => \App\Http\Controllers\Device\Tabs\ServicesController::class,
45
        'printer' => \App\Http\Controllers\Device\Tabs\PrinterController::class,
46
        'logs' => \App\Http\Controllers\Device\Tabs\LogsController::class,
47
        'alerts' => \App\Http\Controllers\Device\Tabs\AlertsController::class,
48
        'alert-stats' => \App\Http\Controllers\Device\Tabs\AlertStatsController::class,
49
        'showconfig' => \App\Http\Controllers\Device\Tabs\ShowConfigController::class,
50
        'netflow' => \App\Http\Controllers\Device\Tabs\NetflowController::class,
51
        'latency' => \App\Http\Controllers\Device\Tabs\LatencyController::class,
52
        'nac' => \App\Http\Controllers\Device\Tabs\NacController::class,
53
        'notes' => \App\Http\Controllers\Device\Tabs\NotesController::class,
54
        'mib' => \App\Http\Controllers\Device\Tabs\MibController::class,
55
        'edit' => \App\Http\Controllers\Device\Tabs\EditController::class,
56
        'capture' => \App\Http\Controllers\Device\Tabs\CaptureController::class,
57
    ];
58
59
    public function index(Request $request, $device, $current_tab = 'overview', $vars = '')
60
    {
61
        $device = str_replace('device=', '', $device);
62
        $device = is_numeric($device) ? DeviceCache::get((int) $device) : DeviceCache::getByHostname($device);
63
        $device_id = $device->device_id;
64
65
        if (! $device->exists) {
66
            abort(404);
67
        }
68
69
        DeviceCache::setPrimary($device_id);
70
71
        $current_tab = str_replace('tab=', '', $current_tab);
72
        $current_tab = array_key_exists($current_tab, $this->tabs) ? $current_tab : 'overview';
73
74
        if ($current_tab == 'port') {
75
            $vars = Url::parseLegacyPath($request->path());
76
            $port = $device->ports()->findOrFail($vars->get('port'));
77
            $this->authorize('view', $port);
78
        } else {
79
            $this->authorize('view', $device);
80
        }
81
82
        $alert_class = $device->disabled ? 'alert-info' : ($device->status ? '' : 'alert-danger');
83
        $parent_id = Vminfo::guessFromDevice($device)->value('device_id');
84
        $overview_graphs = $this->buildDeviceGraphArrays($device);
85
86
        $tabs = array_map(function ($class) {
87
            return app()->make($class);
88
        }, array_filter($this->tabs, 'class_exists')); // TODO remove filter
89
        $title = $tabs[$current_tab]->name();
90
        $data = $tabs[$current_tab]->data($device);
91
92
        // Device Link Menu, select the primary link
93
        $device_links = $this->deviceLinkMenu($device);
94
        $primary_device_link_name = Config::get('html.device.primary_link', 'edit');
95
        if (! isset($device_links[$primary_device_link_name])) {
96
            $primary_device_link_name = array_key_first($device_links);
97
        }
98
        $primary_device_link = $device_links[$primary_device_link_name];
99
        unset($device_links[$primary_device_link_name], $primary_device_link_name);
100
101
        if (view()->exists('device.tabs.' . $current_tab)) {
102
            return view('device.tabs.' . $current_tab, get_defined_vars());
103
        }
104
105
        $tab_content = $this->renderLegacyTab($current_tab, $device, $data);
106
107
        return view('device.tabs.legacy', get_defined_vars());
108
    }
109
110
    private function renderLegacyTab($tab, Device $device, $data)
111
    {
112
        ob_start();
113
        $device = $device->toArray();
114
        Debug::set(false);
115
        chdir(base_path());
116
        $init_modules = ['web', 'auth'];
117
        require base_path('/includes/init.php');
118
119
        $vars['device'] = $device['device_id'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$vars was never initialized. Although not strictly required by PHP, it is generally a good practice to add $vars = array(); before regardless.
Loading history...
120
        $vars['tab'] = $tab;
121
122
        extract($data); // set preloaded data into variables
123
        include "includes/html/pages/device/$tab.inc.php";
124
        $output = ob_get_clean();
125
        ob_end_clean();
126
127
        return $output;
128
    }
129
130
    private function buildDeviceGraphArrays($device)
131
    {
132
        $graph_array = [
133
            'width' => 150,
134
            'height' => 45,
135
            'device' => $device->device_id,
136
            'type' => 'device_bits',
137
            'from' => Carbon::now()->subDay()->timestamp,
138
            'legend' => 'no',
139
            'bg' => 'FFFFFF00',
140
        ];
141
142
        $graphs = [];
143
        foreach (Graph::getOverviewGraphsForDevice($device) as $graph) {
144
            $graph_array['type'] = $graph['graph'];
145
            $graph_array['popup_title'] = __($graph['text']);
146
            $graphs[] = $graph_array;
147
        }
148
149
        return $graphs;
150
    }
151
152
    private function deviceLinkMenu(Device $device)
153
    {
154
        $device_links = [];
155
156
        if (Gate::allows('update', $device)) {
157
            $device_links['edit'] = [
158
                'icon' => 'fa-gear',
159
                'url' => route('device', [$device->device_id, 'edit']),
160
                'title' => __('Edit'),
161
                'external' => false,
162
            ];
163
        }
164
165
        // User defined device links
166
        foreach (array_values(Arr::wrap(Config::get('html.device.links'))) as $index => $link) {
167
            $device_links['custom' . ($index + 1)] = [
168
                'icon' => $link['icon'] ?? 'fa-external-link',
169
                'url' => view(['template' => $link['url']], ['device' => $device])->__toString(),
170
                'title' => $link['title'],
171
                'external' => $link['external'] ?? true,
172
            ];
173
        }
174
175
        // Web
176
        $device_links['web'] = [
177
            'icon' => 'fa-globe',
178
            'url' => 'https://' . $device->hostname,
179
            'title' => __('Web'),
180
            'external' => true,
181
            'onclick' => 'http_fallback(this); return false;',
182
        ];
183
184
        // SSH
185
        $ssh_url = Config::has('gateone.server')
186
            ? Config::get('gateone.server') . '?ssh=ssh://' . (Config::get('gateone.use_librenms_user') ? Auth::user()->username . '@' : '') . $device['hostname'] . '&location=' . $device['hostname']
187
            : 'ssh://' . $device->hostname;
188
        $device_links['ssh'] = [
189
            'icon' => 'fa-lock',
190
            'url' => $ssh_url,
191
            'title' => __('SSH'),
192
            'external' => true,
193
        ];
194
195
        // Telnet
196
        $device_links['telnet'] = [
197
            'icon' => 'fa-terminal',
198
            'url' => 'telnet://' . $device->hostname,
199
            'title' => __('Telnet'),
200
            'external' => true,
201
        ];
202
203
        if (Gate::allows('admin')) {
204
            $device_links['capture'] = [
205
                'icon' => 'fa-bug',
206
                'url' => route('device', [$device->device_id, 'capture']),
207
                'title' => __('Capture'),
208
                'external' => false,
209
            ];
210
        }
211
212
        return $device_links;
213
    }
214
}
215