added login requirement to view articles to protect against potential data scrapping, with redirect functionality to the article the user attempts to view prior to login.

This commit is contained in:
2025-04-12 13:40:51 -06:00
parent f4dfaba1c6
commit 8d78a7d182
11 changed files with 102 additions and 195 deletions
+21 -74
View File
@@ -5,7 +5,6 @@ function article_ids_by_newest()
{
$conn = get_connection();
$results = $conn->query('SELECT article_id FROM article ORDER BY published_at DESC');
$ids = [];
while ($row = $results->fetch_assoc())
@@ -18,7 +17,6 @@ function article_ids_by_oldest()
{
$conn = get_connection();
$results = $conn->query('SELECT article_id FROM article ORDER BY published_at ASC');
$ids = [];
while ($row = $results->fetch_assoc())
@@ -29,12 +27,7 @@ function article_ids_by_oldest()
function articles_ids_by_author($author_id)
{
$conn = get_connection();
$stmt = $conn->prepare('SELECT article_id FROM article WHERE author_id = ? ORDER BY published_at DESC');
$stmt->bind_param('i', $author_id);
$stmt->execute();
$results = $stmt->get_result();
$results = exec_stmt('SELECT article_id FROM article WHERE author_id = ? ORDER BY published_at DESC', 'i', $author_id);
$ids = [];
while ($row = $results->fetch_assoc())
@@ -57,12 +50,7 @@ function article_tags()
function article_ids_by_tag($tag)
{
$conn = get_connection();
$stmt = $conn->prepare('SELECT article_id FROM article WHERE article.article_id IN ( SELECT article_tags.article_id FROM article_tags INNER JOIN tags ON article_tags.tag_id = tags.tag_id WHERE tag = ?) ORDER BY published_at DESC');
$stmt->bind_param('s', $tag);
$stmt->execute();
$results = $stmt->get_result();
$results = exec_stmt('SELECT article_id FROM article WHERE article.article_id IN ( SELECT article_tags.article_id FROM article_tags INNER JOIN tags ON article_tags.tag_id = tags.tag_id WHERE tag = ?) ORDER BY published_at DESC', 's', $tag);
$ids = [];
while ($row = $results->fetch_assoc())
@@ -76,17 +64,8 @@ function article_ids_by_tag($tag)
*/
function article_page_from_markdown($article_id)
{
$conn = get_connection();
$stmt = $conn->prepare('SELECT * FROM article WHERE article_id = ?');
$stmt->bind_param('i', $article_id);
$stmt->execute();
$article = $stmt->get_result()->fetch_assoc();
$stmt = $conn->prepare('SELECT user_id, username, profile_picture FROM user WHERE user_id = ?');
$stmt->bind_param('i', $article['author_id']);
$stmt->execute();
$author = $stmt->get_result()->fetch_assoc();
$article = exec_stmt('SELECT * FROM article WHERE article_id = ?', 'i', $article_id)->fetch_assoc();
$author = exec_stmt('SELECT user_id, username, profile_picture FROM user WHERE user_id = ?', 'i', $article['author_id'])->fetch_assoc();
$markdown = read_file_one_string($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/article.md');
$parsedown = new Parsedown();
@@ -142,16 +121,9 @@ function article_filters()
*/
function article_card($article_id)
{
$conn = get_connection();
$stmt = $conn->prepare('SELECT * FROM article WHERE article_id = ?');
$stmt->bind_param('i', $article_id);
$stmt->execute();
$article = $stmt->get_result()->fetch_assoc();
$article = exec_stmt('SELECT * FROM article WHERE article_id = ?', 'i', $article_id)->fetch_assoc();
$stmt = $conn->prepare('SELECT tag FROM article_tags INNER JOIN tags ON article_tags.tag_id = tags.tag_id WHERE article_id = ?');
$stmt->bind_param('i', $article_id);
$stmt->execute();
$tags_results = $stmt->get_result();
$tags_results = exec_stmt('SELECT tag FROM article_tags INNER JOIN tags ON article_tags.tag_id = tags.tag_id WHERE article_id = ?', 'i', $article_id);
$tags_html = '';
if ($tags_results != null) {
@@ -163,10 +135,7 @@ function article_card($article_id)
$tags_html .= '</div>';
}
$stmt = $conn->prepare('SELECT user_id, username, profile_picture FROM user WHERE user_id = ?');
$stmt->bind_param('i', $article['author_id']);
$stmt->execute();
$author = $stmt->get_result()->fetch_assoc();
$author = exec_stmt('SELECT user_id, username, profile_picture FROM user WHERE user_id = ?', 'i', $article['author_id'])->fetch_assoc();
$card = '
<div class="article_card">
@@ -197,11 +166,7 @@ function article_card($article_id)
function create_article($author_id, $title, $excerpt, $tags, $markdown_file_contents)
{
$conn = get_connection();
$stmt = $conn->prepare('INSERT INTO article (author_id, title, excerpt, published_at) VALUES (?, ?, ?, CURRENT_TIMESTAMP)');
$stmt->bind_param('iss', $author_id, $title, $excerpt);
$stmt->execute();
$id = mysqli_insert_id($conn);
$id = exec_stmt('INSERT INTO article (author_id, title, excerpt, published_at) VALUES (?, ?, ?, CURRENT_TIMESTAMP)', 'iss', $author_id, $title, $excerpt);
$path = $_ENV['ARTICLES_FQ_PATH'] . $id . '/';
mkdir($path);
@@ -212,16 +177,17 @@ function create_article($author_id, $title, $excerpt, $tags, $markdown_file_cont
if (count($tags) > 0) {
$tag_insert_stmt = 'INSERT INTO article_tags (article_id, tag_id) VALUES ';
$types = '';
for ($i = 0; $i < count($tags); $i++) {
$tag_insert_stmt .= '(' . $id . ', ?)';
if ($i < count($tags) - 1)
$tag_insert_stmt .= ', ';
$types .= 'i';
}
$stmt = $conn->prepare($tag_insert_stmt);
$stmt->bind_param('i', ...$tags);
$stmt->execute();
exec_stmt($tag_insert_stmt, $types, ...$tags);
}
return $id;
@@ -229,64 +195,45 @@ function create_article($author_id, $title, $excerpt, $tags, $markdown_file_cont
function update_article($article_id, $title, $excerpt, $tags, $markdown_file_contents)
{
$conn = get_connection();
$stmt = $conn->prepare('UPDATE article SET title = ?, excerpt = ?, updated_at = CURRENT_TIMESTAMP WHERE article_id = ?');
$stmt->bind_param('ssi', $title, $excerpt, $article_id);
$stmt->execute();
exec_stmt('UPDATE article SET title = ?, excerpt = ?, updated_at = CURRENT_TIMESTAMP WHERE article_id = ?', 'ssi', $title, $excerpt, $article_id);
$path = $_ENV['ARTICLES_FQ_PATH'] . $article_id . '/';
$fs = fopen($path . 'article.md', 'w');
fwrite($fs, $markdown_file_contents);
fclose($fs);
$stmt = $conn->prepare('DELETE from article_tags WHERE article_id = ?');
$stmt->bind_param('i', $article_id);
$stmt->execute();
exec_stmt('DELETE from article_tags WHERE article_id = ?', 'i', $article_id);
if (count($tags) > 0) {
$tag_insert_stmt = 'INSERT INTO article_tags (article_id, tag_id) VALUES ';
$input = '';
$types = '';
for ($i = 0; $i < count($tags); $i++) {
$tag_insert_stmt .= '(' . $article_id . ', ?)';
if ($i < count($tags) - 1)
$tag_insert_stmt .= ', ';
$input .= 'i';
$types .= 'i';
}
$stmt = $conn->prepare($tag_insert_stmt);
$stmt->bind_param($input, ...$tags);
$stmt->execute();
exec_stmt($tag_insert_stmt, $types, ...$tags);
}
}
function delete_article($article_id)
{
$conn = get_connection();
$stmt = $conn->prepare('DELETE FROM article_tags WHERE article_id = ?');
$stmt->bind_param('i', $article_id);
$stmt->execute();
$stmt = $conn->prepare('DELETE FROM article WHERE article_id = ?');
$stmt->bind_param('i', $article_id);
$stmt->execute();
exec_stmt('DELETE FROM article_tags WHERE article_id = ?', 'i', $article_id);
exec_stmt('DELETE FROM article WHERE article_id = ?', 'i', $article_id);
delete_dir($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/');
}
function delete_articles_by_author($author_id)
{
$conn = get_connection();
$stmt = $conn->prepare('DELETE FROM article WHERE author_id = ?');
$stmt->bind_param('i', $author_id);
$stmt->execute();
exec_stmt('DELETE FROM article WHERE author_id = ?', 'i', $author_id);
}
function increment_read_counter($article_id)
{
$conn = get_connection();
$stmt = $conn->prepare('UPDATE article SET read_count = read_count + 1 WHERE article_id = ?');
$stmt->bind_param('i', $article_id);
$stmt->execute();
exec_stmt('UPDATE article SET read_count = read_count + 1 WHERE article_id = ?', 'i', $article_id);
}