From 2b401cb405a3d7f846576aa941beb0116d892316 Mon Sep 17 00:00:00 2001 From: johnnyq Date: Sat, 24 Oct 2026 14:03:40 -0400 Subject: [PATCH] Custom Links rebuilt: tabs for the agent menu, top bar, client portal, admin menu and reports menu with counts plus All and Archived, search by name, address or description, sortable columns, each link with its icon, description, address and new-tab marker, links whose address can't work flagged; drag into order on a menu's tab or move up and down, new links at the end of their menu in place of the Order number; one add and edit window with a live preview of the link in its menu, an icon picker with search, a description shown on hover and a new-tab switch that follows the address until changed; copy, open, archive, restore and confirmed delete; addresses limited to web addresses, ITFlow pages, mailto, tel and the remote tool schemes asset links allow, with javascript and data refused and old ones drawn as going nowhere; menus read one shared helper, the agent menu's links sit under Tools even without Reports, the admin menu's get a Links heading, new-tab links get a small arrow, links without an icon get the link arrow and every link shows its description on hover; fixes mailto and tel links showing as unsupported in the side menus, the portal menu taking any address, the delete message missing the name and the new-tab tick never showing; edits audit what changed from what to what; adds functions/custom_link.php and js/custom_link_form.js, no database change; changelog --- CHANGELOG.md | 11 + admin/custom_links.php | 312 ++++++++++++------ admin/includes/side_nav.php | 35 +- admin/modals/custom_link/custom_link_add.php | 63 +--- admin/modals/custom_link/custom_link_edit.php | 85 ++--- admin/modals/custom_link/custom_link_form.php | 103 ++++++ admin/post/custom_link.php | 150 ++++++--- agent/includes/side_nav.php | 38 +-- agent/reports/includes/reports_side_nav.php | 35 +- client/functions.php | 12 +- client/includes/header.php | 4 +- css/itflow_custom.css | 67 +++- functions.php | 1 + functions/custom_link.php | 265 +++++++++++++++ includes/top_nav.php | 34 +- js/custom_link_form.js | 180 ++++++++++ 16 files changed, 1024 insertions(+), 371 deletions(-) create mode 100644 admin/modals/custom_link/custom_link_form.php create mode 100644 functions/custom_link.php create mode 100644 js/custom_link_form.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 25f71b480..c8f214fdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1065,6 +1065,14 @@ If you are coming from 26.08 or earlier, the database update still has to be run - Removing a card says first which recurring invoices stop autopaying with it. It is taken off Stripe as well, each of those recurring invoices says in its history that its autopay stopped and why, and when Stripe can't be reached the card is still removed and you are told it is saved there. - What goes through it: each provider shows its card payments in the last 30 days and when the last came, what its fees came to, and how many are still to record - flagged once they are more than two days old, as the nightly run should have had them. Record fees now does what the nightly run does, and says what it recorded, what Stripe hasn't settled yet and what it wouldn't say. Check connection asks Stripe whose account the key is, and says so - or why it isn't connected. - Settings > Invoices and billing says how card payments stand, under Paid online: none set up, turned off, or on - with live or test keys, where payments go, the card limit and how many came in the last 30 days. The Settings overview flags test keys, card payments going into an archived account, and Stripe's fees not being recorded, and the Invoices and billing tile says whether cards are on. +- **Custom Links** (rebuilt) + - Admin > Custom Links has a tab for each menu a link can show in - the agent menu, the top bar, the client portal, the admin menu and the reports menu - with how many each holds, plus All and Archived. Search looks at the name, the address and the description. + - Each link shows its icon, what it is for, where it goes and whether it opens in a new tab. A link whose address can't work is flagged. + - On a menu's tab, drag the links into the order the menu shows them in, or use Move up and Move down. A new link goes at the end of its menu - the Order number is gone. + - New Link and Edit are one window with a preview of the link in its menu, as it will look. There's an icon picker with search, and a description that shows when the link is hovered. Open in a new tab switches itself on for another site and off for a page of ITFlow, until you change it. + - A link can be copied, to put it in a second menu, archived to hide it without losing it, restored, or opened to try it. Delete asks first. + - The address has to be a web address, a page of ITFlow, mailto:, tel:, or one of the remote tools and shares that asset links open (rdp:, ssh:, smb: and the like). A javascript: or data: address is refused, and one saved before now goes nowhere. + - In the menus: the agent menu's links sit under Tools, with the heading even for a role without Reports. The admin menu's links get a heading of their own, Links. A link that opens in a new tab has a small arrow, a link without an icon gets the link arrow, and every link shows its description on hover - in the top bar as well as its name. - **AI Providers** (rebuilt) - Admin > AI Providers lists each provider with its kind and address, whether its key is saved (it is never shown), whether it runs on your own network, whether passwords and keys go to it as they are, its models and what each is for, and its requests and tokens in the last 30 days, with how many failed. Above the list, which model General, Tickets, Documentation and Billing each use - their own, or General's. Search finds a provider by a model on it too, and the list sorts by name, kind, models, requests or when it was last used. - Add and Edit are one window. Pick its kind - OpenAI, Anthropic (Claude), Azure OpenAI, Google Gemini, Mistral, Groq, OpenRouter, Ollama, LM Studio, or any other OpenAI-compatible service - and its address and name are filled in, with a link to where its keys are made. Test asks it for its models: it says it answered and how many there are, or why not - a key it turned away, an address that isn't there or doesn't answer - beside the field. Save checks first the same way, and a provider that doesn't answer right now (one on your network that is off, say) can still be saved, once it has said so. The key is no longer put into the window; left empty it stays, and Remove the saved key takes it off. @@ -1277,6 +1285,8 @@ If you are coming from 26.08 or earlier, the database update still has to be run - API: `credentials/create` and `credentials/update` ignored `credential_vendor_id` and `credential_software_id`, and took another client's contact or asset. The vendor and license are linked now, and another client's contact, asset, vendor or license is left off. - API: `assets/update` without `asset_ip`, `asset_mac` or `asset_network_id` wiped those on the asset's primary interface. - API: `tickets/resolve` answered success False although it had resolved the ticket. `software/read` by `software_name` failed with a database error. `clients/create` without `client_currency_code` made a client with no currency - it gets the company's now. `certificates/create` said only that it failed - it says what was wrong. `clients/archive` logged as a contact being archived. A request that wasn't GET or POST printed its method back. +- Custom links: an email (mailto:) or phone (tel:) link in the agent, admin or reports menu went to "unsupported://" instead. The client portal's menu took any address, javascript: included. +- Custom links: deleting one said "Custom Link deleted" without its name, and the list's new-tab tick never showed. - Ticket emails: resolving a ticket with the Resolve button or on the board emailed the client a Resolved email with nothing about the ticket in it. It now carries the conversation. - Email parser: a reply sent as plain text kept the whole email it quoted - it is now cut at the reply line, like an HTML reply. - Ticket link: reopening a resolved ticket from it left the resolution SLA as it was, and closing it didn't stop the SLA clock. Both now work as they do from the client portal. @@ -1875,6 +1885,7 @@ If you are coming from 26.08 or earlier, the database update still has to be run - Guest ticket page: `guest/guest_view_ticket.php` is built on the portal's ticket helpers in `client/functions.php` (`portalTicketState()`, `portalAvatarHtml()`, `portalDateTime()`, `portalTicketEventText()`) and sends `Referrer-Policy: no-referrer` (the key is in the address), `X-Frame-Options: DENY` and `frame-ancestors 'none'`. `ticketByGuestKey($ticket_id, $url_key)` (`functions/ticket.php`) returns `ticketRow()` only when the key matches exactly (`hash_equals()`); the page, the handlers and the new `guest/guest_ticket_attachment.php` use it, and `ticketGuestPath($row)` is the page's address from `guest/`. The attachment endpoint follows `client/ticket_attachment.php`'s rules (never an internal or archived reply's file, inline only for `getInlineViewableMimeTypes()`), never serves `Original-parsed-email.eml`, and logs each download. - Guest ticket page: in `guest/guest_post.php`, `close_ticket`, `reopen_ticket` and `add_ticket_feedback` are POST only, with `ticket_id` and `url_key`; the GET forms are gone. Each acts once, on the state it expects, so a second press logs nothing. Reopen calls `resetTicketResolutionSla()` and `syncTicketSlaClock()` and `appNotify()`; close calls `syncTicketSlaClock()`. `add_ticket_feedback` takes Good or Bad only. `ticket_calendar` (GET) sends the booking's calendar file. `ticketScheduleRow($ticket_id)` and `ticketScheduleIcs($row, $schedule)` are what `ticketSetSchedule()` used to build inline, so both give the same UID and event. The CSS class `.itflow-portal-todo-wrap` lets a portal to-do row's title wrap instead of being cut off. - Ticket emails: `emailHtml()` takes a `history` block (a heading and items of who, organisation, action, when and plain text, or a `gap` count) drawn after the button. `emailPlainText($html, $limit)` (`functions/email.php`) is HTML as plain text, keeping line breaks, dropping pictures and the reply line, and cut at a word. `ticketEmailHistory($row, $opts)` (`functions/ticket.php`) builds the history: public and client replies newest first, never internal, archived or the portal's "Ticket ... by X." lines, then the request. `skip` leaves out the reply the email shows and anything newer, `request` and `replies` choose the parts, and `limit` (4) caps the replies. `ticketEmailExcerpt()` takes off the parser's "Email from" line. `ticketEmailContent()` and `ticketAgentEmailContent()` add it per kind and set the preview line through `ticketEmailPreview()`. `ticketMailColleaguesNew()` shows the details unless `ticket_source` is Email. `cron/ticket_email_parser.php` also cuts a reply at a plain-text reply line, quoted or not. +- Custom links: new `functions/custom_link.php` holds the link helpers. `customLinkLocations()` lists the menus (1 agent menu, 2 top bar, 3 client portal, 4 admin menu, 5 reports menu). `customLinkUriProblem()` and `customLinkHref()` check addresses: escapeUrl()'s schemes plus mailto and tel, relative paths allowed, anything else drawn as `#`. `customLinksFor($location)` is a menu's current links, read once a page, with `href` and `icon` added; `customLinkNavItemHtml()` draws a side menu line. Also `customLinkFromInput()`, `customLinkProblem()`, `customLinkCreate()`, `customLinkUpdate()` (returns the from-to changes for the audit log), `customLinkCopy()`, `customLinkReorder()`, `customLinkMenuIds()` and `customLinkIconChoices()`. `admin/includes/side_nav.php`, `agent/includes/side_nav.php`, `agent/reports/includes/reports_side_nav.php`, `includes/top_nav.php` and `portalNav()` read `customLinksFor()`; the portal's nav items take a `title`. `admin/post/custom_link.php` adds `copy_custom_link`, `archive_custom_link`, `restore_custom_link`, `move_custom_link` and `reorder_custom_links` (POST, JSON, like `reorder_categories`). The add and edit windows share `admin/modals/custom_link/custom_link_form.php` with `js/custom_link_form.js`. `custom_link_description` is now used; `custom_link_order` is set by position. No database change. ### Library Updates diff --git a/admin/custom_links.php b/admin/custom_links.php index 96da211cd..0c2d1a774 100644 --- a/admin/custom_links.php +++ b/admin/custom_links.php @@ -1,151 +1,273 @@ Custom Links - your own links in ITFlow's menus: your RMM, your + * documentation, a booking page for clients. Tabs for each menu - the agent + * menu, the top bar, the client portal, the admin menu and the reports menu - + * with how many each holds, and the archived ones, which are kept but not + * shown. Each row shows the link as its menu draws it, what it is for, where + * it goes and whether it opens in a new tab. On a menu's tab they are in that + * menu's order and can be dragged into another (or moved up and down from + * the row's menu). The work is in functions/custom_link.php; the handlers + * are admin/post/custom_link.php. + */ + +// Default Column Sortby Filter - the order the menus show them in +$sort = "custom_link_order"; $order = "ASC"; require_once "includes/inc_all_admin.php"; -$sql = mysqli_query( - $mysqli, - "SELECT SQL_CALC_FOUND_ROWS custom_link_icon, custom_link_id, custom_link_location, custom_link_name, - custom_link_new_tab, custom_link_order, custom_link_uri FROM custom_links - WHERE custom_link_name LIKE '%$q%' - ORDER BY $sort $order LIMIT $record_from, $record_to" -); +$locations = customLinkLocations(); + +// The tabs: every menu, each menu, and the archived ones +$views = ['' => ['label' => 'All menus', 'icon' => 'fa-th-list', 'where' => 'custom_link_archived_at IS NULL', 'title' => 'Every link that shows in a menu']]; +foreach ($locations as $location => $location_config) { + $views[(string) $location] = ['label' => $location_config['label'], 'icon' => $location_config['icon'], 'where' => "custom_link_archived_at IS NULL AND custom_link_location = $location", 'title' => $location_config['about']]; +} +$views['archived'] = ['label' => 'Archived', 'icon' => 'fa-archive', 'where' => 'custom_link_archived_at IS NOT NULL', 'title' => 'Kept, but not shown in any menu']; +$show = (string) ($_GET['show'] ?? ''); +$show = isset($views[$show]) ? $show : ''; +$show_location = $show !== '' && $show !== 'archived' ? intval($show) : 0; + +// Only these sort - an unknown column would take the page down +$sortable_columns = [ + 'custom_link_order' => "custom_link_location ASC, custom_link_order $order, custom_link_name ASC", + 'custom_link_name' => "custom_link_name $order", + 'custom_link_location' => "custom_link_location $order, custom_link_order ASC, custom_link_name ASC", + 'custom_link_uri' => "custom_link_uri $order", +]; +if (!isset($sortable_columns[$sort])) { + $sort = 'custom_link_order'; +} + +$filters = "(custom_link_name LIKE '%$q%' OR custom_link_uri LIKE '%$q%' OR custom_link_description LIKE '%$q%')"; + +$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS custom_link_id, custom_link_name, custom_link_description, custom_link_uri, custom_link_new_tab, custom_link_icon, + custom_link_location, custom_link_order, custom_link_archived_at + FROM custom_links + WHERE $filters AND {$views[$show]['where']} + ORDER BY {$sortable_columns[$sort]}, custom_link_id ASC + LIMIT $record_from, $record_to"); $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); +$counts = mysqli_fetch_row(mysqli_query($mysqli, "SELECT " . implode(', ', array_map(function ($view) { + return "COALESCE(SUM({$view['where']}), 0)"; +}, $views)) . " FROM custom_links WHERE $filters")); +$view_counts = array_combine(array_keys($views), $counts); + +// Dragging into order: one menu's links, in its order, with nothing searched +$can_reorder = $show_location && $sort === 'custom_link_order' && $order === 'ASC' && $q === '' && $num_rows[0] > 1; + +$sort_link = function ($column, $label) use ($url_query_strings_sort, $sort, $disp, $order_icon) { + return '' + . $label . ($sort === $column ? " $order_icon" : '') . ''; +}; +$pill_url = function ($view) { + return '?' . http_build_query(array_filter(['q' => $_GET['q'] ?? '', 'show' => $view])); +}; + +$add_url = 'modals/custom_link/custom_link_add.php' . ($show_location ? "?location=$show_location" : ''); +$csrf = $_SESSION['csrf_token']; + ?>

Custom Links

- +
-
+
-
+
- - + +
+
- -
- - "> +
+
+ "> - - - - - + + + + + + + - + > "; - } else { - $custom_link_new_tab_display = ""; - } - $custom_link_order = intval($row['custom_link_order']); - if ($custom_link_order == 0 ) { - $custom_link_order_display = "-"; - } else { - $custom_link_order_display = $custom_link_order; - } - $custom_link_location = intval($row['custom_link_location']); - if ($custom_link_location == 1) { - $custom_link_location_display = "Main Side Nav"; - } elseif ($custom_link_location == 2) { - $custom_link_location_display = "Top Nav"; - } elseif ($custom_link_location == 3) { - $custom_link_location_display = "Client Portal Nav"; - } elseif ($custom_link_location == 4) { - $custom_link_location_display = "Admin Nav"; - } elseif ($custom_link_location == 5) { - $custom_link_location_display = "Reports Nav"; - } - + $name = escapeHtml($row['custom_link_name']); + $description = trim((string) $row['custom_link_description']); + $edit_url = "modals/custom_link/custom_link_edit.php?id=$custom_link_id"; + $location = intval($row['custom_link_location']); + $location_config = $locations[$location] ?? ['label' => "Menu $location", 'icon' => 'fa-question-circle', 'about' => 'Not a menu ITFlow has - it shows nowhere']; + $new_tab = intval($row['custom_link_new_tab']) === 1; + $problem = customLinkUriProblem($row['custom_link_uri']); + $where_html = '' . escapeHtml($location_config['label']) . ''; + $goes_html = '' . escapeHtml(customLinkUriDisplay($row['custom_link_uri'])) . '' + . ($new_tab ? ' ' : ''); ?> - - + + + + - - - - + + + - - - +
- - Name - - - - Order - - - - URI / New Tab - - - - Location - - ActionOrder
- - - +
+
+ +
+ + +
+ + +
Goes nowhere in its menu - edit its address
+ + +
Archived
+ + +
+ + +
+
+
- +
+ +
Drag them into the order the shows them in.
+ +
Clear the search to drag them into order.
+ 1 && $sort === 'custom_link_order') { ?> +
In the order each menu shows them - open a menu's tab to drag them into another.
+ 'custom links', 'icon' => 'fa-external-link-alt', - 'text' => 'Your own links, added to the menus.', - 'actions' => [['label' => 'New Link', 'modal' => 'modals/custom_link/custom_link_add.php']], + 'filtered' => $q !== '', + 'archived' => $show === 'archived', + 'views' => ['show'], + 'view' => $show_location ? [ + 'title' => 'Nothing in the ' . escapeHtml($locations[$show_location]['label']) . ' yet', + 'icon' => $locations[$show_location]['icon'], + 'text' => escapeHtml($locations[$show_location]['about']) . '.', + ] : [], + 'text' => 'Your own links in ITFlow\'s menus - your RMM, your documentation, a page for booking time with you in the client portal.', + 'actions' => [['label' => 'New Link', 'modal' => $add_url]], ]; + require_once "../includes/filter_footer.php"; ?> -
+ + + + + - - - - + // Your own links (Admin > Custom Links), under a heading of their own + $custom_links = customLinksFor(4); + if ($custom_links) { ?> + + diff --git a/admin/modals/custom_link/custom_link_add.php b/admin/modals/custom_link/custom_link_add.php index 592a6e341..e25e9ed0f 100644 --- a/admin/modals/custom_link/custom_link_add.php +++ b/admin/modals/custom_link/custom_link_add.php @@ -1,72 +1,31 @@ Custom Links, in the menu of the tab it was + * opened from (?location=). The fields are custom_link_form.php. + */ + require_once '../../includes/modal_header.php'; +$custom_link = null; +$default_location = intval($_GET['location'] ?? 1); + ob_start(); ?>
diff --git a/admin/modals/custom_link/custom_link_edit.php b/admin/modals/custom_link/custom_link_edit.php index e9e3d4e9f..d1d03aba7 100644 --- a/admin/modals/custom_link/custom_link_edit.php +++ b/admin/modals/custom_link/custom_link_edit.php @@ -1,25 +1,24 @@ + +if (!$custom_link) { ?> + +
@@ -27,62 +26,16 @@ ob_start();
- + +
+ + + +
+ +
+ + data-custom-link-name> +
+
+ +
+ +
+ + +
+
+
+ data-custom-link-new-tab> + +
+
+ +
+ +
+ + +
+ + + It was in a menu ITFlow no longer has - pick where it should show. + +
+ +
+ +
+ + +
+
+ +
+ + +
+ + +
+
+ + + + + + +
+
+
+ + diff --git a/admin/post/custom_link.php b/admin/post/custom_link.php index 1db1f8408..ec0008106 100644 --- a/admin/post/custom_link.php +++ b/admin/post/custom_link.php @@ -1,51 +1,82 @@ Custom Links: add and change a link, copy it, move it in + * its menu (dragged, or up and down), archive it, bring it back and delete + * it. The work is in functions/custom_link.php. */ defined('FROM_POST_HANDLER') || die("Direct file access is not allowed"); -if (isset($_POST['add_custom_link'])) { +if (isset($_POST['add_custom_link']) || isset($_POST['edit_custom_link'])) { validateCSRFToken(); - $name = escapeSql($_POST['name']); - $uri = escapeSql($_POST['uri']); - $new_tab = intval($_POST['new_tab'] ?? 0); - $icon = preg_replace("/[^0-9a-zA-Z-]/", "", escapeSql($_POST['icon'])); - $order = intval($_POST['order'] ?? 0); - $location = intval($_POST['location']); + $row = isset($_POST['edit_custom_link']) ? customLinkRow($_POST['custom_link_id'] ?? 0) : null; + if (isset($_POST['edit_custom_link']) && !$row) { + flashAlert("That link is gone - it may have been deleted.", 'error'); + redirect(); + } - mysqli_query($mysqli,"INSERT INTO custom_links SET custom_link_name = '$name', custom_link_uri = '$uri', custom_link_new_tab = $new_tab, custom_link_icon = '$icon', custom_link_order = $order, custom_link_location = $location"); + $fields = customLinkFromInput($_POST); + $problem = customLinkProblem($fields); + if ($problem !== '') { + flashAlert(escapeHtml($problem) . ' The link was not saved.', 'error'); + redirect(); + } - $custom_link_id = mysqli_insert_id($mysqli); + $name = escapeSql($fields['name']); + $where = escapeSql(customLinkLocations()[$fields['location']]['label']); - logAudit("Custom Link", "Create", "$session_name created custom link $name -> $uri", 0, $custom_link_id); - - flashAlert("Custom link $name created"); + if ($row) { + $custom_link_id = intval($row['custom_link_id']); + $changes = customLinkUpdate($row, $fields); + if ($changes) { + logAudit("Custom Link", "Edit", "$session_name changed custom link " . escapeSql($row['custom_link_name']) . ": " . escapeSql(implode('; ', $changes)), 0, $custom_link_id); + } + flashAlert("Link " . escapeHtml($fields['name']) . " saved"); + } else { + $custom_link_id = customLinkCreate($fields); + logAudit("Custom Link", "Create", "$session_name added custom link $name to the $where -> " . escapeSql($fields['uri']), 0, $custom_link_id); + flashAlert("Link " . escapeHtml($fields['name']) . " added to the " . escapeHtml(customLinkLocations()[$fields['location']]['label'])); + } redirect(); } -if (isset($_POST['edit_custom_link'])) { +if (isset($_GET['copy_custom_link'])) { validateCSRFToken(); - $custom_link_id = intval($_POST['custom_link_id']); - $name = escapeSql($_POST['name']); - $uri = escapeSql($_POST['uri']); - $new_tab = intval($_POST['new_tab'] ?? 0); - $icon = preg_replace("/[^0-9a-zA-Z-]/", "", escapeSql($_POST['icon'])); - $order = intval($_POST['order'] ?? 0); - $location = intval($_POST['location']); + $row = customLinkRow($_GET['copy_custom_link']); + if ($row) { + $custom_link_id = customLinkCopy($row); + logAudit("Custom Link", "Create", "$session_name copied custom link " . escapeSql($row['custom_link_name']), 0, $custom_link_id); + flashAlert("Link " . escapeHtml($row['custom_link_name']) . " copied - change the copy, or move it to another menu"); + } - mysqli_query($mysqli,"UPDATE custom_links SET custom_link_name = '$name', custom_link_uri = '$uri', custom_link_new_tab = $new_tab, custom_link_icon = '$icon', custom_link_order = $order, custom_link_location = $location WHERE custom_link_id = $custom_link_id"); + redirect(); - logAudit("Custom Link", "Edit", "$session_name edited custom link $name -> $uri", 0, $custom_link_id); +} - flashAlert("Custom Link $name edited"); +if (isset($_GET['archive_custom_link']) || isset($_GET['restore_custom_link'])) { + + validateCSRFToken(); + + $archive = isset($_GET['archive_custom_link']); + $row = customLinkRow($archive ? $_GET['archive_custom_link'] : $_GET['restore_custom_link']); + if ($row) { + $custom_link_id = intval($row['custom_link_id']); + if ($archive) { + mysqli_query($mysqli, "UPDATE custom_links SET custom_link_archived_at = NOW() WHERE custom_link_id = $custom_link_id"); + } else { + // Back at the end of its menu + mysqli_query($mysqli, "UPDATE custom_links SET custom_link_archived_at = NULL, custom_link_order = " . customLinkNextOrder($row['custom_link_location']) . " WHERE custom_link_id = $custom_link_id"); + } + logAudit("Custom Link", $archive ? "Archive" : "Restore", "$session_name " . ($archive ? 'archived' : 'restored') . " custom link " . escapeSql($row['custom_link_name']), 0, $custom_link_id); + flashAlert("Link " . escapeHtml($row['custom_link_name']) . " " . ($archive ? 'archived - it no longer shows in its menu' : 'restored')); + } redirect(); @@ -55,20 +86,65 @@ if (isset($_GET['delete_custom_link'])) { validateCSRFToken(); - $custom_link_id = intval($_GET['delete_custom_link']); - - // Get Custom Link name and uri for logging - $sql = mysqli_query($mysqli,"SELECT custom_link_name, custom_link_uri FROM custom_links WHERE custom_link_id = $custom_link_id"); - $row = mysqli_fetch_assoc($sql); - $custom_link_name = escapeSql($row['custom_link_name']); - $custom_link_uri = escapeSql($row['custom_link_uri']); - - mysqli_query($mysqli,"DELETE FROM custom_links WHERE custom_link_id = $custom_link_id"); - - logAudit("Custom Link", "Delete", "$session_name deleted custom link $custom_link_name -> $custom_link_uri"); - - flashAlert("Custom Link $name deleted", 'error'); + $row = customLinkRow($_GET['delete_custom_link']); + if ($row) { + $custom_link_id = intval($row['custom_link_id']); + mysqli_query($mysqli, "DELETE FROM custom_links WHERE custom_link_id = $custom_link_id"); + logAudit("Custom Link", "Delete", "$session_name deleted custom link " . escapeSql($row['custom_link_name']) . " -> " . escapeSql($row['custom_link_uri']), 0, $custom_link_id); + flashAlert("Link " . escapeHtml($row['custom_link_name']) . " deleted", 'error'); + } redirect(); } + +if (isset($_GET['move_custom_link'])) { + + validateCSRFToken(); + + // One place up or down in its menu + $row = customLinkRow($_GET['move_custom_link']); + if ($row && !$row['custom_link_archived_at']) { + $ids = customLinkMenuIds($row['custom_link_location']); + $at = array_search(intval($row['custom_link_id']), $ids, true); + $up = ($_GET['direction'] ?? '') === 'up'; + $to = $up ? $at - 1 : $at + 1; + if ($at !== false && isset($ids[$to])) { + [$ids[$at], $ids[$to]] = [$ids[$to], $ids[$at]]; + customLinkReorder($row['custom_link_location'], $ids); + logAudit("Custom Link", "Edit", "$session_name moved custom link " . escapeSql($row['custom_link_name']) . ($up ? ' up' : ' down'), 0, intval($row['custom_link_id'])); + } + } + + redirect(); + +} + +if (isset($_POST['reorder_custom_links'])) { + + // Dragged into order on Admin > Custom Links - answers with JSON, as the page saves it in the background + header('Content-Type: application/json'); + if (!hash_equals((string) ($_SESSION['csrf_token'] ?? ''), (string) ($_POST['csrf_token'] ?? ''))) { + echo json_encode(['ok' => false, 'error' => 'The page is out of date - reload it']); + exit; + } + + $location = intval($_POST['location'] ?? 0); + if (!isset(customLinkLocations()[$location])) { + echo json_encode(['ok' => false, 'error' => 'No such menu']); + exit; + } + + // The ones dragged, a page of them, in their place among the menu's others - offset is how many come before them + $current = customLinkMenuIds($location); + $ids = array_values(array_intersect(array_unique(array_map('intval', (array) ($_POST['custom_link_ids'] ?? []))), $current)); + $new_order = array_values(array_diff($current, $ids)); + array_splice($new_order, min(max(0, intval($_POST['offset'] ?? 0)), count($new_order)), 0, $ids); + if (customLinkReorder($location, $new_order)) { + logAudit("Custom Link", "Edit", "$session_name reordered the custom links in the " . escapeSql(customLinkLocations()[$location]['label'])); + } + + echo json_encode(['ok' => true]); + exit; + +} diff --git a/agent/includes/side_nav.php b/agent/includes/side_nav.php index 0d4677289..2fad4c047 100644 --- a/agent/includes/side_nav.php +++ b/agent/includes/side_nav.php @@ -251,8 +251,13 @@ - = 1) { ?> + Custom Links) go under Tools too - with its heading even when Reports isn't shown + $custom_links = customLinksFor(1); + if (lookupUserPermission("module_reporting") >= 1 || $custom_links) { ?> + + = 1) { ?> - - - - - - + diff --git a/agent/reports/includes/reports_side_nav.php b/agent/reports/includes/reports_side_nav.php index 91a2b08aa..04883bb01 100644 --- a/agent/reports/includes/reports_side_nav.php +++ b/agent/reports/includes/reports_side_nav.php @@ -45,37 +45,14 @@ $reports_current = basename($_SERVER['PHP_SELF'], '.php'); + // Your own links (Admin > Custom Links) + $custom_links = customLinksFor(5); + if ($custom_links) { ?> - - - - + foreach ($custom_links as $custom_link) { + echo customLinkNavItemHtml($custom_link); + } ?> diff --git a/client/functions.php b/client/functions.php index af6df82da..fc7999cdb 100644 --- a/client/functions.php +++ b/client/functions.php @@ -218,16 +218,14 @@ function portalNav() { } // The links an admin set to show in the portal (Admin > Custom Links) - $sql = mysqli_query($mysqli, "SELECT custom_link_id, custom_link_icon, custom_link_name, custom_link_new_tab, custom_link_uri FROM custom_links - WHERE custom_link_location = 3 AND custom_link_archived_at IS NULL ORDER BY custom_link_order ASC, custom_link_name ASC"); - while ($row = mysqli_fetch_assoc($sql)) { - $icon = preg_replace('/[^a-z0-9-]/', '', strtolower((string) $row['custom_link_icon'])); + foreach (customLinksFor(3) as $row) { $nav['link-' . intval($row['custom_link_id'])] = [ 'label' => (string) $row['custom_link_name'], - 'icon_class' => 'fas fa-fw fa-' . ($icon !== '' ? $icon : 'external-link-alt'), - 'href' => (string) $row['custom_link_uri'], + 'icon_class' => 'fas fa-fw fa-' . $row['icon'], + 'href' => $row['href'], 'new_tab' => intval($row['custom_link_new_tab']) === 1, - 'pages' => [basename((string) $row['custom_link_uri'])], + 'title' => trim((string) $row['custom_link_description']), + 'pages' => [basename((string) parse_url($row['href'], PHP_URL_PATH))], ]; } diff --git a/client/includes/header.php b/client/includes/header.php index 8b3b69798..a9331b04d 100644 --- a/client/includes/header.php +++ b/client/includes/header.php @@ -147,7 +147,7 @@ $portal_contact_name = (string) ($contact['contact_name'] ?? '');
- > + > @@ -215,7 +215,7 @@ $portal_contact_name = (string) ($contact['contact_name'] ?? ''); - > + >
Your account
diff --git a/css/itflow_custom.css b/css/itflow_custom.css index 1a4fa9e8d..e19bd224c 100644 --- a/css/itflow_custom.css +++ b/css/itflow_custom.css @@ -3128,9 +3128,74 @@ a:focus-visible .itflow-category-name { cursor: grab; touch-action: none; } -[data-category-sortable] .sortable-ghost > * { +[data-category-sortable] .sortable-ghost > *, +[data-custom-link-sortable] .sortable-ghost > * { background-color: var(--bs-tertiary-bg) !important; } +/* Admin > Custom Links - the link window's preview, drawn the way its menu + draws it: a line of a side menu (always dark), an icon on the top bar's + theme colour, or an item of the client portal's light top bar */ +.itflow-link-preview { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: .5rem .75rem; + padding: .5rem .75rem; + border: 1px solid var(--bs-border-color); + border-radius: var(--bs-border-radius); + background-color: var(--bs-tertiary-bg); +} +.itflow-link-preview-stage { + display: flex; + flex: 1 1 12rem; + align-items: center; + min-width: 0; + padding: .35rem .5rem; + border-radius: var(--bs-border-radius); + background-color: #343a40; +} +.itflow-link-preview-item { + display: inline-flex; + align-items: center; + gap: .5rem; + width: 100%; + min-width: 0; + padding: .35rem .6rem; + border-radius: .25rem; + color: #c2c7d0; +} +.itflow-link-preview-item > span { + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} +.itflow-link-preview-tab { + margin-left: auto; + font-size: .7rem; + opacity: .7; +} +.itflow-link-preview[data-location="2"] .itflow-link-preview-stage { + justify-content: flex-end; + background-color: var(--itflow-accent, #007bff); +} +.itflow-link-preview[data-location="2"] .itflow-link-preview-item { + width: auto; + color: #fff; +} +.itflow-link-preview[data-location="2"] .itflow-link-preview-item > span, +.itflow-link-preview[data-location="2"] .itflow-link-preview-tab, +.itflow-link-preview[data-location="3"] .itflow-link-preview-tab { + display: none; +} +.itflow-link-preview[data-location="3"] .itflow-link-preview-stage { + border: 1px solid var(--bs-border-color-translucent); + background-color: var(--bs-body-bg); +} +.itflow-link-preview[data-location="3"] .itflow-link-preview-item { + width: auto; + color: var(--bs-secondary-color); + font-weight: 500; +} /* The canned response picker above a reply box - a searchable list, each response with a line or two of what it says - and the / menu in the reply */ .itflow-canned-menu { diff --git a/functions.php b/functions.php index 2fc95318f..9552f8f3e 100644 --- a/functions.php +++ b/functions.php @@ -59,6 +59,7 @@ require_once __DIR__ . '/functions/reports.php'; require_once __DIR__ . '/functions/client_overview.php'; require_once __DIR__ . '/functions/tags.php'; require_once __DIR__ . '/functions/categories.php'; +require_once __DIR__ . '/functions/custom_link.php'; require_once __DIR__ . '/functions/ticket_fields.php'; require_once __DIR__ . '/functions/ticket.php'; require_once __DIR__ . '/functions/ticket_template.php'; diff --git a/functions/custom_link.php b/functions/custom_link.php new file mode 100644 index 000000000..49c0e9c2a --- /dev/null +++ b/functions/custom_link.php @@ -0,0 +1,265 @@ + Custom + * Links): the agent menu, the top bar (as an icon), the client portal's menu, + * the admin menu or the reports menu. Each has a name, the address it goes + * to, a line about it (shown when the link is hovered), an icon, whether it + * opens in a new tab, and its place in its menu. Archived ones are kept but + * not shown. The handlers are admin/post/custom_link.php; the menus read + * customLinksFor(). + */ + +/** Where a link can show - [location => label, icon, what it is]. */ +function customLinkLocations() { + return [ + 1 => ['label' => 'Agent menu', 'icon' => 'fa-bars', 'about' => 'The menu down the left of every agent page, under Tools'], + 2 => ['label' => 'Top bar', 'icon' => 'fa-window-maximize', 'about' => 'An icon in the bar along the top of every agent page - its name shows on hover'], + 3 => ['label' => 'Client portal', 'icon' => 'fa-user-friends', 'about' => 'The client portal\'s menu, for every contact who signs in'], + 4 => ['label' => 'Admin menu', 'icon' => 'fa-user-shield', 'about' => 'The menu down the left of the admin pages, for admins only'], + 5 => ['label' => 'Reports menu', 'icon' => 'fa-chart-line', 'about' => 'The menu down the left of the reports'], + ]; +} + +/* Icons offered for a link - the ones links tend to want first, then the category window's. Any Font Awesome solid icon's name works. */ +const CUSTOM_LINK_ICONS = [ + 'external-link-alt', 'link', 'globe', 'book', 'question-circle', 'headset', 'life-ring', 'tachometer-alt', 'chart-line', 'chart-pie', + 'video', 'calendar-alt', 'download', 'upload', 'home', 'cloud', 'shield-alt', 'lock', 'key', 'server', +]; + +/** The icons the link window offers, with $keep (the link's own) first when it isn't among them. */ +function customLinkIconChoices($keep = '') { + $icons = array_values(array_unique(array_merge(CUSTOM_LINK_ICONS, CATEGORY_ICONS))); + $keep = categoryIcon($keep); + if ($keep !== '' && !in_array($keep, $icons, true)) { + array_unshift($icons, $keep); + } + return $icons; +} + +/** A link's icon as drawn - its own, or the external link arrow when it has none. */ +function customLinkIcon($icon) { + $icon = categoryIcon($icon); + return $icon !== '' ? $icon : 'external-link-alt'; +} + +/* + * What is wrong with an address a link would go to - '' when nothing. A page + * of ITFlow ("/agent/clients.php"), a web address, an email (mailto:), a + * phone number (tel:), or one of the remote tools and shares asset links + * open (rdp:, ssh:, smb: and the like - escapeUrl()). Never javascript:, + * data: or anything else that would run in the page. + */ +function customLinkUriProblem($uri) { + $uri = trim((string) $uri); + if ($uri === '') { + return 'Add the address it goes to.'; + } + if (mb_strlen($uri) > 500) { + return 'That address is longer than 500 characters.'; + } + if (preg_match('/[\x00-\x1F\x7F]/', $uri)) { + return 'That address has a line break or another control character in it.'; + } + if (preg_match('/^([a-z][a-z0-9+.-]*):/i', $uri, $m)) { + $allowed = ['http', 'https', 'mailto', 'tel', 'file', 'ftp', 'ftps', 'sftp', 'dav', 'webdav', 'caldav', 'carddav', 'ssh', 'telnet', 'smb', 'rdp', 'vnc', + 'rustdesk', 'anydesk', 'connectwise', 'splashtop', 'sip', 'sips', 'ldap', 'ldaps']; + if (!in_array(strtolower($m[1]), $allowed, true)) { + return 'A link can\'t start with "' . $m[1] . ':" - use a web address (https://...), a page of ITFlow (/agent/...), mailto: or tel:.'; + } + } + return ''; +} + +/** The address a menu link goes to - as saved when it may be a link (customLinkUriProblem()), else '#', so nothing saved before the check can run. Raw - escape it. */ +function customLinkHref($uri) { + $uri = trim((string) $uri); + return customLinkUriProblem($uri) === '' ? $uri : '#'; +} + +/** A link's row - null when there is none. */ +function customLinkRow($custom_link_id) { + global $mysqli; + $custom_link_id = intval($custom_link_id); + if (!$custom_link_id) { + return null; + } + return mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT custom_link_id, custom_link_name, custom_link_description, custom_link_uri, custom_link_new_tab, custom_link_icon, + custom_link_location, custom_link_order, custom_link_created_at, custom_link_updated_at, custom_link_archived_at + FROM custom_links WHERE custom_link_id = $custom_link_id LIMIT 1")) ?: null; +} + +/* + * The links a menu shows, in their order - $location from customLinkLocations(). + * Read once a page. Each row with 'href' (customLinkHref()) and 'icon' + * (customLinkIcon()) added, both raw. + */ +function customLinksFor($location) { + global $mysqli; + static $by_location = null; + if ($by_location === null) { + $by_location = []; + $sql = mysqli_query($mysqli, "SELECT custom_link_id, custom_link_name, custom_link_description, custom_link_uri, custom_link_new_tab, custom_link_icon, custom_link_location + FROM custom_links WHERE custom_link_archived_at IS NULL ORDER BY custom_link_order ASC, custom_link_name ASC"); + while ($sql && $row = mysqli_fetch_assoc($sql)) { + $row['href'] = customLinkHref($row['custom_link_uri']); + $row['icon'] = customLinkIcon($row['custom_link_icon']); + $by_location[intval($row['custom_link_location'])][] = $row; + } + } + return $by_location[intval($location)] ?? []; +} + +/* + * A link's fields from a form, cleaned - name, description, uri, new_tab, + * icon and location (one of customLinkLocations(), the agent menu when it + * isn't). Raw: escape them for SQL with customLinkSetSql(). + */ +function customLinkFromInput(array $in) { + $location = intval($in['location'] ?? 1); + return [ + 'name' => mb_substr(trim((string) ($in['name'] ?? '')), 0, 200), + 'description' => mb_substr(trim((string) ($in['description'] ?? '')), 0, 500), + 'uri' => trim((string) ($in['uri'] ?? '')), + 'new_tab' => !empty($in['new_tab']) ? 1 : 0, + 'icon' => categoryIcon($in['icon'] ?? ''), + 'location' => isset(customLinkLocations()[$location]) ? $location : 1, + ]; +} + +/** What is wrong with a link's fields - '' when nothing. */ +function customLinkProblem(array $f) { + if ($f['name'] === '') { + return 'Give the link a name.'; + } + return customLinkUriProblem($f['uri']); +} + +/** The SQL SET list for a link's fields (customLinkFromInput()). */ +function customLinkSetSql(array $f) { + return "custom_link_name = '" . escapeSql($f['name']) . "', custom_link_description = " . ($f['description'] !== '' ? "'" . escapeSql($f['description']) . "'" : 'NULL') + . ", custom_link_uri = '" . escapeSql($f['uri']) . "', custom_link_new_tab = " . intval($f['new_tab']) + . ", custom_link_icon = '" . escapeSql($f['icon']) . "', custom_link_location = " . intval($f['location']); +} + +/** The place after the last link in a menu. */ +function customLinkNextOrder($location) { + global $mysqli; + return intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COALESCE(MAX(custom_link_order), 0) FROM custom_links WHERE custom_link_location = " . intval($location)))[0] ?? 0) + 1; +} + +/** Add a link, at the end of its menu - its id. */ +function customLinkCreate(array $f) { + global $mysqli; + mysqli_query($mysqli, "INSERT INTO custom_links SET " . customLinkSetSql($f) . ", custom_link_order = " . customLinkNextOrder($f['location'])); + return mysqli_insert_id($mysqli); +} + +/* + * Save a link's fields - one moved to another menu goes to the end of it. + * What changed, as audit log phrases ("address from x to y") - raw. + */ +function customLinkUpdate(array $row, array $f) { + global $mysqli; + $order = intval($row['custom_link_location']) !== $f['location'] ? ", custom_link_order = " . customLinkNextOrder($f['location']) : ''; + mysqli_query($mysqli, "UPDATE custom_links SET " . customLinkSetSql($f) . "$order WHERE custom_link_id = " . intval($row['custom_link_id'])); + $locations = customLinkLocations(); + $changes = []; + $was = [ + 'name' => (string) $row['custom_link_name'], 'description' => (string) $row['custom_link_description'], 'uri' => (string) $row['custom_link_uri'], + 'new_tab' => intval($row['custom_link_new_tab']), 'icon' => categoryIcon($row['custom_link_icon']), 'location' => intval($row['custom_link_location']), + ]; + foreach (['name' => 'name', 'uri' => 'address', 'description' => 'description', 'icon' => 'icon'] as $key => $label) { + if ($was[$key] !== $f[$key]) { + $changes[] = "$label from " . ($was[$key] !== '' ? $was[$key] : 'none') . ' to ' . ($f[$key] !== '' ? $f[$key] : 'none'); + } + } + if ($was['location'] !== $f['location']) { + $changes[] = 'moved from the ' . ($locations[$was['location']]['label'] ?? 'menu ' . $was['location']) . ' to the ' . $locations[$f['location']]['label']; + } + if ($was['new_tab'] !== $f['new_tab']) { + $changes[] = $f['new_tab'] ? 'opens in a new tab' : 'opens in the same tab'; + } + return $changes; +} + +/** A copy of a link, named "... (copy)", at the end of the same menu - its id. */ +function customLinkCopy(array $row) { + $f = customLinkFromInput([ + 'name' => mb_substr((string) $row['custom_link_name'], 0, 193) . ' (copy)', 'description' => $row['custom_link_description'], 'uri' => $row['custom_link_uri'], + 'new_tab' => $row['custom_link_new_tab'], 'icon' => $row['custom_link_icon'], 'location' => $row['custom_link_location'], + ]); + return customLinkCreate($f); +} + +/* + * Put a menu's links in this order - the ids as listed, then the rest (the + * archived ones) after them in the order they had. Numbered from 1. How + * many moved. + */ +function customLinkReorder($location, array $ids) { + global $mysqli; + $location = intval($location); + $rows = []; + $sql = mysqli_query($mysqli, "SELECT custom_link_id, custom_link_order FROM custom_links WHERE custom_link_location = $location + ORDER BY custom_link_archived_at IS NOT NULL, custom_link_order ASC, custom_link_name ASC"); + while ($row = mysqli_fetch_assoc($sql)) { + $rows[intval($row['custom_link_id'])] = intval($row['custom_link_order']); + } + $ordered = []; + foreach ($ids as $id) { + $id = intval($id); + if (isset($rows[$id]) && !in_array($id, $ordered, true)) { + $ordered[] = $id; + } + } + foreach (array_keys($rows) as $id) { + if (!in_array($id, $ordered, true)) { + $ordered[] = $id; + } + } + $moved = 0; + foreach ($ordered as $position => $id) { + if ($rows[$id] !== $position + 1) { + mysqli_query($mysqli, "UPDATE custom_links SET custom_link_order = " . ($position + 1) . " WHERE custom_link_id = $id"); + $moved++; + } + } + return $moved; +} + +/** The current links of a menu, in order - their ids. */ +function customLinkMenuIds($location) { + global $mysqli; + $ids = []; + $sql = mysqli_query($mysqli, "SELECT custom_link_id FROM custom_links WHERE custom_link_location = " . intval($location) . " AND custom_link_archived_at IS NULL + ORDER BY custom_link_order ASC, custom_link_name ASC"); + while ($row = mysqli_fetch_row($sql)) { + $ids[] = intval($row[0]); + } + return $ids; +} + +/* + * A link as a line of a side menu - the agent, admin and reports menus: its + * icon and name, what it is for on hover, and a small arrow when it opens in + * a new tab. Lit up on the page it goes to, for a page of ITFlow. + */ +function customLinkNavItemHtml(array $row) { + $new_tab = intval($row['custom_link_new_tab']) === 1; + $path = (string) parse_url($row['href'], PHP_URL_PATH); + $active = !preg_match('#^([a-z][a-z0-9+.-]*:)?//#i', $row['href']) && !preg_match('/^[a-z][a-z0-9+.-]*:/i', $row['href']) + && $path !== '' && basename($path) === basename((string) ($_SERVER['PHP_SELF'] ?? '')); + $about = trim((string) $row['custom_link_description']); + return ''; +} + +/** A link's address as a list shows it - without https:// and a trailing slash. */ +function customLinkUriDisplay($uri) { + return rtrim(preg_replace('#^https?://#i', '', trim((string) $uri)), '/'); +} diff --git a/includes/top_nav.php b/includes/top_nav.php index 4ec15c36c..5f90b1c21 100644 --- a/includes/top_nav.php +++ b/includes/top_nav.php @@ -27,33 +27,15 @@