<?php
	$script_name = explode('/', $_SERVER['SCRIPT_NAME'], 3);
	define('MY_SESSION_NAME', $script_name[1]);
	define('MY_SESSION_PATH', '/'.$script_name[1].'/');
// ****************************************************************
function mobile_init() {
// ****************************************************************
	mobile_session_start();
}
// ****************************************************************
function mobile_session_start($cache_limiter = '') {
// ****************************************************************
	session_set_cookie_params(0, MY_SESSION_PATH);
	session_cache_limiter($cache_limiter);			// nocache, private, private_no_expire, public
	session_start();					// ヘッダー出力の前に定義
	mb_language('Japanese');
	mb_internal_encoding('UTF-8');				// UTF-8, Shift-JIS, iso-8859-1
}
// ****************************************************************
function mobile_common_head($title, $ajaxDisable=true) {
// ****************************************************************
?>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title><?= $title ?></title>
<link rel="stylesheet" href="../jquery.mobile/jquery.mobile-1.1.0.min.css" />
<script src="../js/jquery-1.7.2.min.js"></script>
<?php if ($ajaxDisable) { ?>
<script src="../js/ajaxDisable.js"></script>
<?Php } ?>
<script src="../jquery.mobile/jquery.mobile-1.1.0.min.js"></script>
<script src="../js/jquery.cookie.js"></script>
<?php
}
// ****************************************************************
function mobile_fatal_error($msg) {
// ****************************************************************
	echo $msg;
	exit;
}
// ****************************************************************
function mobile_mysql_connect($dbname) {
// ****************************************************************
	$con = @mysql_connect(_DB_SERVER,_DB_USERNAME,_DB_PASSWORD);
	if ($con == False) {
		mobile_fatal_error('データベース接続エラー');
	}
	mysql_query('set names utf8', $con);
	if (!@mysql_select_db($dbname, $con)) {
		mobile_fatal_error('データベース選択エラー');
	}
	return $con;
}
// ****************************************************************
function mobile_login_check($redirect='../login/') {
// ****************************************************************
	if ($_GET['ac'].'' <> '') {
		$account = $_GET['ac'];
		$password = $_GET['pw'];
		if (($errmsg=mobile_login($account, $password)) == '') {
			return $_SESSION['mobile_login_id'];
		}
	} elseif ($_SESSION['mobile_login_id'].'' <> '') {
		return $_SESSION['mobile_login_id'];
	}
	if ($_COOKIE['mobile_login_id'] <> '' and $_COOKIE['mobile_login_account'] <> '' and $_COOKIE['mobile_login_pass'] <> '') {
		$_SESSION['システム管理者'] = "";
		$id = $_COOKIE['mobile_login_id'];
		$account = $_COOKIE['mobile_login_account'];
		$password = $_COOKIE['mobile_login_pass'];
		$con = mobile_mysql_connect(_DB_ACCOUNT_SCHEMA);
		$log_status = "MOBILE COOKIE ";
		$sql = "select * from m_account where id_account = '" . $id . "' and c_account = '" . $account . "' and c_delete = 0";
		$rs_account = my_mysql_query($sql);
		$row = mysql_num_rows($rs_account);
		if ($row == 0) {
			$sql = "insert into z_loginlog (id_account,c_account,c_password,c_status)";
			$sql .= " VALUES ('0','" . $account . "','" . $password . "','" . "ERROR ".$log_status . "ACCOUNT')";
			$ret = my_mysql_query($sql);
			$con = mysql_close($con);
			redirect($redirect);	 //認証エラー
		}
		$rec_account = mysql_fetch_array($rs_account);
		if (my_hash(Trim($rec_account['c_password'])) <> $_COOKIE['mobile_login_pass']) {
			$sql = "insert into z_loginlog (id_account,c_account,c_password,c_status)";
			$sql .= " VALUES ('" . $rec_account['id_account'] . "','" . $rec_account['c_account'] . "','" . $password . "','" . "ERROR ".$log_status . "PASSWORD')";
			$ret = my_mysql_query($sql);
			$con = mysql_close($con);
			redirect($redirect);	 //認証エラー
		}
		$sql = "insert into z_loginlog (id_account,c_account,c_password,c_status) VALUES ('" . $rec_account['id_account'] . "','" . $rec_account['c_account'] . "','" . $password . "','".$log_status."OK')";
		$ret = my_mysql_query($sql);
		$_SESSION['mobile_login_id'] = $rec_account['id_account'];
		$_SESSION['mobile_login_account'] = $rec_account['c_account'];
		$_SESSION['mobile_login_handle'] = $rec_account['c_handle'];
		if ($rec_account['c_admin'] == "admin") {
			$_SESSION['システム管理者'] = "YES";
		}
		$con = mysql_close($con);

		$password = my_hash(Trim($rec_account['c_password']));
		setcookie("mobile_login_id", $_SESSION['mobile_login_id'], time() + LOGIN_COOKIE_EXPIRE, MY_SESSION_PATH);
		setcookie("mobile_login_pass", $password, time() + LOGIN_COOKIE_EXPIRE, MY_SESSION_PATH);
		setcookie("mobile_login_account", $_SESSION['mobile_login_account'], time() + LOGIN_COOKIE_EXPIRE, MY_SESSION_PATH);
		setcookie("mobile_login_handle", $_SESSION['mobile_login_handle'], time() + LOGIN_COOKIE_EXPIRE, MY_SESSION_PATH);
		return $_SESSION['mobile_login_id'];
	}
	redirect($redirect);
}
// ****************************************************************
function mobile_login($account, $password) {
// ****************************************************************
	$_SESSION['システム管理者'] = "";
	$con = mobile_mysql_connect(_DB_ACCOUNT_SCHEMA);
	$log_status = "MOBILE LOGIN ";
	$sql = "select * from m_account where c_account = '" . $account . "' and c_delete = 0";
	$rs_account = my_mysql_query($sql);
	$row = mysql_num_rows($rs_account);
	if ($row == 0) {
		$sql = "insert into z_loginlog (id_account,c_account,c_password,c_status)";
		$sql .= " VALUES ('0','" . $account . "','" . $password . "','" . "ERROR ".$log_status . "ACCOUNT')";
		$ret = my_mysql_query($sql);
		$con = mysql_close($con);
		return '認証エラー';
	}
	$rec_account = mysql_fetch_array($rs_account);
	if ($rec_account['c_password'] <> $password) {
		$sql = "insert into z_loginlog (id_account,c_account,c_password,c_status)";
		$sql .= " VALUES ('" . $rec_account['id_account'] . "','" . $rec_account['c_account'] . "','" . $password . "','" . "ERROR ".$log_status . "PASSWORD')";
		$ret = my_mysql_query($sql);
		$con = mysql_close($con);
		return '認証エラー';
	}
	$sql = "insert into z_loginlog (id_account,c_account,c_password,c_status) VALUES ('" . $rec_account['id_account'] . "','" . $rec_account['c_account'] . "','" . $password . "','".$log_status."OK')";
	$ret = my_mysql_query($sql);
	$_SESSION['mobile_login_id'] = $rec_account['id_account'];
	$_SESSION['mobile_login_account'] = $rec_account['c_account'];
	$_SESSION['mobile_login_handle'] = $rec_account['c_handle'];
	if ($rec_account['c_admin'] == "admin") {
		$_SESSION['システム管理者'] = "YES";
	}
	$con = mysql_close($con);
	$password = my_hash(Trim($_POST['password']));
	setcookie("mobile_login_id", $_SESSION['mobile_login_id'], time() + LOGIN_COOKIE_EXPIRE, MY_SESSION_PATH);
	setcookie("mobile_login_pass", $password, time() + LOGIN_COOKIE_EXPIRE, MY_SESSION_PATH);
	setcookie("mobile_login_account", $_SESSION['mobile_login_account'], time() + LOGIN_COOKIE_EXPIRE, MY_SESSION_PATH);
	setcookie("mobile_login_handle", $_SESSION['mobile_login_handle'], time() + LOGIN_COOKIE_EXPIRE, MY_SESSION_PATH);
	return '';
}
// ****************************************************************
function mobile_logout() {
// ****************************************************************
	$_SESSION = array();
	if (isset($_COOKIE[session_name()])) {
		setcookie(session_name(), '', time() - 42000, MY_SESSION_PATH);
	}
	session_destroy();
	setcookie("mobile_login_id", '', time() + LOGIN_COOKIE_EXPIRE, MY_SESSION_PATH);		// クリア
	setcookie("mobile_login_pass", '', time() + LOGIN_COOKIE_EXPIRE, MY_SESSION_PATH);		// クリア
	setcookie("mobile_login_account", '', time() + LOGIN_COOKIE_EXPIRE, MY_SESSION_PATH);		// クリア
	setcookie("mobile_login_handle", '', time() + LOGIN_COOKIE_EXPIRE, MY_SESSION_PATH);		// クリア
	return '';
}
// ****************************************************************
function mobile_text($text, $style='', $br=1) {
// ****************************************************************
	$text = '<span style="'.$style.'">'.my_htmlspecialchars($text).'</span>';
	for ($ix=0; $ix<$br; $ix++) {
		$text .= '<br>';
	}
	return $text;
}
// ****************************************************************
function mobile_calendar_memo($memo) {
// ****************************************************************
	if ($memo != '') {
		$memo = str_ireplace('<font color=','<span style=color:',$memo);
		$memo = str_ireplace('</font>','</span>',$memo);
		$memo = str_ireplace('<m>','',str_ireplace('</m>','',$memo));	// 地図タグ除去
		return ins_br(icon_to_imgsrc($memo)).'<br>';
	} else {
		return '';
	}
}
// ****************************************************************
function icon_to_imgsrc($str) {
// ****************************************************************
	return str_ireplace('<icon ', '<img src='._MYHOME_CALENDAR_PATH.'/'.IMAGES_FOLDER.'/', $str);
}
// ****************************************************************
function mobile_calendar_icon($icon) {
// ****************************************************************
	return '<img src="'._MYHOME_CALENDAR_PATH.'/'.IMAGES_FOLDER.'/'.$icon.'">';
}
// ****************************************************************
function holiday_check($year, $month, $day) {
// ****************************************************************
	$datestr = date("Y-m-d", mktime(0, 0, 0, $month, $day, $year));
	$sql = "select * from m_holiday where c_holidaydate = '".$datestr."'";
	$rs = my_mysql_query($sql);
	if ($rec = mysql_fetch_array($rs)) {
		return my_htmlspecialchars($rec['c_holidayname']);
	} else {
		return False;
	}
	return False;
}
// ****************************************************************
function mobile_check_permit_id($login_id, $target_id) {
// ****************************************************************
	if ($login_id."" <> "" && $login_id == $target_id) {
		return "w";
	}
	$con_account = my_mysql_connect(_DB_ACCOUNT_SCHEMA);
	$sql = "select * from m_public where id_account = '" . $target_id . "'";
	$sql .= " and id_permit_id = '" . $login_id . "' and c_delete = 0";
	$rs_public = my_mysql_query($sql, "", $con_account);
	$row_public = mysql_num_rows($rs_public);
	if ($row_public > 0) {
		$rec_public = mysql_fetch_array($rs_public);
		$permit = $rec_public['id_permit_type'];
		// 30:参照許可
		// 60:書込許可
	} else {
		$permit = 0;
	}
	if ($permit == 60) {
		return "w";
	} elseif ($permit == 30) {
		return "r";
	} else {
		return "";
	}
}
?>
