File: export_functions.php

package info (click to toggle)
phpldapadmin 0.9.5-3sarge3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,052 kB
  • ctags: 2,526
  • sloc: php: 21,258; sh: 262; makefile: 132; xml: 42
file content (857 lines) | stat: -rw-r--r-- 23,936 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
<?php
// $Header: /cvsroot/phpldapadmin/phpldapadmin/export_functions.php,v 1.20 2004/11/03 22:00:52 xrenard Exp $

/**
 * Fuctions and classes for exporting ldap entries to others formats
 * (LDIF,DSML,..)
 * An example is provided at the bottom of this file if you want implement yours. *
 * @package phpLDAPadmin
 * @author The phpLDAPadmin development team
 * @see  export.php and export_form.php
 */ 

// include common configuration definitions
include('common.php');

// registry for the exporters
$exporters = array();

$exporters[] = array('output_type'=>'ldif',
			     'desc' => 'LDIF',
			     'extension' => 'ldif'
			     );
      
$exporters[] = array('output_type'=>'dsml',
			     'desc' => 'DSML V.1',
			     'extension' => 'xml'
			     );

$exporters[] = array('output_type'=>'vcard',
			     'desc' => 'VCARD 2.1',
			     'extension' => 'vcf'
			     );
$exporters[] = array('output_type'=>'csv',
			     'desc' => $lang['csv_spreadsheet'],
			     'extension' => 'csv'
			     );

/**
 * This class encapsulate informations about the ldap server 
 * from which the export is done.
 * The following info are provided within this class: 
 *
 * $server_id:  the id of the server.
 * $base_dn:  if the source of the export is the ldap server, 
 *            it indicates the base dn of the search.
 * $query_filter: if the source of the export is the ldap server,
 *                 it indicates the query filter for the search.
 * $scope: if the source of the export is the ldap server,
 *         it indicates the scope of the search.
 * $server_host: the host name of the server.
 * $server_name: the name of the server.
 */

class LdapInfo{

  var $base_dn;
  var $query_filter;
  var $scope;
  var $server_host = NULL;
  var $server_name = NULL;
  var $server_id = NULL;

  /**
   * Create a new LdapInfo object
   *
   * @param int $server_id the server id
   * @param String $base_dn the base_dn for the search in a ldap server
   * @param String $query_filter the query filter for the search
   * @param String scope the scope of the search in a ldap server
   */

  function LdapInfo($server_id,$base_dn = NULL,$query_filter = NULL,$scope = NULL){
    global $servers;
    $this->base_dn = $base_dn;
    $this->query_filter = $query_filter;
    $this->scope = $scope;
    $this->server_name = $servers[ $server_id ][ 'name' ];
    $this->server_host = $servers[ $server_id ][ 'host' ];
    $this->server_id = $server_id;
  }
}


/**
 * This class represents the base class of all exporters
 * It can be subclassed directly if your intend is to write
 * a source exporter(ie. it will act only as a decoree 
 * which will be wrapped by an another exporter.)
 * If you consider writting an exporter for filtering data 
 * or directly display entries, please consider subclass
 * the PlaExporter
 *
 * @see PlaExporter
 */

class PlaAbstractExporter{

  /**
   * Return the number of entries
   * @return int the number of entries to be exported
   */
  function pla_num_entries(){}

  /**
   * Return true if there is some more entries to be processed
   * @return bool if there is some more entries to be processed
   */
  function pla_has_entry(){}

  /**
   * Return the entry as an array
   * @return array  an entry as an array
   */
  function pla_fetch_entry_array(){}

  /**
   * Return the entry as an Entry object
   * @return Entry an entry  as an Entry Object
   */
  function pla_fetch_entry_object(){}

  /**
   * Return a PlaLdapInfo Object
   * @return LdapInfo Object with info from the ldap serveur
   */
  function pla_get_ldap_info(){}

  /**
   * May be call when the processing is finished
   * and to free some ressources.
   * @return bool true or false if any errors is encountered
   */
  function pla_close(){}

}// end PlaAbstractExporter



/**
 * PlaExporter acts a wrapper around another exporter.
 * In other words, it will act as a decorator for another decorator
 */

class PlaExporter extends PlaAbstractExporter{
  // the default CRLN 
  var $br="\n";
  // the wrapped $exporter
  var $exporter;

