# --------------------------------------------------------------
# SESSIONCTL lib
# 
# Session Control Function Library
# 
# 
# --------------------------------------------------------------

#==============================================================================
# RETAIN tag definition.
#==============================================================================

<tag name="retain" keywords="name value empty">
<if value1="#[empty]" value2="" type=eq>
  <if value1="#[value]" value2="" type=eq>
   <set name='tmp_#[name]' value="$[#[name]]" format=tobrowser>
   <input type="hidden" name="#[name]" value="$[tmp_#[name]]">
  <else>
   <set name='tmp_#[name]' value="#[value]" format=tobrowser>
   <input type="hidden" name="#[name]" value="$[tmp_#[name]]">
  </if>
<else>
   <input type="hidden" name="#[name]" value="">
</if>
</tag>

#==============================================================================
# IFNOTSESSION tag definition.
#==============================================================================

<tag name="ifnotsession">
 <if value1="$[_SESSIONID]" value2="" type="eq"><container></if>
</tag>

#------------------------------------------------------------------------------
# LINKBUTTON tag definition.
#------------------------------------------------------------------------------

<TAG NAME=LINKBUTTON KEYWORDS="PAGEDIR PAGE PROCESS NAME NAME2 VALUE VALUE2 TARGET CLASS MULTIPART">

<A HREF="javascript:

 <IF VALUE1="#[PAGEDIR]" VALUE2="" TYPE=NE>
  <SET NAME=X_LEVEL EXPR="$[N_LEVEL] + 1">
 </IF>
                     <IF VALUE1="#[PROCESS]" VALUE2="" TYPE="EQ">
                      document.forms[0].PROCESS.value='';
                     <ELSE>
                      document.forms[0].PROCESS.value='#[PROCESS]';
                     </IF>


                     <IF VALUE1="#[PAGE]" VALUE2="" TYPE="EQ">
                      document.forms[0].PAGE.value='$[PAGE]';
                     <ELSE>
                      document.forms[0].PAGE.value='#[PAGE]';
                     </IF>


                     <IF VALUE1="#[NAME]" VALUE2="" TYPE="NE">
                      document.forms[0].#[NAME].value='#[VALUE]';
                     </IF>


                     <IF VALUE1="#[NAME2]" VALUE2="" TYPE="NE">
                      document.forms[0].#[NAME2].value='#[VALUE2]';
                     </IF>


                     <IF VALUE1="#[MULTIPART]" VALUE2="YES" TYPE="EQ">
                      document.forms[0].encoding='multipart/form-data';
                     </IF>


                     <IF VALUE1="#[TARGET]" VALUE2="" TYPE="NE">
                      document.forms[0].target='#[TARGET]';
                     </IF>


                     <IF VALUE1="#[PAGEDIR]" VALUE2="" TYPE="NE">
                      document.forms[0].P_$[L_LEVEL].value='$[PAGE]';
                      document.forms[0].L_$[L_LEVEL].value='$[X_TITLE]';
                      document.forms[0].D_$[N_LEVEL].value='#[PAGEDIR]';
                      document.forms[0].P_$[N_LEVEL].value='#[PAGE]';
                      document.forms[0].P_LEVEL.value=$[L_LEVEL];
                      document.forms[0].L_LEVEL.value=$[N_LEVEL];
                      document.forms[0].N_LEVEL.value='$[X_LEVEL]';
                     </IF>


                      document.forms[0].submit();
                      document.forms[0].target='';"
                      <if value1="#[CLASS]" value2="" type="ne">
		      CLASS="#[CLASS]"
		      </if>
		      ><EVAL><CONTAINER></EVAL></A>


</TAG>

#------------------------------------------------------------------------------
# ABUTTON tag definition.
#------------------------------------------------------------------------------

