<?xml version="1.0"?>
<!--
 -
 -  $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="blog-home-page"
        xmlns:vm="http://www.openlinksw.com/vspx/ods/"
        xmlns:v="http://www.openlinksw.com/vspx/"
        style="index.xsl"
        doctype="-//W3C//DTD XHTML 1.0 Transitional//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <vm:page>
    <vm:header>
      <vm:title>LDAP Search</vm:title>
    </vm:header>
    <vm:pagewrapper>
      <vm:rawheader caption="LDAP Search"/>
      <v:variable name="res" type="any" default="null"/>
      <vm:body>
      <script type="text/javascript">
  <![CDATA[
function selectAllCheckboxes (form, btn, txt)
{
  var i;
  for (i =0; i < form.elements.length; i++)
    {
      var contr = form.elements[i];
      if (contr != null && contr.type == "checkbox" && contr.name.indexOf (txt) != -1)
        {
    contr.focus();
    if (btn.value == 'Select All')
      contr.checked = true;
    else
            contr.checked = false;
  }
    }
  if (btn.value == 'Select All')
    btn.value = 'Unselect All';
  else
    btn.value = 'Select All';
  btn.focus();
}

function retSelected (form, txt)
{
  var i, ret;
  ret = '';
  for (i = 0; i < form.elements.length; i++)
    {
      var contr = form.elements[i];
      if (contr != null && contr.type == "checkbox" && contr.name.indexOf (txt) != -1 && contr.checked == true)
        {
          ret = ret + contr.value + ', ';
        }
    }
  if (opener == null || opener.em_to == null)
    return;

  if (opener.em_to.value.length)
    ret = ', ' + ret;

  if (ret.length > 0)
    opener.em_to.value = opener.em_to.value + ret;

  opener.focus ();
  close ();
}

]]>
</script>
	<vm:login redirect="index.vspx"/>
         <a href="#" style="text-decoration:none;" onclick="if (opener != null) { opener.focus(); window.close (); } return false;"><img title="Close" alt="Close" src="images/icons/close_16.png" border="0" hspace="3" />Close</a>
	 <v:form type="simple" method="POST" name="ldapf">
	   <table border="0" width="100%" height="100%" cellpadding="0" cellspacing="0">
	     <tr valign='top'>
	       <td>
		 <table class="ctl_grp">
		   <tr>
		     <td colspan="2"><img src="images/icons/filter_24.png"/></td>
		   </tr>
		   <tr>
		     <th>LDAP Server</th>
		     <td>
		       <v:data-list name="ldaps"
			 sql="select LS_ADDRESS from SYS_LDAP_SERVERS"
			 key-column="LS_ADDRESS"
			 value-column="LS_ADDRESS"
			 />
		     </td>
		   </tr>
		   <tr>
		     <th>Filter</th>
		     <td>
		       <v:text name="filt" value=""/>
		     </td>
		   </tr>
		   <tr>
		     <td colspan="2" class="fm_ctl_btn">
		       <span class="fm_ctl_btn">
		       <v:button name="ldap_bts" action="simple" value="Search" >
			 <v:on-post><![CDATA[
			   declare host, port, base, bind, pwd, srch any;
			   declare ver, ufld, uid, hf any;
			   declare tmp any;

			   declare exit handler for sqlstate '*', not found
			   {
			     self.vc_is_valid := 0;
			     if (isstring (__SQL_STATE))
			       self.vc_error_message := __SQL_MESSAGE;
			     else
			       self.vc_error_message := 'LDAP lookup failed';
			   };

			   self.filt.ufl_value := trim (self.filt.ufl_value);
			   if (length (self.filt.ufl_value) = 0)
			     signal ('22023', 'The search criteria is not entered');

			   select LS_BASE, LS_LDAP_VERSION, LS_BIND_DN, LS_UID_FLD, LS_ACCOUNT, LS_PASSWORD
			    into base, ver, bind,  ufld, uid, pwd
			    from SYS_LDAP_SERVERS where LS_ADDRESS = self.ldaps.ufl_value;

			    bind := sprintf ('%s=%s, %s', ufld, uid, bind);
			    hf := WS.WS.PARSE_URI (self.ldaps.ufl_value);

			    srch := sprintf ('(cn=%s*)', self.filt.ufl_value);

			    connection_set ('LDAP_VERSION', ver);
			    host := 'ldap://'|| hf[1];
			    self.res := ldap_search (host, 0, base, srch, bind, pwd);
			    ]]></v:on-post>
		       </v:button>
		     </span>
		     </td>
		   </tr>
		 </table>
		 <?vsp if (length  (self.res)) { ?>
		 <h2>Results from search</h2>
		 <table class="listing">
		   <tr class="listing_header_row">
		     <th>
		       <input type="checkbox" value="Unselect All" onclick="selectAllCheckboxes(this.form, this, 'cb1')" checked="1"/>
		       Name
		     </th>
		     <th>E-Mail</th>
		   </tr>
		   <?vsp
		     declare i, l, j  int;
		     declare elm, dta any;
		     dta := self.res;
		     l := length  (self.res);
		     j := 0;
		     for (i := 0; i < l; i := i + 2)
		       {
		         elm := dta[i];
			 if (elm = 'entry')
			   {
			     declare nam, mail any;
			     nam := get_keyword ('uid', dta[i+1]);
			     mail := get_keyword ('mail', dta[i+1]);
		   ?>
		   <tr class="<?V case when mod(j,2) then 'listing_row_odd' else 'listing_row_even' end ?>">
		     <td>
		       <input type="checkbox" name="cb1_<?V j ?>" value="<?V mail[0] ?>" checked="1" />
		       <?V nam[0] ?>
		     </td>
		     <td><?V mail[0] ?></td>
		   </tr>
		   <?vsp
		              j := j + 1;
		           }
		       }
	           ?>
		 <tr>
		   <td colspan="2">
		     <?vsp if (j > 0) { ?>
		     <span class="fm_ctl_btn">
		       <input type="button" name="ret_mail" value="Use selected" onclick="javascript: retSelected (this.form, 'cb1')"/>
		     </span>
		     <?vsp } else { ?>
		     No results
		     <?vsp } ?>
		   </td>
		 </tr>
		 </table>
		   <?vsp
		   }
	         ?>
	       </td>
	     </tr>
	   </table>
	 </v:form>
      </vm:body>
    </vm:pagewrapper>
  </vm:page>
</v:page>

