Files
itflow/api/v1/kb_articles/kb_article_model.php

211 lines
10 KiB
PHP

<?php
/*
* API - Knowledge Base articles - what the endpoints share. The work is functions/kb.php's, as for
* the Knowledge Base pages: kbArticleFields() checks what is sent, kbArticleSave() saves it -
* keeping the version before - and articles written in Markdown are stored as the pages store them.
*
* Articles are no client's, so no client_id is needed to change one, as on the pages. Reading takes
* Support at View, changing Modify and deleting Full, with the Knowledge Base switched on in
* Admin > Settings > Modules.
*/
isset($mysqli) || die("Direct file access is not allowed");
require_once __DIR__ . '/../../../includes/load_global_settings.php';
// The Knowledge Base switched off
if (!kbOn()) {
http_response_code(403);
echo json_encode(['success' => 'False', 'message' => moduleOffText('kb')]);
exit();
}
/* A call that can't be done - its status and why, and stop. */
function kbApiFail($message, $status = 400) {
http_response_code($status);
echo json_encode(['success' => 'False', 'message' => $message]);
exit();
}
/* A list sent as an array or comma-separated - its trimmed, non-empty values. */
function kbApiList($value) {
$values = is_array($value) ? $value : explode(',', (string) $value);
return array_values(array_filter(array_map(function ($v) {
return trim((string) $v);
}, $values), 'strlen'));
}
/* 1, true, "yes" - on; anything else off. */
function kbApiOn($value) {
return in_array(strtolower(trim(var_export($value, true), "'")), ['1', 'true', 'yes', 'on'], true);
}
/*
* One article as the API gives it - $row from kbArticleRow() or kbArticleSelectSql(), $audience
* from kbAudiencesFor(). Clients it is shared with by name are the ones the key's user can reach.
* $versions, for one article asked for: its earlier versions, newest first.
*/
function kbApiRow(array $row, array $audience, $versions = null) {
$review = kbReviewState($row);
$published = !empty($row['kb_article_published_at']);
$public = $published && empty($row['kb_article_archived_at']) && $row['kb_article_visibility'] === 'public' && kbPublicOn();
$out = [
'kb_article_id' => intval($row['kb_article_id']),
'kb_article_title' => $row['kb_article_title'],
'kb_article_summary' => (string) $row['kb_article_summary'] !== '' ? $row['kb_article_summary'] : null,
'kb_article_content' => (string) $row['kb_article_content'],
'kb_article_markdown' => $row['kb_article_markdown'] ?? null,
'kb_article_format' => ($row['kb_article_markdown'] ?? null) !== null ? 'markdown' : 'html',
'kb_article_category_id' => intval($row['kb_article_category_id']),
'kb_article_category_name' => intval($row['kb_article_category_id']) > 0 ? ($row['category_name'] ?? null) : null,
'kb_article_published_at' => $row['kb_article_published_at'] ?? null,
'kb_article_visibility' => $row['kb_article_visibility'],
'kb_article_all_clients' => intval($row['kb_article_all_clients']) === 1,
'kb_article_client_tag_ids' => array_map('intval', array_column($audience['tags'] ?? [], 'tag_id')),
'kb_article_client_ids' => array_values(array_filter(array_map('intval', array_keys($audience['clients'] ?? [])), 'hasClientAccess')),
'kb_article_review_months' => intval($row['kb_article_review_months']),
'kb_article_reviewed_at' => $row['kb_article_reviewed_at'] ?? null,
'kb_article_next_review' => $review ? $review['next'] : null,
'kb_article_review_due' => $review ? (bool) $review['due'] : false,
'kb_article_views' => intval($row['kb_article_views'] ?? 0),
'kb_article_created_at' => $row['kb_article_created_at'],
'kb_article_created_by' => intval($row['kb_article_created_by']),
'kb_article_created_by_name' => $row['created_by_name'] ?? null,
'kb_article_updated_at' => $row['kb_article_updated_at'] ?? null,
'kb_article_updated_by' => intval($row['kb_article_updated_by']),
'kb_article_updated_by_name' => $row['updated_by_name'] ?? null,
'kb_article_archived_at' => $row['kb_article_archived_at'] ?? null,
'kb_article_public_url' => $public ? kbPublicUrl($row['kb_article_id'], $row['kb_article_title']) : null,
];
if ($versions !== null) {
$out['kb_article_versions'] = $versions;
}
return $out;
}
/* An article's earlier versions, newest first - when each was saved, by whom, its title and whether it was in Markdown. */
function kbApiVersions($kb_article_id) {
global $mysqli;
$versions = [];
$sql = mysqli_query($mysqli, "SELECT kb_article_version_id, kb_article_version_title, kb_article_version_created_at, kb_article_version_created_by,
" . kbMarkdownSql('kb_article_version_markdown') . ", user_name
FROM kb_article_versions LEFT JOIN users ON user_id = kb_article_version_created_by AND kb_article_version_created_by > 0
WHERE kb_article_version_kb_article_id = " . intval($kb_article_id) . "
ORDER BY kb_article_version_created_at DESC, kb_article_version_id DESC");
while ($sql && $row = mysqli_fetch_assoc($sql)) {
$versions[] = [
'kb_article_version_id' => intval($row['kb_article_version_id']),
'kb_article_version_title' => $row['kb_article_version_title'],
'kb_article_version_created_at' => $row['kb_article_version_created_at'],
'kb_article_version_created_by' => intval($row['kb_article_version_created_by']),
'kb_article_version_created_by_name' => $row['user_name'] ?? null,
'kb_article_version_format' => $row['kb_article_version_markdown'] !== null ? 'markdown' : 'html',
];
}
return $versions;
}
/*
* What kbArticleFields() reads, from what the API was sent, over the article as it stands ($row,
* kbArticleRow()) - or a new article's starting values: a draft for agents only, reviewed every
* year. Anything left out stays as it is. [fields, error].
*/
function kbApiFields(array $post, $row = null) {
if ($row) {
$in = [
'title' => $row['kb_article_title'],
'summary' => (string) $row['kb_article_summary'],
'category_id' => intval($row['kb_article_category_id']),
'review_months' => intval($row['kb_article_review_months']),
'published' => !empty($row['kb_article_published_at']),
] + kbAudienceFields($row);
// Clients already on it stay, even ones the key's user can't reach - as on the pages
$in['keep_client_ids'] = $in['client_ids'];
} else {
$in = ['title' => '', 'summary' => '', 'category_id' => 0, 'review_months' => 12, 'published' => false,
'visibility' => 'agents', 'all_clients' => false, 'client_tag_ids' => [], 'client_ids' => [], 'keep_client_ids' => []];
}
if (array_key_exists('kb_article_title', $post)) {
$in['title'] = (string) $post['kb_article_title'];
}
if (array_key_exists('kb_article_summary', $post)) {
$in['summary'] = (string) $post['kb_article_summary'];
}
if (array_key_exists('kb_article_category_id', $post)) {
$category_id = intval($post['kb_article_category_id']);
if ($category_id > 0 && kbCategoryId($category_id) === 0) {
return [[], "kb_article_category_id $category_id isn't a Knowledge Base category - categories/read.php?kind=kb lists them, and 0 is none."];
}
$in['category_id'] = max(0, $category_id);
}
if (array_key_exists('kb_article_review_months', $post)) {
$in['review_months'] = intval($post['kb_article_review_months']);
if (!isset(KB_REVIEW_MONTHS[$in['review_months']])) {
return [[], 'kb_article_review_months is how often it is reviewed: 0 (never), 3, 6, 12 or 24.'];
}
}
if (array_key_exists('kb_article_published', $post)) {
$in['published'] = kbApiOn($post['kb_article_published']);
}
// Who can read it
if (array_key_exists('kb_article_visibility', $post)) {
$in['visibility'] = strtolower(trim((string) $post['kb_article_visibility']));
if (!isset(KB_VISIBILITY[$in['visibility']])) {
return [[], 'kb_article_visibility is agents, clients or public.'];
}
}
if (array_key_exists('kb_article_all_clients', $post)) {
$in['all_clients'] = kbApiOn($post['kb_article_all_clients']);
}
if (array_key_exists('kb_article_client_tag_ids', $post)) {
$asked = array_map('intval', kbApiList($post['kb_article_client_tag_ids']));
$missing = array_diff($asked, kbClientTagIds($asked));
if ($missing) {
return [[], 'kb_article_client_tag_ids: ' . implode(', ', $missing) . " isn't a client tag - tags/read.php?kind=client lists them."];
}
$in['client_tag_ids'] = $asked;
}
if (array_key_exists('kb_article_client_ids', $post)) {
$asked = array_map('intval', kbApiList($post['kb_article_client_ids']));
$missing = array_diff($asked, kbClientIds($asked, $in['keep_client_ids']));
if ($missing) {
return [[], 'kb_article_client_ids: ' . implode(', ', $missing) . " isn't a client the user linked to this API key can reach."];
}
// The clients it is shared with that the key's user can't reach aren't in what they read (kbApiRow()), so they stay
$unseen = array_values(array_filter($in['keep_client_ids'], function ($id) {
return !hasClientAccess($id);
}));
$in['client_ids'] = array_values(array_unique(array_merge($asked, $unseen)));
}
// What it says - in Markdown, as HTML, or as it is
if (array_key_exists('kb_article_markdown', $post)) {
if (!kbMarkdownReady()) {
return [[], 'Articles in Markdown come with database 4.4.3 - run the database update from Maintenance > Update first.'];
}
$in['format'] = 'markdown';
$in['markdown'] = (string) $post['kb_article_markdown'];
} elseif (array_key_exists('kb_article_content', $post)) {
$in['content'] = (string) $post['kb_article_content'];
} elseif ($row && ($row['kb_article_markdown'] ?? null) !== null) {
$in['format'] = 'markdown';
$in['markdown'] = $row['kb_article_markdown'];
} elseif ($row) {
$in['content'] = (string) $row['kb_article_content'];
}
return kbArticleFields($in);
}
/* The article named by kb_article_id - or a 404. */
function kbApiArticle($kb_article_id) {
$row = kbArticleRow($kb_article_id);
if (!$row) {
kbApiFail('No Knowledge Base article has that kb_article_id.', 404);
}
return $row;
}