<?php
class Page_Passthrough extends Page
{
protected function doPreprocess()
{
User::load();
User::assertPermission('view');
$action = Request::post('action');
if ($action === 'save-hwlist') {
$this->saveHwList();
}
if (Request::isPost()) {
Util::redirect('?do=passthrough');
}
}
private function saveHwList()
{
$newgroups = Request::post('newgroup', [], 'array');
foreach ($newgroups as $id => $title) {
$id = strtoupper(preg_replace('/[^a-z0-9_\-]/i', '', $id));
if (empty($id))
continue;
Database::exec("INSERT IGNORE INTO passthrough_group (groupid, title)
VALUES (:group, :title)",
['group' => $id, 'title' => $title]);
}
$groups = Request::post('ptgroup', Request::REQUIRED, 'array');
$insert = [];
$delete = [];
foreach ($groups as $hwid => $group) {
if (empty($group)) {
$delete[] = $hwid;
} else {
$insert[] = [$hwid, '@PASSTHROUGH', $group];
}
}
if (!empty($insert)) {
Database::exec("INSERT INTO statistic_hw_prop (hwid, prop, `value`) VALUES :list
ON DUPLICATE KEY UPDATE `value` = VALUES(`value`)",
['list' => $insert]);
}
if (!empty($delete)) {
Database::exec("DELETE FROM statistic_hw_prop WHERE hwid IN (:list) AND prop = '@PASSTHROUGH'", ['list' => $delete]);
}
Message::addSuccess('list-updated');
Util::redirect('?do=passthrough&show=hwlist');
}
/*
*
*/
protected function doRender()
{
$show = Request::get('show');
if ($show === 'hwlist') {
$this->showHardwareList();
} else {
Util::redirect('?do=passthrough&show=hwlist');
}
}
private function showHardwareList()
{
$q = new HardwareQuery(HardwareInfo::PCI_DEVICE, null, false);
$q->addGlobalColumn('vendor');
$q->addGlobalColumn('device');
$q->addGlobalColumn('class');
$q->addGlobalColumn('@PASSTHROUGH');
$rows = [];
foreach ($q->query('shw.hwid') as $row) {
$row['ptlist'] = Passthrough::getGroupDropdown($row);
$rows[] = $row;
}
// Sort Graphics Cards first, rest by class, vendor, device
usort($rows, function ($row1, $row2) {
$a = $row1['class'];
$b = $row2['class'];
if ($a === $b)
return hexdec($row1['vendor'].$row1['device']) - hexdec($row2['vendor'] . $row2['device']);
if ($a === '0300')
return -1;
if ($b === '0300')
return 1;
return hexdec($a) - hexdec($b);
});
$finalRows = [];
$missing = [];
$lastClass = '';
foreach ($rows as $row) {
if ($row['class'] !== $lastClass) {
// Add class row header
$lastClass = $row['class'];
$finalRows[] = [
'class' => $row['class'],
'class_name' => PciId::getPciId(PciId::DEVCLASS, $row['class'], true) ?: 'Unknown',
];
}
$row['vendor_name'] = PciId::getPciId(PciId::VENDOR, $row['vendor'] ?? '');
$row['device_name'] = PciId::getPciId(PciId::DEVICE, $row['vendor'] . ':' . $row['device']);
$finalRows[] = $row;
// Build up query
if ($row['vendor_name'] === false) {
$missing[$row['vendor']] = true;
}
if ($row['device_name'] === false) {
$missing[$row['vendor'] . ':' . $row['device']] = true;
}
}
Render::addTemplate('hardware-list', [
'list' => $finalRows,
'missing_ids' => json_encode(array_keys($missing)),
]);
}
/*
*
*/
protected function doAjax()
{
//
}
}