  /**
   * Constructor
   * @param source $source the decoree for this exporter
   */
  function PlaExporter( $source ){
    $this->exporter = $source;
  }
  
  /**
   * Return the number of entries
   * @return int the number of entries to be exported
   */
  function pla_num_entries(){
    return $this->exporter->pla_num_entries();
  }

  /**
   * Return true if there is some more entries to be processed
   * @return bool if there is some more entries to be processed
   */
  function pla_has_entry(){
    return $this->exporter->pla_has_entry();
  }

  /**
   * Return the entry as an array
   * @return array  an entry as an array
   */  
  function pla_fetch_entry_array(){
    return $this->exporter->pla_fetch_entry_array();
  }

  /**
   * Return the entry as an Entry object
   * @return Entry an entry  as an Entry Object
   */
  function pla_fetch_entry_object(){
      return $this->exporter->pla_fetch_entry_object();
  }

  /**
   * Return a PlaLdapInfo Object
   * @return LdapInfo Object with info from the ldap serveur
   */
  function pla_get_ldap_info(){
    return $this->exporter->pla_get_ldap_info();
  }

  /**
   * May be call when the processing is finished
   * and to free some ressources.
   * @return bool false if any errors are encountered,false otherwise
   */
  function pla_close(){
    return $this->exporter->pla_close();
  }

  /**
   * Helper method to check if the attribute value should be base 64 encoded.
   * @param String $str the string to check.
   * @return bool true if the string is safe ascii, false otherwise.
   */
  function is_safe_ascii( $str ){
    for( $i=0; $i<strlen($str); $i++ )
      if( ord( $str{$i} ) < 32 || ord( $str{$i} ) > 127 )
	return false;
    return true;
  }
    
  /**
   * Abstract method use to export data. 
   * Must be implemented in a sub-class if you write an exporter 
   * which export data.
   * Leave it empty if you write a sub-class which do only some filtering.
   */
  function export(){}
  
  /**
   * Set the carriage return /linefeed for the export
   * @param String $br the CRLF to be set
   */
  function setOutputFormat( $br ){
    $this->br = $br;
  }

}// end PlaExporter


/**
 * Export data from a ldap server
 * @extends PlaAbstractExporter
 */

class PlaLdapExporter extends PlaAbstractExporter{
  var $entry_id;
  var $results;
  var $server_id;
  var $scope;
  var $entry_array;
  var $num_entries;
  var $ldap_info;
  var $queryFilter;
  var $hasNext;
  var $connection_open_state;
  var $attributes;
  /**
   * Create a PlaLdapExporter object.
   * @param int $server_id the server id
   * @param String $queryFilter the queryFilter for the export
   * @param String $base_dn the base_dn for the data to export
   * @param String $scope the scope for export
   */
  function PlaLdapExporter( $server_id , $queryFilter , $base_dn , $scope, $attributes){
    global $lang;
    $this->scope = $scope;
    $this->base_dn = $base_dn;
    $this->server_id = $server_id;
    $this->queryFilter = $queryFilter;
    // infos for the server
    $this->ldap_info = new LdapInfo($server_id,$base_dn,$queryFilter,$scope);
    // boolean to check if there is more entries
    $this->hasNext = 0;
    // boolean to check the state of the connection
    $this->connection_open_state = 0;

    $this->attributes = $attributes;
    // connect to the server
    $this->ds = @pla_ldap_connect( $this->server_id );
    pla_ldap_connection_is_error( $this->ds );
    $this->connection_open_state = 1;

    // get the data to be exported
    if( $this->scope == 'base' )
      $this->results = @ldap_read( $this->ds, $this->base_dn, $this->queryFilter,$this->attributes, 
				   0, 0, 0, get_export_deref_setting() );
    elseif( $this->scope == 'one' )
      $this->results = @ldap_list( $this->ds, $this->base_dn, $this->queryFilter, $this->attributes, 
				   0, 0, 0, get_export_deref_setting() );
    else // scope == 'sub'
      $this->results = @ldap_search( $this->ds, $this->base_dn, $this->queryFilter, $this->attributes, 
				     0, 0, 0, get_export_deref_setting() );
    
    // if no result, there is a something wrong
    if( ! $this->results ) 
      pla_error(  $lang['error_performing_search'], ldap_error( $this->ds ), ldap_errno( $this->ds ) );
    
    // get the number of entries to be exported
    $this->num_entries = @ldap_count_entries( $this->ds,$this->results );

    if( $this->entry_id = @ldap_first_entry( $this->ds,$this->results ) ){
      $this->hasNext = 1;
    }
  }//end constructor

