1135 lines
27 KiB
C
1135 lines
27 KiB
C
#define _GNU_SOURCE
|
|
#include <mysql/mysql.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include <malloc.h>
|
|
#include <time.h>
|
|
#include <assert.h>
|
|
|
|
static MYSQL* conn2maria();
|
|
static int AddPost(char title[], int authorId, char datePosted[], char post[], int tags[], size_t tagSize, MYSQL* con);
|
|
static int EditPost(int postID, char title[], char post[], MYSQL* con);
|
|
static int AddFilePost(int authorID, char filePath[], MYSQL* con);
|
|
static int EditFilePost(char filePath[], int postID, MYSQL* con);
|
|
static int BlogWrite(MYSQL* con);
|
|
static int BlogEdit(int postID, MYSQL* con);
|
|
static int PullPostData(char* strings[6], int postID, MYSQL* con);
|
|
static int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** dates, MYSQL* con);
|
|
static int AddTag(char tagTitle[], MYSQL* con);
|
|
static int DeleteTag(int tagID, MYSQL* con);
|
|
static int EditTag(int tagID, char tagTitle[], MYSQL* con);
|
|
static int AssignTag(int tagID, int postID, MYSQL* con);
|
|
static int RetractTag(int tagID, int postID, MYSQL* con);
|
|
static int AddAuthor(char firstName[], char lastName[], char email[], MYSQL* con);
|
|
static int PullAuthorData(char* strings[4], int authorID, MYSQL* con);
|
|
static int ChangeAuthor(int postID, int authorID, MYSQL* con);
|
|
static int TogglePost(int postID, MYSQL* con);
|
|
static bool IsEnabled(int postID, MYSQL* con);
|
|
static int PostIDs(int** arr, MYSQL* con);
|
|
static int ActivePostIDs(int** arr, MYSQL* con);
|
|
static int AuthorIDs(int** arr, MYSQL* con);
|
|
|
|
|
|
// Connects to the MariaDB database and returns the connection
|
|
MYSQL* conn2maria()
|
|
{
|
|
MYSQL *con = mysql_init(NULL);
|
|
|
|
if (con == NULL)
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
exit(1);
|
|
}
|
|
|
|
if (mysql_real_connect(con, "localhost", "adam", "1234", "mydb", 0, NULL, 0) == NULL)
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
return con;
|
|
}
|
|
|
|
// Adds a new post to the database, with given values
|
|
int AddPost(char title[], int authorId, char datePosted[], char post[], int tags[], size_t tagSize, MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
char* query;
|
|
if(0 > asprintf(&query,
|
|
"INSERT INTO blog_posts (title,author_id,date_posted,post,state) VALUES (\"%s\", \"%d\", \"%s\", \"%s\", \"0\")",
|
|
title,
|
|
authorId,
|
|
datePosted,
|
|
post
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, query))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
free(query);
|
|
|
|
if (mysql_query(con, "SELECT LAST_INSERT_ID()"))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result = mysql_store_result(con);
|
|
int num_fields = mysql_num_fields(result);
|
|
|
|
MYSQL_ROW row;
|
|
row = mysql_fetch_row(result);
|
|
int postID = atoi(row[0]);
|
|
|
|
if (tags == NULL) return postID;
|
|
|
|
// for each in tags
|
|
for (int i = 0; i < tagSize / sizeof(int); i++)
|
|
{
|
|
AssignTag(tags[i], postID, con);
|
|
}
|
|
|
|
return postID;
|
|
}
|
|
|
|
// Edits a post in the database, to the new given values
|
|
int EditPost(int postID, char title[], char post[], MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
char* selectQuery;
|
|
if(0 > asprintf(&selectQuery,
|
|
"SELECT * FROM blog_posts WHERE id=(%d)",
|
|
postID
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, selectQuery))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
free(selectQuery);
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result = mysql_store_result(con);
|
|
int num_fields = mysql_num_fields(result);
|
|
|
|
if (result == NULL)
|
|
{
|
|
free(selectQuery);
|
|
exit(1);
|
|
}
|
|
|
|
if (mysql_fetch_row(result) == NULL)
|
|
{
|
|
free(selectQuery);
|
|
exit(1);
|
|
}
|
|
|
|
free(selectQuery);
|
|
|
|
char* updateQuery;
|
|
if (0 > asprintf(&updateQuery, "UPDATE blog_posts SET title=(\"%s\"), post=(\"%s\") WHERE id=(%d)",
|
|
title,
|
|
post,
|
|
postID
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, updateQuery))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
free(updateQuery);
|
|
|
|
return postID;
|
|
}
|
|
|
|
// Adds a post using file data
|
|
int AddFilePost(int authorID, char filePath[], MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
if (authorID <= 0) authorID = 1;
|
|
|
|
time_t t = time(NULL);
|
|
struct tm *tm = localtime(&t);
|
|
char date[64];
|
|
assert(strftime(date, sizeof(date), "%F", tm));
|
|
|
|
FILE* FilePointer;
|
|
int BufferLength = 8192;
|
|
char Buffer[BufferLength];
|
|
char StringEndIndicator[] = "\0";
|
|
|
|
FilePointer = fopen(filePath, "r");
|
|
|
|
fgets(Buffer, BufferLength, FilePointer);
|
|
|
|
// Get the substrings startTest = Buffer[0:3] and endTest = Buffer[-5:-1]
|
|
int LineLength = strlen(Buffer);
|
|
char startTest[5] = {Buffer[0], Buffer[1], Buffer[2], Buffer[3], StringEndIndicator[0]};
|
|
char endTest[6] = {Buffer[LineLength - 6], Buffer[LineLength - 5], Buffer[LineLength - 4], Buffer[LineLength - 3], Buffer[LineLength - 2], StringEndIndicator[0]};
|
|
|
|
if (!(strcmp(startTest, "<h3>") == 0 && strcmp(endTest, "</h3>") == 0))
|
|
{
|
|
printf("Start Test : '%s'\nEnd Test : '%s'\n", startTest, endTest);
|
|
return -1;
|
|
}
|
|
|
|
Buffer[LineLength - 6] = StringEndIndicator[0];
|
|
char* trimBuffer;
|
|
trimBuffer = &Buffer[4];
|
|
|
|
// Add a new post with correct title, author and date but with an empty post.
|
|
int postID = AddPost(trimBuffer, authorID, date, "", NULL, 0, con);
|
|
|
|
while (fgets(Buffer, BufferLength, FilePointer))
|
|
{
|
|
char* query;
|
|
if(0 > asprintf(&query,
|
|
"UPDATE blog_posts SET post = CONCAT(post, \"%s\n\") WHERE id=(%d)",
|
|
Buffer,
|
|
postID
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, query))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
free(query);
|
|
}
|
|
|
|
return postID;
|
|
}
|
|
|
|
// Edits a post using file data
|
|
int EditFilePost(char filePath[], int postID, MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
FILE* FilePointer;
|
|
int BufferLength = 8192;
|
|
char Buffer[BufferLength];
|
|
char StringEndIndicator[] = "\0";
|
|
|
|
FilePointer = fopen(filePath, "r");
|
|
|
|
fgets(Buffer, BufferLength, FilePointer);
|
|
|
|
// Get the substrings startTest = Buffer[0:3] and endTest = Buffer[-6:-1]
|
|
int LineLength = strlen(Buffer);
|
|
char startTest[5] = {Buffer[0], Buffer[1], Buffer[2], Buffer[3], StringEndIndicator[0]};
|
|
char endTest[6] = {Buffer[LineLength - 6], Buffer[LineLength - 5], Buffer[LineLength - 4], Buffer[LineLength - 3], Buffer[LineLength - 2], StringEndIndicator[0]};
|
|
|
|
if (!(strcmp(startTest, "<h3>") == 0 && strcmp(endTest, "</h3>") == 0))
|
|
{
|
|
printf("Start Test : %s\nEnd Test : %s\n", startTest, endTest);
|
|
return -1;
|
|
}
|
|
|
|
|
|
Buffer[LineLength - 6] = StringEndIndicator[0];
|
|
char* Title;
|
|
Title = &Buffer[4];
|
|
|
|
// Edits post with correct title and empties post.
|
|
EditPost(postID, Title, "", con);
|
|
|
|
while (fgets(Buffer, BufferLength, FilePointer))
|
|
{
|
|
char* query;
|
|
if(0 > asprintf(&query,
|
|
"UPDATE blog_posts SET post = CONCAT(post, \"%s\") WHERE id=(%d)",
|
|
Buffer,
|
|
postID
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, query))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
free(query);
|
|
}
|
|
|
|
return postID;
|
|
}
|
|
|
|
// Open a temporary file containing the standard template, and add the contents as a post
|
|
int BlogWrite(MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
FILE *fp;
|
|
char* tmpName = tmpnam(NULL);
|
|
|
|
fp = fopen(tmpName, "w+");
|
|
fprintf(fp, "<h3></h3>\n<p></p>\n");
|
|
fclose(fp);
|
|
|
|
char* command;
|
|
if(0 > asprintf(&command, "${EDITOR:-'vi'} %s", tmpName)) exit(1);
|
|
system(command);
|
|
|
|
int postID = AddFilePost(1, tmpName, con);
|
|
|
|
remove(tmpName);
|
|
|
|
return postID;
|
|
}
|
|
|
|
// Open a temporary file containing the contents of the given post and update the contents as a post
|
|
int BlogEdit(int postID, MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
FILE *fp;
|
|
char* tmpName = tmpnam(NULL);
|
|
|
|
char *result[6];
|
|
PullPostData(result, postID, NULL);
|
|
|
|
fp = fopen(tmpName, "w+");
|
|
fprintf(fp, "<h3>%s</h3>\n%s", result[1], result[2]);
|
|
fclose(fp);
|
|
|
|
char* command;
|
|
if(0 > asprintf(&command, "${EDITOR:-'vi'} %s", tmpName)) exit(1);
|
|
system(command);
|
|
|
|
EditFilePost(tmpName, postID, con);
|
|
|
|
remove(tmpName);
|
|
|
|
return postID;
|
|
}
|
|
|
|
// Pulls a single post's data into the supplied array
|
|
int PullPostData(char* strings[6], int postID, MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
char* query;
|
|
if(0 > asprintf(&query, "SELECT * FROM blog_posts WHERE id=(%d)", postID)) exit(1);
|
|
|
|
if (mysql_query(con, query))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
free(query);
|
|
|
|
MYSQL_RES *result = mysql_store_result(con);
|
|
int num_fields = mysql_num_fields(result);
|
|
|
|
MYSQL_ROW row;
|
|
if (!(row = mysql_fetch_row(result))) return postID;
|
|
|
|
for(int i = 0; i < num_fields; i++)
|
|
{
|
|
strings[i] = row[i];
|
|
}
|
|
|
|
return postID;
|
|
}
|
|
|
|
// Pull all coloums of all posts except for the post coloumn
|
|
int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** dates, MYSQL* con)
|
|
{
|
|
if (con == NULL)
|
|
{
|
|
con = conn2maria();
|
|
}
|
|
|
|
if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts"))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result = mysql_store_result(con);
|
|
MYSQL_ROW row;
|
|
row = mysql_fetch_row(result);
|
|
|
|
int count = atoi(row[0]);
|
|
|
|
free(result);
|
|
|
|
if (mysql_query(con, "SELECT id,title,author_id,date_posted FROM blog_posts WHERE state=0;"))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result2 = mysql_store_result(con);
|
|
|
|
// Allocate the array that was passed by reference
|
|
// To be one larger than needed
|
|
*IDs = (int *)malloc(sizeof(int) * (count + 1));
|
|
*authors = (int *)malloc(sizeof(int) * (count + 1));
|
|
*dates = (int *)malloc(sizeof(int) * (count + 1));
|
|
*Titles = (char **)malloc((count + 1) * sizeof(char *));
|
|
|
|
for (int i = 0; i < count + 1; ++i)
|
|
{
|
|
Titles[i] = malloc(sizeof(char) * 255);
|
|
}
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
{
|
|
row = mysql_fetch_row(result2);
|
|
if (row == NULL) break;
|
|
|
|
*(*IDs + i) = atoi(row[0]);
|
|
*(*authors + i) = atoi(row[2]);
|
|
*(*dates + i) = atoi(row[3]);
|
|
|
|
for (int y = 0; y < 255; ++y)
|
|
{
|
|
Titles[i][y] = row[1][y];
|
|
}
|
|
}
|
|
|
|
// Set the last element in the array passed by reference to -1,
|
|
// As an ID can never be negative, it can ensure that it never
|
|
// Enters unknown memory.
|
|
//*(*arr + count) = -1;
|
|
*(*IDs + count) = -1;
|
|
*(*authors + count) = -1;
|
|
*(*dates + count) = -1;
|
|
*(*Titles + count) = "";
|
|
|
|
return count;
|
|
}
|
|
|
|
// Adds a new tag
|
|
int AddTag(char tagTitle[], MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
char* insertQuery;
|
|
if (0 > asprintf(&insertQuery, "INSERT INTO tags (name) VALUES (\"%s\")",
|
|
tagTitle
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, insertQuery))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
free(insertQuery);
|
|
|
|
if (mysql_query(con, "SELECT LAST_INSERT_ID()"))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result = mysql_store_result(con);
|
|
int num_fields = mysql_num_fields(result);
|
|
|
|
MYSQL_ROW row;
|
|
row = mysql_fetch_row(result);
|
|
int tagID = atoi(row[0]);
|
|
|
|
return tagID;
|
|
}
|
|
|
|
// Removes a tag, and unassigns itself from any and all posts.
|
|
int DeleteTag(int tagID, MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
char* deleteQuery;
|
|
if (0 > asprintf(&deleteQuery, "DELETE FROM tags WHERE id=(%d)",
|
|
tagID
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, deleteQuery))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
free(deleteQuery);
|
|
|
|
char* deleteTagsQuery;
|
|
if (0 > asprintf(&deleteTagsQuery, "DELETE FROM blog_post_tags WHERE tag_id=(%d)",
|
|
tagID
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, deleteTagsQuery))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
free(deleteTagsQuery);
|
|
|
|
return tagID;
|
|
}
|
|
|
|
// Edit the tag name
|
|
int EditTag(int tagID, char tagTitle[], MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
char* selectQuery;
|
|
if(0 > asprintf(&selectQuery,
|
|
"SELECT * FROM tags WHERE id=(%d)",
|
|
tagID
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, selectQuery))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result = mysql_store_result(con);
|
|
int num_fields = mysql_num_fields(result);
|
|
|
|
MYSQL_ROW row;
|
|
|
|
if (mysql_fetch_row(result) == NULL)
|
|
{
|
|
free(selectQuery);
|
|
return tagID;
|
|
}
|
|
|
|
free(selectQuery);
|
|
|
|
char* updateQuery;
|
|
if (0 > asprintf(&updateQuery, "UPDATE tags SET name=(\"%s\") WHERE id=(%d)",
|
|
tagTitle,
|
|
tagID
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, updateQuery))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
free(updateQuery);
|
|
|
|
return tagID;
|
|
}
|
|
|
|
// Assign a post a given tag.
|
|
int AssignTag(int tagID, int postID, MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
char* selectQuery;
|
|
if(0 > asprintf(&selectQuery, "SELECT * FROM blog_post_tags WHERE tag_id=(%d) AND blog_post_id=(%d)",
|
|
tagID,
|
|
postID
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, selectQuery))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result = mysql_store_result(con);
|
|
int num_fields = mysql_num_fields(result);
|
|
|
|
if (result == NULL)
|
|
{
|
|
return postID;
|
|
}
|
|
|
|
MYSQL_ROW row;
|
|
|
|
if (mysql_fetch_row(result) == NULL)
|
|
{
|
|
free(selectQuery);
|
|
return postID;
|
|
}
|
|
|
|
free(selectQuery);
|
|
|
|
char* insertQuery;
|
|
if (0 > asprintf(&insertQuery, "INSERT INTO blog_post_tags (tag_id,blog_post_id) VALUES (%d, %d)",
|
|
tagID,
|
|
postID
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, insertQuery))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
free(insertQuery);
|
|
|
|
return postID;
|
|
}
|
|
|
|
// Unassign a tag from the given post
|
|
int RetractTag(int tagID, int postID, MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
char* deleteQuery;
|
|
if (0 > asprintf(&deleteQuery, "DELETE FROM blog_post_tags WHERE tag_id=(%d) AND blog_post_id=(%d)",
|
|
tagID,
|
|
postID
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, deleteQuery))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
free(deleteQuery);
|
|
|
|
return postID;
|
|
}
|
|
|
|
// Add an author
|
|
int AddAuthor(char firstName[], char lastName[], char email[], MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
char* insertQuery;
|
|
if (0 > asprintf(&insertQuery, "INSERT INTO people (first_name, last_name, email) VALUES (\"%s\", \"%s\", \"%s\")",
|
|
firstName,
|
|
lastName,
|
|
email
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, insertQuery))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
free(insertQuery);
|
|
|
|
if (mysql_query(con, "SELECT LAST_INSERT_ID()"))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result = mysql_store_result(con);
|
|
int num_fields = mysql_num_fields(result);
|
|
|
|
MYSQL_ROW row;
|
|
row = mysql_fetch_row(result);
|
|
int authorID = atoi(row[0]);
|
|
|
|
return authorID;
|
|
}
|
|
|
|
// Pull the full name and email of the given author ID
|
|
int PullAuthorData(char* strings[4], int authorID, MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
char* query;
|
|
if(0 > asprintf(&query, "SELECT * FROM people WHERE id=(%d)", authorID)) exit(1);
|
|
|
|
if (mysql_query(con, query))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
free(query);
|
|
|
|
MYSQL_RES *result = mysql_store_result(con);
|
|
int num_fields = mysql_num_fields(result);
|
|
|
|
MYSQL_ROW row;
|
|
|
|
if (!(row = mysql_fetch_row(result))) return authorID;
|
|
|
|
for(int i = 0; i < num_fields; i++)
|
|
{
|
|
strings[i] = row[i];
|
|
}
|
|
|
|
return authorID;
|
|
}
|
|
|
|
// Change the author of a given post
|
|
int ChangeAuthor(int postID, int authorID, MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
char* selectQuery;
|
|
if(0 > asprintf(&selectQuery,
|
|
"SELECT * FROM blog_posts WHERE id=(%d)",
|
|
postID
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, selectQuery))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result = mysql_store_result(con);
|
|
int num_fields = mysql_num_fields(result);
|
|
|
|
MYSQL_ROW row;
|
|
|
|
if (mysql_fetch_row(result) == NULL)
|
|
{
|
|
free(selectQuery);
|
|
return postID;
|
|
}
|
|
|
|
free(selectQuery);
|
|
|
|
char* updateQuery;
|
|
|
|
if (0 > asprintf(&updateQuery, "UPDATE blog_posts SET author_id=(%d) WHERE id=(%d)",
|
|
authorID,
|
|
postID
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, updateQuery))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
free(updateQuery);
|
|
|
|
return postID;
|
|
}
|
|
|
|
// Toggles the posts state, which determines whether the code ignores it.
|
|
int TogglePost(int postID, MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
char* selectQuery;
|
|
if(0 > asprintf(&selectQuery,
|
|
"SELECT * FROM blog_posts WHERE id=(%d)",
|
|
postID
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, selectQuery))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result = mysql_store_result(con);
|
|
int num_fields = mysql_num_fields(result);
|
|
|
|
MYSQL_ROW row;
|
|
row = mysql_fetch_row(result);
|
|
|
|
if (row == NULL) return postID;
|
|
|
|
int state = atoi(row[5]);
|
|
free(selectQuery);
|
|
|
|
char* updateQuery;
|
|
if (0 > asprintf(&updateQuery, "UPDATE blog_posts SET state=(%d) WHERE id=(%d)",
|
|
!state,
|
|
postID
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, updateQuery))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
free(updateQuery);
|
|
|
|
return postID;
|
|
}
|
|
|
|
// Return true if state is 0, false if it doesn't exist or 1
|
|
bool IsEnabled(int postID, MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
char* selectQuery;
|
|
if(0 > asprintf(&selectQuery,
|
|
"SELECT * FROM blog_posts WHERE id=(%d)",
|
|
postID
|
|
)) exit(1);
|
|
|
|
if (mysql_query(con, selectQuery))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result = mysql_store_result(con);
|
|
int num_fields = mysql_num_fields(result);
|
|
|
|
MYSQL_ROW row;
|
|
int postFound = 1;
|
|
|
|
row = mysql_fetch_row(result);
|
|
|
|
if (row == NULL) return false;
|
|
|
|
int state = atoi(row[5]);
|
|
free(selectQuery);
|
|
|
|
return !state;
|
|
}
|
|
|
|
// Returns the count, and pass an array of IDs for all posts by reference,
|
|
// the last element in the array is set to be negative as to prevent going into unallocated data.
|
|
int PostIDs(int** arr, MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts"))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result = mysql_store_result(con);
|
|
MYSQL_ROW row;
|
|
row = mysql_fetch_row(result);
|
|
|
|
int count = atoi(row[0]);
|
|
|
|
free(result);
|
|
|
|
if (mysql_query(con, "SELECT id FROM blog_posts"))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result2 = mysql_store_result(con);
|
|
|
|
// Allocate the array that was passed by reference to be one larger than needed
|
|
*arr = (int *)malloc(sizeof(int) * (count + 1));
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
{
|
|
row = mysql_fetch_row(result2);
|
|
if (row == NULL) break;
|
|
|
|
*(*arr + i) = atoi(row[0]);
|
|
}
|
|
|
|
// Set the last element in the array passed by reference to -1,
|
|
// As an ID can never be negative, it can ensure that it never
|
|
// Enters unknown memory.
|
|
*(*arr + count) = -1;
|
|
|
|
return count;
|
|
}
|
|
|
|
// Returns the count, and pass an array of IDs for all the posts that have state=0 by reference,
|
|
// the last element in the array is set to be negative as to prevent going into unallocated data.
|
|
int ActivePostIDs(int** arr, MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts WHERE state=(0)"))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result = mysql_store_result(con);
|
|
MYSQL_ROW row;
|
|
row = mysql_fetch_row(result);
|
|
|
|
int count = atoi(row[0]);
|
|
|
|
free(result);
|
|
|
|
if (mysql_query(con, "SELECT id FROM blog_posts WHERE state=(0)"))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result2 = mysql_store_result(con);
|
|
|
|
// Allocate the array that was passed by reference to be one larger than needed
|
|
*arr = (int *)malloc(sizeof(int) * (count + 1));
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
{
|
|
row = mysql_fetch_row(result2);
|
|
|
|
if (row == NULL) break;
|
|
|
|
*(*arr + i) = atoi(row[0]);
|
|
}
|
|
|
|
// Set the last element in the array passed by reference to -1,
|
|
// As an ID can never be negative, it can ensure that it never
|
|
// Enters unknown memory.
|
|
*(*arr + count) = -1;
|
|
|
|
return count;
|
|
}
|
|
|
|
// Returns the count, and pass an array of IDs for all authors by reference,
|
|
// the last element in the array is set to be negative as to prevent going into unallocated data.
|
|
int AuthorIDs(int** arr, MYSQL* con)
|
|
{
|
|
if (con == NULL) con = conn2maria();
|
|
|
|
if (mysql_query(con, "SELECT COUNT(*) FROM people"))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result = mysql_store_result(con);
|
|
MYSQL_ROW row;
|
|
row = mysql_fetch_row(result);
|
|
|
|
int count = atoi(row[0]);
|
|
|
|
free(result);
|
|
|
|
if (mysql_query(con, "SELECT id FROM people"))
|
|
{
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
mysql_close(con);
|
|
exit(1);
|
|
}
|
|
|
|
MYSQL_RES *result2 = mysql_store_result(con);
|
|
|
|
// Allocate the array that was passed by reference to be one larger than needed
|
|
*arr = (int *)malloc(sizeof(int) * (count + 1));
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
{
|
|
row = mysql_fetch_row(result2);
|
|
|
|
if (row == NULL) break;
|
|
|
|
*(*arr + i) = atoi(row[0]);
|
|
}
|
|
|
|
// Set the last element in the array passed by reference to -1,
|
|
// As an ID can never be negative, it can ensure that it never
|
|
// Enters unknown memory.
|
|
*(*arr + count) = -1;
|
|
|
|
return count;
|
|
}
|
|
|
|
// Main
|
|
int main(int argc, char *argv[])
|
|
{
|
|
/*
|
|
// ===================================
|
|
// Example of using 'PullPostData()'
|
|
// ===================================
|
|
|
|
// Init array of six strings
|
|
// Pull data of postID '1' into 'result'
|
|
char *result[6];
|
|
PullPostData(result, 42, NULL);
|
|
|
|
// Foreach in result
|
|
for (int i = 0; i < sizeof(result)/sizeof(result[0]); i++)
|
|
{
|
|
// Print contents on a new line
|
|
printf("%s\n", result[i]);
|
|
}
|
|
|
|
// ===================================
|
|
*/
|
|
|
|
/*
|
|
// ===================================
|
|
// Example of using 'PullAuthorData()'
|
|
// ===================================
|
|
|
|
// Init array of four strings
|
|
// Pull data of authorID '1' into 'result'
|
|
char *result[4];
|
|
PullAuthorData(result, 1, NULL);
|
|
|
|
// Foreach in result
|
|
for (int i = 0; i < sizeof(result)/sizeof(result[0]); i++)
|
|
{
|
|
// Print contents on a new line
|
|
printf("%s\n", result[i]);
|
|
}
|
|
// ===================================
|
|
*/
|
|
|
|
/*
|
|
// ===================================
|
|
// Example of using 'PostIDs()'
|
|
// ===================================
|
|
|
|
// Generate array of IDs
|
|
int *postIDs;
|
|
int count1 = PostIDs(&postIDs, NULL);
|
|
|
|
// Loop until element is negative i.e. the last
|
|
for (int i = 0; i < count1; ++i)
|
|
{
|
|
// Print contents on a new line
|
|
printf("%d\n", postIDs[i]);
|
|
}
|
|
|
|
// Free the memory
|
|
free(postIDs);
|
|
|
|
// ===================================
|
|
*/
|
|
|
|
/*
|
|
// ===================================
|
|
// Example of using 'ActivePostIDs()'
|
|
// ===================================
|
|
|
|
// Generate array of IDs
|
|
int *activePostIDs;
|
|
int count2 = ActivePostIDs(&activePostIDs, NULL);
|
|
|
|
// Loop until element is negative i.e. the last
|
|
for (int i = 0; i < count2; ++i)
|
|
{
|
|
// Print contents on a new line
|
|
printf("%d\n", activePostIDs[i]);
|
|
}
|
|
|
|
// Free the memory
|
|
free(activePostIDs);
|
|
|
|
// ===================================
|
|
*/
|
|
|
|
/*
|
|
// ===================================
|
|
// Example of using 'AuthorIDs()'
|
|
// ===================================
|
|
|
|
// Generate array of IDs
|
|
int *authorIDs;
|
|
int count3 = AuthorIDs(&authorIDs, NULL);
|
|
|
|
// Loop until element is negative i.e. the last
|
|
for (int i = 0; i < count3; ++i)
|
|
{
|
|
// Print contents on a new line
|
|
printf("%d\n", authorIDs[i]);
|
|
}
|
|
|
|
// Free the memory
|
|
free(authorIDs);
|
|
|
|
// ===================================
|
|
*/
|
|
|
|
/*
|
|
// ===================================
|
|
// Example of using 'PullPostDescriptions()'
|
|
// ===================================
|
|
|
|
// Generate array of IDs
|
|
int *IDs;
|
|
int *authors;
|
|
int *dates;
|
|
char *Titles[255];
|
|
int count4 = PullPostDescriptions(&IDs, &Titles, &authors, &dates, NULL);
|
|
|
|
// Loop until element is negative i.e. the last
|
|
for (int i = 0; i < count4; ++i)
|
|
{
|
|
// Print contents on a new line
|
|
printf("%d %s %d %d\n", IDs[i], Titles[i], authors[i], dates[i]);
|
|
}
|
|
|
|
// Free the memory
|
|
free(IDs);
|
|
free(authors);
|
|
free(dates);
|
|
|
|
// ===================================
|
|
*/
|
|
|
|
// Open the connection
|
|
MYSQL *con = conn2maria();
|
|
|
|
// AddPost return the inserted rows, NULL pointer for no tags, 0 for size
|
|
//
|
|
//int tags[] = {1, 2, 3};
|
|
//printf("%d", AddPost("Something", 2, "2020-01-01", "Ssdjshdjahdkjomething", tags, sizeof(tags), con));
|
|
|
|
//AssignTag(6, 42, con);
|
|
//EditPost(69, "HEY", "THERE BUDDY", con);
|
|
//printf("%d\n", AddTag("Test", con));
|
|
//DeleteTag(7, con);
|
|
//RetractTag(1, 1, con);
|
|
//EditTag(5, "Projects", con);
|
|
//AddAuthor("Me", "Meson", "email@me.com", con);
|
|
//ChangeAuthor(93, 1, con);
|
|
//TogglePost(81, con);
|
|
//printf("%s\n", IsEnabled(81, con) ? "True" : "False");
|
|
//AddFilePost(1, "/home/adam/Documents/html/linuxenv.html", con);
|
|
//EditFilePost("/home/adam/Documents/html/linuxenv.html", 1, con);
|
|
//BlogWrite(con);
|
|
//BlogEdit(69, con);
|
|
|
|
// =================================
|
|
|
|
exit(0);
|
|
}
|