<?php
class baseForm
{
	function baseForm()
	{
		$this->__construct();
	}

	function __construct()
	{
		$this->db	 = new DataBase();
		//PEARのライブラリを独自に追加する場合
		if (!defined('PATH_SEPARATOR')) {
		    if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
		        define('PATH_SEPARATOR', ':');
		    } else {
		        define('PATH_SEPARATOR', ';');
		    }
		}

		//区切り符号
		define('UNITS_REPLACE_FLG', '%%%');
		define('UNITS_ENTER_FLG', "\r\n");
		define('UNITS_DOCUMENT_ROOT_PATH', BASE_PATH);

		$this->_class_path = realpath(dirname( __FILE__));
		ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.$this->_class_path);

		//TPLへのパス
		$this->_tpl_path = str_replace("/class","",$this->_class_path);

		//フォームジェネレータ編集可能チェック
		$result = 1;
		if($_SESSION["usr_info"]["usr_kanri"]!=1){
			$sql = "SELECT count(*)
					FROM mdl_extMdlFormGnrtr_tbl_form_user
					WHERE frm_usr_id = " . $_SESSION["usr_info"]["usr_code"];
			$result = $this->db->getOne($sql);
		}
		unset($_SESSION["form_generator_kanri"]);
		$_SESSION["form_generator_kanri"] = $result;
	}

	 /*
	 * 管理メニューアクセスコントロール
	 * @return
	 */
	 function setAuth(&$view)
	 {
	 	$menu = array();
		$arr = array();
	 	if($_SESSION["form_generator_kanri"] == 1){

			$menu["frmadd"] = array("sys_path" 		=> "/usr-data/modules/extMdlFormGnrtr/admin/frmadd/index.php",
									"sys_title" 	=> "フォーム基本情報登録",
									"active" 		=> "0",
									);

			$menu["frmlist"] = array("sys_path" 	=> "/usr-data/modules/extMdlFormGnrtr/admin/frmlist/index.php",
									"sys_title" 	=> "フォーム一覧",
									"active" 		=> "0",
									);

			$menu["frmuser"] = array("sys_path" 	=> "/usr-data/modules/extMdlFormGnrtr/admin/frmuser/index.php",
									 "sys_title" 	=> "利用権限設定",
									 "active" 		=> "0",
									);


			if(is_array($menu)) {
				foreach ($menu as $row) {
					if (dirname(BINGOTOPURL.$_SERVER['REQUEST_URI']) == str_replace('/index.php','',BINGOTOPURL.$row['sys_path'])
						 && dirname(BINGOTOPURL.$_SERVER['REQUEST_URI']) != BINGOTOPURL."/usr-data/modules/extMdlFormGnrtr"
						 && dirname(BINGOTOPURL.$_SERVER['REQUEST_URI']) != BINGOTOPURL."/usr-data/modules/extMdlFormGnrtr/admin"
					) {
						$row['active'] = 1;
					} else {
						if($row['sys_path'] == "/usr-data/modules/extMdlFormGnrtr/admin/frmlist/index.php"
						&& (   dirname(BINGOTOPURL.$_SERVER['REQUEST_URI']) == BINGOTOPURL . "/usr-data/modules/extMdlFormGnrtr/admin/partlist"
						    || dirname(BINGOTOPURL.$_SERVER['REQUEST_URI']) == BINGOTOPURL . "/usr-data/modules/extMdlFormGnrtr/admin/partadd"
						    || dirname(BINGOTOPURL.$_SERVER['REQUEST_URI']) == BINGOTOPURL . "/usr-data/modules/extMdlFormGnrtr/admin/inqlist"
						    || dirname(BINGOTOPURL.$_SERVER['REQUEST_URI']) == BINGOTOPURL . "/usr-data/modules/extMdlFormGnrtr/admin/inqpre"
						   )
						){
							$row['active'] = 1;
						}else{
							$row['active'] = 0;
						}
					}
					$row['sys_path'] = BINGOTOPURL.$row['sys_path'];
					$arr[] = $row;
				}
			}
	 	}
		$view->assign('BINGODIR', BINGOTOPURL);
		$view->assign('MENU_AUTH', $arr);
		$view->assign('MENU', MODULES_PATH.'/extMdlFormGnrtr/admin/common/menu.tpl');
	 }

	 //文字転換
	 function textAdjust($text){
	 	$text = str_replace('<script>','&lt;script&gt;',$text);
	 	$text = str_replace('</script>','&lt;/script&gt;',$text);
	 	$text = str_replace("'", "'", $text);
	 	return $text;
	 }


	function getToken() {
    	mt_srand((double)microtime()*1000000);
    	$token = sha1(uniqid(mt_rand(),1));
    	$_SESSION["extFrmGnrtAdmin_token"] = $token;
    	return $token;
    }

	function clrToken() {
    	unset($_SESSION["extFrmGnrtAdmin_token"]);
    }

    function putTokenError() {
    	die('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><p>不正なページ遷移の為処理を停止しました。管理トップよりやり直してください。</p></body></html>');
    }

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