adsb-receiver/build/portal/html/classes/blog.class.php

223 wiersze
12 KiB
PHP
Czysty Zwykły widok Historia

2016-03-02 23:46:37 +00:00
<?php
/////////////////////////////////////////////////////////////////////////////////////
// ADS-B RECEIVER PORTAL //
// =============================================================================== //
// Copyright and Licensing Information: //
// //
// The MIT License (MIT) //
// //
// Copyright (c) 2015-2016 Joseph A. Prochazka //
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy //
// of this software and associated documentation files (the "Software"), to deal //
// in the Software without restriction, including without limitation the rights //
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell //
// copies of the Software, and to permit persons to whom the Software is //
// furnished to do so, subject to the following conditions: //
// //
// The above copyright notice and this permission notice shall be included in all //
// copies or substantial portions of the Software. //
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE //
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, //
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE //
// SOFTWARE. //
/////////////////////////////////////////////////////////////////////////////////////
class blog {
function getAllPosts($orderBy = "desc") {
require_once($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."classes".DIRECTORY_SEPARATOR."settings.class.php");
$settings = new settings();
if ($settings::db_driver == "xml") {
// XML
$posts = array();
2016-03-17 20:36:54 +00:00
$blogPosts = simplexml_load_file($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."data".DIRECTORY_SEPARATOR."blogPosts.xml");
2016-03-02 23:46:37 +00:00
foreach ($blogPosts as $blogPost) {
$posts[] = array("title"=>$blogPost->title, "date"=>$blogPost->date, "author"=>$blogPost->author, "contents"=>$blogPost->contents);
}
// Sort the results by date either desc or asc.
if(strtolower($orderBy) == "desc") {
usort($posts, function($a, $b) {
return strtotime($b["date"]) - strtotime($a["date"]);
});
} else {
usort($posts, function($a, $b) {
return strtotime($a["date"]) - strtotime($b["date"]);
});
}
} else {
// PDO
require_once($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."classes".DIRECTORY_SEPARATOR."common.class.php");
$common = new common();
$dbh = $common->pdoOpen();
$sql = "SELECT * FROM ".$settings::db_prefix."blogPosts ORDER BY date ".$orderBy;
2016-03-20 07:30:18 +00:00
$sth = $dbh->prepare($sql);
2016-03-02 23:46:37 +00:00
$sth->execute();
2016-10-14 20:31:24 +00:00
$posts = $sth->fetchAll(PDO::FETCH_ASSOC);
2016-03-02 23:46:37 +00:00
$sth = NULL;
$dbh = NULL;
return $posts;
}
return $posts;
}
function getPostByTitle($title) {
require_once($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."classes".DIRECTORY_SEPARATOR."settings.class.php");
$settings = new settings();
if ($settings::db_driver == "xml") {
// XML
2016-03-17 20:36:54 +00:00
$blogPosts = simplexml_load_file($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."data".DIRECTORY_SEPARATOR."blogPosts.xml");
2016-03-02 23:46:37 +00:00
foreach ($blogPosts as $blogPost) {
if (strtolower($blogPost->title) == strtolower($title)) {
2016-03-22 20:42:59 +00:00
return (array)$blogPost;
2016-03-02 23:46:37 +00:00
}
}
} else {
// PDO
require_once($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."classes".DIRECTORY_SEPARATOR."common.class.php");
$common = new common();
$dbh = $common->pdoOpen();
$sql = "SELECT * FROM ".$settings::db_prefix."blogPosts WHERE title = :title";
$sth = $dbh->prepare($sql);
2016-03-03 00:49:01 +00:00
$sth->bindParam(':title', $title, PDO::PARAM_STR, 100);
2016-03-02 23:46:37 +00:00
$sth->execute();
2016-10-14 20:31:24 +00:00
$blogPost = $sth->fetch(PDO::FETCH_ASSOC);
2016-03-02 23:46:37 +00:00
$sth = NULL;
$dbh = NULL;
2016-03-20 07:30:18 +00:00
return $blogPost;
2016-03-02 23:46:37 +00:00
}
}
2016-04-02 13:36:52 +00:00
function titleExists($newTitle) {
require_once($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."classes".DIRECTORY_SEPARATOR."settings.class.php");
$settings = new settings();
if ($settings::db_driver == "xml") {
// XML
2016-10-11 18:47:45 +00:00
$blogPosts = simplexml_load_file($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."data".DIRECTORY_SEPARATOR."blogPosts.xml");
2016-04-02 13:36:52 +00:00
foreach ($blogPosts as $blogPost) {
if ($blogPost->title == $newTitle) {
return TRUE;
}
}
return FALSE;
} else {
// PDO
require_once($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."classes".DIRECTORY_SEPARATOR."common.class.php");
$common = new common();
$dbh = $common->pdoOpen();
2016-04-27 21:01:54 +00:00
$sql = "SELECT COUNT(*) FROM ".$settings::db_prefix."blogPosts WHERE title = :title";
2016-04-02 13:36:52 +00:00
$sth = $dbh->prepare($sql);
$sth->bindParam(':title', $title, PDO::PARAM_STR, 100);
$sth->execute();
$count = $sth->fetchColumn();
$sth = NULL;
$dbh = NULL;
if ($count > 0)
return TRUE;
return FALSE;
}
}
2016-03-22 16:32:57 +00:00
function editContentsByTitle($originalTitle, $contents) {
2016-03-02 23:46:37 +00:00
require_once($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."classes".DIRECTORY_SEPARATOR."settings.class.php");
$settings = new settings();
if ($settings::db_driver == "xml") {
2016-04-02 13:36:52 +00:00
// XML
2016-03-17 20:36:54 +00:00
$blogPosts = simplexml_load_file($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."data".DIRECTORY_SEPARATOR."blogPosts.xml");
2016-03-22 16:32:57 +00:00
foreach ($blogPosts->xpath("blogPost[title='".$originalTitle."']") as $blogPost) {
2016-11-28 17:29:18 +00:00
$blogPost->contents = html_entity_decode($contents, null, "UTF-8");
2016-03-02 23:46:37 +00:00
}
file_put_contents($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."data".DIRECTORY_SEPARATOR."blogPosts.xml", $blogPosts->asXML());
} else {
// PDO
require_once($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."classes".DIRECTORY_SEPARATOR."common.class.php");
$common = new common();
$dbh = $common->pdoOpen();
2016-03-22 16:32:57 +00:00
$sql = "UPDATE ".$settings::db_prefix."blogPosts SET contents = :contents WHERE title = :title";
2016-03-02 23:46:37 +00:00
$sth = $dbh->prepare($sql);
2016-03-22 16:32:57 +00:00
$sth->bindParam(':title', $originalTitle, PDO::PARAM_STR, 100);
$sth->bindParam(':contents', $contents, PDO::PARAM_STR, 20000);
2016-03-02 23:46:37 +00:00
$sth->execute();
$sth = NULL;
$dbh = NULL;
}
}
function deletePostByTitle($title) {
require_once($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."classes".DIRECTORY_SEPARATOR."settings.class.php");
$settings = new settings();
if ($settings::db_driver == "xml") {
2016-11-28 17:29:18 +00:00
// XML
2016-03-17 20:36:54 +00:00
$blogPosts = simplexml_load_file($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."data".DIRECTORY_SEPARATOR."blogPosts.xml");
2016-03-02 23:46:37 +00:00
foreach($blogPosts as $blogPost) {
if($blogPost->title == $title) {
$dom = dom_import_simplexml($blogPost);
$dom->parentNode->removeChild($dom);
}
}
file_put_contents($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."data".DIRECTORY_SEPARATOR."blogPosts.xml", $blogPosts->asXml());
} else {
// PDO
require_once($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."classes".DIRECTORY_SEPARATOR."common.class.php");
$common = new common();
$dbh = $common->pdoOpen();
$sql = "DELETE FROM ".$settings::db_prefix."blogPosts WHERE title = :title";
$sth = $dbh->prepare($sql);
2016-03-03 00:49:01 +00:00
$sth->bindParam(':title', $title, PDO::PARAM_STR, 100);
2016-03-02 23:46:37 +00:00
$sth->execute();
$sth = NULL;
$dbh = NULL;
}
}
function addPost($author, $title, $contents) {
require_once($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."classes".DIRECTORY_SEPARATOR."settings.class.php");
$settings = new settings();
if ($settings::db_driver == "xml") {
2016-11-28 17:29:18 +00:00
// XML
2016-03-17 20:36:54 +00:00
$blogPosts = simplexml_load_file($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."data".DIRECTORY_SEPARATOR."blogPosts.xml");
2016-03-02 23:46:37 +00:00
$blogPost = $blogPosts->addChild('blogPost', '');
$blogPost->addChild('title', $title);
2016-04-27 21:01:54 +00:00
$blogPost->addChild('date', gmdate('Y-m-d H:i:s', time()));
2016-03-02 23:46:37 +00:00
$blogPost->addChild('author', $author);
2016-11-28 17:29:18 +00:00
$blogPost->addChild('contents', html_entity_decode($contents, null, "UTF-8"));
2016-03-02 23:46:37 +00:00
$dom = dom_import_simplexml($blogPosts)->ownerDocument;
$dom->formatOutput = TRUE;
file_put_contents($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."data".DIRECTORY_SEPARATOR."blogPosts.xml", $dom->saveXML());
} else {
// PDO
require_once($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."classes".DIRECTORY_SEPARATOR."common.class.php");
$common = new common();
$dbh = $common->pdoOpen();
2016-03-20 07:30:18 +00:00
$sql = "INSERT INTO ".$settings::db_prefix."blogPosts (title, date, author, contents) VALUES (:title, :date, :author, :contents)";
2016-03-02 23:46:37 +00:00
$sth = $dbh->prepare($sql);
2016-03-03 00:49:01 +00:00
$sth->bindParam(':title', $title, PDO::PARAM_STR, 100);
2016-04-27 21:01:54 +00:00
$sth->bindParam(':date', gmdate('Y-m-d H:i:s', time()), PDO::PARAM_STR, 20);
2016-03-03 00:49:01 +00:00
$sth->bindParam(':author', $author, PDO::PARAM_STR, 100);
2016-03-20 07:30:18 +00:00
$sth->bindParam(':contents', $contents, PDO::PARAM_STR, 20000);
2016-03-02 23:46:37 +00:00
$sth->execute();
$sth = NULL;
$dbh = NULL;
}
}
}
2016-03-17 20:36:54 +00:00
?>