<?php
include 'function.php';
$this_pg = 'press.php';
$html_source = 'press_base.html';
$info_source = 'press2_base.html';
$index_page = $tophtml_array['top'];

mb_language("Japanese");
mb_internal_encoding("EUC-JP");

$now = time();

// MySQL接続
$con = pbof_mysql_connect();	//2017.11.07 IL 追加 サーバ移行対応

// HTMLセット
$html = file_get_contents($html_source);
$html = mb_convert_encoding($html, $enc_in, $enc_source);

if($_GET){
	foreach ($_GET as $key => $value){
		$value = text_replace($value);
		$value = mb_convert_kana($value, "sKV", "EUC-JP");
		$value = mysqli_escape_string($con, $value);
		$_GET[$key] = $value;
	}
}
$c = $_POST["c"];
if($c==''){
	$c = $_GET["c"];
}
$c = intval($c);

$d = $_POST["d"];
if($d==''){
	$d = $_GET["d"];
}
$d = intval($d);

if(!$c && !$d) {
	header("Location: {$index_page}");
	exit;
}

if ($d) {
	$UA = $_SERVER['HTTP_USER_AGENT'];
//	if (ereg("iPhone|iPod|Android.*Mobile|Windows.*Phone", $UA)) {
	if (preg_match("iPhone|iPod|Android.*Mobile|Windows.*Phone", $UA)) {
		// スマートフォン
		header("Location: ../sp/news/index.php?d={$d}");
		exit;
	}
}

// カテゴリリスト
$category_name_array = array();
$sql = "select mid, name from scj_category order by sid";
@mb_convert_variables($enc_db, $enc_in, $sql);
$rst = mysqli_query($con, $sql);
while ($col = mysqli_fetch_array($rst)) {
	@mb_convert_variables($enc_in, $enc_db, $col);
	$category_name_array[$col['mid']] = $col['name'];
}
mysqli_free_result($rst);

