07797798f588a0cffa6a2b512dab071e65a34686
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,$attrs= array("*"))
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,$attrs);
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
399 */
400 function copy_FAI_resource_recursive($sourcedn,$destinationdn,$type="branch",$is_first = true,$depth=0)
401 {
402 error_reporting(E_ALL);
404 if($is_first){
405 echo "<font style='font-size:11px;'><h4>".
406 sprintf(_("Creating duplicate of %s in %s"),"<br><i>".$sourcedn."</i><br>","<br><i>".$destinationdn."</i><br>")."</h4>";
407 }else{
408 if(preg_match("/^ou=/",$sourcedn)){
409 echo "<br><h3>"._("Processing")." : ".preg_replace("/,.*$/","",$destinationdn)."</h3>";
410 }else{
411 $tmp = split(",",$sourcedn);
413 echo "<b>Current object : ";
414 for($i = 0; $i < ($depth -1 ) ; $i ++) {
415 echo preg_replace("/^.*=/","",$tmp[$i])." ";
416 }
417 echo "</b>";
418 echo ": <br>";
420 $deststr = $destinationdn;
421 if(strlen($deststr) > 96){
422 $deststr = substr($deststr,0,96)."...";
423 }
425 echo "".$deststr."</br>";
426 }
427 }
429 if($this->hascon){
430 if ($this->reconnect) $this->connect();
432 /* Save base dn */
433 $basedn= $this->basedn;
434 $delarray= array();
436 /* Check if destination entry already exists */
437 $this->cat($destinationdn);
439 if($this->count()){
440 return;
441 }else{
443 $this->clearResult();
445 /* Get source entry */
446 $this->cd($basedn);
447 $this->cat($sourcedn);
448 $attr = $this->fetch();
450 /* Error while fetching object / attribute abort*/
451 if((!$attr) || (count($attr)) ==0) {
452 echo "error while fetching source dn ..";
453 return;
454 }
456 /* check if this is a department */
457 if(in_array("organizationalUnit",$attr['objectClass'])){
458 $attr['dn'] = $this->convert($destinationdn);
459 $this->cd($basedn);
460 $this->create_missing_trees($destinationdn);
461 $this->cd($destinationdn);
463 /* If is first entry, append FAIbranch to department entry */
464 if($is_first){
465 $attr= $this->fetch($this->cat($destinationdn));
467 /* Filter unneeded informations */
468 foreach($attr as $key => $value){
469 if(is_numeric($key)) unset($attr[$key]);
470 if(isset($attr[$key]['count'])){
471 if(is_array($attr[$key])){
472 unset($attr[$key]['count']);
473 }
474 }
475 }
477 unset($attr['count']);
478 unset($attr['dn']);
480 /* Add marking attribute */
481 $attr['objectClass'][] = "FAIbranch";
483 /* Add this entry */
484 $this->modify($attr);
485 }
486 }else{
487 /* If this is no department */
488 foreach($attr as $key => $value){
490 if(in_array($key ,array("FAItemplateFile","FAIscript"))){
491 $sr= ldap_read($this->cid, $this->fix($sourcedn), "(objectClass=FAIclass)", array($key));
492 $ei= ldap_first_entry($this->cid, $sr);
493 $tmp = (@ldap_get_values_len($this->cid, $ei,$key));
494 if(is_array($tmp)){
495 $attr[$key] = $tmp;
496 }
497 }
499 if(is_numeric($key)) unset($attr[$key]);
500 if(isset($attr[$key]['count'])){
501 if(is_array($attr[$key])){
502 unset($attr[$key]['count']);
503 }
504 }
505 }
506 unset($attr['count']);
507 unset($attr['dn']);
509 if(!in_array("gosaApplication" , $attr['objectClass'])){
510 if($type=="branch"){
511 $attr['FAIstate'] ="branch";
512 }elseif($type=="freeze"){
513 $attr['FAIstate'] ="freeze";
514 }else{
515 print_red(_("Unknown FAIstate %s"),$type);
516 }
517 }
519 /* Add entry */
520 $this->cd($destinationdn);
521 $a = $this->fetch($this->cat($destinationdn));
522 if(!count($a)){
523 $this->add($attr);
524 }
526 if($this->error != "Success"){
527 /* Some error occured */
528 print "---------------------------------------------";
529 print $this->get_error()."<br>";
530 print $sourcedn."<br>";
531 print $destinationdn."<br>";
532 print_a( $attr);
533 exit();
534 }
535 }
536 }
538 $this->ls ("(objectClass=*)",$sourcedn);
539 while ($this->fetch()){
540 $deldn= $this->getDN();
541 $delarray[$deldn]= strlen($deldn);
542 }
543 asort ($delarray);
544 reset ($delarray);
546 $depth ++;
547 foreach($delarray as $dn => $bla){
548 if($dn != $destinationdn){
549 $this->cd($basedn);
550 $item = $this->fetch($this->cat($dn));
551 if(!in_array("FAIbranch",$item['objectClass'])){
552 $this->copy_FAI_resource_recursive($dn,str_replace($sourcedn,$destinationdn,$dn),$type,false,$depth);
553 }
554 }
555 }
556 }
557 if($is_first){
558 echo "<br><b>"._("Successfully finished")."</b></font><br><br><br>";
559 }
561 }
563 function modify($attrs)
564 {
565 if(count($attrs) == 0){
566 return (0);
567 }
568 if($this->hascon){
569 if ($this->reconnect) $this->connect();
570 $r = @ldap_modify($this->cid, $this->fix($this->basedn), $attrs);
571 $this->error = @ldap_error($this->cid);
572 return($r ? $r : 0);
573 }else{
574 $this->error = "Could not connect to LDAP server";
575 return("");
576 }
577 }
579 function add($attrs)
580 {
581 if($this->hascon){
582 if ($this->reconnect) $this->connect();
583 $r = @ldap_add($this->cid, $this->fix($this->basedn), $attrs);
584 $this->error = @ldap_error($this->cid);
585 return($r ? $r : 0);
586 }else{
587 $this->error = "Could not connect to LDAP server";
588 return("");
589 }
590 }
592 function create_missing_trees($target)
593 {
594 /* Ignore create_missing trees if the base equals target */
595 if ($target == $this->basedn){
596 return;
597 }
598 $l= array_reverse(explode(",", preg_replace("/,".$this->basedn."/", "", $target)));
599 $cdn= $this->basedn;
600 foreach ($l as $part){
601 $cdn= "$part,$cdn";
603 /* Ignore referrals */
604 $found= false;
605 foreach($this->referrals as $ref){
606 $base= preg_replace('!^[^:]+://[^/]+/([^?]+).*$!', '\\1', $ref['URL']);
607 if ($base == $cdn){
608 $found= true;
609 break;
610 }
611 }
612 if ($found){
613 continue;
614 }
616 $this->cat ($cdn);
617 $attrs= $this->fetch();
619 /* Create missing entry? */
620 if (!count ($attrs)){
621 $type= preg_replace('/^([^=]+)=.*$/', '\\1', $cdn);
622 $param= preg_replace('/^[^=]+=([^,]+),.*$/', '\\1', $cdn);
624 $na= array();
625 switch ($type){
626 case 'ou':
627 $na["objectClass"]= "organizationalUnit";
628 $na["ou"]= $param;
629 break;
630 case 'dc':
631 $na["objectClass"]= array("dcObject", "top", "locality");
632 $na["dc"]= $param;
633 break;
634 default:
635 print_red(sprintf(_("Autocreation of type '%s' is currently not supported. Please report to the GOsa team."), $type));
636 echo $_SESSION['errors'];
637 exit;
638 }
639 $this->cd($cdn);
640 $this->add($na);
641 }
642 }
643 }
645 function recursive_remove()
646 {
647 $delarray= array();
649 /* Get sorted list of dn's to delete */
650 $this->search ("(objectClass=*)");
651 while ($this->fetch()){
652 $deldn= $this->getDN();
653 $delarray[$deldn]= strlen($deldn);
654 }
655 arsort ($delarray);
656 reset ($delarray);
658 /* Delete all dn's in subtree */
659 foreach ($delarray as $key => $value){
660 $this->rmdir($key);
661 }
662 }
664 function get_attribute($dn, $name,$r_array=0)
665 {
666 $data= "";
667 if ($this->reconnect) $this->connect();
668 $sr= @ldap_read($this->cid, $this->fix($dn), "objectClass=*", array("$name"));
670 /* fill data from LDAP */
671 if ($sr) {
672 $ei= @ldap_first_entry($this->cid, $sr);
673 if ($ei) {
674 if ($info= @ldap_get_values_len($this->cid, $ei, "$name")){
675 $data= $info[0];
676 }
678 }
679 }
680 if($r_array==0)
681 return ($data);
682 else
683 return ($info);
686 }
690 function get_additional_error()
691 {
692 $error= "";
693 @ldap_get_option ($this->cid, LDAP_OPT_ERROR_STRING, $error);
694 return ($error);
695 }
697 function get_error()
698 {
699 if ($this->error == 'Success'){
700 return $this->error;
701 } else {
702 $error= $this->error." (".$this->get_additional_error().")";
703 return $error;
704 }
705 }
707 function get_credentials($url, $referrals= NULL)
708 {
709 $ret= array();
710 $url= preg_replace('!\?\?.*$!', '', $url);
711 $server= preg_replace('!^([^:]+://[^/]+)/.*$!', '\\1', $url);
713 if ($referrals == NULL){
714 $referrals= $this->referrals;
715 }
717 if (isset($referrals[$server])){
718 return ($referrals[$server]);
719 } else {
720 $ret['ADMIN']= $this->fix($this->binddn);
721 $ret['PASSWORD']= $this->bindpw;
722 }
724 return ($ret);
725 }
728 function gen_ldif ($dn, $filter= "(objectClass=*)", $attributes= array('*'), $recursive= TRUE)
729 {
730 $display= "";
732 if ($recursive){
733 $this->cd($dn);
734 $this->search("$filter", array('dn'));
735 while ($attrs= $this->fetch()){
736 $display.= $this->gen_one_entry($attrs['dn'], $filter, $attributes);
737 $display.= "\n";
738 }
739 } else {
740 $display.= $this->gen_one_entry($dn);
741 }
743 return ($display);
744 }
746 function gen_xls ($dn, $filter= "(objectClass=*)", $attributes= array('*'), $recursive= TRUE,$r_array=0)
747 {
748 $display= "";
750 $this->cd($dn);
751 $this->search("$filter");
753 $i=0;
754 while ($attrs= $this->fetch()){
755 $j=0;
757 foreach ($attributes as $at){
758 $display[$i][$j]= $this->get_attribute($attrs['dn'], $at,$r_array);
759 $j++;
760 }
762 $i++;
763 }
765 return ($display);
766 }
769 function gen_one_entry($dn, $filter= "(objectClass=*)" , $name= array("*"))
770 {
771 $ret = "";
772 $data = "";
773 if($this->reconnect){
774 $this->connect();
775 }
777 /* Searching Ldap Tree */
778 $sr= @ldap_read($this->cid, $this->fix($dn), $filter, $name);
780 /* Get the first entry */
781 $entry= @ldap_first_entry($this->cid, $sr);
783 /* Get all attributes related to that Objekt */
784 $atts = array();
786 /* Assemble dn */
787 $atts[0]['name'] = "dn";
788 $atts[0]['value'] = array('count' => 1, 0 => $dn);
790 /* Reset index */
791 $i = 1 ;
792 $identifier = array();
793 $attribute= @ldap_first_attribute($this->cid,$entry,$identifier);
794 while ($attribute) {
795 $i++;
796 $atts[$i]['name'] = $attribute;
797 $atts[$i]['value'] = @ldap_get_values_len($this->cid, $entry, "$attribute");
799 /* Next one */
800 $attribute= @ldap_next_attribute($this->cid,$entry,$identifier);
801 }
803 foreach($atts as $at)
804 {
805 for ($i= 0; $i<$at['value']['count']; $i++){
807 /* Check if we must encode the data */
808 if(!preg_match('/^[a-z0-9+@#.=, \/ -]+$/i', $at['value'][$i])) {
809 $ret .= $at['name'].":: ".base64_encode($at['value'][$i])."\n";
810 } else {
811 $ret .= $at['name'].": ".$at['value'][$i]."\n";
812 }
813 }
814 }
816 return($ret);
817 }
820 function dn_exists($dn)
821 {
822 return @ldap_list($this->cid, $this->fix($dn), "(objectClass=*)", array("objectClass"));
823 }
827 function import_complete_ldif($str_attr,&$error,$overwrite,$cleanup)
828 {
829 if($this->reconnect) $this->connect();
831 /* First we have to splitt the string ito detect empty lines
832 An empty line indicates an new Entry */
833 $entries = split("\n",$str_attr);
835 $data = "";
836 $cnt = 0;
837 $current_line = 0;
839 /* Every single line ... */
840 foreach($entries as $entry) {
841 $current_line ++;
843 /* Removing Spaces to ..
844 .. test if a new entry begins */
845 $tmp = str_replace(" ","",$data );
847 /* .. prevent empty lines in an entry */
848 $tmp2 = str_replace(" ","",$entry);
850 /* If the Block ends (Empty Line) */
851 if((empty($entry))&&(!empty($tmp))) {
852 /* Add collected lines as a complete block */
853 $all[$cnt] = $data;
854 $cnt ++;
855 $data ="";
856 } else {
858 /* Append lines ... */
859 if(!empty($tmp2)) {
860 /* check if we need base64_decode for this line */
861 if(ereg("::",$tmp2))
862 {
863 $encoded = split("::",$entry);
864 $attr = $encoded[0];
865 $value = base64_decode($encoded[1]);
866 /* Add linenumber */
867 $data .= $current_line."#".$attr.":".$value."\n";
868 }
869 else
870 {
871 /* Add Linenumber */
872 $data .= $current_line."#".$entry."\n";
873 }
874 }
875 }
876 }
878 /* The Data we collected is not in the array all[];
879 For example the Data is stored like this..
881 all[0] = "1#dn : .... \n
882 2#ObjectType: person \n ...."
884 Now we check every insertblock and try to insert */
885 foreach ( $all as $single) {
886 $lineone = split("\n",$single);
887 $ndn = split("#", $lineone[0]);
888 $line = $ndn[1];
890 $dnn = split (":",$line);
891 $current_line = $ndn[0];
892 $dn = $dnn[0];
893 $value = $dnn[1];
895 /* Every block must begin with a dn */
896 if($dn != "dn") {
897 $error= sprintf(_("This is not a valid DN: '%s'. A block for import should begin with 'dn: ...' in line %s"), $line, $current_line);
898 return -2;
899 }
901 /* Should we use Modify instead of Add */
902 $usemodify= false;
904 /* Delete before insert */
905 $usermdir= false;
907 /* The dn address already exists! */
908 if (($this->dn_exists($value))&&((!$overwrite)&&(!$cleanup))) {
910 $error= sprintf(_("The dn: '%s' (from line %s) already exists in the LDAP database."), $line, $current_line);
911 return ALREADY_EXISTING_ENTRY;
913 } elseif(($this->dn_exists($value))&&($cleanup)){
915 /* Delete first, then add */
916 $usermdir = true;
918 } elseif(($this->dn_exists($value))&&($overwrite)) {
920 /* Modify instead of Add */
921 $usemodify = true;
922 }
924 /* If we can't Import, return with a file error */
925 if(!$this->import_single_entry($single,$usemodify,$usermdir) ) {
926 $error= sprintf(_("Error while importing dn: '%s', please check your LDIF from line %s on!"), $line,
927 $current_line);
928 return UNKNOWN_TOKEN_IN_LDIF_FILE; }
929 }
931 return (INSERT_OK);
932 }
935 /* Imports a single entry */
936 function import_single_entry($str_attr,$modify,$delete)
937 {
938 if($this->reconnect) $this->connect();
940 $ret = false;
941 $rows= split("\n",$str_attr);
942 $data= false;
944 foreach($rows as $row) {
946 /* Check if we use Linenumbers (when import_complete_ldif is called we use
947 Linenumbers) Linenumbers are use like this 123#attribute : value */
948 if(!empty($row)) {
949 if((strpos($row,"#")!=FALSE)&&(strpos($row,"#")<strpos($row,":"))) {
951 /* We are using line numbers
952 Because there is a # before a : */
953 $tmp1= split("#",$row);
954 $current_line= $tmp1[0];
955 $row= $tmp1[1];
956 }
958 /* Split the line into attribute and value */
959 $attr = split(":", $row);
960 $attr[0]= trim($attr[0]); /* attribute */
961 $attr[1]= trim($attr[1]); /* value */
963 /* Check for attributes that are used more than once */
964 if(!isset($data[$attr[0]])) {
965 $data[$attr[0]]=$attr[1];
966 } else {
967 $tmp = $data[$attr[0]];
969 if(!is_array($tmp)) {
970 $new[0]=$tmp;
971 $new[1]=$attr[1];
972 $datas[$attr[0]]['count']=1;
973 $data[$attr[0]]=$new;
974 } else {
975 $cnt = $datas[$attr[0]]['count'];
976 $cnt ++;
977 $data[$attr[0]][$cnt]=$attr[1];
978 $datas[$attr[0]]['count'] = $cnt;
979 }
980 }
981 }
982 }
984 /* If dn is an index of data, we should try to insert the data */
985 if(isset($data['dn'])) {
986 /* Creating Entry */
987 $this->cd($data['dn']);
989 /* Delete existing entry */
990 if($delete){
991 $this->rmdir($data['dn']);
992 }
994 /* Create missing trees */
995 $this->create_missing_trees($data['dn']);
996 unset($data['dn']);
998 /* If entry exists use modify */
999 if(!$modify){
1000 $ret = $this->add($data);
1001 } else {
1002 $ret = $this->modify($data);
1003 }
1004 }
1006 return($ret);
1007 }
1010 function importcsv($str)
1011 {
1012 $lines = split("\n",$str);
1013 foreach($lines as $line)
1014 {
1015 /* continue if theres a comment */
1016 if(substr(trim($line),0,1)=="#"){
1017 continue;
1018 }
1020 $line= str_replace ("\t\t","\t",$line);
1021 $line= str_replace ("\t" ,"," ,$line);
1022 echo $line;
1024 $cells = split(",",$line ) ;
1025 $linet= str_replace ("\t\t",",",$line);
1026 $cells = split("\t",$line);
1027 $count = count($cells);
1028 }
1030 }
1032 }
1034 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1035 ?>