<TAG NAME="ABUTTON" KEYWORDS="PAGE PROCESS NAME VALUE MULTIPART">
 <BUTTON ONCLICK="javascript: 
			      <IF VALUE1="#[PAGE]" VALUE2="" TYPE="NE">
                              document.forms[0].PAGE.value='#[PAGE]';
			      <else>
                              document.forms[0].PAGE.value='$[PAGE]';
                              </IF>
 
			      <IF VALUE1="#[PROCESS]" VALUE2="" TYPE="NE">
                              document.forms[0].PROCESS.value='#[PROCESS]';
                              </IF>

			      <IF VALUE1="#[NAME]" VALUE2="" TYPE="NE">
                              document.forms[0].#[NAME].value='#[VALUE]';
                              </IF>

                              <IF VALUE1="#[MULTIPART]" VALUE2="YES" TYPE="EQ">
                              document.forms[0].encoding='multipart/form-data';
                              </IF>

                              document.forms[0].submit();">&nbsp;<CONTAINER>&nbsp;</BUTTON>
</TAG>

# ----------------------------------------------
# CREATESESSION
# ----------------------------------------------
<tag name="createsession" keywords="page timeout">
 <if value1="$[_session_id]" value2="" type="ne">
  <sql>
       delete from sessions 
             where uuid='$[_session_id]'
  </sql>
 </if>
 <set name="_uuid" expr="uuid()">
 <set name="_timestamp" expr="now()">
 <if value1="#[timeout]" value2="" type="eq">
  <set name="timeout" value="0" scope="local">
 </if>
 <sql>
      insert into sessions 
              set uuid='$[_uuid]',
                  timestamp='$[_timestamp]', 
                  timeout_page='#[page]', 
		  timeout=#[timeout]
 </sql>
 <setcookie name="_session_id" value="$[_uuid]">
</tag>

# ----------------------------------------------
# ENDSESSION
# ----------------------------------------------
<tag name="endsession">
 <if value1="$[_session_id]" value2="" type="ne">
  <sql>
       delete from sessions 
             where uuid='$[_session_id]'
  </sql>
  <setcookie name="_session_id" value="">
 </if>
</tag>

#--------------------------------------------------
#SETENV
#--------------------------------------------------
<tag name="setenv" keywords="name">
 <if value1="$[_session_id]" value2="" type="ne">
  <eval name="_envvalue" scope="local"><container></eval>
  <set name="_envvalue" value="#[_envvalue]" format="binary" scope="local">
  <sql>
      replace into sessionenv 
               set session_uuid='$[_session_id]',
                   name='#[name]',
                   value='#[_envvalue]'
  </sql>$[sqlerror]
  <eval name="#[name]"><container></eval>
 </if>
</tag>

#--------------------------------------------------
#CLEARENV
#--------------------------------------------------
  <TAG NAME='CLEARENV' KEYWORDS='NAME'>
   <IF VALUE1='$[_SESSIONID]' VALUE2='' TYPE=NE>
    <SQL SESSIONID="_SESSIONCTL">
      DELETE FROM sessionenv 
      WHERE seid='$[_SESSIONID]'
        AND sename='#[NAME]'
    </SQL>
    <SET NAME='#[NAME]' VALUE="">
   </IF>
  </TAG>

#--------------------------------------------------
#SETPAGE
#--------------------------------------------------
<tag name="setpage" keywords="NAME DIR">
  <SET NAME=PAGE VALUE='#[NAME]'>
  <IF VALUE1="#[DIR]" VALUE2="" TYPE="NE">
   <SET NAME="DIR" VALUE="$[DIR]/#[DIR]">
   <set name="_CTLDIR" value="$[DIR]">
  </IF>  
</TAG>

#--------------------------------------------------
#SETPROCESS
#--------------------------------------------------

<TAG NAME='SETPROCESS' KEYWORDS='VALUE'>
  <SET NAME=PROCESS VALUE='#[VALUE]'>
</TAG>

#--------------------------------------------------
#CHECKSESSION
#--------------------------------------------------

