<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.7 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2011 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+

session_start();
require('config.php');
require_once('include/functions.php');
require_once('include/user_functions.php');
require_once('include/islogged.php');
require_once('include/article_functions.php');

if ( ! _MOD_ARTICLE)
{
	header("Location: ". _URL ."/index.". _FEXT);
	exit();
}

$id = (int) $_GET['a'];
$article = array();

if ($id > 0)
{
	$preview = ($_GET['mode'] == 'preview') ? true : false;
	$uniq_id = 'article-';
	$result = array();
	$result = art_load_articles(0, 1, '', 0, array($id), '', $preview);
	$article = (array) $result[$id];

	if ( ! array_key_exists('type', $result))
	{
		//	meta tags
		//$meta_title = htmlentities($article['title'], ENT_COMPAT, 'UTF-8');
		$meta_title = specialchars($article['title'], 1);
		$meta_title .= ' - '. _SITENAME;
		
		$meta_description = str_replace('"', '&amp;', $article['excerpt']);
		
		$meta_keywords = '';
		foreach ($article['tags'] as $k => $tag_arr)
		{	
			$meta_keywords .= $tag_arr['tag'] . ', ';
		}
		$meta_keywords = substr($meta_keywords, 0, -2);
		
		//	get categories
		$categories = art_get_categories();

		$uniq_id .= $article['id'];
		//	remove delimiter
		$article['content'] = @preg_replace('/<hr>/', '', $article['content'], 1);
		
		//	build direct link to article
		$article['link'] = art_make_link('article', array('id' => $article['id'], 'title' => $article['title']));
		
		//	convert timestamp to date
		$article['date'] = date('M j, Y', $article['date']);
		
		//	handle article categories - make links
		$pieces = explode(',', $article['category']);
		foreach ($pieces as $k => $cat_id)
		{
			$name = $categories[$cat_id]['name'];
			
			$article['pretty_cats'][$name] = $categories[$cat_id]['link'];
		} 
		
		//	handle comments
		if ($article['allow_comments'] == 1)
		{
			$show_comments = show_comments($uniq_id, $userdata['power']);
		}
		
		art_update_view_count($article['id']); // @since 1.6.5
		
		//	get related articles based on the first category
		$related_articles = array();
		$sql = "SELECT id 
				FROM art_articles 
				WHERE status = '1' 
				  AND (category LIKE '". $article['category'] ."' 
				   OR category LIKE '". $article['category'] .",%' 
				   OR category LIKE '%,". $article['category'] ."' 
				   OR category LIKE '%,". $article['category'] .",%') 
				  AND id != '". $article['id'] ."'";
		$result =  mysql_query($sql) or die(mysql_error());
		$ids = array();
		while($row = @mysql_fetch_assoc($result))
		{
			$ids[] = (int) $row['id'];
		}
		$related_articles = art_load_articles(0, 4, '', 0, $ids);

		if ($related_articles['type'] != '')
		{
			$related_articles = $related_articles['msg'];
		}
	}
	else
	{
		$article = $result['msg'];
	}
}
else 
{
	header("Location: ". _URL ."/article.php");
	exit();
}


if(isset($_COOKIE[COOKIE_AUTHOR]) && $_COOKIE[COOKIE_AUTHOR] != '')
	$smarty->assign('guestname', str_replace( array('"', '>', '<'), "", $_COOKIE[COOKIE_AUTHOR]) );
else
	$smarty->assign('guestname', '');

$must_sign_in = sprintf($lang['must_sign_in'], _URL."/login."._FEXT, _URL."/register."._FEXT);

$twitter_status  = '';
$twitter_status  = sprintf($lang['detail_title'], $video['video_title'], $video['artist']); 
$twitter_status .= ' '. $article['link'];
$twitter_status  = urlencode($twitter_status);

//	Some HTML 
$video_categories = list_categories(0, '');
$article_categories = art_html_list_categories($article['category'], $categories);
if($config['show_tags'] == 1)
{
	$tag_cloud = art_tag_cloud(0, $config['tag_cloud_limit'], $config['shuffle_tags']);
	$smarty->assign('tags', $tag_cloud);
	$smarty->assign('show_tags', 1);
}

// Facebook Like
if ($config['show_fb_like'] == '1')
{
	$smarty->assign('facebook_like_href', urlencode($article['link']));
}
// Facebook image src
if (is_array($article) && $article['meta']['post_thumb_show'] != '')
{
	$facebook_image_src =  _ARTICLE_ATTACH_DIR . $article['meta']['post_thumb_show'];
	$smarty->assign('facebook_image_src', $facebook_image_src);
}
$smarty->assign('uniq_id', $uniq_id); 
$smarty->assign('article', $article);
$smarty->assign('related_articles', $related_articles); 
$smarty->assign('show_comments', $show_comments);

$smarty->assign('must_sign_in', $must_sign_in);
$smarty->assign('categories', $categories);	 
$smarty->assign('article_categories', $article_categories); 
$smarty->assign('list_categories', $video_categories); 
$smarty->assign('guests_can_comment', $config['guests_can_comment']);
$smarty->assign('user_id', $userdata['id']);

// --- DEFAULT SYSTEM FILES - DO NOT REMOVE --- //
$smarty->assign('meta_title', $meta_title);
$smarty->assign('meta_keywords', $meta_keywords);
$smarty->assign('meta_description', $meta_description);
$smarty->assign('template_dir', $template_f);
$smarty->display('article_read.tpl');
?>