<?php
////////////////////////////////////////
// 一般ユーザー：TOPページ
// last update 2010/12/31
// last check 2010/12/31
////////////////////////////////////////
//error_reporting(E_ALL & ~E_NOTICE);ini_set( 'display_errors', 1 );

//DEBUG
if($_SERVER["REMOTE_ADDR"] == "117.102.191.76"){error_reporting(E_ALL & ~E_NOTICE);ini_set( 'display_errors', 1 );}

//アプリケーション設定
require_once("./data_area/class/HRP_Ini.php");
$app_ini=new HRP_Ini();
//ユーザーエージェント判定
require_once('Net/UserAgent/Mobile.php');
$agent=Net_UserAgent_Mobile::singleton();
$ua_type=$ua_model=null;

//使用クラスの定義読み込み
require_once('template/COM_HtmlPage.php');
require_once('account/HRP_AccountMember.php');
require_once('account/HRP_AccountMemberList.php');
require_once('story/HRP_StoryList.php');
require_once('article/HRP_ArticleList.php');
require_once('story/HRP_StoryListForEditor.php');
require_once('setting/HRP_Setting.php');
require_once('genre/HRP_GenreForAdmin.php');
require_once('advertising/HRP_Advertising2017.php');
require_once('story/HRP_StoryOne.php');
require_once('enquete/HRP_EnqueteVoteList.php');

//画面状態パラメータ
$viewParam=array(
	HRP_Ini::SITE_STATUS_OPENED=>array('template'=>HRP_Ini::PATH_TEMPLATE_DIR.'index.html'),
	HRP_Ini::SITE_STATUS_OPENED.HRP_Ini::FILE_PREFIX_SMARTYPHONE => array('template'=>HRP_Ini::PATH_TEMPLATE_DIR.HRP_Ini::FILE_PREFIX_SMARTYPHONE.'index.html'),
	HRP_Ini::SITE_STATUS_OPENED.HRP_Ini::FILE_PREFIX_MOBILE_DOCOMO=>array('template'=>HRP_Ini::PATH_TEMPLATE_DIR.HRP_Ini::FILE_PREFIX_MOBILE_DOCOMO.'index.html'),
	HRP_Ini::SITE_STATUS_OPENED.HRP_Ini::FILE_PREFIX_MOBILE_AU=>array('template'=>HRP_Ini::PATH_TEMPLATE_DIR.HRP_Ini::FILE_PREFIX_MOBILE_AU.'index.html'),
	HRP_Ini::SITE_STATUS_OPENED.HRP_Ini::FILE_PREFIX_MOBILE_SOFTBANK=>array('template'=>HRP_Ini::PATH_TEMPLATE_DIR.HRP_Ini::FILE_PREFIX_MOBILE_SOFTBANK.'index.html'),
);
if($_SERVER["REMOTE_ADDR"] == "218.45.24.202" || $_SERVER["REMOTE_ADDR"] == "115.187.73.62" || $_SERVER["REMOTE_ADDR"] == "202.152.124.234" ){
	$site_status=HRP_Ini::SITE_STATUS_OPENED;
}else{
	$site_status=HRP_Ini::CURRENT_SITE_STATUS;
}

//アクセスコントロール
$required_protocol='HTTP';	//ページの要求プロトコル
include(HRP_Ini::PATH_UTILITY_DIR.'control_access.php');
//会員ログイン判定
$member_login=false;	//会員ログインフラグ
include(HRP_Ini::PATH_UTILITY_DIR.'check_member_login.php');		//会員ログイン＆投票チェック

//ジャンルパラメータ取得
$genre_id=null;
if(isset($_GET['genre_id'])){
	$genre_id=intval($_GET['genre_id']);
	if($genre_id<0){
		$genre_id=null;
	}
}

global $View;

