<?php
include ("inc/config.inc.php");
include (DIR_FUNCTIONS . "generic-functions.php");
include (DIR_FUNCTIONS . "formatting.php");
include (DIR_INCLUDES . "definitions.inc.php");

/* Gelen Full Addresi alır... */
$refer = $_SERVER["HTTP_REFERER"];


/* Gelen Host Addresi alır... */
$host = $_SERVER["HTTP_HOST"];

$basename = "http://$host/";
$adres = str_replace($basename, "", $refer);

$file = basename ($refer);

// tarihli bir klasor altından print mi alınıyor kontrolü , LD
if (is_numeric(substr($adres, 0, 4))) {
	$tarih = str_replace("/", "-", substr($adres, 0, 10) ); 
	// ilk uc bilgi tarih bilgisi olmalı..
	// $arr_tarih = explode("-" , $tarih);
	// $tmp_tarih = $arr_tarih[0] . "-" . $arr_tarih[1] . "-" . $arr_tarih[2]; 	
	

	if (is_date($tarih)) {
		$bugun = $tarih;
		$adres = substr($adres, 11);
	}
}

/* Gelen Full Addresten sadece dosya ismini alır... */
$d_data = explode("-", substr($file, 3, -5));
$content = substr($file, 0, 3);


if (!in_array($content, $arrBolumler)){
	echo "<br><br> LINK HATASI <br><br>";
	print_r($HTTP_GET_VARS);
	exit;
}

$sira = $d_data[0];



$bolum_kodu = array_search($content, $arrBolumler);
$bolum_name = $bolumler_lowerlist[$bolum_kodu];

$TPL_bolum_nm = $bolumler_tr_list[$bolum_kodu];
$cts = new ChangeCase();
$TPL_bolum_name = $cts->tr_ChangeCase($TPL_bolum_nm, true, false);


switch ($content) { 
	case "yaz":
	     	$yazar_rs = get_row("Select yazar_name from yazarlar where yazar_id = '$d_data[0]'");
	     	$yazar_name = $yazar_rs["yazar_name"];
		if ($d_data[3])
			$bugun = substr($d_data[3], 0, 4) . "-" . substr($d_data[3], 4, 2) . "-" .substr($d_data[3], 6, 2);
       	break;
	case "son":
		$bolum_kodu = array_search(substr($sira, 1, 3), $arrBolumler);
		$sira = substr($sira, 4);
		if ($d_data[1])
			$bugun = substr($d_data[1], 0, 4) . "-" . substr($d_data[1], 4, 2) . "-" .substr($d_data[1], 6, 2);
	break;
   	default:
		if ($d_data[1])
			$bugun = substr($d_data[1], 0, 4) . "-" . substr($d_data[1], 4, 2) . "-" .substr($d_data[1], 6, 2);
}

if ($bugun){
	$arr_gun = explode ("-", $bugun);
	$im_folder = "/$arr_gun[0]/$arr_gun[1]/$arr_gun[2]/cm/im/";
}


//echo "$bugun<br>";
$arr_lnk_gun = explode ("-", $bugun);
$lnk_folder = "$arr_lnk_gun[0]/$arr_lnk_gun[1]/$arr_lnk_gun[2]/";
//echo "$basename - $lnk_folder - $adres<br>";
$link = "$basename$lnk_folder$adres";
$link = str_replace("/cp/", "/cpsabah/", $link);


// $link sayfanın en altında gözüken link....

if (preg_match("/-(\d{3})\.html/",$link,$match)) {
	$yayin_kodu = $match[1];
}


switch ($content) { 
	case "yaz" && $yazar_cesit:
		$sql_query = "select haber_id, parent_id  from tbl_records where yayin_tarihi='$bugun'"
	     	. " and yayin='$yayin_kodu' and yazar_id = '$yazar_id' and tip!=10 and tip!=11 and parent_id = '' order by sira";
       	break;
	case "yaz":
		$sql_query = "select haber_id, parent_id from tbl_records where yayin_tarihi='$bugun'"
	     	. " and yayin='$yayin_kodu' and tip!=10 and tip!=11 and bolum='$d_data[1]' and sira = '$d_data[2]' and yazar_id = '$d_data[0]'";
       	break;
	case "son":
		$sql_query = "select haber_id from tbl_sondakika where yayin_tarihi='$bugun'"
	     	. " and yayin='$yayin_kodu' and bolum='$bolum_kodu' and sira = $sira ";
	break;
   	default: 
		$sql_query = "select haber_id, parent_id from tbl_records where yayin_tarihi='$bugun'"
	     	. " and yayin='$yayin_kodu' and bolum='$bolum_kodu' and sira = $sira ";
}


#Şu anki saat...
$TPL_tarih = set_bugun($bugun);
$cur_hour = date("H");
$hangi_tarih = convDate($bugun);

$result1 = get_row($sql_query);
//echo $sql_query; exit;

if (!$result1){
	echo "~error~ Haber.php - Database hatası...";
	$num_rows = 0;
} else {
	$haber_id = $result1["haber_id"];
	$parent_id = $result1["parent_id"];
	$num_rows = 1;
}

