$b['basename']) ? 1 : -1); } // usort function. function sortfile_d($a, $b) { return $a['basename'] == $b['basename'] ? 0 : (($a['basename'] < $b['basename']) ? 1 : -1); } // usort function. Sort by date. function sortdate($a, $b) { return $a->date == $b->date ? 0 : (($a->date < $b->date) ? 1 : -1); } // Rebuilt cache index function rebuilt_cache($type = null) { $dir = 'cache/index'; $posts_cache = array(); $page_cache = array(); $subpage_cache = array(); $author_cache = array(); $scheduled = array(); $category_cache = array(); if (is_dir($dir) === false) { mkdir($dir, 0775, true); } // Rebuilt posts index $tmp = array(); $ctmp = array(); $tmp = glob('content/*/blog/*/*/*.md', GLOB_NOSORT); if (is_array($tmp)) { foreach ($tmp as $file) { if(strpos($file, '/draft/') === false && strpos($file, '/scheduled/') === false) { $posts_cache[] = pathinfo($file); $pc = explode('/', $file); $ctmp[] = $pc[3]; } } } usort($posts_cache, "sortfile_d"); $string_posts = serialize($posts_cache); file_put_contents('cache/index/index-posts.txt', print_r($string_posts, true)); // Rebuilt static page index $ptmp = array(); $ptmp = glob('content/static/*.md', GLOB_NOSORT); if (is_array($ptmp)) { foreach ($ptmp as $file) { if(strpos($file, '/draft/') === false) { $page_cache[] = pathinfo($file); } } } usort($page_cache, "sortfile_a"); $page_string = serialize($page_cache); file_put_contents('cache/index/index-pages.txt', print_r($page_string, true)); // Rebuilt subpage index $sptmp = array(); $sptmp = glob('content/static/*/*.md', GLOB_NOSORT); if (is_array($sptmp)) { foreach ($sptmp as $file) { if(strpos($file, '/draft/') === false) { $subpage_cache[] = pathinfo($file); } } } usort($subpage_cache, "sortfile_a"); $subpage_string = serialize($subpage_cache); file_put_contents('cache/index/index-subpages.txt', print_r($subpage_string, true)); // Rebuilt user profile index $atmp = array(); $atmp = glob('content/*/author.md', GLOB_NOSORT); if (is_array($atmp)) { foreach ($atmp as $file) { $author_cache[] = pathinfo($file); } } usort($author_cache, "sortfile_a"); $author_string = serialize($author_cache); file_put_contents('cache/index/index-author.txt', print_r($author_string, true)); // Rebuilt category files index $ftmp = array(); $cf = array(); $dirc = array(); $dirm = array(); $ftmp = glob('content/data/category/*.md', GLOB_NOSORT); if (is_array($ftmp)) { foreach ($ftmp as $file) { $category_cache[] = pathinfo($file); $cf[] = pathinfo($file, PATHINFO_FILENAME); } } usort($category_cache, "sortfile_a"); $category_string = serialize($category_cache); file_put_contents('cache/index/index-category-files.txt', print_r($category_string, true)); $dirc = array_unique($ctmp, SORT_REGULAR); $dirm = array_unique(array_merge($dirc, $cf), SORT_REGULAR); file_put_contents('cache/index/index-category.txt', print_r(serialize($dirm), true)); // Rebuilt scheduled posts index $stmp = array(); $stmp = glob('content/*/*/*/*/scheduled/*.md', GLOB_NOSORT); if (is_array($stmp)) { foreach ($stmp as $file) { $scheduled[] = pathinfo($file); } } usort($scheduled, "sortfile_d"); $scheduled_string = serialize($scheduled); file_put_contents('cache/index/index-scheduled.txt', print_r($scheduled_string, true)); // Remove the widget cache 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_blog_posts(); } $tmp = array(); // Extract a specific page with results $posts = array_slice($posts, ($page - 1) * $perpage, $perpage); $cList = category_list(true); foreach ($posts as $index => $v) { $post = new stdClass; $filepath = $v['dirname'] . '/' . $v['basename']; // Extract the date $arr = explode('_', $v['basename']); // dirname string $dirname = $v['dirname'] . '/'; // Author string $str = explode('/', $dirname); $author = $str[1]; foreach ($cList as $a => $t) { if ($t['0'] === $str[3]) { $post->category = '' . $t['1'] . ''; $post->categoryUrl = site_url() . 'category/' . $t['0']; $post->categoryCount = $t['2']; $post->categorySlug = $t['0']; $post->categoryMd = $t['0']; $post->categoryTitle = $t['1']; $post->categoryb = '' . $t['1'] . ''; } } $type = $str[4]; $post->ct = $str[3]; // The post author + author url $post->author = $author; $post->authorUrl = site_url() . 'author/' . $author; $profile = get_author($author); if (isset($profile[0])) { $post->authorName = $profile[0]->name; $post->authorAbout = $profile[0]->about; } else { $post->authorName = $author; $post->authorAbout = i18n('Author_Description'); } $post->type = $type; $dt = str_replace($dirname, '', $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); $post->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($filepath))); // 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 post: ' . format_date($post->lastMod, 'l, j F Y, H:i')); $post->image = get_content_tag('image', $content); $post->video = 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, ',')); if (count($tl) == count($t)) { $tCom = array_combine($t, $tl); } else { $tCom = array_combine($t, $t); } 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 = $post->categorySlug. ',' .$post->url; $more = explode('', $content); if (isset($more['1'])) { $content = $more['0'] . '
' . "\n\n" . '' . $more['1']; } // Get the contents and convert it to HTML $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); // Convert image tags to figures if (config('fig.captions') == 'true') { $post->body = preg_replace( '/

