mirror of
https://github.com/itflow-org/itflow
synced 2026-11-27 22:59:00 +00:00
API stage 3a, network IPs, asset interfaces, license seats and keys, notes and racks: a network's documented IP addresses can be read, added one at a time or as a run of up to 256, changed and deleted with the address plan's checks; an asset's interfaces can be read with what each is plugged into, added, changed, made primary, plugged in or unplugged and deleted with the interface window's checks; a license's seats can be set, it can be renewed by a term or to a date, and its keys read, added, handed to the client's devices and users and deleted; asset and contact notes can be read, added with one of your note types, archived, unarchived and deleted once archived, through one shared body in notes_body.php; racks can be read with what is mounted in them, made, changed, archived, unarchived and deleted once archived, and items mounted, moved and taken out with the rack page's checks; new categoryPickedName() checks a posted name for a category kind held by name; enforce_api_rbac.php maps network_ips, asset_interfaces, software_keys, asset_notes, racks and rack_units to Support and contact_notes to Clients, with seats and renew at Modify; software/read.php looked up software_key from the wrong parameter and now finds a license by its own key or any of its keys; 26.11.9 changelog entries
This commit is contained in:
@@ -38,6 +38,11 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
|
||||
- API: projects can be read - one with its tickets and its progress and time - started for a client or as internal work, with a project template's tickets, changed, completed once none of its tickets is open, reopened, archived, unarchived and deleted once archived. A template's tickets can be added to an open project. A ticket goes on or off a project with tickets/create and tickets/update.
|
||||
- API: a ticket's tasks can be read, added, changed, marked done or not done, and deleted, and an approval can be asked of the client or your team, given by the key's agent, and taken off. A task waiting on an approval, or whose approval was declined, can't be marked done.
|
||||
- API: recurring tickets can be read, made - from a ticket template, with its fill-ins answered and its tasks for those answers - changed, paused and resumed, raised now and deleted.
|
||||
- API: a network's documented IP addresses can be read, added - one at a time or a run of up to 256 - changed and deleted, with the same checks as the address plan: inside the subnet, not documented twice, and a DHCP reservation with its MAC.
|
||||
- API: an asset's interfaces can be read - with what each is plugged into - added, changed, made the primary, plugged in or unplugged, and deleted, with the same checks as the interface window.
|
||||
- API: a license's keys can be read, added, handed to the client's devices and users, and deleted, its seats can be set, and it can be renewed by a term or to a date. Reading licenses by software_key finds a license by any of its keys.
|
||||
- API: asset and contact notes can be read, added with one of your note types, archived, unarchived and deleted once archived.
|
||||
- API: racks can be read - one with what is mounted in it - made, changed, archived, unarchived and deleted once archived, and items mounted, moved and taken out, with the same checks as the rack page: an asset in one rack at a time, nothing else on that face, and a rack no shorter than what is in it. Rack photos are still added in ITFlow.
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
@@ -65,6 +70,10 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
|
||||
- API endpoints: `payments/` `read`, `create`, `update`, `delete`, `send` (the receipt); `projects/` `read`, `create`, `update`, `complete`, `reopen`, `archive`, `unarchive`, `delete`, `apply_template`; `tasks/` `read`, `create`, `update`, `complete`, `delete`; `task_approvals/` `create`, `approve`, `delete`; `recurring_tickets/` `read`, `create`, `update`, `status`, `raise`, `delete`. They call `billingMoneyCan()`, `invoiceCanPay()`, `updateInvoiceStatusFromPayments()`, `reconcileCheckChange()`, `reconcileCheckDelete()`, `transferDepositRelease()`, `billingPaymentSend()`, `projectNextNumber()`, `projectApplyTemplate()`, `projectComplete()`, `projectReopen()`, `projectArchive()`, `projectRestore()`, `projectDelete()`, `projectNotifyManager()`, `projectStats()`, `ticketMailTaskApproval()`, `recurringTicketRunNow()`, `recurringTicketNextRun()`, `ticketFillInAnswers()`, `ticketTemplateTaskApplies()` and `recurringTicketMailAssigned()`; the task and recurring ticket handlers work inline, and their endpoints do the same steps. Payment and recurring ticket fields go through `payments/payment_model.php` and `recurring_tickets/recurring_ticket_model.php`, project fields through `projects/project_model.php`.
|
||||
- `enforce_api_rbac.php` maps `payments` to `module_financial` (the endpoints also check Sales at Read through `billingMoneyCan()`), and `projects`, `tasks`, `task_approvals` and `recurring_tickets` to `module_support`, with `complete`, `approve`, `raise` and `apply_template` at level 2.
|
||||
- `tickets/ticket_model.php` takes `ticket_project_id` - the client's project or an internal one (`ticketClientOwnedId()`), 0 to take it off - and turns down any other id rather than dropping it; `tickets/create` and `tickets/update` write it and the ticket's history.
|
||||
- API endpoints: `network_ips/` `read`, `create`, `update`, `delete`; `asset_interfaces/` `read`, `create`, `update`, `delete`; `software/` `seats`, `renew`; `software_keys/` `read`, `create`, `update`, `delete`; `asset_notes/` and `contact_notes/` `read`, `create`, `archive`, `unarchive`, `delete`; `racks/` `read`, `create`, `update`, `archive`, `unarchive`, `delete`; `rack_units/` `create`, `update`, `delete`. They call `networkIpFieldsSet()`, `checkIpForNetwork()`, `expandIpSuffix()`, `interfaceFormSet()`, `interfaceConnectTarget()`, `interfaceConnect()`, `interfaceMakePrimary()`, `interfaceLinks()`, `softwareSyncSeats()`, `softwareSeatHolders()`, `softwareRenewalExtended()`, `softwareAddKeys()`, `softwareKeys()`, `rackUnitConflict()`, `rackLoadUnits()` and `rackDeletePhoto()`; the note, key, rack and rack item handlers work inline, and their endpoints do the same steps. An update sends `interfaceFormSet()` and `networkIpFieldsSet()` the record's own values for the fields it leaves out. Asset and contact notes are one body, `api/v1/notes_body.php`, included with `$note_kind` and `$note_op`, as the billing lines are.
|
||||
- `categoryPickedName($kind, $name, $keep = '')` (`functions/categories.php`) checks a name posted for a category kind held by name - a rack type, an interface type, a note type - against what the picker offers, in any case, and gives the category's own spelling, '' for blank, or null when it is none of them; while a kind has no categories set up, any name is taken.
|
||||
- `enforce_api_rbac.php` maps `network_ips`, `asset_interfaces`, `software_keys`, `asset_notes`, `racks` and `rack_units` to `module_support` and `contact_notes` to `module_client`, with `seats` and `renew` at level 2. Deleting an IP, interface, key or rack item needs Full, as every API delete does, where the page lets Modify do it.
|
||||
- `software/read.php` read `software_key` from the wrong parameter (`software_license`), so a lookup by key matched licenses with no key; it now matches the license's own key or one in `software_keys`.
|
||||
- `php_flag engine off` in `uploads/.htaccess`, its `backups`, `expenses`, `tmp` and `canned_responses` folders and `scripts/.htaccess` is wrapped in `<IfModule mod_php.c>`. Apache only knows `php_flag` under mod_php - with PHP-FPM through `mod_proxy_fcgi` it stops on the line and answers every request in the folder with a 500. Under FPM, PHP still can't run there: `uploads/.htaccess` denies `.php`, `.phtml`, `.phar` and the rest by name, and the other five deny everything.
|
||||
|
||||
## [26.11.8]
|
||||
|
||||
64
api/v1/asset_interfaces/create.php
Normal file
64
api/v1/asset_interfaces/create.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Asset Interfaces - Create
|
||||
* POST /api/v1/asset_interfaces/create.php
|
||||
*
|
||||
* Add a port or network card to an asset, as Add Interface does - a name the
|
||||
* asset doesn't have yet, an address inside its network, a MAC that is one, a
|
||||
* network of the asset's client. The asset's first interface is its primary.
|
||||
*
|
||||
* Parameters (POST, JSON body):
|
||||
* api_key required - Your API key
|
||||
* asset_id required - The asset
|
||||
* client_id the asset's client - needed when the key's user is limited to certain clients
|
||||
* interface_name required - Its name or port number
|
||||
* interface_type optional - An interface type (Admin > Categories)
|
||||
* interface_ip optional - An IP address, DHCP, or empty for none
|
||||
* interface_network_id optional - One of the client's networks
|
||||
* interface_mac, interface_nat_ip, interface_ipv6, interface_vlans, interface_description, interface_notes optional
|
||||
* interface_primary optional - 1: the asset's primary interface
|
||||
* connected_to optional - The interface it is plugged into, on another of the client's assets
|
||||
*
|
||||
* Answer: insert_id, and warnings - such as the address being on another device too.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$insert_id = false;
|
||||
|
||||
$asset_id = intval($_POST['asset_id'] ?? 0);
|
||||
$asset = $asset_id ? mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT asset_id, asset_name, asset_type, asset_client_id FROM assets WHERE asset_id = $asset_id "
|
||||
. clientScopeSql('asset_client_id') . ($client_id ? " AND asset_client_id = $client_id" : '') . " LIMIT 1")) : null;
|
||||
|
||||
if ($asset && apiUserCanAccessClient($asset['asset_client_id'])) {
|
||||
|
||||
$client_id = intval($asset['asset_client_id']);
|
||||
|
||||
require_once 'interface_model.php';
|
||||
|
||||
mysqli_query($mysqli, "INSERT INTO asset_interfaces SET $interface_set, interface_asset_id = $asset_id");
|
||||
$interface_id = intval(mysqli_insert_id($mysqli));
|
||||
|
||||
if ($interface_id) {
|
||||
$insert_id = $interface_id;
|
||||
|
||||
// The first one is the primary whether it is asked for or not
|
||||
$has_primary = mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM asset_interfaces WHERE interface_asset_id = $asset_id AND interface_primary = 1 AND interface_archived_at IS NULL"));
|
||||
if (!empty($_POST['interface_primary']) || !$has_primary) {
|
||||
interfaceMakePrimary($interface_id, $asset_id);
|
||||
}
|
||||
if ($interface_connect) {
|
||||
interfaceConnect($interface_id, $interface_connect);
|
||||
}
|
||||
|
||||
$return_arr['warnings'] = $interface_warnings;
|
||||
|
||||
$label = escapeSql(trim($interface_post['name']) . ' on asset ' . $asset['asset_name']);
|
||||
logAudit("Asset Interface", "Create", "Created interface $label via API ($api_key_name)", $client_id, $asset_id);
|
||||
logAudit("API", "Success", "Created interface $label via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../create_output.php';
|
||||
39
api/v1/asset_interfaces/delete.php
Normal file
39
api/v1/asset_interfaces/delete.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Asset Interfaces - Delete
|
||||
* POST /api/v1/asset_interfaces/delete.php
|
||||
*
|
||||
* Delete an interface, as Delete does on it - unplugged from whatever it was
|
||||
* connected to.
|
||||
*
|
||||
* Parameters (POST, JSON body): api_key, interface_id, client_id (the asset's client -
|
||||
* needed when the key's user is limited to certain clients)
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$delete_count = false;
|
||||
|
||||
$row = !empty($_POST['interface_id']) ? interfaceRow($_POST['interface_id']) : null;
|
||||
if ($row && (!apiUserCanAccessClient($row['asset_client_id']) || ($client_id && intval($row['asset_client_id']) !== $client_id))) {
|
||||
$row = null;
|
||||
}
|
||||
|
||||
if ($row) {
|
||||
|
||||
$client_id = intval($row['asset_client_id']);
|
||||
|
||||
// Its connection goes with it (ON DELETE CASCADE)
|
||||
mysqli_query($mysqli, "DELETE FROM asset_interfaces WHERE interface_id = " . intval($row['interface_id']));
|
||||
$delete_count = mysqli_affected_rows($mysqli);
|
||||
|
||||
if ($delete_count > 0) {
|
||||
$label = escapeSql($row['interface_name'] . ' from asset ' . $row['asset_name']);
|
||||
logAudit("Asset Interface", "Delete", "Deleted interface $label via API ($api_key_name)", $client_id, intval($row['asset_id']));
|
||||
logAudit("API", "Success", "Deleted interface $label via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../delete_output.php';
|
||||
64
api/v1/asset_interfaces/interface_model.php
Normal file
64
api/v1/asset_interfaces/interface_model.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
// An interface's fields from POST, as interfaceFormSet() takes them - with what the
|
||||
// interface has now ($interface_row) for those not sent, on an update. $asset is its
|
||||
// asset (asset_id, asset_name, asset_client_id). Sets $interface_set (the SET clause)
|
||||
// and $interface_warnings, or turns the request down with 400 and why.
|
||||
|
||||
if (!function_exists('apiDeny')) {
|
||||
http_response_code(404);
|
||||
exit();
|
||||
}
|
||||
|
||||
$interface_row = $interface_row ?? null;
|
||||
|
||||
$interface_refuse = function ($message) use (&$return_arr) {
|
||||
http_response_code(400);
|
||||
$message = rtrim(html_entity_decode(strip_tags($message), ENT_QUOTES | ENT_HTML5, 'UTF-8'));
|
||||
$return_arr['success'] = "False";
|
||||
$return_arr['message'] = preg_match('/[.!?]$/', $message) ? $message : "$message.";
|
||||
echo json_encode($return_arr);
|
||||
exit();
|
||||
};
|
||||
|
||||
$interface_post = [];
|
||||
foreach (['interface_name' => 'name', 'interface_description' => 'description', 'interface_type' => 'type', 'interface_mac' => 'mac', 'interface_ip' => 'ip',
|
||||
'interface_nat_ip' => 'nat_ip', 'interface_ipv6' => 'ipv6', 'interface_vlans' => 'vlans', 'interface_notes' => 'notes', 'interface_network_id' => 'network_id'] as $field => $key) {
|
||||
$interface_post[$key] = array_key_exists($field, $_POST) ? (is_scalar($_POST[$field]) ? (string) $_POST[$field] : '') : (string) ($interface_row[$field] ?? '');
|
||||
}
|
||||
// The address: an IP, DHCP, or empty for none
|
||||
$interface_post['ip_mode'] = strtoupper(trim($interface_post['ip'])) === 'DHCP' ? 'dhcp' : (trim($interface_post['ip']) === '' ? 'none' : 'static');
|
||||
|
||||
// A type from Admin > Categories, as the picker offers - the one it has stays allowed
|
||||
if (array_key_exists('interface_type', $_POST)) {
|
||||
$type = categoryPickedName('network_interface', $interface_post['type'], (string) ($interface_row['interface_type'] ?? ''));
|
||||
if ($type === null) {
|
||||
$interface_refuse('interface_type is one of: ' . implode(', ', array_keys(categoryPicker('network_interface'))) . ' - or empty for none.');
|
||||
}
|
||||
$interface_post['type'] = $type;
|
||||
}
|
||||
|
||||
// The network has to be the asset's client's
|
||||
if (array_key_exists('interface_network_id', $_POST) && intval($_POST['interface_network_id'])
|
||||
&& !assetClientOwnedId('network', $_POST['interface_network_id'], $asset['asset_client_id'])) {
|
||||
$interface_refuse("interface_network_id isn't one of the client's networks.");
|
||||
}
|
||||
|
||||
$error = '';
|
||||
$interface_warnings = [];
|
||||
$interface_set = interfaceFormSet($interface_post, $asset, intval($interface_row['interface_id'] ?? 0), $error, $interface_warnings);
|
||||
if ($interface_set === '') {
|
||||
$interface_refuse($error);
|
||||
}
|
||||
$interface_warnings = array_map(function ($warning) {
|
||||
return html_entity_decode(strip_tags($warning), ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||
}, $interface_warnings);
|
||||
|
||||
// What it is plugged into - one of the client's other assets' interfaces
|
||||
$interface_connect = null;
|
||||
if (array_key_exists('connected_to', $_POST)) {
|
||||
$interface_connect = interfaceConnectTarget($_POST['connected_to'], $asset['asset_client_id'], $asset['asset_id']);
|
||||
if (intval($_POST['connected_to']) && !$interface_connect) {
|
||||
$interface_refuse("connected_to is an interface on another of the client's assets - not archived.");
|
||||
}
|
||||
}
|
||||
54
api/v1/asset_interfaces/read.php
Normal file
54
api/v1/asset_interfaces/read.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Asset Interfaces - Read
|
||||
* GET /api/v1/asset_interfaces/read.php
|
||||
*
|
||||
* An asset's ports and network cards, or one - each with its network's name and
|
||||
* what it is plugged into (connected_to: the interface id at the other end, with
|
||||
* its asset), or null.
|
||||
*
|
||||
* asset_id This asset's interfaces - this or interface_id is needed
|
||||
* interface_id Just this interface
|
||||
* client_id optional - Only if the asset is this client's
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_get_method.php';
|
||||
|
||||
$asset_id = intval($_GET['asset_id'] ?? 0);
|
||||
$interface_id = intval($_GET['interface_id'] ?? 0);
|
||||
|
||||
if (!$asset_id && !$interface_id) {
|
||||
http_response_code(400);
|
||||
$return_arr['success'] = "False";
|
||||
$return_arr['message'] = "asset_interfaces/read.php needs asset_id or interface_id.";
|
||||
echo json_encode($return_arr);
|
||||
exit();
|
||||
}
|
||||
|
||||
$sql = mysqli_query($mysqli, "SELECT asset_interfaces.*, asset_name, asset_client_id, network_name, network
|
||||
FROM asset_interfaces
|
||||
JOIN assets ON asset_id = interface_asset_id
|
||||
LEFT JOIN networks ON network_id = interface_network_id AND interface_network_id > 0
|
||||
WHERE " . ($interface_id ? "interface_id = $interface_id" : "interface_asset_id = $asset_id") . " " . apiClientScopeSql('asset_client_id') . "
|
||||
ORDER BY interface_primary DESC, interface_name ASC");
|
||||
|
||||
$rows = [];
|
||||
while ($sql && ($row = mysqli_fetch_assoc($sql))) {
|
||||
$rows[intval($row['interface_id'])] = $row;
|
||||
}
|
||||
|
||||
if ($rows) {
|
||||
$links = interfaceLinks(array_keys($rows));
|
||||
foreach ($rows as $id => $row) {
|
||||
$link = $links[$id] ?? null;
|
||||
$rows[$id]['connected_to'] = $link ? ['interface_id' => intval($link['interface_id']), 'interface_name' => $link['interface_name'], 'asset_id' => intval($link['asset_id']), 'asset_name' => $link['asset_name']] : null;
|
||||
}
|
||||
echo json_encode(['success' => 'True', 'count' => count($rows), 'data' => array_values($rows)]);
|
||||
exit();
|
||||
}
|
||||
|
||||
$sql = false;
|
||||
|
||||
// Output
|
||||
require_once "../read_output.php";
|
||||
63
api/v1/asset_interfaces/update.php
Normal file
63
api/v1/asset_interfaces/update.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Asset Interfaces - Update
|
||||
* POST /api/v1/asset_interfaces/update.php
|
||||
*
|
||||
* Change an interface, as Edit does - only what is sent, with the same checks
|
||||
* as adding one. interface_primary 1 makes it the asset's primary; connected_to
|
||||
* plugs it into another interface, or 0 unplugs it.
|
||||
*
|
||||
* Parameters (POST, JSON body):
|
||||
* api_key required - Your API key
|
||||
* interface_id required - The interface
|
||||
* client_id the asset's client - needed when the key's user is limited to certain clients
|
||||
* the fields of asset_interfaces/create.php optional
|
||||
*
|
||||
* Answer: warnings.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$update_count = false;
|
||||
|
||||
$interface_row = !empty($_POST['interface_id']) ? interfaceRow($_POST['interface_id']) : null;
|
||||
if ($interface_row && (!apiUserCanAccessClient($interface_row['asset_client_id']) || ($client_id && intval($interface_row['asset_client_id']) !== $client_id))) {
|
||||
$interface_row = null;
|
||||
}
|
||||
|
||||
if ($interface_row) {
|
||||
|
||||
$interface_id = intval($interface_row['interface_id']);
|
||||
$asset_id = intval($interface_row['asset_id']);
|
||||
$client_id = intval($interface_row['asset_client_id']);
|
||||
$asset = ['asset_id' => $asset_id, 'asset_name' => $interface_row['asset_name'], 'asset_type' => $interface_row['asset_type'], 'asset_client_id' => $client_id];
|
||||
|
||||
require_once 'interface_model.php';
|
||||
|
||||
if (mysqli_query($mysqli, "UPDATE asset_interfaces SET $interface_set WHERE interface_id = $interface_id")) {
|
||||
$update_count = 1;
|
||||
|
||||
if (!empty($_POST['interface_primary'])) {
|
||||
interfaceMakePrimary($interface_id, $asset_id);
|
||||
}
|
||||
// Moved, or let go when 0
|
||||
if ($interface_connect !== null && $interface_connect !== intval($interface_row['link']['interface_id'] ?? 0)) {
|
||||
if (!empty($interface_row['link'])) {
|
||||
interfaceConnect($interface_id, 0);
|
||||
}
|
||||
if ($interface_connect) {
|
||||
interfaceConnect($interface_id, $interface_connect);
|
||||
}
|
||||
}
|
||||
|
||||
$return_arr['warnings'] = $interface_warnings;
|
||||
|
||||
$label = escapeSql(trim($interface_post['name']) . ' on asset ' . $interface_row['asset_name']);
|
||||
logAudit("Asset Interface", "Edit", "Edited interface $label via API ($api_key_name)", $client_id, $asset_id);
|
||||
logAudit("API", "Success", "Edited interface $label via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../update_output.php';
|
||||
14
api/v1/asset_notes/archive.php
Normal file
14
api/v1/asset_notes/archive.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Asset Notes - Archive
|
||||
* POST /api/v1/asset_notes/archive.php
|
||||
*
|
||||
* See api/v1/notes_body.php.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$note_kind = 'asset';
|
||||
$note_op = 'archive';
|
||||
require_once '../notes_body.php';
|
||||
14
api/v1/asset_notes/create.php
Normal file
14
api/v1/asset_notes/create.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Asset Notes - Create
|
||||
* POST /api/v1/asset_notes/create.php
|
||||
*
|
||||
* See api/v1/notes_body.php.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$note_kind = 'asset';
|
||||
$note_op = 'create';
|
||||
require_once '../notes_body.php';
|
||||
14
api/v1/asset_notes/delete.php
Normal file
14
api/v1/asset_notes/delete.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Asset Notes - Delete
|
||||
* POST /api/v1/asset_notes/delete.php
|
||||
*
|
||||
* See api/v1/notes_body.php.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$note_kind = 'asset';
|
||||
$note_op = 'delete';
|
||||
require_once '../notes_body.php';
|
||||
14
api/v1/asset_notes/read.php
Normal file
14
api/v1/asset_notes/read.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Asset Notes - Read
|
||||
* GET /api/v1/asset_notes/read.php
|
||||
*
|
||||
* See api/v1/notes_body.php.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_get_method.php';
|
||||
|
||||
$note_kind = 'asset';
|
||||
$note_op = 'read';
|
||||
require_once '../notes_body.php';
|
||||
14
api/v1/asset_notes/unarchive.php
Normal file
14
api/v1/asset_notes/unarchive.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Asset Notes - Unarchive
|
||||
* POST /api/v1/asset_notes/unarchive.php
|
||||
*
|
||||
* See api/v1/notes_body.php.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$note_kind = 'asset';
|
||||
$note_op = 'unarchive';
|
||||
require_once '../notes_body.php';
|
||||
14
api/v1/contact_notes/archive.php
Normal file
14
api/v1/contact_notes/archive.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Contact Notes - Archive
|
||||
* POST /api/v1/contact_notes/archive.php
|
||||
*
|
||||
* See api/v1/notes_body.php.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$note_kind = 'contact';
|
||||
$note_op = 'archive';
|
||||
require_once '../notes_body.php';
|
||||
14
api/v1/contact_notes/create.php
Normal file
14
api/v1/contact_notes/create.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Contact Notes - Create
|
||||
* POST /api/v1/contact_notes/create.php
|
||||
*
|
||||
* See api/v1/notes_body.php.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$note_kind = 'contact';
|
||||
$note_op = 'create';
|
||||
require_once '../notes_body.php';
|
||||
14
api/v1/contact_notes/delete.php
Normal file
14
api/v1/contact_notes/delete.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Contact Notes - Delete
|
||||
* POST /api/v1/contact_notes/delete.php
|
||||
*
|
||||
* See api/v1/notes_body.php.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$note_kind = 'contact';
|
||||
$note_op = 'delete';
|
||||
require_once '../notes_body.php';
|
||||
14
api/v1/contact_notes/read.php
Normal file
14
api/v1/contact_notes/read.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Contact Notes - Read
|
||||
* GET /api/v1/contact_notes/read.php
|
||||
*
|
||||
* See api/v1/notes_body.php.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_get_method.php';
|
||||
|
||||
$note_kind = 'contact';
|
||||
$note_op = 'read';
|
||||
require_once '../notes_body.php';
|
||||
14
api/v1/contact_notes/unarchive.php
Normal file
14
api/v1/contact_notes/unarchive.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Contact Notes - Unarchive
|
||||
* POST /api/v1/contact_notes/unarchive.php
|
||||
*
|
||||
* See api/v1/notes_body.php.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$note_kind = 'contact';
|
||||
$note_op = 'unarchive';
|
||||
require_once '../notes_body.php';
|
||||
@@ -145,8 +145,14 @@ $resource_module = [
|
||||
'documents' => 'module_support',
|
||||
'domains' => 'module_support',
|
||||
'networks' => 'module_support',
|
||||
'network_ips' => 'module_support',
|
||||
'asset_interfaces' => 'module_support',
|
||||
'asset_notes' => 'module_support',
|
||||
'racks' => 'module_support',
|
||||
'rack_units' => 'module_support',
|
||||
'services' => 'module_support',
|
||||
'software' => 'module_support',
|
||||
'software_keys' => 'module_support',
|
||||
'tickets' => 'module_support',
|
||||
'ticket_replies' => 'module_support',
|
||||
'technicians' => 'module_support',
|
||||
@@ -156,6 +162,7 @@ $resource_module = [
|
||||
'recurring_tickets' => 'module_support',
|
||||
'clients' => 'module_client',
|
||||
'contacts' => 'module_client',
|
||||
'contact_notes' => 'module_client',
|
||||
'locations' => 'module_client',
|
||||
'portal_invites' => 'module_client',
|
||||
'vendors' => 'module_client',
|
||||
@@ -205,6 +212,8 @@ $operation_level = [
|
||||
'approve.php' => 2,
|
||||
'raise.php' => 2,
|
||||
'apply_template.php' => 2,
|
||||
'seats.php' => 2,
|
||||
'renew.php' => 2,
|
||||
];
|
||||
|
||||
if (!isset($resource_module[$resource])) {
|
||||
|
||||
94
api/v1/network_ips/create.php
Normal file
94
api/v1/network_ips/create.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Network IPs - Create
|
||||
* POST /api/v1/network_ips/create.php
|
||||
*
|
||||
* Document an address on a network, as Add IP does - inside the subnet and not
|
||||
* documented on it already (checkIpForNetwork()). The host part alone (20 in a /24) is
|
||||
* filled out from the subnet. With ip_range_end, every address from ip_address
|
||||
* to it - up to 256 - skipping those outside the subnet or documented already.
|
||||
* A DHCP reservation is for one device, with its MAC.
|
||||
*
|
||||
* Parameters (POST, JSON body):
|
||||
* api_key required - Your API key
|
||||
* network_id required - The network
|
||||
* client_id the network's client - needed when the key's user is limited to certain clients
|
||||
* ip_address required - The address
|
||||
* ip_range_end optional - The last address of a run
|
||||
* ip_type optional - Static (the default), Reservation, Reserved or Virtual
|
||||
* ip_hostname, ip_mac, ip_description optional
|
||||
*
|
||||
* Answer: insert_id (the first, for a run), and added and skipped.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$insert_id = false;
|
||||
|
||||
unset($_POST['ip_id']);
|
||||
require_once 'network_ip_find.php';
|
||||
|
||||
if ($network) {
|
||||
|
||||
$network_id = intval($network['network_id']);
|
||||
$ip_address = trim((string) (is_scalar($_POST['ip_address'] ?? '') ? ($_POST['ip_address'] ?? '') : ''));
|
||||
$range_end = trim((string) (is_scalar($_POST['ip_range_end'] ?? '') ? ($_POST['ip_range_end'] ?? '') : ''));
|
||||
$post = $ip_post();
|
||||
|
||||
if ($range_end !== '' && $post['type'] === 'Reservation') {
|
||||
$ip_refuse('A DHCP reservation is for one device - add them one at a time.');
|
||||
}
|
||||
$error = '';
|
||||
$fields = networkIpFieldsSet($post, $error);
|
||||
if ($fields === '') {
|
||||
$ip_refuse($error);
|
||||
}
|
||||
|
||||
$added = [];
|
||||
$skipped = 0;
|
||||
if ($range_end !== '') {
|
||||
$first = normalizeIpAddress(expandIpSuffix($ip_address, $network['network']));
|
||||
$last = normalizeIpAddress(expandIpSuffix($range_end, $network['network']));
|
||||
$first_int = $first === false ? null : ipv4ToInt($first);
|
||||
$last_int = $last === false ? null : ipv4ToInt($last);
|
||||
if ($first_int === null || $last_int === null || $first_int > $last_int) {
|
||||
$ip_refuse('A range is two IPv4 addresses, the lower first.');
|
||||
}
|
||||
if ($last_int - $first_int >= 256) {
|
||||
$ip_refuse('A range is up to 256 addresses at a time.');
|
||||
}
|
||||
for ($int = $first_int; $int <= $last_int; $int++) {
|
||||
$ip = intToIpv4($int);
|
||||
if (checkIpForNetwork($ip, $network_id) !== '') {
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
mysqli_query($mysqli, "INSERT INTO network_ips SET ip_address = '" . escapeSql($ip) . "', $fields, ip_network_id = $network_id");
|
||||
$added[] = intval(mysqli_insert_id($mysqli));
|
||||
}
|
||||
if (!$added) {
|
||||
$ip_refuse("None of $first to $last could be documented - they are outside the subnet or documented already.");
|
||||
}
|
||||
$what = "$first - $last";
|
||||
} else {
|
||||
$ip_error = checkIpForNetwork($ip_address, $network_id);
|
||||
if ($ip_error) {
|
||||
$ip_refuse($ip_error);
|
||||
}
|
||||
mysqli_query($mysqli, "INSERT INTO network_ips SET ip_address = '" . escapeSql($ip_address) . "', $fields, ip_network_id = $network_id");
|
||||
$added[] = intval(mysqli_insert_id($mysqli));
|
||||
$what = $ip_address;
|
||||
}
|
||||
|
||||
$insert_id = $added[0] ?: false;
|
||||
$return_arr['added'] = count($added);
|
||||
$return_arr['skipped'] = $skipped;
|
||||
|
||||
$label = escapeSql("$what on network " . $network['network_name']);
|
||||
logAudit("IP Address", "Create", "Documented " . count($added) . " address(es), $label via API ($api_key_name)", $client_id, $network_id);
|
||||
logAudit("API", "Success", "Documented $label via API ($api_key_name)", $client_id);
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../create_output.php';
|
||||
33
api/v1/network_ips/delete.php
Normal file
33
api/v1/network_ips/delete.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Network IPs - Delete
|
||||
* POST /api/v1/network_ips/delete.php
|
||||
*
|
||||
* Take an address off its network's address plan, as Delete does on it.
|
||||
*
|
||||
* Parameters (POST, JSON body): api_key, ip_id, client_id (the network's client -
|
||||
* needed when the key's user is limited to certain clients)
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$delete_count = false;
|
||||
|
||||
unset($_POST['network_id']);
|
||||
require_once 'network_ip_find.php';
|
||||
|
||||
if ($ip_row) {
|
||||
|
||||
mysqli_query($mysqli, "DELETE FROM network_ips WHERE ip_id = " . intval($ip_row['ip_id']));
|
||||
$delete_count = mysqli_affected_rows($mysqli);
|
||||
|
||||
if ($delete_count > 0) {
|
||||
$label = escapeSql($ip_row['ip_address'] . ' from network ' . $ip_row['network_name']);
|
||||
logAudit("IP Address", "Delete", "Deleted IP $label via API ($api_key_name)", $client_id);
|
||||
logAudit("API", "Success", "Deleted IP $label via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../delete_output.php';
|
||||
45
api/v1/network_ips/network_ip_find.php
Normal file
45
api/v1/network_ips/network_ip_find.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
// The network a request names (network_id) or the documented address (ip_id) - of a
|
||||
// client the key's user can reach, and client_id's when it is sent - as $network
|
||||
// and $ip_row (null when not found). $client_id becomes the network's client.
|
||||
|
||||
if (!function_exists('apiDeny')) {
|
||||
http_response_code(404);
|
||||
exit();
|
||||
}
|
||||
|
||||
$ip_row = !empty($_POST['ip_id']) ? networkIpRow($_POST['ip_id']) : null;
|
||||
$network = $ip_row ? networkRow($ip_row['ip_network_id']) : (!empty($_POST['network_id']) ? networkRow($_POST['network_id']) : null);
|
||||
if ($network && (!apiUserCanAccessClient($network['network_client_id']) || ($client_id && intval($network['network_client_id']) !== $client_id))) {
|
||||
$network = null;
|
||||
$ip_row = null;
|
||||
}
|
||||
if ($network) {
|
||||
$client_id = intval($network['network_client_id']);
|
||||
}
|
||||
|
||||
// What the form posts for an address's other fields, from the API's names - with
|
||||
// what the address has now for those not sent, on an update
|
||||
$ip_post = function ($row = null) {
|
||||
$post = [];
|
||||
foreach (['ip_type' => 'type', 'ip_hostname' => 'hostname', 'ip_mac' => 'mac', 'ip_description' => 'description'] as $field => $key) {
|
||||
$post[$key] = array_key_exists($field, $_POST) ? (is_scalar($_POST[$field]) ? (string) $_POST[$field] : '') : (string) ($row[$field] ?? '');
|
||||
}
|
||||
// A reservation documented without its MAC (an import) can still be changed, until a MAC is sent
|
||||
$post['allow_no_mac'] = $row && $row['ip_type'] === 'Reservation' && (string) $row['ip_mac'] === '' && !array_key_exists('ip_mac', $_POST);
|
||||
return $post;
|
||||
};
|
||||
|
||||
$ip_refuse = function ($message) use (&$return_arr) {
|
||||
http_response_code(400);
|
||||
$return_arr['success'] = "False";
|
||||
$message = rtrim(html_entity_decode(strip_tags($message), ENT_QUOTES | ENT_HTML5, 'UTF-8'));
|
||||
$return_arr['message'] = preg_match('/[.!?]$/', $message) ? $message : "$message.";
|
||||
echo json_encode($return_arr);
|
||||
exit();
|
||||
};
|
||||
|
||||
if (array_key_exists('ip_type', $_POST) && !isset(NETWORK_IP_TYPES[$_POST['ip_type']])) {
|
||||
$ip_refuse('ip_type is one of: ' . implode(', ', array_keys(NETWORK_IP_TYPES)) . '.');
|
||||
}
|
||||
35
api/v1/network_ips/read.php
Normal file
35
api/v1/network_ips/read.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Network IPs - Read
|
||||
* GET /api/v1/network_ips/read.php
|
||||
*
|
||||
* The addresses documented on a network, lowest first - fixed ones, DHCP
|
||||
* reservations (with the MAC they are for), ones held back and floating ones.
|
||||
*
|
||||
* network_id This network's addresses - this or ip_id is needed
|
||||
* ip_id Just this address
|
||||
* client_id optional - Only if the network is this client's
|
||||
* limit, offset optional - Paging
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_get_method.php';
|
||||
|
||||
$network_id = intval($_GET['network_id'] ?? 0);
|
||||
$ip_id = intval($_GET['ip_id'] ?? 0);
|
||||
|
||||
if (!$network_id && !$ip_id) {
|
||||
http_response_code(400);
|
||||
$return_arr['success'] = "False";
|
||||
$return_arr['message'] = "network_ips/read.php needs network_id or ip_id.";
|
||||
echo json_encode($return_arr);
|
||||
exit();
|
||||
}
|
||||
|
||||
$sql = mysqli_query($mysqli, "SELECT network_ips.*, network_name, network, network_client_id
|
||||
FROM network_ips JOIN networks ON network_id = ip_network_id
|
||||
WHERE " . ($ip_id ? "ip_id = $ip_id" : "ip_network_id = $network_id") . " " . apiClientScopeSql('network_client_id') . "
|
||||
ORDER BY INET_ATON(ip_address) IS NULL, INET_ATON(ip_address), ip_address LIMIT $limit OFFSET $offset");
|
||||
|
||||
// Output
|
||||
require_once "../read_output.php";
|
||||
51
api/v1/network_ips/update.php
Normal file
51
api/v1/network_ips/update.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Network IPs - Update
|
||||
* POST /api/v1/network_ips/update.php
|
||||
*
|
||||
* Change a documented address, as Edit does - only what is sent, with the same
|
||||
* checks as documenting it.
|
||||
*
|
||||
* Parameters (POST, JSON body):
|
||||
* api_key required - Your API key
|
||||
* ip_id required - The address
|
||||
* client_id the network's client - needed when the key's user is limited to certain clients
|
||||
* ip_address, ip_type, ip_hostname, ip_mac, ip_description optional
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$update_count = false;
|
||||
|
||||
unset($_POST['network_id']);
|
||||
require_once 'network_ip_find.php';
|
||||
|
||||
if ($ip_row) {
|
||||
|
||||
$ip_id = intval($ip_row['ip_id']);
|
||||
$network_id = intval($ip_row['ip_network_id']);
|
||||
|
||||
$error = '';
|
||||
$fields = networkIpFieldsSet($ip_post($ip_row), $error);
|
||||
if ($fields === '') {
|
||||
$ip_refuse($error);
|
||||
}
|
||||
|
||||
// Its own row is left out, so keeping the address doesn't trip the duplicate check
|
||||
$ip_address = array_key_exists('ip_address', $_POST) ? trim((string) (is_scalar($_POST['ip_address']) ? $_POST['ip_address'] : '')) : (string) $ip_row['ip_address'];
|
||||
$ip_error = checkIpForNetwork($ip_address, $network_id, $ip_id);
|
||||
if ($ip_error) {
|
||||
$ip_refuse($ip_error);
|
||||
}
|
||||
|
||||
if (mysqli_query($mysqli, "UPDATE network_ips SET ip_address = '" . escapeSql($ip_address) . "', $fields WHERE ip_id = $ip_id")) {
|
||||
$update_count = 1;
|
||||
$label = escapeSql("$ip_address on network " . $ip_row['network_name']);
|
||||
logAudit("IP Address", "Edit", "Edited IP $label via API ($api_key_name)", $client_id, $ip_id);
|
||||
logAudit("API", "Success", "Edited IP $label via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../update_output.php';
|
||||
110
api/v1/notes_body.php
Normal file
110
api/v1/notes_body.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* API - asset and contact notes - one body for both, included by asset_notes/ and
|
||||
* contact_notes/ with $note_kind ('asset' or 'contact') and $note_op (read, create,
|
||||
* archive, unarchive, delete). The notes on an asset's or a contact's page: a type
|
||||
* (Admin > Categories) and what was noted, by the agent who wrote it. Archived ones
|
||||
* stay until deleted, and only an archived one can be deleted.
|
||||
*/
|
||||
|
||||
if (!function_exists('apiDeny') || !isset($note_kind, $note_op)) {
|
||||
http_response_code(404);
|
||||
exit();
|
||||
}
|
||||
|
||||
$k = [
|
||||
'asset' => ['table' => 'asset_notes', 'p' => 'asset_note_', 'owner' => 'asset_note_asset_id', 'parent' => 'assets', 'id' => 'asset_id',
|
||||
'name' => 'asset_name', 'client' => 'asset_client_id', 'category' => 'asset_note_type', 'audit' => 'Asset'],
|
||||
'contact' => ['table' => 'contact_notes', 'p' => 'contact_note_', 'owner' => 'contact_note_contact_id', 'parent' => 'contacts', 'id' => 'contact_id',
|
||||
'name' => 'contact_name', 'client' => 'contact_client_id', 'category' => 'contact_note_type', 'audit' => 'Contact'],
|
||||
][$note_kind];
|
||||
$p = $k['p'];
|
||||
|
||||
$note_refuse = function ($message) use (&$return_arr) {
|
||||
http_response_code(400);
|
||||
$return_arr['success'] = "False";
|
||||
$return_arr['message'] = $message;
|
||||
echo json_encode($return_arr);
|
||||
exit();
|
||||
};
|
||||
|
||||
if ($note_op === 'read') {
|
||||
$parent_id = intval($_GET[$k['id']] ?? 0);
|
||||
$note_id = intval($_GET[$p . 'id'] ?? 0);
|
||||
if (!$parent_id && !$note_id) {
|
||||
$note_refuse("{$k['table']}/read.php needs {$k['id']} or {$p}id.");
|
||||
}
|
||||
$sql = mysqli_query($mysqli, "SELECT {$k['table']}.*, user_name AS {$p}created_by_name, {$k['client']}
|
||||
FROM {$k['table']}
|
||||
JOIN {$k['parent']} ON {$k['id']} = {$k['owner']}
|
||||
LEFT JOIN users ON user_id = {$p}created_by
|
||||
WHERE " . ($note_id ? "{$p}id = $note_id" : "{$k['owner']} = $parent_id") . " " . apiClientScopeSql($k['client']) . "
|
||||
ORDER BY {$p}created_at DESC, {$p}id DESC LIMIT $limit OFFSET $offset");
|
||||
require_once __DIR__ . '/read_output.php';
|
||||
}
|
||||
|
||||
if ($note_op === 'create') {
|
||||
$insert_id = false;
|
||||
$parent_id = intval($_POST[$k['id']] ?? 0);
|
||||
$parent = $parent_id ? mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT {$k['id']}, {$k['name']}, {$k['client']} FROM {$k['parent']} WHERE {$k['id']} = $parent_id "
|
||||
. clientScopeSql($k['client']) . ($client_id ? " AND {$k['client']} = $client_id" : '') . " LIMIT 1")) : null;
|
||||
if ($parent && apiUserCanAccessClient($parent[$k['client']])) {
|
||||
$client_id = intval($parent[$k['client']]);
|
||||
$note_field = $note_kind . '_note';
|
||||
$note = trim((string) (is_scalar($_POST[$note_field] ?? '') ? ($_POST[$note_field] ?? '') : ''));
|
||||
$type = categoryPickedName($k['category'], $_POST[$p . 'type'] ?? '');
|
||||
if ($note === '') {
|
||||
$note_refuse("A note needs something in it - {$note_kind}_note.");
|
||||
}
|
||||
if ($type === null || $type === '') {
|
||||
$note_refuse("{$p}type is one of: " . implode(', ', array_keys(categoryPicker($k['category']))) . '.');
|
||||
}
|
||||
$type_sql = escapeSql($type);
|
||||
mysqli_query($mysqli, "INSERT INTO {$k['table']} SET {$p}type = '$type_sql', {$note_kind}_note = '" . escapeSql($note) . "', {$p}created_by = " . intval($session_user_id) . ", {$k['owner']} = $parent_id");
|
||||
$insert_id = intval(mysqli_insert_id($mysqli)) ?: false;
|
||||
if ($insert_id) {
|
||||
$label = "a $type_sql note for $note_kind " . escapeSql($parent[$k['name']]);
|
||||
logAudit($k['audit'], "Edit", "Created $label via API ($api_key_name)", $client_id, $parent_id);
|
||||
logAudit("API", "Success", "Created $label via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
require_once __DIR__ . '/create_output.php';
|
||||
}
|
||||
|
||||
// The rest act on one note, of a client the key's user can reach
|
||||
$note_id = intval($_POST[$p . 'id'] ?? 0);
|
||||
$row = $note_id ? mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT {$p}id, {$p}type, {$p}archived_at, {$k['id']}, {$k['name']}, {$k['client']}
|
||||
FROM {$k['table']} JOIN {$k['parent']} ON {$k['id']} = {$k['owner']}
|
||||
WHERE {$p}id = $note_id " . clientScopeSql($k['client']) . ($client_id ? " AND {$k['client']} = $client_id" : '') . " LIMIT 1")) : null;
|
||||
if ($row && !apiUserCanAccessClient($row[$k['client']])) {
|
||||
$row = null;
|
||||
}
|
||||
$client_id = intval($row[$k['client']] ?? 0);
|
||||
$label = $row ? escapeSql($row[$p . 'type'] . " note for $note_kind " . $row[$k['name']]) : '';
|
||||
|
||||
if ($note_op === 'archive' || $note_op === 'unarchive') {
|
||||
$update_count = false;
|
||||
$archive = $note_op === 'archive';
|
||||
if ($row && empty($row[$p . 'archived_at']) === $archive) {
|
||||
mysqli_query($mysqli, "UPDATE {$k['table']} SET {$p}archived_at = " . ($archive ? 'NOW()' : 'NULL') . " WHERE {$p}id = $note_id");
|
||||
$update_count = 1;
|
||||
logAudit($k['audit'], "Edit", ($archive ? 'Archived' : 'Restored') . " the $label via API ($api_key_name)", $client_id, intval($row[$k['id']]));
|
||||
logAudit("API", "Success", ($archive ? 'Archived' : 'Restored') . " the $label via API ($api_key_name)", $client_id);
|
||||
}
|
||||
require_once __DIR__ . '/update_output.php';
|
||||
}
|
||||
|
||||
if ($note_op === 'delete') {
|
||||
$delete_count = false;
|
||||
// Archived first, as for the other records the API deletes
|
||||
if ($row && !empty($row[$p . 'archived_at'])) {
|
||||
mysqli_query($mysqli, "DELETE FROM {$k['table']} WHERE {$p}id = $note_id");
|
||||
$delete_count = mysqli_affected_rows($mysqli);
|
||||
if ($delete_count > 0) {
|
||||
logAudit($k['audit'], "Edit", "Deleted the $label via API ($api_key_name)", $client_id, intval($row[$k['id']]));
|
||||
logAudit("API", "Success", "Deleted the $label via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
require_once __DIR__ . '/delete_output.php';
|
||||
}
|
||||
46
api/v1/rack_units/create.php
Normal file
46
api/v1/rack_units/create.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Rack Units - Create
|
||||
* POST /api/v1/rack_units/create.php
|
||||
*
|
||||
* Mount an item in a rack, as Add to rack does - one of the client's assets (in
|
||||
* one rack at a time), or a named item such as a patch panel or a shelf - where
|
||||
* nothing else is on that face.
|
||||
*
|
||||
* Parameters (POST, JSON body):
|
||||
* api_key required - Your API key
|
||||
* rack_id required - The rack
|
||||
* client_id its client - needed when the key's user is limited to certain clients
|
||||
* asset_id The client's asset - or unit_device for an item that isn't one
|
||||
* unit_device A name for the item
|
||||
* unit_type optional - Without an asset: Device (the default), Patch Panel, Cable Management, Shelf, PDU, UPS, KVM / Console or Blanking Panel
|
||||
* unit_start_number required - The lowest unit it takes
|
||||
* unit_height optional - How many units, 1 if left out
|
||||
* unit_face optional - front (the default) or rear
|
||||
* unit_full_depth optional - 1: it takes the front and the rear
|
||||
* unit_power_watts, unit_notes optional
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$insert_id = false;
|
||||
|
||||
require_once '../racks/rack_find.php';
|
||||
|
||||
if ($rack) {
|
||||
|
||||
require_once 'unit_model.php';
|
||||
|
||||
mysqli_query($mysqli, "INSERT INTO rack_units SET $unit_set, unit_rack_id = $rack_id");
|
||||
$insert_id = intval(mysqli_insert_id($mysqli)) ?: false;
|
||||
|
||||
if ($insert_id) {
|
||||
$what = escapeSql("$unit_label to rack " . $rack['rack_name']);
|
||||
logAudit("Rack", "Edit", "Added $what via API ($api_key_name)", $client_id, $rack_id);
|
||||
logAudit("API", "Success", "Added $what via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../create_output.php';
|
||||
32
api/v1/rack_units/delete.php
Normal file
32
api/v1/rack_units/delete.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Rack Units - Delete
|
||||
* POST /api/v1/rack_units/delete.php
|
||||
*
|
||||
* Take an item out of its rack, as Remove does - the asset stays.
|
||||
*
|
||||
* Parameters (POST, JSON body): api_key, unit_id, client_id (the rack's client -
|
||||
* needed when the key's user is limited to certain clients)
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$delete_count = false;
|
||||
|
||||
require_once 'unit_find.php';
|
||||
|
||||
if ($unit) {
|
||||
|
||||
mysqli_query($mysqli, "DELETE FROM rack_units WHERE unit_id = $unit_id");
|
||||
$delete_count = mysqli_affected_rows($mysqli);
|
||||
|
||||
if ($delete_count > 0) {
|
||||
$what = escapeSql(rackUnitName($unit['asset_name'], $unit['unit_device'], $unit['unit_type']) . ' from rack ' . $rack['rack_name']);
|
||||
logAudit("Rack", "Edit", "Removed $what via API ($api_key_name)", $client_id, $rack_id);
|
||||
logAudit("API", "Success", "Removed $what via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../delete_output.php';
|
||||
18
api/v1/rack_units/unit_find.php
Normal file
18
api/v1/rack_units/unit_find.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
// The rack item a request names (unit_id) and its rack - of a client the key's user
|
||||
// can reach - as $unit and $rack, or null.
|
||||
|
||||
if (!function_exists('apiDeny')) {
|
||||
http_response_code(404);
|
||||
exit();
|
||||
}
|
||||
|
||||
$unit_id = intval($_POST['unit_id'] ?? 0);
|
||||
$unit = $unit_id ? mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT rack_units.*, asset_name FROM rack_units LEFT JOIN assets ON asset_id = unit_asset_id WHERE unit_id = $unit_id")) : null;
|
||||
// The rack comes from the item itself, never from the request
|
||||
$rack_lookup_id = intval($unit['unit_rack_id'] ?? 0);
|
||||
require_once '../racks/rack_find.php';
|
||||
if (!$rack) {
|
||||
$unit = null;
|
||||
}
|
||||
70
api/v1/rack_units/unit_model.php
Normal file
70
api/v1/rack_units/unit_model.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
// An item in a rack from POST, checked as the rack item form checks it - with what
|
||||
// the item has now ($unit) for those not sent, on an update. $rack is its rack and
|
||||
// $client_id the rack's client. Sets $unit_set (the SET clause) and $unit_label, or
|
||||
// turns the request down with 400 and why.
|
||||
|
||||
if (!function_exists('apiDeny')) {
|
||||
http_response_code(404);
|
||||
exit();
|
||||
}
|
||||
|
||||
$unit = $unit ?? null;
|
||||
$unit_id = intval($unit['unit_id'] ?? 0);
|
||||
$sent = function ($field, $current) {
|
||||
return array_key_exists($field, $_POST) ? $_POST[$field] : $current;
|
||||
};
|
||||
|
||||
$asset_id = intval($sent('asset_id', $unit['unit_asset_id'] ?? 0));
|
||||
$label = trim((string) $sent('unit_device', $unit['unit_device'] ?? ''));
|
||||
$unit_type = (string) $sent('unit_type', $unit['unit_type'] ?? 'Device');
|
||||
$start = intval($sent('unit_start_number', $unit['unit_start_number'] ?? 0));
|
||||
$height = max(1, intval($sent('unit_height', $unit ? intval($unit['unit_end_number']) - intval($unit['unit_start_number']) + 1 : 1)));
|
||||
$end = $start + $height - 1;
|
||||
$face = (string) $sent('unit_face', $unit['unit_face'] ?? 'front');
|
||||
$full_depth = intval($sent('unit_full_depth', $unit['unit_full_depth'] ?? 0)) === 1 ? 1 : 0;
|
||||
$power_watts = intval($sent('unit_power_watts', $unit['unit_power_watts'] ?? 0));
|
||||
$notes = mb_substr(trim((string) $sent('unit_notes', $unit['unit_notes'] ?? '')), 0, 255);
|
||||
$rack_units = intval($rack['rack_units']);
|
||||
|
||||
if ($unit_type === '' || !array_key_exists($unit_type, rackItemTypes())) {
|
||||
if (array_key_exists('unit_type', $_POST)) {
|
||||
$rack_refuse('unit_type is one of: ' . implode(', ', array_keys(rackItemTypes())) . '.');
|
||||
}
|
||||
$unit_type = 'Device';
|
||||
}
|
||||
if (!in_array($face, ['front', 'rear'], true)) {
|
||||
$rack_refuse('unit_face is front or rear.');
|
||||
}
|
||||
if ($start < 1 || $end > $rack_units) {
|
||||
$rack_refuse("That doesn't fit - this rack runs from U1 to U$rack_units.");
|
||||
}
|
||||
|
||||
// The asset has to be the client's, and in one place at a time
|
||||
$asset_name = '';
|
||||
if ($asset_id) {
|
||||
$asset_row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT asset_name FROM assets WHERE asset_id = $asset_id AND asset_client_id = " . intval($client_id)));
|
||||
if (!$asset_row) {
|
||||
$rack_refuse("asset_id isn't one of the client's assets.");
|
||||
}
|
||||
$asset_name = $asset_row['asset_name'];
|
||||
$racked = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT rack_name, unit_start_number, unit_end_number FROM rack_units LEFT JOIN racks ON rack_id = unit_rack_id
|
||||
WHERE unit_asset_id = $asset_id AND unit_id != $unit_id LIMIT 1"));
|
||||
if ($racked) {
|
||||
$rack_refuse("$asset_name is already racked in {$racked['rack_name']} at " . html_entity_decode(rackPositionLabel($racked['unit_start_number'], $racked['unit_end_number'])) . '.');
|
||||
}
|
||||
} elseif ($label === '') {
|
||||
$rack_refuse('Send asset_id, or a name for the item in unit_device.');
|
||||
}
|
||||
|
||||
$conflict = rackUnitConflict($rack['rack_id'], $start, $end, $face, $full_depth, $unit_id);
|
||||
if ($conflict) {
|
||||
$rack_refuse(html_entity_decode(rackPositionLabel($start, $end)) . " on the $face is taken by {$conflict['name']} (" . html_entity_decode(rackPositionLabel($conflict['start'], $conflict['end'])) . ").");
|
||||
}
|
||||
|
||||
// A linked asset is drawn by its own asset type, so only unlinked items keep one
|
||||
$unit_set = "unit_device = '" . escapeSql(mb_substr($label, 0, 200)) . "', unit_asset_id = " . ($asset_id ?: 'NULL') . ", unit_type = " . ($asset_id ? 'NULL' : "'" . escapeSql($unit_type) . "'") . ",
|
||||
unit_start_number = $start, unit_end_number = $end, unit_face = '$face', unit_full_depth = $full_depth,
|
||||
unit_power_watts = " . ($power_watts > 0 ? $power_watts : 'NULL') . ", unit_notes = '" . escapeSql($notes) . "'";
|
||||
$unit_label = rackUnitName($asset_name, $label, $unit_type) . ' at ' . html_entity_decode(rackPositionLabel($start, $end)) . " ($face)";
|
||||
36
api/v1/rack_units/update.php
Normal file
36
api/v1/rack_units/update.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Rack Units - Update
|
||||
* POST /api/v1/rack_units/update.php
|
||||
*
|
||||
* Move or change an item in its rack, as Edit does on it - only what is sent,
|
||||
* with the same checks as mounting it.
|
||||
*
|
||||
* Parameters (POST, JSON body):
|
||||
* api_key required - Your API key
|
||||
* unit_id required - The item
|
||||
* client_id the rack's client - needed when the key's user is limited to certain clients
|
||||
* the fields of rack_units/create.php but rack_id optional
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$update_count = false;
|
||||
|
||||
require_once 'unit_find.php';
|
||||
|
||||
if ($unit) {
|
||||
|
||||
require_once 'unit_model.php';
|
||||
|
||||
if (mysqli_query($mysqli, "UPDATE rack_units SET $unit_set WHERE unit_id = $unit_id")) {
|
||||
$update_count = 1;
|
||||
$what = escapeSql("$unit_label in rack " . $rack['rack_name']);
|
||||
logAudit("Rack", "Edit", "Updated $what via API ($api_key_name)", $client_id, $rack_id);
|
||||
logAudit("API", "Success", "Updated $what via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../update_output.php';
|
||||
30
api/v1/racks/archive.php
Normal file
30
api/v1/racks/archive.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Racks - Archive
|
||||
* POST /api/v1/racks/archive.php
|
||||
*
|
||||
* Archive a rack, as Archive does - it and what is in it stay as they are.
|
||||
*
|
||||
* Parameters (POST, JSON body): api_key, rack_id, client_id (its client - needed when
|
||||
* the key's user is limited to certain clients)
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$update_count = false;
|
||||
|
||||
require_once 'rack_find.php';
|
||||
|
||||
if ($rack && empty($rack['rack_archived_at'])) {
|
||||
|
||||
mysqli_query($mysqli, "UPDATE racks SET rack_archived_at = NOW() WHERE rack_id = $rack_id");
|
||||
|
||||
$update_count = 1;
|
||||
$name = escapeSql($rack['rack_name']);
|
||||
logAudit("Rack", "Archive", "Archived rack $name via API ($api_key_name)", $client_id, $rack_id);
|
||||
logAudit("API", "Success", "Archived rack $name via API ($api_key_name)", $client_id);
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../update_output.php';
|
||||
53
api/v1/racks/create.php
Normal file
53
api/v1/racks/create.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Racks - Create
|
||||
* POST /api/v1/racks/create.php
|
||||
*
|
||||
* Document a client's rack, as New Rack does. Photos are added in ITFlow.
|
||||
*
|
||||
* Parameters (POST, JSON body):
|
||||
* api_key required - Your API key
|
||||
* client_id required - The client
|
||||
* rack_name required
|
||||
* rack_type required - A rack type (Admin > Categories)
|
||||
* rack_units required - How tall, 1 to 70 units
|
||||
* rack_units_descending optional - 1: U1 is at the top
|
||||
* rack_model, rack_depth, rack_power_capacity (watts), rack_location_id (the client's),
|
||||
* rack_physical_location, rack_description, rack_notes optional
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$insert_id = false;
|
||||
|
||||
$rack_lookup_id = 0;
|
||||
require_once 'rack_find.php';
|
||||
|
||||
if (!$client_id || !mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM clients WHERE client_id = $client_id AND client_archived_at IS NULL"))) {
|
||||
$rack_refuse('client_id is required - the client the rack is for.');
|
||||
}
|
||||
|
||||
require_once 'rack_model.php';
|
||||
|
||||
foreach (['rack_name' => 'A rack needs a name - rack_name.', 'rack_type' => 'rack_type is one of: ' . implode(', ', array_keys(categoryPicker('rack_type'))) . '.', 'rack_units' => 'rack_units is how tall it is - 1 to 70 units.'] as $field => $message) {
|
||||
if (!isset($rack_set[$field])) {
|
||||
$rack_refuse($message);
|
||||
}
|
||||
}
|
||||
$rack_set['rack_client_id'] = $client_id;
|
||||
|
||||
mysqli_query($mysqli, "INSERT INTO racks SET " . implode(', ', array_map(function ($column, $value) {
|
||||
return "$column = $value";
|
||||
}, array_keys($rack_set), $rack_set)));
|
||||
$rack_id = intval(mysqli_insert_id($mysqli));
|
||||
|
||||
if ($rack_id) {
|
||||
$insert_id = $rack_id;
|
||||
$name = escapeSql((string) getFieldById('racks', $rack_id, 'rack_name'));
|
||||
logAudit("Rack", "Create", "Created rack $name via API ($api_key_name)", $client_id, $rack_id);
|
||||
logAudit("API", "Success", "Created rack $name via API ($api_key_name)", $client_id);
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../create_output.php';
|
||||
37
api/v1/racks/delete.php
Normal file
37
api/v1/racks/delete.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Racks - Delete
|
||||
* POST /api/v1/racks/delete.php
|
||||
*
|
||||
* Delete an archived rack, as Delete does - what is mounted in it and its photos
|
||||
* go with it; the assets stay. Archived first, as for the other records the API deletes.
|
||||
*
|
||||
* Parameters (POST, JSON body): api_key, rack_id, client_id (its client - needed when
|
||||
* the key's user is limited to certain clients)
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$delete_count = false;
|
||||
|
||||
require_once 'rack_find.php';
|
||||
|
||||
if ($rack && !empty($rack['rack_archived_at'])) {
|
||||
|
||||
// rack_units go with it (ON DELETE CASCADE)
|
||||
mysqli_query($mysqli, "DELETE FROM racks WHERE rack_id = $rack_id");
|
||||
$delete_count = mysqli_affected_rows($mysqli);
|
||||
|
||||
if ($delete_count > 0) {
|
||||
rackDeletePhoto($client_id, $rack['rack_photo']);
|
||||
rackDeletePhoto($client_id, $rack['rack_photo_rear']);
|
||||
|
||||
$name = escapeSql($rack['rack_name']);
|
||||
logAudit("Rack", "Delete", "Deleted rack $name via API ($api_key_name)", $client_id);
|
||||
logAudit("API", "Success", "Deleted rack $name via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../delete_output.php';
|
||||
27
api/v1/racks/rack_find.php
Normal file
27
api/v1/racks/rack_find.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
// The rack a request names (rack_id) - of a client the key's user can reach, and
|
||||
// client_id's when it is sent - as $rack, or null. $client_id becomes its client.
|
||||
|
||||
if (!function_exists('apiDeny')) {
|
||||
http_response_code(404);
|
||||
exit();
|
||||
}
|
||||
|
||||
$rack_id = intval($rack_lookup_id ?? ($_POST['rack_id'] ?? 0));
|
||||
$rack = $rack_id ? mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM racks WHERE rack_id = $rack_id "
|
||||
. clientScopeSql('rack_client_id') . ($client_id ? " AND rack_client_id = $client_id" : '') . " LIMIT 1")) : null;
|
||||
if ($rack && !apiUserCanAccessClient($rack['rack_client_id'])) {
|
||||
$rack = null;
|
||||
}
|
||||
if ($rack) {
|
||||
$client_id = intval($rack['rack_client_id']);
|
||||
}
|
||||
|
||||
$rack_refuse = function ($message) use (&$return_arr) {
|
||||
http_response_code(400);
|
||||
$return_arr['success'] = "False";
|
||||
$return_arr['message'] = $message;
|
||||
echo json_encode($return_arr);
|
||||
exit();
|
||||
};
|
||||
56
api/v1/racks/rack_model.php
Normal file
56
api/v1/racks/rack_model.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
// A rack's fields from POST, checked as the rack form checks them, each only when
|
||||
// sent, into $rack_set (column => SQL value). On an update $rack is the rack.
|
||||
// $client_id is its client.
|
||||
|
||||
if (!function_exists('apiDeny')) {
|
||||
http_response_code(404);
|
||||
exit();
|
||||
}
|
||||
|
||||
$rack = $rack ?? null;
|
||||
$rack_set = [];
|
||||
$text = function ($field) {
|
||||
return trim((string) (is_scalar($_POST[$field] ?? '') ? ($_POST[$field] ?? '') : ''));
|
||||
};
|
||||
|
||||
if (array_key_exists('rack_name', $_POST)) {
|
||||
if ($text('rack_name') === '') {
|
||||
$rack_refuse('A rack needs a name - rack_name.');
|
||||
}
|
||||
$rack_set['rack_name'] = "'" . escapeSql(mb_substr($text('rack_name'), 0, 200)) . "'";
|
||||
}
|
||||
if (array_key_exists('rack_type', $_POST)) {
|
||||
$type = categoryPickedName('rack_type', $text('rack_type'), (string) ($rack['rack_type'] ?? ''));
|
||||
if ($type === null || $type === '') {
|
||||
$rack_refuse('rack_type is one of: ' . implode(', ', array_keys(categoryPicker('rack_type'))) . '.');
|
||||
}
|
||||
$rack_set['rack_type'] = "'" . escapeSql(mb_substr($type, 0, 50)) . "'";
|
||||
}
|
||||
if (array_key_exists('rack_units', $_POST)) {
|
||||
$units = intval($_POST['rack_units']);
|
||||
if ($units < 1 || $units > 70) {
|
||||
$rack_refuse('rack_units is how tall it is - 1 to 70 units.');
|
||||
}
|
||||
$rack_set['rack_units'] = $units;
|
||||
}
|
||||
if (array_key_exists('rack_units_descending', $_POST)) {
|
||||
$rack_set['rack_units_descending'] = intval($_POST['rack_units_descending']) === 1 ? 1 : 0;
|
||||
}
|
||||
if (array_key_exists('rack_power_capacity', $_POST)) {
|
||||
$watts = intval($_POST['rack_power_capacity']);
|
||||
$rack_set['rack_power_capacity'] = $watts > 0 ? $watts : 'NULL';
|
||||
}
|
||||
if (array_key_exists('rack_location_id', $_POST)) {
|
||||
$location = intval($_POST['rack_location_id']);
|
||||
if ($location && !mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM locations WHERE location_id = $location AND location_client_id = " . intval($client_id)))) {
|
||||
$rack_refuse("rack_location_id isn't one of the client's locations.");
|
||||
}
|
||||
$rack_set['rack_location_id'] = $location;
|
||||
}
|
||||
foreach (['rack_model' => 200, 'rack_depth' => 50, 'rack_physical_location' => 200, 'rack_description' => 0, 'rack_notes' => 0] as $field => $length) {
|
||||
if (array_key_exists($field, $_POST)) {
|
||||
$rack_set[$field] = "'" . escapeSql($length ? mb_substr($text($field), 0, $length) : $text($field)) . "'";
|
||||
}
|
||||
}
|
||||
46
api/v1/racks/read.php
Normal file
46
api/v1/racks/read.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Racks - Read
|
||||
* GET /api/v1/racks/read.php
|
||||
*
|
||||
* A client's racks - archived ones too. One, by rack_id, also has what is mounted
|
||||
* in it (units): each item's position (unit_start_number to unit_end_number), face,
|
||||
* whether it is full depth, and the asset it is, or its own name and type.
|
||||
*
|
||||
* rack_id optional - Just this rack, with its units
|
||||
* client_id optional - Just this client's
|
||||
* limit, offset optional - Paging
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_get_method.php';
|
||||
|
||||
if (isset($_GET['rack_id'])) {
|
||||
$id = intval($_GET['rack_id']);
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM racks WHERE rack_id = $id " . apiClientScopeSql('rack_client_id'));
|
||||
if ($sql && ($row = mysqli_fetch_assoc($sql))) {
|
||||
$row['units'] = array_map(function ($unit) {
|
||||
return [
|
||||
'unit_id' => intval($unit['unit_id']),
|
||||
'unit_start_number' => intval($unit['unit_start_number']),
|
||||
'unit_end_number' => intval($unit['unit_end_number']),
|
||||
'unit_face' => $unit['unit_face'],
|
||||
'unit_full_depth' => intval($unit['unit_full_depth']),
|
||||
'unit_device' => $unit['unit_device'],
|
||||
'unit_type' => $unit['unit_type'],
|
||||
'unit_power_watts' => $unit['unit_power_watts'] !== null ? intval($unit['unit_power_watts']) : null,
|
||||
'unit_notes' => $unit['unit_notes'],
|
||||
'asset_id' => $unit['asset_id'] !== null ? intval($unit['asset_id']) : null,
|
||||
'asset_name' => $unit['asset_name'],
|
||||
'asset_type' => $unit['asset_type'],
|
||||
];
|
||||
}, array_reverse(rackLoadUnits([$id])[$id]));
|
||||
echo json_encode(['success' => 'True', 'count' => 1, 'data' => [$row]]);
|
||||
exit();
|
||||
}
|
||||
} else {
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM racks WHERE 1=1 " . apiClientScopeSql('rack_client_id') . " ORDER BY rack_id LIMIT $limit OFFSET $offset");
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once "../read_output.php";
|
||||
30
api/v1/racks/unarchive.php
Normal file
30
api/v1/racks/unarchive.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Racks - Unarchive
|
||||
* POST /api/v1/racks/unarchive.php
|
||||
*
|
||||
* Bring an archived rack back, as Restore does.
|
||||
*
|
||||
* Parameters (POST, JSON body): api_key, rack_id, client_id (its client - needed when
|
||||
* the key's user is limited to certain clients)
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$update_count = false;
|
||||
|
||||
require_once 'rack_find.php';
|
||||
|
||||
if ($rack && !empty($rack['rack_archived_at'])) {
|
||||
|
||||
mysqli_query($mysqli, "UPDATE racks SET rack_archived_at = NULL WHERE rack_id = $rack_id");
|
||||
|
||||
$update_count = 1;
|
||||
$name = escapeSql($rack['rack_name']);
|
||||
logAudit("Rack", "Restore", "Restored rack $name via API ($api_key_name)", $client_id, $rack_id);
|
||||
logAudit("API", "Success", "Unarchived rack $name via API ($api_key_name)", $client_id);
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../update_output.php';
|
||||
53
api/v1/racks/update.php
Normal file
53
api/v1/racks/update.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Racks - Update
|
||||
* POST /api/v1/racks/update.php
|
||||
*
|
||||
* Change a rack, as Edit does - only what is sent. It can't be made shorter than
|
||||
* the highest item mounted in it.
|
||||
*
|
||||
* Parameters (POST, JSON body):
|
||||
* api_key required - Your API key
|
||||
* rack_id required - The rack
|
||||
* client_id its client - needed when the key's user is limited to certain clients
|
||||
* the fields of racks/create.php optional
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$update_count = false;
|
||||
|
||||
require_once 'rack_find.php';
|
||||
|
||||
if ($rack) {
|
||||
|
||||
require_once 'rack_model.php';
|
||||
|
||||
if (!$rack_set) {
|
||||
$rack_refuse('Send at least one of the fields of racks/create.php.');
|
||||
}
|
||||
|
||||
// Shrinking the rack under an item would leave it hanging off the top
|
||||
if (isset($rack_set['rack_units'])) {
|
||||
$units = intval($rack_set['rack_units']);
|
||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT unit_device, unit_type, unit_end_number, asset_name FROM rack_units
|
||||
LEFT JOIN assets ON asset_id = unit_asset_id
|
||||
WHERE unit_rack_id = $rack_id AND unit_end_number > $units ORDER BY unit_end_number DESC LIMIT 1"));
|
||||
if ($row) {
|
||||
$rack_refuse("Can't shrink the rack to {$units}U - " . rackUnitName($row['asset_name'], $row['unit_device'], $row['unit_type']) . " is mounted up to U" . intval($row['unit_end_number']) . ". Move or remove it first.");
|
||||
}
|
||||
}
|
||||
|
||||
if (mysqli_query($mysqli, "UPDATE racks SET " . implode(', ', array_map(function ($column, $value) {
|
||||
return "$column = $value";
|
||||
}, array_keys($rack_set), $rack_set)) . " WHERE rack_id = $rack_id")) {
|
||||
$update_count = 1;
|
||||
$name = escapeSql((string) getFieldById('racks', $rack_id, 'rack_name'));
|
||||
logAudit("Rack", "Edit", "Edited rack $name via API ($api_key_name)", $client_id, $rack_id);
|
||||
logAudit("API", "Success", "Edited rack $name via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../update_output.php';
|
||||
@@ -11,9 +11,9 @@ if (isset($_GET['software_id'])) {
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM software WHERE software_id = '$id' AND software_archived_at IS NULL AND 1=1 " . apiClientScopeSql('software_client_id') . "");
|
||||
|
||||
} elseif (isset($_GET['software_key'])) {
|
||||
// Specific software via key
|
||||
$key = mysqli_real_escape_string($mysqli, $_GET['software_license']);
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM software WHERE software_key = '$key' AND software_archived_at IS NULL AND 1=1 " . apiClientScopeSql('software_client_id') . " ORDER BY software_id LIMIT $limit OFFSET $offset");
|
||||
// Specific software via key - its own, or one of its keys
|
||||
$key = mysqli_real_escape_string($mysqli, (string) $_GET['software_key']);
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM software WHERE (software_key = '$key' OR software_id IN (SELECT software_key_software_id FROM software_keys WHERE software_key = '$key')) AND software_archived_at IS NULL AND 1=1 " . apiClientScopeSql('software_client_id') . " ORDER BY software_id LIMIT $limit OFFSET $offset");
|
||||
|
||||
} elseif (isset($_GET['software_name'])) {
|
||||
// Software by name
|
||||
|
||||
58
api/v1/software/renew.php
Normal file
58
api/v1/software/renew.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Licenses - Renew
|
||||
* POST /api/v1/software/renew.php
|
||||
*
|
||||
* Renew a license, as Renew does - by a term from its renewal date (or from
|
||||
* today, when that has passed), or to a date of your choosing - with the
|
||||
* purchase reference for it.
|
||||
*
|
||||
* Parameters (POST, JSON body):
|
||||
* api_key required - Your API key
|
||||
* software_id required - The license
|
||||
* client_id its client - needed when the key's user is limited to certain clients
|
||||
* renew_term 1m, 3m, 6m, 1y, 2y or 3y - this or renew_date
|
||||
* renew_date The new renewal date, Y-m-d
|
||||
* purchase_reference optional - The order or invoice it was renewed on
|
||||
*
|
||||
* Answer: software_expire - its renewal date now.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$update_count = false;
|
||||
|
||||
$software_lookup_id = intval($_POST['software_id'] ?? 0);
|
||||
require_once 'software_find.php';
|
||||
|
||||
if ($software) {
|
||||
|
||||
$software_id = intval($software['software_id']);
|
||||
$term = (string) ($_POST['renew_term'] ?? '');
|
||||
$until = !empty($_POST['renew_date']) ? softwareFormDate($_POST['renew_date']) : ($term !== '' ? softwareRenewalExtended($software['software_expire'], $term) : null);
|
||||
|
||||
if (!$until) {
|
||||
http_response_code(400);
|
||||
$return_arr['success'] = "False";
|
||||
$return_arr['message'] = 'Send renew_term - one of ' . implode(', ', array_keys(SOFTWARE_RENEWAL_TERMS)) . ' - or renew_date, the new renewal date as 2027-06-30.';
|
||||
echo json_encode($return_arr);
|
||||
exit();
|
||||
}
|
||||
|
||||
$reference = trim((string) (is_scalar($_POST['purchase_reference'] ?? '') ? ($_POST['purchase_reference'] ?? '') : ''));
|
||||
$reference_sql = $reference !== '' ? ", software_purchase_reference = '" . escapeSql(mb_substr($reference, 0, 200)) . "'" : '';
|
||||
|
||||
if (mysqli_query($mysqli, "UPDATE software SET software_expire = '$until' $reference_sql WHERE software_id = $software_id")) {
|
||||
$update_count = 1;
|
||||
$return_arr['software_expire'] = $until;
|
||||
|
||||
$was = $software['software_expire'] ?: 'no date';
|
||||
$label = escapeSql($software['software_name']) . " until $until (was " . escapeSql($was) . ")" . ($reference !== '' ? ', reference ' . escapeSql($reference) : '');
|
||||
logAudit("Software", "Renew", "Renewed license $label via API ($api_key_name)", $client_id, $software_id);
|
||||
logAudit("API", "Success", "Renewed license " . escapeSql($software['software_name']) . " via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../update_output.php';
|
||||
70
api/v1/software/seats.php
Normal file
70
api/v1/software/seats.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/*
|
||||
* API - Licenses - Seats
|
||||
* POST /api/v1/software/seats.php
|
||||
*
|
||||
* Who holds a seat on a license, as Seats does on it - the client's devices
|
||||
* (asset_ids) and users (contact_ids), each list in place of the one it had.
|
||||
* One left out stays as it is. A device or user that loses its seat gives back
|
||||
* the keys it was handed.
|
||||
*
|
||||
* Parameters (POST, JSON body):
|
||||
* api_key required - Your API key
|
||||
* software_id required - The license
|
||||
* client_id its client - needed when the key's user is limited to certain clients
|
||||
* asset_ids optional - The client's assets on it
|
||||
* contact_ids optional - The client's contacts on it
|
||||
*
|
||||
* Answer: added and removed (names), and warning when it is over its seats.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$update_count = false;
|
||||
|
||||
$software_lookup_id = intval($_POST['software_id'] ?? 0);
|
||||
require_once 'software_find.php';
|
||||
|
||||
if ($software && (array_key_exists('asset_ids', $_POST) || array_key_exists('contact_ids', $_POST))) {
|
||||
|
||||
$software_id = intval($software['software_id']);
|
||||
|
||||
// What is not sent stays as it is
|
||||
$holders = softwareSeatHolders($software_id);
|
||||
$wanted = [];
|
||||
foreach (['asset' => ['asset_ids', 'assets'], 'contact' => ['contact_ids', 'contacts']] as $kind => [$field, $list]) {
|
||||
if (array_key_exists($field, $_POST)) {
|
||||
$ids = array_values(array_unique(array_filter(array_map('intval', (array) $_POST[$field]))));
|
||||
$owned = softwareClientIds($kind, $ids, $client_id);
|
||||
if (count($owned) !== count($ids)) {
|
||||
http_response_code(400);
|
||||
$return_arr['success'] = "False";
|
||||
$return_arr['message'] = "$field are the client's own {$kind}s, not archived - these aren't: " . implode(', ', array_diff($ids, $owned)) . '.';
|
||||
echo json_encode($return_arr);
|
||||
exit();
|
||||
}
|
||||
$wanted[$kind] = $owned;
|
||||
} else {
|
||||
$wanted[$kind] = array_map(function ($row) use ($kind) {
|
||||
return intval($row[$kind . '_id']);
|
||||
}, $holders[$list]);
|
||||
}
|
||||
}
|
||||
|
||||
$changes = softwareSyncSeats($software_id, $client_id, $wanted['asset'], $wanted['contact']);
|
||||
|
||||
$update_count = 1;
|
||||
$return_arr['added'] = array_values($changes['added']);
|
||||
$return_arr['removed'] = array_values($changes['removed']);
|
||||
$return_arr['warning'] = $software_seat_warning($software_id);
|
||||
|
||||
if ($changes['added'] || $changes['removed']) {
|
||||
$label = escapeSql($software['software_name'] . softwareSeatChangeText($changes));
|
||||
logAudit("Software", "Seats", "Changed seats on license $label via API ($api_key_name)", $client_id, $software_id);
|
||||
logAudit("API", "Success", "Changed seats on license " . escapeSql($software['software_name']) . " via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../update_output.php';
|
||||
24
api/v1/software/software_find.php
Normal file
24
api/v1/software/software_find.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
// The license a request names (software_id) - of a client the key's user can reach,
|
||||
// and client_id's when it is sent - as $software (softwareRow()), or null. $client_id
|
||||
// becomes its client.
|
||||
|
||||
if (!function_exists('apiDeny')) {
|
||||
http_response_code(404);
|
||||
exit();
|
||||
}
|
||||
|
||||
$software = !empty($software_lookup_id) ? softwareRow($software_lookup_id) : null;
|
||||
if ($software && (!apiUserCanAccessClient($software['software_client_id']) || ($client_id && intval($software['software_client_id']) !== $client_id))) {
|
||||
$software = null;
|
||||
}
|
||||
if ($software) {
|
||||
$client_id = intval($software['software_client_id']);
|
||||
}
|
||||
|
||||
// A seat warning, as plain text
|
||||
$software_seat_warning = function ($software_id) {
|
||||
$warning = softwareSeatWarning($software_id);
|
||||
return $warning === '' ? '' : ucfirst(ltrim(html_entity_decode(strip_tags($warning), ENT_QUOTES | ENT_HTML5, 'UTF-8'), ' -')) . '.';
|
||||
};
|
||||
49
api/v1/software_keys/create.php
Normal file
49
api/v1/software_keys/create.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/*
|
||||
* API - License Keys - Create
|
||||
* POST /api/v1/software_keys/create.php
|
||||
*
|
||||
* Add keys to a license, as Add keys does - one per line, or a list. Blank ones
|
||||
* and keys it has already are skipped.
|
||||
*
|
||||
* Parameters (POST, JSON body):
|
||||
* api_key required - Your API key
|
||||
* software_id required - The license
|
||||
* client_id its client - needed when the key's user is limited to certain clients
|
||||
* keys required - The keys, one per line or as a list
|
||||
*
|
||||
* Answer: insert_id (the last key added), and added - how many.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$insert_id = false;
|
||||
|
||||
$software_lookup_id = intval($_POST['software_id'] ?? 0);
|
||||
require_once '../software/software_find.php';
|
||||
|
||||
if ($software) {
|
||||
|
||||
$software_id = intval($software['software_id']);
|
||||
$text = is_array($_POST['keys'] ?? null) ? implode("\n", array_filter($_POST['keys'], 'is_scalar')) : (string) (is_scalar($_POST['keys'] ?? '') ? ($_POST['keys'] ?? '') : '');
|
||||
|
||||
$added = softwareAddKeys($software_id, $text);
|
||||
if (!$added) {
|
||||
http_response_code(400);
|
||||
$return_arr['success'] = "False";
|
||||
$return_arr['message'] = 'No new keys to add - keys was blank, or ' . $software['software_name'] . ' has them already.';
|
||||
echo json_encode($return_arr);
|
||||
exit();
|
||||
}
|
||||
|
||||
$insert_id = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT MAX(software_key_id) FROM software_keys WHERE software_key_software_id = $software_id"))[0]);
|
||||
$return_arr['added'] = $added;
|
||||
|
||||
$label = "$added key" . ($added === 1 ? '' : 's') . ' to license ' . escapeSql($software['software_name']);
|
||||
logAudit("Software", "Keys", "Added $label via API ($api_key_name)", $client_id, $software_id);
|
||||
logAudit("API", "Success", "Added $label via API ($api_key_name)", $client_id);
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../create_output.php';
|
||||
33
api/v1/software_keys/delete.php
Normal file
33
api/v1/software_keys/delete.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/*
|
||||
* API - License Keys - Delete
|
||||
* POST /api/v1/software_keys/delete.php
|
||||
*
|
||||
* Delete a key from its license, as Delete does on it. The seats it was handed
|
||||
* with stay.
|
||||
*
|
||||
* Parameters (POST, JSON body): api_key, software_key_id, client_id (the license's
|
||||
* client - needed when the key's user is limited to certain clients)
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$delete_count = false;
|
||||
|
||||
require_once 'key_find.php';
|
||||
|
||||
if ($key_row) {
|
||||
|
||||
mysqli_query($mysqli, "DELETE FROM software_keys WHERE software_key_id = $software_key_id");
|
||||
$delete_count = mysqli_affected_rows($mysqli);
|
||||
|
||||
if ($delete_count > 0) {
|
||||
$label = escapeSql($software['software_name']);
|
||||
logAudit("Software", "Keys", "Deleted a key from license $label via API ($api_key_name)", $client_id, intval($software['software_id']));
|
||||
logAudit("API", "Success", "Deleted a key from license $label via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../delete_output.php';
|
||||
17
api/v1/software_keys/key_find.php
Normal file
17
api/v1/software_keys/key_find.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
// The key a request names (software_key_id) and its license ($software) - of a client
|
||||
// the key's user can reach - as $key_row, or null.
|
||||
|
||||
if (!function_exists('apiDeny')) {
|
||||
http_response_code(404);
|
||||
exit();
|
||||
}
|
||||
|
||||
$software_key_id = intval($_POST['software_key_id'] ?? 0);
|
||||
$key_row = $software_key_id ? mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT software_key_id, software_key, software_key_software_id FROM software_keys WHERE software_key_id = $software_key_id")) : null;
|
||||
$software_lookup_id = intval($key_row['software_key_software_id'] ?? 0);
|
||||
require_once '../software/software_find.php';
|
||||
if (!$software) {
|
||||
$key_row = null;
|
||||
}
|
||||
59
api/v1/software_keys/read.php
Normal file
59
api/v1/software_keys/read.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/*
|
||||
* API - License Keys - Read
|
||||
* GET /api/v1/software_keys/read.php
|
||||
*
|
||||
* A license's keys, oldest first, each with the client's devices (assets) and
|
||||
* users (contacts) it is handed to.
|
||||
*
|
||||
* software_id required - The license
|
||||
* software_key_id optional - Just this key
|
||||
* client_id optional - Only if the license is this client's
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_get_method.php';
|
||||
|
||||
$software_id = intval($_GET['software_id'] ?? 0);
|
||||
$key_id = intval($_GET['software_key_id'] ?? 0);
|
||||
if (!$software_id && $key_id) {
|
||||
$software_id = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT software_key_software_id FROM software_keys WHERE software_key_id = $key_id"))[0] ?? 0);
|
||||
}
|
||||
|
||||
$sql = $software_id ? mysqli_query($mysqli, "SELECT software_id FROM software WHERE software_id = $software_id " . apiClientScopeSql('software_client_id')) : false;
|
||||
|
||||
if ($sql && mysqli_num_rows($sql)) {
|
||||
$keys = [];
|
||||
foreach (softwareKeys($software_id) as $key) {
|
||||
if ($key_id && $key['id'] !== $key_id) {
|
||||
continue;
|
||||
}
|
||||
$keys[] = [
|
||||
'software_key_id' => $key['id'],
|
||||
'software_key' => $key['key'],
|
||||
'software_id' => $software_id,
|
||||
'assets' => array_map(function ($row) {
|
||||
return ['asset_id' => intval($row['asset_id']), 'asset_name' => $row['asset_name']];
|
||||
}, $key['assets']),
|
||||
'contacts' => array_map(function ($row) {
|
||||
return ['contact_id' => intval($row['contact_id']), 'contact_name' => $row['contact_name']];
|
||||
}, $key['contacts']),
|
||||
];
|
||||
}
|
||||
if ($keys) {
|
||||
echo json_encode(['success' => 'True', 'count' => count($keys), 'data' => $keys]);
|
||||
exit();
|
||||
}
|
||||
$sql = false;
|
||||
}
|
||||
|
||||
if (!$software_id) {
|
||||
http_response_code(400);
|
||||
$return_arr['success'] = "False";
|
||||
$return_arr['message'] = "software_keys/read.php needs software_id or software_key_id.";
|
||||
echo json_encode($return_arr);
|
||||
exit();
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once "../read_output.php";
|
||||
88
api/v1/software_keys/update.php
Normal file
88
api/v1/software_keys/update.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/*
|
||||
* API - License Keys - Update
|
||||
* POST /api/v1/software_keys/update.php
|
||||
*
|
||||
* Change a key, or who it is handed to, as Edit does on it - only what is sent.
|
||||
* Each device or user it is handed to gets a seat on the license, if it had none.
|
||||
*
|
||||
* Parameters (POST, JSON body):
|
||||
* api_key required - Your API key
|
||||
* software_key_id required - The key
|
||||
* client_id the license's client - needed when the key's user is limited to certain clients
|
||||
* software_key optional - The key itself
|
||||
* asset_ids optional - The client's assets it is handed to, in place of those it had
|
||||
* contact_ids optional - The client's contacts it is handed to, in place of those it had
|
||||
*
|
||||
* Answer: warning, when the license is over its seats.
|
||||
*/
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
$update_count = false;
|
||||
|
||||
require_once 'key_find.php';
|
||||
|
||||
if ($key_row) {
|
||||
|
||||
$software_id = intval($software['software_id']);
|
||||
|
||||
$refuse = '';
|
||||
if (array_key_exists('software_key', $_POST)) {
|
||||
$key = mb_substr(trim((string) (is_scalar($_POST['software_key']) ? $_POST['software_key'] : '')), 0, 400);
|
||||
if ($key === '') {
|
||||
$refuse = 'A key cannot be blank - delete it instead.';
|
||||
}
|
||||
}
|
||||
$holders = [];
|
||||
foreach (['asset' => 'asset_ids', 'contact' => 'contact_ids'] as $kind => $field) {
|
||||
if ($refuse === '' && array_key_exists($field, $_POST)) {
|
||||
$ids = array_values(array_unique(array_filter(array_map('intval', (array) $_POST[$field]))));
|
||||
$holders[$kind] = softwareClientIds($kind, $ids, $client_id);
|
||||
if (count($holders[$kind]) !== count($ids)) {
|
||||
$refuse = "$field are the client's own {$kind}s, not archived - these aren't: " . implode(', ', array_diff($ids, $holders[$kind])) . '.';
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($refuse === '' && !array_key_exists('software_key', $_POST) && !$holders) {
|
||||
$refuse = 'Send at least one of software_key, asset_ids or contact_ids.';
|
||||
}
|
||||
if ($refuse !== '') {
|
||||
http_response_code(400);
|
||||
$return_arr['success'] = "False";
|
||||
$return_arr['message'] = $refuse;
|
||||
echo json_encode($return_arr);
|
||||
exit();
|
||||
}
|
||||
|
||||
if (isset($key)) {
|
||||
mysqli_query($mysqli, "UPDATE software_keys SET software_key = '" . escapeSql($key) . "' WHERE software_key_id = $software_key_id");
|
||||
}
|
||||
|
||||
// Who it is handed to - each gets a seat if they had none
|
||||
if (isset($holders['asset'])) {
|
||||
mysqli_query($mysqli, "DELETE FROM software_key_asset_assignments WHERE software_key_id = $software_key_id");
|
||||
foreach ($holders['asset'] as $asset_id) {
|
||||
mysqli_query($mysqli, "INSERT IGNORE INTO software_key_asset_assignments SET software_key_id = $software_key_id, asset_id = $asset_id");
|
||||
mysqli_query($mysqli, "INSERT IGNORE INTO software_assets SET software_id = $software_id, asset_id = $asset_id");
|
||||
}
|
||||
}
|
||||
if (isset($holders['contact'])) {
|
||||
mysqli_query($mysqli, "DELETE FROM software_key_contact_assignments WHERE software_key_id = $software_key_id");
|
||||
foreach ($holders['contact'] as $contact_id) {
|
||||
mysqli_query($mysqli, "INSERT IGNORE INTO software_key_contact_assignments SET software_key_id = $software_key_id, contact_id = $contact_id");
|
||||
mysqli_query($mysqli, "INSERT IGNORE INTO software_contacts SET software_id = $software_id, contact_id = $contact_id");
|
||||
}
|
||||
}
|
||||
|
||||
$update_count = 1;
|
||||
$return_arr['warning'] = $software_seat_warning($software_id);
|
||||
|
||||
$label = escapeSql($software['software_name']);
|
||||
logAudit("Software", "Keys", "Updated a key on license $label via API ($api_key_name)", $client_id, $software_id);
|
||||
logAudit("API", "Success", "Updated a key on license $label via API ($api_key_name)", $client_id);
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../update_output.php';
|
||||
@@ -691,6 +691,29 @@ function categoryPicker($kind, $keep = [], array $opts = []) {
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/*
|
||||
* A name posted for a kind held by name (a rack type, an asset note type) as
|
||||
* the picker would post it: one of the kind's current categories, in any case,
|
||||
* or $keep (what the record has now). '' for blank, null when it is none of
|
||||
* them - while the kind has none set up, any name is taken as it is.
|
||||
*/
|
||||
function categoryPickedName($kind, $name, $keep = '') {
|
||||
$name = trim((string) $name);
|
||||
if ($name === '') {
|
||||
return '';
|
||||
}
|
||||
$rows = categoryPicker($kind, (string) $keep !== '' ? [$keep] : []);
|
||||
if (!$rows) {
|
||||
return mb_substr($name, 0, 200);
|
||||
}
|
||||
foreach (array_keys($rows) as $value) {
|
||||
if (categoryNameKey($value) === categoryNameKey($name)) {
|
||||
return (string) $value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/* One picker <option> for a categoryPicker() row - its colour, icon and description, the value as the kind is held, and a note when archived or not set up. */
|
||||
function categoryOptionHtml(array $row, $selected = false) {
|
||||
$config = categoryKind($row['category_type'] ?? '');
|
||||
|
||||
Reference in New Issue
Block a user