date == $b->date ? 0 : ($a->date < $b->date) ? 1 : -1; } // Rebuilt cache index function rebuilt_cache($type) { $dir = 'cache/index'; $posts_cache_sorted = array(); $posts_cache_unsorted = array(); $page_cache = array(); $author_cache = array(); if (is_dir($dir) === false) { mkdir($dir, 0775, true); } if ($type === 'posts') { $tmpu = array(); $tmpu = glob('content/*/blog/*/*/*.md', GLOB_NOSORT); if (is_array($tmpu)) { foreach ($tmpu as $fileu) { if(strpos($fileu, 'draft') === false) { $posts_cache_unsorted[] = $fileu; } } } $string = serialize($posts_cache_unsorted); file_put_contents('cache/index/index-unsorted.txt', print_r($string, true)); $tmp = array(); $tmp = glob('content/*/blog/*/*/*.md', GLOB_NOSORT); if (is_array($tmp)) { foreach ($tmp as $file) { if(strpos($file, 'draft') === false) { $posts_cache_sorted[] = pathinfo($file); } } } usort($posts_cache_sorted, "sortfile"); $string = serialize($posts_cache_sorted); file_put_contents('cache/index/index-sorted.txt', print_r($string, true)); } elseif ($type === 'page') { $page_cache = glob('content/static/*.md', GLOB_NOSORT); $string = serialize($page_cache); file_put_contents('cache/index/index-page.txt', print_r($string, true)); } elseif ($type === 'subpage') { $page_cache = glob('content/static/*/*.md', GLOB_NOSORT); $string = serialize($page_cache); file_put_contents('cache/index/index-sub-page.txt', print_r($string, true)); } elseif ($type === 'author') { $author_cache = glob('content/*/author.md', GLOB_NOSORT); $string = serialize($author_cache); file_put_contents('cache/index/index-author.txt', print_r($string, true)); } elseif ($type === 'all') { rebuilt_cache('posts'); rebuilt_cache('page'); rebuilt_cache('subpage'); rebuilt_cache('author'); } foreach (glob('cache/widget/*.cache', GLOB_NOSORT) as $file) { unlink($file); } } // Return blog posts. function get_posts($posts, $page = 1, $perpage = 0) { if (empty($posts)) { $posts = get_post_sorted(); } $tmp = array(); // Extract a specific page with results $posts = array_slice($posts, ($page - 1) * $perpage, $perpage); foreach ($posts as $index => $v) { $post = new stdClass; $filepath = $v['dirname'] . '/' . $v['basename']; // Extract the date $arr = explode('_', $filepath); // Replaced string $replaced = substr($arr[0], 0, strrpos($arr[0], '/')) . '/'; // Author string $str = explode('/', $replaced); $author = $str[count($str) - 5]; if($str[count($str) - 3] == 'uncategorized') { $category = default_category(); $post->category = '' . $category->title . ''; } else { $category = get_category_info($str[count($str) - 3]); $post->category = '' . $category[0]->title . ''; } $type = $str[count($str) - 2]; // The post author + author url $post->author = $author; $post->authorUrl = site_url() . 'author/' . $author; $post->type = $type; $dt = str_replace($replaced, '', $arr[0]); $t = str_replace('-', '', $dt); $time = new DateTime($t); $timestamp = $time->format("Y-m-d H:i:s"); // The post date $post->date = strtotime($timestamp); // The archive per day $post->archive = site_url() . 'archive/' . date('Y-m', $post->date); if (config('permalink.type') == 'post') { $post->url = site_url() . 'post/' . str_replace('.md', '', $arr[2]); } else { $post->url = site_url() . date('Y/m', $post->date) . '/' . str_replace('.md', '', $arr[2]); } $post->file = $filepath; $content = file_get_contents($filepath); // Extract the title and body $post->title = get_content_tag('t', $content, 'Untitled: ' . date('l jS \of F Y', $post->date)); $post->image = get_content_tag('image', $content); $post->video = get_youtube_id(get_content_tag('video', $content)); $post->link = get_content_tag('link', $content); $post->quote = get_content_tag('quote', $content); $post->audio = get_content_tag('audio', $content); $tag = array(); $url = array(); $bc = array(); $rel = array(); $tagt = get_content_tag('tag', $content); $t = explode(',', rtrim($arr[1], ',')); if(!empty($tagt)) { $tl = explode(',', rtrim($tagt, ',')); $tCom = array_combine($t, $tl); foreach ($tCom as $key => $val) { if(!empty($val)) { $tag[] = array($val, site_url() . 'tag/' . strtolower($key)); } else { $tag[] = array($key, site_url() . 'tag/' . strtolower($key)); } } } else { foreach ($t as $tt) { $tag[] = array($tt, site_url() . 'tag/' . strtolower($tt)); } } foreach ($tag as $a) { $url[] = ''; $bc[] = '' . $a[0] . ''; } $post->tag = implode(' ', $url); $post->tagb = implode(' ยป ', $bc); $post->related = rtrim($arr[1], ','); // Get the contents and convert it to HTML $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); if (config("views.counter")) { $post->views = get_views($post->file); } $post->description = get_content_tag("d", $content, get_description($post->body)); $tmp[] = $post; } return $tmp; } // Find post by year, month and name, previous, and next. function find_post($year, $month, $name) { $posts = get_post_sorted(); foreach ($posts as $index => $v) { $arr = explode('_', $v['basename']); if (strpos($arr[0], "$year-$month") !== false && strtolower($arr[2]) === strtolower($name . '.md') || strtolower($arr[2]) === strtolower($name . '.md')) { // Use the get_posts method to return // a properly parsed object $ar = get_posts($posts, $index + 1, 1); $nx = get_posts($posts, $index, 1); $pr = get_posts($posts, $index + 2, 1); if ($index == 0) { if (isset($pr[0])) { return array( 'current' => $ar[0], 'prev' => $pr[0] ); } else { return array( 'current' => $ar[0], 'prev' => null ); } } elseif (count($posts) == $index + 1) { return array( 'current' => $ar[0], 'next' => $nx[0] ); } else { return array( 'current' => $ar[0], 'next' => $nx[0], 'prev' => $pr[0] ); } } } } // Find draft. function find_draft($year, $month, $name) { $posts = get_draft_posts(); foreach ($posts as $index => $v) { $arr = explode('_', $v['basename']); if (strpos($arr[0], "$year-$month") !== false && strtolower($arr[2]) === strtolower($name . '.md') || strtolower($arr[2]) === strtolower($name . '.md')) { // Use the get_posts method to return // a properly parsed object $ar = get_posts($posts, $index + 1, 1); $nx = get_posts($posts, $index, 1); $pr = get_posts($posts, $index + 2, 1); if ($index == 0) { if (isset($pr[0])) { return array( 'current' => $ar[0], 'prev' => $pr[0] ); } else { return array( 'current' => $ar[0], 'prev' => null ); } } elseif (count($posts) == $index + 1) { return array( 'current' => $ar[0], 'next' => $nx[0] ); } else { return array( 'current' => $ar[0], 'next' => $nx[0], 'prev' => $pr[0] ); } } } } // Return category page. function get_category($category, $page, $perpage) { $posts = get_post_sorted(); $tmp = array(); foreach ($posts as $index => $v) { $filepath = $v['dirname'] . '/' . $v['basename']; // Extract the date $arr = explode('_', $filepath); // Replaced string $replaced = substr($arr[0], 0, strrpos($arr[0], '/')) . '/'; // Author string $str = explode('/', $replaced); $cat = $str[count($str) - 3]; if (strtolower($category) === strtolower($cat)) { $tmp[] = $v; } } if (empty($tmp)) { not_found(); } $tmp = array_unique($tmp, SORT_REGULAR); return $tmp = get_posts($tmp, $page, $perpage); } // Return category data. function get_category_info($category) { $posts = get_category_files(); $tmp = array(); if (!empty($posts)) { foreach ($posts as $index => $v) { if (stripos($v, $category . '.md') !== false) { $desc = new stdClass; // Replaced string $replaced = substr($v, 0, strrpos($v, '/')) . '/'; // The static page URL $url= str_replace($replaced, '', $v); $desc->url = site_url() . 'category/' . str_replace('.md', '', $url); $desc->md = str_replace('.md', '', $url); $desc->file = $v; // Get the contents and convert it to HTML $content = file_get_contents($v); // Extract the title and body $desc->title = get_content_tag('t', $content, $category); $desc->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); $desc->description = get_content_tag("d", $content, get_description($desc->body)); $tmp[] = $desc; } } } if (strtolower($category) == 'uncategorized') { return default_category(); } return $tmp; } // Return default profile function default_category() { $tmp = array(); $desc = new stdClass; $desc->title = 'Uncategorized'; $desc->url = site_url() . 'category/uncategorized'; $desc->body = "