()<\/p>/', '

$1
$2
', $post->body ); } if (config('views.counter') == 'true') { $post->views = get_views($post->file); } else { $post->views = null; } $post->description = get_content_tag("d", $content, get_description($post->body)); $word_count = str_word_count(strip_tags($post->body)); $post->readTime = ceil($word_count / 200); $tmp[] = $post; } return $tmp; } function get_pages($pages, $page = 1, $perpage = 0) { if (empty($pages)) { $pages = get_static_pages(); } $tmp = array(); // Extract a specific page with results $pages = array_slice($pages, ($page - 1) * $perpage, $perpage); foreach ($pages as $index => $v) { $post = new stdClass; // The static page URL $url= $v['filename']; $post->url = site_url() . $url; $post->file = $v['dirname'] . '/' . $v['basename']; $post->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($post->file))); $post->md = $url; // Get the contents and convert it to HTML $content = file_get_contents($post->file); // Extract the title and body $post->title = get_content_tag('t', $content, 'Untitled static page: ' . format_date($post->lastMod, 'l, j F Y, H:i')); // Get the contents and convert it to HTML $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); if (config('views.counter') == 'true') { $post->views = get_views($post->file); } $post->description = get_content_tag("d", $content, get_description($post->body)); $word_count = str_word_count(strip_tags($post->body)); $post->readTime = ceil($word_count / 200); $tmp[] = $post; } return $tmp; } function get_subpages($sub_pages, $page = 1, $perpage = 0) { if (empty($sub_pages)) { $sub_pages = get_static_subpages(); } $tmp = array(); // Extract a specific page with results $sub_pages = array_slice($sub_pages, ($page - 1) * $perpage, $perpage); foreach ($sub_pages as $index => $v) { $post = new stdClass; $static = str_replace(dirname($v['dirname']) . '/', '', $v['dirname']); // The static page URL $url= $v['filename']; $post->url = site_url() . $static . "/" . $url; $post->file = $v['dirname'] . '/' . $v['basename']; $post->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($post->file))); $post->md = $url; $post->parent = $static; // Get the contents and convert it to HTML $content = file_get_contents($post->file); // Extract the title and body $post->title = get_content_tag('t', $content, 'Untitled static subpage: ' . format_date($post->lastMod, 'l, j F Y, H:i')); // Get the contents and convert it to HTML $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)); $word_count = str_word_count(strip_tags($post->body)); $post->readTime = ceil($word_count / 200); $tmp[] = $post; } return $tmp; } // Find post by year, month and name, previous, and next. function find_post($year, $month, $name) { $posts = get_blog_posts(); foreach ($posts as $index => $v) { $arr = explode('_', $v['basename']); if ((strpos($arr[0], "$year-$month") !== false && strtolower($arr[2]) === strtolower($name . '.md')) || ($year === NULL && 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 static page. function find_page($static = null) { $pages = get_static_pages(); $tmp = array(); if (!empty($pages)) { foreach ($pages as $index => $v) { if (is_null($static)) { $post = new stdClass; // The static page URL $url= $v['filename']; $post->url = site_url() . $url; $post->file = $v['dirname'] . '/' . $v['basename']; $post->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($post->file))); $post->md = $url; // Get the contents and convert it to HTML $content = file_get_contents($post->file); // Extract the title and body $post->title = get_content_tag('t', $content, 'Untitled static page: ' . format_date($post->lastMod, 'l, j F Y, H:i')); // Get the contents and convert it to HTML $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); if (config('views.counter') == 'true') { $post->views = get_views($post->file); } $post->description = get_content_tag("d", $content, get_description($post->body)); $word_count = str_word_count(strip_tags($post->body)); $post->readTime = ceil($word_count / 200); $tmp[] = $post; } elseif (stripos($v['basename'], $static . '.md') !== false) { // Use the get_posts method to return // a properly parsed object $ar = get_pages($pages, $index + 1, 1); $nx = get_pages($pages, $index, 1); $pr = get_pages($pages, $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($pages) == $index + 1) { return array( 'current' => $ar[0], 'next' => $nx[0] ); } else { return array( 'current' => $ar[0], 'next' => $nx[0], 'prev' => $pr[0] ); } } } } return $tmp; } // Return static subpage. function find_subpage($static, $sub_static = null) { $sub_pages = array_values(get_static_subpages($static)); $tmp = array(); if (!empty($sub_pages)) { foreach ($sub_pages as $index => $v) { if (is_null($sub_static)) { $post = new stdClass; // The static page URL $url= $v['filename']; $post->url = site_url() . $static . "/" . $url; $post->file = $v['dirname'] . '/' . $v['basename']; $post->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($post->file))); $post->md = $url; $post->parent = $static; // Get the contents and convert it to HTML $content = file_get_contents($post->file); // Extract the title and body $post->title = get_content_tag('t', $content, 'Untitled static subpage: ' . format_date($post->lastMod, 'l, j F Y, H:i')); // Get the contents and convert it to HTML $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)); $word_count = str_word_count(strip_tags($post->body)); $post->readTime = ceil($word_count / 200); $tmp[] = $post; } elseif (stripos($v['basename'], $sub_static . '.md') !== false) { // Use the get_posts method to return // a properly parsed object $ar = get_subpages($sub_pages, $index + 1, 1); $nx = get_subpages($sub_pages, $index, 1); $pr = get_subpages($sub_pages, $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($sub_pages) == $index + 1) { return array( 'current' => $ar[0], 'next' => $nx[0] ); } else { return array( 'current' => $ar[0], 'next' => $nx[0], 'prev' => $pr[0] ); } } } } return $tmp; } // Return category page. function get_category($category, $page, $perpage, $random = null) { $posts = get_blog_posts(); if ($random === true) { shuffle($posts); } $tmp = array(); if (empty($perpage)) { $perpage = 10; } foreach ($posts as $index => $v) { // dirname string $dirname = $v['dirname'] . '/'; $str = explode('/', $dirname); if (strtolower($category) === strtolower($str[3])) { $tmp[] = $v; } } if (empty($tmp)) { return false; } $tmp = array_unique($tmp, SORT_REGULAR); return $tmp = get_posts($tmp, $page, $perpage); } // Return category info. function get_category_info($category = null) { $tmp = array(); $cslug= get_category_slug(); if (!empty($cslug)) { asort($cslug); if (is_null($category)) { foreach ($cslug as $key => $c){ $ctmp = read_category_info($c); if (!empty($ctmp[0])) { $tmp[] = $ctmp[0]; } else { $tmp[] = default_category($c); } } } else { foreach ($cslug as $key => $c){ if ($c === $category) { $ctmp = read_category_info($category); if (!empty($ctmp[0])) { $tmp[] = $ctmp[0]; } else { $tmp[] = default_category($category); } } } } } return $tmp; } function read_category_info($category) { $tmp = array(); $cFiles = get_category_files(); if (!empty($cFiles)) { foreach ($cFiles as $index => $v) { if (stripos($v['basename'], $category . '.md') !== false) { $desc = new stdClass; // The filename $filename = $v['dirname'] . '/' . $v['basename']; $url= $v['filename']; $desc->url = site_url() . 'category/' . $url; $desc->md = $url; $desc->slug = $url; $desc->count = get_categorycount($url); $desc->file = $filename; // Get the contents and convert it to HTML $content = file_get_contents($desc->file); // Extract the title and body $desc->title = get_content_tag('t', $content, $category); // Get the contents and convert it to HTML $desc->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); $desc->description = get_content_tag("d", $content, get_description($desc->body)); $tmp[] = $desc; } } } return $tmp; } // Return default category function default_category($category = null) { $tmp = array(); $desc = new stdClass; if ($category == 'uncategorized') { $desc->title = i18n("Uncategorized"); $desc->url = site_url() . 'category/uncategorized'; $desc->slug = 'uncategorized'; $desc->body = '

