This repository has been archived on 2022-02-10. You can view files and clone it, but cannot push or open issues or pull requests.
rangitaki/res/php/BlogListGenerator.php

138 lines
4 KiB
PHP
Raw Permalink Normal View History

2015-06-11 17:32:58 +02:00
<?php
/**
2016-02-16 14:15:21 +01:00
* PHP Version 7
2015-06-11 17:32:58 +02:00
*
* Rangitaki Project
2015-06-11 17:32:58 +02:00
*
* @category Blogs
* @package RangitakiPHP
* @author mmk2410 <marcelmichaelkapfer@yahoo.co.nz>
* @license MIT License
* @link http://marcel-kapfer.de/rangitaki
*/
2016-12-29 21:54:37 +01:00
namespace mmk2410\rbe\BlogListGenerator;
/**
* The blog list generator class is a collection of functions for generating
* blog list
* or getting informations about them
2015-06-11 17:32:58 +02:00
*
* Since there is no initialize function, I recommend to use the short
* access syntay
2015-06-11 17:32:58 +02:00
*
* @category Generator
* @package Rbe
* @author Marcel Kapfer (mmk2410) <marcelmichaelkapfer@yahoo>
* @license MIT License
* @link http://marcel-kapfer.de/rangitaki
2015-06-11 17:32:58 +02:00
*/
class BlogListGenerator
{
/**
* A function to generate a blog nav item
*
* @param string $directory The directory of the blog file
* @param string $blogname The name of the blog file
* @param string $blogmaintitle The name of the main blog
*
* @return None
*/
2016-12-29 21:54:37 +01:00
public function listBlog($directory, $blogname, $blogmaintitle)
{
// get content of the blog file;
$blog = file_get_contents($directory . $blogname);
// add a line break as a security measurement
$blog = $blog . "\n";
// check if the first line includes a title
if (substr($blog, 0, 6) == "%TITLE") {
// grab the title
2016-12-29 09:40:35 +01:00
$itemname = substr($blog, 8, strpos($blog, "\n") - 8);
// if on main blog
2016-12-29 09:40:35 +01:00
if ($itemname == "main") {
// create a nav item to the main blog
2016-12-29 09:40:35 +01:00
$atag = "<a class='nav-item' href='./'>$blogmaintitle</a>";
2015-06-11 17:32:58 +02:00
} else {
// create a link to the blog
$link = "./?blog=" . substr($blogname, 0, -3);
// create a nav item to the blog
2016-12-29 09:40:35 +01:00
$atag = "<a class='nav-item' href='$link'>$itemname</a>";
2015-06-11 17:32:58 +02:00
}
2016-12-29 09:40:35 +01:00
$blog = substr($blog, strpos($blog, "\n") + 1);
2015-06-11 17:32:58 +02:00
}
2016-12-29 09:40:35 +01:00
// nav item as link to external page
if (substr($blog, 0, 4) == "%URL") {
$itemurl = substr($blog, 6, strpos($blog, "\n") - 6);
$atag = "<a class='nav-item' href='$itemurl'>$itemname</a>";
}
return $atag;
2015-06-11 17:32:58 +02:00
}
/**
* A function to get the name of a blog
*
* @param string $file The path of the blog file
*
* @return string
*/
2016-12-29 21:54:37 +01:00
public function getName($file)
{
// get the content of the blog file
$blog = file_get_contents($file);
// add a line break as a securit measure
$blog = $blog . "\n";
// check if first line includes a title
if (substr($blog, 0, 6) == "%TITLE") {
// grab the title
$blog = substr($blog, 8, strpos($blog, "\n") - 8);
return $blog;
}
}
/**
* A function to recieve the amount of articles
* of a blog
*
* @param string $blog the blog name
*
* @return int Amount of files
*/
2016-12-29 21:54:37 +01:00
public static function getArticleAmount($blog)
{
$directory = "./articles/" . $blog . "/";
if (!file_exists($directory)) {
return 0;
} else {
$i = 0;
$handle = opendir($directory);
2016-12-29 21:54:37 +01:00
while (($file = readdir($handle)) !== false) {
if (!in_array($file, array('.','..'))) {
$i++;
}
}
return $i;
2015-07-03 10:59:54 +02:00
}
}
/**
* A function returning the external linkn of
* a blog.
*
* @param string $blog the blog name
* @param string $dir root directory of installation
*
* @return string link to external page else null
*/
public function getExternalLink($blog, $dir)
{
$path = $dir . "/blogs/" . $blog;
$blog = file_get_contents($path) . "\n";
if (substr($blog, 0, 6) == "%TITLE") {
$blog = substr($blog, strpos($blog, "\n") + 1);
}
if (substr($blog, 0, 4) == "%URL") {
return substr($blog, 6, strpos($blog, "\n") - 6);
}
return null;
}
2015-06-11 17:32:58 +02:00
}