entry.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 525 行 · 第 1/2 页
PHP
525 行
function dn($newdn = '')
{
if ($newdn == '') {
return $this->_dn;
}
$this->_olddn = $this->_dn;
$this->_dn = $newdn;
$this->updateCheck['newdn'] = true;
}
/**
* check if a certain attribute exists in the directory
*
* @param string attribute name.
* @return boolean
*/
function exists($attr)
{
if (array_key_exists($attr, $this->_attrs)) {
return true;
}
return false;
}
/**
* get_value get the values for a attribute
*
* returns either an array or a string
* possible values for option:
* alloptions - returns an array with the values + a countfield.
* i.e.: array (count=>1, 'sn'=>'huse');
* single - returns the, first value in the array as a string.
*
* @param $attr string attribute name
* @param $options array
*/
function get_value($attr = '', $options = '')
{
if (array_key_exists($attr, $this->_attrs)) {
if ($options == 'single') {
if (is_array($this->_attrs[$attr])) {
return $this->_attrs[$attr][0];
} else {
return $this->_attrs[$attr];
}
}
$value = $this->_attrs[$attr];
if (!$options == 'alloptions') {
unset ($value['count']);
}
return $value;
} else {
return '';
}
}
/**
* add/delete/modify attributes
*
* this function tries to do all the things that replace(),delete() and add() does on an object.
* Syntax:
* array ( 'attribute' => newval, 'delattribute' => '', newattrivute => newval);
* Note: You cannot use this function to modify parts of an attribute. You must modify the whole attribute.
* You may call the function many times before running $entry->update();
*
* @param array attributes to be modified
* @return mixed errorObject if failure, true if success.
*/
function modify($attrs = array()) {
if (!is_array($attrs) || count ($attrs) < 1 ) {
return $this->raiseError("You did not supply an array as expected",1000);
}
foreach ($attrs as $k => $v) {
// empty values are deleted (ldap v3 handling is in update() )
if ($v == '' && $this->exists($k)) {
$this->_delAttrs[$k] = '';
continue;
}
/* existing attributes are modified*/
if ($this->exists($k) ) {
if (is_array($v)) {
$this->_modAttrs[$k] = $v;
} else {
$this->_modAttrs[$k][0] = $v;
}
} else {
/* new ones are created */
if (is_array($v) ) {
// an empty array is deleted...
if (count($v) == 0 ) {
$this->_delAttrs[$k] = '';
} else {
$this->_addAttrs[$k] = $v;
}
} else {
// dont't add empty attributes
if ($v != null) $this->_addAttrs[$k][0] = $v;
}
}
}
return true;
}
/**
* replace a certain attributes value
*
* replace - replace a certain attributes value
* example:
* $entry->replace(array('uid'=>array('tarjei')));
*
* @param array attributes to be replaced
* @return mixed error if failure, true if sucess.
*/
function replace($attrs = array() )
{
foreach ($attrs as $k => $v) {
if ($this->exists($k)) {
if (is_array($v)) {
$this->_attrs[$k] = $v;
$this->_attrs[$k]['count'] = count($v);
$this->_modAttrs[$k] = $v;
} else {
$this->_attrs[$k]['count'] = 1;
$this->_attrs[$k][0] = $v;
$this->_modAttrs[$k][0] = $v;
}
} else {
return $this->raiseError("Attribute $k does not exist",16); // 16 = no such attribute exists.
}
}
return true;
}
/**
* delete attributes
*
* Use this function to delete certain attributes from an object.
*
* @param - array of attributes to be deleted
* @return mixed Net_Ldap_Error if failure, true if success.
*/
function delete($attrs = array())
{
foreach ($attrs as $k => $v) {
if ($this->exists ($k)) {
// if v is a null, then remove the whole attribute, else only the value.
if ($v == '') {
unset($this->_attrs[$k]);
$this->_delAttrs[$k] = "";
// else we remove only the correct value.
} else {
for ($i = 0;$i< $this->_attrs[$k]['count'];$i++) {
if ($this->_attrs[$k][$i] == $v ) {
unset ($this->_attrs[$k][$i]);
$this->_delAttrs[$k] = $v;
continue;
}
}
}
} else {
$this->raiseError("You tried to delete a nonexisting attribute!",16);
}
}
return true;
}
/**
* update the Entry in LDAP
*
* After modifying an object, you must run update() to
* make the updates on the ldap server. Before that, they only exists in the object.
*
* @param object Net_LDAP
* @return mixed Net_LDAP_Error object on failure or true on success
*/
function update ($ldapObject = null)
{
if ($ldapObject == null && $this->_link == null ) {
$this->raiseError("No link to database");
}
if ($ldapObject != null) {
$this->_link =& $ldapObject->_link;
}
//if it's a new
if ($this->updateCheck['newdn'] && !$this->updateCheck['newEntry']) {
if (@ldap_get_option( $this->_link, LDAP_OPT_PROTOCOL_VERSION, $version) && $version != 3) {
return $this->raiseError("Moving or renaming an dn is only supported in LDAP V3!", 80);
}
$newparent = ldap_explode_dn($this->_dn, 0);
unset($newparent['count']);
$relativeDn = array_shift($newparent);
$newparent = join(',', $newparent);
if (!@ldap_rename($this->_link, $this->_olddn, $relativeDn, $newparent, true)) {
return $this->raiseError("DN not renamed: " . ldap_error($this->_link), ldap_errno($this->_link));
}
}
if ($this->updateCheck['newEntry']) {
//print "<br>"; print_r($this->_clean_entry());
if (!@ldap_add($this->_link, $this->dn(), $this->_clean_entry()) ) {
return $this->raiseError("Entry" . $this->dn() . " not added!" .
ldap_error($this->_link), ldap_errno($this->_link));
} else {
return true;
}
// update existing entry
} else {
$this->_error['first'] = $this->_modAttrs;
$this->_error['count'] = count($this->_modAttrs);
// modified attributes
if (( count($this->_modAttrs)>0) &&
!ldap_modify($this->_link, $this->dn(), $this->_modAttrs))
{
return $this->raiseError("Entry " . $this->dn() . " not modified(attribs not modified): " .
ldap_error($this->_link),ldap_errno($this->_link));
}
// attributes to be deleted
if (( count($this->_delAttrs) > 0 ))
{
// in ldap v3 we need to supply the old attribute values for deleting
if (@ldap_get_option( $this->_link, LDAP_OPT_PROTOCOL_VERSION, $version) && $version == 3) {
foreach ( $this->_delAttrs as $k => $v ) {
if ( $v == '' && $this->exists($k) ) {
$this->_delAttrs[$k] = $this->get_value( $k );
}
}
}
if ( !ldap_mod_del($this->_link, $this->dn(), $this->_delAttrs) ) {
return $this->raiseError("Entry " . $this->dn() . " not modified (attributes not deleted): " .
ldap_error($this->_link),ldap_errno($this->_link));
}
}
// new attributes
if ((count($this->_addAttrs)) > 0 && !ldap_modify($this->_link, $this->dn(), $this->_addAttrs)) {
return $this->raiseError( "Entry " . $this->dn() . " not modified (attributes not added): " .
ldap_error($this->_link),ldap_errno($this->_link));
}
return true;
}
}
}
?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?