Code

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