<tag name="checksession">

 <sql>
    select *,
           if((timeout > 0) && (unix_timestamp(now()) - unix_timestamp(timestamp) > (timeout * 60)),'0','1') as session_valid
      from sessions 
     where uuid="$[_session_id]"
 </sql>$[sqlerror]
  
 <ifrow>
  <if value1="$[.session_valid]" value2="1" type="eq">
   <set name="session_timestamp" value="$[.timestamp]">
   <set name="_timestamp" expr="now()">
   <sql>
        update sessions
	       set timestamp='$[_timestamp]'
 	     where uuid="$[_session_id]"
   </sql>

  <sql>
      select * from sessionenv where session_uuid="$[_session_id]"
  </sql>$[sqlerror]
  <whilerow>
   <eval name="$[.name]">$[.value]</eval>
  </whilerow>

  <else>
   <setpage name="$[.timeout_page]">
   <set name="_session_id" value="">
  </if>

 <else>
  <setpage name="">
  <set name="_session_id" value="">
 </ifrow>

</tag>

#--------------------------------------------------
#FIRST RUN - See FETCHPROC in SESSIONCTL tag
#--------------------------------------------------

<TAG NAME='FIRSTRUN'>
        <IF VALUE1='$[_PROCESSRAN]' VALUE2='FALSE' TYPE=EQ>
          <EVAL NAME='_FIRSTRUN'>$[_FIRSTRUN] <CONTAINER></EVAL>
        </IF>
</TAG>

#--------------------------------------------------
#RUNERROR - See FETCHPROC in SESSIONCTL tag
#--------------------------------------------------

<TAG NAME='RUNERROR'>
        <IF VALUE1='$[_PROCESSRAN]' VALUE2='TRUE' TYPE=EQ>
          <EVAL NAME="_RUNERROR">$[_RUNERROR] <CONTAINER></EVAL>
        </IF>
</TAG>
      
