YAML Config Implement and post api

This commit is contained in:
Marcel Kapfer (mmk2410) 2016-04-17 17:00:18 +02:00
parent e9c09311fe
commit 6d1928da70
7 changed files with 529 additions and 4 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ nbproject/
.idea/
completer.hist
feed/
vendor/

View File

@ -4,12 +4,14 @@
"type": "project",
"require": {
"erusev/parsedown": "^1.6",
"fguillot/picofeed": "^0.1.18"
"fguillot/picofeed": "^0.1.18",
"slim/slim": "^3.0",
"symfony/yaml": "^3.0"
},
"license": "MIT License",
"authors": [
{
"name": "mmk2410",
"name": "Marcel Kapfer (mmk2410)",
"email": "marcelmichaelkapfer@yahoo.co.nz"
}
],

285
composer.lock generated
View File

@ -4,9 +4,36 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "4f3494edee00a6dd92be308e4f860622",
"content-hash": "d93c93221ea797a445a0313f18feccda",
"hash": "9dd7a02ced81071c430f6ac395855013",
"content-hash": "805975f3cf5b0b95d0be593e4599d471",
"packages": [
{
"name": "container-interop/container-interop",
"version": "1.1.0",
"source": {
"type": "git",
"url": "https://github.com/container-interop/container-interop.git",
"reference": "fc08354828f8fd3245f77a66b9e23a6bca48297e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/container-interop/container-interop/zipball/fc08354828f8fd3245f77a66b9e23a6bca48297e",
"reference": "fc08354828f8fd3245f77a66b9e23a6bca48297e",
"shasum": ""
},
"type": "library",
"autoload": {
"psr-4": {
"Interop\\Container\\": "src/Interop/Container/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Promoting the interoperability of container objects (DIC, SL, etc.)",
"time": "2014-12-30 15:22:37"
},
{
"name": "erusev/parsedown",
"version": "1.6.0",
@ -94,6 +121,260 @@
"homepage": "https://github.com/fguillot/picoFeed",
"time": "2016-02-09 02:49:54"
},
{
"name": "nikic/fast-route",
"version": "v0.6.0",
"source": {
"type": "git",
"url": "https://github.com/nikic/FastRoute.git",
"reference": "31fa86924556b80735f98b294a7ffdfb26789f22"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nikic/FastRoute/zipball/31fa86924556b80735f98b294a7ffdfb26789f22",
"reference": "31fa86924556b80735f98b294a7ffdfb26789f22",
"shasum": ""
},
"require": {
"php": ">=5.4.0"
},
"type": "library",
"autoload": {
"psr-4": {
"FastRoute\\": "src/"
},
"files": [
"src/functions.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Nikita Popov",
"email": "nikic@php.net"
}
],
"description": "Fast request router for PHP",
"keywords": [
"router",
"routing"
],
"time": "2015-06-18 19:15:47"
},
{
"name": "pimple/pimple",
"version": "v3.0.2",
"source": {
"type": "git",
"url": "https://github.com/silexphp/Pimple.git",
"reference": "a30f7d6e57565a2e1a316e1baf2a483f788b258a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/silexphp/Pimple/zipball/a30f7d6e57565a2e1a316e1baf2a483f788b258a",
"reference": "a30f7d6e57565a2e1a316e1baf2a483f788b258a",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0.x-dev"
}
},
"autoload": {
"psr-0": {
"Pimple": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
}
],
"description": "Pimple, a simple Dependency Injection Container",
"homepage": "http://pimple.sensiolabs.org",
"keywords": [
"container",
"dependency injection"
],
"time": "2015-09-11 15:10:35"
},
{
"name": "psr/http-message",
"version": "1.0",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-message.git",
"reference": "85d63699f0dbedb190bbd4b0d2b9dc707ea4c298"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-message/zipball/85d63699f0dbedb190bbd4b0d2b9dc707ea4c298",
"reference": "85d63699f0dbedb190bbd4b0d2b9dc707ea4c298",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for HTTP messages",
"keywords": [
"http",
"http-message",
"psr",
"psr-7",
"request",
"response"
],
"time": "2015-05-04 20:22:00"
},
{
"name": "slim/slim",
"version": "3.3.0",
"source": {
"type": "git",
"url": "https://github.com/slimphp/Slim.git",
"reference": "939f2e85d57508de9cff241d10091cd972f221c3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/slimphp/Slim/zipball/939f2e85d57508de9cff241d10091cd972f221c3",
"reference": "939f2e85d57508de9cff241d10091cd972f221c3",
"shasum": ""
},
"require": {
"container-interop/container-interop": "^1.1",
"nikic/fast-route": "^0.6",
"php": ">=5.5.0",
"pimple/pimple": "^3.0",
"psr/http-message": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0",
"squizlabs/php_codesniffer": "^2.5"
},
"type": "library",
"autoload": {
"psr-4": {
"Slim\\": "Slim"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Rob Allen",
"email": "rob@akrabat.com",
"homepage": "http://akrabat.com"
},
{
"name": "Josh Lockhart",
"email": "hello@joshlockhart.com",
"homepage": "https://joshlockhart.com"
},
{
"name": "Gabriel Manricks",
"email": "gmanricks@me.com",
"homepage": "http://gabrielmanricks.com"
},
{
"name": "Andrew Smith",
"email": "a.smith@silentworks.co.uk",
"homepage": "http://silentworks.co.uk"
}
],
"description": "Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs",
"homepage": "http://slimframework.com",
"keywords": [
"api",
"framework",
"micro",
"router"
],
"time": "2016-03-10 21:37:40"
},
{
"name": "symfony/yaml",
"version": "v3.0.4",
"source": {
"type": "git",
"url": "https://github.com/symfony/yaml.git",
"reference": "0047c8366744a16de7516622c5b7355336afae96"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/yaml/zipball/0047c8366744a16de7516622c5b7355336afae96",
"reference": "0047c8366744a16de7516622c5b7355336afae96",
"shasum": ""
},
"require": {
"php": ">=5.5.9"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Yaml\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Yaml Component",
"homepage": "https://symfony.com",
"time": "2016-03-04 07:55:57"
},
{
"name": "zendframework/zendxml",
"version": "1.0.2",

3
config.yaml Normal file
View File

@ -0,0 +1,3 @@
rcc: # settings concerning the rangitaki control center
rcc: on # enable rangitaki control center (enable: 'on')
api: on # enable rangitaki api (enable: 'on')

139
rcc/api/post/index.php Normal file
View File

@ -0,0 +1,139 @@
<?php
// Marcel Kapfer (mmk2410)
// License: MIT License
// api for accessing blog posts
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../../../vendor/autoload.php';
require '../../../res/php/Config.php';
require '../../../res/php/ArticleGenerator.php';
use \mmk2410\rbe\config\Config as Config;
$config = new Config("../../../config.yaml", '../../../vendor/autoload.php');
$settings = $config->getConfig();
if ($settings["rcc"]["api"] == "on" && $settings["rcc"]["rcc"] == "on") {
$app = new \Slim\App();
/**
* api for fetching a blog post
*
* @param string $_GET["blog"] name of the blog
* @param string $_GET["post"] filename of the blog post
*
* @return JSON json string containing the blog post
*/
$app->get('/', function (Request $request, Response $response) {
$blog = $_GET["blog"];
$post = $_GET["post"];
if (!$blog || !$post) {
$data = array('code' => 400, 'status' => 'Bad Request', 'error' => 'Not enough arguments');
$response = $response->withHeader('Content-type', 'application/json');
$response = $response->withJson($data, 400);
return $response;
}
$path = "../../../articles/" . $blog . "/";
$data =
ArticleGenerator::getArray($path, $post);
$response = $response->withHeader('Content-type', 'application/json');
$response = $response->withJson($data, 201);
return $response;
});
/**
* api for changing/creating a blog post
*
* @param string $_POST["data"] all data
*/
$app->post('/', function (Request $request, Response $response) {
$blog = $_POST["blog"];
$post = $_POST["post"];
$title = $_POST["title"];
$author = $_POST["author"];
$date = $_POST["date"];
$tags = $_POST["tags"];
$text = $_POST["text"];
if (!$blog || !$post || (!$title && !$text)) {
$data = array('code' => 400, 'status' => 'Bad Request', 'error' => 'Not enough arguments');
$response = $response->withHeader('Content-type', 'application/json');
$response = $response->withJson($data, 400);
return $response;
}
$text = str_replace('\n', '<br>', $text);
$md = <<<EOD
%TITLE: $title
%DATE: $date
%AUTHOR: $author
%TAGS: $tags
$text
EOD;
$path = "../../../articles/$blog/$post";
if (file_put_contents($path, $md)) {
$data = array('code' => 201, 'status' => 'Post created successfully');
$response = $response->withHeader('Content-type', 'application/json');
$response = $response->withJson($data, 201);
} else {
$data = array('code' => 500, 'status' => 'Internal server error');
$response = $response->withHeader('Content-type', 'application/json');
$response = $response->withJson($data, 500);
}
return $response;
});
/**
* api for deleting a blog post
*
* @param string $_GET["blog"] name of the blog
* @param string $_GET["post"] filename of the blog post
*
* @return JSON json string containing the blog post
*/
$app->delete('/', function (Request $request, Response $response) {
$blog = $_GET["blog"];
$post = $_GET["post"];
if (!$blog || !$post) {
$data = array('code' => 400, 'status' => 'Bad Request', 'error' => 'Not enough arguments');
$response = $response->withHeader('Content-type', 'application/json');
$response = $response->withJson($data, 400);
return $response;
}
$path = "../../../articles/$blog/$post";
if (!file_exists($path)) {
$data = array('code' => 400, 'status' => 'Bad Request', 'error' => 'No such file');
$response = $response->withHeader('Content-type', 'application/json');
$response = $response->withJson($data, 400);
return $response;
}
if (!unlink($path)) {
$data = array('code' => 500, 'status' => 'Bad Request', 'error' => 'Internal server error');
$response = $response->withHeader('Content-type', 'application/json');
$response = $response->withJson($data, 500);
return $response;
}
$data = array('code' => 201, 'status' => 'File successfully deleted');
$response = $response->withHeader('Content-type', 'application/json');
$response = $response->withJson($data, 201);
return $response;
});
$app->run();
}

View File

@ -265,4 +265,46 @@ class ArticleGenerator
return $article;
}
/**
* A function to get a array of the article
*
* @param $directory The directory where the article is stored
* @param $articlefile The name of the article file
* @return string
*/
public function getArray($directory, $articlefile)
{
$article = file_get_contents($directory . $articlefile);
if (substr($article, 0, 6) == "%TITLE") { // get and remove the title
$title = substr($article, 8, strpos($article, "\n") - 8);
$article = substr($article, strpos($article, "\n") + 1);
}
if (substr($article, 0, 5) == "%DATE") { // get and remove the title
$date = substr($article, 7, strpos($article, "\n") - 7);
$article = substr($article, strpos($article, "\n") + 1);
}
if (substr($article, 0, 7) == "%AUTHOR") { // get and remove the title
$author = substr($article, 9, strpos($article, "\n") - 9);
$article = substr($article, strpos($article, "\n") + 1);
}
if (substr($article, 0, 5) == "%TAGS") { // get and remove the tags
$tags = substr($article, 7, strpos($article, "\n") - 7); // get the tags
$tags = explode(", ", $tags); // split them into an array
$article = substr($article, strpos($article, "\n") + 1);
}
$data = array(
"title" => $title,
"date" => $date,
"author" => $author,
"tags" => $tags,
"text" => $article
);
return $data;
}
}

57
res/php/Config.php Normal file
View File

@ -0,0 +1,57 @@
<?php
/**
* PHP Version 7
*
* Configuration parser for yaml configuration files
*
* @category Configuration
* @package Rbe
* @author Marcel Kapfer (mmk2410) <marcelmichaelkapfer@yahoo.co.nz>
* @license MIT License
* @link http://marcel-kapfer.de/rangitaki
*/
namespace mmk2410\rbe\config;
/**
* PHP Version 7
*
* Configuration parser for yaml configuration files
*
* @category Configuration
* @package Rbe
* @author Marcel Kapfer (mmk2410) <marcelmichaelkapfer@yahoo.co.nz>
* @license MIT License
* @link http://marcel-kapfer.de/rangitaki
*/
class Config
{
/**
* Path to yaml file
* @var string
*/
private $file;
/**
* Constructor for the Config class
*
* @param $config path to the yaml file
* @param $composer path to the composer autoload
*/
public function __construct($config, $composer)
{
$this->file = $config;
require $composer;
}
/**
* Return yaml config as PHP array
*
* @return config array
*/
public function getConfig()
{
$yaml = new \Symfony\Component\Yaml\Parser();
return $yaml->parse(file_get_contents($this->file));
}
}