Generate Article
Request this page with POST supplying following arguments:
Argument
|
Comment
|
blogpost_id
|
GUID of the blogpost in the database.
|
referer
|
URL, from which this page was requested, after generating
the article, the page will redirect back to the referer.
|
");
include_once($COMMONS."/footer.php");
die();
}
// If request is not GET or POST, throw method not allowed
if(strcmp($_SERVER["REQUEST_METHOD"], "POST") != 0)
{
header($_SERVER["SERVER_PROTOCOL"]." 405 Method Not Allowed", true, 405);
include_once($_SERVER["DOCUMENT_ROOT"]."/errors/405.php");
die();
}
// Check DB connection
if($conn == null){
header($_SERVER["SERVER_PROTOCOL"]." 503 Service Unavailable", true, 503);
include_once($_SERVER["DOCUMENT_ROOT"]."/errors/503.php");
die();
}
// If one of the needed parameters isn't set, show 400
if(!(isset($_POST["blogpost_id"]) && isset($_POST["referer"])))
{
header($_SERVER["SERVER_PROTOCOL"]." 400: Bad Request", true, 400);
include_once($_SERVER["DOCUMENT_ROOT"]."/errors/400.php");
die;
}
// Get the blogpost id and referer
$blogId = sanitize_input($_POST["blogpost_id"]);
$referer = sanitize_input($_POST["referer"]);
// Get the address of the blogpost
$blogAddress = get_blogpost_address($conn, $blogId);
// Try to open the file to which to render the blogpost.
if (!($fp = fopen("article/".$blogAddress.".php", 'w'))) {
header($_SERVER["SERVER_PROTOCOL"]." 500 Could not open file for writing",
true, 505);
include_once($_SERVER["DOCUMENT_ROOT"]."/errors/500.php");
die();
}
// Attempt to generate the blogpost
$blogRendered = generate_article($conn, $fp, $blogId);
// If blogpost could not be loaded, display warning and die.
if(!$blogRendered) {
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Foud", true, 404);
include_once($_SERVER["DOCUMENT_ROOT"]."/errors/404.php");
die();
}
// Redirect back to the referrer.
header("Location: ".$referer);
?>