diff --git a/brain/api/v1/ImagesAPI.inc.php b/brain/api/v1/ImagesAPI.inc.php new file mode 100644 index 0000000..aed3092 --- /dev/null +++ b/brain/api/v1/ImagesAPI.inc.php @@ -0,0 +1,30 @@ +getUploadedFiles(); + $path = date("Y") . "/" . date("m"); + + $uploadPath = "../public/assets/images/blog/" . $path; + + FileUploader::uploadFile($uploadPath, $image["post_image"]); + + $response = [ + "message" => "Image Added. Very slick", + "type" => "postImageAdded", + "url" => + "/assets/images/blog/" . + $path . + "/" . + $image["post_image"]->getClientFileName(), + ]; + + return $response; + } +} diff --git a/brain/controller/APIControl.inc.php b/brain/controller/APIControl.inc.php index a881b7e..ceae441 100644 --- a/brain/controller/APIControl.inc.php +++ b/brain/controller/APIControl.inc.php @@ -2,7 +2,7 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -//include "brain/data/Auth.inc.php"; +include "../brain/api/v1/ImagesAPI.inc.php"; class APIControl { @@ -18,7 +18,6 @@ class APIControl default: break; } - $response->getBody()->write(json_encode($result)); return $response->withHeader("Content-Type", "application/json"); } @@ -43,6 +42,33 @@ class APIControl break; case "logout": $result = Auth::logout($body); + break; + case "page": + //move methdology to its own API class + $task = $args["fourth"]; + $token = $request->getHeader("fipamo-access-token"); + if (Session::verifyToken($token[0])) { + switch ($task) { + case "delete": + case "add": + case "edit": + $result = (new Book("../content/pages"))->editPage( + $task, + $request + ); + break; + + case "add-entry-image": + $result = ImagesAPI::uploadImage($request); + break; + } + } else { + $result = [ + "message" => "API access denied, homie", + "type" => "API_ERROR", + ]; + } + break; default: $result = [ diff --git a/brain/controller/DashControl.inc.php b/brain/controller/DashControl.inc.php index 04930bf..d21a8b6 100644 --- a/brain/controller/DashControl.inc.php +++ b/brain/controller/DashControl.inc.php @@ -18,7 +18,12 @@ class DashControl switch (isset($args["second"]) ? $args["second"] : "index") { case "pages": $currentPage = isset($args["fourth"]) ? $args["fourth"] : 1; - $data = (new Book("../content/pages"))->getPages($currentPage, 4); + $filter = isset($args["third"]) ? $args["third"] : "all"; + $data = (new Book("../content/pages"))->getPages( + $currentPage, + 4, + $filter + ); $template = "dash/book.twig"; $pageOptions = [ "entryCount" => $data["entryCount"], diff --git a/brain/data/Book.inc.php b/brain/data/Book.inc.php index fbf325a..f9d25aa 100644 --- a/brain/data/Book.inc.php +++ b/brain/data/Book.inc.php @@ -33,23 +33,157 @@ class Book return $page; } + public function editPage($task, $request) + { + $content = $this->getContents(); + if ($task == "delete") { + $parsed = json_decode(file_get_contents("php://input"), true); + $body = find($content, ["uuid" => $parsed["id"]]); + } else { + $body = $request->getParsedBody(); + } + + $page = find($content, ["uuid" => $body["uuid"]]); + $image = $request->getUploadedFiles(); + $member = Session::get("member"); + + if ($task != "add") { + $path = + date("Y", date($page["rawCreated"])) . + "/" . + date("m", date($page["rawCreated"])); + } else { + $path = date("Y", date()) . "/" . date("m"); + } + + if (isset($image["feature_image"])) { + $feature = $image["feature_image"]->getClientFileName(); + FileUploader::uploadFile( + "../public/assets/images/blog/" . $path . "/", + $image["feature_image"] + ); + } else { + if (isset($body["feature_image"])) { + $url = explode("/", $body["feature_image"]); + $feature = + "/" . + $url[3] . + "/" . + $url[4] . + "/" . + $url[5] . + "/" . + $url[6] . + "/" . + $url[7] . + "/" . + $url[8]; + } else { + $feature = $body["feature"]; + } + } + + if ($task == "delete") { + $deleted = "true"; + $body["menu"] ? ($body["menu"] = "true") : ($body["menu"] = "false"); + $body["published"] + ? ($body["published"] = "true") + : ($body["published"] = "false"); + $body["featured"] + ? ($body["featured"] = "true") + : ($body["featured"] = "false"); + } else { + $deleted = !$page["deleted"] ? "false" : $page["deleted"]; + } + + $created = + $task != "add" + ? new \Moment\Moment($page["rawCreated"]) + : new \Moment\Moment(); + $updated = new \Moment\Moment(); + + $write = + "---\n" . + "id: " . + $body["id"] . + "\n" . + "uuid: " . + $body["uuid"] . + "\n" . + "title: " . + $body["title"] . + "\n" . + "feature: " . + $feature . + "\n" . + "path: " . + $path . + "\n" . + "layout: " . + $body["layout"] . + "\n" . + "tags: " . + $body["tags"] . + "\n" . + "author: " . + $member["handle"] . + "\n" . + "created: " . + $created->format("Y-m-d\TH:i:sP") . + "\n" . + "updated: " . + $updated->format("Y-m-d\TH:i:sP") . + "\n" . + "deleted: " . + $deleted . + "\n" . + "slug: " . + $body["slug"] . + "\n" . + "menu: " . + $body["menu"] . + "\n" . + "published: " . + $body["published"] . + "\n" . + "featured: " . + $body["featured"] . + "\n---\n" . + $body["content"]; + + // if layout is index, change path to file + + if ($body["layout"] == "index") { + $writePath = "../content/start/index.md"; + } else { + $writePath = "../content/pages/" . $path . "/" . $body["slug"] . ".md"; + } + + ($new = fopen($writePath, "w")) or die("Unable to open file!"); + fwrite($new, $write); + fclose($new); + + $response = [ + "message" => "File edited. Nice work", + "type" => $task == "write" ? "postUpdated" : "postAdded", + ]; + + return $response; + } + public function getPages(int $page, int $limit, string $sort = null) { $content = $this->getContents(); $published = filter($content, function ($item) { - return $item["published"] == "true"; + return $item["published"] == true && $item["deleted"] == false; }); $deleted = filter($content, function ($item) { - return $item["deleted"]; + return $item["deleted"] == true; }); $all = $content; - $filter = isset($sort) ? $sort : "all"; - - //echo $filter; - $filtered = []; switch ($filter) { case "published": $filtered = $published; @@ -61,9 +195,7 @@ class Book $filtered = $content; break; } - $numOfPages = ceil(count($filtered) / $limit); - $folder = []; if (count($filtered) != 0) { @@ -99,7 +231,7 @@ class Book "numOfPages" => $numOfPages, "entryCount" => count($filtered), "paginate" => [ - "sort" => $filter, + "sort" => $sort, "nextPage" => $next, "prevPage" => $prev, ], @@ -116,7 +248,6 @@ class Book $contents = []; foreach ($this->files as $file) { $doc = $parser->parse(file_get_contents($file), false); - $meta = $doc->getYAML(); $page = [ "id" => $meta["id"], @@ -129,6 +260,8 @@ class Book "author" => $meta["author"], "created" => date("Y M D d", $meta["created"]), "updated" => date("Y M D d", $meta["updated"]), + "rawCreated" => $meta["created"], + "rawUpdated" => $meta["updated"], "deleted" => $meta["deleted"], "menu" => $meta["menu"], "featured" => $meta["featured"], @@ -137,7 +270,6 @@ class Book "filePath" => $file, "content" => $doc->getContent(), ]; - //checks for duplicates $uuid = $meta["uuid"]; $found = current( diff --git a/brain/data/Session.inc.php b/brain/data/Session.inc.php index 12e8596..1d6d330 100644 --- a/brain/data/Session.inc.php +++ b/brain/data/Session.inc.php @@ -38,6 +38,24 @@ class Session } } + public static function verifyToken($token) + { + $data = json_decode(file_get_contents(self::$file), true); + if ($data["member"] != null) { + $secret = (new Settings())->getFolks("secret"); + if ( + Token::validate($token, $secret) && + Token::validateExpiration($token, $secret) + ) { + return true; + } else { + return false; + } + } else { + return false; + } + } + public static function set($key, $value) { $data = json_decode(file_get_contents(self::$file), true); diff --git a/brain/utility/FileUploader.inc.php b/brain/utility/FileUploader.inc.php new file mode 100644 index 0000000..5afbfc2 --- /dev/null +++ b/brain/utility/FileUploader.inc.php @@ -0,0 +1,29 @@ +getClientFileName(), $directory); + //$extension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION); + + // see http://php.net/manual/en/function.random-bytes.php + //$basename = bin2hex(random_bytes(8)); + //$filename = sprintf("%s.%0.8s", $basename, $extension); + + $file->moveTo($directory . "/" . $file->getClientFileName()); + } catch (Error $e) { + echo "failed to upload image: " . $e->getMessage(); + throw new Error("Failed to upload image file"); + } + } +} diff --git a/brain/views/dash/page-edit.twig b/brain/views/dash/page-edit.twig index e299485..5e1278c 100644 --- a/brain/views/dash/page-edit.twig +++ b/brain/views/dash/page-edit.twig @@ -7,6 +7,7 @@ {% set id = page['id'] %} {% set uuid = page['uuid'] %} {% set slug = page['slug'] %} + {% set layout = page['layout'] %} {% set feature = page['feature'] %} {% set _title = page['title'] %} {% set tags = page['tags'] %} @@ -16,6 +17,7 @@ {% set id = '' %} {% set uuid = '' %} {% set slug = '' %} + {% set layout = 'pages' %} {% set feature = '' %} {% set title = '' %} {% set tags = '' %} @@ -32,7 +34,7 @@ {% endblock %} {% block mainContent %} -