-
Notifications
You must be signed in to change notification settings - Fork 1
Class: Chapters
// Columns
public $UID;
public $Series;
public $Title;
public $ChNum;
public $Rar;
public $Pages;
public $Folder;
public $ExistingFolder;
Pretty simple start off, same as every class. There is a variable for every single column in the database. $UID is the individual chapters unique identifier. $Series is the series UID that specific chapter is linked to. $Title is the name of the chapter. ** $ChNum** is the chapter number, Chapter 1, Chapter 1.1 for instance. $Rar is the variable used for handling the ZIP/7z/Rar Files. It is not actually inserted into the database. $Pages is the number of pages extracted from the $Rar file. $Folder is the newly created folder that stores that specific chapters images. $ExistingFolder is called seriesFolder in the database, this is used for a few different things, but usually to store locations for path creating on insert and delete. Often has the series folder for chapters in it.
public function __construct($db)
{
$this->conn = $db;
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
Similar to the series class, only difference is it does not run any checks to see if there is an existing folder. It assigns the connection to the database from the variable passed to the class in initialization. setAttribute Is set so that the errors from PDO are more detailed and not just Hey something happened good luck.
public function display()
{
$query = "SELECT * FROM " . $this->table . "";
$stmt = $this->conn->prepare($query);
$stmt->execute();
return $stmt;
}
Simple function here, it grabs every row from the database of choice, in this case chapters, and sends the array back to the endpoint that called it. The endpoint handles this array.
Beware, nested if statements
public function insert()
{
$this->Folder = uniqid('chapter_'); // Assigns the folder variable a random id with a prefix of 'chapter_'
$query = "INSERT INTO " . $this->table . " SET series = :series, Title = :title, ChNum = :chnum, Pages = :pages, Folder = :folder, seriesFolder = :sfolder";
$stmt = $this->conn->prepare($query);
// Sanitize's the variables recieved.
$this->Title = htmlspecialchars(strip_tags($this->Title));
$this->Series = htmlspecialchars(strip_tags($this->Series));
$this->ChNum = htmlspecialchars(strip_tags($this->ChNum));
$this->Pages = htmlspecialchars(strip_tags($this->Pages));
$this->Folder = htmlspecialchars(strip_tags($this->Folder));
$this->ExistingFolder = htmlspecialchars(strip_tags($this->ExistingFolder));
// Bind's the variables to their spots on the SQL, could probably be done in one line. Not sure.
$stmt->bindParam(":series", $this->Series);
$stmt->bindParam(":title", $this->Title);
$stmt->bindParam(":chnum", $this->ChNum);
$stmt->bindParam(":pages", $this->Pages);
$stmt->bindParam(":folder", $this->Folder);
$stmt->bindParam(":sfolder", $this->ExistingFolder);
if (mkdir(__DIR__ . "/../series/" . $this->ExistingFolder . "/" . $this->Folder)) { // Creates the new chapters folder inside of the given folder of the series.
if (move_uploaded_file($this->Rar['tmp_name'][0], __DIR__ . "/../series/" . $this->ExistingFolder . "/" . $this->Folder . "/" . $this->Rar['name'][0])) { // Moves the zip/7z/rar file into the newly created folder to store the chapter.
$zip = new ZipArchive;
$res = $zip->open(__DIR__ . "/../series/" . $this->ExistingFolder . "/" . $this->Folder . "/" . $this->Rar['name'][0]); // Opens the zip for extraction.
if ($res === true) { // Checks if the zip could be opened.
if ($zip->extractTo(__DIR__ . "/../series/" . $this->ExistingFolder . "/" . $this->Folder . "/")) { // Extracts the zip.
$zip->close(); // Closes the connection to the zip so it can be handled.
unlink(__DIR__ . "/../series/" . $this->ExistingFolder . "/" . $this->Folder . "/" . $this->Rar['name'][0]); // Deletes said zip.
$this->Pages = count(scandir(__DIR__ . "/../series/" . $this->ExistingFolder . "/" . $this->Folder . "/")) - 2 ?? 0; // Counts the number of images extracted and assigns the number to the pages variable.
if ($stmt->execute()) { // Attempts to insert the information assigned to the SQL statement.
return true;
} else {
print_r("Execute Failed."); // Error message for if the SQL Insertion failed.
}
} else {
print_r("Extract To Failed."); // Error message for if the zip file couldnt be extracted.
}
} else {
print_r("Zip Open Failed."); // Error message for if the zip couldnt be opened.
}
} else {
rmdir(__DIR__ . "/../series/" . $this->Folder); // Directory deletion for if the zip cant be moved to the newly created chapter folder. Currently this line is an error, it should be 'rmdir(__DIR__ . "/../series/" . $this->ExistingFolder . "/" . $this->Folder);' Will fix next commit.
}
}
return false;
}
For this function, there is notes in the code itself for what does what.
public function update()
{
$query = "UPDATE " . $this->table . " SET Title = :title, ChNum = :chnum WHERE UID = :uid";
$stmt = $this->conn->prepare($query);
// Sanitization
$this->UID = htmlspecialchars(strip_tags($this->UID));
$this->ChNum = htmlspecialchars(strip_tags($this->ChNum));
$this->Title = htmlspecialchars(strip_tags($this->Title));
// Bind
$stmt->bindParam(":uid", $this->UID);
$stmt->bindParam(":chnum", $this->ChNum);
$stmt->bindParam(":title", $this->Title);
if ($stmt->execute()) {
return true;
}
return false;
}
Simple update function, Sanitizes the received data and updates the chapters number and title. Should it be unable to update these for any reason it will return false, the endpoint handles everything else.
public function delete()
{
$query = "DELETE FROM " . $this->table . " WHERE UID = ?"; // The actually delete query
$qFolder = "SELECT * FROM series WHERE UID = ?"; // The query to get the folder of the chapter being deleted
$stmt = $this->conn->prepare($query);
$FolderStmt = $this->conn->prepare($qFolder);
// Sanitization of the variables, although dont really need to sanitize the folder variable as its being assigned from the database.
$this->UID = htmlspecialchars(strip_tags($this->UID));
$this->Folder = htmlspecialchars(strip_tags($this->Folder));
// Bind's the parameters according to the SQL
$stmt->bindParam(1, $this->UID);
$FolderStmt->bindParam(1, $this->Series);
if ($FolderStmt->execute()) { // Runs the SQL to get the folder of the chapter being deleted
if ($rows = $FolderStmt->fetch(PDO::FETCH_ASSOC)) { // Turns the results of the previous if stmt into an associative array.
$files = glob(__DIR__ . "/../series/" . $rows['Folder'] . "/" . $this->Folder . "/*.*"); // Grabs everything from the directory pulled from the database.
// Loops through every file/image in the folder and deletes it
foreach ($files as $file) {
if (is_file($file))
{
unlink($file);
}
}
if (file_exists(__DIR__ . "/../series/" . $rows['Folder'] . "/" . $this->Folder)) { // Checks to see if the folder for the chapter actually exists
if (rmdir(__DIR__ . "/../series/" . $rows['Folder'] . "/" . $this->Folder)) { // Removes the folder for the chapter
if ($stmt->execute()) { // Runs the DELETE SQL query
return true;
}
}
} else {
if ($stmt->execute()) { // Runs the DELETE SQL query if the folder doesnt exist.
return true;
}
}
} else {
print_r("rows found nothing"); // Error Message should the SQL come back false, the UID matched no chapters.
}
}
return false;
}
Again, notes inside the function explain everything.
public function searchList()
{
$query = "SELECT * FROM " . $this->table . " WHERE series = ?";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(1, $this->UID);
$stmt->execute();
return $stmt;
}
Grabs all chapters according the the series UID provided. Returns the array for the endpoint to handle.
public function searchSingle()
{
$query = "SELECT * FROM " . $this->table . " WHERE ChNum = :chnum AND series = :series";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(":chnum", $this->ChNum);
$stmt->bindParam(":series", $this->Series);
$stmt->execute();
//return $stmt;
if ($rows = $stmt->fetch(PDO::FETCH_ASSOC)) {
$this->UID = $rows['UID'];
$this->ChNum = $rows['ChNum'];
$this->Series = $rows['series'];
$this->Title = $rows['Title'];
$this->Pages = $rows['Pages'];
$this->Folder = $rows['Folder'];
$this->ExistingFolder = $rows['seriesFolder'];
}
}
Searches the database according to the chapter number, and series UID provided, assigns the returned data to variables and allows for use of those variables in the endpoint.
public function pageArr()
{
// Sanitization
$this->Folder = htmlspecialchars(strip_tags($this->Folder));
$this->ExistingFolder = htmlspecialchars(strip_tags($this->ExistingFolder));
$arr = scandir(__DIR__ . "/../series/" . $this->ExistingFolder . "/" . $this->Folder . "/");
array_push($arr, $this->Folder, $this->ExistingFolder);
return $arr;
}
This function scandirs a directory using a path created with the provided data. This data is then pushed into another array along with the chapter and series folders for use in the endpoint and client side.