  /**
   * Return the entry as an array
   * @return array  an entry as an array
   */  
  function pla_fetch_entry_array(){
    return $this->entry_array;
  }

  /**
   * Return the entry as an Entry object
   * @return Entry an entry  as an Entry Object
   */
  function pla_fetch_entry_object(){
    // to do
  }
  
  /**
   * Return a PlaLdapInfo Object
   * @return LdapInfo Object with info from the ldap serveur
   */
  function pla_get_ldap_info(){
    return $this->ldap_info;
  }

  /**
   * Return the number of entries
   * @return int the number of entries to be exported
   */
  function pla_num_entries(){
    return $this->num_entries;
  }

  /**
   * Return true if there is some more entries to be processed
   * @return bool if there is some more entries to be processed
   */
  function pla_has_entry(){
    if( $this->hasNext ){
      unset( $this->entry_array );
      $dn = @ldap_get_dn( $this->ds,$this->entry_id );
      $this->entry_array['dn'] = $dn;
      
      //get the attributes of the entry
      $attrs = @ldap_get_attributes($this->ds,$this->entry_id);
      if( $attr = @ldap_first_attribute( $this->ds,$this->entry_id,$attrs ) ){
	
	//iterate over the attributes
	while( $attr ){
	  if( is_attr_binary( $this->server_id,$attr ) ){
	    $this->entry_array[$attr] = @ldap_get_values_len( $this->ds,$this->entry_id,$attr );
	  }
	  else{
	    $this->entry_array[$attr] = @ldap_get_values( $this->ds,$this->entry_id,$attr );
	  }
	  unset( $this->entry_array[$attr]['count'] );
	  $attr = @ldap_next_attribute( $this->ds,$this->entry_id,$attrs );
	}// end while attr
	
	if(!$this->entry_id = @ldap_next_entry( $this->ds,$this->entry_id ) ){
	  $this->hasNext = 0;
	}
      }// end if attr
      return true;
    }
    else{
      $this->pla_close();
      return false;
    }
  }
 
  /**
   * May be call when the processing is finished
   * and to free some ressources.
   * @return bool true or false if any errors is encountered
   */
  function pla_close(){
    if($this->connection_open_state){
      return @ldap_close( $this->ds );
    }
    else{
      return true;
    }
  }
} // end PlaLdapExporter

/**
 * Export entries to ldif format
 * @extends PlaExporter
 */

class PlaLdifExporter extends PlaExporter{

  // variable to keep the count of the entries
  var $counter = 0;

  // the maximum length of the ldif line
  var $MAX_LDIF_LINE_LENGTH = 76;

  /**
   * Create a PlaLdifExporter object
   * @param PlaAbstractExporter $exporter the source exporter
   */
  function PlaLdifExporter( $exporter ){
    $this->exporter = $exporter;
  }

  /**
   * Export entries to ldif format
   */
  function export(){
    $pla_ldap_info = $this->pla_get_ldap_info();
    $this->displayExportInfo($pla_ldap_info);

    //While there is an entry, fecth the entry as an array 
    while($this->pla_has_entry()){
      $entry = $this->pla_fetch_entry_array();
      $this->counter++;
      
      // display comment before each entry
      global $lang;
      $title_string = "# " . $lang['entry'] . " " . $this->counter . ": " . $entry['dn'] ; 
      if( strlen( $title_string ) > $this->MAX_LDIF_LINE_LENGTH-3 )
	$title_string = substr( $title_string, 0, $this->MAX_LDIF_LINE_LENGTH-3 ) . "...";
      echo "$title_string$this->br";

      // display dn
      if( $this->is_safe_ascii( $entry['dn'] ))
	$this->multi_lines_display("dn:". $entry['dn']);
      else
	$this->multi_lines_display("dn:: " . base64_encode( $entry['dn'] ));
      array_shift($entry);

      // display the attributes
      foreach( $entry as $key => $attr ){
	foreach( $attr as $value ){
	  if(  !$this->is_safe_ascii($value) || is_attr_binary($pla_ldap_info->server_id,$key ) ){
	    $this->multi_lines_display( $key.":: " . base64_encode( $value ) );
	  }
	  else{
	    $this->multi_lines_display( $key.": ".$value );
	  }
	}
      }// end foreach $entry

      echo $this->br;
      // flush every 5th entry (sppeds things up a bit)
      if( 0 == $this->counter % 5 )
	flush();
    }
  }
  