' . i18n('Uncategorized_comment') . '

'; $desc->md = 'uncategorized'; $desc->description = i18n('Uncategorized_comment'); $desc->file = ''; $desc->count = get_categorycount($desc->md); } else { $desc->title = $category; $desc->url = site_url() . 'category/' . $category; $desc->slug = $category; $desc->body = '

' . i18n('All_blog_posts') . ': ' . $category . '

'; $desc->md = $category; $desc->description = i18n('All_blog_posts') . ': ' . $category; $desc->file = ''; $desc->count = get_categorycount($category); } return $tmp[] = $desc; } // Return category list function category_list($custom = null) { $dir = "cache/widget"; $filename = "cache/widget/category.list.cache"; $tmp = array(); $cat = array(); $list = array(); if (is_dir($dir) === false) { mkdir($dir, 0775, true); } if (file_exists($filename)) { $cat = unserialize(file_get_contents($filename)); } else { $arr = get_category_info(null); foreach ($arr as $i => $a) { $cat[] = array($a->md, $a->title, $a->count, $a->description); } $tmp = serialize($cat); file_put_contents($filename, print_r($tmp, true)); } if(!empty($custom)) { return $cat; } echo ''; } // Return type page. function get_type($type, $page, $perpage) { $posts = get_blog_posts(); $tmp = array(); if (empty($perpage)) { $perpage = 10; } foreach ($posts as $index => $v) { // dirname string $dirname = $v['dirname'] . '/'; $str = explode('/', $dirname); if (strtolower($type) === strtolower($str[4])) { $tmp[] = $v; } } if (empty($tmp)) { return false; } $tmp = array_unique($tmp, SORT_REGULAR); return $tmp = get_posts($tmp, $page, $perpage); } // Return tag page. function get_tag($tag, $page, $perpage, $random = null) { $posts = get_blog_posts(); 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)) { return false; } $tmp = array_unique($tmp, SORT_REGULAR); return $tmp = get_posts($tmp, $page, $perpage); } // Return archive page. function get_archive($req, $page, $perpage) { $posts = get_blog_posts(); $tmp = array(); foreach ($posts as $index => $v) { $str = explode('_', $v['basename']); if (strpos($str[0], "$req") !== false) { $tmp[] = $v; } } if (empty($tmp)) { return false; } return $tmp = get_posts($tmp, $page, $perpage); } // Return posts list on profile. function get_profile_posts($name, $page, $perpage) { $posts = get_blog_posts(); $tmp = array(); foreach ($posts as $index => $v) { $str = explode('/', $v['dirname']); if (strtolower($name) === strtolower($str[1])) { $tmp[] = $v; } } if (empty($tmp)) { return false; } return $tmp = get_posts($tmp, $page, $perpage); } // Return author info. function get_author($name) { $names = get_author_name(); $tmp = array(); if (!empty($names)) { foreach ($names as $index => $v) { $author = new stdClass; // dirname string $dirname = $v['dirname']; // Author string $str = explode('/', $dirname); $profile = $str[1]; if ($name === $profile) { // Profile URL $filename = $v['dirname'] . '/' . $v['basename']; $author->file = $filename; $author->url = site_url() . 'author/' . $profile; // Get the contents and convert it to HTML $content = file_get_contents($author->file); // Extract the title and body $author->name = get_content_tag('t', $content, $author); // Get the contents and convert it to HTML $author->about = MarkdownExtra::defaultTransform(remove_html_comments($content)); $author->body = $author->about; $author->title = $author->name; $author->description = strip_tags($author->about); $tmp[] = $author; } } } if (!empty($tmp)) { return $tmp; } else { return false; } } // Return default profile function default_profile($name) { $tmp = array(); $author = new stdClass; $author->name = $name; $author->title = $name; $author->about = '

