entry.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 525 行 · 第 1/2 页

PHP
525
字号
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +--------------------------------------------------------------------------+
// | Net_LDAP                                                                 |
// +--------------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group                                    |
// +--------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or            |
// | modify it under the terms of the GNU Lesser General Public               |
// | License as published by the Free Software Foundation; either             |
// | version 2.1 of the License, or (at your option) any later version.       |
// |                                                                          |
// | This library 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        |
// | Lesser General Public License for more details.                          |
// |                                                                          |
// | You should have received a copy of the GNU Lesser General Public         |
// | License along with this library; if not, write to the Free Software      |
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA |
// +--------------------------------------------------------------------------+
// | Authors: Tarjej Huse                                                     |
// +--------------------------------------------------------------------------+
//
// $Id: Entry.php 4831 2006-02-06 09:59:09Z nbm $

/**
 * This class represents an LDAP entry
 *
 * @package Net_LDAP
 * @author Tarjei Huse
 * @version $Revision: 4831 $
 */
class Net_LDAP_Entry extends PEAR
{
    /**#@+
     * Array of the attributes
     *
     * @access private
     * @var array
     */
    var $_attrs = array();
    
    /**
     * Array of attributes to be deleted upon update()
     */    
    var $_delAttrs = array();

    /**
     * Array of attributes to be modified upon update()
     */    
    var $_modAttrs = array();

    /**
     * Array of attributes to be added upon update()
     */        
    var $_addAttrs = array();
    /**#@-*/
    
    /**
     * The distinguished name of the entry
     * 
     * @access private
     * @var string
     */
    var $_dn = '';
    
    /**
     * LDAP resource link
     * 
     * @access private
     * @var resource
     */
    var $_link = null;
    
    /**
     * Value of old DN if DN has changed
     *
     * @access private
     * @var string
     */
    var $_olddn = '';

    /**#@+
     * Array of errors for debugging class
     *
     * @access private
     */
    var $_error = array();

    /**
     * updatechecks
     */
    var $updateCheck = array('newdn'    => false,
                             'modify'   => false,
                             'newEntry' => true
                             ); // since the entry is not changed before the update();

    /**
     * Net_LDAP_Schema object TO BE REMOVED
     */                             
    var $_schema;
    /**#@-*/
    
    /** Constructor
     *
     * @param - link - ldap_resource_link, dn = string entry dn, attributes - array entry attributes array.
     * @return - none
     **/
    function Net_LDAP_Entry($link = null, $dn = null, $attributes = null)
    {
        if (!is_null($link)) {
            $this->_link = $link;
        }
        if (!is_null($dn)) {
            $this->_set_dn($dn);
        }
        if (is_array($attributes) && count($attributes) > 0) {
            $this->_set_attributes($attributes);
        } else {
            $this->updateCheck['newEntry'] = true;
        }
    }
    
    /** 
     * Set the reasourcelink to the ldapserver.
     *
     * @access private
     * @param resource LDAP link
     */
    function _set_link(&$link) 
    {
        $this->_link = $link;
    }
    
    /**
     * set the entrys DN 
     *
     * @access private
     * @param string
     */
    function _set_dn($dn)
    {
        $this->_dn = $dn;
    }

    /**
     * sets the internal array of the entrys attributes.
     *
     * @access private
     * @param array
     */
    function _set_attributes($attributes= array())
    {
        $this->_attrs = $attributes;
        // this is the sign that the entry exists in the first place: 
        $this->updateCheck['newEntry'] = false;
    }

   /** 
    * removes [count] entries from the array.
    * 
    * remove all the count elements in the array:
    * Used before ldap_modify, ldap_add
    * 
    * @access private
    * @return array Cleaned array of attributes
    */
    function _clean_entry()
    {
        $attributes = array();
        
        for ($i=0; $i < $this->_attrs['count'] ; $i++) {
        
            $attr = $this->_attrs[$i];
        
            if ($this->_attrs[$attr]['count'] == 1) {
                $attributes[$this->_attrs[$i]] = $this->_attrs[$attr][0];
            } else {
                $attributes[$attr] = $this->_attrs[$attr];
                unset ($attributes[ $attr ]['count']);
            }
        }
         
        return $attributes;

    }

   /**
    * returns an assosiative array of all the attributes in the array
    *
    * attributes -  returns an assosiative array of all the attributes in the array
    * of the form array ('attributename'=>'singelvalue' , 'attribute'=>array('multiple','values'))
    *
    * @param none
    * @return array Array of attributes and values.
    */
    function attributes()
    {
        return $this->_clean_entry();
    }

   /**
    * Add one or more attribute to the entry
    *
    * The values given will be added to the values which already exist for the given attributes.
    * usage:
    * $entry->add ( array('sn'=>'huse',objectclass=>array(top,posixAccount)))
    *
    * @param array Array of attributes
    * @return mixed Net_Ldap_Error if error, else true.
    */
    function add($attr = array())
    {
        if (!isset($this->_attrs['count'])) {
            $this->_attrs['count'] = 0;
        }
        if (!is_array($attr)) {
            return $this->raiseError("Net_LDAP::add : the parameter supplied is not an array, $attr", 1000);   
        }
        /* if you passed an empty array, that is your problem! */
        if (count ($attr)==0) {
            return true;        
        }
        foreach ($attr as $k => $v ) {
            // empty entrys should not be added to the entry.
            if ($v == '') {
                continue;
            }

            if ($this->exists($k)) {
                if (!is_array($this->_attrs[$k])) {
                    return $this->raiseError("Possible malformed array as parameter to Net_LDAP::add().");
                }
                array_push($this->_attrs[$k],$v);
                $this->_attrs[$k]['count']++;
            } else {
                $this->_attrs[$k][0] = $v;
                $this->_attrs[$k]['count'] = 1;
                $this->_attrs[$this->_attrs['count']] = $k;
                $this->_attrs['count']++;
            }
            // Fix for bug #952
            if (empty($this->_addAttrs[$k])) {
                $this->_addAttrs[$k] = array();
            }
            if (false == is_array($v)) {
                $v = array($v);
            }
            foreach ($v as $value) {
                array_push($this->_addAttrs[$k], $value);
            }
        }
        return true;
    }

   /**
    * Set or get the DN for the object
    *
    * If a new dn is supplied, this will move the object when running $obj->update();
    *
    * @param string DN
    */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?