if ($d) {
	$sql = "select scj_blog.mid, scj_blog.cate, scj_blog.title, scj_blog.main, scj_blog.html1, scj_blog.html2, scj_category.grp, scj_blog.vdate from scj_blog inner join scj_category on scj_blog.cate = scj_category.mid where scj_blog.view = '1' and scj_blog.mid ='{$d}' and scj_blog.vdate <= '{$now}'";
	@mb_convert_variables($enc_db, $enc_in, $sql);
	$rst = mysqli_query($con, $sql);
	$col = mysqli_fetch_array($rst);
	@mb_convert_variables($enc_in, $enc_db, $col);
	if (!$col) {
		header("Location: {$index_page}");
		exit;
	}
	mysqli_free_result($rst);
	
	// EVERY ONEならリダイレクト
	if ($category_name_array[$col['cate']] == 'EVERY ONE') {
		header("Location: everyone.php");
		exit;
	}
	// プレスルームandインフォメーション以外ならリダイレクト
	if ($category_name_array[$col['cate']] != 'プレスルーム' && $col['grp']!=5) {
		header("Location: sc_activity.php");
		exit;
	}
	// 人材募集かお知らせならHTML再読み込み
	if ($col['cate'] == 31 || $col['cate'] == 32) {
		// HTML再セット
		$html = file_get_contents($info_source);
		$html = mb_convert_encoding($html, $enc_in, $enc_source);
		$cate_sql = "and ( scj_blog.cate = '31' or scj_blog.cate = '32' ) ";
	} else {
		$cate_sql = "and ( scj_blog.cate != '31' and scj_blog.cate != '32' ) ";
	}
	
	// エントリーリスト
	$entry_list = '';
	$year = '';
	if ($col['grp']==5) {
		//$sql = "select scj_blog.mid, scj_blog.cate, scj_blog.title, scj_blog.vdate, case when month(from_unixtime(scj_blog.vdate)) > 3 then year(from_unixtime(scj_blog.vdate)) else year(from_unixtime(scj_blog.vdate))-1 end as year from scj_blog inner join scj_category on scj_blog.cate = scj_category.mid where scj_blog.view = '1' and scj_blog.vdate <= '{$now}' and scj_blog.cateindex != '1' and scj_category.grp = '{$col['grp']}' {$cate_sql}order by scj_blog.vdate desc";
		$sql = "select scj_blog.mid, scj_blog.cate, scj_blog.title, scj_blog.vdate, date_format(from_unixtime(scj_blog.vdate), '%Y') as year from scj_blog inner join scj_category on scj_blog.cate = scj_category.mid where scj_blog.view = '1' and scj_blog.vdate <= '{$now}' and scj_blog.cateindex != '1' and scj_category.grp = '{$col['grp']}' {$cate_sql}order by scj_blog.vdate desc";
	} else {
		//$sql = "select scj_blog.mid, scj_blog.cate, scj_blog.title, scj_blog.vdate, case when month(from_unixtime(scj_blog.vdate)) > 3 then year(from_unixtime(scj_blog.vdate)) else year(from_unixtime(scj_blog.vdate))-1 end as year from scj_blog inner join scj_category on scj_blog.cate = scj_category.mid where scj_blog.view = '1' and scj_blog.vdate <= '{$now}' and scj_blog.cateindex != '1' and scj_blog.cate = '{$col['cate']}' order by scj_blog.vdate desc";
		$sql = "select scj_blog.mid, scj_blog.cate, scj_blog.title, scj_blog.vdate, date_format(from_unixtime(scj_blog.vdate), '%Y') as year from scj_blog inner join scj_category on scj_blog.cate = scj_category.mid where scj_blog.view = '1' and scj_blog.vdate <= '{$now}' and scj_blog.cateindex != '1' and scj_blog.cate = '{$col['cate']}' order by scj_blog.vdate desc";
	}
	@mb_convert_variables($enc_db, $enc_in, $sql);
	$rst = mysqli_query($con, $sql);
	$cnt = 0;
	while ($cols = mysqli_fetch_array($rst)) {
		if ($year=='') {
			$year = $cols['year'];
			$cnt++;
			$entry_list .= "<li><a href=\"#{$cnt}\">■".$year."年度</a>\n<ul class=\"fxmn\">\n";
		} elseif ($year != $cols['year']) {
			$year = $cols['year'];
			$cnt++;
			$entry_list .= "</ul>\n</li>\n";
			$entry_list .= "<li><a href=\"#{$cnt}\">■".$year."年度</a>\n<ul class=\"fxmn\">\n";
		}
		@mb_convert_variables($enc_in, $enc_db, $cols);
		$entry_list .= '<li><a href="'.$this_pg.'?d='.$cols['mid'].'">'.$cols['title'].'</a></li>'."\n";
	}
	if ($entry_list) $entry_list .= "</ul>\n</li>\n";
	mysqli_free_result($rst);
	// エントリーリスト扉ページ
	$ientry_list = '';
	$sql = "select * from scj_blog where view = '1' and cateindex = '1' and cate = '{$col['cate']}' and scj_blog.vdate <= '{$now}' limit 1";
	@mb_convert_variables($enc_db, $enc_in, $sql);
	$rst = mysqli_query($con, $sql);
	$cols = mysqli_fetch_array($rst);
	if ($cols) {
		@mb_convert_variables($enc_in, $enc_db, $cols);
		$ientry_list .= '<li><a href="'.$this_pg.'?d='.$cols['mid'].'">'.$cols['title'].'</a></li>'."\n";
	}
	mysqli_free_result($rst);
} else {
	// EVERY ONEならリダイレクト
	if ($category_name_array[$c] == 'EVERY ONE') {
		header("Location: everyone.php");
		exit;
	}
	// プレスルームandインフォメーション以外ならリダイレクト
	$sql = "select * from scj_category where cate = '{$c}' limit 1";
	@mb_convert_variables($enc_db, $enc_in, $sql);
	$rst = mysqli_query($con, $sql);
	$col = mysqli_fetch_array($rst);
	@mb_convert_variables($enc_in, $enc_db, $col);
	if (!$col) {
		header("Location: {$index_page}");
		exit;
	}
	mysqli_free_result($rst);
	if ($category_name_array[$c] != 'プレスルーム' && $col['grp'] != 5) {
		header("Location: sc_activity.php");
		exit;
	}
	$sql = "select * from scj_blog where view = '1' and cate = '{$c}' and scj_blog.vdate <= '{$now}' order by vdate desc limit 1";
	@mb_convert_variables($enc_db, $enc_in, $sql);
	$rst = mysqli_query($con, $sql);
	$col = mysqli_fetch_array($rst);
	@mb_convert_variables($enc_in, $enc_db, $col);
	if (!$col) {
		header("Location: {$index_page}");
		exit;
	}
	mysqli_free_result($rst);
	
	// エントリーリスト
	$entry_list = '';
	$year = '';
	$sql = "select mid, cate, title, vdate, date_format(from_unixtime(vdate), '%Y') as year from scj_blog where view = '1' and scj_blog.vdate <= '{$now}' and cateindex != '1' and cate = '{$c}' order by vdate desc";
	@mb_convert_variables($enc_db, $enc_in, $sql);
	$rst = mysqli_query($con, $sql);
	$cnt = 0;
	while ($cols = mysqli_fetch_array($rst)) {
		if ($year=='') {
			$year = $cols['year'];
			$cnt++;
			$entry_list .= "<li><a href=\"#{$cnt}\">■".$year."年度</a>\n<ul class=\"fxmn\">\n";
		} elseif ($year != $col['year']) {
			$year = $cols['year'];
			$cnt++;
			$entry_list .= "</ul>\n</li>\n";
			$entry_list .= "<li><a href=\"#{$cnt}\">■".$year."年度</a>\n<ul class=\"fxmn\">\n";
		}
		@mb_convert_variables($enc_in, $enc_db, $cols);
		$entry_list .= '<li><a href="'.$this_pg.'?d='.$cols['mid'].'">'.$cols['title'].'</a></li>'."\n";
	}
	if ($entry_list) $entry_list .= "</ul>\n</li>\n";
	mysqli_free_result($rst);
	// エントリーリスト扉ページ
	$ientry_list = '';
	$sql = "select * from scj_blog where view = '1' and cateindex = '1' and cate = '{$c}' and vdate <= '{$now}' limit 1";
	@mb_convert_variables($enc_db, $enc_in, $sql);
	$rst = mysqli_query($con, $sql);
	$cols = mysqli_fetch_array($rst);
	if ($cols) {
		@mb_convert_variables($enc_in, $enc_db, $cols);
		$ientry_list .= '<li><a href="'.$this_pg.'?d='.$cols['mid'].'">'.$cols['title'].'</a></li>'."\n";
	}
	mysqli_free_result($rst);
}