' . i18n('Author_Description') . '

'; $author->body = '

' . i18n('Author_Description') . '

'; $author->description = i18n('Author_Description'); $author->url = site_url(). 'author/' . $name; $author->file = ''; return $tmp[] = $author; } // Return frontpage content function get_frontpage() { $front = new stdClass; $filename = 'content/data/frontpage/frontpage.md'; if (file_exists($filename)) { $content = file_get_contents($filename); $front->title = get_content_tag('t', $content, 'Welcome'); $front->url = site_url() . 'front'; // Get the contents and convert it to HTML $front->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); } else { $front->title = 'Welcome'; $front->url = site_url() . 'front'; $front->body = 'Welcome to our website.'; } return $front; } // Return search page. function get_keyword($keyword, $page, $perpage) { $posts = get_blog_posts(); $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) { if (!in_array($v, $tmp)) { $tmp[] = $v; } } } } if (empty($tmp)) { return false; } return $tmp = get_posts($tmp, $page, $perpage); } // Get related posts base on post category. function get_related($tag, $custom = null, $count = null) { if (empty($count)) { $count = config('related.count'); if (empty($count)) { $count = 3; } } $tmp = array(); $exp = explode(',', $tag); $posts = get_category($exp[0], 1, $count + 1, true); foreach ($posts as $post) { if ($post->url !== $exp[1]) { $tmp[] = $post; } } if (empty($custom)) { if (!empty($tmp)) { $i = 1; echo ''; } else { echo ''; } } else { return $tmp; } } // Return post count. Matching $var and $str provided. function get_count($var, $str) { $posts = get_blog_posts(); $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 category count. Matching $var function get_categorycount($var) { $posts = get_blog_posts(); $tmp = array(); foreach ($posts as $index => $v) { if (stripos($v['dirname'], '/' . $var . '/') !== false) { $tmp[] = $v; } } return count($tmp); } // Return type count. Matching $var function get_typecount($var) { $posts = get_blog_posts(); $tmp = array(); foreach ($posts as $index => $v) { if (stripos($v['dirname'], '/' . $var) !== false) { $tmp[] = $v; } } return count($tmp); } // Return draft count. Matching $var function get_draftcount($var) { $posts = get_draft_posts(); $tmp = array(); foreach ($posts as $index => $v) { if (stripos($v['dirname'], '/' . $var . '/') !== false) { $tmp[] = $v; } } return count($tmp); } // Return scheduled post count. Matching $var function get_scheduledcount($var) { $posts = get_scheduled_posts(); $tmp = array(); foreach ($posts as $index => $v) { if (stripos($v['dirname'], '/' . $var . '/') !== false) { $tmp[] = $v; } } return count($tmp); } // Return tag count. Matching $var function get_tagcount($var) { $posts = get_blog_posts(); $tmp = array(); foreach ($posts as $index => $v) { $arr = explode('_', $v['basename']); $mtag = explode(',', rtrim($arr[1], ',')); foreach ($mtag as $t) { if (strtolower($t) === strtolower($var)) { $tmp[] = $v; } } } return count($tmp); } // Return search result count function keyword_count($keyword) { $posts = get_blog_posts(); $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 recent type lists function recent_type($type, $custom = null, $count = null) { if (empty($count)) { $count = config('recent.count'); if (empty($count)) { $count = 5; } } $dir = 'cache/widget'; $filename = 'cache/widget/recent.' . $type . '.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_type($type, 1, $count); $tmp = serialize($posts); file_put_contents($filename, print_r($tmp, true)); } } else { $posts = get_type($type, 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/data/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 && stripos($key, '/scheduled/') == false && stripos($key, '/draft/') == 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_blog_posts(); $by_year = array(); $col = array(); if (!empty($posts)) { if (!file_exists($filename)) { foreach ($posts as $index => $v) { $arr = explode('_', $v['filename']); $date = $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 ''; } } elseif ($custom === 'month-year') { foreach ($by_year as $year => $months) { $by_month = array_count_values($months); # Sort the months krsort($by_month); foreach ($by_month as $month => $count) { $name = format_date(mktime(0, 0, 0, $month, 1, 2010), 'F'); echo '
  • ' . $name . ' ' . $year .' ('.$count.')
  • '; } } } elseif ($custom === 'year') { foreach ($by_year as $year => $months) { $by_month = array_count_values($months); # Sort the months krsort($by_month); echo '
  • ' . $year .' ('. count($months) .')
  • '; } } 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_blog_posts(); $tags = array(); $tagcloud_count = config('tagcloud.count'); if(empty($tagcloud_count)) { $tagcloud_count = 40; } if (!empty($posts)) { if (!file_exists($filename)) { foreach ($posts as $index => $v) { $arr = explode('_', $v['filename']); $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)) { // Font sizes $max_size = 22; // max font size in % $min_size = 8; // min font size in % // Get the largest and smallest array values $max_qty = max(array_values($tag_collection)); $min_qty = min(array_values($tag_collection)); // Find the range of values $spread = $max_qty - $min_qty; if (0 == $spread) { // we don't want to divide by zero $spread = 1; } // Font-size increment // this is the increase per tag quantity (times used) $step = ($max_size - $min_size)/($spread); arsort($tag_collection); $sliced_tags = array_slice($tag_collection, 0, $tagcloud_count, true); ksort($sliced_tags); foreach ($sliced_tags as $tag => $count) { $size = $min_size + (($count - $min_qty) * $step); echo ' '.tag_i18n($tag).' '; } } 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, 'date' => $prev->date, 'body' => $prev->body, 'description' => $prev->description, 'tag' => $prev->tag, 'author' => $prev->author, 'authorName' => $prev->authorName, 'authorAbout' => $prev->authorAbout, 'authorUrl' => $prev->authorUrl, 'related' => $prev->related, 'views' => $prev->views, 'type' => $prev->type, 'file' => $prev->file, 'image' => $prev->image, 'video' => $prev->video, 'audio' => $prev->audio, 'quote' => $prev->quote, 'link' => $prev->link, 'category' => $prev->category, 'categoryUrl' => $prev->categoryUrl, 'categoryCount' => $prev->categoryCount, 'categorySlug' => $prev->categorySlug, 'categoryMd' => $prev->categoryMd, 'categoryTitle' => $prev->categoryTitle, 'readTime' => $prev->readTime, 'lastMod' => $prev->lastMod ); } } // 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, 'date' => $next->date, 'body' => $next->body, 'description' => $next->description, 'tag' => $next->tag, 'author' => $next->author, 'authorName' => $next->authorName, 'authorAbout' => $next->authorAbout, 'authorUrl' => $next->authorUrl, 'related' => $next->related, 'views' => $next->views, 'type' => $next->type, 'file' => $next->file, 'image' => $next->image, 'video' => $next->video, 'audio' => $next->audio, 'quote' => $next->quote, 'link' => $next->link, 'category' => $next->category, 'categoryUrl' => $next->categoryUrl, 'categoryCount' => $next->categoryCount, 'categorySlug' => $next->categorySlug, 'categoryMd' => $next->categoryMd, 'categoryTitle' => $next->categoryTitle, 'readTime' => $next->readTime, 'lastMod' => $next->lastMod ); } } function static_prev($prev) { if (!empty($prev)) { return array( 'url' => $prev->url, 'title' => $prev->title, 'body' => $prev->body, 'description' => $prev->description, 'views' => $prev->views, 'file' => $prev->file, 'readTime' => $prev->readTime, 'lastMod' => $prev->lastMod ); } } function static_next($next) { if (!empty($next)) { return array( 'url' => $next->url, 'title' => $next->title, 'body' => $next->body, 'description' => $next->description, 'views' => $next->views, 'file' => $next->file, 'readTime' => $next->readTime, 'lastMod' => $next->lastMod ); } } // Helper function to determine whether // to show the pagination buttons function has_pagination($total, $perpage, $page = 1) { if (!$total) { $total = count(get_blog_posts()); } $totalPage = ceil($total / $perpage); $number = i18n('Page') . ' ' . $page . ' ' . i18n('of') . ' ' . $totalPage; $pager = get_pagination($total, $page, $perpage, 2); return array( 'prev' => $page > 1, 'next' => $total > $page * $perpage, 'pagenum' => $number, 'html' => $pager, 'items' => $total, 'perpage' => $perpage ); } //function to return the pagination string function get_pagination($totalitems, $page = 1, $perpage = 10, $adjacents = 1, $pagestring = '?page=') { //defaults if(!$adjacents) $adjacents = 1; if(!$perpage) $perpage = 10; if(!$page) $page = 1; //other vars $prev = $page - 1; //previous page is page - 1 $next = $page + 1; //next page is page + 1 $lastpage = ceil($totalitems / $perpage); //lastpage is = total items / items per page, rounded up. $lpm1 = $lastpage - 1; //last page minus 1 /* Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once. */ $pagination = ''; if($lastpage > 1) { $pagination .= ''; } return $pagination; } // 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, $url = null, $char = null) { $teaserType = config('teaser.type'); $more = config('read.more'); if(empty($more)) { $more = 'Read more'; } if(empty($char)) { $char = config('teaser.char'); if(empty($char)) { $char = 200; } } if ($teaserType === 'full') { $readMore = explode('', $string); if (isset($readMore['1'])) { $patterns = array('
    ', '


    '); $string = str_replace($patterns, '', $readMore['0']); $string = replace_href($string, 'a', 'footnote-ref', $url); return $string . ''; } else { return $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, $url = null) { 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'); if (!empty($url)) { return $imgSource; } else { 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'; if (!empty($url)) { return $vidThumb; } else { return '
    '; } } } else { if (!empty($default)) { if (!empty($url)) { return $default; } else { return '
    '; } } } } else { // Ignore } } } // Get image from post and Youtube thumbnail. function get_image($text) { libxml_use_internal_errors(true); $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 $imgSource; } 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] . '/sddefault.jpg'; return $vidThumb; } } else{ return false; } } // 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 { if ($p->url) { echo '
    '; } } } // Social links. Deprecated function social($imgDir = null) { $twitter = config('social.twitter'); $facebook = config('social.facebook'); $tumblr = config('social.tumblr'); $rss = site_url() . 'feed/rss'; if ($imgDir === null) { $imgDir = "readable/img/"; } if (!empty($twitter)) { echo 'Twitter'; } if (!empty($facebook)) { echo 'Facebook'; } if (!empty($tumblr)) { echo 'Tumblr'; } echo 'RSS Feed'; } // Copyright function copyright() { $blogcp = blog_copyright(); $credit = '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 getAbsolutePath = function(href) { var link = document.createElement('a'); link.href = href; return link.href; }; var disqus_shortname = '{$disqus}'; var disqus_title = '{$title}'; var disqus_url = getAbsolutePath('{$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() { $analytics = config('google.analytics.id'); $gtag = config('google.gtag.id'); $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','//www.google-analytics.com/analytics.js','ga'); ga('create', '{$analytics}', 'auto'); ga('send', 'pageview'); EOF; $gtagScript = << EOF; if (!empty($gtag)) { return $gtagScript; } elseif (!empty($analytics)) { return $script; } } function slashUrl($url) { return rtrim($url, '/') . '/'; } function parseNodes($nodes, $child = null, $class = null) { if (empty($child)) { $ul = ''; return $ul; } else { $ul = ''; return $ul; } } function parseNode($node, $child = null) { $req = strtok($_SERVER["REQUEST_URI"],'?'); $url = parse_url(slashUrl($node->slug)); $su = parse_url(site_url()); if (empty($child)) { if (isset($url['host']) && isset($su['host'])) { if ($url['host'] == $su['host']) { if (slashUrl($url['path']) == slashUrl($req)) { $li = ''; return $li; } else { if (isset($url['host']) && isset($su['host'])) { if ($url['host'] == $su['host']) { if (slashUrl($url['path']) == slashUrl($req)) { $li = ''; return $li; } } // Menu function menu($class = null) { $filename = "content/data/menu.json"; if (file_exists($filename)) { $json = json_decode(file_get_contents('content/data/menu.json', true)); $nodes = json_decode($json); if (empty($nodes)) { get_menu($class); } else { $html = parseNodes($nodes, null, $class); libxml_use_internal_errors(true); $doc = new DOMDocument(); $doc->loadHTML('' . $html); $finder = new DOMXPath($doc); $elements = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' dropdown-menu ')]"); // loop through all
      with dropdown-menu class foreach ($elements as $element) { $nodes = $element->childNodes; foreach ($nodes as $node) { $class = $node->getAttribute('class'); if (stripos($class, 'active')) { $parentClass = $element->parentNode->getAttribute('class') . ' active'; $element->parentNode->setAttribute('class', $parentClass); } } } return preg_replace('~<(?:!DOCTYPE|/?(?:html|head|body))[^>]*>\s*~i', '', mb_convert_encoding($doc->saveHTML($doc->documentElement), 'UTF-8')); } } else { get_menu($class); } } // 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)); $filename= pathinfo($v, PATHINFO_FILENAME); // Extract the title and body return get_content_tag('t', $content, str_replace('-', ' ', $filename)); } // Auto generate menu from static page function get_menu($custom) { $posts = get_static_pages(); $req = $_SERVER['REQUEST_URI']; if (!empty($posts)) { asort($posts); echo ''; } else { echo ''; } } // Search form function search($text = null) { if(!empty($text)) { echo << EOF; } else { $search = i18n('Search'); echo << EOF; } if (isset($_GET['search'])) { $search = _h($_GET['search']); $url = site_url() . 'search/' . remove_accent($search); header("Location: $url"); } } // The not found error function not_found() { $vroot = rtrim(config('views.root'), '/'); $lt = $vroot . '/layout--404.html.php'; if (file_exists($lt)) { $layout = 'layout--404'; } else { $layout = ''; } header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found"); render('404', array( 'title' => i18n('This_page_doesnt_exist') . ' - ' . blog_title(), 'description' => i18n('This_page_doesnt_exist'), 'canonical' => site_url(), 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . i18n('This_page_doesnt_exist'), 'bodyclass' => 'error-404', 'is_404' => true, ), $layout); die(); } // Turn an array of posts into an RSS feed function generate_rss($posts, $data = null) { $feed = new Feed(); $channel = new Channel(); $rssLength = config('rss.char'); $data = $data; if (is_null($data)) { $channel ->title(blog_title()) ->description(blog_description()) ->url(site_url()) ->appendTo($feed); } else { $channel ->title($data->title) ->description(strip_tags($data->body)) ->url($data->url) ->appendTo($feed); } if ($posts) { 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(); $item ->category(strip_tags($p->category)); $item ->title($p->title) ->pubDate($p->date) ->description($body) ->url($p->url) ->appendTo($channel); } } return $feed; } // Return post, archive url for sitemap function sitemap_post_path() { $posts = get_blog_posts(); $tmp = array(); foreach ($posts as $index => $v) { $post = new stdClass; $filepath = $v['dirname'] . '/' . $v['basename']; // Extract the date $arr = explode('_', $v['basename']); // dirname string $dirname = $v['dirname'] . '/'; // Author string $str = explode('/', $dirname); $author = $str[1]; $post->authorUrl = site_url() . 'author/' . $author; $dt = str_replace($dirname, '', $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); $post->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($filepath))); // 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; // Filename $filename= $v['filename']; $file = $v['dirname'] . '/' . $v['basename']; $post->url = site_url() . $filename; $post->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($file))); $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.category') !== 'false') { echo '' . site_url() . 'sitemap.category.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'; } if (config('sitemap.priority.type') !== 'false') { echo '' . site_url() . 'sitemap.type.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 = array(); if ($priority !== 'false') { $posts = sitemap_post_path(); } echo ''; foreach ($posts as $p) { echo '' . $p->url . '' . $priority . '' . date('Y-m-d\TH:i:sP', $p->lastMod) . ''; } echo ''; } elseif ($str == 'static') { $priority = (config('sitemap.priority.static')) ? config('sitemap.priority.static') : $default_priority; $posts = array(); if ($priority !== 'false') { $posts = sitemap_page_path(); } echo ''; foreach ($posts as $p) { echo '' . $p->url . '' . $priority . '' . date('Y-m-d\TH:i:sP', $p->lastMod) . ''; } echo ''; } elseif ($str == 'tag') { $priority = (config('sitemap.priority.tag')) ? config('sitemap.priority.tag') : $default_priority; $posts = array(); if ($priority !== 'false') { $posts = get_blog_posts(); } $tags = array(); echo ''; if($posts) { foreach ($posts as $index => $v) { $arr = explode('_', $v['filename']); $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 = array(); 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 ''; } elseif ($str == 'category') { $priority = (config('sitemap.priority.category')) ? config('sitemap.priority.category') : $default_priority; $posts = array(); if ($priority !== 'false') { $posts = get_blog_posts(); } $cats = array(); echo ''; if($posts) { foreach ($posts as $index => $v) { $dirname = $v['dirname'] . '/'; $str = explode('/', $dirname); $cats[] = $str[3]; } foreach ($cats as $c) { $cat[] = site_url() . 'category/' . strtolower($c); } if (isset($cat)) { $cat = array_unique($cat, SORT_REGULAR); foreach ($cat as $c) { echo '' . $c . '' . $priority . ''; } } } echo ''; } elseif ($str == 'type') { $priority = (config('sitemap.priority.type')) ? config('sitemap.priority.type') : $default_priority; $posts = array(); if ($priority !== 'false') { $posts = get_blog_posts(); } $cats = array(); echo ''; if($posts) { foreach ($posts as $index => $v) { $dirname = $v['dirname'] . '/'; $str = explode('/', $dirname); $types[] = $str[4]; } foreach ($types as $t) { $type[] = site_url() . 'type/' . strtolower($t); } if (isset($type)) { $type = array_unique($type, SORT_REGULAR); foreach ($type as $t) { echo '' . $t . '' . $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); } // 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, '/type/') !== false || stripos($req, '/blog') !== false || $req == site_path() . '/' || stripos($req, site_path() . '/?page') !== false) { 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 blog language function blog_language() { return str_replace('_', '-', config('language')); } // 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'); $version = 'HTMLy ' . constant('HTMLY_VERSION'); $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; } // File cache function file_cache($request) { if (config('cache.off') == 'true') return; $hour = str_replace(',', '.', config('cache.expiration')); if (empty($hour)) { $hour = 6; } $now = time(); $c = str_replace('/', '#', str_replace('?', '~', rawurldecode($request))); $cachefile = 'cache/page/' . $c . '.cache'; if (file_exists($cachefile)) { if ($now - filemtime($cachefile) >= 60 * 60 * $hour) { unlink($cachefile); } else { header('Content-type: text/html; charset=utf-8'); readfile($cachefile); die; } } } // Add page views count function add_view($page) { $dir = 'content/data/'; if (!is_dir($dir)) { mkdir($dir, 0775, true); } $filename = "content/data/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, JSON_UNESCAPED_UNICODE)); } // Get the page views count function get_views($page) { $_views = array(); $filename = "content/data/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|)/', '/(\s|)(\s|)/', '/(\s|)(\s|)/', '/(\s|)(\s|)/', '/(\s|)(\s|)/', '/(\s|)(\s|)/', '/(\s|)(\s|)/', '/(\s|)(\s|)/', '/(\s|)(\s|)/'); return preg_replace($patterns, '', $content); } // Google recaptcha function isCaptcha($reCaptchaResponse) { if (config('google.reCaptcha') != 'true') { 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 video ID function get_video_id($url) { if(empty($url)) { return; } $link = parse_url($url); if(!isset($link['host'])) { return $url; } if (stripos($link['host'], 'youtube.com') !== false || stripos($link['host'], 'youtu.be') !== false) { preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match); return $match[1]; } elseif (stripos($link['host'], 'vimeo.com') !== false) { preg_match('%^https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)(?:[?]?.*)$%im', $url, $match); return $match[3]; } else { return $url; } } // 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; } } // translate tag to i18n function tag_i18n($tag) { static $tags = array(); if (empty($tags)) { $filename = "content/data/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; } // Replace href function replace_href($string, $tag, $class, $url) { libxml_use_internal_errors(true); // Load the HTML in DOM $doc = new DOMDocument(); $doc->loadHTML('' . $string); // Then select all anchor tags $all_anchor_tags = $doc->getElementsByTagName($tag); foreach ($all_anchor_tags as $_tag) { if ($_tag->getAttribute('class') == $class) { // If match class get the href value $old = $_tag->getAttribute('href'); $new = $_tag->setAttribute('href', $url . mb_convert_encoding($old, 'UTF-8')); } } return preg_replace('~<(?:!DOCTYPE|/?(?:html|head|body))[^>]*>\s*~i', '', mb_convert_encoding($doc->saveHTML($doc->documentElement), 'UTF-8')); } // Set the language function get_language() { $langID = config('language'); $langFile = 'lang/'. $langID . '.ini'; $local = $langID; // Settings for the language if (!isset($langID) || config('language') === 'en' || config('language') === 'en_US' || !file_exists($langFile)) { i18n('source', 'lang/en_US.ini'); // Load the English language file setlocale(LC_ALL, 'en_US.utf8'); // Change locale to English } else { i18n('source', $langFile); setlocale(LC_ALL, $local . '.utf8'); } } // Format the date function format_date($date, $dateFormat = null) { if (empty($dateFormat)) { $dateFormat = config('date.format'); } if (extension_loaded('intl')) { $format_map = array('s' => 'ss', 'i' => 'mm', 'H' => 'HH', 'G' => 'H', 'd' => 'dd', 'j' => 'd', 'D' => 'EE', 'l' => 'EEEE', 'm' => 'MM', 'M' => 'MMM', 'F' => 'MMMM', 'Y' => 'yyyy'); $intlFormat = strtr($dateFormat, $format_map); $formatter = new IntlDateFormatter(config('language'), IntlDateFormatter::NONE, IntlDateFormatter::NONE, config('timezone'), IntlDateFormatter::GREGORIAN, $intlFormat); return $formatter->format($date); } else { return date($dateFormat, $date); } } // Publish scheduled post function publish_scheduled() { $posts = get_scheduled_posts(); if (!empty($posts)) { foreach ($posts as $index => $v) { $str = explode('_', $v['basename']); $old = $v['dirname'] . '/' . $v['basename']; $new = dirname($v['dirname']) . '/' . $v['basename']; $t = str_replace('-', '', $str[0]); $time = new DateTime($t); $timestamp = $time->format("Y m d H:i:s"); if (date('Y m d H:i:s') >= $timestamp) { rename($old, $new); rebuilt_cache('all'); clear_cache(); } } } }