Topics that don't need a category, or don't fit into any other existing category.

"; $desc->description = 'Uncategorized Posts'; return $tmp[] = $desc; } // Return tag page. function get_tag($tag, $page, $perpage, $random) { $posts = get_post_sorted(); if ($random === true) { shuffle($posts); } $tmp = array(); foreach ($posts as $index => $v) { $str = explode('_', $v['basename']); $mtag = explode(',', rtrim($str[1], ',')); $etag = explode(',', $tag); foreach ($mtag as $t) { foreach ($etag as $e) { $e = trim($e); if (strtolower($t) === strtolower($e)) { $tmp[] = $v; } } } } if (empty($tmp)) { not_found(); } $tmp = array_unique($tmp, SORT_REGULAR); return $tmp = get_posts($tmp, $page, $perpage); } // Return archive page. function get_archive($req, $page, $perpage) { $posts = get_post_sorted(); $tmp = array(); foreach ($posts as $index => $v) { $str = explode('_', $v['basename']); if (strpos($str[0], "$req") !== false) { $tmp[] = $v; } } if (empty($tmp)) { not_found(); } return $tmp = get_posts($tmp, $page, $perpage); } // Return posts list on profile. function get_profile_posts($name, $page, $perpage) { $posts = get_post_sorted(); $tmp = array(); foreach ($posts as $index => $v) { $str = explode('/', $v['dirname']); $author = $str[count($str) - 4]; if (strtolower($name) === strtolower($author)) { $tmp[] = $v; } } if (empty($tmp)) { return; } return $tmp = get_posts($tmp, $page, $perpage); } // Return draft list function get_draft($profile, $page, $perpage) { $posts = get_draft_posts(); $tmp = array(); foreach ($posts as $index => $v) { $str = explode('/', $v['dirname']); $author = $str[count($str) - 4]; if (strtolower($profile) === strtolower($author)) { $tmp[] = $v; } } if (empty($tmp)) { return; } return $tmp = get_posts($tmp, $page, $perpage); } // Return author info. function get_author($name) { $names = get_author_name(); $username = 'config/users/' . $name . '.ini'; $tmp = array(); if (!empty($names)) { foreach ($names as $index => $v) { $author = new stdClass; // Replaced string $replaced = substr($v, 0, strrpos($v, '/')) . '/'; // Author string $str = explode('/', $replaced); $profile = $str[count($str) - 2]; if ($name === $profile) { // Profile URL $url = str_replace($replaced, '', $v); $author->url = site_url() . 'author/' . $profile; // Get the contents and convert it to HTML $content = file_get_contents($v); // Extract the title and body $author->name = get_content_tag('t', $content, $author); $author->about = MarkdownExtra::defaultTransform(remove_html_comments($content)); $tmp[] = $author; } } } if (!empty($tmp) || file_exists($username)) { return $tmp; } else { not_found(); } } // Return default profile function default_profile($name) { $tmp = array(); $author = new stdClass; $author->name = $name; $author->about = '

