Location: PHPKode > projects > LDAPted > ldapted/includes/Config.php
<?php
/***************************************************************************
 *
 *                                  Config.php
 *                              -------------------
 *
 *   begin                : Friday, Jul 5, 2002
 *   copyright            : (C) 2002 The Kabramps Team
 *   email                : hide@address.com,
 *                          hide@address.com
 *
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 *   See the GNU General Public License for more details.
 *   (http://www.gnu.org/licenses/gpl.html)
 *
 ***************************************************************************/
include_once('includes/XMLDocument.php');

class Config extends XMLDocument {

  var $dn;
  var $ldap;
  var $ldapbase;

  function Config($filename) {
    $this->init($filename);
  }
  
  function get_options() {
    return
      $this->get_xml_content_by_key("/ldapted/options/option","name");
  }

  function get_skins() {
    return
      $this->get_xml_content_by_key("/ldapted/skins/skin","dir");
  }
	function get_languages()
	{
  		$return = $this->get_xml_content_by_key('/ldapted/languages/language', 'id');
		return $return;
	}
  
  function get_hosts() {
    return 
      $this->get_xml_attributes("/ldapted/hosts/host","name");
  }
  
  function get_host_options($host) {
    return
      $this->get_xml_content_by_key("/ldapted/hosts/host[@name=\"".$host."\"]/options/option","name");
  }

  function get_host_ldapbase($host) {
    $return =
      $this->get_xml_content("/ldapted/hosts/host[@name=\"".$host."\"]/options/option[@name='ldapbase']",0,1);
    return $return[0];
  }
  function get_host_form_path($host) {
    $return =
      $this->get_xml_attributes("/ldapted/hosts/host[@name=\"".$host."\"]/forms","path");
    return $return[0];
  }

  function get_host_form_file($host, $name) {
    $return =
      $this->get_xml_attributes("/ldapted/hosts/host[@name=\"".$host."\"]/forms/form[@name=\"".$name."\"]","file");
    return $return[0]; 
  }

  function get_host_form_base($host, $name ) {
    $return =
      $this->get_xml_attributes("/ldapted/hosts/host[@name=\"".$host."\"]/forms/form[@name=\"".$name."\"]","base");
    return $return[0]; 
  }

  function get_host_form_filter($host, $name ) {
    $return =
      $this->get_xml_attributes("/ldapted/hosts/host[@name=\"".$host."\"]/forms/form[@name=\"".$name."\"]","filter");
    return $return[0]; 
  }

  function get_host_form_emptyfilter($host, $name ) {
    $return =
      $this->get_xml_attributes("/ldapted/hosts/host[@name=\"".$host."\"]/forms/form[@name=\"".$name."\"]","emptyfilter");
    return $return[0]; 
  }

  function get_default_language() {
    $return =
      $this->get_xml_content("/ldapted/options/option[@name=\"language\"]",0,1);
    return $return[0];
  }
  
  function get_host_form_description($host, $name, $lang=null)
  {
    if ( ! $lang ) {
      $lang=$this->get_default_language();
    }
    $return = $this->get_xml_content("/ldapted/hosts/host[@name=\"".$host."\"]/forms/form[@name=\"".$name."\"]/label[@lang=\"".$lang."\"]",0,1);
    return $return[0];
  }

  function get_host_form_ids($host,$id) {
    return
      $this->get_xml_attributes("/ldapted/hosts/host[@name=\"".$host."\"]/acl/entry[".$id."]/forms/form","refname");
  }

  function get_host_entry_count($host) {
    return
      count( $this->get_xml_tags("/ldapted/hosts/host[@name=\"".$host."\"]/acl/entry[*]") );
  }


	function get_host_lists( $host )
	{
		$path = $this->get_xml_attributes("/ldapted/hosts/host[@name=\"".$host."\"]/lists",'path');
		$file = $this->get_xml_attributes("/ldapted/hosts/host[@name=\"".$host."\"]/lists/list",'file');
		return $path[0].$file[0];
	}
  /**
   *
   * @param string $host Name of the connected LDAP host
   * @param string $dn The BindDN the user provided in the login
   * @return array $forms all valid forms for the user with the given BindDN
   *
   */
	function get_host_forms($host, $dn)
	{
		global $language;
		
		$return = array();
		$this->ldapbase = $this->get_host_ldapbase( $host );
		$this->dn = $dn;

		$entries = $this->get_host_entry_count( $host ); //acl entries
		for ( $i = 1; $i <= $entries; $i++ )
		{
			// looks if a rule of an entry matchs with the given $dn
			if ( $this->entry_match("/ldapted/hosts/host[@name=\"".$host."\"]/acl/entry[".$i."]/conditions") )
			{
				$forms = $this->get_host_form_ids( $host, $i);

				foreach( $forms as $form )
				{
					if ( ! in_array( $form, $return ) )
					{
						$label = $this->get_host_form_description($host, $form, $language );
						$return[$form] = $label;
					}
				}
			}
		}
		return $return;
	}

