221 lines
		
	
	
		
			7.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			221 lines
		
	
	
		
			7.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
namespace api\blog;
 | 
						|
use api\utils\imgUtils;
 | 
						|
use DOMDocument;
 | 
						|
use PDO;
 | 
						|
use Psr\Http\Message\UploadedFileInterface;
 | 
						|
 | 
						|
require_once __DIR__ . "/../utils/config.php";
 | 
						|
require_once __DIR__ . "/../utils/imgUtils.php";
 | 
						|
 | 
						|
/**
 | 
						|
 * Blog Data Class
 | 
						|
 * Define all functions which either get, update, create or delete posts
 | 
						|
 */
 | 
						|
class blogData
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * Get all blog posts
 | 
						|
     * @return array - Array of all blog posts or error message
 | 
						|
     */
 | 
						|
    public function getBlogPosts(): array
 | 
						|
    {
 | 
						|
        $conn = dbConn();
 | 
						|
        $stmt = $conn->prepare("SELECT ID, title, dateCreated, dateModified, body, categories FROM blog ORDER BY dateCreated DESC;");
 | 
						|
        $stmt->execute();
 | 
						|
 | 
						|
        // set the resulting array to associative
 | 
						|
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
 | 
						|
 | 
						|
        if ($result)
 | 
						|
        {
 | 
						|
            return $result;
 | 
						|
        }
 | 
						|
 | 
						|
        return array("errorMessage" => "Error, blog data not found");
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Get a blog post with the given ID
 | 
						|
     * @param string $ID - ID of the blog post to get
 | 
						|
     * @return array - Array of all blog posts or error message
 | 
						|
     */
 | 
						|
    public function getBlogPost(string $ID): array
 | 
						|
    {
 | 
						|
        $conn = dbConn();
 | 
						|
        $stmt = $conn->prepare("SELECT  ID, title, dateCreated, dateModified, featured, headerImg, body, categories FROM blog WHERE ID = :ID;");
 | 
						|
        $stmt->bindParam(":ID", $ID);
 | 
						|
        $stmt->execute();
 | 
						|
 | 
						|
        // set the resulting array to associative
 | 
						|
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
 | 
						|
 | 
						|
        if ($result)
 | 
						|
        {
 | 
						|
            return $result;
 | 
						|
        }
 | 
						|
 | 
						|
        return array("errorMessage" => "Error, blog post could not found");
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Get the latest blog post
 | 
						|
     * @return array - Array of the latest blog post or error message
 | 
						|
     */
 | 
						|
    public function getLatestBlogPost(): array
 | 
						|
    {
 | 
						|
        $conn = dbConn();
 | 
						|
        $stmt = $conn->prepare("SELECT ID, title, dateCreated, dateModified, featured, headerImg, body, categories FROM blog ORDER BY dateCreated DESC LIMIT 1;");
 | 
						|
        $stmt->execute();
 | 
						|
 | 
						|
        // set the resulting array to associative
 | 
						|
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
 | 
						|
 | 
						|
        if ($result)
 | 
						|
        {
 | 
						|
            return $result;
 | 
						|
        }
 | 
						|
 | 
						|
        return array("errorMessage" => "Error, blog post could not found");
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Get featured blog post
 | 
						|
     * @return array - Array of the featured blog post or error message
 | 
						|
     */
 | 
						|
    public function getFeaturedBlogPost(): array
 | 
						|
    {
 | 
						|
        $conn = dbConn();
 | 
						|
        $stmt = $conn->prepare("SELECT ID, title, dateCreated, dateModified, featured, headerImg, body, categories FROM blog WHERE featured = 1;");
 | 
						|
        $stmt->execute();
 | 
						|
 | 
						|
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
 | 
						|
 | 
						|
        if ($result)
 | 
						|
        {
 | 
						|
            return $result;
 | 
						|
        }
 | 
						|
 | 
						|
        return array("errorMessage" => "Error, blog post could not found");
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Upload the images in the post to temp folder and return image location
 | 
						|
     * @param UploadedFileInterface $img - Image to upload
 | 
						|
     * @return string|array - String with error message or array with the location of the uploaded file
 | 
						|
     */
 | 
						|
    public function uploadPostImage(UploadedFileInterface $img): string|array
 | 
						|
    {
 | 
						|
        $targetDir = "../blog/imgs/tmp/";
 | 
						|
        $imagUtils = new imgUtils();
 | 
						|
        $targetFile = $imagUtils->uploadFile($targetDir, $img);
 | 
						|
 | 
						|
        $file = $targetDir . basename($img->getClientFilename());
 | 
						|
 | 
						|
        if (file_exists($file))
 | 
						|
        {
 | 
						|
            return array("url" => $file);
 | 
						|
        }
 | 
						|
 | 
						|
        if (!is_array($targetFile))
 | 
						|
        {
 | 
						|
            return $targetFile;
 | 
						|
        }
 | 
						|
 | 
						|
        if (file_exists($targetFile["imgLocation"]))
 | 
						|
        {
 | 
						|
            return array("url" => $targetFile["imgLocation"]);
 | 
						|
        }
 | 
						|
 | 
						|
        return "Couldn't upload the image";
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Creates a new post directory, uploads the header image and moves the images from the
 | 
						|
     * temp folder to the new folder, then updates the post html to point to the new images, finally
 | 
						|
     * it creates the post in the database
 | 
						|
     * @param string $title - Title of the blog post
 | 
						|
     * @param string $body - Body of the blog post
 | 
						|
     * @param string $dateCreated - Date the blog post was created
 | 
						|
     * @param string $featured - Whether the blog post is featured or not
 | 
						|
     * @param string $categories - Categories of the blog post
 | 
						|
     * @param UploadedFileInterface $headerImg - Header image of the blog post
 | 
						|
     * @return int|string - ID of the blog post or error message
 | 
						|
     */
 | 
						|
    public function createPost(string $title, string $body, string $dateCreated, string $featured, string $categories, UploadedFileInterface $headerImg): int|string
 | 
						|
    {
 | 
						|
        $conn = dbConn();
 | 
						|
        $targetFile = "";
 | 
						|
        $folderID = uniqid();
 | 
						|
        if ($headerImg !== null)
 | 
						|
        {
 | 
						|
            $targetDir = "../blog/imgs/" . $title . "_" . $folderID . "/";
 | 
						|
            mkdir($targetDir, 0777, true);
 | 
						|
            $imagUtils = new imgUtils();
 | 
						|
            $targetFile = $imagUtils->uploadFile($targetDir, $headerImg);
 | 
						|
        }
 | 
						|
 | 
						|
        $targetFile = array("imgLocation" => ".../blog/imgs/placeholder.png");
 | 
						|
 | 
						|
        if (!is_array($targetFile))
 | 
						|
        {
 | 
						|
            return $targetFile;
 | 
						|
        }
 | 
						|
 | 
						|
        $htmlDoc = new DOMDocument();
 | 
						|
        $htmlDoc->loadHTML($body, LIBXML_NOERROR);
 | 
						|
        $doc = $htmlDoc->getElementsByTagName('body')->item(0);
 | 
						|
        $imgs = $doc->getElementsByTagName('img');
 | 
						|
 | 
						|
        $srcList = array();
 | 
						|
 | 
						|
        foreach ($imgs as $img)
 | 
						|
        {
 | 
						|
            $src = $img->getAttribute("src");
 | 
						|
            $srcList[] = $src;
 | 
						|
            $fileName = basename($src);
 | 
						|
 | 
						|
            $img->setAttribute("src", $targetDir . $fileName);
 | 
						|
        }
 | 
						|
 | 
						|
        $files = scandir("../blog/imgs/tmp/");
 | 
						|
        foreach ($files as $file)
 | 
						|
        {
 | 
						|
            if ($file != "." && $file != "..")
 | 
						|
            {
 | 
						|
                if (!in_array("../blog/imgs/tmp/" . $file, $srcList))
 | 
						|
                {
 | 
						|
                    unlink("../blog/imgs/tmp/" . $file);
 | 
						|
                }
 | 
						|
                else
 | 
						|
                {
 | 
						|
                    rename("../blog/imgs/tmp/" . $file, $targetDir . $file);
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        $newBody = '';
 | 
						|
        foreach ($doc->childNodes as $node)
 | 
						|
        {
 | 
						|
            $newBody .= $htmlDoc->saveHTML($node);
 | 
						|
        }
 | 
						|
 | 
						|
        $stmt = $conn->prepare("INSERT INTO blog (title, dateCreated, dateModified, featured, headerImg, body, categories, folderID) 
 | 
						|
                                       VALUES (:title, :dateCreated, :dateModified, :featured, :headerImg, :body, :categories, :folderID);");
 | 
						|
        $stmt->bindParam(":title", $title);
 | 
						|
        $stmt->bindParam(":dateCreated", $dateCreated);
 | 
						|
        $stmt->bindParam(":dateModified", $dateCreated);
 | 
						|
        $stmt->bindParam(":featured", $featured);
 | 
						|
        $stmt->bindParam(":headerImg", $targetFile["imgLocation"]);
 | 
						|
        $stmt->bindParam(":body", $newBody);
 | 
						|
        $stmt->bindParam(":categories", $categories);
 | 
						|
        $stmt->bindParam(":folderID", $folderID);
 | 
						|
 | 
						|
        if ($stmt->execute())
 | 
						|
        {
 | 
						|
            return intval($conn->lastInsertId());
 | 
						|
        }
 | 
						|
 | 
						|
        return "Error, couldn't create post";
 | 
						|
    }
 | 
						|
} |