phpldapadmin/new_attr.php

68 lines
2.0 KiB
PHP
Raw Normal View History

2009-06-30 08:05:37 +00:00
<?php
/*
* new_attr.php
* Adds an attribute/value pair to an object
*
* Variables that come in as POST vars:
2009-06-30 08:09:20 +00:00
* - dn
2009-06-30 08:05:37 +00:00
* - server_id
* - attr
* - val
2009-06-30 08:07:14 +00:00
* - binary
2009-06-30 08:05:37 +00:00
*/
2009-06-30 08:07:14 +00:00
require 'common.php';
2009-06-30 08:05:37 +00:00
2009-06-30 08:09:20 +00:00
2009-06-30 08:05:37 +00:00
$server_id = $_POST['server_id'];
2009-06-30 08:07:14 +00:00
$attr = $_POST['attr'];
2009-06-30 08:09:20 +00:00
$val = isset( $_POST['val'] ) ? $_POST['val'] : false;;
2009-06-30 08:05:37 +00:00
$val = utf8_encode( $val );
2009-06-30 08:09:20 +00:00
$dn = $_POST['dn'] ;
2009-06-30 08:05:37 +00:00
$encoded_dn = rawurlencode( $dn );
$encoded_attr = rawurlencode( $attr );
2009-06-30 08:07:14 +00:00
$is_binary_val = isset( $_POST['binary'] ) ? true : false;
if( ! $is_binary_val && $val == "" ) {
pla_error( "You left the attribute value blank. Please go back and try again." );
}
if( is_server_read_only( $server_id ) )
pla_error( "You cannot perform updates while server is in read-only mode" );
2009-06-30 08:05:37 +00:00
check_server_id( $server_id ) or pla_error( "Bad server_id: " . htmlspecialchars( $server_id ) );
have_auth_info( $server_id ) or pla_error( "Not enough information to login to server. Please check your configuration." );
2009-06-30 08:07:14 +00:00
// special case for binary attributes (like jpegPhoto and userCertificate):
// we must go read the data from the file and override $val with the binary data
if( $is_binary_val ) {
$file = $_FILES['val']['tmp_name'];
2009-06-30 08:05:37 +00:00
$f = fopen( $file, 'r' );
2009-06-30 08:07:14 +00:00
$binary_data = fread( $f, filesize( $file ) );
2009-06-30 08:05:37 +00:00
fclose( $f );
2009-06-30 08:07:14 +00:00
$val = $binary_data;
2009-06-30 08:05:37 +00:00
}
2009-06-30 08:07:14 +00:00
// Automagically hash new userPassword attributes according to the
// chosen in config.php.
if( 0 == strcasecmp( $attr, 'userpassword' ) )
{
2009-06-30 08:09:20 +00:00
if( isset( $servers[$server_id]['default_hash'] ) &&
$servers[$server_id]['default_hash'] != '' )
{
2009-06-30 08:07:14 +00:00
$enc_type = $servers[$server_id]['default_hash'];
2009-06-30 08:09:20 +00:00
$val = password_hash( $val, $enc_type );
2009-06-30 08:07:14 +00:00
}
}
2009-06-30 08:05:37 +00:00
$ds = pla_ldap_connect( $server_id ) or pla_error( "Could not connect to LDAP server" );
$new_entry = array( $attr => $val );
$result = @ldap_mod_add( $ds, $dn, $new_entry );
if( $result )
2009-06-30 08:09:20 +00:00
header( "Location: edit.php?server_id=$server_id&dn=$encoded_dn&modified_attrs[]=$encoded_attr" );
2009-06-30 08:05:37 +00:00
else
pla_error( "Failed to add the attribute.", ldap_error( $ds ) , ldap_errno( $ds ) );