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

91 lines
4.1 KiB
PHP

<?php
/*
* API - Knowledge Base articles - Read
* GET /api/v1/kb_articles/read.php
*
* The Knowledge Base's articles, newest change first, or one of them.
*
* Parameters (GET):
* api_key required - Your API key
* kb_article_id optional - One article, archived or not, with its earlier versions
* q optional - Articles with these words in the title, the summary or the text
* category_id optional - Articles in this Knowledge Base category, 0 for none
* visibility optional - agents, clients or public: who can read them
* published optional - 1 for published articles only, 0 for drafts only
* client_id optional - The articles this client's portal shows (published, shared with it)
* archived optional - 1 to include archived articles
* limit optional - Max rows to return (default 50)
* offset optional - Offset for pagination (default 0)
*
* Each article: kb_article_id, kb_article_title, kb_article_summary, kb_article_content (as
* HTML), kb_article_markdown (as written for an article in Markdown, null for rich text),
* kb_article_format (markdown or html), kb_article_category_id and _name, kb_article_published_at
* (null for a draft), kb_article_visibility, kb_article_all_clients, kb_article_client_tag_ids,
* kb_article_client_ids (the clients it is shared with by name that the key's user can reach),
* kb_article_review_months, kb_article_reviewed_at, kb_article_next_review, kb_article_review_due,
* kb_article_views, kb_article_created_at, _by and _by_name, kb_article_updated_at, _by and
* _by_name, kb_article_archived_at, kb_article_public_url (its address on the public help center,
* when it is on it) and tags. One article asked for by its id also has kb_article_versions.
*/
require_once '../validate_api_key.php';
require_once '../require_get_method.php';
require_once 'kb_article_model.php';
if (isset($_GET['kb_article_id'])) {
$where = 'kb_article_id = ' . intval($_GET['kb_article_id']);
} else {
$where = intval($_GET['archived'] ?? 0) === 1 ? '1 = 1' : 'kb_article_archived_at IS NULL';
if (isset($_GET['category_id'])) {
$where .= ' AND kb_article_category_id = ' . max(0, intval($_GET['category_id']));
}
if (isset($_GET['visibility'])) {
$visibility = strtolower(trim((string) $_GET['visibility']));
if (!isset(KB_VISIBILITY[$visibility])) {
kbApiFail('visibility is agents, clients or public.');
}
$where .= " AND kb_article_visibility = '$visibility'";
}
if (isset($_GET['published'])) {
$where .= intval($_GET['published']) === 1 ? ' AND kb_article_published_at IS NOT NULL' : ' AND kb_article_published_at IS NULL';
}
if (!empty($client_id_supplied)) {
$kb_client_id = intval($_GET['client_id']);
if (!apiUserCanAccessClient($kb_client_id)) {
apiDeny("The user linked to this API key does not have access to that client.");
}
$where .= ' AND ' . kbClientSql($kb_client_id);
}
$q = trim((string) ($_GET['q'] ?? ''));
if ($q !== '') {
$q_sql = escapeSql($q);
$where .= " AND (kb_article_title LIKE '%$q_sql%' OR kb_article_summary LIKE '%$q_sql%' OR MATCH(kb_article_content_raw) AGAINST ('$q_sql'))";
}
}
$sql = mysqli_query($mysqli, kbArticleSelectSql() . " WHERE $where
ORDER BY COALESCE(kb_article_updated_at, kb_article_created_at) DESC, kb_article_id DESC
LIMIT $limit OFFSET $offset");
if ($sql && mysqli_num_rows($sql) > 0) {
$found = [];
while ($row = mysqli_fetch_assoc($sql)) {
$found[intval($row['kb_article_id'])] = $row;
}
$audiences = kbAudiencesFor(array_keys($found));
$one = isset($_GET['kb_article_id']);
$rows = [];
foreach ($found as $kb_article_id => $row) {
$rows[] = kbApiRow($row, $audiences[$kb_article_id] ?? [], $one ? kbApiVersions($kb_article_id) : null);
}
$rows = tagApiAttach($rows, 'kb_article');
echo json_encode(['success' => 'True', 'count' => count($rows), 'data' => $rows]);
exit();
}
// Output - nothing found
require_once '../read_output.php';