c6193c635039e99e16cb4967bfe92f36a89f6c94
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
399 */
400 function copy_FAI_resource_recursive($sourcedn,$destinationdn,$type="branch",$is_first = true,$depth=0)
401 {
402 error_reporting(E_ALL);
405 if($is_first){
406 echo "<font style='font-size:11px;'><h4>".
407 sprintf(_("Creating duplicate of %s in %s"),"<br><i>".$sourcedn."</i><br>","<br><i>".$destinationdn."</i><br>")."</h4>";
408 }else{
409 if($depth == 1 ){
410 echo "<br><h3>"._("Processing")." : ".preg_replace("/,.*$/","",$destinationdn)."</h3>";
411 }else{
412 $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 if($this->count($this->fetch($this->cat($destinationdn)))){
438 return;
439 }else{
441 /* Get source entrie */
442 $this->cd($basedn);
443 $attr = $this->fetch($this->cat($sourcedn));
445 /* check if this is a department */
446 if(in_array("organizationalUnit",$attr['objectClass'])){
447 $attr['dn'] = $this->convert($destinationdn);
448 $this->cd($basedn);
449 $this->create_missing_trees($destinationdn);
450 $this->cd($destinationdn);
452 /* If is first entry, append FAIbranch to department entry */
453 if($is_first){
454 $attr= $this->fetch($this->cat($destinationdn));
456 /* Filter unneeded informations */
457 foreach($attr as $key => $value){
458 if(is_numeric($key)) unset($attr[$key]);
459 if(isset($attr[$key]['count'])){
460 if(is_array($attr[$key])){
461 unset($attr[$key]['count']);
462 }
463 }
464 }
466 unset($attr['count']);
467 unset($attr['dn']);
469 /* Add marking attribute */
470 $attr['objectClass'][] = "FAIbranch";
472 /* Add this entry */
473 $this->modify($attr);
474 }
476 }else{
477 /* If this is no department */
478 foreach($attr as $key => $value){
480 if(in_array($key ,array("FAItemplateFile","FAIscript"))){
481 $sr= ldap_read($this->cid, $this->fix($sourcedn), "(objectClass=FAIclass)", array($key));
482 $ei= ldap_first_entry($this->cid, $sr);
483 $tmp = (@ldap_get_values_len($this->cid, $ei,$key));
484 if(is_array($tmp)){
485 $attr[$key] = $tmp;
486 }
487 }
489 if(is_numeric($key)) unset($attr[$key]);
490 if(isset($attr[$key]['count'])){
491 if(is_array($attr[$key])){
492 unset($attr[$key]['count']);
493 }
494 }
495 }
496 unset($attr['count']);
497 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 $depth ++;
536 foreach($delarray as $dn => $bla){
537 if($dn != $destinationdn){
538 $this->cd($basedn);
539 $item = $this->fetch($this->cat($dn));
540 if(!in_array("FAIbranch",$item['objectClass'])){
541 $this->copy_FAI_resource_recursive($dn,str_replace($sourcedn,$destinationdn,$dn),$type,false,$depth);
542 }
543 }
544 }
545 }
546 if($is_first){
547 echo "<br><br><b>"._("Successfully finished")."</b></font>";
548 }
550 }
552 function modify($attrs)
553 {
554 if(count($attrs) == 0){
555 return (0);
556 }
557 if($this->hascon){
558 if ($this->reconnect) $this->connect();
559 $r = @ldap_modify($this->cid, $this->fix($this->basedn), $attrs);
560 $this->error = @ldap_error($this->cid);
561 return($r ? $r : 0);
562 }else{
563 $this->error = "Could not connect to LDAP server";
564 return("");
565 }
566 }
568 function add($attrs)
569 {
570 if($this->hascon){
571 if ($this->reconnect) $this->connect();
572 $r = @ldap_add($this->cid, $this->fix($this->basedn), $attrs);
573 $this->error = @ldap_error($this->cid);
574 return($r ? $r : 0);
575 }else{
576 $this->error = "Could not connect to LDAP server";
577 return("");
578 }
579 }
581 function create_missing_trees($target)
582 {
583 /* Ignore create_missing trees if the base equals target */
584 if ($target == $this->basedn){
585 return;
586 }
587 $l= array_reverse(explode(",", preg_replace("/,".$this->basedn."/", "", $target)));
588 $cdn= $this->basedn;
589 foreach ($l as $part){
590 $cdn= "$part,$cdn";
592 /* Ignore referrals */
593 $found= false;
594 foreach($this->referrals as $ref){
595 $base= preg_replace('!^[^:]+://[^/]+/([^?]+).*$!', '\\1', $ref['URL']);
596 if ($base == $cdn){
597 $found= true;
598 break;
599 }
600 }
601 if ($found){
602 continue;
603 }
605 $this->cat ($cdn);
606 $attrs= $this->fetch();
608 /* Create missing entry? */
609 if (!count ($attrs)){
610 $type= preg_replace('/^([^=]+)=.*$/', '\\1', $cdn);
611 $param= preg_replace('/^[^=]+=([^,]+),.*$/', '\\1', $cdn);
613 $na= array();
614 switch ($type){
615 case 'ou':
616 $na["objectClass"]= "organizationalUnit";
617 $na["ou"]= $param;
618 break;
619 case 'dc':
620 $na["objectClass"]= array("dcObject", "top", "locality");
621 $na["dc"]= $param;
622 break;
623 default:
624 print_red(sprintf(_("Autocreation of type '%s' is currently not supported. Please report to the GOsa team."), $type));
625 echo $_SESSION['errors'];
626 exit;
627 }
628 $this->cd($cdn);
629 $this->add($na);
630 }
631 }
632 }
634 function recursive_remove()
635 {
636 $delarray= array();
638 /* Get sorted list of dn's to delete */
639 $this->search ("(objectClass=*)");
640 while ($this->fetch()){
641 $deldn= $this->getDN();
642 $delarray[$deldn]= strlen($deldn);
643 }
644 arsort ($delarray);
645 reset ($delarray);
647 /* Delete all dn's in subtree */
648 foreach ($delarray as $key => $value){
649 $this->rmdir($key);
650 }
651 }
653 function get_attribute($dn, $name,$r_array=0)
654 {
655 $data= "";
656 if ($this->reconnect) $this->connect();
657 $sr= @ldap_read($this->cid, $this->fix($dn), "objectClass=*", array("$name"));
659 /* fill data from LDAP */
660 if ($sr) {
661 $ei= @ldap_first_entry($this->cid, $sr);
662 if ($ei) {
663 if ($info= @ldap_get_values_len($this->cid, $ei, "$name")){
664 $data= $info[0];
665 }
667 }
668 }
669 if($r_array==0)
670 return ($data);
671 else
672 return ($info);
675 }
679 function get_additional_error()
680 {
681 $error= "";
682 @ldap_get_option ($this->cid, LDAP_OPT_ERROR_STRING, $error);
683 return ($error);
684 }
686 function get_error()
687 {
688 if ($this->error == 'Success'){
689 return $this->error;
690 } else {
691 $error= $this->error." (".$this->get_additional_error().")";
692 return $error;
693 }
694 }
696 function get_credentials($url, $referrals= NULL)
697 {
698 $ret= array();
699 $url= preg_replace('!\?\?.*$!', '', $url);
700 $server= preg_replace('!^([^:]+://[^/]+)/.*$!', '\\1', $url);
702 if ($referrals == NULL){
703 $referrals= $this->referrals;
704 }
706 if (isset($referrals[$server])){
707 return ($referrals[$server]);
708 } else {
709 $ret['ADMIN']= $this->fix($this->binddn);
710 $ret['PASSWORD']= $this->bindpw;
711 }
713 return ($ret);
714 }
717 function gen_ldif ($dn, $filter= "(objectClass=*)", $attributes= array('*'), $recursive= TRUE)
718 {
719 $display= "";
721 if ($recursive){
722 $this->cd($dn);
723 $this->search("$filter", array('dn'));
724 while ($attrs= $this->fetch()){
725 $display.= $this->gen_one_entry($attrs['dn'], $filter, $attributes);
726 $display.= "\n";
727 }
728 } else {
729 $display.= $this->gen_one_entry($dn);
730 }
732 return ($display);
733 }
735 function gen_xls ($dn, $filter= "(objectClass=*)", $attributes= array('*'), $recursive= TRUE,$r_array=0)
736 {
737 $display= "";
739 $this->cd($dn);
740 $this->search("$filter");
742 $i=0;
743 while ($attrs= $this->fetch()){
744 $j=0;
746 foreach ($attributes as $at){
747 $display[$i][$j]= $this->get_attribute($attrs['dn'], $at,$r_array);
748 $j++;
749 }
751 $i++;
752 }
754 return ($display);
755 }
758 function gen_one_entry($dn, $filter= "(objectClass=*)" , $name= array("*"))
759 {
760 $ret = "";
761 $data = "";
762 if($this->reconnect){
763 $this->connect();
764 }
766 /* Searching Ldap Tree */
767 $sr= @ldap_read($this->cid, $this->fix($dn), $filter, $name);
769 /* Get the first entry */
770 $entry= @ldap_first_entry($this->cid, $sr);
772 /* Get all attributes related to that Objekt */
773 $atts = array();
775 /* Assemble dn */
776 $atts[0]['name'] = "dn";
777 $atts[0]['value'] = array('count' => 1, 0 => $dn);
779 /* Reset index */
780 $i = 1 ;
781 $identifier = array();
782 $attribute= @ldap_first_attribute($this->cid,$entry,$identifier);
783 while ($attribute) {
784 $i++;
785 $atts[$i]['name'] = $attribute;
786 $atts[$i]['value'] = @ldap_get_values_len($this->cid, $entry, "$attribute");
788 /* Next one */
789 $attribute= @ldap_next_attribute($this->cid,$entry,$identifier);
790 }
792 foreach($atts as $at)
793 {
794 for ($i= 0; $i<$at['value']['count']; $i++){
796 /* Check if we must encode the data */
797 if(!preg_match('/^[a-z0-9+@#.=, \/ -]+$/i', $at['value'][$i])) {
798 $ret .= $at['name'].":: ".base64_encode($at['value'][$i])."\n";
799 } else {
800 $ret .= $at['name'].": ".$at['value'][$i]."\n";
801 }
802 }
803 }
805 return($ret);
806 }
809 function dn_exists($dn)
810 {
811 return @ldap_list($this->cid, $this->fix($dn), "(objectClass=*)", array("objectClass"));
812 }
816 function import_complete_ldif($str_attr,&$error,$overwrite,$cleanup)
817 {
818 if($this->reconnect) $this->connect();
820 /* First we have to splitt the string ito detect empty lines
821 An empty line indicates an new Entry */
822 $entries = split("\n",$str_attr);
824 $data = "";
825 $cnt = 0;
826 $current_line = 0;
828 /* Every single line ... */
829 foreach($entries as $entry) {
830 $current_line ++;
832 /* Removing Spaces to ..
833 .. test if a new entry begins */
834 $tmp = str_replace(" ","",$data );
836 /* .. prevent empty lines in an entry */
837 $tmp2 = str_replace(" ","",$entry);
839 /* If the Block ends (Empty Line) */
840 if((empty($entry))&&(!empty($tmp))) {
841 /* Add collected lines as a complete block */
842 $all[$cnt] = $data;
843 $cnt ++;
844 $data ="";
845 } else {
847 /* Append lines ... */
848 if(!empty($tmp2)) {
849 /* check if we need base64_decode for this line */
850 if(ereg("::",$tmp2))
851 {
852 $encoded = split("::",$entry);
853 $attr = $encoded[0];
854 $value = base64_decode($encoded[1]);
855 /* Add linenumber */
856 $data .= $current_line."#".$attr.":".$value."\n";
857 }
858 else
859 {
860 /* Add Linenumber */
861 $data .= $current_line."#".$entry."\n";
862 }
863 }
864 }
865 }
867 /* The Data we collected is not in the array all[];
868 For example the Data is stored like this..
870 all[0] = "1#dn : .... \n
871 2#ObjectType: person \n ...."
873 Now we check every insertblock and try to insert */
874 foreach ( $all as $single) {
875 $lineone = split("\n",$single);
876 $ndn = split("#", $lineone[0]);
877 $line = $ndn[1];
879 $dnn = split (":",$line);
880 $current_line = $ndn[0];
881 $dn = $dnn[0];
882 $value = $dnn[1];
884 /* Every block must begin with a dn */
885 if($dn != "dn") {
886 $error= sprintf(_("This is not a valid DN: '%s'. A block for import should begin with 'dn: ...' in line %s"), $line, $current_line);
887 return -2;
888 }
890 /* Should we use Modify instead of Add */
891 $usemodify= false;
893 /* Delete before insert */
894 $usermdir= false;
896 /* The dn address already exists! */
897 if (($this->dn_exists($value))&&((!$overwrite)&&(!$cleanup))) {
899 $error= sprintf(_("The dn: '%s' (from line %s) already exists in the LDAP database."), $line, $current_line);
900 return ALREADY_EXISTING_ENTRY;
902 } elseif(($this->dn_exists($value))&&($cleanup)){
904 /* Delete first, then add */
905 $usermdir = true;
907 } elseif(($this->dn_exists($value))&&($overwrite)) {
909 /* Modify instead of Add */
910 $usemodify = true;
911 }
913 /* If we can't Import, return with a file error */
914 if(!$this->import_single_entry($single,$usemodify,$usermdir) ) {
915 $error= sprintf(_("Error while importing dn: '%s', please check your LDIF from line %s on!"), $line,
916 $current_line);
917 return UNKNOWN_TOKEN_IN_LDIF_FILE; }
918 }
920 return (INSERT_OK);
921 }
924 /* Imports a single entry */
925 function import_single_entry($str_attr,$modify,$delete)
926 {
927 if($this->reconnect) $this->connect();
929 $ret = false;
930 $rows= split("\n",$str_attr);
931 $data= false;
933 foreach($rows as $row) {
935 /* Check if we use Linenumbers (when import_complete_ldif is called we use
936 Linenumbers) Linenumbers are use like this 123#attribute : value */
937 if(!empty($row)) {
938 if((strpos($row,"#")!=FALSE)&&(strpos($row,"#")<strpos($row,":"))) {
940 /* We are using line numbers
941 Because there is a # before a : */
942 $tmp1= split("#",$row);
943 $current_line= $tmp1[0];
944 $row= $tmp1[1];
945 }
947 /* Split the line into attribute and value */
948 $attr = split(":", $row);
949 $attr[0]= trim($attr[0]); /* attribute */
950 $attr[1]= trim($attr[1]); /* value */
952 /* Check for attributes that are used more than once */
953 if(!isset($data[$attr[0]])) {
954 $data[$attr[0]]=$attr[1];
955 } else {
956 $tmp = $data[$attr[0]];
958 if(!is_array($tmp)) {
959 $new[0]=$tmp;
960 $new[1]=$attr[1];
961 $datas[$attr[0]]['count']=1;
962 $data[$attr[0]]=$new;
963 } else {
964 $cnt = $datas[$attr[0]]['count'];
965 $cnt ++;
966 $data[$attr[0]][$cnt]=$attr[1];
967 $datas[$attr[0]]['count'] = $cnt;
968 }
969 }
970 }
971 }
973 /* If dn is an index of data, we should try to insert the data */
974 if(isset($data['dn'])) {
975 /* Creating Entry */
976 $this->cd($data['dn']);
978 /* Delete existing entry */
979 if($delete){
980 $this->rmdir($data['dn']);
981 }
983 /* Create missing trees */
984 $this->create_missing_trees($data['dn']);
985 unset($data['dn']);
987 /* If entry exists use modify */
988 if(!$modify){
989 $ret = $this->add($data);
990 } else {
991 $ret = $this->modify($data);
992 }
993 }
995 return($ret);
996 }
999 function importcsv($str)
1000 {
1001 $lines = split("\n",$str);
1002 foreach($lines as $line)
1003 {
1004 /* continue if theres a comment */
1005 if(substr(trim($line),0,1)=="#"){
1006 continue;
1007 }
1009 $line= str_replace ("\t\t","\t",$line);
1010 $line= str_replace ("\t" ,"," ,$line);
1011 echo $line;
1013 $cells = split(",",$line ) ;
1014 $linet= str_replace ("\t\t",",",$line);
1015 $cells = split("\t",$line);
1016 $count = count($cells);
1017 }
1019 }
1021 }
1023 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1024 ?>