Just another HTMLy user.

'; $author->description = 'Just another HTMLy user'; return $tmp[] = $author; } // Return static page. function get_static_post($static) { $posts = get_static_pages(); $tmp = array(); if (!empty($posts)) { foreach ($posts as $index => $v) { if (stripos($v, $static . '.md') !== false) { $post = new stdClass; // Replaced string $replaced = substr($v, 0, strrpos($v, '/')) . '/'; // The static page URL $url = str_replace($replaced, '', $v); $post->url = site_url() . str_replace('.md', '', $url); $post->file = $v; // Get the contents and convert it to HTML $content = file_get_contents($v); // Extract the title and body $post->title = get_content_tag('t', $content, $static); $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); if (config("views.counter")) { $post->views = get_views($post->file); } $post->description = get_content_tag("d", $content, get_description($post->body)); $tmp[] = $post; } } } return $tmp; } // Return static page. function get_static_sub_post($static, $sub_static) { $posts = get_static_sub_pages($static); $tmp = array(); if (!empty($posts)) { foreach ($posts as $index => $v) { if (stripos($v, $sub_static . '.md') !== false) { $post = new stdClass; // Replaced string $replaced = substr($v, 0, strrpos($v, '/')) . '/'; // The static page URL $url = str_replace($replaced, '', $v); $post->url = site_url() . $static . "/" . str_replace('.md', '', $url); $post->file = $v; // Get the contents and convert it to HTML $content = file_get_contents($v); // Extract the title and body $post->title = get_content_tag('t', $content, $sub_static); $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); $post->views = get_views($post->file); $post->description = get_content_tag("d", $content, get_description($post->body)); $tmp[] = $post; } } } return $tmp; } // Return search page. function get_keyword($keyword, $page, $perpage) { $posts = get_post_sorted(); $tmp = array(); $words = explode(' ', $keyword); foreach ($posts as $index => $v) { $arr = explode('_', $v['basename']); $filter = $arr[1] . ' ' . $arr[2]; foreach ($words as $word) { if (stripos($filter, $word) !== false) { $tmp[] = $v; } } } return ($tmp) ? get_posts($tmp, $page, $perpage) : []; } // Get related posts base on post tag. function get_related($tag, $custom = null, $count = null) { if (empty($count)) { $count = config('related.count'); if (empty($count)) { $count = 3; } } $posts = get_tag($tag, 1, $count + 1, true); $tmp = array(); $req = urldecode($_SERVER['REQUEST_URI']); foreach ($posts as $post) { $url = $post->url; if (stripos($url, $req) === false) { $tmp[] = $post; } } if (empty($custom)) { $total = count($tmp); if ($total >= 1) { $i = 1; echo ''; } else { echo ''; } } else { return $tmp; } } // Return post count. Matching $var and $str provided. function get_count($var, $str) { $posts = get_post_sorted(); $tmp = array(); foreach ($posts as $index => $v) { $arr = explode('_', $v[$str]); $url = $arr[0]; if (stripos($url, "$var") !== false) { $tmp[] = $v; } } return count($tmp); } // Return tag count. Matching $var and $str provided. function get_categorycount($var) { $posts = get_post_sorted(); $tmp = array(); foreach ($posts as $index => $v) { $filepath = $v['dirname'] . '/' . $v['basename']; // Extract the date $arr = explode('_', $filepath); // Replaced string $replaced = substr($arr[0], 0, strrpos($arr[0], '/')) . '/'; // Author string $str = explode('/', $replaced); $cat = $str[count($str) - 3]; if (stripos($cat, "$var") !== false) { $tmp[] = $v; } } return count($tmp); } // Return tag count. Matching $var and $str provided. function get_draftcount($var) { $posts = get_draft_posts(); $tmp = array(); foreach ($posts as $index => $v) { $filepath = $v['dirname'] . '/' . $v['basename']; // Extract the date $arr = explode('_', $filepath); // Replaced string $replaced = substr($arr[0], 0, strrpos($arr[0], '/')) . '/'; // Author string $str = explode('/', $replaced); $cat = $str[count($str) - 3]; if (stripos($cat, "$var") !== false) { $tmp[] = $v; } } return count($tmp); } // Return tag count. Matching $var and $str provided. function get_tagcount($var, $str) { $posts = get_post_sorted(); $tmp = array(); foreach ($posts as $index => $v) { $arr = explode('_', $v[$str]); $url = $arr[1]; if (stripos($url, "$var") !== false) { $tmp[] = $v; } } return count($tmp); } // Return search result count function keyword_count($keyword) { $posts = get_post_sorted(); $tmp = array(); $words = explode(' ', $keyword); foreach ($posts as $index => $v) { $arr = explode('_', $v['basename']); $filter = $arr[1] . ' ' . $arr[2]; foreach ($words as $word) { if (stripos($filter, $word) !== false) { $tmp[] = $v; } } } $tmp = array_unique($tmp, SORT_REGULAR); return count($tmp); } // Return recent posts lists function recent_posts($custom = null, $count = null) { if (empty($count)) { $count = config('recent.count'); if (empty($count)) { $count = 5; } } $dir = "cache/widget"; $filename = "cache/widget/recent.cache"; $tmp = array(); $posts = array(); if (is_dir($dir) === false) { mkdir($dir, 0775, true); } if (file_exists($filename)) { $posts = unserialize(file_get_contents($filename)); if (count($posts) != $count) { $posts = get_posts(null, 1, $count); $tmp = serialize($posts); file_put_contents($filename, print_r($tmp, true)); } } else { $posts = get_posts(null, 1, $count); $tmp = serialize($posts); file_put_contents($filename, print_r($tmp, true)); } if (!empty($custom)) { return $posts; } else { echo ''; } } // Return popular posts lists function popular_posts($custom = null, $count = null) { static $_views = array(); $tmp = array(); if (empty($count)) { $count = config('popular.count'); if (empty($count)) { $count = 5; } } if (config('views.counter') == 'true') { if (empty($_views)) { $filename = 'content/views.json'; if (file_exists($filename)) { $_views = json_decode(file_get_contents($filename), true); if(is_array($_views)) { arsort($_views); $i = 1; foreach ($_views as $key => $val) { if (file_exists($key)) { if (stripos($key, 'blog') !== false) { $tmp[] = pathinfo($key); if ($i++ >= $count) break; } } } $dir = "cache/widget"; $filecache = "cache/widget/popular.cache"; $ar = array(); $posts = array(); if (is_dir($dir) === false) { mkdir($dir, 0775, true); } if (file_exists($filecache)) { $posts = unserialize(file_get_contents($filecache)); if (count($posts) != $count) { $posts = get_posts($tmp, 1, $count); $ar = serialize($posts); file_put_contents($filecache, print_r($ar, true)); } } else { $posts = get_posts($tmp, 1, $count); $ar = serialize($posts); file_put_contents($filecache, print_r($ar, true)); } if (empty($custom)) { echo ''; } else { return $posts; } } else { if(empty($custom)) { echo ''; } else { return $tmp; } } } else { if (empty($custom)) { echo ''; } else { return $tmp; } } } } else { if (empty($custom)) { echo ''; } else { return $tmp; } } } // Return an archive list, categorized by year and month. function archive_list($custom = null) { $dir = "cache/widget"; $filename = "cache/widget/archive.cache"; $ar = array(); if (is_dir($dir) === false) { mkdir($dir, 0775, true); } $posts = get_post_unsorted(); $by_year = array(); $col = array(); if (!empty($posts)) { if (!file_exists($filename)) { foreach ($posts as $index => $v) { $arr = explode('_', $v); // Replaced string $str = $arr[0]; $replaced = substr($str, 0, strrpos($str, '/')) . '/'; $date = str_replace($replaced, '', $arr[0]); $data = explode('-', $date); $col[] = $data; } foreach ($col as $row) { $y = $row['0']; $m = $row['1']; $by_year[$y][] = $m; } $ar = serialize($by_year); file_put_contents($filename, print_r($ar, true)); } else { $by_year = unserialize(file_get_contents($filename)); } # Most recent year first krsort($by_year); # Iterate for display $i = 0; $len = count($by_year); if (empty($custom)) { foreach ($by_year as $year => $months) { if ($i == 0) { $class = 'expanded'; $arrow = '▼'; } else { $class = 'collapsed'; $arrow = '►'; } $i++; $by_month = array_count_values($months); # Sort the months krsort($by_month); $script = << -1){this.parentNode.className = 'collapsed';this.innerHTML = '►';} else {this.parentNode.className = 'expanded';this.innerHTML = '▼';} EOF; echo ''; } } else { return $by_year; } } } // Return tag cloud. function tag_cloud($custom = null) { $dir = "cache/widget"; $filename = "cache/widget/tags.cache"; $tg = array(); if (is_dir($dir) === false) { mkdir($dir, 0775, true); } $posts = get_post_unsorted(); $tags = array(); if (!empty($posts)) { if (!file_exists($filename)) { foreach ($posts as $index => $v) { $arr = explode('_', $v); $data = rtrim($arr[1], ','); $mtag = explode(',', $data); foreach ($mtag as $etag) { $tags[] = strtolower($etag); } } $tag_collection = array_count_values($tags); ksort($tag_collection); $tg = serialize($tag_collection); file_put_contents($filename, print_r($tg, true)); } else { $tag_collection = unserialize(file_get_contents($filename)); } if(empty($custom)) { echo ''; } else { return $tag_collection; } } else { if(empty($custom)) return; return $tags; } } // Helper function to determine whether // to show the previous buttons function has_prev($prev) { if (!empty($prev)) { return array( 'url' => $prev->url, 'title' => $prev->title ); } } // Helper function to determine whether // to show the next buttons function has_next($next) { if (!empty($next)) { return array( 'url' => $next->url, 'title' => $next->title ); } } // Helper function to determine whether // to show the pagination buttons function has_pagination($total, $perpage, $page = 1) { if (!$total) { $total = count(get_post_unsorted()); } return array( 'prev' => $page > 1, 'next' => $total > $page * $perpage ); } // Get the meta description function get_description($string, $char = null) { if(empty($char)) { $char = config('description.char'); if(empty($char)) { $char = 150; } } if (strlen(strip_tags($string)) < $char) { return safe_html(strip_tags($string)); } else { $string = safe_html(strip_tags($string)); $string = substr($string, 0, $char); $string = substr($string, 0, strrpos($string, ' ')); return $string; } } // Get the teaser function get_teaser($string, $char = null) { $teaserType = config('teaser.type'); if(empty($char)) { $char = config('teaser.char'); if(empty($char)) { $char = 200; } } if ($teaserType === 'full') { echo $string; } elseif (strlen(strip_tags($string)) < $char) { $string = preg_replace('/\s\s+/', ' ', strip_tags($string)); $string = ltrim(rtrim($string)); return $string; } else { $string = preg_replace('/\s\s+/', ' ', strip_tags($string)); $string = ltrim(rtrim($string)); $string = substr($string, 0, $char); $string = substr($string, 0, strrpos($string, ' ')); return $string; } } // Get thumbnail from image and Youtube. function get_thumbnail($text) { if (config('img.thumbnail') == 'true') { $teaserType = config('teaser.type'); if (strlen(strip_tags($text)) > config('teaser.char') && $teaserType === 'trimmed') { libxml_use_internal_errors(true); $default = config('default.thumbnail'); $dom = new DOMDocument(); $dom->loadHtml($text); $imgTags = $dom->getElementsByTagName('img'); $vidTags = $dom->getElementsByTagName('iframe'); if ($imgTags->length > 0) { $imgElement = $imgTags->item(0); $imgSource = $imgElement->getAttribute('src'); return '
'; } elseif ($vidTags->length > 0) { $vidElement = $vidTags->item(0); $vidSource = $vidElement->getAttribute('src'); $fetch = explode("embed/", $vidSource); if (isset($fetch[1])) { $vidThumb = '//img.youtube.com/vi/' . $fetch[1] . '/default.jpg'; return '
'; } } else { if (!empty($default)) { return '
'; } } } else { } } } // Return edit tab on post function tab($p) { $user = $_SESSION[config("site.url")]['user']; $role = user('role', $user); if (isset($p->author)) { if ($user === $p->author || $role === 'admin') { echo '
'; } } else { echo '
'; } } // Use base64 encode image to speed up page load time. function base64_encode_image($filename = string, $filetype = string) { if ($filename) { $imgbinary = fread(fopen($filename, "r"), filesize($filename)); return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary); } } // Social links function social($imgDir = null) { $twitter = config('social.twitter'); $facebook = config('social.facebook'); $google = config('social.google'); $tumblr = config('social.tumblr'); $rss = site_url() . 'feed/rss'; if ($imgDir === null) { $imgDir = "default/img/"; } if (!empty($twitter)) { echo 'Twitter'; } if (!empty($facebook)) { echo 'Facebook'; } if (!empty($google)) { echo 'Google+'; } if (!empty($tumblr)) { echo 'Tumblr'; } echo 'RSS Feed'; } // Copyright function copyright() { $blogcp = blog_copyright(); $credit = 'Proudly powered by HTMLy'; if (!empty($blogcp)) { return $copyright = '

