diff options
Diffstat (limited to 'modules-available/usblockoff/api.inc.php')
-rw-r--r-- | modules-available/usblockoff/api.inc.php | 66 |
1 files changed, 64 insertions, 2 deletions
diff --git a/modules-available/usblockoff/api.inc.php b/modules-available/usblockoff/api.inc.php index 67f805f5..52a98b67 100644 --- a/modules-available/usblockoff/api.inc.php +++ b/modules-available/usblockoff/api.inc.php @@ -31,13 +31,15 @@ function HandleParameters() 'lastseen' => time() ); newDevice($id, $serial, $hwProps, $deviceProps); - } elseif ($getAction == "deletedevice") { + } elseif ($getAction === "deletedevice") { $id = Request::get('id', '', 'string'); $serial = Request::get('serial', '', 'string'); deleteDevice($id, $serial); - } elseif ($getAction == "getrule") { + } elseif ($getAction === "getrule") { $configid = Request::get('configid', '0', 'int'); getRule($configid); + } elseif ($getAction === "getidlist") { + echo getIDList(); } } @@ -166,3 +168,63 @@ function deleteDevice($id, $serial) echo "Type is not a USB device"; } } + +/** + * Return a sorted list with all vendor / id information as JSON + */ +function getIDList() { + $usblist = array(); + + // TODO: Online version takes a bit longer to load but is more accurate. (2018 instead of 2015) + //$lines = file('http://www.linux-usb.org/usb.ids'); + $lines = file('/var/lib/usbutils/usb.ids'); + $currentVendor = ''; + $br = false; + foreach ($lines as $line) { + if ($line === "\n" && $br) { + // If its the first part (vid - name / pid - name) skip comments else break because the part we needed is finished. + break; + } else if ($line[0] === '#' || $line === "\n") { + continue; + } else if (!ctype_space($line[0])) { + $br = true; + // It's a vendor id. + $l = explode(' ', preg_replace('~[\r\n\t]+~', '', $line)); + $vendor = array(); + $vendor['name'] = $l[1]; + $vendor['products'] = array(); + $currentVendor = $l[0]; + $usblist[$l[0]] = $vendor; + } else if (!ctype_space($line[1])) { + // It's a product id. + $l = explode(' ', preg_replace('~[\r\n\t]+~', '', $line)); + $usblist[$currentVendor]['products'][$l[0]] = $l[1]; + } else { + // It's a interface + continue; + } + } + + $sortVendorName = []; + $sortVendorId = []; + foreach ($usblist as $key => $value) { + $sortVendorName[] = (string)$value['name']; + $sortVendorId[] = (string)$key; + + $sortProductName = []; + $sortProductId = []; + $tmp = $value['products']; + $keys2 = array_keys($tmp); + foreach ($tmp as $k => $v) { + $sortProductName[] = $v; + $sortProductId[] = $k; + } + + array_multisort($sortProductName, SORT_ASC, $sortProductId, SORT_ASC, $tmp, $keys2); + $usblist[$key]['products'] = array_combine($keys2, $tmp); + } + + $keys = array_keys($usblist); + array_multisort($sortVendorName, SORT_ASC, $sortVendorId, SORT_ASC, $usblist, $keys); + return json_encode(array_combine($keys, $usblist)); +} |