1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
|
<?php
// $Header: /cvsroot/phpldapadmin/phpldapadmin/ldif_functions.php,v 1.23 2004/04/18 19:21:54 xrenard Exp $
/**
* @todo put the display_parse_error method in ldif_import here
* @todo do not allow entry whose number of unfolded lines is
* over 200 for example
* @todo later: return the entry as an array before returning the entry object
* (just make a wrapper method)
* @todo make a class CSVReader wich extends FileReader
*
* @package phpLDAPadmin
*
* @author The phpLDAPadmin development team
*/
/**
* This class represente an entry in the ldif file
*/
class LdifEntry{
var $dn;
var $changeType;
var $attributes=array();
/**
* Creates a new LdifEntry, with optionally specified DN, changeType, and attributes.
*
* @param String $dn the distinguished name of the entry. Default is set to an empty string
* @param String $changeType the change type associated with the entry. Default is set to add.
* @param String[] $atts the attributes of the entry
*/
function LdifEntry($dn="",$changeType="add",$atts = array()){
$this->dn=$dn;
$this->changeType=$changeType;
$this->attributes=$atts;
}
/**
* Return the dn of the entry
*
* @return String the dn of the entry
*/
function getDn(){
return $this->dn;
}
/**
* Setter method for the distinguished name
*
* @param String $dn the distinguished name of the entry
*/
function setDn($dn){
$this->dn = $dn;
}
/**
* Getter method for the change type
*
* @return String the change type of the entry
*/
function getChangeType(){
return $this->changeType;
}
/**
* Setter method for the change type of the entry
*
* @param String $changeType the change type of the entry
*/
function setChangeType($changeType){
$this->changeType = $changeType;
}
/**
* Add the attributes to the entry
*
* @param String[][] $atts the attributes of the entry
*/
function setAttributes($atts){
$this->attributes = $atts;
}
/**
* Get the attributes of the entry
*
* @return String[][] the attributes of the entry
*/
function getAttributes(){
return $this->attributes;
}
}
/**
* This exception is similar to the one in LdifReader
* Should be remove latter
* see comment for the class Ldif_LdapEntryReader
*/
class LdifEntryReaderException{
var $lineNumber;
var $currentLine;
var $message;
/**
* Constructor of the exception
*
* @param int $lineNumber the number of the line where
* the error occured
* @param String currentLine the line wich raised an exception
* @param String the message associated the exception
*/
function LdifEntryReaderException($lineNumber,$currentLine,$message){
$this->lineNumber = $lineNumber;
$this->currentLine =$currentLine;
$this->message = $message;
}
}
/**
* Class in charge of reading a paricular entry
*/
class LdifEntryReader{
//the entry
var $entry;
// the lines of the entry fetch from the file
var $lines;
// the dn of the entry
var $dn="";
// error flag
var $_error;
// the current line number of the entry;
var $_currentLineNumber;
/**
* Constructor of the LdifEntryReader
*
* @param String[] $lines the line of the entry
*/
function LdifEntryReader( &$lines ){
$this->lines = &$lines;
$this->_currentLineNumber = 1;
$this->_error = 0;
}
/**
* Read the change type action associated with the entry
*
* @return String the change type action of the entry
*/
function _readChangeType(){
$changeType ="add";
$arr = array();
// no lines after the dn one
if(count($this->lines)==0){
$this->lines[0] = "";
$this->setLdifEntryReaderException(new LdifEntryReaderException($this->_currentLineNumber,$this->lines[0],"Missing attibutes or changetype attribute for entry",new LdifEntry($this->dn)));
}
// get the change type of the entry
elseif(ereg("changetype:[ ]*(delete|add|modrdn|moddn|modify)",$this->lines[0],$arr)){
$changeType = $arr[1];
array_shift($this->lines);
$this->_currentLineNumber++;
}
return $changeType;
}
/**
* Check if the distinguished name is base 64 encoded
*
* @return boolean true if the dn is base 64 encoded, false otherwise
*/
function _isDnBase64Encoded($dn){
return ereg("dn::",$dn)?1:0;
}
/**
* Return the base64 decoded value of an attribute
*
* @param $attr the attribute to be decoded
* @return String base64 decoded value of an attribute
*/
function _getBase64DecodedValue($attr){
return base64_decode(trim($attr));
}
/**
* Fetch the dn value from a line of the ldif file
*
* @param String $currentDnLine line with a distinguished name
* @return the value of the distinguished name
*/
function _getDnValue(){
$currentDnLine=$this->lines[0];
if($this->_isDNBase64Encoded($currentDnLine)){
$currentDnValue = $this->_getBase64DecodedValue(substr($currentDnLine,4,strlen($currentDnLine)-1));
}else{
$currentDnValue = substr($currentDnLine,3,strlen($currentDnLine)-1);
}
// switch to the next line
array_shift($this->lines);
$this->_currentLineNumber++;
return trim($currentDnValue);
}
/**
* Check if the dn line is valid
*
* @return boolean true if the dn is valid, false otherwise.
*/
function isValidDn(){
return ereg("^dn:",$this->lines[0])?1:0;
}
/**
* Return the entry read from the ldif lines
*
* @return LdifEntry the entry
*/
function getEntry(){
global $lang;
// the dn is not valid, throw the exception and return the entry with the non valid dn
if (! $this->isValidDn() ){
$dn = $this->lines[0];
$changeType = $this->_readChangeType();
//For the moment, overwrite the exception
$this->setLdifEntryReaderException(new LdifEntryReaderException($this->_currentLineNumber,$this->lines[0],$lang['valid_dn_line_required'],new LdifEntry($this->dn)));
return new LdifEntry( $dn , $changeType );
}
$dn=$this->_getDnValue();
$changeType = $this->_readChangeType();
$this->entry = new LdifEntry($dn,$changeType);
if($changeType=="add"){
$this->_getAddAttributes();
}
elseif($changeType=="delete"){
//do nothing
}
elseif($changeType=="modrdn"||$changeType=="moddn"){
$this->_getModrdnAttributes();
}
elseif($changeType=="modify"){
$this->_getModifyAttributes();
}
return $this->entry;
}
/**
* Checked if the parsing of the entry has raised some exception
*
* @return bool true if the reading of the entry raised some exceptions, else otherwise.
*/
function hasRaisedException(){
return $this->_error;
}
/**
* Set the exception handler for the entry reader
*
* @param Ldap_ldifEntryReaderException the exception handler associate
* with the parsing error.
*/
function setLdifEntryReaderException($ldifEntryReaderException){
$this->_error=1;
$this->_ldifEntryReaderException= $ldifEntryReaderException;
}
/**
* Return the exception handler of the entry Reader
*
* @return Ldap_LdifEntryReaderException the exception associate with
* the exception wich occur during parsing the file.
*/
function getLdifEntryReaderException(){
return $this->_ldifEntryReaderException;
}
/**
* Method to retrieve the attribute value of a ldif line,
* and get the base 64 decoded value if it is encoded
*/
function _getAttributeValue($attributeValuePart){
$attribute_value="";
if(substr($attributeValuePart,0,1)==":"){
$attribute_value = $this->_getBase64DecodedValue(trim(substr($attributeValuePart,1)));
}
elseif(substr($attributeValuePart,0,1)=="<"){
// we need to handle the case for the scheme "file://" as it
//doesn't seem to be supported by fopen
$file_path_with_scheme= trim(substr($attributeValuePart,1));
if(ereg("^file://",$file_path_with_scheme)){
$file_path = substr(trim($file_path_with_scheme),7);
if($handle = @fopen($file_path, "rb")){
if(!$attribute_value = @fread($handle,filesize($file_path))){
$this->setLdifEntryReaderException(new LdifEntryReaderException($this->_currentLineNumber,$this->lines[0],"Unable to read file",$this->entry));
}
@fclose($handle);
}
else{
$this->setLdifEntryReaderException(new LdifEntryReaderException($this->_currentLineNumber,$this->lines[0],"Unable to open file",$this->entry));
}
}
else{
$this->setLdifEntryReaderException(new LdifEntryReaderException($this->_currentLineNumber,$this->lines[0],"The url attribute value should begin with file:///",$this->entry));
}
}
//it's a string
else{
$attribute_value = $attributeValuePart;
}
return trim($attribute_value);
}
/**
* Build the attributes array when the change type is add.
*/
function _getAddAttributes(){
if(count($this->lines)==0){
$this->lines[0]="";
$this->setLdifEntryReaderException(new LdifEntryReaderException($this->_currentLineNumber,$this->lines[0],"Missing attributes for the entry",$this->entry));
}
//$attrs =array();
while(count($this->lines)!=0 &&$this->_error!=1){
$currentLine = &$this->lines[0];
// echo $this->_currentLineNumber;
if(ereg(":",$currentLine)){
//get the position of the character ":"
$pos = strpos($currentLine,":");
//get the description of the attribute
$attributeDescription = substr($currentLine,0, $pos);
// get the value part of the attribute
$attribute_value_part = trim(substr($currentLine,$pos+1,strlen($currentLine)));
$attribute_value = $this->_getAttributeValue($attribute_value_part);
$this->entry->attributes[$attributeDescription][] = trim($attribute_value);
//echo count($this->entry->attributes);;
// $this->entry->add($attrs);
array_shift($this->lines);
$this->_currentLineNumber++;
}
else{
$this->setLdifEntryReaderException(new LdifEntryReaderException($this->_currentLineNumber,$this->lines[0],"Attribute not well formed",$this->entry));
//jetter l'exception
}
}
}
/**
* Build the attributes array for the entry when the change type is modify
*/
function _getModifyAttributes(){
if(count($this->lines)==0){
$this->lines[0]="";
$this->setLdifEntryReaderException(new LdifEntryReaderException($this->_currentLineNumber,$this->lines[0],"Missing attributes for the entry",$this->entry));
}
$numberModification=0;
// while the array is not empty
while(count($this->lines)!=0 &&$this->_error!=1){
$new_entry_mod = 0;
// get the current line with the action
$currentLine = &$this->lines[0];
$attribute= explode(":",$currentLine);
if(count($attribute)==2){
$action_attribute = trim($attribute[0]);
$action_attribute_value =trim($attribute[1]);
if($action_attribute != "add" && $action_attribute != "delete" && $action_attribute !="replace"){
$this->setLdifEntryReaderException(new LdifEntryReaderException($this->_currentLineNumber,$this->lines[0],"The attribute name should be add, delete or replace",$this->entry));
}
// put the action attribute in the array
$this->entry->attributes[$numberModification] = array();
$this->entry->attributes[$numberModification][$action_attribute] = $this->_getAttributeValue($action_attribute_value);
$this->entry->attributes[$numberModification][$action_attribute_value] = array();
// fetching the attribute for the following line
array_shift($this->lines);
$currentLine=&$this->lines[0];
$this->_currentLineNumber++;
while(trim($currentLine)!="-" && $this->_error!=1 && count($this->lines)!=0 ){
// if there is a valid line
if(ereg(":",$currentLine)){
//get the position of the character ":"
$pos = strpos($currentLine,":");
//get the name of the attribute to modify
$attribute_name = substr($currentLine,0, $pos);
//check that it correspond to the one specified before
if ($attribute_name == $action_attribute_value){
// get the value part of the attribute
$attribute_value_part = trim(substr($currentLine,$pos+1,strlen($currentLine)));
$attribute_value = $this->_getAttributeValue($attribute_value_part);
$this->entry->attributes[$numberModification][$attribute_name][]=$attribute_value;
array_shift($this->lines);
$this->_currentLineNumber++;
if(count($this->lines)!=0)
$currentLine = &$this->lines[0];
}
else{
$this->setLdifEntryReaderException(new LdifEntryReaderException($this->_currentLineNumber,$this->lines[0],"The attribute to modify doesn't match the one specified by the ".$action_attribute." attribute.",$this->entry));
}
}
else{
$this->setLdifEntryReaderException(new LdifEntryReaderException($this->_currentLineNumber,$this->lines[0],"Attribute is not valid",$this->entry));
}
}// end inner while
// we get a "-" charachter, we remove it from the array
if ($currentLine == "-"){
array_shift($this->lines);
$this->_currentLineNumber++;
}
$numberModification++;
}
}
}
/**
* Build the attributes for the entry when the change type is modrdn
*/
function _getModrdnAttributes(){
$attrs = array();
$numLines = count($this->lines);
if($numLines != 2 && $numLines !=3){
if($numLines==0){
$this->lines[0]="";
}
$this->setLdifEntryReaderException(new LdifEntryReaderException($this->_currentLineNumber,$this->lines[0],"The entry is not valid",$this->entry));
}
else{
$currentLine = $this->lines[0];
//first we need to check if there is an new rdn specified
if(ereg("^newrdn:(:?)",$currentLine)){
$attributeValue = $this->_getAttributeValue(trim(substr($currentLine,7)));
$attrs['newrdn']=$attributeValue;
//switch to the deleteoldrdn attribute
array_shift($this->lines);
$this->_currentLineNumber++;
$arr=array();
if(ereg("^deleteoldrdn:[ ]*(0|1)",$this->lines[0],$arr)){
$attrs['deleteoldrdn'] = $arr[1];
//switch to the possible new superior attribute
if($numLines>2){
array_shift($this->lines);
$this->_currentLineNumber++;
$currentLine = $this->lines[0];
//then the possible new superior attribute
//if(trim($currentLine)!=""){
if(ereg("^newsuperior:",$currentLine)){
$attrs['newsuperior'] = $this->_getAttributeValue(trim(substr($currentLine,12)));
}
else{
$this->setLdifEntryReaderException(new LdifEntryReaderException($this->_currentLineNumber,$this->lines[0],"the attribute name should be newsuperior",$this->entry));
}
}
else{
//as the first character is not ,,we "can write it this way for teh moment"
if($pos = strpos($this->entry->dn,",")){
$attrs['newsuperior'] = substr($this->entry->dn,$pos+1);
}
else{
$this->setLdifEntryReaderException(new LdifEntryReaderException($this->_currentLineNumber,$this->lines[0],"Container is null",$this->entry));
}
}
}
else{
$this->setLdifEntryReaderException(new LdifEntryReaderException($this->_currentLineNumber,$this->lines[0],"a valid deleteoldrdn attribute should be specified",$this->entry));
}
}
else{
$this->setLdifEntryReaderException(new LdifEntryReaderException($this->_currentLineNumber,$this->lines[0],"a valid newrdn attribute should be specified",$this->entry));
}
$this->entry->attributes = $attrs;
}
}
}
/**
* Exception which can be raised during processing the ldif file
*/
class LdifReaderException{
var $lineNumber;
var $message;
var $currentLine;
/**
* Constructor of the exception
*
* @param int $lineNumber the number of the line where
* the error occured
* @param String currentLine the line wich raised an exception
* @param String the message associated the exception
*/
function LdifReaderException($lineNumber,$currentLine,$message){
$this->lineNumber = $lineNumber;
$this->currentLine =$currentLine;
$this->message = $message;
}
}
/**
* Helper base class to read file.
*/
class FileReader{
//the file pointer
var $_fp;
//the current line number
var $_currentLineNumber;
// the current line
var $_currentLine;
/**
* Constructor of the FileReader class
* @param $path2File
*/
function FileReader($path2File){
$this->_fp = fopen ($path2File, "r");
}
/**
* Returns true if we reached the end of the file.
*
* @return bool true if it's the end of file, false otherwise.
*/
function eof(){
return @feof($this->_fp);
}
/**
* Helper method to switch to the next line
*/
function _nextLine(){
//$nextLine="";
$this->_currentLineNumber++;
$nextLine = fgets($this->_fp, 1024);
$this->_currentLine = $nextLine;
//need to check if it is a very long line (not folded line for example)
while(!ereg("\n|\r\n|\r",$nextLine) && !$this->eof()){
$nextLine = fgets($this->_fp, 1024);
$this->_currentLine.= trim($nextLine);
}
}
/**
* Check if is the current line is a blank line.
*
* @return bool if it is a blank line,false otherwise.
*/
function _isBlankLine(){
return(trim($this->_currentLine)=="")?1:0;
}
/**
* Close the handler
*/
function close(){
return @fclose($this->_fp);
}
}
/**
* Main parser of the ldif file
*/
class LdifReader extends FileReader{
// the current entry
var $_currentEntry;
// array containing the lines of the current entry
var $_currentLines;
// the reader of the entry
var $_ldapEntryHandler;
//boolean flag for error
var $_error;
// warning message. Only use for the verion number
var $_warningMessage;
// TODO What is this variable? Used below, why?
var $_warningVersion;
var $_dnLineNumber;
// continuous mode operation flag
var $continuous_mode;
/**
* Private constructor of the LDIFReader class.
* Marked as private as we need to instantiate the class
* by using other constructor with parameters
* Use to initialize instance members.
*/
function _LdifReader(){
$this->_error=0;
$this->_warning=0;
$this->_currentLineNumber=0;
$this->_currentLines = array();
$this->_warningMessage="";
$this->_warningVersion="";
//need to change this one
$this->_currentEntry = new LdifEntry();
}
/**
* Constructor of the class
*
* @param String $path2File path of the ldif file to read
* @param boolean $continuous_mode 1 if continuous mode operation, 0 otherwise
*/
function LdifReader( $path2File , $continuous_mode = 0 ){
parent::FileReader( $path2File );
$this->continuous_mode = $continuous_mode;
$this->_LdifReader();
}
/**
* Returns the lines that generated the Ldif Entry.
*
* @return String[] The lines from the entry.
*/
function getCurrentLines(){
return $this->_currentLines;
}
/**
* Check if it's a ldif comment line.
*
* @return bool true if it's a comment line,false otherwise
*/
function _isCommentLine(){
return substr(trim($this->_currentLine),0,1)=="#"?1:0;
}
/**
* Check if the current line is a line containing the distinguished
* name of an entry.
*
* @return bool true if the line contains a dn line, false otherwise.
*/
function _isDnLine(){
return ereg("^dn:",$this->_currentLine)?1:0;
}
/**
* Return the current entry object
*
* @return Ldap_Ldif_entry the current ldif entry
*/
function getCurrentEntry(){
return $this->_currentEntry;
}
/**
* Get the lines of the next entry
*
* @return String[] the lines (unfolded) of the next entry
*/
function nextLines(){
$endEntryFound=0;
//free the array (instance member)
unset($this->_currentLines);
// reset the error state
$this->_error = 0;
if( $this->_hasMoreEntries() && !$this->eof() ){
//the first line is the dn one
$this->_currentLines[0]= trim($this->_currentLine);
$count=0;
// while we end on a blank line, fetch the attribute lines
while(!$this->eof() && !$endEntryFound ){
//fetch the next line
$this->_nextLine();
// if the next line begin with a space,we append it to the current row
// else we push it into the array (unwrap)
if(substr($this->_currentLine,0,1)==" "){
$this->_currentLines[$count].=trim($this->_currentLine);
}
elseif(substr($this->_currentLine,0,1)=="#"){
//do nothing
// echo $this->_currentLine;
}
elseif(trim($this->_currentLine)!=""){
$this->_currentLines[++$count]=trim($this->_currentLine);
}
else{
$endEntryFound=1;
}
}//end while
//return the ldif entry array
return $this->_currentLines;
}
else{
return false;
}
}
/**
* Check if the ldif version is present in the ldif
*
* @return true if a version line was found,false otherwise
*/
function hasVersionNumber(){
$ldifLineFound = 0;
while(!$this->eof() && !$ldifLineFound){
// fetch the first line
$this->_nextLine();
// if it's a ldif comment line or a blank line,leave it and continue
if($this->_isCommentLine() || $this->_isBlankLine()){
//debug usage
//echo $this->_currentLineNumber." - " .($this->_isCommentLine()?"comment":"blank line\n")."<br>";
}
elseif(ereg("^version",trim($this->_currentLine))){
$ldifLineFound=1;
$this->_nextLine();
}
else{
$this->_warningVersion=1;
global $lang;
$this->_warningMessage = $lang['warning_no_ldif_version_found'];
$ldifLineFound=1;
}
}// end while loop
return $this->_warningVersion;
}
/**
* Private method to check if there is more entries in the file
*
* @return boolean true if an entry was found, false otherwise.
*/
function _hasMoreEntries(){
$entry_found = 0;
while( !$this->eof() && !$entry_found ){
//if it's a comment or blank line,switch to the next line
if( $this->_isCommentLine() || $this->_isBlankLine() ){
//debug usage
// echo $this->_currentLineNumber." - " .($this->_isCommentLine()?"comment":"blank line\n")."<br>";
$this->_nextLine();
}
else{
$this->_currentDnLine = $this->_currentLine;
$this->dnLineNumber = $this->_currentLineNumber;
$entry_found=1;
}
}
return $entry_found;
}
/**
* Associate the ldif reader with a exception which occurs during
* proceesing the file.
* Easier to do exception if we had to switch to php5
*
* @param Ldap_LdifReaderException $ldifReaderException
*/
function setLdapLdifReaderException($ldifReaderException){
$this->_ldifReaderException= $ldifReaderException;
if( !$this->continuous_mode )
$this->done();
$this->_error=1;
}
/**
* Return the exception raised during processing the file
*
* @return Ldap_ldifReaderException
*/
function getLdapLdifReaderException(){
return $this->_ldifReaderException;
}
/**
* Helper method which return the value
* of the instance member $_error
*
* @return true if an error was encountered, false otherwise
*/
function _ldifHasError(){
return $this->_error;
}
function hasRaisedException(){
return $this->_error;
}
/**
* Close the ldif file
*
* @return void
*/
function done(){
if (!$this->_ldifHasError()){
@fclose($this->_fp);
}
}
/**
* Return a ldif entry object
*
* @return LdifEntry the entry object buid from the lines of the ldif file
*/
function readEntry(){
if($lines = $this->nextLines()){
$ldifEntryReader = new LdifEntryReader($lines);
//fetch entry
$entry = $ldifEntryReader->getEntry();
$this->_currentEntry = $entry;
// if any exception has raised, catch it and throw it to the main reader
if($ldifEntryReader->hasRaisedException()){
$exception = $ldifEntryReader->getLdifEntryReaderException();
$faultyLineNumber = $this->dnLineNumber + $exception->lineNumber - 1;
$this->setLdapLdifReaderException(new LdifReaderException($faultyLineNumber,$exception->currentLine,$exception->message));
if ( ! $this->continuous_mode )
return 0;
}
return $entry;
}
else
return false;
}
function getWarningMessage(){
return $this->_warningMessage;
}
}
/**
* Helper class to write entries into the ldap server
*/
class LdapWriter{
var $ldapConnexion;
var $_writeError=0;
/**
* Constructor
*/
function LdapWriter(&$conn){
$this->ldapConnexion = &$conn;
}
/**
* Add a new entry to the ldap server
*
* @param LdifEntry $entry the entry to add
* @return true in case of success, false otherwise
*/
function ldapAdd($entry){
return @ldap_add($this->ldapConnexion,$entry->dn,$entry->attributes);
}
/**
* Modify an entry
*
* @param LdifEntry $entry the entry to add
* @return true in case of success, false otherwise
*/
function ldapModify($entry){
$changeType = $entry->getChangeType();
switch($changeType){
case "add":
$this->_writeError= $this->ldapAdd($entry);
break;
case "delete":
$this->_writeError = @ldap_delete($this->ldapConnexion,$entry->dn);
break;
case "modrdn":
$atts=$entry->getAttributes();
$this->_writeError = @ldap_rename( $this->ldapConnexion, $entry->dn,$atts['newrdn'], $atts['newsuperior'], $atts['deleteoldrdn'] );
break;
case "moddn":
$atts=$entry->getAttributes();
$this->_writeError = @ldap_rename( $this->ldapConnexion, $entry->dn,$atts['newrdn'], $atts['newsuperior'], $atts['deleteoldrdn'] );
break;
case "modify":
$attributes = $entry->attributes;
$num_attributes = count($attributes);
for($i=0;$i<$num_attributes;$i++)
{
$action = key($attributes[$i]);
array_shift($attributes[$i]);
switch($action){
case "add":
$this->_writeError = @ldap_mod_add($this->ldapConnexion,$entry->dn,$attributes[$i]);
break;
case "delete":
$this->_writeError = @ldap_mod_del($this->ldapConnexion,$entry->dn,$attributes[$i]);
break;
case "replace":
$this->_writeError = @ldap_mod_replace($this->ldapConnexion,$entry->dn,$attributes[$i]);
break;
}
}
}
return $this->_writeError;
}
/**
* Close the connection to the ldap server
*
* @return boolean true in case of success, false otherwise
*/
function ldapClose(){
return @ldap_close($this->ldapConnexion);
}
}
?>
|