  /**
   *
   * @param string $xpath
   * @param string $operator Operators like '&','|' and '!'
   *
   */
  function entry_match($xpath,$operator="&") {
    $tag = $this->get_xml_tags($xpath."/*");
    $exp = array();
    for ( $i=0;$i<count( $tag );$i++ ) {
      switch ( $tag[$i] ) {
	case "not":
	  $exp[] = $this->entry_match($xpath."/".$tag[$i],"!");
	  break;
	case "or":
	  $exp[] = $this->entry_match($xpath."/".$tag[$i],"|");
	  break;
	case "and":
	  $exp[] = $this->entry_match($xpath."/".$tag[$i],"&");
	  break;
	case "condition":
	  $n = $i+1;
	  //$dn = ereg_replace(",".$this->ldapbase."$","",$this->dn);
	  $dn = $this->dn;
	  $compare = $this->get_xml_attributes($xpath."/*[".$n."]","compare");
	  $compare = $compare[0];
	  switch ( $compare ) {
	  case "contains":
	    $attribute = $this->get_xml_attributes($xpath."/*[".$n."]","attribute");
	    $value = $this->get_xml_attributes($xpath."/*[".$n."]","value");
	    if ( strtolower($attribute[0]) == "dn" ) {
	      $exp[] = ereg($value[0],$dn );
	    } else {
	      $exp[] = $this->ldap_ereg($this->dn,$attribute[0],$value[0]);
	    }
	    break;
	  case "equals":
	    $attribute = $this->get_xml_attributes($xpath."/*[".$n."]","attribute");
	    $value = $this->get_xml_attributes($xpath."/*[".$n."]","value");
	    if ( strtolower($attribute[0]) == "dn" ) {
	      // I guess this should result in true or false ;-)
	      //$exp[] = ereg("^".$value[0]."$",$dn );
	      // This line wasn't working so I added these:
	      if ($dn == $value[0]) {
	      	$exp[] = true;
	      } else {
	      	$exp[] = false;
	      }
	    } else {
	      $exp[] = $this->ldap_ereg($this->dn,$attribute[0],"^".$value[0]."$");
	    }
	    break;
	  case "regularexp":
	    $attribute = $this->get_xml_attributes($xpath."/*[".$n."]","attribute");
	    $value = $this->get_xml_attributes($xpath."/*[".$n."]","value");
	    if ( strtolower($attribute[0]) == "dn" ) {
	      $exp[] = ereg($value[0],$dn );
	    } else {
	      $exp[] = $this->ldap_ereg($this->dn,$attribute[0],$value[0]);
	    }
	    break;
	  case "ends":
	    $attribute = $this->get_xml_attributes($xpath."/*[".$n."]","attribute");
	    $value = $this->get_xml_attributes($xpath."/*[".$n."]","value");
	    if ( strtolower($attribute[0]) == "dn" ) {
	      $exp[] = ereg($value[0]."$",$dn );
	    } else {
	      $exp[] = $this->ldap_ereg($this->dn,$attribute[0],$value[0]."$");
	    }
	    break;
	  case "join":
	    $attribute = $this->get_xml_attributes($xpath."/*[".$n."]","attribute");
	    $attribute = strtolower($attribute[0]);
	    @$value = $this->ldap->search($this->dn,array($attribute),null,1);
	    $joindn = $this->get_xml_attributes($xpath."/*[".$n."]","joindn");
	    $joinattribute = $this->get_xml_attributes($xpath."/*[".$n."]","joinattribute");
	    if( $this->ldap_ereg($joindn[0].",".$this->ldapbase,$joinattribute[0],$value[0][$attribute][0]) ) {
	      $exp[] = true;
	    } else {
	      $exp[] = false;
	    }
	    break;
	  }
      }
    }
    if ( count($exp) > 0 ) {
      $return = $exp[0];
    } else {
      $return = true;
    }
    for ( $i=1; $i < count($exp); $i++ ) {
      switch ( $operator ) {
      case "!":
	if ( ! ( ( ! $return ) && $exp[$i] ) ) {
	  $return = true;
	} else {
	  $return = false;
	}
	break;
      case "&":
	if ( $return && $exp[$i] ) {
	  $return = true;
	} else {
	  $return = false;
	}
	break;
      case "|":
	if ( $return || $exp[$i] ) {
	  $return = true;
	} else {
	  $return = false;
	}
	break;
      }
    }
    return $return;
  }

  function ldap_ereg($dn,$attribute,$value) {
    $attribute = strtolower($attribute);
    @$result = $this->ldap->search($dn,array($attribute),null,1);
    if ( $result[0] != null ) {
      $key = array_keys($result[0]);
      for ( $j=0; $j<count($result[0][$attribute]); $j++ ) {
	if ( $value != "" ) {
	  if ( ereg($value,$result[0][$attribute][$j]) ) {
	    return true;
	  }
	}
      }
    }
    return false;
  }
}


?>
Return current item: LDAPted