if($member_login) {
	$account_member=new HRP_AccountMemberList();
	if($account_member->queryAccountMemberOne($_SESSION[HRP_Ini::SESSION_KEY_MEMBER]->user_id->getValue())){
		$View['ranking_name']=$account_member->records[0]['ranking_name'];
		$ranking_icon = '';
		if(!$account_member->records[0]['icon_l_path']) {
			require_once 'ranking/HRP_RankingForAdmin.php';
			$rankingForAdminTable = new HRP_RankingForAdmin();
			$ranking = $rankingForAdminTable->getRankingById(HRP_Ini::DEFAULT_RANKING_ID);
			$ranking_icon = HRP_Ini::URL_RANKING_IMAGE_ROOT.$ranking['icon_l_path'].$ranking['icon_l_type'];
		} else {
			$ranking_icon = HRP_Ini::URL_RANKING_IMAGE_ROOT.$account_member->records[0]['icon_l_path'].$account_member->records[0]['icon_l_type'];
		}
	}
	$View['member_name'] = $_SESSION[HRP_Ini::SESSION_KEY_MEMBER]->nick_name->getHtml();
	$View['ranking_icon'] = $ranking_icon;
	$View['userIcon'] = '/member/profile/images/'.$_SESSION[HRP_Ini::SESSION_KEY_MEMBER]->user_id->getValue().'/thumbnail_image.gif';
} else {
	$View['member_name'] = false;
}

////////////
//ページ出力
////////////
$html=new COM_HtmlPage();	//HTMLページクラス作成
$mb_encode=$template=$content_header=null;
$htmlParam=array();			//置換対象配列作成

