20091006 displaying a jpeg stored on ldap - plembo/onemoretech GitHub Wiki

title: displaying a jpeg stored on ldap link: https://onemoretech.wordpress.com/2009/10/06/displaying-a-jpeg-stored-on-ldap/ author: lembobro description: post_id: 232 created: 2009/10/06 20:07:00 created_gmt: 2009/10/06 20:07:00 comment_status: closed post_name: displaying-a-jpeg-stored-on-ldap status: publish post_type: post

displaying a jpeg stored on ldap

If you're like me, you've established an inviolable rule that jpeg images may not be stored on your LDAP directory. [Note the code below was updated on 18 April 2013] Of course that rule wouldn' apply to your own projects. Here's a bit of perl cgi script that will read out the contents of the jpegphoto attribute and write it to a file for display by a .cgi script (the "tmp" directory referenced in the script is at the same level as the directory where the cgi script is invoked, and its contents can be displayed by the web server):

#!/usr/bin/perl -w
# Display jpegphoto for user
use strict;
use CGI;
use CGI::Carp;
use Net::LDAP;
use Net::LDAPS;
use Net::LDAP::Entry;
    
my $q = CGI->new;
    
print $q->header(-charset=>'UTF-8');
print $q->start_html();

print "n";    
print $q->h1("LDAP Photo");

my $dirHost = "ldap.example.com";
my $dirPort = "636";
my $targetID = "myuserid";
my $imgfile = "/var/www/html/images/$targetID.jpg";
my $imgurl = "/images/$targetID.jpg";

my $ldap = Net::LDAPS->new($dirHost,
			   port =>$dirPort,
			   verify =>'none',
			   version =>'3',
			);
my $mesg = $ldap->bind();
    
my $basedn = "dc=example,dc=com";
my $query = "(uid=$targetID)";
my @attrs = qw(cn uid jpegphoto);
    
$mesg = $ldap->search(
    base => $basedn,
    scope => 'sub',
    filter => $query,
    attr => @attrs
);
    
my $entry = $mesg->shift_entry();
my $cn = $entry->get_value('cn');
my $photo = $entry->get_value( 'jpegPhoto' );
    
if($photo) {
      open TMP, ">$imgfile" or die $!;
      binmode(TMP);
      $| = 1;
      print TMP $photo;
      close TMP;

      print $q->img({src=>"$imgurl",
	      alt=>"my photo",
              height=>"120",
              width=>"120"}
		 );
}
else {
      print "n";
      print "No photo in entryn";
}
print $q->h3("$cn");
print "n";
print $q->end_html();

Copyright 2004-2019 Phil Lembo