<?xml version="1.0" encoding="UTF-8"?>
<!--
 -
 -  $Id$
 -
 -  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 -  project.
 -
 -  Copyright (C) 1998-2018 OpenLink Software
 -
 -  This project 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; only version 2 of the License, dated June 1991.
 -
 -  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, write to the Free Software Foundation, Inc.,
 -  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 -
-->
<v:page name="user_edit_pwd"
  doctype="-//W3C//DTD XHTML 1.0 Transitional//EN"
  fast-render="1" xmlns:v="http://www.openlinksw.com/vspx/"
  style="index.xsl"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  xmlns:vm="http://www.openlinksw.com/vspx/ods/"
  xmlns:v="http://www.openlinksw.com/vspx/">

    <v:variable persist="0" name="secretq" type="varchar" default="''"/>
    <v:variable persist="0" name="secreta" type="varchar" default="''"/>
    <v:variable persist="0" name="oid_urlv" type="varchar" default="''"/>
  <v:variable persist="0" name="certificate_login" type="integer" default="1"/>
  <v:variable persist="0" name="certificate_content" type="varchar" default="null"/>
    <v:variable persist="0" name="sioc_limit" type="int" default="10"/>
    <v:variable persist="0" name="no_pwd" type="int" default="0"/>
  <v:variable persist="0" name="tab_no" type="int" default="0" param-name="tabNo"/>
  <v:variable persist="temp" name="cert_del"  type="int" default="0" param-name="cert_del"/>
  <v:variable persist="temp" name="key_del"  type="varchar" default="null" param-name="key_del"/>
  <v:variable persist="0" name="cert_edit" type="int" default="null" param-name="cert_edit"/>
    <v:before-data-bind>
      <![CDATA[
      declare pwd, node_id, tmp varchar;

      SELECT WAUI_SEC_QUESTION,
             WAUI_SEC_ANSWER,
             pwd_magic_calc (U_NAME, U_PASSWORD, 1),
             WAUI_OPENID_URL
        INTO self.secretq,
             self.secreta,
             pwd,
             self.oid_urlv
        FROM WA_USER_INFO,
             DB.DBA.SYS_USERS
       where WAUI_U_ID = U_ID
         and U_NAME = self.u_name;
	self.sioc_limit := coalesce (USER_GET_OPTION (self.u_name, 'SIOC_POSTS_QUERY_LIMIT'), 10);

      if (self.cert_edit is not null and not e.ve_is_post)
        {
          for select * from WA_USER_CERTS where UC_ID = self.cert_edit do
	    {
	      self.certificate_login := UC_LOGIN;
	      self.certificate_content := UC_CERT;
	    }
	}

	tmp := uuid ();
	node_id := subseq (tmp, strrchr (tmp, '-'));
	if (pwd like '%'||node_id and regexp_match ('[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}', pwd) is not null)
	  self.no_pwd := 1;
        else
	  self.no_pwd := 0;
      ]]>
    </v:before-data-bind>
 <br/>
  <div id="b" class="c1">
    <div class="tabs">
      <v:url name="sec_url_0" value="Password" url="--sprintf('uiedit_sec.vspx?page=3&tabNo=0')" xhtml_class="--case when self.tabNo = 0 then 'tabX activeTabX' else 'tabX' end"/>
      <v:url name="sec_url_3" value="Password recovery" url="--sprintf('uiedit_sec.vspx?page=3&tabNo=3')" xhtml_class="--case when self.tabNo = 3 then 'tabX activeTabX' else 'tabX' end"/>
      <v:url name="sec_url_1" value="OpenID" url="--sprintf('uiedit_sec.vspx?page=3&tabNo=1')" xhtml_class="--case when self.tabNo = 1 then 'tabX activeTabX' else 'tabX' end"/>
      <v:url name="sec_url_4" value="Limits" url="--sprintf('uiedit_sec.vspx?page=3&tabNo=4')" xhtml_class="--case when self.tabNo = 4 then 'tabX activeTabX' else 'tabX' end"/>
  <?vsp 
       declare ua any;
       ua := http_request_header (http_request_header (), 'User-Agent');
       if (coalesce (strstr (ua, 'MSIE'), -1) > 0 or regexp_match ('Mozilla.*Windows.*Firefox.*\.NET CLR .*', ua) is not null)
       {
	 declare svc_url varchar;
	 svc_url := (select  top 1 WS_CERT_GEN_URL from DB.DBA.WA_SETTINGS);

          if (length (svc_url))
          {
            for (select U_NAME, U_FULL_NAME, U_E_MAIL, WAUI_BORG, WAUI_BCOUNTRY
                   from SYS_USERS, VSPX_SESSION, WA_USER_INFO
                  where VS_SID = self.sid and VS_REALM = 'wa' and VS_UID = U_NAME and U_ID = WAUI_U_ID) do
            {
            declare url, webid varchar;

            webid := sioc..person_iri (sioc..user_obj_iri (U_NAME));
              url := sprintf ('%s?uri=%U&name=%U&email=%U&organization=%U', svc_url, webid, coalesce (U_FULL_NAME, U_NAME), coalesce (U_E_MAIL, ''), coalesce (WAUI_BORG, ''));

     ?>
 	  <a name="sec_url" href='<?vsp http (url); ?>' class="tabX">Certificate Generator</a>
     <?vsp
	     }
	   }
	  else if (regexp_match ('Mozilla.*Windows.*Firefox.*\.NET CLR .*', ua) is not null)
	        {
	  goto no_gen_svc;
	 }
        }
        else if (not ((coalesce (strstr (ua, 'Windows'), -1) > 0) and (coalesce (strstr (ua, 'Safari'), -1) > 0) and (coalesce (strstr (ua, 'Chrome'), -1) <= 0)))
        {
	  no_gen_svc:;
      ?>
      <v:url name="sec_url_6" value="Certificate Generator" url="--sprintf('uiedit_sec.vspx?page=3&tabNo=6')" xhtml_class="--case when self.tabNo = 6 then 'tabX activeTabX' else 'tabX' end"/>
      <?vsp
          }
       ?>
      <v:url name="sec_url_5" value="X.509 Certificates" url="--sprintf('uiedit_sec.vspx?page=3&tabNo=5')" xhtml_class="--case when self.tabNo = 5 then 'tabX activeTabX' else 'tabX' end"/>
      <v:url name="sec_url_6" value="Certificate &amp; Private Key" url="--sprintf('uiedit_sec.vspx?page=3&tabNo=7')" xhtml_class="--case when self.tabNo = 7 then 'tabX activeTabX' else 'tabX' end"/>
    </div>
    <div class="contents">
  <v:form name="usr_pwd" type="simple" method="POST" action="uiedit_sec.vspx?page=3">
      <v:template name="sec_tmpl_0" type="simple" enabled="-- equ (self.tab_no, 0)">
	  <input type="hidden" value="0" name="tabNo" /> 
          <h2>Change login password</h2>
          <p class="fm_expln">For your security, please use a password not found in a dictionary, consisting of both letters, and numbers or non-alphanumeric characters.</p>
			<table>
	      <?vsp if (not self.no_pwd) { ?>
              <tr>
                <th width="135px"><label for="opwd1">Old password</label></th>
                <td>
                  <v:text xhtml_id="opwd1" name="opwd1" value="" type="password" error-glyph="*"/>
                </td>
              </tr>
              <?vsp } ?>
              <tr>
                <th><label for="npwd1">New password</label></th>
                <td>
            <v:text error-glyph="*" xhtml_id="npwd1" name="npwd1" value="" type="password"/>
                </td>
              </tr>
              <tr>
                <th><label for="npwd2">Repeat password</label></th>
                <td>
            <v:text error-glyph="*" xhtml_id="npwd2" name="npwd2" value="" type="password"/>
                </td>
              </tr>
              <tr>
                <td colspan="2" class="ctrl">
		      <span class="fm_ctl_btn">
                  <v:button name="user_pwd_change1111" value="Change" action="simple">
                    <v:on-post>
			<![CDATA[
			declare pwd_match int;

                  if (self.vc_is_valid = 0)
                    return;

			declare exit handler for not found
			{
			  signal ('22023', 'Internal error, user does not exist');
			};

			select equ (pwd_magic_calc (U_NAME, U_PASSWORD, 1), self.opwd1.ufl_value) into pwd_match
                    from SYS_USERS
                   where U_NAME = self.u_name and U_IS_ROLE = 0;

			if (not pwd_match and not self.no_pwd)
			  {
                    self.opwd1.vc_error_message := sprintf ('The old password for "%s" was not correct.', self.u_name);
			    self.opwd1.ufl_failed := 1;
			    self.npwd1.ufl_failed := 1;
			    self.npwd2.ufl_failed := 1;
			    self.vc_is_valid := 0;
			    self.npwd1.ufl_value := '';
			    self.npwd2.ufl_value := '';
			    self.opwd1.ufl_value := '';
			    return;
		          }

                        if ( length(self.npwd1.ufl_value) < 1 or length(self.npwd1.ufl_value) > 255)
                        {
                          self.npwd1.vc_error_message := 'The password value cannot be empty nor longer than 255 chars';
                          self.vc_is_valid := 0;
                          return;
                  }
                        if (self.npwd1.ufl_value = self.npwd2.ufl_value and length (self.npwd1.ufl_value))
                        {
			  if (self.no_pwd)
			    {
                        self.opwd1.ufl_value := (SELECT pwd_magic_calc (U_NAME, U_PASSWORD, 1) FROM DB.DBA.SYS_USERS where U_NAME = self.u_name);
			    }
			  USER_CHANGE_PASSWORD (self.u_name, self.opwd1.ufl_value, self.npwd1.ufl_value);
			    self.opwd1.ufl_value := '';
			  self.msg_msg := 'The password was changed successfully';
                      --self.pwd_msg.vc_data_bind (e);
                        }
                        else
                        {
                    self.npwd2.vc_error_message := 'The new values for the password did not match.  Please re-enter all fields.';
                          self.vc_is_valid := 0;
			  self.npwd2.ufl_failed := 1;
			  self.opwd1.ufl_failed := 1;
			  self.npwd2.ufl_failed := 1;
                        }
		        self.npwd1.ufl_value := '';
			self.npwd2.ufl_value := '';
			self.opwd1.ufl_value := '';
                      ]]>
                    </v:on-post>
                  </v:button>
		 </span>
                </td>
              </tr>
      </table>
      </v:template>
      <v:template name="sec_tmpl_1" type="simple" enabled="-- equ (self.tab_no, 1)">
	  <input type="hidden" value="1" name="tabNo" /> 
          <br />
			<table>
              <tr>
              <th width="135px">
                <label for="oid_url1">OpenID URL</label>
              </th>
                <td nowrap="nowrap">
            <v:text error-glyph="*" xhtml_id="oid_url1" name="oid_url1" value="--self.oid_urlv" xhtml_size="100" xhtml_class="_validate_ _uri_ _canEmpty_" xhtml_onblur="javascript: validateField(this);" />
                  <div style="display:inline; color:red;"><v:error-summary match="oid_url1" /></div>
                </td>
              </tr>
              <tr>
		<td colspan="2" class="ctrl">
		  <span class="fm_ctl_btn">
            <v:button name="bt_set6" value="Save OpenID" action="simple" xhtml_onclick="return validateInputs(this);">
              <v:on-post>
                <![CDATA[
                  if (self.vc_is_valid = 0)
                    return;

			declare err any;

                  err := ODS..openid_url_set (self.u_id, self.oid_url1.ufl_value);
			if (err is not null)
			  {
                            self.vc_is_valid := 0;
			    self.vc_error_message := err;
			    return;
			  }
                    self.vc_redirect ('uiedit_sec.vspx?page=3&tabNo=1');
                      ]]>
                    </v:on-post>
		</v:button>
	    </span>
                </td>
              </tr>
      </table>
      </v:template>
      <v:template name="sec_tmpl_3" type="simple" enabled="-- equ (self.tab_no, 3)">
	  <input type="hidden" value="3" name="tabNo" /> 
          <h2>Password recovery questions</h2>
          <p class="fm_expln">Manage password recovery procedure. Set verification question and answer.</p>
			<table>
              <tr>
              <th width="135px">
                <label for="sec_question">Secret question</label>
              </th>
                <td nowrap="nowrap">
                  <v:text error-glyph="*" name="sec_question" xhtml_id="sec_qst" value="--self.secretq">
                    <v:validator test="length" min="0" max="800" message="Security question cannot be longer then 800 chars" empty-allowed="1" name="vv_secq1"/>
            <v:validator test="sql" expression="length(trim(self.sec_question.ufl_value)) > 800" name="vv_secq2" message="Security question cannot be longer then 800 chars"/>
                  </v:text>
                  <script type="text/javascript">
                      function setSecQuestion()
                      {
                        var S = document.getElementById('dummy_1233211_dummy');
                        var V = S[S.selectedIndex].value;

                        document.getElementById('sec_qst').value = V;
                      }
                  </script>
                  <select name="dummy_1233211_dummy" id="dummy_1233211_dummy" onchange="setSecQuestion()">
                    <option value="">~pick predefined~</option>
                    <option VALUE="First Car">First Car</option>
                    <option VALUE="Mothers Maiden Name">Mothers Maiden Name</option>
                    <option VALUE="Favorite Pet">Favorite Pet</option>
                    <option VALUE="Favorite Sports Team">Favorite Sports Team</option>
                  </select>
                  <div style="display:inline; color:red;"><v:error-summary match="sec_question" /></div>
                </td>
              </tr>
              <tr>
                <th><label for="sec_answer">Secret answer</label></th>
                <td nowrap="nowrap">
                  <v:text error-glyph="*" xhtml_id="sec_answer" name="sec_answer" value="--self.secreta">
            <v:validator test="length" min="0" max="800" message="Security answer cannot be longer then 800 chars" name="vv_seca1" empty-allowed="1"/>
                  </v:text>
                  <div style="display:inline; color:red;"><v:error-summary match="sec_answer" /></div>
                </td>
              </tr>
              <tr>
                <td colspan="2" class="ctrl">
		      <span class="fm_ctl_btn">
                  <v:button name="user_sset" value="Save" action="simple">
                    <v:on-post>
                      <![CDATA[
                  if(self.vc_is_valid = 0)
                    return;

                        WA_USER_EDIT (self.u_name, 'SEC_QUESTION', self.sec_question.ufl_value);
                        WA_USER_EDIT (self.u_name, 'SEC_ANSWER', self.sec_answer.ufl_value);
                    self.vc_redirect ('uiedit_sec.vspx?page=3&tabNo=3');
                      ]]>
                    </v:on-post>
		</v:button>
	    </span>
                </td>
              </tr>
      </table>
  </v:template>
      <v:template name="sec_tmpl_4" type="simple" enabled="-- equ (self.tab_no, 4)">
	  <input type="hidden" value="4" name="tabNo" /> 
          <h2>Applications restrictions</h2>
			<table>
              <tr>
              <th width="135px"><label for="sioc_limit1">SIOC query result limit</label></th>
                <td nowrap="nowrap">
            <v:text error-glyph="*" xhtml_id="sioc_limit1" name="sioc_limit1" value="--self.sioc_limit" />
                  <div style="display:inline; color:red;"><v:error-summary match="sioc_limit" /></div>
                </td>
              </tr>
              <tr>
                <td colspan="2" class="ctrl">
		      <span class="fm_ctl_btn">
            <v:button name="bt_cncl_5" value="Cancel" action="simple" xhtml_onclick="javascript: return initLoadProfile();">
			  <v:on-post>
			    self.vc_redirect (sprintf ('uhome.vspx?ufname=%U&amp;l=1', self.u_name));
			  </v:on-post>
			</v:button>
                  <v:button name="bt_set1" value="Save Restrictions" action="simple">
                    <v:on-post>
                      <![CDATA[
                    self.sioc_limit1.ufl_value := atoi (self.sioc_limit1.ufl_value);
                    if ((self.sioc_limit1.ufl_value < 1) or (self.sioc_limit1.ufl_value > 1000))
                    {
                      self.sioc_limit1.vc_error_message := 'Positive number between 1 and 1000 is required';
                      self.vc_is_valid := 0;
                    return;
                    }

                    USER_SET_OPTION (self.u_name, 'SIOC_POSTS_QUERY_LIMIT', self.sioc_limit1.ufl_value);
                    self.vc_redirect ('uiedit_sec.vspx?page=3&tabNo=4');
                      ]]>
                    </v:on-post>
		</v:button>
	    </span>
                </td>
              </tr>
      </table>
  </v:template>
      <v:template name="sec_tmpl_5" type="simple" enabled="-- equ (self.tab_no, 5)">
          <h2>User Certificates</h2>
          <p>Manage certificates used for authentication.</p>
	  <input type="hidden" value="5" name="tabNo" /> 
	  <table class="listing">
	      <tr class="listing_header_row">
		  <th>Subject</th>
		  <th>Created</th>
		  <th>Fingerprint</th>
		  <th>Login enabled</th>
		  <th>Action</th>
	      </tr>
		  <?vsp 
		  if (self.cert_del is not null)
		    delete from WA_USER_CERTS where UC_ID = self.cert_del and UC_U_ID = self.u_id;

              for (select UC_ID, UC_CERT, UC_LOGIN, UC_FINGERPRINT, UC_TS from WA_USER_CERTS where UC_U_ID = self.u_id order by UC_TS desc) do
              {
		  ?>
		  <tr>
		      <td><?vsp http_value (get_certificate_info (2, cast (UC_CERT as varchar), 0, ''), 'b'); ?></td>
		      <td><?vsp http_value (wa_abs_date (UC_TS), 'b'); ?></td>
		      <td><?vsp http_value (UC_FINGERPRINT, 'b'); ?></td>
		      <td><?V case when UC_LOGIN = 1 then 'Yes' else 'No' end ?></td>
		      <td>
			  <v:url name="ce1" value="Edit" url="--sprintf ('uiedit_sec.vspx?page=3&tabNo=5&cert_edit=%d', UC_ID)" render-only="1"/>
			  <v:url name="ce2" value="Delete" url="--sprintf ('uiedit_sec.vspx?page=3&tabNo=5&cert_del=%d', UC_ID)" render-only="1"/>
		      </td>
		  </tr>	    
		  <?vsp 
		    }
		  ?>
	  </table>
	  <br/>
          <h3>X.509 Certificate</h3>
			<table width="100%">
	      <?vsp if (length (self.certificate_content)) { ?>
            <tr>
              <th width="135px">
                Subject
              </th>
              <td align="left">
		  <?vsp
		  http_value (get_certificate_info (2, cast(self.certificate_content as varchar), 0, ''),'b');
		  ?>
              </td>
            </tr>
            <tr>
              <th width="135px">
                Agent ID
              </th>
              <td align="left">
		  <?vsp
  		          http_value (ODS.ODS_API.SSL_WEBID_GET (cast(self.certificate_content as varchar)),'b');
		  ?>
              </td>
            </tr>
            <tr>
              <th width="135px">
                Fingerprint
              </th>
              <td align="left">
        		  <?vsp
  		          http_value (get_certificate_info (6, cast(self.certificate_content as varchar), 0, ''),'b');
  		        ?>
              </td>
            </tr>
	      <?vsp } ?>
            <tr>
              <td colspan="2" align="left">
                <label>
                  <v:check-box name="cert_import" value="1" xhtml_id="cert_import" auto-submit="1">
                    <v:after-data-bind>
                      <![CDATA[
                        if (e.ve_is_post and e.ve_initiator is not null and e.ve_initiator.vc_name = control.vc_name)
                        {
                          control.ufl_selected := atoi (get_keyword ('cert_import', e.ve_params, '0'));
                        }
                      ]]>
                    </v:after-data-bind>
                  </v:check-box>
                  <b>Import from local file</b>
                </label>
              </td>
            </tr>
            <v:template type="simple" condition="self.cert_import.ufl_selected">
            <tr>
              <th width="135px">
                File to import
              </th>
              <td align="left">
                <input type="file" name="cert_file" />
              </td>
            </tr>
            </v:template>
            <v:template type="simple" condition="not self.cert_import.ufl_selected">
            <tr>
              <td colspan="2" align="left">
	    <v:textarea name="cert1" value="--self.certificate_content" xhtml_cols="70" xhtml_rows="20" />
	</td>
      </tr>
            </v:template>
      <tr>
	      <td colspan="2" align="left">
          <label>
            <v:check-box name="cert_login" value="1" xhtml_id="cert_login" >
             <v:after-data-bind>
               if (not e.ve_is_post)
                 control.ufl_selected := self.certificate_login;
             </v:after-data-bind>
            </v:check-box>
              <b>Enable Automatic WebID Login</b>
          </label>
 	      </td>
      </tr>
      <tr>
        <td colspan="2" class="ctrl">
          <span class="fm_ctl_btn">
              <v:button name="bt_cncl_6" value="Cancel" action="simple" >
                      <v:on-post>
                        <![CDATA[
                    self.vc_redirect ('uiedit_sec.vspx?page=3&tabNo=5');
                        ]]>
                      </v:on-post>
            </v:button>
            <v:button name="bt_set2" value="Save Certificate" action="simple">
              <v:on-post>
                <![CDATA[
  		    declare agent, mail varchar;
                        declare cert any;

                  if(self.vc_is_valid = 0)
                    return;

                        if (self.cert_import.ufl_selected)
                        {
                          cert := get_keyword ('cert_file', e.ve_params);
                        }
                        else
                        {
                          cert := self.cert1.ufl_value;
                        }
                        agent := ODS.ODS_API.SSL_WEBID_GET (cert);
                        mail := FOAF_SSL_MAIL_GET (cert);
                        if ((agent is null and mail is null and length (cert)) or (0 = length (cert)))
		    {
                      self.vc_is_valid := 0;
		      self.vc_error_message := 'The certificate must be in PEM format and must have Alternate Name attribute.';
		      return;
		    }
		    if (self.cert_edit is null)
                      {
		        insert into WA_USER_CERTS (UC_U_ID, UC_CERT, UC_FINGERPRINT, UC_LOGIN, UC_TS) 
                            values (self.u_id, cert, get_certificate_info (6, cert, 0, ''), self.cert_login.ufl_selected, now ());
		      }
		    else
		      {
                            update WA_USER_CERTS
                             set UC_CERT = cert,
                                 UC_FINGERPRINT = get_certificate_info (6, cert, 0, ''),
				UC_LOGIN = self.cert_login.ufl_selected
				where UC_U_ID = self.u_id and UC_ID = self.cert_edit ;
			self.cert_edit := null;	
		      }   
		    commit work;  
                    self.vc_redirect ('uiedit_sec.vspx?page=3&tabNo=5');
                ]]>
              </v:on-post>
            </v:button>
          </span>
        </td>
      </tr>
    </table>
  </v:template>
      <v:template name="sec_tmpl_6" type="simple" enabled="-- equ (self.tab_no, 6)">
	  <input type="hidden" value="6" name="tabNo" />
            <h3>X.509 Certificate Parameters</h3>
            <iframe id="cert" src="cert.vsp?sid=<?V self.sid ?>" width="650" height="270" frameborder="0" scrolling="auto">
  	            <p>Your browser does not support iframes.</p>
  	          </iframe>
  </v:template>
      <v:template name="sec_tmpl_7" type="simple" enabled="-- equ (self.tab_no, 7)">
	  <input type="hidden" value="7" name="tabNo" />
	  <table class="listing">
	      <tr class="listing_header_row">
		  <th>Name</th>
		  <th>Type</th>
		  <th>Action</th>
	      </tr>
		  <?vsp
		  declare key_no, sk int;
		  if (self.key_del is not null)
		    {
		      USER_KEY_DELETE (self.u_name, self.key_del);
                      set_user_id (self.u_name);
		      xenc_key_remove (self.key_del);
		      set_user_id ('dba');
		    }
		  key_no := 0;
		  sk := (select WAUI_SALMON_KEY from WA_USER_INFO where WAUI_U_ID = self.u_id);
              for (select xenc_key, xenc_type from ods_user_keys (username) (xenc_key varchar, xenc_type varchar) x where username = self.u_name) do
              {
		  ?>
		  <tr>
		      <td>
			  <?vsp if (sk = xenc_key) { ?>
			  <input type="radio" name="defk" value="<?V xenc_key ?>" checked="true"/>
			  <?vsp } else { ?>
			  <input type="radio" name="defk" value="<?V xenc_key ?>" />
			  <?vsp } ?>
			  <?vsp http_value (xenc_key, 'b'); ?>
		      </td>
		      <td><?vsp http_value (xenc_type, 'b'); ?></td>
		      <td>
			  <v:url name="ce7" value="Delete" url="--sprintf ('uiedit_sec.vspx?page=3&tabNo=7&key_del=%U', xenc_key)" render-only="1"/>
		      </td>
		  </tr>
		  <?vsp
		      key_no := key_no + 1;
		    }
		  if (not key_no)
		    http ('<tr><td colspan="4">No keys</td></tr>');   
		  ?>
	  </table>
	  <br/>
            <span class="fm_ctl_btn">
              <v:button name="bt_def_1" value="Remove Default" action="simple" >
              <v:on-post>
                <![CDATA[
		      update WA_USER_INFO set WAUI_SALMON_KEY = null where WAUI_U_ID = self.u_id;
		      self.vc_redirect ('uiedit_sec.vspx?page=3&tabNo=7');
                ]]>
              </v:on-post>
              </v:button>
              <v:button name="bt_def_2" value="Set Default" action="simple" >
              <v:on-post>
                <![CDATA[
		    declare k any;

		    k := get_keyword ('defk', params, null);
		      update WA_USER_INFO set WAUI_SALMON_KEY = k where WAUI_U_ID = self.u_id;
                    self.vc_redirect ('uiedit_sec.vspx?page=3&tabNo=7');
                ]]>
              </v:on-post>
	    </v:button>
	</span>
          <br/><br/>
    <fieldset>
			<legend><b>Private Key</b></legend>
            <table>
        <tr>
                <th width="135px">
                  Key Name (*)
                </th>
	      <td align="left">
		  <v:text name="kn" value="">
		  </v:text>
  	      </td>
        </tr>
        <tr>
                <td></td>
                <td>
                  <label>
		<v:check-box name="inpsel" value="1" xhtml_id="inpsel" auto-submit="1">
                      <v:after-data-bind>
                        <![CDATA[
			if (e.ve_is_post and e.ve_initiator is not null and e.ve_initiator.vc_name = control.vc_name)
			  {
                            control.ufl_selected := atoi (get_keyword ('inpsel', e.ve_params, '0')); 
			  }
                        ]]>
                      </v:after-data-bind>
		</v:check-box>
		    Import key from local file
		</label>
	    </td>
	</tr>
	<v:template type="simple" condition="self.inpsel.ufl_selected">
        <tr>
                  <th>
		File to import	
                  </th>
	      <td align="left">
		  <input type="file" name="k_value" value="Select Key"/>
  	      </td>
        </tr>
        <tr>
                  <th>
                    File Password
                  </th>
  	      <td align="left">
		  <v:text name="k_pwd" type="password" value="" /><br/>
	      </td>
	  </tr>
      </v:template>
	<v:template type="simple" condition="not self.inpsel.ufl_selected">
        <tr>
                  <th>
		Key Type
                  </th>
  	      <td align="left">
		  <v:select-list name="kt">
		      <v:item name="RSA" value="RSA"/>
		      <v:item name="DSA" value="DSA"/>
		  </v:select-list>
  	      </td>
        </tr>
        <tr>
                  <th>
		Key Length
                  </th>
  	      <td align="left">
		  <v:select-list name="kl">
		      <v:item name="512" value="512"/>
		      <v:item name="1024" value="1024"/>
		      <v:item name="2048" value="2048"/>
		  </v:select-list>
  	      </td>
        </tr>
    </v:template>
        <tr>
          <td colspan="2" class="ctrl">
            <span class="fm_ctl_btn">
              <v:button name="bt_cncl_7" value="Cancel" action="simple" >
                      <v:on-post>
                        <![CDATA[
                    self.vc_redirect ('uiedit_sec.vspx?page=3&tabNo=7');
                        ]]>
                      </v:on-post>
              </v:button>
              <v:button name="bt_gen7" value="Generate Key" action="simple" enabled="--equ (self.inpsel.ufl_selected, 0)">
                <v:on-post>
                  <![CDATA[
                    if (self.vc_is_valid = 0)
		      return;

		    declare exit handler for sqlstate '*', not found
		    {
		       self.vc_error_message := __SQL_MESSAGE;
		       self.vc_is_valid := 0;
		       return;
		    };
                          declare kn varchar;
                          declare kl integer;

                          kn := trim (self.kn.ufl_value);
                          if (not length (kn))
                      {
                            self.vc_error_message := 'Please specify Key Name';
			self.vc_is_valid := 0;
			return;
		      }  
		    kl := atoi (self.kl.ufl_value);
		    set_user_id (self.u_name); 
		    set_qualifier ('DB');
		    if (self.kt.ufl_value = 'RSA')
		      {
		        xenc_key_RSA_create (self.kn.ufl_value, kl);
		      }
		    else if (self.kt.ufl_value = 'DSA')
		      { 
		        xenc_key_DSA_create (self.kn.ufl_value, kl);
		      }
                          USER_KEY_STORE (self.u_name, kn, self.kt.ufl_value, 'DER', '');
                    set_user_id ('dba');
                    self.vc_redirect ('uiedit_sec.vspx?page=3&tabNo=7');
                  ]]>
                </v:on-post>
              </v:button>
              <v:button name="bt_set7" value="Import Key" action="simple" enabled="--equ (self.inpsel.ufl_selected, 1)">
                <v:on-post>
                  <![CDATA[
                    if (self.vc_is_valid = 0)
                      return;

		    declare exit handler for sqlstate '*', not found
		    {
		       self.vc_error_message := __SQL_MESSAGE;
		       self.vc_is_valid := 0;
		       return;
		    };
                          declare kn varchar;

                          kn := trim (self.kn.ufl_value);
                          if (not length (kn))
                          {
                            self.vc_error_message := 'Please specify Key Name';
                            self.vc_is_valid := 0;
                            return;
                          }
                          if (not length (get_keyword ('k_value', e.ve_params)))
                          {
                            self.vc_error_message := 'Please specify file to import';
                            self.vc_is_valid := 0;
                            return;
                          }
		    set_user_id (self.u_name);
                          USER_KEY_LOAD (kn, get_keyword ('k_value', e.ve_params), 'X.509', 'PKCS12', self.k_pwd.ufl_value, null, 1);
                    self.vc_redirect ('uiedit_sec.vspx?page=3&tabNo=7');
                  ]]>
                </v:on-post>
              </v:button>
            </span>
          </td>
        </tr>
      </table>
  </fieldset>
  </v:template>
    </v:form>
</div>
</div>
</v:page>