//指定ジャンル引継ぎ
$htmlParam['story_list_opt']='';
if($genre_id){
	$htmlParam['story_list_opt']='?genre_id='.$genre_id;
}
//$ua_type=HRP_Ini::USER_AGENT_SMARTPHONE;
switch($ua_type){
	case HRP_Ini::USER_AGENT_DOCOMO:
		$mb_encode=HRP_Ini::MB_ENCODING_SJIS;
		$template=$viewParam[$site_status.HRP_Ini::FILE_PREFIX_MOBILE_DOCOMO]['template'];
		$content_header='Content-type:application/xhtml+xml; charset='.HRP_Ini::ENCODING_DOCOMO;
		$htmlParam['encoding']=HRP_Ini::ENCODING_DOCOMO;
		include(HRP_Ini::PATH_UTILITY_DIR.'mb_html_parts.php');		//HTMLパーツの取得(携帯用)
		//メイン記事ダイジェスト取得
		$story_list=new HRP_StoryList();
		if($story_list->queryMainStory($genre_id)){
			$htmlParam['mainStory']=$story_list->getMainStoryHtmlForMobile();
		}else{
			$htmlParam['mainStory']='メイン記事が取得できませんでした。';
		}
		//最新キニナル記事取得
		$article_list=new HRP_ArticleList();
		if($article_list->queryMainArticle()){
			$htmlParam['mainArticle']=$article_list->getMainArticleHtmlForMobile();
		}else{
			$htmlParam['mainArticle']='新着キニナルが取得できませんでした。';
		}
		break;
	case HRP_Ini::USER_AGENT_AU:
		$mb_encode=HRP_Ini::MB_ENCODING_SJIS;
		$template=$viewParam[$site_status.HRP_Ini::FILE_PREFIX_MOBILE_AU]['template'];
		$content_header='Content-type:application/xhtml+xml; charset='.HRP_Ini::ENCODING_AU;
		$htmlParam['encoding']=HRP_Ini::ENCODING_AU;
		include(HRP_Ini::PATH_UTILITY_DIR.'mb_html_parts.php');		//HTMLパーツの取得(携帯用)
		//メイン記事ダイジェスト取得
		$story_list=new HRP_StoryList();
		if($story_list->queryMainStory($genre_id)){
			$htmlParam['mainStory']=$story_list->getMainStoryHtmlForMobile();
		}else{
			$htmlParam['mainStory']='メイン記事が取得できませんでした。';
		}
		//最新キニナル記事取得
		$article_list=new HRP_ArticleList();
		if($article_list->queryMainArticle()){
			$htmlParam['mainArticle']=$article_list->getMainArticleHtmlForMobile();
		}else{
			$htmlParam['mainArticle']='新着キニナルが取得できませんでした。';
		}
		break;
	case HRP_Ini::USER_AGENT_SOFTBANK:
		$mb_encode=HRP_Ini::MB_ENCODING_UTF8;
		$template=$viewParam[$site_status.HRP_Ini::FILE_PREFIX_MOBILE_SOFTBANK]['template'];
		$content_header='Content-type:application/xhtml+xml; charset='.HRP_Ini::ENCODING_SOFTBANK;
		$htmlParam['encoding']=HRP_Ini::ENCODING_SOFTBANK;
		include(HRP_Ini::PATH_UTILITY_DIR.'mb_html_parts.php');		//HTMLパーツの取得(携帯用)
		//メイン記事ダイジェスト取得
		$story_list=new HRP_StoryList();
		if($story_list->queryMainStory($genre_id)){
			$htmlParam['mainStory']=$story_list->getMainStoryHtmlForMobile();
		}else{
			$htmlParam['mainStory']='メイン記事が取得できませんでした。';
		}
		//最新キニナル記事取得
		$article_list=new HRP_ArticleList();
		if($article_list->queryMainArticle()){
			$htmlParam['mainArticle']=$article_list->getMainArticleHtmlForMobile();
		}else{
			$htmlParam['mainArticle']='新着キニナルが取得できませんでした。';
		}
		break;
	case HRP_Ini::USER_AGENT_SMARTPHONE: //Smart Phone
		require_once('advertising/HRP_AdnwForSp.php');
		$AD = new HRP_ADNW();
	default:	//PC
		if(!$AD){
			require_once('advertising/HRP_AdnwForPc.php');
			$AD = new HRP_ADNW();
		}
		$View['ad']=true;
		$View['jscroll']=true;
		$View['breadcrumb']=false;
		$View['shop_header_info']=false;
		$template = 'sp_base.phtml';

		$setting=new HRP_Setting();
		$setting->querySetting();
		$mb_encode=HRP_Ini::ENCODING;

		$View['MainTemplate']='kininaru_top_page.phtml';

		//ページングパラメータ取得
		$page_no=0;
		if(isset($_GET['page_no'])){
			$page_no=intval($_GET['page_no']);
			if($page_no<0){
				$page_no=0;
			}
		}
		//ユーザーID（ライター）パラメータ取得（無効化）
		$user_id=null;
		//メイン記事一覧を取得
		$story_list=new HRP_StoryListForEditor();
		$View['has_next_story_page']=false;

		if(isset($_GET['genre_id']) && $_GET['genre_id'] == 'all') {
/*
			if($story_list->queryGenre()) {
				$story_contents = '';
				$genres = $story_list->records ;
				foreach ($genres as $genre) {
					if($story_list->queryStoryForUser($user_id,$genre['id'],$page_no,$setting->records['0']['story_number'])) {
						$stories = $story_list->records;
						#print_r($stories);
						die();
					}
				}
			}
*/
		} else {
			if($story_list->queryStoryForUser2017($user_id,$genre_id,$page_no,10)){
				$real_story_count=0;
				foreach((array)$story_list->records['story'] as $story_record){
					$story_url=$story_record['records'][0]['url'] ?? '';
					if(strpos($story_url,'story.php?story_id=')===0){
						$real_story_count++;
					}
				}
				$View['has_next_story_page']=($real_story_count>=10);
				if(count($story_list->records) > 0) {

					if($_GET['ajax']) {
						$htmlParam['story_content'] = $story_list->getStoryHtmlForSmartPhone($_SERVER['SCRIPT_NAME'],$setting->records['0']['story_number'],$user_id,$genre_id);
						print json_encode($htmlParam);
						exit;
					}

				} else {
					$genreQuery = new HRP_GenreForAdmin();
					$genre = $genreQuery->queryGenre($genre_id);
					if($_GET['ajax']) {
						$htmlParam['story_content'] = $story_list->getStoryHtmlForSmartPhone($_SERVER['SCRIPT_NAME'],$setting->records['0']['story_number'],$user_id,$genre_id);
						print json_encode($htmlParam);
						exit;
					}
					$View['jscroll']=false;
				}

				if(count($story_list->records['story']) < 11) {
					$View['jscroll']=false;
				}

			}else{
				$htmlParam['mainStory']='メイン記事一覧が取得できませんでした。';
				$View['jscroll']=false;
			}
			$htmlParam['genre_contents'] = '';
		}

////////////
//ここからサイドバーの取得
////////////

////////////
//DBコネクション確立
////////////

		$db=new COM_Query();

		try{
			$dsn_opt=array('host'=>HRP_Ini::DB_HOST,'port'=>HRP_Ini::DB_PORT,'dbname'=>HRP_Ini::DB_NAME,'user'=>HRP_Ini::DB_USER,'password'=>HRP_Ini::DB_PWD);
			$db->doConnect($dsn_opt);
		}catch(PDOException $e){
			$db->error->setInfo(
				COM_ERROR::TYPE_SYSTEM,
				"PDOError".$e->getMessage(),
				__FILE__.__LINE__
			);
			$db->doClose();
		}

		//error_reporting(E_ALL & ~E_NOTICE);ini_set( 'display_errors', 1 );
		//TOPアンケート取得
		$enqueteVote=new HRP_EnqueteVoteList();
		if(isset($_GET['mode'])){
			$modeStatus = $_GET['mode'];
			if ( $modeStatus != '' && $modeStatus == HRP_Ini::ENQUETE_MODE_RESULT){
				$View['enquete'] = '';
			} else {
				$View['enquete'] = $enqueteVote->queryOwnEnquete2017($db,$member_login,$_SERVER["REMOTE_ADDR"], $_SERVER['HTTP_USER_AGENT'] );
			}
		} else {
			$View['enquete'] = $enqueteVote->queryOwnEnquete2017( $db, $member_login, $_SERVER["REMOTE_ADDR"], $_SERVER['HTTP_USER_AGENT'] );
		}

		//イチオシの取得
		//todo あとで統合
		if(0==$page_no){
			$View['ichioshi']=$story_list->getSideBarIchioshiForTop2017($db);
		}else{
			$View['ichioshi']=$story_list->getSideBarIchioshi2017($db);
		}

		$View['origin']='top';
		if($page_no!=0 && is_numeric($page_no)){
			$View['origin'].="_page_no_{$page_no}";
		}
		if($genre_id){
			$View['origin'].="_genre_id_{$genre_id}";
		}

		//キニナル調査依頼
		$ArticleList=new HRP_ArticleList();
		$View['side-kininaru_post']=$ArticleList->getMainArticle2017($db);

		//最新の記事へのコメント取得
		$UserComment=new HRP_StoryOne();
		$View['side-kininaru_comment']=$UserComment->getUserCommentForSidebar2017($db);


		$View['og:type']='website';
		$View['og:image']=' https://hamarepo.com/story/images/275/title_image.jpg ';

		switch ($genre_id){

			case 1:
				$View['title']='政治・経済カテゴリの記事一覧';
				$View['keywords']='横浜,経済,情報,ニュース,企業,KOEI,AMANO,NISSAN,ナイトライフ,団地妻,オランダ妻';
				$View['description']='政治・経済記事一覧｜横浜のキニナル政治や経済、企業を調査して記事にします。情報受付中！横浜の「そうだったんだ！」が見つかるWebマガジン『はまれぽ.com』';
				break;

			case 2:
				$View['title']='娯楽・文化カテゴリの記事一覧';
				$View['keywords']='横浜,観光,文化,ベイスターズ,みなとみらい';
				$View['description']='娯楽・文化記事一覧｜横浜のキニナル情報や、噂、口コミを調査して記事にします。投稿受付中！横浜の「そうだったんだ！」が見つかるWebマガジン『はまれぽ.com』';
				break;

			case 3:
				$View['title']='キニナル飲食店・グルメ記事の一覧';
				$View['keywords']='家系ラーメン,ラーメン,デカ盛り,グルメ,中華街,ラーメン,横浜,川崎,湘南';
				$View['description']='横浜、川崎、湘南、神奈川の不思議なお店、不思議なメニュー、デカ盛りなど調査してレポート。地域のナゾを調査する『はまれぽ.com』。あなたがキニナル事をお寄せください。';
				break;

			case 4:
				$View['title']='教育・育児カテゴリの記事一覧';
				$View['keywords']='横浜,教育,子育て,学校,こども';
				$View['description']='教育・育児記事一覧｜横浜のキニナル教育や子育てに関する情報を調査して記事にします。投稿受付中！横浜の「そうだったんだ！」が見つかるWebマガジン『はまれぽ.com』';
				break;

			case 5:
				$View['title']='生活・健康カテゴリの記事一覧';
				$View['keywords']='横浜,健康,生活,地域,環境';
				$View['description']='生活・健康記事一覧｜横浜のキニナル噂、口コミを調査して記事にします。投稿受付中！横浜の「そうだったんだ！」が見つかるWebマガジン『はまれぽ.com』';
				break;

			case 6:
				$View['title']='横浜に関わりのある有名人にインタビュー';
				$View['keywords']='横浜,インタビュー,有名人,情報,取材';
				$View['description']='インタビュー記事一覧｜横浜に関わりのある有名人を取材して記事にします。投稿受付中！横浜の「そうだったんだ！」が見つかるWebマガジン『はまれぽ.com』';
				break;

			case 13:
				$View['title']='誰もが主役」横浜にゆかりのあるチマタの人をインタビューで特集';
				$View['keywords']='横浜,みんな主役,特集,募集中,はまびと';
				$View['description']='出演者募集中！「みんなが主役」ちまたの一生懸命何かに取り組む人にスポットライトを当てて紹介します。神奈川県の地域情報サイト『はまれぽ.com』';
				break;

			default:
				$View['title']='『はまれぽ.com』 神奈川のスポット、入りたいけど入れない場所、不思議なモノ・オブジェ、真面目な疑問を徹底調査';
				$View['keywords']='横浜,観光,情報,投稿,キニナル';
				$View['description']='横浜、川崎、湘南、神奈川のスポット、入りたいけど入れない場所、不思議なモノ・オブジェ、真面目な疑問を徹底調査してレポート。地域のナゾを調査する『はまれぽ.com』。あなたがキニナル事をお寄せください。';
				break;

		}

		$View['MainTemplate']='kininaru_list.phtml';
		include( HRP_Ini::PATH_TEMPLATE_DIR_2017 .'base_template.phtml' );
		exit;
		break;

}

