<?php 
defined('IN_PHPCMS') or exit('Access Denied');
$itemid = $articleid = isset($articleid) ? intval($articleid) : 0;
if(!$articleid) return FALSE;
$tablename = channel_table('article', $channelid);
$article = $db->get_one("SELECT * FROM $tablename WHERE articleid=$itemid");
if(!$article || $article['islink'] || !$article['ishtml'] || $article['status'] != 3) return FALSE;
extract($article);
$r = $db->get_one("SELECT content FROM ".channel_table('article_data', $channelid)." WHERE articleid=$itemid");
$content = $r['content'];
unset($article, $r);

if($MOD['enable_reword']) $content = reword($content);
if($MOD['enable_keylink']) $content = keylink($content);

global $myfields;
if(!$myfields) $myfields = cache_read($tablename.'_fields.php');
$fields = array();
if(is_array($myfields))
{
	foreach($myfields as $k=>$v)
	{
		$myfield = $v['name'];
		$fields[] = array('title'=>$v['title'],'value'=>$$myfield);
	}
}
$CAT = cache_read('category_'.$catid.'.php');
$enableprotect = $CAT['enableprotect'];

$head['title'] = $title.'-'.$CHA['channelname'];
$head['keywords'] = $keywords.($keywords ? ',' : '').$CAT['seo_keywords'].','.$CHA['seo_keywords'].','.$CHA['channelname'];
$head['description'] = str_cut(strip_tags(trim($introduce)), 100);

if($copyfrom)
{
	$copyfrom = explode('|', $copyfrom);
	$copyfromname = $copyfrom[0];
	$copyfromurl = $copyfrom[1];
}
else
{
	$copyfromname = $LANG['internet'];
	$copyfromurl = '#';
}
unset($copyfrom);
if($keywords) $keys = explode(',', $keywords);
$itemurl = linkurl($linkurl, 1);
if($titleintact) $title = $titleintact;
$img_maxwidth = isset($MOD['img_maxwidth']) ? $MOD['img_maxwidth'] : 550;
$adddate = date('Y-m-d H:i:s', $addtime);
$position = catpos($catid);
$cat_name = $CAT['catname'];
$cat_url = $CAT['linkurl'];

if(!$skinid) $skinid = $CAT['defaultitemskin'];
if($skinid) $skindir = PHPCMS_PATH.'templates/'.$CONFIG['defaulttemplate'].'/skins/'.$skinid;
if(!$templateid) $templateid = $CAT['defaultitemtemplate'] ? $CAT['defaultitemtemplate'] : 'content';

$page = 0;
$pages = '';
$pagenumber = 0;

if($paginationtype==1)
{
	$charnumber = strlen($content);
	if($charnumber > $maxcharperpage)
	{
		$pagenumber = ceil($charnumber/$maxcharperpage);
		$contents = $content;
	}
}
elseif($paginationtype==2)
{
	if(strpos($content, '[next]'))
	{
		$contents = explode('[next]', $content);
		$pagenumber = count($contents);
	}
}

if($pagenumber > 1)
{
	for($i = 0; $i < $pagenumber; $i++)
	{
		$page = $i+1;
		if($paginationtype==1)
		{
			$offset = $i*$maxcharperpage;
			$content = get_substr($contents, $offset, $maxcharperpage);
		}
		elseif($paginationtype==2)
		{
			$content = $contents[$i];
		}
		ob_start();
		$pages = articlepage($catid, $ishtml, $urlruleid, $htmldir, $prefix, $articleid, $addtime, $pagenumber, $page);
		include template($mod, $templateid);
		$data = ob_get_contents();
		ob_clean();
		$filename = PHPCMS_ROOT.'/'.item_url('path', $catid, $ishtml, $urlruleid, $htmldir, $prefix, $articleid, $addtime, $page);
		$filepath = dirname($filename);
		is_dir($filepath) or dir_create($filepath);
		file_put_contents($filename,$data);
		if($page == 1)
		{
			$indexname = PHPCMS_ROOT.'/'.item_url('path', $catid, $ishtml, $urlruleid, $htmldir, $prefix, $articleid, $addtime, 0);
			copy($filename, $indexname);
			chmod($indexname, 0777);
		}
	}
}
else
{
	ob_start();
	include template($mod, $templateid);
	$data = ob_get_contents();
	ob_clean();
	$filename = PHPCMS_ROOT.'/'.item_url('path', $catid, $ishtml, $urlruleid, $htmldir, $prefix, $articleid, $addtime, 0);
	$filepath = dirname($filename);
	is_dir($filepath) or dir_create($filepath);
	file_put_contents($filename, $data);
	chmod($filename, 0777);
}
return TRUE;
?>