Browse Source

PSR-2 conform

pull/110/head
Kanti 11 years ago
parent
commit
0237e04a0e
8 changed files with 366 additions and 298 deletions
  1. +40
    -26
      system/admin/admin.php
  2. +49
    -51
      system/htmly.php
  3. +75
    -58
      system/includes/dispatch.php
  4. +159
    -130
      system/includes/functions.php
  5. +6
    -8
      system/includes/opml.php
  6. +4
    -5
      system/includes/session.php
  7. +32
    -19
      system/includes/updater.php
  8. +1
    -1
      system/resources/htmly.js

+ 40
- 26
system/admin/admin.php View File

@ -1,7 +1,8 @@
<?php <?php
// Return username.ini value // Return username.ini value
function user($key, $user = null) {
function user($key, $user = null)
{
$value = 'config/users/' . $user . '.ini'; $value = 'config/users/' . $user . '.ini';
static $_config = array(); static $_config = array();
if (file_exists($value)) { if (file_exists($value)) {
@ -13,7 +14,8 @@ function user($key, $user = null) {
} }
// Create a session // Create a session
function session($user, $pass, $str = null) {
function session($user, $pass, $str = null)
{
$user_file = 'config/users/' . $user . '.ini'; $user_file = 'config/users/' . $user . '.ini';
$user_enc = user('encryption', $user); $user_enc = user('encryption', $user);
$user_pass = user('password', $user); $user_pass = user('password', $user);
@ -32,21 +34,22 @@ function session($user, $pass, $str = null) {
} }
// Clean URLs // Clean URLs
function remove_accent($str) {
function remove_accent($str)
{
$a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ', 'Į', 'į', 'İ', 'ı', 'IJ', 'ij', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', 'Ļ', 'ļ', 'Ľ', 'ľ', 'Ŀ', 'ŀ', 'Ł', 'ł', 'Ń', 'ń', 'Ņ', 'ņ', 'Ň', 'ň', 'ʼn', 'Ō', 'ō', 'Ŏ', 'ŏ', 'Ő', 'ő', 'Œ', 'œ', 'Ŕ', 'ŕ', 'Ŗ', 'ŗ', 'Ř', 'ř', 'Ś', 'ś', 'Ŝ', 'ŝ', 'Ş', 'ş', 'Š', 'š', 'Ţ', 'ţ', 'Ť', 'ť', 'Ŧ', 'ŧ', 'Ũ', 'ũ', 'Ū', 'ū', 'Ŭ', 'ŭ', 'Ů', 'ů', 'Ű', 'ű', 'Ų', 'ų', 'Ŵ', 'ŵ', 'Ŷ', 'ŷ', 'Ÿ', 'Ź', 'ź', 'Ż', 'ż', 'Ž', 'ž', 'ſ', 'ƒ', 'Ơ', 'ơ', 'Ư', 'ư', 'Ǎ', 'ǎ', 'Ǐ', 'ǐ', 'Ǒ', 'ǒ', 'Ǔ', 'ǔ', 'Ǖ', 'ǖ', 'Ǘ', 'ǘ', 'Ǚ', 'ǚ', 'Ǜ', 'ǜ', 'Ǻ', 'ǻ', 'Ǽ', 'ǽ', 'Ǿ', 'ǿ'); $a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ', 'Į', 'į', 'İ', 'ı', 'IJ', 'ij', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', 'Ļ', 'ļ', 'Ľ', 'ľ', 'Ŀ', 'ŀ', 'Ł', 'ł', 'Ń', 'ń', 'Ņ', 'ņ', 'Ň', 'ň', 'ʼn', 'Ō', 'ō', 'Ŏ', 'ŏ', 'Ő', 'ő', 'Œ', 'œ', 'Ŕ', 'ŕ', 'Ŗ', 'ŗ', 'Ř', 'ř', 'Ś', 'ś', 'Ŝ', 'ŝ', 'Ş', 'ş', 'Š', 'š', 'Ţ', 'ţ', 'Ť', 'ť', 'Ŧ', 'ŧ', 'Ũ', 'ũ', 'Ū', 'ū', 'Ŭ', 'ŭ', 'Ů', 'ů', 'Ű', 'ű', 'Ų', 'ų', 'Ŵ', 'ŵ', 'Ŷ', 'ŷ', 'Ÿ', 'Ź', 'ź', 'Ż', 'ż', 'Ž', 'ž', 'ſ', 'ƒ', 'Ơ', 'ơ', 'Ư', 'ư', 'Ǎ', 'ǎ', 'Ǐ', 'ǐ', 'Ǒ', 'ǒ', 'Ǔ', 'ǔ', 'Ǖ', 'ǖ', 'Ǘ', 'ǘ', 'Ǚ', 'ǚ', 'Ǜ', 'ǜ', 'Ǻ', 'ǻ', 'Ǽ', 'ǽ', 'Ǿ', 'ǿ');
$b = array('A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o'); $b = array('A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
$cyr = array('ж', 'ч', 'щ', 'ш', 'ю', 'а', 'б', 'в', 'г', 'д', 'e', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ъ', 'ь', 'я', 'Ж', 'Ч', 'Щ', 'Ш', 'Ю', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ъ', 'Ь', 'Я'); $cyr = array('ж', 'ч', 'щ', 'ш', 'ю', 'а', 'б', 'в', 'г', 'д', 'e', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ъ', 'ь', 'я', 'Ж', 'Ч', 'Щ', 'Ш', 'Ю', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ъ', 'Ь', 'Я');
$lat = array('zh', 'ch', 'sht', 'sh', 'yu', 'a', 'b', 'v', 'g', 'd', 'e', 'z', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'h', 'c', 'y', 'x', 'q', 'Zh', 'Ch', 'Sht', 'Sh', 'Yu', 'A', 'B', 'V', 'G', 'D', 'E', 'Z', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F', 'H', 'c', 'Y', 'X', 'Q'); $lat = array('zh', 'ch', 'sht', 'sh', 'yu', 'a', 'b', 'v', 'g', 'd', 'e', 'z', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'h', 'c', 'y', 'x', 'q', 'Zh', 'Ch', 'Sht', 'Sh', 'Yu', 'A', 'B', 'V', 'G', 'D', 'E', 'Z', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F', 'H', 'c', 'Y', 'X', 'Q');
$a = array_merge($a,$cyr);
$b = array_merge($b,$lat);
$a = array_merge($a, $cyr);
$b = array_merge($b, $lat);
return str_replace($a, $b, $str); return str_replace($a, $b, $str);
} }
// Edit blog posts // Edit blog posts
function edit_post($title, $tag, $url, $content, $oldfile, $destination = null, $description = null, $date = null) {
function edit_post($title, $tag, $url, $content, $oldfile, $destination = null, $description = null, $date = null)
{
$oldurl = explode('_', $oldfile); $oldurl = explode('_', $oldfile);
if($date !== null)
{
if($date !== null) {
$oldurl[0] = substr($oldurl[0], 0, strrpos($oldurl[0], '/')) . '/' . date('Y-m-d-h-i-s',strtotime($date)); $oldurl[0] = substr($oldurl[0], 0, strrpos($oldurl[0], '/')) . '/' . date('Y-m-d-h-i-s',strtotime($date));
} }
@ -97,7 +100,8 @@ function edit_post($title, $tag, $url, $content, $oldfile, $destination = null,
} }
// Edit static page // Edit static page
function edit_page($title, $url, $content, $oldfile, $destination = null, $description = null) {
function edit_page($title, $url, $content, $oldfile, $destination = null, $description = null)
{
$dir = substr($oldfile, 0, strrpos($oldfile, '/')); $dir = substr($oldfile, 0, strrpos($oldfile, '/'));
$post_title = $title; $post_title = $title;
@ -135,7 +139,8 @@ function edit_page($title, $url, $content, $oldfile, $destination = null, $descr
} }
// Add blog post // Add blog post
function add_post($title, $tag, $url, $content, $user, $description = null) {
function add_post($title, $tag, $url, $content, $user, $description = null)
{
$post_date = date('Y-m-d-H-i-s'); $post_date = date('Y-m-d-H-i-s');
$post_title = $title; $post_title = $title;
@ -169,7 +174,8 @@ function add_post($title, $tag, $url, $content, $user, $description = null) {
} }
// Add static page // Add static page
function add_page($title, $url, $content, $description = null) {
function add_page($title, $url, $content, $description = null)
{
$post_title = $title; $post_title = $title;
$post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 \-\p{L}]/u', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url))); $post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 \-\p{L}]/u', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url)));
@ -201,7 +207,8 @@ function add_page($title, $url, $content, $description = null) {
} }
// Add static sub page // Add static sub page
function add_sub_page($title, $url, $content, $static, $description = null) {
function add_sub_page($title, $url, $content, $static, $description = null)
{
$post_title = $title; $post_title = $title;
$post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 \-\p{L}]/u', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url))); $post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 \-\p{L}]/u', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url)));
@ -233,7 +240,8 @@ function add_sub_page($title, $url, $content, $static, $description = null) {
} }
// Delete blog post // Delete blog post
function delete_post($file, $destination) {
function delete_post($file, $destination)
{
if (!login()) if (!login())
return null; return null;
$deleted_content = $file; $deleted_content = $file;
@ -258,7 +266,8 @@ function delete_post($file, $destination) {
} }
// Delete static page // Delete static page
function delete_page($file, $destination) {
function delete_page($file, $destination)
{
if (!login()) if (!login())
return null; return null;
$deleted_content = $file; $deleted_content = $file;
@ -287,8 +296,8 @@ function delete_page($file, $destination) {
} }
// Edit user profile // Edit user profile
function edit_profile($title, $content, $user) {
function edit_profile($title, $content, $user)
{
$user_title = $title; $user_title = $title;
$user_content = '<!--t ' . $user_title . ' t-->' . "\n\n" . $content; $user_content = '<!--t ' . $user_title . ' t-->' . "\n\n" . $content;
@ -311,8 +320,8 @@ function edit_profile($title, $content, $user) {
} }
// Import RSS feed // Import RSS feed
function migrate($title, $time, $tags, $content, $url, $user, $source) {
function migrate($title, $time, $tags, $content, $url, $user, $source)
{
$post_date = date('Y-m-d-H-i-s', $time); $post_date = date('Y-m-d-H-i-s', $time);
$post_title = $title; $post_title = $title;
$post_tag = preg_replace(array('/[^a-zA-Z0-9,.\-\p{L}]/u', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($tags)); $post_tag = preg_replace(array('/[^a-zA-Z0-9,.\-\p{L}]/u', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($tags));
@ -341,7 +350,8 @@ function migrate($title, $time, $tags, $content, $url, $user, $source) {
} }
// Fetch RSS feed // Fetch RSS feed
function get_feed($feed_url, $credit, $message = null) {
function get_feed($feed_url, $credit, $message = null)
{
$source = file_get_contents($feed_url); $source = file_get_contents($feed_url);
$feed = new SimpleXmlElement($source); $feed = new SimpleXmlElement($source);
if (!empty($feed->channel->item)) { if (!empty($feed->channel->item)) {
@ -350,7 +360,7 @@ function get_feed($feed_url, $credit, $message = null) {
$descriptionB = $entry->description; $descriptionB = $entry->description;
if (!empty($descriptionA)) { if (!empty($descriptionA)) {
$content = $descriptionA; $content = $descriptionA;
} else if (!empty($descriptionB)) {
} elseif (!empty($descriptionB)) {
$content = preg_replace('#<br\s*/?>#i', "\n", $descriptionB); $content = preg_replace('#<br\s*/?>#i', "\n", $descriptionB);
} else { } else {
return $str = '<li>Can not read the feed content.</li>'; return $str = '<li>Can not read the feed content.</li>';
@ -376,7 +386,8 @@ function get_feed($feed_url, $credit, $message = null) {
} }
// Get recent posts by user // Get recent posts by user
function get_recent_posts() {
function get_recent_posts()
{
if (isset($_SESSION[config("site.url")]['user'])) { if (isset($_SESSION[config("site.url")]['user'])) {
$posts = get_profile($_SESSION[config("site.url")]['user'], 1, 5); $posts = get_profile($_SESSION[config("site.url")]['user'], 1, 5);
if (!empty($posts)) { if (!empty($posts)) {
@ -411,7 +422,8 @@ function get_recent_posts() {
} }
// Get all static pages // Get all static pages
function get_recent_pages() {
function get_recent_pages()
{
if (isset($_SESSION[config("site.url")]['user'])) { if (isset($_SESSION[config("site.url")]['user'])) {
$posts = get_static_post(null); $posts = get_static_post(null);
if (!empty($posts)) { if (!empty($posts)) {
@ -458,7 +470,8 @@ function get_recent_pages() {
} }
// Get all available zip files // Get all available zip files
function get_backup_files() {
function get_backup_files()
{
if (isset($_SESSION[config("site.url")]['user'])) { if (isset($_SESSION[config("site.url")]['user'])) {
$files = get_zip_files(); $files = get_zip_files();
if (!empty($files)) { if (!empty($files)) {
@ -505,8 +518,8 @@ function get_backup_files() {
} }
} }
function clear_post_cache($post_date, $post_tag, $post_url, $filename) {
function clear_post_cache($post_date, $post_tag, $post_url, $filename)
{
$b = str_replace('/', '#', site_path() . '/'); $b = str_replace('/', '#', site_path() . '/');
$t = explode('-', $post_date); $t = explode('-', $post_date);
$c = explode(',', $post_tag); $c = explode(',', $post_tag);
@ -585,7 +598,8 @@ function clear_post_cache($post_date, $post_tag, $post_url, $filename) {
} }
} }
function clear_page_cache($url) {
function clear_page_cache($url)
{
$b = str_replace('/', '#', site_path() . '/'); $b = str_replace('/', '#', site_path() . '/');
$p = 'cache/page/' . $b . $url . '.cache'; $p = 'cache/page/' . $b . $url . '.cache';
if (file_exists($p)) { if (file_exists($p)) {


+ 49
- 51
system/htmly.php View File

@ -13,8 +13,7 @@ include 'system/includes/opml.php';
// Load the configuration file // Load the configuration file
config('source', 'config/config.ini'); config('source', 'config/config.ini');
if(config('timezone'))
{
if(config('timezone')) {
date_default_timezone_set(config('timezone')); date_default_timezone_set(config('timezone'));
} }
@ -64,7 +63,7 @@ get('/index', function () {
}); });
// Get submitted login data // Get submitted login data
post('/login', function() {
post('/login', function () {
$proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); $proper = is_csrf_proper(from($_REQUEST, 'csrf_token'));
@ -111,7 +110,7 @@ post('/login', function() {
} }
}); });
get("/:static/:sub/edit", function($static, $sub) {
get("/:static/:sub/edit", function ($static, $sub) {
if (login()) { if (login()) {
@ -144,7 +143,7 @@ get("/:static/:sub/edit", function($static, $sub) {
header("location: $login"); header("location: $login");
} }
}); });
post("/:static/:sub/edit", function($static, $sub) {
post("/:static/:sub/edit", function ($static, $sub) {
$proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); $proper = is_csrf_proper(from($_REQUEST, 'csrf_token'));
if (!login()) { if (!login()) {
@ -194,7 +193,7 @@ post("/:static/:sub/edit", function($static, $sub) {
} }
}); });
get("/:static/:sub/delete", function($static, $sub) {
get("/:static/:sub/delete", function ($static, $sub) {
if (login()) { if (login()) {
@ -227,7 +226,7 @@ get("/:static/:sub/delete", function($static, $sub) {
header("location: $login"); header("location: $login");
} }
}); });
post("/:static/:sub/delete", function() {
post("/:static/:sub/delete", function () {
$proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); $proper = is_csrf_proper(from($_REQUEST, 'csrf_token'));
if ($proper && login()) { if ($proper && login()) {
@ -238,7 +237,7 @@ post("/:static/:sub/delete", function() {
}); });
// The blog post page // The blog post page
get('/:year/:month/:name', function($year, $month, $name) {
get('/:year/:month/:name', function ($year, $month, $name) {
if (config("views.counter") != "true") { if (config("views.counter") != "true") {
if (!login()) { if (!login()) {
@ -295,7 +294,7 @@ get('/:year/:month/:name', function($year, $month, $name) {
}); });
// Edit blog post // Edit blog post
get('/:year/:month/:name/edit', function($year, $month, $name) {
get('/:year/:month/:name/edit', function ($year, $month, $name) {
if (login()) { if (login()) {
@ -333,7 +332,7 @@ get('/:year/:month/:name/edit', function($year, $month, $name) {
}); });
// Get edited data for blog post // Get edited data for blog post
post('/:year/:month/:name/edit', function() {
post('/:year/:month/:name/edit', function () {
$proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); $proper = is_csrf_proper(from($_REQUEST, 'csrf_token'));
@ -347,11 +346,10 @@ post('/:year/:month/:name/edit', function() {
$date = from($_REQUEST, 'date'); $date = from($_REQUEST, 'date');
$time = from($_REQUEST, 'time'); $time = from($_REQUEST, 'time');
$dateTime = null; $dateTime = null;
if($date !== null && $time !== null)
{
if($date !== null && $time !== null) {
$dateTime = $date . ' ' . $time; $dateTime = $date . ' ' . $time;
} }
if ($proper && !empty($title) && !empty($tag) && !empty($content)) { if ($proper && !empty($title) && !empty($tag) && !empty($content)) {
if(empty($url)) { if(empty($url)) {
$url = $title; $url = $title;
@ -388,7 +386,7 @@ post('/:year/:month/:name/edit', function() {
}); });
// Delete blog post // Delete blog post
get('/:year/:month/:name/delete', function($year, $month, $name) {
get('/:year/:month/:name/delete', function ($year, $month, $name) {
if (login()) { if (login()) {
@ -426,7 +424,7 @@ get('/:year/:month/:name/delete', function($year, $month, $name) {
}); });
// Get deleted data for blog post // Get deleted data for blog post
post('/:year/:month/:name/delete', function() {
post('/:year/:month/:name/delete', function () {
$proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); $proper = is_csrf_proper(from($_REQUEST, 'csrf_token'));
if ($proper && login()) { if ($proper && login()) {
@ -437,7 +435,7 @@ post('/:year/:month/:name/delete', function() {
}); });
// The author page // The author page
get('/author/:profile', function($profile) {
get('/author/:profile', function ($profile) {
if (!login()) { if (!login()) {
file_cache($_SERVER['REQUEST_URI']); file_cache($_SERVER['REQUEST_URI']);
@ -486,7 +484,7 @@ get('/author/:profile', function($profile) {
}); });
// Edit the profile // Edit the profile
get('/edit/profile', function() {
get('/edit/profile', function () {
if (login()) { if (login()) {
@ -503,7 +501,7 @@ get('/edit/profile', function() {
}); });
// Get edited data for static page // Get edited data for static page
post('/edit/profile', function() {
post('/edit/profile', function () {
$proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); $proper = is_csrf_proper(from($_REQUEST, 'csrf_token'));
@ -596,7 +594,7 @@ get('/admin/posts', function () {
}); });
// The author page // The author page
get('/admin/mine', function() {
get('/admin/mine', function () {
if (login()) { if (login()) {
@ -653,7 +651,7 @@ get('/admin/mine', function() {
}); });
// The static page // The static page
get('/:static', function($static) {
get('/:static', function ($static) {
if ($static === 'sitemap.xml' || $static === 'sitemap.base.xml' || $static === 'sitemap.post.xml' || $static === 'sitemap.static.xml' || $static === 'sitemap.tag.xml' || $static === 'sitemap.archive.xml' || $static === 'sitemap.author.xml') { if ($static === 'sitemap.xml' || $static === 'sitemap.base.xml' || $static === 'sitemap.post.xml' || $static === 'sitemap.static.xml' || $static === 'sitemap.tag.xml' || $static === 'sitemap.archive.xml' || $static === 'sitemap.author.xml') {
@ -661,17 +659,17 @@ get('/:static', function($static) {
if ($static === 'sitemap.xml') { if ($static === 'sitemap.xml') {
generate_sitemap('index'); generate_sitemap('index');
} else if ($static === 'sitemap.base.xml') {
} elseif ($static === 'sitemap.base.xml') {
generate_sitemap('base'); generate_sitemap('base');
} else if ($static === 'sitemap.post.xml') {
} elseif ($static === 'sitemap.post.xml') {
generate_sitemap('post'); generate_sitemap('post');
} else if ($static === 'sitemap.static.xml') {
} elseif ($static === 'sitemap.static.xml') {
generate_sitemap('static'); generate_sitemap('static');
} else if ($static === 'sitemap.tag.xml') {
} elseif ($static === 'sitemap.tag.xml') {
generate_sitemap('tag'); generate_sitemap('tag');
} else if ($static === 'sitemap.archive.xml') {
} elseif ($static === 'sitemap.archive.xml') {
generate_sitemap('archive'); generate_sitemap('archive');
} else if ($static === 'sitemap.author.xml') {
} elseif ($static === 'sitemap.author.xml') {
generate_sitemap('author'); generate_sitemap('author');
} }
@ -744,7 +742,7 @@ get('/:static', function($static) {
}); });
// Edit the static page // Edit the static page
get('/:static/edit', function($static) {
get('/:static/edit', function ($static) {
if (login()) { if (login()) {
@ -771,7 +769,7 @@ get('/:static/edit', function($static) {
}); });
// Get edited data for static page // Get edited data for static page
post('/:static/edit', function() {
post('/:static/edit', function () {
$proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); $proper = is_csrf_proper(from($_REQUEST, 'csrf_token'));
if (!login()) { if (!login()) {
@ -819,7 +817,7 @@ post('/:static/edit', function() {
}); });
// Deleted the static page // Deleted the static page
get('/:static/delete', function($static) {
get('/:static/delete', function ($static) {
if (login()) { if (login()) {
@ -846,7 +844,7 @@ get('/:static/delete', function($static) {
}); });
// Get deleted data for static page // Get deleted data for static page
post('/:static/delete', function() {
post('/:static/delete', function () {
$proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); $proper = is_csrf_proper(from($_REQUEST, 'csrf_token'));
if ($proper && login()) { if ($proper && login()) {
@ -857,7 +855,7 @@ post('/:static/delete', function() {
}); });
// Add blog post // Add blog post
get('/add/post', function() {
get('/add/post', function () {
if (login()) { if (login()) {
@ -875,7 +873,7 @@ get('/add/post', function() {
}); });
// Get submitted blog post data // Get submitted blog post data
post('/add/post', function() {
post('/add/post', function () {
$proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); $proper = is_csrf_proper(from($_REQUEST, 'csrf_token'));
@ -921,7 +919,7 @@ post('/add/post', function() {
}); });
// Add the static page // Add the static page
get('/add/page', function() {
get('/add/page', function () {
if (login()) { if (login()) {
@ -939,7 +937,7 @@ get('/add/page', function() {
}); });
// Get submitted static page data // Get submitted static page data
post('/add/page', function() {
post('/add/page', function () {
$proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); $proper = is_csrf_proper(from($_REQUEST, 'csrf_token'));
@ -979,7 +977,7 @@ post('/add/page', function() {
}); });
// Import page // Import page
get('/admin/import', function() {
get('/admin/import', function () {
if (login()) { if (login()) {
config('views.root', 'system/admin/views'); config('views.root', 'system/admin/views');
render('import', array( render('import', array(
@ -995,7 +993,7 @@ get('/admin/import', function() {
}); });
// Get import post // Get import post
post('/admin/import', function() {
post('/admin/import', function () {
$proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); $proper = is_csrf_proper(from($_REQUEST, 'csrf_token'));
@ -1039,7 +1037,7 @@ post('/admin/import', function() {
}); });
// Backup page // Backup page
get('/admin/backup', function() {
get('/admin/backup', function () {
if (login()) { if (login()) {
config('views.root', 'system/admin/views'); config('views.root', 'system/admin/views');
render('backup', array( render('backup', array(
@ -1055,7 +1053,7 @@ get('/admin/backup', function() {
}); });
// Create Zip file // Create Zip file
get('/admin/backup-start', function() {
get('/admin/backup-start', function () {
if (login()) { if (login()) {
config('views.root', 'system/admin/views'); config('views.root', 'system/admin/views');
render('backup-start', array( render('backup-start', array(
@ -1071,7 +1069,7 @@ get('/admin/backup-start', function() {
}); });
// Delete all cache // Delete all cache
get('/admin/clear-cache', function() {
get('/admin/clear-cache', function () {
if (login()) { if (login()) {
config('views.root', 'system/admin/views'); config('views.root', 'system/admin/views');
render('clear-cache', array( render('clear-cache', array(
@ -1088,7 +1086,7 @@ get('/admin/clear-cache', function() {
// The tag page // The tag page
get('/tag/:tag', function($tag) {
get('/tag/:tag', function ($tag) {
if (!login()) { if (!login()) {
file_cache($_SERVER['REQUEST_URI']); file_cache($_SERVER['REQUEST_URI']);
@ -1118,7 +1116,7 @@ get('/tag/:tag', function($tag) {
}); });
// The archive page // The archive page
get('/archive/:req', function($req) {
get('/archive/:req', function ($req) {
if (!login()) { if (!login()) {
file_cache($_SERVER['REQUEST_URI']); file_cache($_SERVER['REQUEST_URI']);
@ -1142,7 +1140,7 @@ get('/archive/:req', function($req) {
if (isset($time[0]) && isset($time[1]) && isset($time[2])) { if (isset($time[0]) && isset($time[1]) && isset($time[2])) {
$timestamp = date('d F Y', $date); $timestamp = date('d F Y', $date);
} else if (isset($time[0]) && isset($time[1])) {
} elseif (isset($time[0]) && isset($time[1])) {
$timestamp = date('F Y', $date); $timestamp = date('F Y', $date);
} else { } else {
$timestamp = $req; $timestamp = $req;
@ -1164,7 +1162,7 @@ get('/archive/:req', function($req) {
}); });
// The search page // The search page
get('/search/:keyword', function($keyword) {
get('/search/:keyword', function ($keyword) {
if (!login()) { if (!login()) {
file_cache($_SERVER['REQUEST_URI']); file_cache($_SERVER['REQUEST_URI']);
@ -1195,7 +1193,7 @@ get('/search/:keyword', function($keyword) {
}); });
// The JSON API // The JSON API
get('/api/json', function() {
get('/api/json', function () {
header('Content-type: application/json'); header('Content-type: application/json');
@ -1207,7 +1205,7 @@ get('/api/json', function() {
}); });
// Show the RSS feed // Show the RSS feed
get('/feed/rss', function() {
get('/feed/rss', function () {
header('Content-Type: application/rss+xml'); header('Content-Type: application/rss+xml');
@ -1216,7 +1214,7 @@ get('/feed/rss', function() {
}); });
// Generate OPML file // Generate OPML file
get('/feed/opml', function() {
get('/feed/opml', function () {
header('Content-Type: text/xml'); header('Content-Type: text/xml');
@ -1224,7 +1222,7 @@ get('/feed/opml', function() {
echo generate_opml(); echo generate_opml();
}); });
get('/admin/update/now/:csrf', function($CSRF) {
get('/admin/update/now/:csrf', function ($CSRF) {
$proper = is_csrf_proper($CSRF); $proper = is_csrf_proper($CSRF);
$updater = new \Kanti\HubUpdater(array( $updater = new \Kanti\HubUpdater(array(
@ -1244,7 +1242,7 @@ get('/admin/update/now/:csrf', function($CSRF) {
} }
}); });
get('/:static/add', function($static) {
get('/:static/add', function ($static) {
if (login()) { if (login()) {
@ -1268,7 +1266,7 @@ get('/:static/add', function($static) {
header("location: $login"); header("location: $login");
} }
}); });
post('/:static/add', function($static) {
post('/:static/add', function ($static) {
$proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); $proper = is_csrf_proper(from($_REQUEST, 'csrf_token'));
@ -1307,7 +1305,7 @@ post('/:static/add', function($static) {
} }
}); });
get('/:static/:sub', function($static, $sub) {
get('/:static/:sub', function ($static, $sub) {
$father_post = get_static_post($static); $father_post = get_static_post($static);
if (!$father_post) { if (!$father_post) {
@ -1339,7 +1337,7 @@ get('/:static/:sub', function($static, $sub) {
// If we get here, it means that // If we get here, it means that
// nothing has been matched above // nothing has been matched above
get('.*', function() {
get('.*', function () {
not_found(); not_found();
}); });


+ 75
- 58
system/includes/dispatch.php View File

@ -3,7 +3,8 @@ if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 50300) {
error(500, 'dispatch requires at least PHP 5.3 to run.'); error(500, 'dispatch requires at least PHP 5.3 to run.');
} }
function _log($message) {
function _log($message)
{
if (config('debug.enable') == true && php_sapi_name() !== 'cli') { if (config('debug.enable') == true && php_sapi_name() !== 'cli') {
$file = config('debug.log'); $file = config('debug.log');
$type = $file ? 3 : 0; $type = $file ? 3 : 0;
@ -11,8 +12,8 @@ function _log($message) {
} }
} }
function site_url(){
function site_url()
{
if (config('site.url') == null) if (config('site.url') == null)
error(500, '[site.url] is not set'); error(500, '[site.url] is not set');
@ -20,36 +21,39 @@ function site_url(){
return rtrim(config('site.url'),'/').'/'; return rtrim(config('site.url'),'/').'/';
} }
function site_path(){
function site_path()
{
static $_path; static $_path;
if (config('site.url') == null) if (config('site.url') == null)
error(500, '[site.url] is not set'); error(500, '[site.url] is not set');
if (!$_path) if (!$_path)
$_path = rtrim(parse_url(config('site.url'), PHP_URL_PATH),'/'); $_path = rtrim(parse_url(config('site.url'), PHP_URL_PATH),'/');
return $_path; return $_path;
} }
function error($code, $message) {
function error($code, $message)
{
@header("HTTP/1.0 {$code} {$message}", true, $code); @header("HTTP/1.0 {$code} {$message}", true, $code);
die($message); die($message);
} }
function config($key, $value = null) {
function config($key, $value = null)
{
static $_config = array(); static $_config = array();
if ($key === 'source' && file_exists($value)) if ($key === 'source' && file_exists($value))
$_config = parse_ini_file($value, true); $_config = parse_ini_file($value, true);
else if ($value == null)
elseif ($value == null)
return (isset($_config[$key]) ? $_config[$key] : null); return (isset($_config[$key]) ? $_config[$key] : null);
else else
$_config[$key] = $value; $_config[$key] = $value;
} }
function to_b64($str) {
function to_b64($str)
{
$str = base64_encode($str); $str = base64_encode($str);
$str = preg_replace('/\//', '_', $str); $str = preg_replace('/\//', '_', $str);
$str = preg_replace('/\+/', '.', $str); $str = preg_replace('/\+/', '.', $str);
@ -57,7 +61,8 @@ function to_b64($str) {
return trim($str, '-'); return trim($str, '-');
} }
function from_b64($str) {
function from_b64($str)
{
$str = preg_replace('/\_/', '/', $str); $str = preg_replace('/\_/', '/', $str);
$str = preg_replace('/\./', '+', $str); $str = preg_replace('/\./', '+', $str);
$str = preg_replace('/\-/', '=', $str); $str = preg_replace('/\-/', '=', $str);
@ -67,8 +72,8 @@ function from_b64($str) {
if (extension_loaded('mcrypt')) { if (extension_loaded('mcrypt')) {
function encrypt($decoded, $algo = MCRYPT_RIJNDAEL_256, $mode = MCRYPT_MODE_CBC) {
function encrypt($decoded, $algo = MCRYPT_RIJNDAEL_256, $mode = MCRYPT_MODE_CBC)
{
if (($secret = config('cookies.secret')) == null) if (($secret = config('cookies.secret')) == null)
error(500, '[cookies.secret] is not set'); error(500, '[cookies.secret] is not set');
@ -80,8 +85,8 @@ if (extension_loaded('mcrypt')) {
return sprintf('%s|%s', $encrypted, to_b64($iv_code)); return sprintf('%s|%s', $encrypted, to_b64($iv_code));
} }
function decrypt($encoded, $algo = MCRYPT_RIJNDAEL_256, $mode = MCRYPT_MODE_CBC) {
function decrypt($encoded, $algo = MCRYPT_RIJNDAEL_256, $mode = MCRYPT_MODE_CBC)
{
if (($secret = config('cookies.secret')) == null) if (($secret = config('cookies.secret')) == null)
error(500, '[cookies.secret] is not set'); error(500, '[cookies.secret] is not set');
@ -96,13 +101,14 @@ if (extension_loaded('mcrypt')) {
} }
function set_cookie($name, $value, $expire = 31536000, $path = '/') {
function set_cookie($name, $value, $expire = 31536000, $path = '/')
{
$value = (function_exists('encrypt') ? encrypt($value) : $value); $value = (function_exists('encrypt') ? encrypt($value) : $value);
setcookie($name, $value, time() + $expire, $path); setcookie($name, $value, time() + $expire, $path);
} }
function get_cookie($name) {
function get_cookie($name)
{
$value = from($_COOKIE, $name); $value = from($_COOKIE, $name);
if ($value) if ($value)
@ -111,7 +117,8 @@ function get_cookie($name) {
return $value; return $value;
} }
function delete_cookie() {
function delete_cookie()
{
$cookies = func_get_args(); $cookies = func_get_args();
foreach ($cookies as $ck) foreach ($cookies as $ck)
setcookie($ck, '', -10, '/'); setcookie($ck, '', -10, '/');
@ -120,7 +127,8 @@ function delete_cookie() {
// if we have APC loaded, enable cache functions // if we have APC loaded, enable cache functions
if (extension_loaded('apc')) { if (extension_loaded('apc')) {
function cache($key, $func, $ttl = 0) {
function cache($key, $func, $ttl = 0)
{
if (($data = apc_fetch($key)) === false) { if (($data = apc_fetch($key)) === false) {
$data = call_user_func($func); $data = call_user_func($func);
if ($data !== null) { if ($data !== null) {
@ -130,7 +138,8 @@ if (extension_loaded('apc')) {
return $data; return $data;
} }
function cache_invalidate() {
function cache_invalidate()
{
foreach (func_get_args() as $key) { foreach (func_get_args() as $key) {
apc_delete($key); apc_delete($key);
} }
@ -138,8 +147,8 @@ if (extension_loaded('apc')) {
} }
function warn($name = null, $message = null) {
function warn($name = null, $message = null)
{
static $warnings = array(); static $warnings = array();
if ($name == '*') if ($name == '*')
@ -154,15 +163,18 @@ function warn($name = null, $message = null) {
$warnings[$name] = $message; $warnings[$name] = $message;
} }
function _u($str) {
function _u($str)
{
return urlencode($str); return urlencode($str);
} }
function _h($str, $enc = 'UTF-8', $flags = ENT_QUOTES) {
function _h($str, $enc = 'UTF-8', $flags = ENT_QUOTES)
{
return htmlentities($str, $flags, $enc); return htmlentities($str, $flags, $enc);
} }
function from($source, $name) {
function from($source, $name)
{
if (is_array($name)) { if (is_array($name)) {
$data = array(); $data = array();
foreach ($name as $k) foreach ($name as $k)
@ -172,8 +184,8 @@ function from($source, $name) {
return isset($source[$name]) ? $source[$name] : null ; return isset($source[$name]) ? $source[$name] : null ;
} }
function stash($name, $value = null) {
function stash($name, $value = null)
{
static $_stash = array(); static $_stash = array();
if ($value === null) if ($value === null)
@ -184,26 +196,26 @@ function stash($name, $value = null) {
return $value; return $value;
} }
function method($verb = null) {
function method($verb = null)
{
if ($verb == null || (strtoupper($verb) == strtoupper($_SERVER['REQUEST_METHOD']))) if ($verb == null || (strtoupper($verb) == strtoupper($_SERVER['REQUEST_METHOD'])))
return strtoupper($_SERVER['REQUEST_METHOD']); return strtoupper($_SERVER['REQUEST_METHOD']);
error(400, 'bad request'); error(400, 'bad request');
} }
function client_ip() {
function client_ip()
{
if (isset($_SERVER['HTTP_CLIENT_IP'])) if (isset($_SERVER['HTTP_CLIENT_IP']))
return $_SERVER['HTTP_CLIENT_IP']; return $_SERVER['HTTP_CLIENT_IP'];
else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
return $_SERVER['HTTP_X_FORWARDED_FOR']; return $_SERVER['HTTP_X_FORWARDED_FOR'];
return $_SERVER['REMOTE_ADDR']; return $_SERVER['REMOTE_ADDR'];
} }
function redirect(/* $code_or_path, $path_or_cond, $cond */) {
function redirect(/* $code_or_path, $path_or_cond, $cond */)
{
$argv = func_get_args(); $argv = func_get_args();
$argc = count($argv); $argc = count($argv);
@ -243,8 +255,8 @@ function redirect(/* $code_or_path, $path_or_cond, $cond */) {
exit; exit;
} }
function partial($view, $locals = null) {
function partial($view, $locals = null)
{
if (is_array($locals) && count($locals)) { if (is_array($locals) && count($locals)) {
extract($locals, EXTR_SKIP); extract($locals, EXTR_SKIP);
} }
@ -267,11 +279,13 @@ function partial($view, $locals = null) {
return ''; return '';
} }
function content($value = null) {
function content($value = null)
{
return stash('$content$', $value); return stash('$content$', $value);
} }
function render($view, $locals = null, $layout = null) {
function render($view, $locals = null, $layout = null)
{
$login = login(); $login = login();
if(!$login) { if(!$login) {
$c = str_replace('/', '#', str_replace('?', '~', $_SERVER['REQUEST_URI'])); $c = str_replace('/', '#', str_replace('?', '~', $_SERVER['REQUEST_URI']));
@ -319,14 +333,15 @@ function render($view, $locals = null, $layout = null) {
} }
} }
function json($obj, $code = 200) {
function json($obj, $code = 200)
{
header('Content-type: application/json', true, $code); header('Content-type: application/json', true, $code);
echo json_encode($obj); echo json_encode($obj);
exit; exit;
} }
function condition() {
function condition()
{
static $cb_map = array(); static $cb_map = array();
$argv = func_get_args(); $argv = func_get_args();
@ -350,8 +365,8 @@ function condition() {
error(500, 'condition ['.$name.'] is undefined'); error(500, 'condition ['.$name.'] is undefined');
} }
function middleware($cb_or_path = null) {
function middleware($cb_or_path = null)
{
static $cb_map = array(); static $cb_map = array();
if ($cb_or_path == null || is_string($cb_or_path)) { if ($cb_or_path == null || is_string($cb_or_path)) {
@ -363,8 +378,8 @@ function middleware($cb_or_path = null) {
} }
} }
function filter($sym, $cb_or_val = null) {
function filter($sym, $cb_or_val = null)
{
static $cb_map = array(); static $cb_map = array();
if (is_callable($cb_or_val)) { if (is_callable($cb_or_val)) {
@ -384,7 +399,8 @@ function filter($sym, $cb_or_val = null) {
error(500, 'bad call to filter()'); error(500, 'bad call to filter()');
} }
function route_to_regex($route) {
function route_to_regex($route)
{
$route = preg_replace_callback('@:[\w]+@i', function ($matches) { $route = preg_replace_callback('@:[\w]+@i', function ($matches) {
$token = str_replace(':', '', $matches[0]); $token = str_replace(':', '', $matches[0]);
return '(?P<'.$token.'>[a-z0-9_\0-\.]+)'; return '(?P<'.$token.'>[a-z0-9_\0-\.]+)';
@ -392,8 +408,8 @@ function route_to_regex($route) {
return '@^'.rtrim($route, '/').'$@i'; return '@^'.rtrim($route, '/').'$@i';
} }
function route($method, $pattern, $callback = null) {
function route($method, $pattern, $callback = null)
{
// callback map by request type // callback map by request type
static $route_map = array( static $route_map = array(
'GET' => array(), 'GET' => array(),
@ -458,16 +474,18 @@ function route($method, $pattern, $callback = null) {
} }
function get($path, $cb) {
function get($path, $cb)
{
route('GET', $path, $cb); route('GET', $path, $cb);
} }
function post($path, $cb) {
function post($path, $cb)
{
route('POST', $path, $cb); route('POST', $path, $cb);
} }
function flash($key, $msg = null, $now = false) {
function flash($key, $msg = null, $now = false)
{
static $x = array(), static $x = array(),
$f = null; $f = null;
@ -497,10 +515,10 @@ function flash($key, $msg = null, $now = false) {
$x[$key] = $msg; $x[$key] = $msg;
} }
function dispatch() {
function dispatch()
{
$path = urldecode($_SERVER['REQUEST_URI']); $path = urldecode($_SERVER['REQUEST_URI']);
if (config('site.url') !== null) if (config('site.url') !== null)
$path = preg_replace('@^'.preg_quote(site_path()).'@', '', $path); $path = preg_replace('@^'.preg_quote(site_path()).'@', '', $path);
@ -511,4 +529,3 @@ function dispatch() {
route(method(), "/{$uri}"); route(method(), "/{$uri}");
} }
?>

+ 159
- 130
system/includes/functions.php View File

@ -10,8 +10,8 @@ use \Suin\RSSWriter\Item;
use \Kanti\HubUpdater; use \Kanti\HubUpdater;
// Get blog post path. Unsorted. Mostly used on widget. // Get blog post path. Unsorted. Mostly used on widget.
function get_post_unsorted() {
function get_post_unsorted()
{
static $_unsorted = array(); static $_unsorted = array();
if (empty($_unsorted)) { if (empty($_unsorted)) {
@ -26,8 +26,8 @@ function get_post_unsorted() {
} }
// Get blog post with more info about the path. Sorted by filename. // Get blog post with more info about the path. Sorted by filename.
function get_post_sorted() {
function get_post_sorted()
{
static $_sorted = array(); static $_sorted = array();
if (empty($_sorted)) { if (empty($_sorted)) {
@ -40,9 +40,9 @@ function get_post_sorted() {
return $_sorted; return $_sorted;
} }
// Get static page path. Unsorted.
function get_static_pages() {
// Get static page path. Unsorted.
function get_static_pages()
{
static $_page = array(); static $_page = array();
if (empty($_page)) { if (empty($_page)) {
@ -55,9 +55,9 @@ function get_static_pages() {
return $_page; return $_page;
} }
// Get static page path. Unsorted.
function get_static_sub_pages($static = null) {
// Get static page path. Unsorted.
function get_static_sub_pages($static = null)
{
static $_sub_page = array(); static $_sub_page = array();
if (empty($_sub_page)) { if (empty($_sub_page)) {
@ -69,7 +69,7 @@ function get_static_sub_pages($static = null) {
} }
if ($static != null) { if ($static != null) {
$stringLen = strlen($static); $stringLen = strlen($static);
return array_filter($_sub_page, function($sub_page)use($static, $stringLen) {
return array_filter($_sub_page, function ($sub_page) use ($static, $stringLen) {
$x = explode("/", $sub_page); $x = explode("/", $sub_page);
if ($x[count($x) - 2] == $static) { if ($x[count($x) - 2] == $static) {
return true; return true;
@ -80,9 +80,9 @@ function get_static_sub_pages($static = null) {
return $_sub_page; return $_sub_page;
} }
// Get author bio path. Unsorted.
function get_author_names() {
// Get author bio path. Unsorted.
function get_author_names()
{
static $_author = array(); static $_author = array();
if (empty($_author)) { if (empty($_author)) {
@ -96,9 +96,9 @@ function get_author_names() {
return $_author; return $_author;
} }
// Get backup file.
function get_zip_files() {
// Get backup file.
function get_zip_files()
{
static $_zip = array(); static $_zip = array();
if (empty($_zip)) { if (empty($_zip)) {
@ -113,18 +113,20 @@ function get_zip_files() {
} }
// usort function. Sort by filename. // usort function. Sort by filename.
function sortfile($a, $b) {
function sortfile($a, $b)
{
return $a['filename'] == $b['filename'] ? 0 : ( $a['filename'] < $b['filename'] ) ? 1 : -1; return $a['filename'] == $b['filename'] ? 0 : ( $a['filename'] < $b['filename'] ) ? 1 : -1;
} }
// usort function. Sort by date. // usort function. Sort by date.
function sortdate($a, $b) {
function sortdate($a, $b)
{
return $a->date == $b->date ? 0 : ( $a->date < $b->date ) ? 1 : -1; return $a->date == $b->date ? 0 : ( $a->date < $b->date ) ? 1 : -1;
} }
// Rebuilt cache index // Rebuilt cache index
function rebuilt_cache($type) {
function rebuilt_cache($type)
{
$dir = 'cache/index'; $dir = 'cache/index';
$posts_cache_sorted = array(); $posts_cache_sorted = array();
$posts_cache_unsorted = array(); $posts_cache_unsorted = array();
@ -174,9 +176,9 @@ function rebuilt_cache($type) {
} }
} }
// Return blog posts.
function get_posts($posts, $page = 1, $perpage = 0) {
// Return blog posts.
function get_posts($posts, $page = 1, $perpage = 0)
{
if (empty($posts)) { if (empty($posts)) {
$posts = get_post_sorted(); $posts = get_post_sorted();
} }
@ -239,7 +241,7 @@ function get_posts($posts, $page = 1, $perpage = 0) {
$post->tagb = implode(' » ', $bc); $post->tagb = implode(' » ', $bc);
$post->file = $filepath; $post->file = $filepath;
$content = file_get_contents($filepath); $content = file_get_contents($filepath);
// Extract the title and body // Extract the title and body
@ -261,8 +263,8 @@ function get_posts($posts, $page = 1, $perpage = 0) {
} }
// Find post by year, month and name, previous, and next. // Find post by year, month and name, previous, and next.
function find_post($year, $month, $name) {
function find_post($year, $month, $name)
{
$posts = get_post_sorted(); $posts = get_post_sorted();
foreach ($posts as $index => $v) { foreach ($posts as $index => $v) {
@ -305,8 +307,8 @@ function find_post($year, $month, $name) {
} }
// Return tag page. // Return tag page.
function get_tag($tag, $page, $perpage, $random) {
function get_tag($tag, $page, $perpage, $random)
{
$posts = get_post_sorted(); $posts = get_post_sorted();
if ($random === true) { if ($random === true) {
@ -340,8 +342,8 @@ function get_tag($tag, $page, $perpage, $random) {
} }
// Return archive page. // Return archive page.
function get_archive($req, $page, $perpage) {
function get_archive($req, $page, $perpage)
{
$posts = get_post_sorted(); $posts = get_post_sorted();
$tmp = array(); $tmp = array();
@ -362,8 +364,8 @@ function get_archive($req, $page, $perpage) {
} }
// Return posts list on profile. // Return posts list on profile.
function get_profile($profile, $page, $perpage) {
function get_profile($profile, $page, $perpage)
{
$posts = get_post_sorted(); $posts = get_post_sorted();
$tmp = array(); $tmp = array();
@ -385,8 +387,8 @@ function get_profile($profile, $page, $perpage) {
} }
// Return author bio. // Return author bio.
function get_bio($author) {
function get_bio($author)
{
$names = get_author_names(); $names = get_author_names();
$username = 'config/users/' . $author . '.ini'; $username = 'config/users/' . $author . '.ini';
@ -431,8 +433,8 @@ function get_bio($author) {
} }
} }
function default_profile($author) {
function default_profile($author)
{
$tmp = array(); $tmp = array();
$profile = new stdClass; $profile = new stdClass;
@ -445,8 +447,8 @@ function default_profile($author) {
} }
// Return static page. // Return static page.
function get_static_post($static) {
function get_static_post($static)
{
$posts = get_static_pages(); $posts = get_static_pages();
$tmp = array(); $tmp = array();
@ -489,8 +491,8 @@ function get_static_post($static) {
} }
// Return static page. // Return static page.
function get_static_sub_post($static, $sub_static) {
function get_static_sub_post($static, $sub_static)
{
$posts = get_static_sub_pages($static); $posts = get_static_sub_pages($static);
$tmp = array(); $tmp = array();
@ -531,8 +533,8 @@ function get_static_sub_post($static, $sub_static) {
} }
// Return search page. // Return search page.
function get_keyword($keyword, $page, $perpage) {
function get_keyword($keyword, $page, $perpage)
{
$posts = get_post_sorted(); $posts = get_post_sorted();
$tmp = array(); $tmp = array();
@ -559,7 +561,8 @@ function get_keyword($keyword, $page, $perpage) {
} }
// Get related posts base on post tag. // Get related posts base on post tag.
function get_related($tag) {
function get_related($tag)
{
$perpage = config('related.count'); $perpage = config('related.count');
$posts = get_tag(strip_tags($tag), 1, $perpage + 1, true); $posts = get_tag(strip_tags($tag), 1, $perpage + 1, true);
$tmp = array(); $tmp = array();
@ -588,8 +591,8 @@ function get_related($tag) {
} }
// Return post count. Matching $var and $str provided. // Return post count. Matching $var and $str provided.
function get_count($var, $str) {
function get_count($var, $str)
{
$posts = get_post_sorted(); $posts = get_post_sorted();
$tmp = array(); $tmp = array();
@ -605,8 +608,8 @@ function get_count($var, $str) {
} }
// Return seaarch result count // Return seaarch result count
function keyword_count($keyword) {
function keyword_count($keyword)
{
$posts = get_post_sorted(); $posts = get_post_sorted();
$tmp = array(); $tmp = array();
@ -628,15 +631,14 @@ function keyword_count($keyword) {
return count($tmp); return count($tmp);
} }
function recent() {
function recent()
{
$str = '<ul>'; $str = '<ul>';
$posts = get_posts(null, 1, 5); $posts = get_posts(null, 1, 5);
foreach($posts as $post)
{
foreach($posts as $post) {
$str .= '<li><a href="' . $post->url . '">' . $post->title . '</a></li>'; $str .= '<li><a href="' . $post->url . '">' . $post->title . '</a></li>';
} }
if(empty($posts))
{
if(empty($posts)) {
$str .= '<li>No Posts Found</li>'; $str .= '<li>No Posts Found</li>';
} }
$str .= '</ul>'; $str .= '</ul>';
@ -644,8 +646,8 @@ function recent() {
} }
// Return an archive list, categorized by year and month. // Return an archive list, categorized by year and month.
function archive_list() {
function archive_list()
{
$posts = get_post_unsorted(); $posts = get_post_unsorted();
$by_year = array(); $by_year = array();
$col = array(); $col = array();
@ -715,8 +717,8 @@ EOF;
} }
// Return tag cloud. // Return tag cloud.
function tag_cloud() {
function tag_cloud()
{
$posts = get_post_unsorted(); $posts = get_post_unsorted();
$tags = array(); $tags = array();
@ -747,7 +749,8 @@ function tag_cloud() {
// Helper function to determine whether // Helper function to determine whether
// to show the previous buttons // to show the previous buttons
function has_prev($prev) {
function has_prev($prev)
{
if (!empty($prev)) { if (!empty($prev)) {
return array( return array(
'url' => $prev->url, 'url' => $prev->url,
@ -758,7 +761,8 @@ function has_prev($prev) {
// Helper function to determine whether // Helper function to determine whether
// to show the next buttons // to show the next buttons
function has_next($next) {
function has_next($next)
{
if (!empty($next)) { if (!empty($next)) {
return array( return array(
'url' => $next->url, 'url' => $next->url,
@ -769,7 +773,8 @@ function has_next($next) {
// Helper function to determine whether // Helper function to determine whether
// to show the pagination buttons // to show the pagination buttons
function has_pagination($total, $perpage, $page = 1) {
function has_pagination($total, $perpage, $page = 1)
{
if (!$total) { if (!$total) {
$total = count(get_post_unsorted()); $total = count(get_post_unsorted());
} }
@ -780,8 +785,8 @@ function has_pagination($total, $perpage, $page = 1) {
} }
// Get the meta description // Get the meta description
function get_description($string) {
function get_description($string)
{
$string = remove_accent($string); $string = remove_accent($string);
$string = preg_replace('/[^A-Za-z0-9 !@#$%^&*(),.-]/u', ' ', strip_tags($string)); $string = preg_replace('/[^A-Za-z0-9 !@#$%^&*(),.-]/u', ' ', strip_tags($string));
$string = ltrim($string); $string = ltrim($string);
@ -790,8 +795,8 @@ function get_description($string) {
} }
// Get the teaser // Get the teaser
function get_teaser($text, $url) {
function get_teaser($text, $url)
{
$teaserType = config('teaser.type'); $teaserType = config('teaser.type');
if (strlen(strip_tags($text)) < config('teaser.char') || $teaserType === 'full') { if (strlen(strip_tags($text)) < config('teaser.char') || $teaserType === 'full') {
@ -806,8 +811,8 @@ function get_teaser($text, $url) {
} }
// Get thumbnail from image and Youtube. // Get thumbnail from image and Youtube.
function get_thumbnail($text) {
function get_thumbnail($text)
{
if (config('img.thumbnail') == 'true') { if (config('img.thumbnail') == 'true') {
$teaserType = config('teaser.type'); $teaserType = config('teaser.type');
@ -838,13 +843,14 @@ function get_thumbnail($text) {
} }
} }
} else { } else {
} }
} }
} }
// Return edit tab on post // Return edit tab on post
function tab($p) {
function tab($p)
{
$user = $_SESSION[config("site.url")]['user']; $user = $_SESSION[config("site.url")]['user'];
$role = user('role', $user); $role = user('role', $user);
if (isset($p->author)) { if (isset($p->author)) {
@ -857,7 +863,8 @@ function tab($p) {
} }
// Use base64 encode image to speed up page load time. // Use base64 encode image to speed up page load time.
function base64_encode_image($filename = string, $filetype = string) {
function base64_encode_image($filename = string, $filetype = string)
{
if ($filename) { if ($filename) {
$imgbinary = fread(fopen($filename, "r"), filesize($filename)); $imgbinary = fread(fopen($filename, "r"), filesize($filename));
return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary); return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
@ -865,8 +872,8 @@ function base64_encode_image($filename = string, $filetype = string) {
} }
// Social links // Social links
function social($imgDir = null) {
function social($imgDir = null)
{
$twitter = config('social.twitter'); $twitter = config('social.twitter');
$facebook = config('social.facebook'); $facebook = config('social.facebook');
$google = config('social.google'); $google = config('social.google');
@ -897,8 +904,8 @@ function social($imgDir = null) {
} }
// Copyright // Copyright
function copyright() {
function copyright()
{
$blogcp = blog_copyright(); $blogcp = blog_copyright();
$credit = 'Proudly powered by <a href="http://www.htmly.com" target="_blank">HTMLy</a>.'; $credit = 'Proudly powered by <a href="http://www.htmly.com" target="_blank">HTMLy</a>.';
@ -910,7 +917,8 @@ function copyright() {
} }
// Disqus on post. // Disqus on post.
function disqus($title = null, $url = null) {
function disqus($title = null, $url = null)
{
$comment = config('comment.system'); $comment = config('comment.system');
$disqus = config('disqus.shortname'); $disqus = config('disqus.shortname');
$script = <<<EOF $script = <<<EOF
@ -918,7 +926,7 @@ function disqus($title = null, $url = null) {
var disqus_shortname = '{$disqus}'; var disqus_shortname = '{$disqus}';
var disqus_title = '{$title}'; var disqus_title = '{$title}';
var disqus_url = '{$url}'; var disqus_url = '{$url}';
(function() {
(function () {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
@ -931,13 +939,14 @@ EOF;
} }
// Disqus comment count on teaser // Disqus comment count on teaser
function disqus_count() {
function disqus_count()
{
$comment = config('comment.system'); $comment = config('comment.system');
$disqus = config('disqus.shortname'); $disqus = config('disqus.shortname');
$script = <<<EOF $script = <<<EOF
<script type="text/javascript"> <script type="text/javascript">
var disqus_shortname = '{$disqus}'; var disqus_shortname = '{$disqus}';
(function() {
(function () {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/count.js'; dsq.src = '//' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
@ -950,7 +959,8 @@ EOF;
} }
// Disqus recent comments // Disqus recent comments
function recent_comments() {
function recent_comments()
{
$comment = config('comment.system'); $comment = config('comment.system');
$disqus = config('disqus.shortname'); $disqus = config('disqus.shortname');
$script = <<<EOF $script = <<<EOF
@ -966,12 +976,13 @@ EOF;
} }
} }
function facebook() {
function facebook()
{
$comment = config('comment.system'); $comment = config('comment.system');
$appid = config('fb.appid'); $appid = config('fb.appid');
$script = <<<EOF $script = <<<EOF
<div id="fb-root"></div> <div id="fb-root"></div>
<script>(function(d, s, id) {
<script>(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0]; var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return; if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id; js = d.createElement(s); js.id = id;
@ -987,7 +998,8 @@ EOF;
} }
// Google Publisher (Google+ page). // Google Publisher (Google+ page).
function publisher() {
function publisher()
{
$publisher = config('google.publisher'); $publisher = config('google.publisher');
if (!empty($publisher)) { if (!empty($publisher)) {
return $publisher; return $publisher;
@ -995,19 +1007,17 @@ function publisher() {
} }
// Google Analytics // Google Analytics
function analytics($analyticsDir = null) {
function analytics($analyticsDir = null)
{
$analytics = config('google.analytics.id'); $analytics = config('google.analytics.id');
if($analyticsDir === null)
{
if($analyticsDir === null) {
$analyticsDir = '//www.google-analytics.com/analytics.js'; $analyticsDir = '//www.google-analytics.com/analytics.js';
}
else
{
} else {
$analyticsDir = site_url() . 'themes/' . $analyticsDir . 'analytics.js'; $analyticsDir = site_url() . 'themes/' . $analyticsDir . 'analytics.js';
} }
$script = <<<EOF $script = <<<EOF
<script> <script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(function (i,s,o,g,r,a,m) {i['GoogleAnalyticsObject']=r;i[r]=i[r]||function () {
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','{$analyticsDir}','ga'); })(window,document,'script','{$analyticsDir}','ga');
@ -1022,7 +1032,8 @@ EOF;
} }
// Menu // Menu
function menu() {
function menu()
{
$menu = config('blog.menu'); $menu = config('blog.menu');
$req = $_SERVER['REQUEST_URI']; $req = $_SERVER['REQUEST_URI'];
@ -1081,7 +1092,8 @@ function menu() {
} }
} }
function get_title_from_file($v) {
function get_title_from_file($v)
{
// Get the contents and convert it to HTML // Get the contents and convert it to HTML
$content = MarkdownExtra::defaultTransform(file_get_contents($v)); $content = MarkdownExtra::defaultTransform(file_get_contents($v));
@ -1178,7 +1190,8 @@ function get_menu() {//aktive Link for Sub Pages ::TODO
} }
// Search form // Search form
function search() {
function search()
{
echo <<<EOF echo <<<EOF
<form id="search-form" method="get"> <form id="search-form" method="get">
<input type="text" class="search-input" name="search" value="Search" onfocus="if (this.value == 'Search') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search';}"> <input type="text" class="search-input" name="search" value="Search" onfocus="if (this.value == 'Search') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search';}">
@ -1192,15 +1205,16 @@ EOF;
} }
// The not found error // The not found error
function not_found() {
function not_found()
{
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
render('404', null, false); render('404', null, false);
die(); die();
} }
// Turn an array of posts into an RSS feed // Turn an array of posts into an RSS feed
function generate_rss($posts) {
function generate_rss($posts)
{
$feed = new Feed(); $feed = new Feed();
$channel = new Channel(); $channel = new Channel();
$rssLength = config('rss.char'); $rssLength = config('rss.char');
@ -1245,8 +1259,8 @@ function generate_rss($posts) {
} }
// Return post, archive url for sitemap // Return post, archive url for sitemap
function get_path() {
function get_path()
{
$posts = get_post_sorted(); $posts = get_post_sorted();
$tmp = array(); $tmp = array();
@ -1296,8 +1310,8 @@ function get_path() {
} }
// Return static page path for sitemap // Return static page path for sitemap
function get_static_path() {
function get_static_path()
{
$posts = get_static_pages(); $posts = get_static_pages();
$tmp = array(); $tmp = array();
@ -1323,8 +1337,8 @@ function get_static_path() {
} }
// Generate sitemap.xml. // Generate sitemap.xml.
function generate_sitemap($str) {
function generate_sitemap($str)
{
header('X-Robots-Tag: noindex'); header('X-Robots-Tag: noindex');
echo '<?xml version="1.0" encoding="UTF-8"?>'; echo '<?xml version="1.0" encoding="UTF-8"?>';
@ -1457,8 +1471,8 @@ function generate_sitemap($str) {
} }
// Function to generate OPML file // Function to generate OPML file
function generate_opml() {
function generate_opml()
{
$opml_data = array( $opml_data = array(
'head' => array( 'head' => array(
'title' => blog_title() . ' OPML File', 'title' => blog_title() . ' OPML File',
@ -1484,13 +1498,14 @@ function generate_opml() {
} }
// Turn an array of posts into a JSON // Turn an array of posts into a JSON
function generate_json($posts) {
function generate_json($posts)
{
return json_encode($posts); return json_encode($posts);
} }
// Create Zip files // Create Zip files
function Zip($source, $destination, $include_dir = false) {
function Zip($source, $destination, $include_dir = false)
{
if (!extension_loaded('zip') || !file_exists($source)) { if (!extension_loaded('zip') || !file_exists($source)) {
return false; return false;
} }
@ -1518,11 +1533,11 @@ function Zip($source, $destination, $include_dir = false) {
if (is_dir($file) === true) { if (is_dir($file) === true) {
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
} else if (is_file($file) === true) {
} elseif (is_file($file) === true) {
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
} }
} }
} else if (is_file($source) === true) {
} elseif (is_file($source) === true) {
$zip->addFromString(basename($source), file_get_contents($source)); $zip->addFromString(basename($source), file_get_contents($source));
} }
@ -1530,7 +1545,8 @@ function Zip($source, $destination, $include_dir = false) {
} }
// TRUE if the current page is the front page. // TRUE if the current page is the front page.
function is_front() {
function is_front()
{
$req = $_SERVER['REQUEST_URI']; $req = $_SERVER['REQUEST_URI'];
if ($req == site_path() . '/' || strpos($req, site_path() . '/?page') !== false) { if ($req == site_path() . '/' || strpos($req, site_path() . '/?page') !== false) {
return true; return true;
@ -1540,7 +1556,8 @@ function is_front() {
} }
// TRUE if the current page is an index page like frontpage, tag index, archive index and search index. // TRUE if the current page is an index page like frontpage, tag index, archive index and search index.
function is_index() {
function is_index()
{
$req = $_SERVER['REQUEST_URI']; $req = $_SERVER['REQUEST_URI'];
if (strpos($req, '/archive/') !== false || strpos($req, '/tag/') !== false || strpos($req, '/search/') !== false || $req == site_path() . '/' || strpos($req, site_path() . '/?page') !== false) { if (strpos($req, '/archive/') !== false || strpos($req, '/tag/') !== false || strpos($req, '/search/') !== false || $req == site_path() . '/' || strpos($req, site_path() . '/?page') !== false) {
return true; return true;
@ -1550,33 +1567,39 @@ function is_index() {
} }
// Return blog title // Return blog title
function blog_title() {
function blog_title()
{
return config('blog.title'); return config('blog.title');
} }
// Return blog tagline // Return blog tagline
function blog_tagline() {
function blog_tagline()
{
return config('blog.tagline'); return config('blog.tagline');
} }
// Return blog description // Return blog description
function blog_description() {
function blog_description()
{
return config('blog.description'); return config('blog.description');
} }
// Return blog copyright // Return blog copyright
function blog_copyright() {
function blog_copyright()
{
return config('blog.copyright'); return config('blog.copyright');
} }
// Return author info // Return author info
function authorinfo($title = null, $body = null) {
function authorinfo($title = null, $body = null)
{
if (config('author.info') == 'true') { if (config('author.info') == 'true') {
return '<div class="author-info"><h4>by <strong>' . $title . '</strong></h4>' . $body . '</div>'; return '<div class="author-info"><h4>by <strong>' . $title . '</strong></h4>' . $body . '</div>';
} }
} }
function head_contents($title, $description, $canonical) {
function head_contents($title, $description, $canonical)
{
$styleImage = config('lightbox'); $styleImage = config('lightbox');
$jq = config('jquery'); $jq = config('jquery');
$output = ''; $output = '';
@ -1597,8 +1620,7 @@ function head_contents($title, $description, $canonical) {
$lightbox = '<script src="' . site_url() . 'system/plugins/lightbox/js/lightbox-2.6.min.js"></script>'; $lightbox = '<script src="' . site_url() . 'system/plugins/lightbox/js/lightbox-2.6.min.js"></script>';
$corejs = '<script src="' . site_url() . 'system/resources/htmly.js"></script>'; $corejs = '<script src="' . site_url() . 'system/resources/htmly.js"></script>';
$webmasterTools = ''; $webmasterTools = '';
if(!empty($wmt_id))
{
if(!empty($wmt_id)) {
$webmasterTools = '<meta name="google-site-verification" content="' . $wmt_id . '" />'; $webmasterTools = '<meta name="google-site-verification" content="' . $wmt_id . '" />';
} }
@ -1616,7 +1638,8 @@ function head_contents($title, $description, $canonical) {
} }
// Return toolbar // Return toolbar
function toolbar() {
function toolbar()
{
$user = $_SESSION[config("site.url")]['user']; $user = $_SESSION[config("site.url")]['user'];
$role = user('role', $user); $role = user('role', $user);
$base = site_url(); $base = site_url();
@ -1651,9 +1674,9 @@ EOF;
echo '</ul></div>'; echo '</ul></div>';
} }
// File cache
function file_cache($request) {
// File cache
function file_cache($request)
{
if(config('cache.off')) return; if(config('cache.off')) return;
$c = str_replace('/', '#', str_replace('?', '~', $request)); $c = str_replace('/', '#', str_replace('?', '~', $request));
@ -1666,25 +1689,29 @@ function file_cache($request) {
} }
} }
function generate_csrf_token() {
function generate_csrf_token()
{
$_SESSION[config("site.url")]['csrf_token'] = sha1(microtime(true) . mt_rand(10000, 90000)); $_SESSION[config("site.url")]['csrf_token'] = sha1(microtime(true) . mt_rand(10000, 90000));
} }
function get_csrf() {
function get_csrf()
{
if (!isset($_SESSION[config("site.url")]['csrf_token']) || empty($_SESSION[config("site.url")]['csrf_token'])) { if (!isset($_SESSION[config("site.url")]['csrf_token']) || empty($_SESSION[config("site.url")]['csrf_token'])) {
generate_csrf_token(); generate_csrf_token();
} }
return $_SESSION[config("site.url")]['csrf_token']; return $_SESSION[config("site.url")]['csrf_token'];
} }
function is_csrf_proper($csrf_token) {
function is_csrf_proper($csrf_token)
{
if ($csrf_token == get_csrf()) { if ($csrf_token == get_csrf()) {
return true; return true;
} }
return false; return false;
} }
function add_view($page) {
function add_view($page)
{
$filename = "cache/count.json"; $filename = "cache/count.json";
$views = array(); $views = array();
if (file_exists($filename)) { if (file_exists($filename)) {
@ -1698,7 +1725,8 @@ function add_view($page) {
file_put_contents($filename, json_encode($views)); file_put_contents($filename, json_encode($views));
} }
function get_views($page) {
function get_views($page)
{
static $_views = array(); static $_views = array();
if (empty($_views)) { if (empty($_views)) {
@ -1713,14 +1741,14 @@ function get_views($page) {
return -1; return -1;
} }
function get_content_tag($tag, $string, $alt = null) {
function get_content_tag($tag, $string, $alt = null)
{
$reg = '/\<!--' . $tag . '(.+)' . $tag . '--\>/'; $reg = '/\<!--' . $tag . '(.+)' . $tag . '--\>/';
$ary = array(); $ary = array();
if (preg_match($reg, $string, $ary)) { if (preg_match($reg, $string, $ary)) {
if (isset($ary[1])) { if (isset($ary[1])) {
$result = trim($ary[1]); $result = trim($ary[1]);
if(!empty($result))
{
if(!empty($result)) {
return $result; return $result;
} }
} }
@ -1728,6 +1756,7 @@ function get_content_tag($tag, $string, $alt = null) {
return $alt; return $alt;
} }
function remove_html_comments($content) {
function remove_html_comments($content)
{
return trim(preg_replace('/(\s|)<!--(.*)-->(\s|)/', '', $content)); return trim(preg_replace('/(\s|)<!--(.*)-->(\s|)/', '', $content));
} }

+ 6
- 8
system/includes/opml.php View File

@ -1,22 +1,22 @@
<?php <?php
class OPML
class opml
{ {
private $data; private $data;
private $writer; private $writer;
public function __construct($data) public function __construct($data)
{ {
$this->data = $data; $this->data = $data;
$this->writer = new XMLWriter(); $this->writer = new XMLWriter();
$this->writer->openMemory(); $this->writer->openMemory();
} }
public function render() public function render()
{ {
$this->writer->startDocument('1.0', 'UTF-8'); $this->writer->startDocument('1.0', 'UTF-8');
$this->writer->startElement('opml'); $this->writer->startElement('opml');
$this->writer->writeAttribute('version', '2.0'); $this->writer->writeAttribute('version', '2.0');
// Header // Header
$this->writer->startElement('head'); $this->writer->startElement('head');
foreach ($this->data['head'] as $key => $value) { foreach ($this->data['head'] as $key => $value) {
@ -24,7 +24,7 @@ class OPML
} }
$this->writer->writeElement('dateModified', date("D, d M Y H:i:s T")); $this->writer->writeElement('dateModified', date("D, d M Y H:i:s T"));
$this->writer->endElement(); $this->writer->endElement();
// Body // Body
$this->writer->startElement('body'); $this->writer->startElement('body');
foreach ($this->data['body'] as $outlines) { foreach ($this->data['body'] as $outlines) {
@ -35,11 +35,9 @@ class OPML
$this->writer->endElement(); $this->writer->endElement();
} }
$this->writer->endElement(); $this->writer->endElement();
$this->writer->endElement(); $this->writer->endElement();
$this->writer->endDocument(); $this->writer->endDocument();
return $this->writer->outputMemory(); return $this->writer->outputMemory();
} }
} }
?>

+ 4
- 5
system/includes/session.php View File

@ -2,13 +2,12 @@
session_start(); session_start();
function login() {
function login()
{
if(isset($_SESSION[config("site.url")]['user']) && !empty($_SESSION[config("site.url")]['user'])) { if(isset($_SESSION[config("site.url")]['user']) && !empty($_SESSION[config("site.url")]['user'])) {
return true; return true;
}
else {
} else {
return false; return false;
} }
}
}

+ 32
- 19
system/includes/updater.php View File

@ -1,16 +1,18 @@
<?php <?php
class CacheOneFile {
class CacheOneFile
{
protected $fileName = ""; protected $fileName = "";
protected $holdTime = 43200; //12h protected $holdTime = 43200; //12h
public function __construct($fileName, $holdTime = 43200) {
public function __construct($fileName, $holdTime = 43200)
{
$this->fileName = $fileName; $this->fileName = $fileName;
$this->holdTime = $holdTime; $this->holdTime = $holdTime;
} }
public function is() {
public function is()
{
if (!file_exists($this->fileName)) if (!file_exists($this->fileName))
return false; return false;
if (filemtime($this->fileName) < ( time() - $this->holdTime )) { if (filemtime($this->fileName) < ( time() - $this->holdTime )) {
@ -20,25 +22,28 @@ class CacheOneFile {
return true; return true;
} }
public function get() {
public function get()
{
return file_get_contents($this->fileName); return file_get_contents($this->fileName);
} }
public function set($content) {
public function set($content)
{
file_put_contents($this->fileName, $content); file_put_contents($this->fileName, $content);
} }
} }
class Updater {
class Updater
{
protected $cachedInfo = "cache/downloadInfo.json"; protected $cachedInfo = "cache/downloadInfo.json";
protected $versionFile = "cache/installedVersion.json"; protected $versionFile = "cache/installedVersion.json";
protected $zipFile = "cache/tmpZipFile.zip"; protected $zipFile = "cache/tmpZipFile.zip";
protected $infos = array(); protected $infos = array();
protected $context = null; protected $context = null;
protected function genCaFile(){
protected function genCaFile()
{
if(file_exists("cache/ca_bundle.crt")) if(file_exists("cache/ca_bundle.crt"))
return 1; return 1;
//https://github.com/bagder/ca-bundle //https://github.com/bagder/ca-bundle
@ -3938,13 +3943,14 @@ kI26oQ==
-----END CERTIFICATE-----"); -----END CERTIFICATE-----");
} }
public function __construct() {
public function __construct()
{
if (!file_exists("cache/")) { if (!file_exists("cache/")) {
mkdir("cache/"); mkdir("cache/");
} }
$this->genCaFile(); $this->genCaFile();
$this->cachedInfo = new CacheOneFile($this->cachedInfo); $this->cachedInfo = new CacheOneFile($this->cachedInfo);
$this->context = stream_context_create( $this->context = stream_context_create(
array( array(
'http' => array( 'http' => array(
@ -3959,7 +3965,8 @@ kI26oQ==
$this->infos = $this->getInfos(); $this->infos = $this->getInfos();
} }
protected function getInfos() {
protected function getInfos()
{
$path = "https://api.github.com/repos/danpros/htmly/releases"; $path = "https://api.github.com/repos/danpros/htmly/releases";
if ($this->cachedInfo->is()) { if ($this->cachedInfo->is()) {
$fileContent = $this->cachedInfo->get(); $fileContent = $this->cachedInfo->get();
@ -3980,7 +3987,8 @@ kI26oQ==
return json_decode($fileContent, true); return json_decode($fileContent, true);
} }
public function updateAble() {
public function updateAble()
{
if (!in_array('https', stream_get_wrappers())) if (!in_array('https', stream_get_wrappers()))
return false; return false;
if (empty($this->infos)) if (empty($this->infos))
@ -3998,7 +4006,8 @@ kI26oQ==
return true; return true;
} }
public function update() {
public function update()
{
if ($this->updateAble()) { if ($this->updateAble()) {
if ($this->download("https://github.com/danpros/htmly/archive/" . $this->infos[0]['tag_name'] . ".zip")) { if ($this->download("https://github.com/danpros/htmly/archive/" . $this->infos[0]['tag_name'] . ".zip")) {
if ($this->unZip()) { if ($this->unZip()) {
@ -4014,7 +4023,8 @@ kI26oQ==
return false; return false;
} }
protected function download($url) {
protected function download($url)
{
$file = @fopen($url, 'r', false , $this->context); $file = @fopen($url, 'r', false , $this->context);
if ($file == false) if ($file == false)
return false; return false;
@ -4022,7 +4032,8 @@ kI26oQ==
return true; return true;
} }
protected function unZip() {
protected function unZip()
{
$path = dirname($_SERVER['SCRIPT_FILENAME']) . "/" . $this->zipFile; $path = dirname($_SERVER['SCRIPT_FILENAME']) . "/" . $this->zipFile;
$zip = new ZipArchive; $zip = new ZipArchive;
@ -4047,7 +4058,8 @@ kI26oQ==
} }
} }
public function printOne() {
public function printOne()
{
$releases = $this->infos; $releases = $this->infos;
$string = "<h3>Updated to<h3>"; $string = "<h3>Updated to<h3>";
$string .= "<h2>[" . $releases[0]['tag_name'] . "] " . $releases[0]['name'] . "</h2>\n"; $string .= "<h2>[" . $releases[0]['tag_name'] . "] " . $releases[0]['name'] . "</h2>\n";
@ -4055,7 +4067,8 @@ kI26oQ==
return $string; return $string;
} }
public function getName() {
public function getName()
{
return $this->infos[0]['tag_name']; return $this->infos[0]['tag_name'];
} }


+ 1
- 1
system/resources/htmly.js View File

@ -7,4 +7,4 @@
}); });
}); });
})(jQuery);
})(jQuery);

Loading…
Cancel
Save