  // display info related to this export
  function displayExportInfo($pla_ldap_info){
    global $lang;
    echo "version: 1$this->br$this->br";
    echo "# " . sprintf( $lang['ldif_export_for_dn'],  $pla_ldap_info->base_dn  ) . $this->br;
    echo "# " . sprintf( $lang['generated_on_date'], date("F j, Y g:i a") ) . $this->br;
    echo "# " . $lang['server'] . ": " .$pla_ldap_info->server_name  . " (" . $pla_ldap_info->server_host . ")" . $this->br;
    echo "# " . $lang['search_scope'] . ": " . $pla_ldap_info->scope . $this->br;
    echo "# " . $lang['total_entries'] . ": " . $this->pla_num_entries() . $this->br; 
    echo $this->br;
  }

  /**
   * Helper method to wrap ldif lines
   * @param String $str the line to be wrapped if needed.
   */
  function multi_lines_display( $str ){
    
    $length_string = strlen($str);
    $max_length = $this->MAX_LDIF_LINE_LENGTH;
    
    while ($length_string > $max_length){
      echo substr($str,0,$max_length).$this->br." ";
      $str= substr($str,$max_length,$length_string);
      $length_string = strlen($str);
      
      // need to do minus one to align on the right
      // the first line with the possible following lines 
      // as these will have an extra space
      $max_length = $this->MAX_LDIF_LINE_LENGTH-1;
    }
    echo $str."".$this->br;
  }

}

/**
 * Export entries to DSML v.1
 * @extends PlaExporter
 */

class PlaDsmlExporter extends PlaExporter{

  //not in use
  var $indent_step = 2;
  var $counter = 0;

  /**
   * Create a PlaDsmlExporter object
   * @param PlaAbstractExporter $exporter the decoree exporter
   */
  function PlaDsmlExporter( $exporter ){
    $this->exporter = $exporter;
  }

  /**
   * Export the entries to DSML
   */
   function export(){
     global $lang;
     $pla_ldap_info = $this->pla_get_ldap_info();
     // not very elegant, but do the job for the moment as we have just 4 level
     $directory_entries_indent = "  ";
     $entry_indent= "    ";
     $attr_indent = "      ";
     $attr_value_indent = "        ";

     // print declaration
     echo "<?xml version=\"1.0\"?>$this->br";
     
     // print root element     
     echo "<dsml>$this->br";
     
     // print info related to this export
     echo "<!-- " . $this->br;
     echo "# " . sprintf( $lang['dsml_export_for_dn'],  $pla_ldap_info->base_dn  ) . $this->br;
     echo "# " . sprintf( $lang['generated_on_date'], date("F j, Y g:i a") ) . $this->br;
     echo "# " . $lang['server'] . ": " .  $pla_ldap_info->server_name  . " (" . $pla_ldap_info->server_host . ")" . $this->br;
     echo "# " . $lang['search_scope'] . ": " . $pla_ldap_info->scope . $this->br;
     echo "# " . $lang['total_entries'] . ": " . $this->pla_num_entries() . $this->br; 
     echo "-->" . $this->br;


     echo $directory_entries_indent."<directory-entries>$this->br";
     //While there is an entry, fetch the entry as an array 
      while($this->pla_has_entry()){
	$entry = $this->pla_fetch_entry_array();
	$this->counter++;
	// display dn
	echo $entry_indent."<entry dn=\"". htmlspecialchars( $entry['dn'] ) ."\">".$this->br;
	array_shift($entry);
	
	// echo the objectclass attributes first
	if(isset($entry['objectClass'])){
	  echo $attr_indent."<objectClass>".$this->br;
	  foreach($entry['objectClass'] as $ocValue){
	    echo $attr_value_indent."<oc-value>$ocValue</oc-value>".$this->br;
	  }
	  echo $attr_indent."</objectClass>".$this->br;
	  unset($entry['objectClass']);
	}
	
	$binary_mode = 0;
	// display the attributes
	foreach($entry as $key=>$attr){
	  echo $attr_indent."<attr name=\"$key\">".$this->br;
	  
	  // if the attribute is binary, set the flag $binary_mode to true
	  $binary_mode = is_attr_binary($pla_ldap_info->server_id,$key)?1:0;
	  
	  foreach($attr as $value){
	    echo $attr_value_indent."<value>".($binary_mode?base64_encode( $value):  htmlspecialchars( $value ) )."</value>".$this->br;
	  }
	  echo $attr_indent."</attr>".$this->br;
	}// end foreach $entry
	echo $entry_indent."</entry>".$this->br;
	
	// flush every 5th entry (speeds things up a bit)
	if( 0 == $this->counter % 5 )
	  flush();
      }
      echo $directory_entries_indent."</directory-entries>$this->br";
      echo "</dsml>".$this->br;
   }
}