$htmlParam['mainStory'] = preg_replace('/img\/a_kininaru\.png/',get_template_directory_uri().'/common/img/kininaru_default_answer_header.png',$htmlParam['mainStory']);

add_filter("wp_list_bookmarks", "top_feature_filter");
$param = array(
	'echo' => 0,
	'titl_before' => '<h2 id="h2_top_feature">',
	'title_after' => '</h2>',
	'title_li' => '<img src="/wp-content/themes/e2info/common/img/top_feature_header.png" width="34" height="17" alt="特集">',
	'category_before' => '<div id="top_feature">',
	'category_after' => '</div>',
	'show_name' => 1,

);
if (function_exists('mylinkorder_list_bookmarks')) {
	$param['orderby'] = 'link_order';
	$htmlParam['links'] = mylinkorder_list_bookmarks($param);
} else {
	$htmlParam['links'] = wp_list_bookmarks($param);
}

if($html->loadTemplate($template)){		//テンプレート読み込み
}
$htmlParam['error']=$html->error->getHTMLMessage();			//エラー情報
$html->param=$htmlParam;
$html->replace();
$html->trans_coding($mb_encode,HRP_Ini::ENCODING);
header($content_header);


$html->show();	//画面表示

function top_feature_filter($output) {
	$output = preg_replace('/(<li><a [^>]*>)([^<][^i])/is', '$1<img src="/wp-content/themes/e2info/common/img/top_feature_marker.png" width="14" height="14"/>$2', $output);
	return $output;
}

?>
