<?php
session_cache_limiter('private, must-revalidate');
session_start();

include $_SERVER['DOCUMENT_ROOT'].'/scjcms/function.php';
$this_pg = 'index.php';
$html_source = 'index_base.html';
$index_page = '../index.html';

$enc_source = 'UTF-8';
$enc_out = 'UTF-8';
$enc_in = 'eucJP-win';

$now = time();

mb_language("Japanese");
mb_internal_encoding("eucJP-win");

// MySQL接続
//	$con = mysql_connect($DBSERVER, $DBUSER, $DBPASSWORD);
//	mysql_query($mysqlset);
//	$selectdb = mysql_select_db($DBNAME);

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

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

// include処理
$html = preg_replace_callback('/<\!--#include virtual="(.*?)" -->/', 'rep_include', $html);
function rep_include($match) {
	global $rootdomain, $enc_in, $enc_source;
	$html = '';
	if ($match[1]) {
		$html = mb_convert_encoding(file_get_contents(str_replace('/sp/', '../', $match[1])), $enc_in, $enc_source);
	}
	return $html;
}

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

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

// カテゴリリスト
$category_name_array = array();
$sql = "select mid, name, sub, mainid 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);
	if ($col['sub'] == 1) {
		$sql2 = "select name from scj_category where mid = '{$col['mainid']}' limit 1";
		@mb_convert_variables($enc_db, $enc_in, $sql2);
		$rst2 = mysqli_query($con, $sql2);
		$col2 = mysqli_fetch_array($rst2);
		mysqli_free_result($rst2);
		$category_name_array[$col['mid']] = $col2['name'].'/'.$col['name'];
	} else {
		$category_name_array[$col['mid']] = $col['name'];
	}
}
mysqli_free_result($rst);

// データセット
//$sql = "select scj_blog.mid, scj_blog.cate, scj_blog.title, scj_blog.main, scj_blog.html1, scj_blog.html2, scj_category.grp from scj_blog inner join scj_category on scj_blog.cate = scj_category.mid where scj_blog.view = '1' and scj_category.name != '人材募集' and scj_blog.spnoview != '1' and scj_blog.vdate <= '{$now}' and scj_blog.mid ='{$d}'";
$sql = "select scj_blog.mid, scj_blog.vdate, scj_blog.cate, scj_blog.title, scj_blog.main, scj_blog.html1, scj_blog.html2, scj_category.grp from scj_blog inner join scj_category on scj_blog.cate = scj_category.mid where scj_blog.view = '1' and scj_blog.spnoview != '1' and scj_blog.vdate <= '{$now}' and scj_blog.mid ='{$d}'";
@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?d={$d}");
	//exit;
}
// プレスルームならリダイレクト or インフォメーショングループならリダイレクト
if ($category_name_array[$col['cate']] == 'プレスルーム' || $col['grp']==5) {
	//header("Location: press.php?d={$d}");
	//exit;
}

// 記事前後取得
//$sql = "select scj_blog.mid from scj_blog inner join scj_category on scj_blog.cate = scj_category.mid where scj_blog.view = '1' and scj_blog.spnoview != '1' and scj_blog.vdate <= '{$now}' and ( ( scj_blog.cate = '{$col['cate']}' and scj_blog.cateindex != '1') or ( scj_category.mainid = '{$col['cate']}' ) ) order by scj_blog.vdate desc";
$sql = "select scj_blog.mid from scj_blog inner join scj_category on scj_blog.cate = scj_category.mid where scj_blog.view = '1' and scj_category.name != '人材募集' and scj_blog.spnoview != '1' and scj_blog.vdate <= '{$now}' order by scj_blog.vdate desc";
@mb_convert_variables($enc_db, $enc_in, $sql);
$rst = mysqli_query($con, $sql);
$f_mid = '';
$b_mid = '';
$mid = '';
while ($cols = mysqli_fetch_array($rst)) {
	@mb_convert_variables($enc_in, $enc_db, $cols);
	if ($cols['mid']==$col['mid']) {
		if ($mid) {
			$f_mid = $mid;
		} else {
			$f_mid = $cols['mid'];
		}
	} elseif ($f_mid) {
		$b_mid = $cols['mid'];
		break;
	} else {
		$mid = $cols['mid'];
	}
}
mysqli_free_result($rst);
if ($f_mid == $col['mid']) $f_mid = '';
if ($f_mid) {
	$html = str_replace('<-flink->', 'index.php?d='.$f_mid, $html);
} else {
	$html = preg_replace('/<!--flink_start-->.*?<!--flink_end-->/', '<li></li>', $html);
}
if ($b_mid) {
	$html = str_replace('<-blink->', 'index.php?d='.$b_mid, $html);
} else {
	$html = preg_replace('/<!--blink_start-->.*?<!--blink_end-->/', '<li></li>', $html);
}

// データセット
$html = preg_replace('/<-title->/', $col['title'], $html);
function zoomimg_p($matches) {
	$src_p = str_replace('./', '../../scjcms/', $matches[2]);
	if (!is_file($src_p)) return '';
	$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;
	//↓↓2023.05.17 修正 Nishite
//	$size = getimagesize($matches[2]);
	$size = getimagesize($src_p);
	//↑↑2023.05.17 修正 Nishite
	
	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 = '<img src="'.$src_p.'" />';
	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('/(<img[^>]+?)height="[0-9]+"/', '\\1', $col['main']);
$col['main'] = preg_replace_callback('/<img[^>]+?src="(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, 'eucJP-win');
	$col['main'] = preg_replace('/xxx'.$i.'/', $col['html'.$i], $col['main']);
}
// ディレクトリパス修正
$col['main'] = str_replace('./dat/', $basedomain.'dat/', $col['main']);
$html = preg_replace('/<-main->/', $col['main'], $html);
$html = preg_replace('/<-mid->/', $col['mid'], $html);
$html = preg_replace('/<-category_name->/', $category_name_array[$col['cate']], $html);
if ($i = $leftmenu_array[$category_name_array[$col['cate']]]) {
	$html = preg_replace('/'.$i.'.gif/', $i.'-on.gif', $html);
}
// 2018.09.13 公開日追加
$html = str_replace('<div class="articleHeader">', '<div class="articleHeader"><p class="contentDate">（公開日：'.date('Y.m.d', $col['vdate']).'）</p>', $html);

// HTML表示
$html_header = "Content-Type: text/html; charset=UTF-8";
header($html_header);
header('Content-Language: ja');
$html = mb_convert_encoding($html, $enc_out, $enc_in);

// facebook&twitter

//↓2018.03.12 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_sp.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_sp.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_sp.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_sp.png" class="sns"></a>';

//↑2018.03.12 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);

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

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

echo $html;

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

?>