class PlaVcardExporter extends PlaExporter{

  // mappping one to one attribute
  var $vcardMapping = array('cn' => 'FN',
			    'title' => 'TITLE',
			    'homePhone' => 'TEL;HOME',
			    'mobile' => 'TEL;CELL',
			    'mail' => 'EMAIL;Internet',
			    'labeledURI' =>'URL',
			    'o' => 'ORG',
			    'audio' => 'SOUND',
			    'facsmileTelephoneNumber' =>'TEL;WORK;HOME;VOICE;FAX',
			    'jpegPhoto' => 'PHOTO;ENCODING=BASE64',
			    'businessCategory' => 'ROLE',
			    'description' => 'NOTE'
			    );
  
  var $deliveryAddress  = array("postOfficeBox",
				"street",
				"l",
				"st",
				"postalCode",
				"c");

  function PlaVcardExporter($exporter){
    $this->exporter = $exporter;
  }

  /**
   * When doing an exporter, the method export need to be overriden.
   * A basic implementation is provided here. Customize to your need
   **/

  function export(){

    // With the method pla->get_ldap_info,
    // you have access to some values related
    // to you ldap server
    $ldap_info = $this->pla_get_ldap_info();
    $base_dn = $ldap_info->base_dn;
    $server_id = $ldap_info->server_id;
    $scope = $ldap_info->scope;
    $server_name = $ldap_info->server_name;
    $server_host = $ldap_info->server_host;

    while( $this->pla_has_entry() ){
      $entry = $this->pla_fetch_entry_array();

      //fetch the dn
      $dn = $entry['dn'];
      unset( $entry['dn'] );

      // check the attributes needed for the delivery address
      // field
      $addr = "ADR:";
      foreach( $this->deliveryAddress as $attr_name ){
	if( isset( $entry[$attr_name] ) ){
	  $addr .= $entry[$attr_name][0];
	  unset($entry[$attr_name]);
	}
	$addr .= ';';
      }
      echo "BEGIN:VCARD$this->br";

      // loop for the attributes
      foreach( $entry as $attr_name=>$attr_values ){

	// if an attribute of the ldap entry exist
	// in the mapping array for vcard
	if( isset( $this->vcardMapping[$attr_name] ) ){

	  // case of organisation. Need to append the
	  // possible ou attribute
	  if( 0 == strcasecmp( $attr_name , 'o' )){
	    echo $this->vcardMapping[$attr_name].":$attr_values[0]";
	    if( isset($entry['ou'] ) )
	      foreach( $entry['ou'] as $ou_value ){
		echo ";$ou_value";
	      }
	  }
	  // the attribute is binary. (to do : need to fold the line)
	  else if( 0 == strcasecmp( $attr_name,'audio') || 0 == strcasecmp( $attr_name,'jpegPhoto') ){
	    echo $this->vcardMapping[$attr_name].":$this->br";
	    echo " ".base64_encode( $attr_values[0]);
	  }
	  /*	  else if( $attr_name == "sn"){
	    echo $this->vcardMapping[$attr_name].":$attr_values[0]";
	  }
	  elseif( $attr_name == "homePostalAddress"){
          }*/
	  // else just print the value with the relevant attribute name
	  else{
	    echo $this->vcardMapping[$attr_name].":$attr_values[0]";
	  }
	  echo $this->br;
	}
      }
      // need to check
      echo "UID:$dn";
      echo $this->br;
      echo "VERSION:2.1";
      echo $this->br;
      echo $addr;
      echo $this->br;
      echo "END:VCARD";
      echo $this->br;
    }// end while
  }
}




