<?php
include 'function.php';
$this_pg = 'everyone.php';
$html_source = 'everyone_base.html';
$index_page = '/everyone/index.html';

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);
	
	// プレスルームならリダイレクト or インフォメーショングループならリダイレクト
	if ($category_name_array[$col['cate']] == 'プレスルーム' || $col['grp']==5) {
		header("Location: press.php");
		exit;
	}
	// EVERY ONE以外ならリダイレクト
	if ($category_name_array[$col['cate']] != 'EVERY ONE') {
		header("Location: sc_activity.php");
		exit;
	}
	
	// エントリーリスト
	$entry_list = '';
	$sql = "select scj_blog.mid, scj_blog.title, date_format(from_unixtime(scj_blog.vdate), '%Y') as year from scj_blog where view = '1' and cate = '{$col['cate']}' and scj_blog.vdate <= '{$now}' order by vdate desc";
	@mb_convert_variables($enc_db, $enc_in, $sql);
	$rst = mysqli_query($con, $sql);
	$cnt = 0;
	$icnt = array();
	$year = '';
	while ($cols = mysqli_fetch_array($rst)) {
		@mb_convert_variables($enc_in, $enc_db, $cols);
		if ($year=='') {
			$year = $cols['year'];
			$cnt++;
			$icnt[$cnt] = 0;
			$entry_list .= "<li><a href=\"#{$cnt}\">■".$year."年（#icnt{$cnt}）</a>\n<ul class=\"fxmn\">\n";
		} elseif ($year != $cols['year']) {
			$year = $cols['year'];
			$cnt++;
			$icnt[$cnt] = 0;
			$entry_list .= "</ul>\n</li>\n";
			$entry_list .= "<li><a href=\"#{$cnt}\">■".$year."年（#icnt{$cnt}）</a>\n<ul class=\"fxmn\">\n";
		}
		$entry_list .= '<li><a href="'.$this_pg.'?d='.$cols['mid'].'">'.$cols['title'].'</a></li>'."\n";
		$icnt[$cnt]++;
	}
	mysqli_free_result($rst);
	foreach ($icnt as $key => $val) {
		if ($val) {
			$entry_list = str_replace('#icnt'.$key, $val, $entry_list);
		}
	}
} else {
	// プレスルームならリダイレクト
	if ($category_name_array[$col['cate']] == 'プレスルーム') {
		header("Location: press.php");
		exit;
	}
	// EVERY ONE以外ならリダイレクト
	if ($category_name_array[$c] != 'EVERY ONE') {
		header("Location: sc_activity.php");
		exit;
	}
	$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.cate = '{$c}' and ( scj_category.grp = '0' or scj_category.grp = '2' ) and scj_blog.vdate <= '{$now}' order by scj_blog.cateindex desc,scj_blog.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 = '';
	$sql = "select scj_blog.mid, scj_blog.title, date_format(from_unixtime(scj_blog.vdate), '%Y') as year from scj_blog where view = '1' and cate = '{$c}' and scj_blog.vdate <= '{$now}' order by vdate desc";
	@mb_convert_variables($enc_db, $enc_in, $sql);
	$rst = mysqli_query($con, $sql);
	$cnt = 0;
	$icnt = array();
	$year = '';
	while ($cols = mysqli_fetch_array($rst)) {
		@mb_convert_variables($enc_in, $enc_db, $cols);
		if ($year=='') {
			$year = $cols['year'];
			$cnt++;
			$icnt[$cnt] = 0;
			$entry_list .= "<li><a href=\"#{$cnt}\">■".$year."年（#icnt{$cnt}）</a>\n<ul class=\"fxmn\">\n";
		} elseif ($year != $cols['year']) {
			$year = $cols['year'];
			$cnt++;
			$icnt[$cnt] = 0;
			$entry_list .= "</ul>\n</li>\n";
			$entry_list .= "<li><a href=\"#{$cnt}\">■".$year."年（#icnt{$cnt}）</a>\n<ul class=\"fxmn\">\n";
		}
		$entry_list .= '<li><a href="'.$this_pg.'?d='.$cols['mid'].'">'.$cols['title'].'</a></li>'."\n";
		$icnt[$cnt]++;
	}
	mysqli_free_result($rst);
	foreach ($icnt as $key => $val) {
		if ($val) {
			$entry_list = str_replace('#icnt'.$key, $val, $entry_list);
		}
	}
}

// データセット
$html = preg_replace('/<!--cateindex_start-->.*?<!--cateindex_end-->/s', '', $html);
$html = preg_replace('/<-list->/', $entry_list, $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('<-category_name-></h2>', '<-category_name-><div class="entry" style="margin:0px; text-align:right; font-size:55.53%;">（公開日：'.date('Y.m.d', $col['vdate']).'）</div></h2>', $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);

// facebook&twitter
$facebook = '<a title="facebookでシェアする" href="http://www.facebook.com/sharer.php?u=http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . '&t=' . $col['title'] . '" target="_blank"><img src="/common/img/fb.png"></a>';
$twitter = '<a href="http://twitter.com/share?url=http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . '&text=' . $col['title'] . '" target="_blank"><img src="/common/img/tw.png"></a>';
$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);

//↓2020.09.01 IL 追加 Canonicalタグ設置
$ar_PARAM = array();
$ar_PARAM["URL_MAIN"]   = G_SCJCMS_ROOT_URL . "everyone.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);
echo $html;

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

?>