//Buraya, haber sayısı 0 ise 404 sayfasını getir...
if ($num_rows <= 0){
	echo "<script language=\"javascript\">location.href='templates/template_404.php.html'</script>";
	exit;
}

//Eğer ilişkili haberleri varsa onlarıda getir...
if ($haber_id){
	$next_q_h = " WHERE haber_id = '$haber_id' Or parent_id = '$haber_id'";
} 
if ($parent_id && strlen($parent_id)>11){
	if ($haber_id){
		$next_q_p = " or haber_id = '$parent_id' Or parent_id = '$parent_id'";
	} else {
		$next_q_p = " WHERE haber_id = '$parent_id' Or parent_id = '$parent_id'";
	}
}


if ($content == "son")
	$next_query = "Select haber_id, baslik, spot, haber From tbl_sondakika WHERE haber_id = '$haber_id'";	
else
	$next_query = "Select haber_id, parent_id, baslik, spot, haber, yazar_id, tip From tbl_records $next_q_h $next_q_p Order By sira";

//echo "$next_query<br>";

$result = app_db_query($next_query);
$TPL_hepsi = "";
$count = 0;
while ($row = app_db_fetch_array($result)) {
	extract($row, EXTR_OVERWRITE);
	$haber_resim_id = $haber_id;
	$sbh_title = "SABAH - Çıkış Al";
	
	//Eğer resim varsa getir...
	$sql_query3 = "select image_path, caption from tbl_resim where haber_id='$haber_id' order by sira";
	$resimler = get_images($sql_query3, $bugun, "b");
	$img_count = count($resimler);
	
	if ($img_count > 0) {
		$res_file_path = $resimler[0];
		$res_file_path = "
		<table border=\"0\" align=\"left\" cellpadding=\"0\" cellspacing=\"0\">
		<tr>
		<td valign=\"top\"><img src=\"$im_folder$res_file_path\" height=\"160\" border=\"0\"></td>
		<td width=\"7\" valign=\"top\"><img src=\"i/1_pix_trans.gif\" width=\"7\" height=\"1\" border=\"0\"></td>
		</tr>
		<tr valign=\"top\"> 
		<td height=\"7\" colspan=\"2\"><img src=\"i/1_pix_trans.gif\" width=\"1\" height=\"7\" border=\"0\"></td>
		</tr>
		</table>";
	} else {
		$res_file_path = "";
	}
	
	$print_baslik = "<div align=\"center\"><font size=\"3\"><b>$baslik</b></font></div><br>";

	#Eğer Spot boş değilse <br> o zaman konacak...
	if ($spot)
		$print_spot = "<b>$spot</b><br><br>";

	$print_haber = str_replace("***", "<div align=\"center\">***</div>", $haber);
	$print_haber = "$res_file_path<span class=\"PrintHaber\">$print_haber</span><br><br>";
	$TPL_hepsi .= $print_baslik . $print_spot . $print_haber;
$count++;
}

#Eğer tip 8,9,10,11 ise yazardır ve yazar adı getirilir...
if ($tip == 8 || $tip == 9 || $tip == 10 || $tip == 11) {
	$yazar_rs = get_row("Select yazar_name from yazarlar where yazar_id = '$yazar_id'");
	$yazar_name = $yazar_rs["yazar_name"];
}

#Eğer haber, Anasayfa haberinin devamı ise ana sayfadaki başlık, spot ve haber üst tarafa getirilecek...
$sql_query_anasayfa = "select baslik, spot, haber from tbl_records where yayin_tarihi='$bugun'" .
     	" and yayin='$yayin_kodu' and bolum='1' and manset_haber_id = '$haber_id'";
//echo $sql_query_anasayfa;
$result_anasayfa = app_db_query($sql_query_anasayfa);
if ($result_anasayfa)
	$num_rows_anasayfa = app_db_num_rows($result_anasayfa);

if ($num_rows_anasayfa > 0) {
	$row_anasayfa = app_db_fetch_array($result_anasayfa);
	$TPL_anasayfa = "";
	if (!($yazar_id > 0 AND ($tip==8 || $tip==10))){
		$TPL_anasayfa .= "<div align=\"center\"><font size=\"3\"><b>" . $row_anasayfa["baslik"] . "</b></font></div><br>";
	}
	else {
		$TPL_anasayfa .= "<div align=\"center\"><font size=\"3\"><b>" . $row_anasayfa["baslik"] . "</b></font></div><br>";
	}

	if ($row_anasayfa["spot"])
		$TPL_anasayfa .= "<b>" . $row_anasayfa["spot"] . "</b><br><br>";
		
	if ($row_anasayfa["haber"])
		$anasayfa_haber = "<span class=\"PrintHaber\">" . $row_anasayfa["haber"] . "</span><br><br>";
		$TPL_anasayfa .= str_replace("***", "<div align=\"center\">***</div>", $anasayfa_haber);

	$TPL_anasayfa .= "<div align=\"center\">***</div><br>";
} else {
	$TPL_anasayfa = "";
}

$TPL_hepsi = $TPL_anasayfa . $TPL_hepsi;	

include ("templates/template_print.php.html");
?>
