<?
extract($_REQUEST);
$work_path = dirname(__FILE__);
chdir($work_path);
chdir('..');
$root_path = getcwd();
chdir('..');
include('admin/config.inc.php');
include('admin/mysql.class.php');
$my = new MySQL();
include('admin/types.class.php');
$types = new Types();
include('admin/chapters.class.php');
$chapter = new Chapters();
include('admin/infochapters.class.php');
$infochapter = new Infochapters();
include('admin/functions.php');
//$lang_dir = '/www';
if(preg_match('|[^A-z0-9_-]|',$table)) $table= 'materials';

//require($root_path.'/_ab/_zones.htm');

echo WriteLine($table,false,(int)$tid,(int)$cid,(int)$page);

function WriteLine($table,$by_date=false,$type=10,$chid=0,$page=1)	{	//it's for lenta
	global $root_path,$my,$chapter,$types,$lang_dir;
		if(empty($table)) $table = 'materials';
		$table = mysql_escape_string($table);
		//if(empty($type)) $type = 10;
		$typedir = $types->typetree[$type]->dir;

		$curlima = $chapter->GetLimits($chid?$chid:($type?$typedir:$table).'l');
//		$curlima = $chapter->GetLimits('newsl');
		$limit = $curlima['value']['ARTICLE_LIMIT'];
		if(empty($limit)) $limit = 10;
//$limit = 5;	//DEBUG!!!
		$where = 'type='.(int)$type.' AND m.date<NOW() AND m.tosite=1'; 
		if($chid) $where .= ' AND m.chapter='.(int)$chid;
		if($by_date) $where .= ' AND m.date LIKE ("'.substr($by_date,0,10).'%")';
		list($skoka) = $my->FetchRow('SELECT count(*) as skoka FROM '.$table.' m WHERE '.$where);
		$page = empty($page)?1:(int)$page;
		$navbar = '';
		$html_full  = _geTempl((($type?$typedir:$table).'ll'),'htm');
//		$html_full  = _geTempl('newsl','htm');
		$_html = _geTempl(($type?$typedir:$table).'ll');
//		$_html = _geTempl('newsl');
		if($skoka>$limit) $navbar = navigate($skoka,$limit,$page,'/line/?tid='.(int)$type.'&cid='.(int)$chid.'&');
		$qu = 'SELECT m.*,t.dir as t_dir,t.name as t_name,c.dir as c_dir,c.name as c_name,v.views as views,k.title as k_name,k.img as k_img FROM '.$table.' m LEFT JOIN types t ON t.id=m.type LEFT JOIN chapters c ON c.id=m.chapter LEFT JOIN views v ON (v.id_news=m.id AND tablo="'.$table.'") LEFT JOIN columnist k ON k.id=m.columnist WHERE '.$where.' ORDER BY m.date DESC'.($by_date?false:' LIMIT '.(($page-1)*$limit).','.$limit);
//echo "<!-- $qu -->";
		$my->ExecQuery($qu);
		while ($res=$my->FetchObject()) {
			$html = trim($_html);
			$html = str_replace("<?=ITEM_ID?>",$res->id,$html);
			$img = $res->type==13 ? $res->k_img : $res->img;
			if($img && file_exists($root_path.($res->type==13?'/columnist':'').'/imgthumb/'.$img)) {
				$_img = _geTempl('image');
				//$size = getimagesize($root_path.($res->type==13?'/columnist':'').'/imgthumb/'.$img);
				$_img = 
					str_replace('<?=IMAGE_NAME?>',($res->type==13?'/columnist':'').'/imgthumb/'.$img,
					str_replace('<?=IMAGE_SIZE?>','width="68"',
					str_replace('<?=IMAGE_ALT?>',htmlspecialchars(strip_tags($res->type==13?$res->k_name:$res->foto_alt),ENT_QUOTES,'cp1251'),
					str_replace('<?=IMAGE_CLASS?>','img',
					$_img))));
			}else {
				$_img = '';
				$html = str_replace('pic','is_hidden',$html);
			}
			$html = str_replace("<?=ITEM_IMAGE?>",$_img,$html);
			$html = str_replace("<?=ITEM_TIME?>",_PrTime($res->date),$html);
			$html = str_replace("<?=ITEM_DATE?>",_PrDate($res->date,'d.m.Y'),$html);
			$html = str_replace("<?=ITEM_AUTHOR?>",($res->k_name?_doformatInfo($res->k_name):$res->author),$html);
			$html = str_replace("<?=ITEM_LINK?>",'/'.$res->t_dir.'/'.$res->c_dir._GetUrl($res->date),$html);
			$html = str_replace("<?=ITEM_TITLE?>",strip_tags($res->title),$html);
			$html = str_replace("<?=ITEM_SUBTITLE?>",strip_tags($res->subtitle),$html);
			$html = str_replace("<?=ITEM_ANONS?>",truncate(strip_tags($res->content),200),$html);
			$html = str_replace("<?=ITEM_TAGS?>",getTags($res->id),$html);
			$html = str_replace("<?=ITEM_COMMENTS_ID?>",$res->comments_id,$html); 			
			$html = str_replace("<?=ITEM_VIEWS?>",$res->views,$html);

			if($ivideo=_checkAdds($res->id,'video')) {
				$html = str_replace('<?=VIDEO_LINK?>',$ivideo[0],$html);
				$html = str_replace('<?=VIDEO_TITLE?>',$ivideo[1],$html);
			}else {
				$html = str_replace('<?=VIDEO_LINK?>','',$html);
				$html = str_replace('<?=VIDEO_TITLE?>','',$html);
				$html = str_replace('is_video','is_hidden',$html);
			}
			if($iaudio=_checkAdds($res->id,'audio')) {
				$html = str_replace('<?=AUDIO_LINK?>',$iaudio[0],$html);
				$html = str_replace('<?=AUDIO_TITLE?>',$iaudio[1],$html);
			}else {
				$html = str_replace('<?=AUDIO_LINK?>','',$html);
				$html = str_replace('<?=AUDIO_TITLE?>','',$html);
				$html = str_replace('is_audio','is_hidden',$html);
			}
			if($iphoto=_checkAdds($res->id,'foto')) {
				$html = str_replace('<?=PHOTO_LINK?>',$iphoto[0],$html);
				$html = str_replace('<?=PHOTO_TITLE?>',$iphoto[1],$html);
			}else {
				$html = str_replace('<?=PHOTO_LINK?>','',$html);
				$html = str_replace('<?=PHOTO_TITLE?>','',$html);
				$html = str_replace('is_photo','is_hidden',$html);
			}

			$html_all .= $html;
		}

		$head = _geTempl('head');
		$data = 
			str_replace("<?=CHAPTER_NAME?>",$types->typetree[$type]->name.($chid?' » '.$chapter->chaptree[$chid]->name:''),
			str_replace("<?=ITEMS?>",$html_all,
			str_replace("<?=NAVBAR?>",$navbar,
			$html_full)));
//		ob_start();
//		@include($root_path.$lang_dir.'/_ukrnet/links.php');
//		$links = ob_get_clean();
		$data = str_replace('<!--#include virtual="/_ukrnet/links.php"-->',$links,$data);
//		ob_start();
//		@include($root_path.$lang_dir.'/_ukrnet/joblinks.php');
//		$joblinks = ob_get_clean();
		$data = str_replace('<!--#include virtual="/_ukrnet/joblinks.php"-->',$joblinks,$data);
		$foot = _geTempl('foot');
//		ob_start();
//		@include($root_path.$lang_dir.'/_links.php');
//		$_links = ob_get_clean();
		$foot = str_replace('<!--#include virtual="/_links.php" -->',$_links,$foot);

		return replInc($head.$data.$foot);
	}
function _doformatInfo($text) {
	global $infochapter;
	$sql=new mysql;
	$sql->ExecQuery("SELECT * FROM info");
	while($res=$sql->FetchObject()){
		if(!empty($res->aliases)) $atitle = explode(',',$res->aliases);
		$atitle[] = $res->title;
		foreach($atitle as $kk=>$title) {
			if(!empty($title)) {
				$title = trim($title);
				if(strstr($text,$title)&&!strstr($text,">".$title."</a>")) {
					$text=eregi_replace($title,'<a href="/info/'.$infochapter->chaptree[$res->chapter]->dir.'/'.translIt($res->title).'.html">'.$title.'</a>',$text);
				}
			}
		}
	}
	return $text;
}

?>