mirror of
https://github.com/typemill/typemill.git
synced 2025-07-31 03:10:19 +02:00
Version 1.0.1
This commit is contained in:
@@ -1,81 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace System\Models;
|
||||
|
||||
class Cache
|
||||
{
|
||||
private $cachePath;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$cachePath = getcwd() . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
|
||||
if(!is_dir($cachePath)){
|
||||
mkdir($cachePath, 0774, true) or die('Please create a cache folder in your root and make it writable.');
|
||||
}
|
||||
is_writable($cachePath) or die('Your cache folder is not writable.');
|
||||
$this->cachePath = $cachePath;
|
||||
}
|
||||
|
||||
public function validate()
|
||||
{
|
||||
if(isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$requestFile = $this->cachePath.'request.txt';
|
||||
if(!file_exists($requestFile))
|
||||
{
|
||||
$this->writeFile($requestFile, time());
|
||||
return false;
|
||||
}
|
||||
|
||||
$lastRequest = file_get_contents($requestFile);
|
||||
if(time() - $lastRequest > 600)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function refresh($data, $name)
|
||||
{
|
||||
$sData = serialize($data);
|
||||
$dataFile = $this->cachePath.$name.'.txt';
|
||||
$requestFile = $this->cachePath.'request.txt';
|
||||
|
||||
$this->writeFile($dataFile, $sData);
|
||||
$this->writeFile($requestFile, time());
|
||||
}
|
||||
|
||||
public function getData($name)
|
||||
{
|
||||
if (file_exists($this->cachePath.$name.'.txt'))
|
||||
{
|
||||
$data = file_get_contents($this->cachePath.$name.'.txt');
|
||||
$data = unserialize($data);
|
||||
return $data;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function clearData($name)
|
||||
{
|
||||
/* todo */
|
||||
}
|
||||
|
||||
public function clearAll()
|
||||
{
|
||||
/* todo */
|
||||
}
|
||||
|
||||
public function writeFile($file, $data)
|
||||
{
|
||||
$fp = fopen($file, "w");
|
||||
fwrite($fp, $data);
|
||||
fclose($fp);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@@ -40,16 +40,16 @@ class Folder
|
||||
* vars: multidimensional array with folder- and file-names
|
||||
* returns: array of objects. Each object contains information about an item (file or folder).
|
||||
*/
|
||||
public static function getFolderContentDetails(array $folderContent, $baseUrl, $fullSlug = NULL, $fullPath = NULL, $keyPath = NULL, $chapter = NULL)
|
||||
{
|
||||
public static function getFolderContentDetails(array $folderContent, $baseUrl, $fullSlugWithFolder = NULL, $fullSlugWithoutFolder = NULL, $fullPath = NULL, $keyPath = NULL, $chapter = NULL)
|
||||
{
|
||||
$contentDetails = [];
|
||||
$iteration = 0;
|
||||
$chapternr = 1;
|
||||
|
||||
|
||||
foreach($folderContent as $key => $name)
|
||||
{
|
||||
$item = new \stdClass();
|
||||
|
||||
|
||||
if(is_array($name))
|
||||
{
|
||||
$nameParts = self::getStringParts($key);
|
||||
@@ -63,21 +63,22 @@ class Folder
|
||||
$item->slug = implode("-",$nameParts);
|
||||
$item->slug = URLify::filter(iconv('ISO-8859-15', 'UTF-8', $item->slug));
|
||||
$item->path = $fullPath . DIRECTORY_SEPARATOR . $key;
|
||||
$item->urlRel = $fullSlug . '/' . $item->slug;
|
||||
$item->urlAbs = $baseUrl . $fullSlug . '/' . $item->slug;
|
||||
$item->urlRelWoF = $fullSlugWithoutFolder . '/' . $item->slug;
|
||||
$item->urlRel = $fullSlugWithFolder . '/' . $item->slug;
|
||||
$item->urlAbs = $baseUrl . $fullSlugWithoutFolder . '/' . $item->slug;
|
||||
$item->key = $iteration;
|
||||
$item->keyPath = $keyPath ? $keyPath . '.' . $iteration : $iteration;
|
||||
$item->keyPathArray = explode('.', $item->keyPath);
|
||||
$item->chapter = $chapter ? $chapter . '.' . $chapternr : $chapternr;
|
||||
|
||||
$item->folderContent = self::getFolderContentDetails($name, $baseUrl, $item->urlRel, $item->path, $item->keyPath, $item->chapter);
|
||||
$item->folderContent = self::getFolderContentDetails($name, $baseUrl, $item->urlRel, $item->urlRelWoF, $item->path, $item->keyPath, $item->chapter);
|
||||
}
|
||||
else
|
||||
{
|
||||
$nameParts = self::getStringParts($name);
|
||||
$fileType = array_pop($nameParts);
|
||||
|
||||
if($name == 'index.md' || $fileType !== 'md' ) break;
|
||||
if($name == 'index.md' || $fileType !== 'md' ) continue;
|
||||
|
||||
$item->originalName = $name;
|
||||
$item->elementType = 'file';
|
||||
@@ -92,8 +93,9 @@ class Folder
|
||||
$item->keyPath = $keyPath . '.' . $iteration;
|
||||
$item->keyPathArray = explode('.',$item->keyPath);
|
||||
$item->chapter = $chapter . '.' . $chapternr;
|
||||
$item->urlRel = $fullSlug . '/' . $item->slug;
|
||||
$item->urlAbs = $baseUrl . $fullSlug . '/' . $item->slug;
|
||||
$item->urlRelWoF = $fullSlugWithoutFolder . '/' . $item->slug;
|
||||
$item->urlRel = $fullSlugWithFolder . '/' . $item->slug;
|
||||
$item->urlAbs = $baseUrl . $fullSlugWithoutFolder . '/' . $item->slug;
|
||||
}
|
||||
$iteration++;
|
||||
$chapternr++;
|
||||
|
33
system/Models/VersionCheck.php
Normal file
33
system/Models/VersionCheck.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace System\Models;
|
||||
|
||||
class VersionCheck
|
||||
{
|
||||
function checkVersion($url)
|
||||
{
|
||||
$opts = array(
|
||||
'http'=>array(
|
||||
'method' => "GET",
|
||||
'header' => "Referer: $url\r\n"
|
||||
)
|
||||
);
|
||||
|
||||
$context = stream_context_create($opts);
|
||||
|
||||
try {
|
||||
$version = file_get_contents('http://typemill.net/tma1/checkversion', false, $context);
|
||||
|
||||
if ($version)
|
||||
{
|
||||
$version = json_decode($version);
|
||||
return $version->version;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
61
system/Models/Write.php
Normal file
61
system/Models/Write.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace System\Models;
|
||||
|
||||
class Write
|
||||
{
|
||||
protected $basePath;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$basePath = getcwd() . DIRECTORY_SEPARATOR;
|
||||
$this->basePath = $basePath;
|
||||
}
|
||||
|
||||
protected function checkPath($folder)
|
||||
{
|
||||
$folderPath = $this->basePath . $folder;
|
||||
|
||||
if(!is_dir($folderPath) AND !mkdir($folderPath, 0774, true))
|
||||
{
|
||||
throw new Exception("The folder '{$folder}' is missing and we could not create it. Please create the folder manually on your server.");
|
||||
return false;
|
||||
}
|
||||
if(!is_writable($folderPath))
|
||||
{
|
||||
throw new Exception("Please make the folder '{$folder}' writable.");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function checkFile($folder, $file)
|
||||
{
|
||||
if(!file_exists($this->basePath . $folder . DIRECTORY_SEPARATOR . $file))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function writeFile($folder, $file, $data)
|
||||
{
|
||||
$filePath = $this->basePath . $folder . DIRECTORY_SEPARATOR . $file;
|
||||
$openFile = fopen($filePath, "w");
|
||||
|
||||
fwrite($openFile, $data);
|
||||
fclose($openFile);
|
||||
}
|
||||
|
||||
public function getFile($folderName, $fileName)
|
||||
{
|
||||
if($this->checkFile($folderName, $fileName))
|
||||
{
|
||||
$fileContent = file_get_contents($folderName . DIRECTORY_SEPARATOR . $fileName);
|
||||
return $fileContent;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
85
system/Models/WriteCache.php
Normal file
85
system/Models/WriteCache.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace System\Models;
|
||||
|
||||
class WriteCache extends Write
|
||||
{
|
||||
/**
|
||||
* Validates, if the cache is valid or invalid and has to be refreshed
|
||||
* @param int $duration how many seconds the cache is valid.
|
||||
* @return boolean for an invalid cache (false) and for a valid cache (true).
|
||||
*/
|
||||
public function validate($folderName, $fileName, $duration)
|
||||
{
|
||||
if(isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!$this->checkPath($folderName))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!$this->checkFile($folderName, $fileName))
|
||||
{
|
||||
$this->writeFile($folderName, $fileName, time());
|
||||
return false;
|
||||
}
|
||||
|
||||
$lastRefresh = file_get_contents($folderName . DIRECTORY_SEPARATOR . $fileName);
|
||||
if(time() - $lastRefresh > $duration)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a cache file.
|
||||
* Serializes an object and writes it to the cache file together with a file that holds the last refresh time.
|
||||
* @param object $cacheData has to be an object (e.g. navigation object).
|
||||
* @param string $cacheFile has to be the name of the file you want to update (in case there are more than one cache files.
|
||||
*/
|
||||
public function updateCache($folderName, $cacheFileName, $requestFileName, $cacheData)
|
||||
{
|
||||
$sCacheData = serialize($cacheData);
|
||||
$this->writeFile($folderName, $cacheFileName, $sCacheData);
|
||||
if($requestFileName)
|
||||
{
|
||||
$this->writeFile($folderName, $requestFileName, time());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the recent cache.
|
||||
* Takes a filename, gets the file and unserializes the cache into an object.
|
||||
* @param string $fileName is the name of the cache file.
|
||||
*/
|
||||
public function getCache($folderName, $cacheFileName)
|
||||
{
|
||||
$sCacheData = $this->getFile($folderName, $cacheFileName);
|
||||
if($sCacheData)
|
||||
{
|
||||
return unserialize($sCacheData);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo Create a function to clear a specific cache file
|
||||
*/
|
||||
public function clearCache($name)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo Create a function to clear all cache files
|
||||
*/
|
||||
public function clearAllCacheFiles()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
47
system/Models/WriteSitemap.php
Normal file
47
system/Models/WriteSitemap.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace System\Models;
|
||||
|
||||
class WriteSitemap extends Write
|
||||
{
|
||||
public function updateSitemap($folderName, $sitemapFileName, $requestFileName, $data, $baseUrl)
|
||||
{
|
||||
$sitemap = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
|
||||
$sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
|
||||
$sitemap = $this->addUrlSet($sitemap, $baseUrl);
|
||||
$sitemap .= $this->generateUrlSets($data);
|
||||
$sitemap .= '</urlset>';
|
||||
|
||||
$this->writeFile($folderName, $sitemapFileName, $sitemap);
|
||||
$this->writeFile($folderName, $requestFileName, time());
|
||||
}
|
||||
|
||||
public function generateUrlSets($data)
|
||||
{
|
||||
$urlset = '';
|
||||
|
||||
foreach($data as $item)
|
||||
{
|
||||
if($item->elementType == 'folder')
|
||||
{
|
||||
$urlset = $this->addUrlSet($urlset, $item->urlAbs);
|
||||
$urlset .= $this->generateUrlSets($item->folderContent, $urlset);
|
||||
}
|
||||
else
|
||||
{
|
||||
$urlset = $this->addUrlSet($urlset, $item->urlAbs);
|
||||
}
|
||||
}
|
||||
return $urlset;
|
||||
}
|
||||
|
||||
public function addUrlSet($urlset, $url)
|
||||
{
|
||||
$urlset .= ' <url>' . "\n";
|
||||
$urlset .= ' <loc>' . $url . '</loc>' . "\n";
|
||||
$urlset .= ' </url>' . "\n";
|
||||
return $urlset;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
35
system/Models/WriteYaml.php
Normal file
35
system/Models/WriteYaml.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace System\Models;
|
||||
|
||||
class WriteYaml extends Write
|
||||
{
|
||||
/**
|
||||
* Get the a yaml file.
|
||||
* @param string $fileName is the name of the Yaml Folder.
|
||||
* @param string $yamlFileName is the name of the Yaml File.
|
||||
*/
|
||||
public function getYaml($folderName, $yamlFileName)
|
||||
{
|
||||
$yaml = $this->getFile($folderName, $yamlFileName);
|
||||
if($yaml)
|
||||
{
|
||||
return \Symfony\Component\Yaml\Yaml::parse($yaml);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a yaml file.
|
||||
* @param string $fileName is the name of the Yaml Folder.
|
||||
* @param string $yamlFileName is the name of the Yaml File.
|
||||
* @param array $contentArray is the content as an array.
|
||||
*/
|
||||
public function updateYaml($folderName, $yamlFileName, $contentArray)
|
||||
{
|
||||
$yaml = \Symfony\Component\Yaml\Yaml::dump($contentArray);
|
||||
$this->writeFile($folderName, $yamlFileName, $yaml);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user