home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _2209130097ac4734cddafb260644264b < prev    next >
Text File  |  2000-03-15  |  6KB  |  226 lines

  1. # Copyright (c) 1998 Graham Barr <gbarr@pobox.com>. All rights reserved.
  2. # This program is free software; you can redistribute it and/or
  3. # modify it under the same terms as Perl itself.
  4.  
  5. package URI::ldap;
  6.  
  7. use strict;
  8.  
  9. use vars qw(@ISA $VERSION);
  10. $VERSION = "1.10";
  11.  
  12. require URI::_server;
  13. @ISA=qw(URI::_server);
  14.  
  15. use URI::Escape qw(uri_unescape);
  16.  
  17.  
  18. sub default_port { 389 }
  19.  
  20. sub _ldap_elem {
  21.   my $self  = shift;
  22.   my $elem  = shift;
  23.   my $query = $self->query;
  24.   my @bits  = (split(/\?/,defined($query) ? $query : ""),("")x4);
  25.   my $old   = $bits[$elem];
  26.  
  27.   if (@_) {
  28.     my $new = shift;
  29.     $new =~ s/\?/%3F/g;
  30.     $bits[$elem] = $new;
  31.     $query = join("?",@bits);
  32.     $query =~ s/\?+$//;
  33.     $query = undef unless length($query);
  34.     $self->query($query);
  35.   }
  36.  
  37.   $old;
  38. }
  39.  
  40. sub dn {
  41.   my $old = shift->path(@_);
  42.   $old =~ s:^/::;
  43.   uri_unescape($old);
  44. }
  45.  
  46. sub attributes {
  47.   my $self = shift;
  48.   my $old = _ldap_elem($self,0, @_ ? join(",", map { my $tmp = $_; $tmp =~ s/,/%2C/g; $tmp } @_) : ());
  49.   return $old unless wantarray;
  50.   map { uri_unescape($_) } split(/,/,$old);
  51. }
  52.  
  53. sub scope {
  54.   my $self = shift;
  55.   my $old = _ldap_elem($self,1, @_);
  56.   return unless defined wantarray && defined $old;
  57.   uri_unescape($old) || "base";
  58. }
  59.  
  60. sub filter {
  61.   my $self = shift;
  62.   my $old = _ldap_elem($self,2, @_);
  63.   return unless defined wantarray && defined $old;
  64.   uri_unescape($old) || "(objectClass=*)";
  65. }
  66.  
  67. sub extensions {
  68.   my $self = shift;
  69.   my @ext;
  70.   while (@_) {
  71.     my $key = shift;
  72.     my $value = shift;
  73.     push(@ext, join("=", map { $_="" unless defined; s/,/%2C/g; $_ } $key, $value));
  74.   }
  75.   @ext = join(",", @ext) if @ext;
  76.   my $old = _ldap_elem($self,3, @ext);
  77.   return $old unless wantarray;
  78.   map { uri_unescape($_) } map { /^([^=]+)=(.*)$/ } split(/,/,$old);
  79. }
  80.  
  81. sub canonical
  82. {
  83.     my $self = shift;
  84.     my $other = $self->SUPER::canonical;
  85.  
  86.     # The stuff below is not as efficient as one might hope...
  87.  
  88.     $other = $other->clone if $other == $self;
  89.  
  90.     $other->dn(_normalize_dn($other->dn));
  91.  
  92.     # Should really know about mixed case "postalAddress", etc...
  93.     $other->attributes(map lc, $other->attributes);
  94.  
  95.     # Lowecase scope, remove default
  96.     my $old_scope = $other->scope;
  97.     my $new_scope = lc($old_scope);
  98.     $new_scope = "" if $new_scope eq "base";
  99.     $other->scope($new_scope) if $new_scope ne $old_scope;
  100.  
  101.     # Remove filter if default
  102.     my $old_filter = $other->filter;
  103.     $other->filter("") if lc($old_filter) eq "(objectclass=*)" ||
  104.                       lc($old_filter) eq "objectclass=*";
  105.  
  106.     # Lowercase extensions types and deal with known extension values
  107.     my @ext = $other->extensions;
  108.     for (my $i = 0; $i < @ext; $i += 2) {
  109.     my $etype = $ext[$i] = lc($ext[$i]);
  110.     if ($etype =~ /^!?bindname$/) {
  111.         $ext[$i+1] = _normalize_dn($ext[$i+1]);
  112.     }
  113.     }
  114.     $other->extensions(@ext) if @ext;
  115.     
  116.     $other;
  117. }
  118.  
  119. sub _normalize_dn  # RFC 2253
  120. {
  121.     my $dn = shift;
  122.  
  123.     return $dn;
  124.     # The code below will fail if the "+" or "," is embedding in a quoted
  125.     # string or simply escaped...
  126.  
  127.     my @dn = split(/([+,])/, $dn);
  128.     for (@dn) {
  129.     s/^([a-zA-Z]+=)/lc($1)/e;
  130.     }
  131.     join("", @dn);
  132. }
  133.  
  134. 1;
  135.  
  136. __END__
  137.  
  138. =head1 NAME
  139.  
  140. URI::ldap - LDAP Uniform Resource Locators
  141.  
  142. =head1 SYNOPSIS
  143.  
  144.   use URI;
  145.  
  146.   $uri = URI->new("ldap:$uri_string");
  147.   $dn     = $uri->dn;
  148.   $filter = $uri->filter;
  149.   @attr   = $uri->attributes;
  150.   $scope  = $uri->scope;
  151.   %extn   = $uri->extensions;
  152.   
  153.   $uri = URI->new("ldap:");  # start empty
  154.   $uri->host("ldap.itd.umich.edu");
  155.   $uri->dn("o=University of Michigan,c=US");
  156.   $uri->attributes(qw(postalAddress));
  157.   $uri->scope('sub');
  158.   $uri->filter('(cn=Babs Jensen)');
  159.   print $uri->as_string,"\n";
  160.  
  161. =head1 DESCRIPTION
  162.  
  163. C<URI::ldap> provides an interface to parse an LDAP URI in its
  164. constituent parts and also build a URI as described in
  165. RFC 2255.
  166.  
  167. =head1 METHODS
  168.  
  169. C<URI::ldap> support all the generic and server methods defined by
  170. L<URI>, plus the following.
  171.  
  172. Each of the following methods can be used to set or get the value in
  173. the URI. The values are passed in unescaped form.  None of these will
  174. return undefined values, but elements without a default can be empty.
  175. If arguments are given then a new value will be set for the given part
  176. of the URI.
  177.  
  178. =over 4
  179.  
  180. =item $uri->dn( [$new_dn] )
  181.  
  182. Set or get the I<Distinguised Name> part of the URI.  The DN
  183. identifies the base object of the LDAP search.
  184.  
  185. =item $uri->attributes( [@new_attrs] )
  186.  
  187. Set or get the list of attribute names which will be
  188. returned by the search.
  189.  
  190. =item $uri->scope( [$new_scope] )
  191.  
  192. Set or get the scope that the search will use. The value can be one of
  193. C<"base">, C<"one"> or C<"sub">. If none is given then it will default
  194. to C<"base">.
  195.  
  196. =item $uri->filter( [$new_filter] )
  197.  
  198. Set or get the filter that the search will use. If none is given then
  199. it will default to C<"(objectClass=*)">.
  200.  
  201. =item $uri->extensions( [$etype => $evalue,...] )
  202.  
  203. Set or get the extensions used for the search. The list passed should
  204. be in the form etype1 => evalue1, etype2 => evalue2,... This is also
  205. the form of list that will be returned.
  206.  
  207. =back
  208.  
  209. =head1 SEE ALSO
  210.  
  211. L<RFC-2255|http://www.cis.ohio-state.edu/htbin/rfc/rfc2255.html>
  212.  
  213. =head1 AUTHOR
  214.  
  215. Graham Barr E<lt>F<gbarr@pobox.com>E<gt>
  216.  
  217. Slightly modified by Gisle Aas to fit into the URI distribution.
  218.  
  219. =head1 COPYRIGHT
  220.  
  221. Copyright (c) 1998 Graham Barr. All rights reserved. This program is
  222. free software; you can redistribute it and/or modify it under the same
  223. terms as Perl itself.
  224.  
  225. =cut
  226.