#--------------------------------------------------
#SESSIONCTL
#--------------------------------------------------
<TAG NAME='SESSIONCTL' KEYWORDS="PAGE STYLESHEET">

 <tag name="defaultnav">
  <if expr="('$[PAGE]' = '') and ('$[PROCESS]' = '')">
   <eval><container></eval>  
  </if>
 </tag>

 <DATABASE NAME="$[database]" HOST="$[host]" USER="$[user]" PASSWORD="$[password]" SESSIONID="_SESSIONCTL">

  <TAG NAME='FETCHPAGE'>

    <tag name="directory" keywords="NAME PAGE">
     <set name="DIR" value="$[DIR]/#[NAME]">
     <set name="PAGE" value="#[PAGE]"> 
     <fetchpage>
    </tag>

    <tag name="page" keywords="TEMPLATE CLASS onload secure loggedin" SCOPE=LOCAL>
    
      <TAG NAME='AUTHOR' SCOPE=LOCAL>
        <EVAL NAME=AUTHOR SCOPE=PARENT><CONTAINER></EVAL>
      </TAG>

      <TAG NAME='DESCRIPTION' SCOPE=LOCAL>
       <CONTAINER NAME="DESCRIPTION" EVAL="NO" SCOPE="PARENT">
      </TAG>

      <TAG NAME='KEYWORDS' SCOPE=LOCAL>
       <CONTAINER NAME="KEYWORDS" EVAL="NO" SCOPE="PARENT">
      </TAG>

      <TAG NAME='CONTENTS' SCOPE=LOCAL>
        <EVAL NAME="_BODY"><CONTAINER></EVAL>
      </TAG>

      <TAG NAME='TEMPLATE' SCOPE=LOCAL>
        <TAG NAME='CONTENTS' SCOPE=LOCAL>
         $[_BODY]
	</TAG>		
	<EVAL NAME=TEMPLATEDATA SCOPE=PARENT><CONTAINER EVAL="NO"></EVAL>
      </TAG>

      <if expr="('$[HTTPS]'!='on') and ('#[secure]'='yes')">
       <html><head><meta http-equiv="refresh" content="0; url=https://$[HTTP_HOST]$[REQUEST_URI]"></head></html>
      <else>
      
      <if expr="('$[__account_id]'='') and ('#[loggedin]'='yes')">
       <html><head><meta http-equiv="refresh" content="0; url=https://$[HTTP_HOST]/login"></head></html>
      <else>

      <!DOCTYPE html>
      <!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
      <!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
      <!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
      <!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->                            

      <head>
       <EVAL><CONTAINER eval="no"></EVAL>
       <META NAME=AUTHOR CONTENT="#[AUTHOR]">
       <META NAME=DESCRIPTION CONTENT="#[DESCRIPTION]">
       <META NAME=KEYWORDS CONTENT="#[KEYWORDS]">
       <if value1="$[_STYLESHEET]" value2="" type="NE">
        <link rel="stylesheet" type="text/css" href="$[_STYLESHEET]">
       </if>
       $[head]
       
      </head>

      <body <if value1="#[CLASS]" value2="" type="ne">class="#[CLASS]"</if>
            <if value1="#[onload]" value2="" type="ne">onload="#[onload]"</if>>
            
       <form method="post" enctype="application/x-www-form-urlencoded">

	<INPUT TYPE=HIDDEN NAME="_LASTPAGE" VALUE="$[PAGE]" >
	<INPUT TYPE=HIDDEN NAME="PROCESS" VALUE="" >
	<INPUT TYPE=HIDDEN NAME="EXITPROCESS" VALUE="$[EXITPROCESS]" >
	<input type="hidden" name="DIR" value="$[DIR]">
	<input type="hidden" name="_CTLDIR" value="$[_CTLDIR]">
	<INPUT TYPE=HIDDEN NAME="PAGE" VALUE="$[PAGE]" >
        <INPUT TYPE=HIDDEN NAME="_TIMESTAMP" VALUE="$[_timestamp]" >
	
        <IF VALUE1="#[TEMPLATE]" VALUE2="" TYPE="EQ">
         $[_BODY]
	<ELSE>
         <EVAL><INCLUDE FILE="./$[DIR]/#[TEMPLATE].temp"></EVAL>
	 #[TEMPLATEDATA]
	</IF>

       </form>
       
      </body>
     </html>
     
     </if>      
     </if>

    </TAG>

    <eval><INCLUDE FILE="./$[DIR]/$[PAGE].page"></eval>

  </TAG>

  <tag name="fetchproc">
   <tag name="process">

    <if value1="$[_TIMESTAMP:0]" value2="$[session_timestamp]" type="eq">
     <set name="_PROCESSRAN" value="FALSE">
    <else>
     <set name="_PROCESSRAN" value="TRUE">
    </if>

    <eval><container></eval>
    <eval>$[_FIRSTRUN]</eval>
    <eval>$[_RUNERROR]</eval>

   </tag>

   <EVAL><INCLUDE FILE="./$[DIR]/$[PROCESS].proc"></EVAL>
      
  </TAG>

  <tag name="virtual" keywords="SERVER DIR">
   <if value1="$[PAGE]" value2="" type="eq">
    <if value1="#[SERVER]" value2="$[$SERVER_NAME]" type="eq">
     <set name="DIR" value="#[DIR]">
     <set name="_CTLDIR" value="#[DIR]">
    </if>
   </if>
  </tag>

  <if value1="$[_session_id]" value2="" type="ne">
    <checksession>
  </if>
  
  <eval name="head"><container></eval>  
  
  <if value1="$[PROCESS]" value2="" type="ne">
   <fetchproc>
  </if>
  
  <if value1="$[PAGE]" value2="" type="eq">
   <setpage name="#[PAGE]">
  <else>
    <script>
     window.history.replaceState('','','/$[PAGE]');
    </script>
  </if>

  <fetchpage>

 </database>
</tag>
