mirror of
https://github.com/typemill/typemill.git
synced 2025-08-05 05:37:45 +02:00
Version 1.0.0
This commit is contained in:
23
system/Controllers/Controller.php
Normal file
23
system/Controllers/Controller.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace System\Controllers;
|
||||
|
||||
/* Use the slim-container */
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
protected $c;
|
||||
|
||||
public function __construct(ContainerInterface $c)
|
||||
{
|
||||
$this->c = $c;
|
||||
}
|
||||
|
||||
protected function render404($response, $content = NULL)
|
||||
{
|
||||
return $this->c->view->render($response->withStatus(404), '/404.twig', $content);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
132
system/Controllers/PageController.php
Normal file
132
system/Controllers/PageController.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace System\Controllers;
|
||||
|
||||
use System\Models\Folder;
|
||||
use System\Models\Cache;
|
||||
use System\Models\Helpers;
|
||||
|
||||
class PageController extends Controller
|
||||
{
|
||||
|
||||
public function index($request, $response, $args)
|
||||
{
|
||||
|
||||
/* Initiate Variables */
|
||||
$structure = false;
|
||||
$contentHTML = false;
|
||||
$item = false;
|
||||
$breadcrumb = false;
|
||||
$description = '';
|
||||
$settings = $this->c->get('settings');
|
||||
$pathToContent = $settings['rootPath'] . $settings['contentFolder'];
|
||||
$cache = new Cache();
|
||||
$uri = $request->getUri();
|
||||
$base_url = $uri->getBaseUrl();
|
||||
|
||||
if($cache->validate())
|
||||
{
|
||||
$structure = $this->getCachedStructure($cache);
|
||||
$cached = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
$structure = $this->getFreshStructure($pathToContent, $cache, $uri);
|
||||
$cached = false;
|
||||
|
||||
if(!$structure)
|
||||
{
|
||||
$content = '<h1>No Content</h1><p>Your content folder is empty.</p>';
|
||||
$this->c->view->render($response, '/index.twig', [ 'content' => $content ]);
|
||||
}
|
||||
}
|
||||
|
||||
/* if the user is on startpage */
|
||||
if(empty($args))
|
||||
{
|
||||
/* check, if there is an index-file in the root of the content folder */
|
||||
$contentMD = file_exists($pathToContent . DIRECTORY_SEPARATOR . 'index.md') ? file_get_contents($pathToContent . DIRECTORY_SEPARATOR . 'index.md') : NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* get the request url */
|
||||
$urlRel = $uri->getBasePath() . '/' . $args['params'];
|
||||
|
||||
/* find the url in the content-item-tree and return the item-object for the file */
|
||||
$item = Folder::getItemForUrl($structure, $urlRel);
|
||||
|
||||
if(!$item && $cached)
|
||||
{
|
||||
$structure = $this->getFreshStructure($pathToContent, $cache, $uri);
|
||||
$item = Folder::getItemForUrl($structure, $urlRel);
|
||||
}
|
||||
if(!$item){ return $this->render404($response, array( 'navigation' => $structure, 'settings' => $settings, 'base_url' => $base_url )); }
|
||||
|
||||
/* get breadcrumb for page */
|
||||
$breadcrumb = Folder::getBreadcrumb($structure, $item->keyPathArray);
|
||||
|
||||
/* add the paging to the item */
|
||||
$item = Folder::getPagingForItem($structure, $item);
|
||||
|
||||
/* check if url is a folder. If so, check if there is an index-file for the folder */
|
||||
if($item->elementType == 'folder' && $item->index)
|
||||
{
|
||||
$filePath = $pathToContent . $item->path . DIRECTORY_SEPARATOR . 'index.md';
|
||||
}
|
||||
elseif($item->elementType == 'file')
|
||||
{
|
||||
$filePath = $pathToContent . $item->path;
|
||||
}
|
||||
|
||||
/* read the content of the file */
|
||||
$contentMD = isset($filePath) ? file_get_contents($filePath) : false;
|
||||
}
|
||||
|
||||
/* initialize parsedown */
|
||||
$Parsedown = new \ParsedownExtra();
|
||||
|
||||
/* parse markdown-file to html-string */
|
||||
$contentHTML = $Parsedown->text($contentMD);
|
||||
$description = substr(strip_tags($contentHTML),0,150);
|
||||
$description = trim(preg_replace('/\s+/', ' ', $description));
|
||||
|
||||
/*
|
||||
$timer['topiccontroller']=microtime(true);
|
||||
$timer['end topiccontroller']=microtime(true);
|
||||
Helpers::printTimer($timer);
|
||||
*/
|
||||
|
||||
$route = empty($args) && $settings['startpage'] ? '/cover.twig' : '/index.twig';
|
||||
|
||||
$this->c->view->render($response, $route, array('navigation' => $structure, 'content' => $contentHTML, 'item' => $item, 'breadcrumb' => $breadcrumb, 'settings' => $settings, 'description' => $description, 'base_url' => $base_url ));
|
||||
}
|
||||
|
||||
|
||||
protected function getCachedStructure($cache)
|
||||
{
|
||||
return $cache->getData('structure');
|
||||
}
|
||||
|
||||
|
||||
protected function getFreshStructure($pathToContent, $cache, $uri)
|
||||
{
|
||||
/* scan the content of the folder */
|
||||
$structure = Folder::scanFolder($pathToContent);
|
||||
|
||||
/* if there is no content, render an empty page */
|
||||
if(count($structure) == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/* create an array of object with the whole content of the folder */
|
||||
$structure = Folder::getFolderContentDetails($structure, $uri->getBaseUrl(), $uri->getBasePath());
|
||||
|
||||
/* cache navigation */
|
||||
$cache->refresh($structure, 'structure');
|
||||
|
||||
return $structure;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
138
system/Controllers/SetupController.php
Normal file
138
system/Controllers/SetupController.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace System\Controllers;
|
||||
|
||||
use \Symfony\Component\Yaml\Yaml;
|
||||
|
||||
class SetupController extends Controller
|
||||
{
|
||||
public function setup($request, $response, $args)
|
||||
{
|
||||
$themes = $this->getThemes();
|
||||
$copyright = $this->getCopyright();
|
||||
$uri = $request->getUri();
|
||||
$base_url = $uri->getBaseUrl();
|
||||
$errors = false;
|
||||
|
||||
/* Check, if setting folder is */
|
||||
if(!is_writable($this->c->get('settings')['settingsPath'])){ $errors['folder'] = 'Your settings folder is not writable.'; }
|
||||
|
||||
$data = array(
|
||||
'themes' => $themes,
|
||||
'copyright' => $copyright,
|
||||
'inputs' => false,
|
||||
'errors' => $errors,
|
||||
'base_url' => $base_url
|
||||
);
|
||||
$this->c->view->render($response, '/setup.twig', $data);
|
||||
}
|
||||
|
||||
public function save($request, $response, $args)
|
||||
{
|
||||
if($request->isPost())
|
||||
{
|
||||
$params = $request->getParams();
|
||||
|
||||
$copyright = $this->getCopyright();
|
||||
$themes = $this->getThemes();
|
||||
$errors = array();
|
||||
$uri = $request->getUri();
|
||||
$base_url = $uri->getBaseUrl();
|
||||
|
||||
/* Validate Title */
|
||||
if(!isset($params['title'])){ $errors['title'] = 'Please add a title. '; }
|
||||
if(strlen($params['title']) < 2){ $errors['title'] = 'Title is too short (< 2). '; }
|
||||
if(strlen($params['title']) > 20){ $errors['title'] = 'Title is too long (> 20). '; }
|
||||
|
||||
/* Validate Author */
|
||||
if(isset($params['author']) && !empty($params['author']))
|
||||
{
|
||||
if(strlen($params['author']) < 2){ $errors['author'] = 'Text is too short (< 2). '; }
|
||||
if(strlen($params['author']) > 40){ $errors['author'] .= 'Text is too long (> 40). '; }
|
||||
if(preg_match('/[\(\)\[\]\{\}\?\*\$\"\'\|<>=!;@#%§]/', $params['author'])){ $errors['author'] .= 'Only special chars like a,b a-b a_b a&b are allowed.'; }
|
||||
}
|
||||
|
||||
/* Validate Year */
|
||||
if(!isset($params['year'])){ $errors['year'] = 'Please add a year, e.g. 2017.'; }
|
||||
if(!preg_match('/^(\d{4})$/', $params['year'])){ $errors['year'] = 'Use four digits for the year like 2017.'; }
|
||||
|
||||
/* Validate Copyright */
|
||||
if(isset($params['copyright']) AND !in_array($params['copyright'], $copyright )){ $errors['copyright'] = 'Please select a valid copyright.'; }
|
||||
|
||||
/* Validate Theme */
|
||||
if(!isset($params['theme']) AND !in_array($params['theme'], $themes)){ $errors['theme'] = 'Please select a valid theme.'; }
|
||||
|
||||
/* Validate Startpage */
|
||||
if(isset($params['startpage'])){ $params['startpage'] = true; }else{ $params['startpage'] = false; }
|
||||
|
||||
/* Validate Folder Writable */
|
||||
if(!is_writable($this->c->get('settings')['settingsPath'])){ $errors['folder'] = 'Your settings folder is not writable.'; }
|
||||
|
||||
/* Prevent Title From Hacking */
|
||||
$params['title'] = htmlentities(stripslashes($params['title']));
|
||||
|
||||
if(!empty($errors))
|
||||
{
|
||||
$data = array(
|
||||
'themes' => $themes,
|
||||
'copyright' => $copyright,
|
||||
'errors' => $errors,
|
||||
'inputs' => $params,
|
||||
'base_url' => $base_url
|
||||
|
||||
);
|
||||
$this->c->view->render($response, '/setup.twig', $data);
|
||||
}
|
||||
else
|
||||
{
|
||||
$file = $this->c->get('settings')['settingsPath'] . DIRECTORY_SEPARATOR . 'settings.yaml';
|
||||
$fh = fopen($file, 'w');
|
||||
$yaml = Yaml::dump($params);
|
||||
|
||||
file_put_contents($file, $yaml);
|
||||
|
||||
$data = array(
|
||||
'inputs' => $params,
|
||||
'base_url' => $base_url
|
||||
|
||||
);
|
||||
|
||||
$this->c->view->render($response, '/welcome.twig', $data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getCopyright()
|
||||
{
|
||||
return array(
|
||||
"©",
|
||||
"CC-BY",
|
||||
"CC-BY-NC",
|
||||
"CC-BY-NC-ND",
|
||||
"CC-BY-NC-SA",
|
||||
"CC-BY-ND",
|
||||
"CC-BY-SA",
|
||||
"None"
|
||||
);
|
||||
}
|
||||
|
||||
private function getThemes()
|
||||
{
|
||||
$themeFolder = $this->c->get('settings')['rootPath'] . $this->c->get('settings')['themeFolder'];
|
||||
$themeFolderC = scandir($themeFolder);
|
||||
$themes = array();
|
||||
foreach ($themeFolderC as $key => $theme)
|
||||
{
|
||||
if (!in_array($theme, array(".","..")))
|
||||
{
|
||||
if (is_dir($themeFolder . DIRECTORY_SEPARATOR . $theme))
|
||||
{
|
||||
$themes[] = $theme;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $themes;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user