// 2010以前
//$entry_list .= '<li><a href="http://www.savechildren.or.jp/news/2010.html">■2010年度</a></li>';
//$entry_list .= '<li><a href="http://www.savechildren.or.jp/news/2009.html">■2009年度</a></li>';

// データセット
$html = preg_replace('/<-list->/', $entry_list, $html);
if ($ientry_list) {
	$html = preg_replace('/<-cateindex->/', $ientry_list, $html);
} else {
	$html = preg_replace('/<!--cateindex_start-->.*?<!--cateindex_end-->/s', '', $html);
}
$html = preg_replace('/<-title->/', $col['title'], $html);

function zoomimg_p($matches) {
	if (preg_match('/nozoom/', $matches[4])) return $matches[0];
	$path = $matches[1].$matches[2].$matches[3];
	$path = str_replace('&amp;', '&', $path);
	preg_match('/scale=([0-9]+)/', $path, $reg);
	$scale = $reg[1];
	if (!$scale) $scale = 130;
	$size = getimagesize($matches[2]);
	if ($size[0] > $size[1]) {
		$w = $scale;
		$h = floor($size[1] / ( $size[0] / $w ));
	} else {
		$h = $scale;
		$w = floor($size[0] / ( $size[1] / $h ));
	}
	$img = '<a href="'.$matches[2].'" class="zoom"><img src="blank.gif" width="'.$w.'" height="'.$h.'" class="zimg" border="0" style="background:url('.$path.') no-repeat;" /></a>';
	return $img;
}

$col['main'] = str_replace('src="http://www.savechildren.or.jp/scjcms/img.php', 'src="img.php', $col['main']);
$col['main'] = preg_replace('/<img\salt=".*?"/', '<img', $col['main']);
$col['main'] = preg_replace_callback('/<img\ssrc="(img\.php\?path=)([^"]*?\.jpg)([^"]*?)("[^\/]*?\/>)/', "zoomimg_p", $col['main']);
//$col['main'] = preg_replace_callback('/<img\ssrc="http:\/\/www.savechildren.or.jp\/scjcms\/(img\.php\?path=)([^"]*?\.jpg)([^"]*?)("[^\/]*?\/>)/', "zoomimg_p", $col['main']);
//$col['main'] = preg_replace('/<img\ssrc="img\.php\?path=([^"]*?\.jpg)[^"]*?"[^\/]*?\/>/', '<a href="\\1" class="zoom">\\0</a>', $col['main']);
//$col['main'] = preg_replace('/<img\ssrc="http:\/\/www.savechildren.or.jp\/scjcms\/img\.php\?path=([^"]*?\.jpg)[^"]*?"[^\/]*?\/>/', '<a href="\\1" class="zoom">\\0</a>', $col['main']);
// 追加HTMLセット
for ($i=1; $i<3; $i++) {
	$col["html".$i] = html_entity_decode($col["html".$i], ENT_QUOTES, 'EUC-JP');
	$col['main'] = preg_replace('/xxx'.$i.'/', $col['html'.$i], $col['main']);
}
$html = preg_replace('/<-main->/', $col['main'], $html);
$html = str_replace('<h2 class="entry-header">', '<h2 class="entry-header"><div class="entry" style="margin:0px; text-align:right; font-size:13px;">（公開日：'.date('Y.m.d', $col['vdate']).'）</div>', $html);
$html = preg_replace('/<-category_name->/', $category_name_array[$col['cate']], $html);
$html = preg_replace('/<-category_name_pan->/', '<a href="'.$this_pg.'?c='.$col['cate'].'">'.$category_name_array[$col['cate']].'</a>', $html);
if ($i = $leftmenu_array[$category_name_array[$col['cate']]]) {
	$html = preg_replace('/'.$i.'.gif/', $i.'-on.gif', $html);
}

