1 <?php
2 /*****************************************************************************
3 newldap.inc - version 1.0
4 Copyright (C) 2003 Alejandro Escanero Blanco <alex@ofmin.com>
5 Copyright (C) 2004 Cajus Pollmeier <pollmeier@gonicus.de>
7 Based in code of ldap.inc of
8 Copyright (C) 1998 Eric Kilfoil <eric@ipass.net>
9 *****************************************************************************/
11 define("ALREADY_EXISTING_ENTRY",-10001);
12 define("UNKNOWN_TOKEN_IN_LDIF_FILE",-10002);
13 define("NO_FILE_UPLOADED",10003);
14 define("INSERT_OK",10000);
15 define("COLON_OVERRIDE", TRUE);
17 class LDAP{
19 var $hascon =false;
20 var $hasres =false;
21 var $reconnect=false;
22 var $tls = false;
23 var $basedn ="";
24 var $cid;
25 var $error = ""; // Any error messages to be returned can be put here
26 var $start = 0; // 0 if we are fetching the first entry, otherwise 1
27 var $objectClasses = array(); // Information read from slapd.oc.conf
28 var $binddn = "";
29 var $bindpw = "";
30 var $hostname = "";
31 var $follow_referral = FALSE;
32 var $referrals= array();
34 function LDAP($binddn,$bindpw, $hostname, $follow_referral= FALSE, $tls= FALSE)
35 {
36 $this->follow_referral= $follow_referral;
37 $this->tls=$tls;
38 $this->binddn=$this->convert($binddn);
40 $this->bindpw=$bindpw;
41 $this->hostname=$hostname;
42 $this->connect();
43 }
46 function convert($dn)
47 {
48 if (COLON_OVERRIDE == TRUE){
49 $res= preg_replace("/\\\\,/", '###GOSAREPLACED###', $dn);
50 $res= preg_replace("/\\\\2C/", '###GOSAREPLACED###', $res);
51 #if ($dn != $res){
52 # echo "Conversation from '$dn' to '$res'<br>";
53 #}
54 return ($res);
55 } else {
56 return ($dn);
57 }
58 }
61 function fix($dn)
62 {
63 if (COLON_OVERRIDE == TRUE){
64 $res= preg_replace("/###GOSAREPLACED###/", '\,', $dn);
65 #if ($dn != $res){
66 # echo "Fix from '$dn' to '$res'<br>";
67 #}
68 return ($res);
69 } else {
70 return ($dn);
71 }
72 }
75 function connect()
76 {
77 $this->hascon=false;
78 $this->reconnect=false;
79 if ($this->cid= @ldap_connect($this->hostname)) {
80 @ldap_set_option($this->cid, LDAP_OPT_PROTOCOL_VERSION, 3);
81 if (function_exists("ldap_set_rebind_proc") && $this->follow_referral) {
82 @ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
83 @ldap_set_rebind_proc($this->cid, array(&$this, "rebind"));
84 }
85 if (function_exists("ldap_start_tls") && $this->tls){
86 @ldap_start_tls($this->cid);
87 }
89 $this->error = "No Error";
90 if ($bid = @ldap_bind($this->cid, $this->fix($this->binddn), $this->bindpw)) {
91 $this->error = "Success";
92 $this->hascon=true;
93 } else {
94 if ($this->reconnect){
95 if ($this->error != "Success"){
96 $this->error = "Could not rebind to " . $this->binddn;
97 }
98 } else {
99 $this->error = "Could not bind to " . $this->binddn;
100 }
101 }
102 } else {
103 $this->error = "Could not connect to LDAP server";
104 }
105 }
107 function rebind($ldap, $referral)
108 {
109 $credentials= $this->get_credentials($referral);
110 if (@ldap_bind($ldap, $this->fix($credentials['ADMIN']), $credentials['PASSWORD'])) {
111 $this->error = "Success";
112 $this->hascon=true;
113 $this->reconnect= true;
114 return (0);
115 } else {
116 $this->error = "Could not bind to " . $credentials['ADMIN'];
117 return NULL;
118 }
119 }
121 function reconnect()
122 {
123 if ($this->reconnect){
124 @ldap_unbind($this->cid);
125 $this->cid = NULL;
126 }
127 }
129 function unbind()
130 {
131 @ldap_unbind($this->cid);
132 $this->cid = NULL;
133 }
135 function disconnect()
136 {
137 if($this->hascon){
138 @ldap_close($this->cid);
139 $this->hascon=false;
140 }
141 }
143 function cd($dir)
144 {
145 if ($dir == "..")
146 $this->basedn = $this->getParentDir();
147 else
148 $this->basedn = $this->convert($dir);
149 }
151 function getParentDir($basedn = "")
152 {
153 if ($basedn=="")
154 $basedn = $this->basedn;
155 else
156 $basedn = $this-convert($this->basedn);
157 return(ereg_replace("[^,]*[,]*[ ]*(.*)", "\\1", $basedn));
158 }
160 function search($filter, $attrs= array())
161 {
162 if($this->hascon){
163 if ($this->reconnect) $this->connect();
164 $this->clearResult();
165 $this->sr = @ldap_search($this->cid, $this->fix($this->basedn), $filter, $attrs);
166 $this->error = @ldap_error($this->cid);
167 $this->resetResult();
168 $this->hasres=true;
170 return($this->sr);
171 }else{
172 $this->error = "Could not connect to LDAP server";
173 return("");
174 }
175 }
177 function ls($filter = "(objectclass=*)", $basedn = "",$attrs = array("*"))
178 {
179 if($this->hascon){
180 if ($this->reconnect) $this->connect();
181 $this->clearResult();
182 if ($basedn == "")
183 $basedn = $this->basedn;
184 else
185 $basedn= $this->convert($basedn);
186 $this->sr = @ldap_list($this->cid, $this->fix($basedn), $filter,$attrs);
187 $this->error = @ldap_error($this->cid);
188 $this->resetResult();
189 $this->hasres=true;
190 return($this->sr);
191 }else{
192 $this->error = "Could not connect to LDAP server";
193 return("");
194 }
195 }
197 function cat($dn)
198 {
199 if($this->hascon){
200 if ($this->reconnect) $this->connect();
201 $this->clearResult();
202 $filter = "(objectclass=*)";
203 $this->sr = @ldap_read($this->cid, $this->fix($dn), $filter);
204 $this->error = @ldap_error($this->cid);
205 $this->resetResult();
206 $this->hasres=true;
207 return($this->sr);
208 }else{
209 $this->error = "Could not connect to LDAP server";
210 return("");
211 }
212 }
214 function set_size_limit($size)
215 {
216 /* Ignore zero settings */
217 if ($size == 0){
218 @ldap_set_option($this->cid, LDAP_OPT_SIZELIMIT, 10000000);
219 }
220 if($this->hascon){
221 @ldap_set_option($this->cid, LDAP_OPT_SIZELIMIT, $size);
222 } else {
223 $this->error = "Could not connect to LDAP server";
224 }
225 }
227 function fetch()
228 {
229 if($this->hascon){
230 if($this->hasres){
231 if ($this->start == 0)
232 {
233 $this->start = 1;
234 $this->re= @ldap_first_entry($this->cid, $this->sr);
235 } else {
236 $this->re= @ldap_next_entry($this->cid, $this->re);
237 }
238 if ($this->re)
239 {
240 $att= @ldap_get_attributes($this->cid, $this->re);
241 $att['dn']= $this->convert(@ldap_get_dn($this->cid, $this->re));
242 }
243 $this->error = @ldap_error($this->cid);
244 if (!isset($att)){
245 $att= array();
246 }
247 return($att);
248 }else{
249 $this->error = "Perform a Fetch with no Search";
250 return("");
251 }
252 }else{
253 $this->error = "Could not connect to LDAP server";
254 return("");
255 }
256 }
258 function resetResult()
259 {
260 $this->start = 0;
261 }
263 function clearResult()
264 {
265 if($this->hasres){
266 $this->hasres = false;
267 @ldap_free_result($this->sr);
268 }
269 }
271 function getDN()
272 {
273 if($this->hascon){
274 if($this->hasres){
276 if(!$this->re)
277 {
278 $this->error = "Perform a Fetch with no valid Result";
279 }
280 else
281 {
282 $rv = @ldap_get_dn($this->cid, $this->re);
284 $this->error = @ldap_error($this->cid);
285 return($this->convert($rv));
286 }
287 }else{
288 $this->error = "Perform a Fetch with no Search";
289 return("");
290 }
291 }else{
292 $this->error = "Could not connect to LDAP server";
293 return("");
294 }
295 }
297 function count()
298 {
299 if($this->hascon){
300 if($this->hasres){
301 $rv = @ldap_count_entries($this->cid, $this->sr);
302 $this->error = @ldap_error($this->cid);
303 return($rv);
304 }else{
305 $this->error = "Perform a Fetch with no Search";
306 return("");
307 }
308 }else{
309 $this->error = "Could not connect to LDAP server";
310 return("");
311 }
312 }
314 function rm($attrs = "", $dn = "")
315 {
316 if($this->hascon){
317 if ($this->reconnect) $this->connect();
318 if ($dn == "")
319 $dn = $this->basedn;
321 $r = @ldap_mod_del($this->cid, $this->fix($dn), $attrs);
322 $this->error = @ldap_error($this->cid);
323 return($r);
324 }else{
325 $this->error = "Could not connect to LDAP server";
326 return("");
327 }
328 }
330 function rename($attrs, $dn = "")
331 {
332 if($this->hascon){
333 if ($this->reconnect) $this->connect();
334 if ($dn == "")
335 $dn = $this->basedn;
337 $r = @ldap_mod_replace($this->cid, $this->fix($dn), $attrs);
338 $this->error = @ldap_error($this->cid);
339 return($r);
340 }else{
341 $this->error = "Could not connect to LDAP server";
342 return("");
343 }
344 }
346 function rmdir($deletedn)
347 {
348 if($this->hascon){
349 if ($this->reconnect) $this->connect();
350 $r = @ldap_delete($this->cid, $this->fix($deletedn));
351 $this->error = @ldap_error($this->cid);
352 return($r ? $r : 0);
353 }else{
354 $this->error = "Could not connect to LDAP server";
355 return("");
356 }
357 }
359 /**
360 * Function rmdir_recursive
361 *
362 * Description: Based in recursive_remove, adding two thing: full subtree remove, and delete own node.
363 * Parameters: The dn to delete
364 * GiveBack: True on sucessfull , 0 in error, and "" when we don't get a ldap conection
365 *
366 */
368 function rmdir_recursive($deletedn)
369 {
370 if($this->hascon){
371 if ($this->reconnect) $this->connect();
372 $delarray= array();
374 /* Get sorted list of dn's to delete */
375 $this->ls ("(objectClass=*)",$deletedn);
376 while ($this->fetch()){
377 $deldn= $this->getDN();
378 $delarray[$deldn]= strlen($deldn);
379 }
380 arsort ($delarray);
381 reset ($delarray);
383 /* Really Delete ALL dn's in subtree */
384 foreach ($delarray as $key => $value){
385 $this->rmdir_recursive($key);
386 }
388 /* Finally Delete own Node */
389 $r = @ldap_delete($this->cid, $this->fix($deletedn));
390 $this->error = @ldap_error($this->cid);
391 return($r ? $r : 0);
392 }else{
393 $this->error = "Could not connect to LDAP server";
394 return("");
395 }
396 }
398 /* Copy given attributes and sub-dns with attributes to destination dn
400 */
401 function copy_FAI_resource_recursive($sourcedn,$destinationdn,$type="branch",$is_first = false)
402 {
403 error_reporting(E_ALL);
404 if($this->hascon){
405 if ($this->reconnect) $this->connect();
407 /* Save base dn */
408 $basedn= $this->basedn;
409 $delarray= array();
411 /* Check if destination entry already exists */
412 if($this->count($this->fetch($this->cat($destinationdn)))){
413 return;
414 }else{
416 /* Get source entrie */
417 $this->cd($basedn);
418 $attr = $this->fetch($this->cat($sourcedn));
420 /* check if this is a department */
421 if(in_array("organizationalUnit",$attr['objectClass'])){
422 $attr['dn'] = $this->convert($destinationdn);
423 $this->cd($basedn);
424 $this->create_missing_trees($destinationdn);
425 $this->cd($destinationdn);
427 /* If is first entry, append FAIbranch to department entry */
428 if($is_first){
429 $attr= $this->fetch($this->cat($destinationdn));
431 /* Filter unneeded informations */
432 foreach($attr as $key => $value){
433 if(is_numeric($key)) unset($attr[$key]);
434 if(isset($attr[$key]['count'])){
435 if(($attr[$key]['count']==1)&&($key!="objectClass")){
436 $attr[$key] = $attr[$key][0];
437 }
438 }
440 if(isset($attr[$key]['count'])){
441 if(is_array($attr[$key])){
442 unset($attr[$key]['count']);
443 }
444 }
445 }
446 unset($attr['count']);
447 unset($attr['dn']);
449 /* Add marking attribute */
450 $attr['objectClass'][] = "FAIbranch";
452 /* Add this entry */
453 $this->modify($attr);
454 }
456 }else{
457 /* If this is no department */
458 foreach($attr as $key => $value){
459 if(is_numeric($key)) unset($attr[$key]);
460 if(isset($attr[$key]['count'])){
461 if(($attr[$key]['count']==1)&&($key!="objectClass")){
462 $attr[$key] = $attr[$key][0];
463 }
464 }
465 if(isset($attr[$key]['count'])){
466 if(is_array($attr[$key])){
467 unset($attr[$key]['count']);
468 }
469 }
470 }
471 unset($attr['count']);
472 unset($attr['dn']);
474 if($type=="branch"){
475 $attr['FAIstate'] ="branch";
476 }elseif($type=="freeze"){
477 $attr['FAIstate'] ="freeze";
478 }else{
479 print_red(_("Unknown FAIstate %s"),$type);
480 }
482 /* Add entry */
483 $this->cd($destinationdn);
484 $a = $this->fetch($this->cat($destinationdn));
485 if(!count($a)){
486 $this->add($attr);
487 }
489 if($this->error != "Success"){
490 /* Some error occured */
491 print "---------------------------------------------";
492 print $this->get_error()."<br>";
493 print $sourcedn."<br>";
494 print $destinationdn."<br>";
495 print_a( $attr);
496 exit();
497 }
498 }
499 }
501 $this->ls ("(objectClass=*)",$sourcedn);
502 while ($this->fetch()){
503 $deldn= $this->getDN();
504 $delarray[$deldn]= strlen($deldn);
505 }
506 asort ($delarray);
507 reset ($delarray);
509 foreach($delarray as $dn => $bla){
510 if($dn != $destinationdn){
511 $this->cd($basedn);
512 $item = $this->fetch($this->cat($dn));
513 if(!in_array("FAIbranch",$item['objectClass'])){
514 $this->copy_FAI_resource_recursive($dn,str_replace($sourcedn,$destinationdn,$dn),$type);
515 }
516 }
517 }
518 }
519 }
521 function modify($attrs)
522 {
523 if($this->hascon){
524 if ($this->reconnect) $this->connect();
525 $r = @ldap_modify($this->cid, $this->fix($this->basedn), $attrs);
526 $this->error = @ldap_error($this->cid);
527 return($r ? $r : 0);
528 }else{
529 $this->error = "Could not connect to LDAP server";
530 return("");
531 }
532 }
534 function add($attrs)
535 {
536 if($this->hascon){
537 if ($this->reconnect) $this->connect();
538 $r = @ldap_add($this->cid, $this->fix($this->basedn), $attrs);
539 $this->error = @ldap_error($this->cid);
540 return($r ? $r : 0);
541 }else{
542 $this->error = "Could not connect to LDAP server";
543 return("");
544 }
545 }
547 function create_missing_trees($target)
548 {
549 /* Ignore create_missing trees if the base equals target */
550 if ($target == $this->basedn){
551 return;
552 }
553 $l= array_reverse(explode(",", preg_replace("/,".$this->basedn."/", "", $target)));
554 $cdn= $this->basedn;
555 foreach ($l as $part){
556 $cdn= "$part,$cdn";
558 /* Ignore referrals */
559 $found= false;
560 foreach($this->referrals as $ref){
561 $base= preg_replace('!^[^:]+://[^/]+/([^?]+).*$!', '\\1', $ref['URL']);
562 if ($base == $cdn){
563 $found= true;
564 break;
565 }
566 }
567 if ($found){
568 continue;
569 }
571 $this->cat ($cdn);
572 $attrs= $this->fetch();
574 /* Create missing entry? */
575 if (!count ($attrs)){
576 $type= preg_replace('/^([^=]+)=.*$/', '\\1', $cdn);
577 $param= preg_replace('/^[^=]+=([^,]+),.*$/', '\\1', $cdn);
579 $na= array();
580 switch ($type){
581 case 'ou':
582 $na["objectClass"]= "organizationalUnit";
583 $na["ou"]= $param;
584 break;
585 case 'dc':
586 $na["objectClass"]= array("dcObject", "top", "locality");
587 $na["dc"]= $param;
588 break;
589 default:
590 print_red(sprintf(_("Autocreation of type '%s' is currently not supported. Please report to the GOsa team."), $type));
591 echo $_SESSION['errors'];
592 exit;
593 }
594 $this->cd($cdn);
595 $this->add($na);
596 }
597 }
598 }
600 function recursive_remove()
601 {
602 $delarray= array();
604 /* Get sorted list of dn's to delete */
605 $this->search ("(objectClass=*)");
606 while ($this->fetch()){
607 $deldn= $this->getDN();
608 $delarray[$deldn]= strlen($deldn);
609 }
610 arsort ($delarray);
611 reset ($delarray);
613 /* Delete all dn's in subtree */
614 foreach ($delarray as $key => $value){
615 $this->rmdir($key);
616 }
617 }
619 function get_attribute($dn, $name,$r_array=0)
620 {
621 $data= "";
622 if ($this->reconnect) $this->connect();
623 $sr= @ldap_read($this->cid, $this->fix($dn), "objectClass=*", array("$name"));
625 /* fill data from LDAP */
626 if ($sr) {
627 $ei= @ldap_first_entry($this->cid, $sr);
628 if ($ei) {
629 if ($info= @ldap_get_values_len($this->cid, $ei, "$name")){
630 $data= $info[0];
631 }
633 }
634 }
635 if($r_array==0)
636 return ($data);
637 else
638 return ($info);
641 }
645 function get_additional_error()
646 {
647 $error= "";
648 @ldap_get_option ($this->cid, LDAP_OPT_ERROR_STRING, $error);
649 return ($error);
650 }
652 function get_error()
653 {
654 if ($this->error == 'Success'){
655 return $this->error;
656 } else {
657 $error= $this->error." (".$this->get_additional_error().")";
658 return $error;
659 }
660 }
662 function get_credentials($url, $referrals= NULL)
663 {
664 $ret= array();
665 $url= preg_replace('!\?\?.*$!', '', $url);
666 $server= preg_replace('!^([^:]+://[^/]+)/.*$!', '\\1', $url);
668 if ($referrals == NULL){
669 $referrals= $this->referrals;
670 }
672 if (isset($referrals[$server])){
673 return ($referrals[$server]);
674 } else {
675 $ret['ADMIN']= $this->fix($this->binddn);
676 $ret['PASSWORD']= $this->bindpw;
677 }
679 return ($ret);
680 }
683 function gen_ldif ($dn, $filter= "(objectClass=*)", $attributes= array('*'), $recursive= TRUE)
684 {
685 $display= "";
687 if ($recursive){
688 $this->cd($dn);
689 $this->search("$filter", array('dn'));
690 while ($attrs= $this->fetch()){
691 $display.= $this->gen_one_entry($attrs['dn'], $filter, $attributes);
692 $display.= "\n";
693 }
694 } else {
695 $display.= $this->gen_one_entry($dn);
696 }
698 return ($display);
699 }
701 function gen_xls ($dn, $filter= "(objectClass=*)", $attributes= array('*'), $recursive= TRUE,$r_array=0)
702 {
703 $display= "";
705 $this->cd($dn);
706 $this->search("$filter");
708 $i=0;
709 while ($attrs= $this->fetch()){
710 $j=0;
712 foreach ($attributes as $at){
713 $display[$i][$j]= $this->get_attribute($attrs['dn'], $at,$r_array);
714 $j++;
715 }
717 $i++;
718 }
720 return ($display);
721 }
724 function gen_one_entry($dn, $filter= "(objectClass=*)" , $name= array("*"))
725 {
726 $ret = "";
727 $data = "";
728 if($this->reconnect){
729 $this->connect();
730 }
732 /* Searching Ldap Tree */
733 $sr= @ldap_read($this->cid, $this->fix($dn), $filter, $name);
735 /* Get the first entry */
736 $entry= @ldap_first_entry($this->cid, $sr);
738 /* Get all attributes related to that Objekt */
739 $atts = array();
741 /* Assemble dn */
742 $atts[0]['name'] = "dn";
743 $atts[0]['value'] = array('count' => 1, 0 => $dn);
745 /* Reset index */
746 $i = 1 ;
747 $identifier = array();
748 $attribute= @ldap_first_attribute($this->cid,$entry,$identifier);
749 while ($attribute) {
750 $i++;
751 $atts[$i]['name'] = $attribute;
752 $atts[$i]['value'] = @ldap_get_values_len($this->cid, $entry, "$attribute");
754 /* Next one */
755 $attribute= @ldap_next_attribute($this->cid,$entry,$identifier);
756 }
758 foreach($atts as $at)
759 {
760 for ($i= 0; $i<$at['value']['count']; $i++){
762 /* Check if we must encode the data */
763 if(!preg_match('/^[a-z0-9+@#.=, \/ -]+$/i', $at['value'][$i])) {
764 $ret .= $at['name'].":: ".base64_encode($at['value'][$i])."\n";
765 } else {
766 $ret .= $at['name'].": ".$at['value'][$i]."\n";
767 }
768 }
769 }
771 return($ret);
772 }
775 function dn_exists($dn)
776 {
777 return @ldap_list($this->cid, $this->fix($dn), "(objectClass=*)", array("objectClass"));
778 }
782 function import_complete_ldif($str_attr,&$error,$overwrite,$cleanup)
783 {
784 if($this->reconnect) $this->connect();
786 /* First we have to splitt the string ito detect empty lines
787 An empty line indicates an new Entry */
788 $entries = split("\n",$str_attr);
790 $data = "";
791 $cnt = 0;
792 $current_line = 0;
794 /* Every single line ... */
795 foreach($entries as $entry) {
796 $current_line ++;
798 /* Removing Spaces to ..
799 .. test if a new entry begins */
800 $tmp = str_replace(" ","",$data );
802 /* .. prevent empty lines in an entry */
803 $tmp2 = str_replace(" ","",$entry);
805 /* If the Block ends (Empty Line) */
806 if((empty($entry))&&(!empty($tmp))) {
807 /* Add collected lines as a complete block */
808 $all[$cnt] = $data;
809 $cnt ++;
810 $data ="";
811 } else {
813 /* Append lines ... */
814 if(!empty($tmp2)) {
815 /* check if we need base64_decode for this line */
816 if(ereg("::",$tmp2))
817 {
818 $encoded = split("::",$entry);
819 $attr = $encoded[0];
820 $value = base64_decode($encoded[1]);
821 /* Add linenumber */
822 $data .= $current_line."#".$attr.":".$value."\n";
823 }
824 else
825 {
826 /* Add Linenumber */
827 $data .= $current_line."#".$entry."\n";
828 }
829 }
830 }
831 }
833 /* The Data we collected is not in the array all[];
834 For example the Data is stored like this..
836 all[0] = "1#dn : .... \n
837 2#ObjectType: person \n ...."
839 Now we check every insertblock and try to insert */
840 foreach ( $all as $single) {
841 $lineone = split("\n",$single);
842 $ndn = split("#", $lineone[0]);
843 $line = $ndn[1];
845 $dnn = split (":",$line);
846 $current_line = $ndn[0];
847 $dn = $dnn[0];
848 $value = $dnn[1];
850 /* Every block must begin with a dn */
851 if($dn != "dn") {
852 $error= sprintf(_("This is not a valid DN: '%s'. A block for import should begin with 'dn: ...' in line %s"), $line, $current_line);
853 return -2;
854 }
856 /* Should we use Modify instead of Add */
857 $usemodify= false;
859 /* Delete before insert */
860 $usermdir= false;
862 /* The dn address already exists! */
863 if (($this->dn_exists($value))&&((!$overwrite)&&(!$cleanup))) {
865 $error= sprintf(_("The dn: '%s' (from line %s) already exists in the LDAP database."), $line, $current_line);
866 return ALREADY_EXISTING_ENTRY;
868 } elseif(($this->dn_exists($value))&&($cleanup)){
870 /* Delete first, then add */
871 $usermdir = true;
873 } elseif(($this->dn_exists($value))&&($overwrite)) {
875 /* Modify instead of Add */
876 $usemodify = true;
877 }
879 /* If we can't Import, return with a file error */
880 if(!$this->import_single_entry($single,$usemodify,$usermdir) ) {
881 $error= sprintf(_("Error while importing dn: '%s', please check your LDIF from line %s on!"), $line,
882 $current_line);
883 return UNKNOWN_TOKEN_IN_LDIF_FILE; }
884 }
886 return (INSERT_OK);
887 }
890 /* Imports a single entry */
891 function import_single_entry($str_attr,$modify,$delete)
892 {
893 if($this->reconnect) $this->connect();
895 $ret = false;
896 $rows= split("\n",$str_attr);
897 $data= false;
899 foreach($rows as $row) {
901 /* Check if we use Linenumbers (when import_complete_ldif is called we use
902 Linenumbers) Linenumbers are use like this 123#attribute : value */
903 if(!empty($row)) {
904 if((strpos($row,"#")!=FALSE)&&(strpos($row,"#")<strpos($row,":"))) {
906 /* We are using line numbers
907 Because there is a # before a : */
908 $tmp1= split("#",$row);
909 $current_line= $tmp1[0];
910 $row= $tmp1[1];
911 }
913 /* Split the line into attribute and value */
914 $attr = split(":", $row);
915 $attr[0]= trim($attr[0]); /* attribute */
916 $attr[1]= trim($attr[1]); /* value */
918 /* Check for attributes that are used more than once */
919 if(!isset($data[$attr[0]])) {
920 $data[$attr[0]]=$attr[1];
921 } else {
922 $tmp = $data[$attr[0]];
924 if(!is_array($tmp)) {
925 $new[0]=$tmp;
926 $new[1]=$attr[1];
927 $datas[$attr[0]]['count']=1;
928 $data[$attr[0]]=$new;
929 } else {
930 $cnt = $datas[$attr[0]]['count'];
931 $cnt ++;
932 $data[$attr[0]][$cnt]=$attr[1];
933 $datas[$attr[0]]['count'] = $cnt;
934 }
935 }
936 }
937 }
939 /* If dn is an index of data, we should try to insert the data */
940 if(isset($data['dn'])) {
941 /* Creating Entry */
942 $this->cd($data['dn']);
944 /* Delete existing entry */
945 if($delete){
946 $this->rmdir($data['dn']);
947 }
949 /* Create missing trees */
950 $this->create_missing_trees($data['dn']);
951 unset($data['dn']);
953 /* If entry exists use modify */
954 if(!$modify){
955 $ret = $this->add($data);
956 } else {
957 $ret = $this->modify($data);
958 }
959 }
961 return($ret);
962 }
965 function importcsv($str)
966 {
967 $lines = split("\n",$str);
968 foreach($lines as $line)
969 {
970 /* continue if theres a comment */
971 if(substr(trim($line),0,1)=="#"){
972 continue;
973 }
975 $line= str_replace ("\t\t","\t",$line);
976 $line= str_replace ("\t" ,"," ,$line);
977 echo $line;
979 $cells = split(",",$line ) ;
980 $linet= str_replace ("\t\t",",",$line);
981 $cells = split("\t",$line);
982 $count = count($cells);
983 }
985 }
987 }
989 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
990 ?>