<?php
$b = settings('blog_page,show_full_blog_posts');
if($url['pid'] == $b['blog_page'])
{
//captca questions and answers
$spamQuestions = array('What is 3 + 1?', 'What colour is the sky?', 'What is 2 + 1?', 'What is 2 + 2?',);
$spamAnswers = array(4, 'blue', 3, 4);
//function to format tags
function tags($url, $l)
{
$list = explode(",", $l);
if(is_array($list))
{
foreach($list as $key => $word)
{
$out .= (!empty($word)) ? '<a href="/'.$url.'/'.trim($word).'" title="View other posts tagged '.trim($word).'" style="background:url(/inc/blog/images/tag.png) 0 0 no-repeat;">'.trim($word).'</a> ' : '';
}
}
return $out;
}
//save comment
$answer = trim(strtolower($_POST['digit']));
if(isset($_POST['comment_name']) && in_array($answer, $spamAnswers) && empty($_POST['conemail']))
{
$post = $form->clean_fields($_POST);
$count1 = substr_count($post['comment'], 'http:');
$count2 = substr_count($post['comment'], 'https:');
$count3 = substr_count($post['comment'], 'url=');
$count4 = substr_count($post['comment'], 'ugg');
$count = ($count1+$count2+$count3+$count4);
if(!empty($post['comment_name']) && !empty($post['comment']) && $count == 0)
{
$now = time();
$approved = $mysql->get_array($mysql->query("comments", "fs_blog_entries", "ID = '{$post['eid']}' LIMIT 1"));
$mysql->insert("fs_blog_comments", "name,email,website,comment,stamp,approved,eid", "{$post['comment_name']},{$post['comment_email']},{$post['comment_website']},{$post['comment']},$now,{$approved['comments']},{$post['eid']}");
die();
exit;
}
}
//posts per page
$perpage = 10;
$start = (int)$_GET['junk'];
//check GET['page'] var to see if user is requesting posts by a tag or a single post
$sql = "ORDER BY stamp DESC LIMIT $start, $perpage";
$show_more_button = true;
$hide_comments_box = true;
$useH1 = false;
if(!empty($_GET['page']))
{
$key = addslashes($_GET['page']);
//is it a post?
$check = $mysql->num_rows($mysql->query("ID", "fs_blog_entries", "`status` = 'Publish' AND url_key = '$key' LIMIT 1"));
if($check == 1)
{
$sql = "AND url_key = '$key' LIMIT 1";
$show_more_button = false;
$hide_comments_box = false;
$useH1 = true;
}
//is it a tag?
if($check == 0)
{
$temp = "AND tags LIKE '%$key%' ORDER BY stamp DESC";
$check = $mysql->num_rows($mysql->query("ID", "fs_blog_entries", "`status` = 'Publish' $temp LIMIT 1"));
if($check == 1)
{
$show_more_button = false;
$mainhtml .= '<h1>Showing all posts with tag <em>'.$_GET['page'].'</em></h1>';
$sql = $temp;
}
}
//
}
//show content from the blog CMS page
if($show_more_button == true && $hide_comments_box == true && $_GET['page'] != 'moreposts')
{
$blogcontent = $mysql->get_array($mysql->query("content", $mysql->perfix."pages", "ID = {$b['blog_page']} LIMIT 1"));
$blog_out .= stripslashes($blogcontent['content']);
}
//show posts
if($b['show_full_blog_posts'] == 'n')
{
include('truncate.php');
}
$query = $mysql->query("ID,title,stamp,content,url_key,tags,seotitle,seokeywords,seodesc,meta_tags", "fs_blog_entries", "`status` = 'Publish' $sql");
$show_more_button = ($mysql->num_rows($mysql->query("ID,title,stamp,content,url_key,tags", "fs_blog_entries", "`status` = 'Publish'")) <= $perpage) ? false : $show_more_button;
while($blog = $mysql->get_array($query))
{
$blog_out .= ($useH1) ? '<h1' : '<h2';
$blog_out .= '><a href="/'.$b['blog_page'].'/'.$blog['url_key'].'">'.stripslashes($blog['title']).'</a>';
$blog_out .= ($useH1) ? '</h1>' : '</h2>';
$blog_out .= '<small class="post-details">Posted on '.date("d F Y", $blog['stamp']).'<span style="margin-left:25px;">'.tags($b['blog_page'], $blog['tags']).'</span></small>';
$content = stripslashes($blog['content']);
$blog_out .= (!$useH1 && $b['show_full_blog_posts'] == 'n') ? truncateHtml($content, 450, '...<p><a href="/'.$b['blog_page'].'/'.$blog['url_key'].'">Continue Reading</a></p>') : $content;
$comments_query = $mysql->query("name,email,website,comment,stamp", "fs_blog_comments", "eid = {$blog['ID']} AND approved = 'y' ORDER BY stamp DESC");
$numcomments = $mysql->num_rows($comments_query);
$numcomments = ($numcomments == 1) ? '1 comment' : $numcomments.' comments';
$array['page_title'] .= ($hide_comments_box == true) ? '' : ' - '.stripslashes($blog['title']);
$blog_out .= '<table border="0" cellspacing="0" cellpadding="0" class="social"><tr><td>

<div class="addthis_toolbox addthis_default_style" addthis:url="'.httpOrHttps().'://'.$_SERVER['HTTP_HOST'].'/'.$b['blog_page'].'/'.$blog['url_key'].'" addthis:title="'.$blog['title'].'">
<a class="addthis_button_facebook_like HideForMobile" fb:like:layout="button_count"></a>
<a class="addthis_button_tweet HideForMobile"></a>
<a class="addthis_counter addthis_pill_style"></a>
</div>

</td>
<td align="right" nowrap="nowrap"><a href="/'.$b['blog_page'].'/'.$blog['url_key'].'#comments" title="View or post comments">'.$numcomments.'</a><a href="/'.$b['blog_page'].'/'.$blog['url_key'].'" style="margin-left:20px;">Permalink</a></td>
</tr></table>';
$blog_out .= '<hr />';
//if viewing single post, inject meta data into head
if($hide_comments_box == false)
{
$array['page_title'] = (!empty($blog['seotitle'])) ? stripslashes($blog['seotitle']) : $array['page_title'];
$array['keywords'] = (!empty($blog['seokeywords'])) ? stripslashes($blog['seokeywords']) : $array['keywords'];
$array['meta_desc'] = (!empty($blog['seodesc'])) ? stripslashes($blog['seodesc']) : $array['meta_desc'];
$mt = '<meta property="og:type" content="website" /><meta property="og:url" content="'.httpOrHttps().'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'" />';
if(!empty($blog['seotitle'])){$mt .= '<meta property="og:title" content="'.stripslashes($blog['seotitle']).'" />';}
if(!empty($blog['seodesc'])){$mt .= '<meta property="og:description" content="'.stripslashes($blog['seodesc']).'" />';}
if(!empty($blog['meta_tags'])){$mt .= stripslashes($blog['meta_tags']);}
$mainhtml = str_replace("</head>", $mt.'</head>', $mainhtml);
}
//if viewing single post, show related posts
if($hide_comments_box == false)
{
$list = explode(",", $blog['tags']);
if(is_array($list))
{
foreach($list as $key => $word)
{
$related_sql .= "tags LIKE '%".trim($word)."%' OR ";
}
$related_sql = substr($related_sql, 0, -4);
}
$related_query = $mysql->query("title,url_key,stamp", "fs_blog_entries", "status = 'Publish' AND ($related_sql) AND ID != {$blog['ID']} ORDER BY stamp DESC LIMIT 5");
if($mysql->num_rows($related_query) > 0)
{
$blog_out .= '<h2>Related posts</h2><ul class="related-posts">';
while($related = $mysql->get_array($related_query))
{
$blog_out .= '<li><a href="/'.$b['blog_page'].'/'.$related['url_key'].'">'.$related['title'].'</a><small>Posted on '.date("d F Y", $related['stamp']).'</small></li>';
}
$blog_out .= '</ul>';
}
}
//comments
$hide_comments = ($hide_comments_box == true) ? 'display:none;' : '';
$blog_out .= '<a name="comments"></a><div id="comments_box'.$blog['ID'].'" class="comments-box" style="'.$hide_comments.'"><h2>Comments <span>('.number_format($mysql->num_rows($comments_query)).')</span></h2>';
while($comment = $mysql->get_array($comments_query))
{
$name = (!empty($comment['website'])) ? '<a href="http://'.str_replace("http://", "", $comment['website']).'" target="_blank">'.$comment['name'].'</a>' : $comment['name'];
$blog_out .= stripslashes('<p><strong>'.$name.'<small><span>&middot;</span>'.date("d F Y - h:ia", $comment['stamp']).'</small></strong>'.nl2br($comment['comment']).'</p><hr />');
}
//comments form
$rand = rand(0, (sizeof($spamQuestions)-1));
$blog_out .= '<h2>Post a comment</h2>';
$blog_out .= '<form name="form'.$blog['ID'].'" id="form'.$blog['ID'].'" method="post" action="">
<table border="0" cellspacing="0" cellpadding="0">
<tr><th>Your name</th><td><input name="comment_name" type="text" id="comment_name" size="32" class="required" /></td></tr>
<tr><th>Your email address<small>(Will not be published)</small></th><td><input name="comment_email" type="text" id="comment_email" size="32" class="validate-email" /></td></tr>
<tr><th>Website URL<small>(Optional)</small></th><td><input name="comment_website" type="text" id="comment_website" size="32" /></td></tr>
<tr><th valign="top">Your comment</th><td><textarea name="comment" rows="8" id="comment" class="required"></textarea></td></tr>
<tr><th>'.$spamQuestions[$rand].'<small>(Anti-spam question)</small></th><td><input name="digit" id="digit" type="text" size="6" class="required" /></td></tr>
<tr><td>&nbsp;</td><td><input type="button" name="button" id="button'.$blog['ID'].'" value="Post comment" onclick="post_comment(\'form'.$blog['ID'].'\');" class="button" /></td></tr>
<tr style="display:none;"><td>&nbsp;</td><td><input type="text" name="conemail" id="conemail" value="" autocomplete="off" /></td></tr>
</table>
<input type="hidden" name="eid" id="eid" value="'.$blog['ID'].'" />
</form>';
$blog_out .= '</div>';
//footer
$blog_out .= '';
}
//if moreposts is set in GET, show the contents
if($_GET['page'] == 'moreposts')
{
echo $blog_out.'<script>addthis.toolbox(\'.addthis_toolbox\');</script>';
die();
exit;
}
$mainhtml .= '<div id="blog_content">'.$blog_out.'</div>';
//javascript to check the comments form before posting
$mainhtml .= '<script type="text/javascript">function post_comment(form){var valid = new Validation(form, {onSubmit:false});var result = valid.validate();if(result){$(form).request({onComplete: function(){new Insertion.Before(form, \'<p style="font-weight:bold;" align="center">Thank you, your comment has been sent.</p>\');$(form).reset();}})}}</script><script type="text/javascript" src="//s7.addthis.com/js/250/addthis_widget.js#domready=1" id="addthisscript"></script>';
//show more button
if($show_more_button == true)
{
$mainhtml .= '<h3 align="center"><a href="javascript:;" id="load_more_content">Show older posts</a><input type="hidden" id="loadmore" name="loadmore" value="'.$perpage.'" /></h3>';
$mainhtml .= '<script type="text/javascript">Event.observe(\'load_more_content\', \'click\', function(event){var more = document.getElementById(\'loadmore\').value;new Ajax.Request(\'/'.$b['blog_page'].'/moreposts/\'+more+\'/\', {
method: \'get\',evalScripts:true,onCreate:function(){$(\'load_more_content\').update(\'Loading\');},onSuccess: function(transport){new Insertion.Bottom(\'blog_content\', transport.responseText);$(\'load_more_content\').update(\'Show older posts\');document.getElementById(\'loadmore\').value = parseInt(more)+'.$perpage.';}});});</script>
';
}
//add js in the head
$head = '';
if(!strstr($mainhtml, 'blog/src/style.css'))
{
$head .= '<link rel="stylesheet" type="text/css" href="/inc/blog/src/style.css" />';
}
if(!strstr($mainhtml, 'prototype.js'))
{
$head .= '<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>';
}
if(!strstr($mainhtml, 'scriptaculous.js'))
{
$head .= '<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/scriptaculous/1.8.3/scriptaculous.js?load=effects,builder"></script>';
}
if(!strstr($mainhtml, 'validation.js'))
{
$head .= '<script type="text/javascript" src="/inc/blog/src/validation.js"></script>';
}
$mainhtml = str_replace("</head>", $head.'</head>', $mainhtml);
//include this add-ons's overwrite css file from the template being used, if there is one
$t = settings('template');
if(file_exists('templates/'.$t['template'].'/addons/blog.css'))
{
$mainhtml = str_replace("</head>", '<link rel="stylesheet" type="text/css" href="/templates/'.$t['template'].'/addons/blog.css" /></head>', $mainhtml);
}
else
{
$mainhtml = str_replace("</head>", '<link rel="stylesheet" type="text/css" href="//www.freesites.com.au/addons/blog.css" /></head>', $mainhtml);
}
//end
}
?>