<?php
/*
	Copyright(c) 2010 Shift Tech Inc. All Rights Reserved.
 	http://www.shift-tech.jp/

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**** datetime ****/
require_once( LIB_PATH.'/common/DateTime.php');
/**
 * 日付の妥当性チェック
 * 　使い方：	$this->_form->registerRule('date_valid', 'callback', 'check_date_valid'); 
 * 				$this->_form->addRule('xxxx_date', '・日付が不正です<br>', 'date_check',null,'server'); 
 * @param	$date	Object	日付タグ
 * @param	$bool	bool	未設定を許す場合はFALSE
 */
function check_date_valid($date,$bool=false)
{
	if(!is_array($date)) {
		list($y,$m,$d) = explode('-', $date);
	} else {
		list($y,$m,$d) = _get_dateparam($date);	
	}
	if ($bool) {
		return @checkdate($m, $d, $y);
	} else {
		if (($y.$m.$d) != '') {
			return @checkdate($m, $d, $y);
		} else {
			return true;
		}
	}
}

/**
 * 時間の妥当性チェック
 * 　使い方：	check_date_valid() に同じ
 * @param	$time	Object
 * @param	$bool	bool	未設定を許す場合はFALSE
 */
function check_time_valid($time,$bool=false)
{
	list($h,$i) = _get_timeparam($time);	

	if (!$bool) {
		if (($h.$i) == '') {
			return true;
		}
	}

	return (is_numeric($h) && $h >= 0 && $h <= 23 && is_numeric($i) && $i >= 0 && $i <= 23); 
}


/**
 * 日付のFROMTO矛盾チェック
 * 　使い方：	$this->_form->registerRule('date_from_to', 'callback', 'check_date_from_to'); 
 * 				$this->_form->addRule( array('xxxx_from','xxxx_to'), '・日付範囲の指定が不正です<br>', 'date_check',null,'server'); 
 * @access	public
 * @param	$datefields	Object	日付タグ
 */
function check_date_from_to($datefields)
{
	if (is_array($datefields)) {
		/*数字が丸められて正しく判定されないので削除
		list($fy,$fm,$fd) = _get_dateparam($datefields[0]);
		$fromtime = mktime(0,0,0,$fm,$fd,$fy);
		list($ty,$tm,$td) = _get_dateparam($datefields[1]);
		$totime = mktime(0,0,0,$tm,$td,$ty);
		*/
		$fromtime = strtotime($datefields[0]);
		$totime = $datefields[1]?strtotime($datefields[1]):null;
		if ( $fromtime > $totime && $totime>0) {
			return false;
		} else {
			return true;
		}
	}
	return true;	
}

/**
 * 時刻のFROMTO矛盾チェック
 * 　使い方：	check_date_valid() に同じ
 * @access	public
 * @param	$timefields	Object
 */
function check_time_from_to($timefields)
{
	if (is_array($timefields)) {
		
		list($fh,$fi) = _get_timeparam($timefields[0]);
		$fromtimestr = sprintf("%02d%02d",$fh,$fi);

		list($th,$ti) = _get_timeparam($timefields[1]);
		$totimestr = sprintf("%02d%02d",$th,$ti);

		// 4桁の数値として比較
		if ( intval($fromtimestr) > intval($totimestr) ) {
			return false;
		} else {
			return true;
		}
	}
	return true;
}

/**
 * 日付時刻のFROMTO矛盾チェック
 * 　使い方：	check_date_valid() に同じ
 * @access	public
 * @param	$datetimefields	Object
 */
function check_datetime_from_to($datetimefields)
{
	if (is_array($datetimefields)) {
		
		list($fy,$fm,$fd) = _get_dateparam($datetimefields[0]);
		list($fh,$fi) = _get_timeparam($datetimefields[0]);
		$fromtime = mktime($fh,$fi,0,$fm,$fd,$fy);

		list($ty,$tm,$td) = _get_dateparam($datetimefields[1]);
		list($th,$ti) = _get_timeparam($datetimefields[1]);
		$totime = mktime($th,$ti,0,$tm,$td,$ty);

		if ( $fromtime > $totime ) {
			return false;
		} else {
			return true;
		}
	}
	return true;
}

/**
 *	時間の妥当性チェック(H:i Format)
 * @param	$datetimefields	Object
 */
function check_time_format_valid($time)
{
	if (ereg("^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]$", $time)) {
		$h = substr($time, 0,2);
		$i = substr($time, 3,2);
		$s = substr($time, 6,2);
		if ($h >= 24 || $i >= 60 || $s >= 60) {
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}

/**
 * URL書式チェック
 * 　使い方：	$this->_form->registerRule('check_url', 'callback', 'check_url'); 
 * 				$this->_form->addRule( 'url', '・URLが不正です<br>', 'check_url',null,'server'); 
 * @access	public
 * @param	$datefields	Object	
 * php.iniの設定が必要?
 */
function check_url($value)
{
	if(empty($value)) {
		return true;
	}
	if (preg_match("/s?https?:\/\/[-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#]+/",$value))	{
		return true;
	}
	else{
		return false;
	}
}

/**
 * URL存在チェック
 * 　使い方：	$this->_form->registerRule('check_url', 'callback', 'check_urlexist'); 
 * 				$this->_form->addRule( array('url1','url2, ...'), '・URLにアクセスできません<br>', 'check_url',null,'server'); 
 * @access	public
 * @param	$datefields	Object	
 * php.iniの設定が必要?
 */
function check_urlexist($value)
{
	if(empty($value)) {
		return true;
	}
	$arr = explode("\n", $value);
	if (!empty($arr)) {
		foreach($arr as $url) {
			$tmp = explode(',', $url);
			if(is_array($tmp)&& count($tmp)==2) {
				if(!$ft=@fopen($tmp[0], "r")) {
					return	false;
				} else {
					fclose($ft);
					if(!isset($tmp[1])|| $tmp[1]==null) {
						return false;
					}
				}
			} else {
				return false;
			}
		}
	} else {
		$tmp = explode(',', $value);
			if(!$ft=@fopen($tmp[0], "r")) {
				return	false;
			} else {
				fclose($ft);
				if(!isset($tmp[1])|| $tmp[1]==null) {
					return false;
				}
			}
	}
	return true;
}

/*
 * 全角カナチェック
 * 　@param $str
 */
function check_kana($str = null) {
	if($str != null) {
		$str = str_replace(" ", "",mb_convert_kana( $str, "s"));
		return mb_ereg("^([ァ-ヶｦー　])+$", $str);
	}
}

/*
 * 半カナ禁止チェック
 * @param $str
 */
function check_hankana($str = null) {
	if($str != null) {
		return !mb_ereg("[ｦ-ﾟｰ]",$str);
	}
}

/*
 *  数値範囲チェック
 * 
 */
function NumericRange($value, $val_ar) {
	return $val_ar[0] <= $value && $value <= $val_ar[1];
} 

/*
 * トークンチェック
 */
function check_token($value)
{
	if(isset($_SESSION["bAdmin_token"]) && $_SESSION["bAdmin_token"] != ""
	 && $value != "") {
		if($_SESSION["bAdmin_token"] === $value ) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}
?>