' . $blogcp . '

' . $credit . '

'; } else { return $credit = '

' . $credit . '

'; } } // Disqus on post. function disqus($title = null, $url = null) { $comment = config('comment.system'); $disqus = config('disqus.shortname'); $script = << var disqus_shortname = '{$disqus}'; var disqus_title = '{$title}'; var disqus_url = '{$url}'; (function () { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })(); EOF; if (!empty($disqus) && $comment == 'disqus') { return $script; } } // Disqus comment count on teaser function disqus_count() { $comment = config('comment.system'); $disqus = config('disqus.shortname'); $script = << var disqus_shortname = '{$disqus}'; (function () { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/count.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })(); EOF; if (!empty($disqus) && $comment == 'disqus') { return $script; } } // Disqus recent comments function recent_comments() { $comment = config('comment.system'); $disqus = config('disqus.shortname'); $script = << EOF; if (!empty($disqus) && $comment == 'disqus') { return $script; } } // Facebook comments function facebook() { $comment = config('comment.system'); $appid = config('fb.appid'); $script = << EOF; if (!empty($appid) && $comment == 'facebook') { return $script; } } // Google Publisher (Google+ page). function publisher() { $publisher = config('google.publisher'); if (!empty($publisher)) { return $publisher; } } // Google Analytics function analytics($analyticsDir = null) { $analytics = config('google.analytics.id'); if ($analyticsDir === null) { $analyticsDir = '//www.google-analytics.com/analytics.js'; } else { $analyticsDir = site_url() . 'themes/' . $analyticsDir . 'analytics.js'; } $script = << (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), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','{$analyticsDir}','ga'); ga('create', '{$analytics}', 'auto'); ga('send', 'pageview'); EOF; if (!empty($analytics)) { return $script; } } // Menu function menu($custom = null) { $menu = config('blog.menu'); $req = $_SERVER['REQUEST_URI']; if (!empty($menu)) { $links = explode('|', $menu); echo ''; } else { get_menu($custom); } } // Get the title from file function get_title_from_file($v) { // Get the contents and convert it to HTML $content = MarkdownExtra::defaultTransform(file_get_contents($v)); $replaced = substr($v, 0, strrpos($v, '/')) . '/'; $base = str_replace($replaced, '', $v); // Extract the title and body return get_content_tag('t', $content, str_replace('-', ' ', str_replace('.md', '', $base))); } // Auto generate menu from static page function get_menu($custom) { $posts = get_static_pages(); $req = $_SERVER['REQUEST_URI']; if (!empty($posts)) { krsort($posts); echo ''; } else { echo ''; } } // Search form function search($text = null) { if(!empty($text)) { echo << EOF; } else { echo << EOF; } if (isset($_GET['search'])) { $search = $_GET['search']; $url = site_url() . 'search/' . remove_accent($search); header("Location: $url"); } } // The not found error function not_found() { header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found"); render('404', array( 'title' => 'This page doesn\'t exist! - ' . blog_title(), 'description' => '', 'canonical' => false, 'breadcrumb' => '' . config('breadcrumb.home') . ' » 404 Not Found', 'bodyclass' => 'error-404', )); die(); } // Turn an array of posts into an RSS feed function generate_rss($posts) { $feed = new Feed(); $channel = new Channel(); $rssLength = config('rss.char'); $channel ->title(blog_title()) ->description(blog_description()) ->url(site_url()) ->appendTo($feed); foreach ($posts as $p) { if (!empty($rssLength)) { if (strlen(strip_tags($p->body)) < config('rss.char')) { $string = preg_replace('/\s\s+/', ' ', strip_tags($p->body)); $body = $string . '...'; } else { $string = preg_replace('/\s\s+/', ' ', strip_tags($p->body)); $string = substr($string, 0, config('rss.char')); $string = substr($string, 0, strrpos($string, ' ')); $body = $string . '...'; } } else { $body = $p->body; } $item = new Item(); $cats = explode(',', str_replace(' ', '', strip_tags(remove_accent($p->category)))); foreach ($cats as $cat) { $item ->category($cat, site_url() . 'category/' . strtolower($cat)); } $item ->title($p->title) ->pubDate($p->date) ->description($body) ->url($p->url) ->appendTo($channel); } echo $feed; } // Return post, archive url for sitemap function sitemap_post_path() { $posts = get_post_sorted(); $tmp = array(); foreach ($posts as $index => $v) { $post = new stdClass; $filepath = $v['dirname'] . '/' . $v['basename']; // Extract the date $arr = explode('_', $filepath); // Replaced string $replaced = substr($arr[0], 0, strrpos($arr[0], '/')) . '/'; // Author string $str = explode('/', $replaced); $author = $str[count($str) - 3]; $post->authorUrl = site_url() . 'author/' . $author; $dt = str_replace($replaced, '', $arr[0]); $t = str_replace('-', '', $dt); $time = new DateTime($t); $timestamp = $time->format("Y-m-d H:i:s"); // The post date $post->date = strtotime($timestamp); // The archive per day $post->archiveday = site_url() . 'archive/' . date('Y-m-d', $post->date); // The archive per day $post->archivemonth = site_url() . 'archive/' . date('Y-m', $post->date); // The archive per day $post->archiveyear = site_url() . 'archive/' . date('Y', $post->date); // The post URL if (config('permalink.type') == 'post') { $post->url = site_url() . 'post/' . str_replace('.md', '', $arr[2]); } else { $post->url = site_url() . date('Y/m', $post->date) . '/' . str_replace('.md', '', $arr[2]); } $tmp[] = $post; } return $tmp; } // Return static page path for sitemap function sitemap_page_path() { $posts = get_static_pages(); $tmp = array(); if (!empty($posts)) { foreach ($posts as $index => $v) { $post = new stdClass; // Replaced string $replaced = substr($v, 0, strrpos($v, '/')) . '/'; // The static page URL $url = str_replace($replaced, '', $v); $post->url = site_url() . str_replace('.md', '', $url); $tmp[] = $post; } } return $tmp; } // Generate sitemap.xml. function generate_sitemap($str) { $default_priority = '0.5'; header('X-Robots-Tag: noindex'); echo ''; if ($str == 'index') { echo ''; if (config('sitemap.priority.base') !== 'false') { echo '' . site_url() . 'sitemap.base.xml'; } if (config('sitemap.priority.post') !== 'false') { echo '' . site_url() . 'sitemap.post.xml'; } if (config('sitemap.priority.static') !== 'false') { echo '' . site_url() . 'sitemap.static.xml'; } if (config('sitemap.priority.tag') !== 'false') { echo '' . site_url() . 'sitemap.tag.xml'; } if (config('sitemap.priority.archiveDay') !== 'false' || config('sitemap.priority.archiveMonth') !== 'false' || config('sitemap.priority.archiveYear') !== 'false') { echo '' . site_url() . 'sitemap.archive.xml'; } if (config('sitemap.priority.author') !== 'false') { echo '' . site_url() . 'sitemap.author.xml'; } echo ''; } elseif ($str == 'base') { $priority = (config('sitemap.priority.base')) ? config('sitemap.priority.base') : '1.0'; echo ''; if ($priority !== 'false') { echo '' . site_url() . '' . $priority . ''; } echo ''; } elseif ($str == 'post') { $priority = (config('sitemap.priority.post')) ? config('sitemap.priority.post') : $default_priority; $posts = []; if ($priority !== 'false') { $posts = sitemap_post_path(); } echo ''; foreach ($posts as $p) { echo '' . $p->url . '' . $priority . '' . date('Y-m-d', $p->date) . ''; } echo ''; } elseif ($str == 'static') { $priority = (config('sitemap.priority.static')) ? config('sitemap.priority.static') : $default_priority; $posts = []; if ($priority !== 'false') { $posts = sitemap_page_path(); } echo ''; foreach ($posts as $p) { echo '' . $p->url . '' . $priority . ''; } echo ''; } elseif ($str == 'tag') { $priority = (config('sitemap.priority.tag')) ? config('sitemap.priority.tag') : $default_priority; $posts = []; if ($priority !== 'false') { $posts = get_post_unsorted(); } $tags = array(); echo ''; if($posts) { foreach ($posts as $index => $v) { $arr = explode('_', $v); $data = $arr[1]; $mtag = explode(',', $data); foreach ($mtag as $etag) { $tags[] = strtolower($etag); } } foreach ($tags as $t) { $tag[] = site_url() . 'tag/' . strtolower($t); } if (isset($tag)) { $tag = array_unique($tag, SORT_REGULAR); foreach ($tag as $t) { echo '' . $t . '' . $priority . ''; } } } echo ''; } elseif ($str == 'archive') { $priorityDay = (config('sitemap.priority.archiveDay')) ? config('sitemap.priority.archiveDay') : $default_priority; $priorityMonth = (config('sitemap.priority.archiveMonth')) ? config('sitemap.priority.archiveMonth') : $default_priority; $priorityYear = (config('sitemap.priority.archiveYear')) ? config('sitemap.priority.archiveYear') : $default_priority; $posts = sitemap_post_path(); $day = array(); $month = array(); $year = array(); foreach ($posts as $p) { $day[] = $p->archiveday; $month[] = $p->archivemonth; $year[] = $p->archiveyear; } $day = array_unique($day, SORT_REGULAR); $month = array_unique($month, SORT_REGULAR); $year = array_unique($year, SORT_REGULAR); echo ''; if ($priorityDay !== 'false') { foreach ($day as $d) { echo '' . $d . '' . $priorityDay . ''; } } if ($priorityMonth !== 'false') { foreach ($month as $m) { echo '' . $m . '' . $priorityMonth . ''; } } if ($priorityYear !== 'false') { foreach ($year as $y) { echo '' . $y . '' . $priorityYear . ''; } } echo ''; } elseif ($str == 'author') { $priority = (config('sitemap.priority.author')) ? config('sitemap.priority.author') : $default_priority; $author = []; if ($priority !== 'false') { $posts = sitemap_post_path(); foreach ($posts as $p) { $author[] = $p->authorUrl; } $author = array_unique($author, SORT_REGULAR); } echo ''; if ($priority !== 'false') { foreach ($author as $a) { echo '' . $a . '' . $priority . ''; } } echo ''; } } // Function to generate OPML file function generate_opml() { $opml_data = array( 'head' => array( 'title' => blog_title() . ' OPML File', 'ownerName' => blog_title(), 'ownerId' => site_url() ), 'body' => array( array( 'text' => blog_title(), 'description' => blog_description(), 'htmlUrl' => site_url(), 'language' => 'unknown', 'title' => blog_title(), 'type' => 'rss', 'version' => 'RSS2', 'xmlUrl' => site_url() . 'feed/rss' ) ) ); $opml = new opml($opml_data); echo $opml->render(); } // Turn an array of posts into a JSON function generate_json($posts) { return json_encode($posts); } // Create Zip files function Zip($source, $destination, $include_dir = false) { if (!extension_loaded('zip') || !file_exists($source)) { return false; } if (file_exists($destination)) { unlink($destination); } $zip = new ZipArchive(); if (!$zip->open($destination, ZIPARCHIVE::CREATE)) { return false; } if (is_dir($source) === true) { $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); foreach ($files as $file) { $file = str_replace('\\', '/', $file); // Ignore "." and ".." folders if (in_array(substr($file, strrpos($file, '/') + 1), array('.', '..'))) continue; if (is_dir($file) === true) { $zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); } elseif (is_file($file) === true) { $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); } } } elseif (is_file($source) === true) { $zip->addFromString(basename($source), file_get_contents($source)); } return $zip->close(); } // TRUE if the current page is an index page like frontpage, tag index, archive index and search index. function is_index() { $req = $_SERVER['REQUEST_URI']; if (stripos($req, '/category/') !== false || stripos($req, '/archive/') !== false || stripos($req, '/tag/') !== false || stripos($req, '/search/') !== false || stripos($req, '/blog') !== false || $req == site_path() . '/' || stripos($req, site_path() . '/?page') !== false) { return true; } else { return false; } } // TRUE if the current page is the front page. function is_front($value = null) { if (!empty($value)) { return true; } else { return false; } } // TRUE if the current page is the blog page. function is_blog($value = null) { if (!empty($value)) { return true; } else { return false; } } // TRUE if the current page is tag index. function is_tag($value = null) { if (!empty($value)) { return true; } else { return false; } } // TRUE if the current page is archive index. function is_archive($value = null) { if (!empty($value)) { return true; } else { return false; } } // TRUE if the current page is search index. function is_search($value = null) { if (!empty($value)) { return true; } else { return false; } } // TRUE if the current page is category index. function is_category($value = null) { if (!empty($value)) { return true; } else { return false; } } // TRUE if the current page is profile page. function is_profile($value = null) { if (!empty($value)) { return true; } else { return false; } } // TRUE if the current page is post page. function is_post($value = null) { if (!empty($value)) { return true; } else { return false; } } // TRUE if the current page is static page page. function is_page($value = null) { if (!empty($value)) { return true; } else { return false; } } // TRUE if the current page is sub static page. function is_subpage($value = null) { if (!empty($value)) { return true; } else { return false; } } // Return blog title function blog_title() { return config('blog.title'); } // Return blog tagline function blog_tagline() { return config('blog.tagline'); } // Return blog description function blog_description() { return config('blog.description'); } // Return blog copyright function blog_copyright() { return config('blog.copyright'); } // Return author info. Deprecated function authorinfo($name = null, $about = null) { if (config('author.info') == 'true') { return '