/**
 * Export to cvs format
 *
 * @author Glen Ogilvie
 */

class PlaCSVExporter extends PlaExporter{

  function PlaCSVExporter($exporter){
    $this->exporter = $exporter;
  }
  
  /**
   * When doing an exporter, the method export need to be overriden.
   * A basic implementation is provided here. Customize to your need
   **/
  
  var $separator = ",";
  var $qualifier = '"';
  var $multivalue_separator = " | ";
  var $escapeCode = '"';
  
  function export(){
    
    // With the method pla->get_ldap_info,
    // you have access to some values related
    // to you ldap server
    $ldap_info = $this->pla_get_ldap_info();
    $base_dn = $ldap_info->base_dn;
    $server_id = $ldap_info->server_id;
    $scope = $ldap_info->scope;
    $server_name = $ldap_info->server_name;
    $server_host = $ldap_info->server_host;
    
    $entries = array();
    $headers = array();
		
    // go thru and find all the attribute names first.  This is needed, because, otherwise we have
    // no idea as to which search attributes were actually populated with data
    while( $this->pla_has_entry() ) {
      $entry = $this->pla_fetch_entry_array();
      foreach (array_keys($entry) as $key) {
	if (!in_array($key, $headers)) 
	  array_push($headers,$key);
      }
      array_push($entries, $entry);
    }
    
    $num_headers = count($headers);
    
    // print out the headers
    for ($i = 0; $i < $num_headers; $i++) {
      echo $this->qualifier. $headers[$i].$this->qualifier;
      if ($i < $num_headers-1) 
	echo $this->separator;
    }
    
    array_shift($headers);
    $num_headers--;
    
    echo $this->br;
	
    // loop on every entry
    foreach ($entries as $entry) {
      
      //print the dn
      $dn = $entry['dn'];
      unset( $entry['dn'] );
      echo $this->qualifier. $this->LdapEscape($dn).$this->qualifier.$this->separator;
      
      // print the attributes
      for($j=0;$j<$num_headers;$j++){

	$attr_name = $headers[$j];

	echo $this->qualifier;
	if (key_exists($attr_name, $entry)) {
	  $binary_attribute = is_attr_binary( $server_id, $attr_name )?1:0;
	  
	  $attr_values = $entry[$attr_name];
	  
	  $num_attr_values = count( $attr_values );
	  for( $i=0 ; $i<$num_attr_values; $i++){
	    if($binary_attribute)
	      echo base64_encode($attr_values[$i]);
	    else
	      echo $this->LdapEscape($attr_values[$i]);
	    
	    if($i < $num_attr_values - 1)
	      echo $this->multivalue_separator;
	    
	  }
	}// end if key
	echo $this->qualifier;
	if( $j < $num_headers - 1 )
	  echo $this->separator;
      }
          echo $this->br;
    }
  }//end export
  
  // function to escape data, where the qualifier happens to also 
  // be in the data.
  function LdapEscape ($var) {
    return str_replace($this->qualifier, $this->escapeCode.$this->qualifier, $var);
  }
}





class MyCustomExporter extends PlaExporter{

  function MyCutsomExporter($exporter){
    $this->exporter = $exporter;
  }

  /**
   * When doing an exporter, the method export need to be overriden.
   * A basic implementation is provided here. Customize to your need
   **/


  function export(){

    // With the method pla->get_ldap_info,
    // you have access to some values related
    // to you ldap server
    $ldap_info = $this->pla_get_ldap_info();
    $base_dn = $ldap_info->base_dn;
    $server_id = $ldap_info->server_id;
    $scope = $ldap_info->scope;
    $server_name = $ldap_info->server_name;
    $server_host = $ldap_info->server_host;


    // Just a simple loop. For each entry 
    // do your custom export
    // see PlaLdifExporter or PlaDsmlExporter as an example 
    while( $this->pla_has_entry() ){
      $entry = $this->pla_fetch_entry_array();
      
      //fetch the dn
      $dn = $entry['dn'];
      unset( $entry['dn'] );
	    
      // loop for the attributes
      foreach( $entry as $attr_name=>$attr_values ){
	foreach( $attr_values as $value ){
	  
	  // simple example
	  // echo "Attribute Name:".$attr_name;
	  //  echo " - value:".$value;
	  // echo $this->br;
	  }
      }
      
    }// end while
  }

}
?>