// SSI 置き換え
$html = str_replace('<!--#include virtual="/ssi/taginc_head.html"-->', mb_convert_encoding(file_get_contents("../ssi/taginc_head.html"), $enc_in, $enc_source), $html);
$html = str_replace('<!--#include virtual="/ssi/header.html"-->', mb_convert_encoding(file_get_contents("../ssi/header.html"), $enc_in, $enc_source), $html);
$html = str_replace('<!--#include virtual="/ssi/gnavi_others.html"-->', mb_convert_encoding(file_get_contents("../ssi/gnavi_others.html"), $enc_in, $enc_source), $html);
$html = str_replace('<!--#include virtual="/ssi/footer.html"-->', mb_convert_encoding(file_get_contents("../ssi/footer.html"), $enc_in, $enc_source), $html);
$html = str_replace('<!--#include virtual="/ssi/taginc_body.html"-->', mb_convert_encoding(file_get_contents("../ssi/taginc_body.html"), $enc_in, $enc_source), $html);
$html = str_replace('<!--#include virtual="/ssi/support_1.html"-->', mb_convert_encoding(file_get_contents("../ssi/support_1.html"), $enc_in, $enc_source), $html);

		// --↓2024.09.12 追加
$html = str_replace('<!--#include virtual="/ssi/taginc_head_body.html"-->', mb_convert_encoding(file_get_contents("../ssi/taginc_head_body.html"), $enc_in, $enc_source), $html);

//↓2020.09.01 IL 追加 Canonicalタグ設置
$ar_PARAM = array();
$ar_PARAM["URL_MAIN"]   = G_SCJCMS_ROOT_URL . "press.php";
$w_CanonicalTag = pbsf_GenHTML_CanonicalTag($ar_PARAM);

$html = str_replace("<!--{v_CanonicalTag}-->", $w_CanonicalTag, $html);
//↑2020.09.01 IL 追加 Canonicalタグ設置

// HTML表示
header($html_header);
header('Content-Language: ja');
$html = mb_convert_encoding($html, $enc_out, $enc_in);

// facebook&twitter

//↓2018.01.25 IL PHPのワーニングがでることがあるため、記事タイトルのエンコード方法を変更。
//	$facebook = '<a title="facebookでシェアする" href="http://www.facebook.com/sharer.php?u=' . urlencode('http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]) . '&t=' . urlencode(mb_convert_encoding($col['title'], 'UTF-8', $enc_in)) . '" target="_blank"><img src="/common/img/fb.png" class="sns"></a>';
//	$twitter = '<a href="http://twitter.com/share?url=' . urlencode('http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]) . '&text=' . urlencode(mb_convert_encoding($col['title'], 'UTF-8', $enc_in)) . '" target="_blank"><img src="/common/img/tw.png" class="sns"></a>';

	$w_TITLE = mb_convert_encoding(mb_convert_encoding($col['title'], "sjis-win", "eucJP-win"), "UTF-8", "sjis-win");
	$facebook = '<a title="facebookでシェアする" href="http://www.facebook.com/sharer.php?u=' . urlencode('http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]) . '&t=' . urlencode($w_TITLE) . '" target="_blank"><img src="/common/img/fb.png" class="sns"></a>';
	$twitter = '<a href="http://twitter.com/share?url=' . urlencode('http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]) . '&text=' . urlencode($w_TITLE) . '" target="_blank"><img src="/common/img/tw.png" class="sns"></a>';
//↑2018.01.25 IL PHPのワーニングがでることがあるため、記事タイトルのエンコード方法を変更。

$html = str_replace('<div class="facebook_btn"></div>', '<div class="facebook_btn">'.$facebook.'</div>', $html);
$html = str_replace('<div class="twitter_btn"></div>', '<div class="twitter_btn">'.$twitter.'</div>', $html);

$html = str_replace('<-url->', 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"], $html);
echo $html;

// MySQL接続解除
$con = mysqli_close($con);
exit;

?>