by ' . $name . '

' . $about . '
'; } } // Output head contents function head_contents() { $output = ''; $wmt_id = config('google.wmt.id'); $favicon = ''; $charset = ''; $generator = ''; $xua = ''; $viewport = ''; $sitemap = ''; $feed = ''; $webmasterTools = ''; if (!empty($wmt_id)) { $webmasterTools = ''; } $output .= $charset . "\n" . $xua . "\n" . $viewport . "\n" . $generator . "\n" . $favicon . "\n" . $sitemap . "\n" . $feed . "\n" . $webmasterTools . "\n"; return $output; } // Return toolbar function toolbar() { $user = $_SESSION[config("site.url")]['user']; $role = user('role', $user); $base = site_url(); echo << EOF; echo '
'; } // File cache function file_cache($request) { if (config('cache.off')) return; $c = str_replace('/', '#', str_replace('?', '~', $request)); $cachefile = 'cache/page/' . $c . '.cache'; if (file_exists($cachefile)) { header('Content-type: text/html; charset=utf-8'); readfile($cachefile); die; } } // Generate csrf token function generate_csrf_token() { $_SESSION[config("site.url")]['csrf_token'] = sha1(microtime(true) . mt_rand(10000, 90000)); } // Get csrf token function get_csrf() { if (!isset($_SESSION[config("site.url")]['csrf_token']) || empty($_SESSION[config("site.url")]['csrf_token'])) { generate_csrf_token(); } return $_SESSION[config("site.url")]['csrf_token']; } // Check the csrf token function is_csrf_proper($csrf_token) { if ($csrf_token == get_csrf()) { return true; } return false; } // Add page views count function add_view($page) { $filename = "content/views.json"; $views = array(); if (file_exists($filename)) { $views = json_decode(file_get_contents($filename), true); } if (isset($views[$page])) { $views[$page]++; } else { $views[$page] = 1; } file_put_contents($filename, json_encode($views)); } // Get the page views count function get_views($page) { static $_views = array(); if (empty($_views)) { $filename = "content/views.json"; if (file_exists($filename)) { $_views = json_decode(file_get_contents($filename), true); } } if (isset($_views[$page])) { return $_views[$page]; } return -1; } // Get tag inside the markdown files function get_content_tag($tag, $string, $alt = null) { $reg = '/\(\s|)/', '', $content)); } // Google recaptcha function isCaptcha($reCaptchaResponse) { if (!config("google.reCaptcha")) { return true; } $url = "https://www.google.com/recaptcha/api/siteverify"; $options = array( "secret" => config("google.reCaptcha.private"), "response" => $reCaptchaResponse, "remoteip" => $_SERVER['REMOTE_ADDR'], ); $fileContent = @file_get_contents($url . "?" . http_build_query($options)); if ($fileContent === false) { return false; } $json = json_decode($fileContent, true); if ($json == false) { return false; } return ($json['success']); } // Get YouTube video ID function get_youtube_id($url) { if(empty($url)) { return; } preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches); return $matches[1]; } // Shorten the string function shorten($string = null, $char = null) { if(empty($char) || empty($string)) { return; } if (strlen(strip_tags($string)) < $char) { $string = preg_replace('/\s\s+/', ' ', strip_tags($string)); $string = ltrim(rtrim($string)); return $string; } else { $string = preg_replace('/\s\s+/', ' ', strip_tags($string)); $string = ltrim(rtrim($string)); $string = substr($string, 0, $char); $string = substr($string, 0, strrpos($string, ' ')); return $string; } } // save the i18n tag function save_tag_i18n($tag,$tagDisplay) { $filename = "content/tags.lang"; $tags = array(); $tmp = array(); $views = array(); $tt = explode(',', rtrim($tag, ',')); $tl = explode(',', rtrim($tagDisplay, ',')); $tags = array_combine($tt,$tl); if (file_exists($filename)) { $views = unserialize(file_get_contents($filename)); foreach ($tags as $key => $val) { if (isset($views[$key])) { $views[$key] = $val; } else { $views[$key] = $val; } } } else { $views = $tags; } $tmp = serialize($views); file_put_contents($filename, print_r($tmp, true)); } // translate tag to i18n function tag_i18n($tag) { static $tags = array(); if (empty($tags)) { $filename = "content/tags.lang"; if (file_exists($filename)) { $tags = unserialize(file_get_contents($filename)); } } if (isset($tags[$tag])) { return $tags[$tag]; } return $tag; } // return html safe string function safe_html($string) { $string = htmlspecialchars($string, ENT_QUOTES); $string = preg_replace('/\r\n|\r|\n/', ' ', $string); $string = preg_replace('/\s\s+/', ' ', $string); $string = ltrim(rtrim($string)); return $string; } // return tag safe string function safe_tag($string) { $tags = array(); $string = preg_replace('/[\s-]+/', ' ', $string); $string = explode(',', $string); $string = array_map('trim', $string); foreach ($string as $str) { $tags[] = $str; } $string = implode(',', $tags); $string = preg_replace('/[\s_]/', '-', $string); return $string; } // rename category folder function rename_category_folder($string, $old_url) { $old = str_replace('.md', '/', $old_url); $url = substr($old, 0, strrpos($old, '/')); $ostr = explode('/', $url); $url = '/blog/' . $ostr[count($ostr) - 1]; $dir = get_category_folder(); $file = array(); foreach ($dir as $index => $v) { if (stripos($v, $url) !== false) { $str = explode('/', $v); $n = $str[count($ostr) - 4] . '/' . $str[count($ostr) - 3] .'/'. $str[count($ostr) - 2] .'/'. $string . '/'; $file[] = array($v, $n); } } foreach ($file as $f) { if(is_dir($f[0])) { rename($f[0], $f[1]); } } }