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 $start = microtime();
164 if($this->hascon){
165 if ($this->reconnect) $this->connect();
166 $this->clearResult();
167 $this->sr = @ldap_search($this->cid, $this->fix($this->basedn), $filter, $attrs);
168 $this->error = @ldap_error($this->cid);
169 $this->resetResult();
170 $this->hasres=true;
172 /* Time management */
173 $r = split(" ",$start);
174 $ms = $r[0];
175 $s= $r[1];
177 $re = split(" ",microtime());
178 $mse = $re[0];
179 $se= $re[1];
181 $add = 0;
182 if(($mse -$ms)<0){
183 $se --;
184 $add = 1;
185 }
186 $secs = ($se -$s);
187 $msecs = (int)(($add+($mse -$ms))*1000);
188 $time = $secs +($msecs/1000);
189 if($time > .2){
190 $Sattrs = "";
191 foreach($attrs as $att){
192 $Sattrs .= " ".$att;
193 }
194 print_red(sprintf(_("Ldap search took about %s seconds, used filter '%s' with following attributes '%s '. Please check for performance improvements."),$time,htmlentities($filter),$Sattrs));
195 }
196 return($this->sr);
197 }else{
198 $this->error = "Could not connect to LDAP server";
199 return("");
200 }
201 }
203 function ls($filter = "(objectclass=*)", $basedn = "",$attrs = array("*"))
204 {
205 if($this->hascon){
206 if ($this->reconnect) $this->connect();
207 $this->clearResult();
208 if ($basedn == "")
209 $basedn = $this->basedn;
210 else
211 $basedn= $this->convert($basedn);
212 $this->sr = @ldap_list($this->cid, $this->fix($basedn), $filter,$attrs);
213 $this->error = @ldap_error($this->cid);
214 $this->resetResult();
215 $this->hasres=true;
216 return($this->sr);
217 }else{
218 $this->error = "Could not connect to LDAP server";
219 return("");
220 }
221 }
223 function cat($dn)
224 {
225 if($this->hascon){
226 if ($this->reconnect) $this->connect();
227 $this->clearResult();
228 $filter = "(objectclass=*)";
229 $this->sr = @ldap_read($this->cid, $this->fix($dn), $filter);
230 $this->error = @ldap_error($this->cid);
231 $this->resetResult();
232 $this->hasres=true;
233 return($this->sr);
234 }else{
235 $this->error = "Could not connect to LDAP server";
236 return("");
237 }
238 }
240 function set_size_limit($size)
241 {
242 /* Ignore zero settings */
243 if ($size == 0){
244 @ldap_set_option($this->cid, LDAP_OPT_SIZELIMIT, 10000000);
245 }
246 if($this->hascon){
247 @ldap_set_option($this->cid, LDAP_OPT_SIZELIMIT, $size);
248 } else {
249 $this->error = "Could not connect to LDAP server";
250 }
251 }
253 function fetch()
254 {
255 if($this->hascon){
256 if($this->hasres){
257 if ($this->start == 0)
258 {
259 $this->start = 1;
260 $this->re= @ldap_first_entry($this->cid, $this->sr);
261 } else {
262 $this->re= @ldap_next_entry($this->cid, $this->re);
263 }
264 if ($this->re)
265 {
266 $att= @ldap_get_attributes($this->cid, $this->re);
267 $att['dn']= $this->convert(@ldap_get_dn($this->cid, $this->re));
268 }
269 $this->error = @ldap_error($this->cid);
270 if (!isset($att)){
271 $att= array();
272 }
273 return($att);
274 }else{
275 $this->error = "Perform a Fetch with no Search";
276 return("");
277 }
278 }else{
279 $this->error = "Could not connect to LDAP server";
280 return("");
281 }
282 }
284 function resetResult()
285 {
286 $this->start = 0;
287 }
289 function clearResult()
290 {
291 if($this->hasres){
292 $this->hasres = false;
293 @ldap_free_result($this->sr);
294 }
295 }
297 function getDN()
298 {
299 if($this->hascon){
300 if($this->hasres){
302 if(!$this->re)
303 {
304 $this->error = "Perform a Fetch with no valid Result";
305 }
306 else
307 {
308 $rv = @ldap_get_dn($this->cid, $this->re);
310 $this->error = @ldap_error($this->cid);
311 return($this->convert($rv));
312 }
313 }else{
314 $this->error = "Perform a Fetch with no Search";
315 return("");
316 }
317 }else{
318 $this->error = "Could not connect to LDAP server";
319 return("");
320 }
321 }
323 function count()
324 {
325 if($this->hascon){
326 if($this->hasres){
327 $rv = @ldap_count_entries($this->cid, $this->sr);
328 $this->error = @ldap_error($this->cid);
329 return($rv);
330 }else{
331 $this->error = "Perform a Fetch with no Search";
332 return("");
333 }
334 }else{
335 $this->error = "Could not connect to LDAP server";
336 return("");
337 }
338 }
340 function rm($attrs = "", $dn = "")
341 {
342 if($this->hascon){
343 if ($this->reconnect) $this->connect();
344 if ($dn == "")
345 $dn = $this->basedn;
347 $r = @ldap_mod_del($this->cid, $this->fix($dn), $attrs);
348 $this->error = @ldap_error($this->cid);
349 return($r);
350 }else{
351 $this->error = "Could not connect to LDAP server";
352 return("");
353 }
354 }
356 function rename($attrs, $dn = "")
357 {
358 if($this->hascon){
359 if ($this->reconnect) $this->connect();
360 if ($dn == "")
361 $dn = $this->basedn;
363 $r = @ldap_mod_replace($this->cid, $this->fix($dn), $attrs);
364 $this->error = @ldap_error($this->cid);
365 return($r);
366 }else{
367 $this->error = "Could not connect to LDAP server";
368 return("");
369 }
370 }
372 function rmdir($deletedn)
373 {
374 if($this->hascon){
375 if ($this->reconnect) $this->connect();
376 $r = @ldap_delete($this->cid, $this->fix($deletedn));
377 $this->error = @ldap_error($this->cid);
378 return($r ? $r : 0);
379 }else{
380 $this->error = "Could not connect to LDAP server";
381 return("");
382 }
383 }
385 /**
386 * Function rmdir_recursive
387 *
388 * Description: Based in recursive_remove, adding two thing: full subtree remove, and delete own node.
389 * Parameters: The dn to delete
390 * GiveBack: True on sucessfull , 0 in error, and "" when we don't get a ldap conection
391 *
392 */
394 function rmdir_recursive($deletedn)
395 {
396 if($this->hascon){
397 if ($this->reconnect) $this->connect();
398 $delarray= array();
400 /* Get sorted list of dn's to delete */
401 $this->ls ("(objectClass=*)",$deletedn);
402 while ($this->fetch()){
403 $deldn= $this->getDN();
404 $delarray[$deldn]= strlen($deldn);
405 }
406 arsort ($delarray);
407 reset ($delarray);
409 /* Really Delete ALL dn's in subtree */
410 foreach ($delarray as $key => $value){
411 $this->rmdir_recursive($key);
412 }
414 /* Finally Delete own Node */
415 $r = @ldap_delete($this->cid, $this->fix($deletedn));
416 $this->error = @ldap_error($this->cid);
417 return($r ? $r : 0);
418 }else{
419 $this->error = "Could not connect to LDAP server";
420 return("");
421 }
422 }
424 /* Copy given attributes and sub-dns with attributes to destination dn
426 */
427 function copy_FAI_resource_recursive($sourcedn,$destinationdn,$type="branch",$is_first = false)
428 {
429 error_reporting(E_ALL);
430 if($this->hascon){
431 if ($this->reconnect) $this->connect();
433 /* Save base dn */
434 $basedn= $this->basedn;
435 $delarray= array();
437 /* Check if destination entry already exists */
438 if($this->count($this->fetch($this->cat($destinationdn)))){
439 return;
440 }else{
442 /* Get source entrie */
443 $this->cd($basedn);
444 $attr = $this->fetch($this->cat($sourcedn));
446 /* check if this is a department */
447 if(in_array("organizationalUnit",$attr['objectClass'])){
448 $attr['dn'] = $this->convert($destinationdn);
449 $this->cd($basedn);
450 $this->create_missing_trees($destinationdn);
451 $this->cd($destinationdn);
453 /* If is first entry, append FAIbranch to department entry */
454 if($is_first){
455 $attr= $this->fetch($this->cat($destinationdn));
457 /* Filter unneeded informations */
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 }
466 if(isset($attr[$key]['count'])){
467 if(is_array($attr[$key])){
468 unset($attr[$key]['count']);
469 }
470 }
471 }
472 unset($attr['count']);
473 unset($attr['dn']);
475 /* Add marking attribute */
476 $attr['objectClass'][] = "FAIbranch";
478 /* Add this entry */
479 $this->modify($attr);
480 }
482 }else{
483 /* If this is no department */
484 foreach($attr as $key => $value){
485 if(is_numeric($key)) unset($attr[$key]);
486 if(isset($attr[$key]['count'])){
487 if(($attr[$key]['count']==1)&&($key!="objectClass")){
488 $attr[$key] = $attr[$key][0];
489 }
490 }
491 if(isset($attr[$key]['count'])){
492 if(is_array($attr[$key])){
493 unset($attr[$key]['count']);
494 }
495 }
496 }
497 unset($attr['count']);
498 unset($attr['dn']);
500 if($type=="branch"){
501 $attr['FAIstate'] ="branch";
502 }elseif($type=="freeze"){
503 $attr['FAIstate'] ="freeze";
504 }else{
505 print_red(_("Unknown FAIstate %s"),$type);
506 }
508 /* Add entry */
509 $this->cd($destinationdn);
510 $a = $this->fetch($this->cat($destinationdn));
511 if(!count($a)){
512 $this->add($attr);
513 }
515 if($this->error != "Success"){
516 /* Some error occured */
517 print "---------------------------------------------";
518 print $this->get_error()."<br>";
519 print $sourcedn."<br>";
520 print $destinationdn."<br>";
521 print_a( $attr);
522 exit();
523 }
524 }
525 }
527 $this->ls ("(objectClass=*)",$sourcedn);
528 while ($this->fetch()){
529 $deldn= $this->getDN();
530 $delarray[$deldn]= strlen($deldn);
531 }
532 asort ($delarray);
533 reset ($delarray);
535 foreach($delarray as $dn => $bla){
536 if($dn != $destinationdn){
537 $this->cd($basedn);
538 $item = $this->fetch($this->cat($dn));
539 if(!in_array("FAIbranch",$item['objectClass'])){
540 $this->copy_FAI_resource_recursive($dn,str_replace($sourcedn,$destinationdn,$dn),$type);
541 }
542 }
543 }
544 }
545 }
547 function modify($attrs)
548 {
549 if($this->hascon){
550 if ($this->reconnect) $this->connect();
551 $r = @ldap_modify($this->cid, $this->fix($this->basedn), $attrs);
552 $this->error = @ldap_error($this->cid);
553 return($r ? $r : 0);
554 }else{
555 $this->error = "Could not connect to LDAP server";
556 return("");
557 }
558 }
560 function add($attrs)
561 {
562 if($this->hascon){
563 if ($this->reconnect) $this->connect();
564 $r = @ldap_add($this->cid, $this->fix($this->basedn), $attrs);
565 $this->error = @ldap_error($this->cid);
566 return($r ? $r : 0);
567 }else{
568 $this->error = "Could not connect to LDAP server";
569 return("");
570 }
571 }
573 function create_missing_trees($target)
574 {
575 /* Ignore create_missing trees if the base equals target */
576 if ($target == $this->basedn){
577 return;
578 }
579 $l= array_reverse(explode(",", preg_replace("/,".$this->basedn."/", "", $target)));
580 $cdn= $this->basedn;
581 foreach ($l as $part){
582 $cdn= "$part,$cdn";
584 /* Ignore referrals */
585 $found= false;
586 foreach($this->referrals as $ref){
587 $base= preg_replace('!^[^:]+://[^/]+/([^?]+).*$!', '\\1', $ref['URL']);
588 if ($base == $cdn){
589 $found= true;
590 break;
591 }
592 }
593 if ($found){
594 continue;
595 }
597 $this->cat ($cdn);
598 $attrs= $this->fetch();
600 /* Create missing entry? */
601 if (!count ($attrs)){
602 $type= preg_replace('/^([^=]+)=.*$/', '\\1', $cdn);
603 $param= preg_replace('/^[^=]+=([^,]+),.*$/', '\\1', $cdn);
605 $na= array();
606 switch ($type){
607 case 'ou':
608 $na["objectClass"]= "organizationalUnit";
609 $na["ou"]= $param;
610 break;
611 case 'dc':
612 $na["objectClass"]= array("dcObject", "top", "locality");
613 $na["dc"]= $param;
614 break;
615 default:
616 print_red(sprintf(_("Autocreation of type '%s' is currently not supported. Please report to the GOsa team."), $type));
617 echo $_SESSION['errors'];
618 exit;
619 }
620 $this->cd($cdn);
621 $this->add($na);
622 }
623 }
624 }
626 function recursive_remove()
627 {
628 $delarray= array();
630 /* Get sorted list of dn's to delete */
631 $this->search ("(objectClass=*)");
632 while ($this->fetch()){
633 $deldn= $this->getDN();
634 $delarray[$deldn]= strlen($deldn);
635 }
636 arsort ($delarray);
637 reset ($delarray);
639 /* Delete all dn's in subtree */
640 foreach ($delarray as $key => $value){
641 $this->rmdir($key);
642 }
643 }
645 function get_attribute($dn, $name,$r_array=0)
646 {
647 $data= "";
648 if ($this->reconnect) $this->connect();
649 $sr= @ldap_read($this->cid, $this->fix($dn), "objectClass=*", array("$name"));
651 /* fill data from LDAP */
652 if ($sr) {
653 $ei= @ldap_first_entry($this->cid, $sr);
654 if ($ei) {
655 if ($info= @ldap_get_values_len($this->cid, $ei, "$name")){
656 $data= $info[0];
657 }
659 }
660 }
661 if($r_array==0)
662 return ($data);
663 else
664 return ($info);
667 }
671 function get_additional_error()
672 {
673 $error= "";
674 @ldap_get_option ($this->cid, LDAP_OPT_ERROR_STRING, $error);
675 return ($error);
676 }
678 function get_error()
679 {
680 if ($this->error == 'Success'){
681 return $this->error;
682 } else {
683 $error= $this->error." (".$this->get_additional_error().")";
684 return $error;
685 }
686 }
688 function get_credentials($url, $referrals= NULL)
689 {
690 $ret= array();
691 $url= preg_replace('!\?\?.*$!', '', $url);
692 $server= preg_replace('!^([^:]+://[^/]+)/.*$!', '\\1', $url);
694 if ($referrals == NULL){
695 $referrals= $this->referrals;
696 }
698 if (isset($referrals[$server])){
699 return ($referrals[$server]);
700 } else {
701 $ret['ADMIN']= $this->fix($this->binddn);
702 $ret['PASSWORD']= $this->bindpw;
703 }
705 return ($ret);
706 }
709 function gen_ldif ($dn, $filter= "(objectClass=*)", $attributes= array('*'), $recursive= TRUE)
710 {
711 $display= "";
713 if ($recursive){
714 $this->cd($dn);
715 $this->search("$filter", array('dn'));
716 while ($attrs= $this->fetch()){
717 $display.= $this->gen_one_entry($attrs['dn'], $filter, $attributes);
718 $display.= "\n";
719 }
720 } else {
721 $display.= $this->gen_one_entry($dn);
722 }
724 return ($display);
725 }
727 function gen_xls ($dn, $filter= "(objectClass=*)", $attributes= array('*'), $recursive= TRUE,$r_array=0)
728 {
729 $display= "";
731 $this->cd($dn);
732 $this->search("$filter");
734 $i=0;
735 while ($attrs= $this->fetch()){
736 $j=0;
738 foreach ($attributes as $at){
739 $display[$i][$j]= $this->get_attribute($attrs['dn'], $at,$r_array);
740 $j++;
741 }
743 $i++;
744 }
746 return ($display);
747 }
750 function gen_one_entry($dn, $filter= "(objectClass=*)" , $name= array("*"))
751 {
752 $ret = "";
753 $data = "";
754 if($this->reconnect){
755 $this->connect();
756 }
758 /* Searching Ldap Tree */
759 $sr= @ldap_read($this->cid, $this->fix($dn), $filter, $name);
761 /* Get the first entry */
762 $entry= @ldap_first_entry($this->cid, $sr);
764 /* Get all attributes related to that Objekt */
765 $atts = array();
767 /* Assemble dn */
768 $atts[0]['name'] = "dn";
769 $atts[0]['value'] = array('count' => 1, 0 => $dn);
771 /* Reset index */
772 $i = 1 ;
773 $identifier = array();
774 $attribute= @ldap_first_attribute($this->cid,$entry,$identifier);
775 while ($attribute) {
776 $i++;
777 $atts[$i]['name'] = $attribute;
778 $atts[$i]['value'] = @ldap_get_values_len($this->cid, $entry, "$attribute");
780 /* Next one */
781 $attribute= @ldap_next_attribute($this->cid,$entry,$identifier);
782 }
784 foreach($atts as $at)
785 {
786 for ($i= 0; $i<$at['value']['count']; $i++){
788 /* Check if we must encode the data */
789 if(!preg_match('/^[a-z0-9+@#.=, \/ -]+$/i', $at['value'][$i])) {
790 $ret .= $at['name'].":: ".base64_encode($at['value'][$i])."\n";
791 } else {
792 $ret .= $at['name'].": ".$at['value'][$i]."\n";
793 }
794 }
795 }
797 return($ret);
798 }
801 function dn_exists($dn)
802 {
803 return @ldap_list($this->cid, $this->fix($dn), "(objectClass=*)", array("objectClass"));
804 }
808 function import_complete_ldif($str_attr,&$error,$overwrite,$cleanup)
809 {
810 if($this->reconnect) $this->connect();
812 /* First we have to splitt the string ito detect empty lines
813 An empty line indicates an new Entry */
814 $entries = split("\n",$str_attr);
816 $data = "";
817 $cnt = 0;
818 $current_line = 0;
820 /* Every single line ... */
821 foreach($entries as $entry) {
822 $current_line ++;
824 /* Removing Spaces to ..
825 .. test if a new entry begins */
826 $tmp = str_replace(" ","",$data );
828 /* .. prevent empty lines in an entry */
829 $tmp2 = str_replace(" ","",$entry);
831 /* If the Block ends (Empty Line) */
832 if((empty($entry))&&(!empty($tmp))) {
833 /* Add collected lines as a complete block */
834 $all[$cnt] = $data;
835 $cnt ++;
836 $data ="";
837 } else {
839 /* Append lines ... */
840 if(!empty($tmp2)) {
841 /* check if we need base64_decode for this line */
842 if(ereg("::",$tmp2))
843 {
844 $encoded = split("::",$entry);
845 $attr = $encoded[0];
846 $value = base64_decode($encoded[1]);
847 /* Add linenumber */
848 $data .= $current_line."#".$attr.":".$value."\n";
849 }
850 else
851 {
852 /* Add Linenumber */
853 $data .= $current_line."#".$entry."\n";
854 }
855 }
856 }
857 }
859 /* The Data we collected is not in the array all[];
860 For example the Data is stored like this..
862 all[0] = "1#dn : .... \n
863 2#ObjectType: person \n ...."
865 Now we check every insertblock and try to insert */
866 foreach ( $all as $single) {
867 $lineone = split("\n",$single);
868 $ndn = split("#", $lineone[0]);
869 $line = $ndn[1];
871 $dnn = split (":",$line);
872 $current_line = $ndn[0];
873 $dn = $dnn[0];
874 $value = $dnn[1];
876 /* Every block must begin with a dn */
877 if($dn != "dn") {
878 $error= sprintf(_("This is not a valid DN: '%s'. A block for import should begin with 'dn: ...' in line %s"), $line, $current_line);
879 return -2;
880 }
882 /* Should we use Modify instead of Add */
883 $usemodify= false;
885 /* Delete before insert */
886 $usermdir= false;
888 /* The dn address already exists! */
889 if (($this->dn_exists($value))&&((!$overwrite)&&(!$cleanup))) {
891 $error= sprintf(_("The dn: '%s' (from line %s) already exists in the LDAP database."), $line, $current_line);
892 return ALREADY_EXISTING_ENTRY;
894 } elseif(($this->dn_exists($value))&&($cleanup)){
896 /* Delete first, then add */
897 $usermdir = true;
899 } elseif(($this->dn_exists($value))&&($overwrite)) {
901 /* Modify instead of Add */
902 $usemodify = true;
903 }
905 /* If we can't Import, return with a file error */
906 if(!$this->import_single_entry($single,$usemodify,$usermdir) ) {
907 $error= sprintf(_("Error while importing dn: '%s', please check your LDIF from line %s on!"), $line,
908 $current_line);
909 return UNKNOWN_TOKEN_IN_LDIF_FILE; }
910 }
912 return (INSERT_OK);
913 }
916 /* Imports a single entry */
917 function import_single_entry($str_attr,$modify,$delete)
918 {
919 if($this->reconnect) $this->connect();
921 $ret = false;
922 $rows= split("\n",$str_attr);
923 $data= false;
925 foreach($rows as $row) {
927 /* Check if we use Linenumbers (when import_complete_ldif is called we use
928 Linenumbers) Linenumbers are use like this 123#attribute : value */
929 if(!empty($row)) {
930 if((strpos($row,"#")!=FALSE)&&(strpos($row,"#")<strpos($row,":"))) {
932 /* We are using line numbers
933 Because there is a # before a : */
934 $tmp1= split("#",$row);
935 $current_line= $tmp1[0];
936 $row= $tmp1[1];
937 }
939 /* Split the line into attribute and value */
940 $attr = split(":", $row);
941 $attr[0]= trim($attr[0]); /* attribute */
942 $attr[1]= trim($attr[1]); /* value */
944 /* Check for attributes that are used more than once */
945 if(!isset($data[$attr[0]])) {
946 $data[$attr[0]]=$attr[1];
947 } else {
948 $tmp = $data[$attr[0]];
950 if(!is_array($tmp)) {
951 $new[0]=$tmp;
952 $new[1]=$attr[1];
953 $datas[$attr[0]]['count']=1;
954 $data[$attr[0]]=$new;
955 } else {
956 $cnt = $datas[$attr[0]]['count'];
957 $cnt ++;
958 $data[$attr[0]][$cnt]=$attr[1];
959 $datas[$attr[0]]['count'] = $cnt;
960 }
961 }
962 }
963 }
965 /* If dn is an index of data, we should try to insert the data */
966 if(isset($data['dn'])) {
967 /* Creating Entry */
968 $this->cd($data['dn']);
970 /* Delete existing entry */
971 if($delete){
972 $this->rmdir($data['dn']);
973 }
975 /* Create missing trees */
976 $this->create_missing_trees($data['dn']);
977 unset($data['dn']);
979 /* If entry exists use modify */
980 if(!$modify){
981 $ret = $this->add($data);
982 } else {
983 $ret = $this->modify($data);
984 }
985 }
987 return($ret);
988 }
991 function importcsv($str)
992 {
993 $lines = split("\n",$str);
994 foreach($lines as $line)
995 {
996 /* continue if theres a comment */
997 if(substr(trim($line),0,1)=="#"){
998 continue;
999 }
1001 $line= str_replace ("\t\t","\t",$line);
1002 $line= str_replace ("\t" ,"," ,$line);
1003 echo $line;
1005 $cells = split(",",$line ) ;
1006 $linet= str_replace ("\t\t",",",$line);
1007 $cells = split("\t",$line);
1008 $count = count($cells);
1009 }
1011 }
1013 }
1015 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1016 ?>