<?php
/*
* session.php
* http://tasuke.dna.affrc.go.jp/
*
* Author: Ryutaro Itoh
*
* Copyright (c) 2013 National Institute of Agrobiological Sciences   
*
* This software is released under the MIT License.
*
* http://opensource.org/licenses/mit-license.php
*/

require "./common.php";

session_name("tasukessid");
ini_set("session.cookie_httponly", "true");
ini_set('session.hash_function', 7);
ini_set('session.hash_bits_per_character', 5);
session_set_cookie_params(60*60*24*7);
session_start();

(isset($_POST['init']) && !empty($_POST['init'])) ? $init=$_POST['init']:returnError("Error");
(isset($_POST['string']) && !empty($_POST['string'])) ? $string=$_POST['string']:returnError("Error");
if(preg_match("/\;|\||\&|\`|\(|\)|\<|\>|\*|\?|\{|\}|\[|\]|\!/",$init)) {
        $init ="<div align=\"center\"><font color=\"red\" style=\"font-size:18px;\">Invalid Value!!</font></div>";
        echo $init;
        exit;
}
if(preg_match('/<script>/',$string)) {
	$string ="<div align=\"center\"><font color=\"red\" style=\"font-size:18px;\">Invalid Value!!</font></div>";
	echo $string;
	exit;
}
if($init=="start"){
    $_SESSION["IDENT"] = session_id();
    session_write_close();
}elseif($init=="expire"){
    $_SESSION = array();
    if (isset($_COOKIE["tasukessid"])) {
        setcookie("tasukessid", '', time() - 1, '/');
    }
    session_destroy();
    echo "destroy";
}elseif($init=="load"){
    if (!isset($_COOKIE["tasukessid"])){
        $_SESSION["IDENT"] = session_id();
        session_write_close();
        echo "start";
    }else{
        $_SESSION["IDENT"] = session_id();
        if($_COOKIE["tasukessid"] != $_SESSION["IDENT"]){
            $_SESSION = array();
            setcookie("tasukessid", '', time() - 1, '/');
            session_destroy();
            session_name("tasukessid");
            session_start();
            session_regenerate_id(true);
            $_SESSION["IDENT"] = session_id();
            session_write_close();
            echo "regenerate";
        }else{
            if(isset($_SESSION["primerRes"])){
                echo $_SESSION["primerRes"];
            }else{
                echo "---SPLIT---false";
            }
        }
    }
}elseif($init=="storeP3Res"){
    if (!isset($_COOKIE["tasukessid"])){
        $_SESSION["IDENT"] = session_id();
        session_write_close();
        echo "error---SPLIT---Cookies---SPLIT---true";
    }else{
        $_SESSION["IDENT"] = session_id();
        if($_COOKIE["tasukessid"] != $_SESSION["IDENT"]){
            $_SESSION = array();
            setcookie("tasukessid", '', time() - 1, '/');
            session_destroy();
            session_name("tasukessid");
            session_start();
            session_regenerate_id(true);
            $_SESSION["IDENT"] = session_id();
	    if(preg_match('/<script>/',$string)) {
        	$string ="<div align=\"center\"><font color=\"red\" style=\"font-size:18px;\">Invalid Value!!</font></div>";
        	echo $string;
        	exit;
	    } else {
            	$_SESSION["primerRes"] = $string."---SPLIT---true";
                session_write_close();
                echo $_SESSION["primerRes"];
	    }
        }else{
            $_SESSION["IDENT"] = session_id();
	    if(preg_match('/<script>/',$string)) {
                $string ="<div align=\"center\"><font color=\"red\" style=\"font-size:18px;\">Invalid Value!!</font></div>";
        	echo $string;
        	exit;
	    } else {
            	$_SESSION["primerRes"] = $string."---SPLIT---true";
            	session_write_close();
	    }
        }
    }
}

function returnError($str){
    echo $str;
}
?>
