2009-06-30 08:09:20 +00:00
|
|
|
<?php
|
2009-06-30 11:52:55 +00:00
|
|
|
// $Header: /cvsroot/phpldapadmin/phpldapadmin/htdocs/copy.php,v 1.44.2.3 2008/12/12 12:20:22 wurley Exp $
|
2009-06-30 09:22:30 +00:00
|
|
|
|
2009-06-30 09:29:51 +00:00
|
|
|
/**
|
2009-06-30 08:05:37 +00:00
|
|
|
* Copies a given object to create a new one.
|
|
|
|
*
|
|
|
|
* Vars that come in as POST vars
|
2009-06-30 10:46:00 +00:00
|
|
|
* - source_dn (rawurlencoded)
|
|
|
|
* - new_dn (form element)
|
|
|
|
* - server_id
|
2009-06-30 09:29:51 +00:00
|
|
|
*
|
|
|
|
* @package phpLDAPadmin
|
|
|
|
*/
|
|
|
|
/**
|
2009-06-30 08:05:37 +00:00
|
|
|
*/
|
|
|
|
|
2009-06-30 09:29:51 +00:00
|
|
|
require './common.php';
|
2009-06-30 08:05:37 +00:00
|
|
|
|
2009-06-30 11:46:44 +00:00
|
|
|
if (! $_SESSION[APPCONFIG]->isCommandAvailable('entry_move'))
|
2009-06-30 11:52:55 +00:00
|
|
|
error(sprintf('%s%s %s',_('This operation is not permitted by the configuration'),_(':'),_('copy entry')),'error','index.php');
|
2009-06-30 08:07:14 +00:00
|
|
|
|
2009-06-30 11:52:55 +00:00
|
|
|
$entry = array();
|
2009-06-30 10:46:00 +00:00
|
|
|
$entry['src']['id'] = get_request('server_id');
|
|
|
|
$entry['dst']['id'] = get_request('dest_server_id');
|
2009-06-30 08:05:37 +00:00
|
|
|
|
2009-06-30 11:46:44 +00:00
|
|
|
$entry['src']['ldapserver'] = $_SESSION[APPCONFIG]->ldapservers->Instance($entry['src']['id']);
|
|
|
|
$entry['dst']['ldapserver'] = $_SESSION[APPCONFIG]->ldapservers->Instance($entry['dst']['id']);
|
2009-06-30 10:46:00 +00:00
|
|
|
|
|
|
|
if ($entry['dst']['ldapserver']->isReadOnly())
|
2009-06-30 11:52:55 +00:00
|
|
|
error(_('Destination server is currently READ-ONLY.'),'error','index.php');
|
2009-06-30 09:29:51 +00:00
|
|
|
|
2009-06-30 10:46:00 +00:00
|
|
|
if (! $entry['src']['ldapserver']->haveAuthInfo() || ! $entry['dst']['ldapserver']->haveAuthInfo())
|
2009-06-30 11:52:55 +00:00
|
|
|
error(_('Not enough information to login to server. Please check your configuration.'),'error','index.php');
|
2009-06-30 09:29:51 +00:00
|
|
|
|
2009-06-30 10:46:00 +00:00
|
|
|
$entry['src']['dn'] = get_request('old_dn');
|
|
|
|
$entry['dst']['dn'] = get_request('new_dn');
|
|
|
|
$entry['src']['recursive'] = (get_request('recursive') == 'on') ? true : false;
|
|
|
|
$entry['src']['remove'] = (get_request('remove') == 'yes') ? true : false;
|
2009-06-30 08:05:37 +00:00
|
|
|
|
2009-06-30 09:29:51 +00:00
|
|
|
# Error checking
|
2009-06-30 10:46:00 +00:00
|
|
|
if (strlen(trim($entry['dst']['dn'])) == 0)
|
2009-06-30 11:52:55 +00:00
|
|
|
error(_('You left the destination DN blank.'),'error','index.php');
|
2009-06-30 09:29:51 +00:00
|
|
|
|
2009-06-30 10:46:00 +00:00
|
|
|
if (pla_compare_dns($entry['src']['dn'],$entry['dst']['dn']) == 0 && $entry['src']['id'] == $entry['dst']['id'])
|
2009-06-30 11:52:55 +00:00
|
|
|
error(_('The source and destination DN are the same.'),'error','index.php');
|
2009-06-30 09:29:51 +00:00
|
|
|
|
2009-06-30 10:46:00 +00:00
|
|
|
if ($entry['dst']['ldapserver']->dnExists($entry['dst']['dn']))
|
2009-06-30 11:52:55 +00:00
|
|
|
error(sprintf(_('The destination entry (%s) already exists.'),pretty_print_dn($entry['dst']['dn'])),'error','index.php');
|
2009-06-30 09:29:51 +00:00
|
|
|
|
2009-06-30 10:46:00 +00:00
|
|
|
if (! $entry['dst']['ldapserver']->dnExists(get_container($entry['dst']['dn'])))
|
2009-06-30 11:52:55 +00:00
|
|
|
error(sprintf(_('The destination container (%s) does not exist.'),pretty_print_dn(get_container($entry['dst']['dn']))),'error','index.php');
|
2009-06-30 09:29:51 +00:00
|
|
|
|
2009-06-30 10:46:00 +00:00
|
|
|
if ($entry['src']['recursive']) {
|
2009-06-30 11:52:55 +00:00
|
|
|
$filter = get_request('filter','POST',false,'(objectClass=*)');
|
2009-06-30 09:29:51 +00:00
|
|
|
|
|
|
|
# Build a tree similar to that of the tree browser to give to r_copy_dn
|
2009-06-30 08:05:37 +00:00
|
|
|
$snapshot_tree = array();
|
2009-06-30 10:46:00 +00:00
|
|
|
printf('<h3 class="title">%s%s</h3>',_('Copying '),htmlspecialchars($entry['src']['dn']));
|
2009-06-30 10:26:08 +00:00
|
|
|
printf('<h3 class="subtitle">%s</h3>',_('Recursive copy progress'));
|
2009-06-30 09:29:51 +00:00
|
|
|
print '<br /><br />';
|
|
|
|
print '<small>';
|
2009-06-30 10:26:08 +00:00
|
|
|
print _('Building snapshot of tree to copy... ');
|
2009-06-30 09:29:51 +00:00
|
|
|
|
2009-06-30 10:46:00 +00:00
|
|
|
$snapshot_tree = build_tree($entry['src']['ldapserver'],$entry['src']['dn'],array(),$filter);
|
2009-06-30 10:26:08 +00:00
|
|
|
printf('<span style="color:green">%s</span><br />',_('Success'));
|
2009-06-30 08:09:20 +00:00
|
|
|
|
2009-06-30 09:29:51 +00:00
|
|
|
# Prevent script from bailing early on a long delete
|
|
|
|
@set_time_limit(0);
|
|
|
|
|
2009-06-30 10:46:00 +00:00
|
|
|
$copy_result = r_copy_dn($entry['src']['ldapserver'],$entry['dst']['ldapserver'],$snapshot_tree,$entry['src']['dn'],$entry['dst']['dn']);
|
|
|
|
# @todo: This is not showing the complete results - only the children of the dst - need to look at.
|
|
|
|
$copy_message = $copy_result;
|
2009-06-30 09:29:51 +00:00
|
|
|
print '</small>';
|
2009-06-30 08:09:20 +00:00
|
|
|
|
2009-06-30 08:05:37 +00:00
|
|
|
} else {
|
2009-06-30 10:46:00 +00:00
|
|
|
$copy_result = copy_dn($entry['src']['ldapserver'],$entry['dst']['ldapserver'],$entry['src']['dn'],$entry['dst']['dn']);
|
|
|
|
$copy_message = sprintf('%s DN%s <b>%s</b> %s',_('Copy successful!'),_(':'),htmlspecialchars($entry['dst']['dn']),_('has been created.'));
|
2009-06-30 08:05:37 +00:00
|
|
|
}
|
|
|
|
|
2009-06-30 09:29:51 +00:00
|
|
|
if ($copy_result) {
|
2009-06-30 10:46:00 +00:00
|
|
|
$redirect_url = sprintf('cmd.php?cmd=template_engine&server_id=%s&dn=%s',$entry['dst']['id'],rawurlencode($entry['dst']['dn']));
|
|
|
|
$new_rdn = get_rdn($entry['dst']['dn']);
|
|
|
|
$container = get_container($entry['dst']['dn']);
|
|
|
|
|
|
|
|
if ($entry['src']['remove'])
|
|
|
|
$redirect_url = sprintf('cmd.php?cmd=delete_form&server_id=%s&dn=%s',$entry['src']['id'],rawurlencode($entry['src']['dn']));
|
|
|
|
|
|
|
|
system_message(array(
|
|
|
|
'title'=>_('Copy Entry'),
|
|
|
|
'body'=>$copy_message,
|
|
|
|
'type'=>'info'),
|
|
|
|
$redirect_url);
|
2009-06-30 08:05:37 +00:00
|
|
|
}
|
|
|
|
|
2009-06-30 10:26:08 +00:00
|
|
|
function r_copy_dn($ldapserver_src,$ldapserver_dst,$snapshottree,$root_dn,$dn_dst) {
|
2009-06-30 10:46:00 +00:00
|
|
|
if (DEBUG_ENABLED)
|
|
|
|
debug_log('Entered with (%s,%s,%s,%s,%s)',1,__FILE__,__LINE__,__METHOD__,
|
2009-06-30 10:26:08 +00:00
|
|
|
$ldapserver_src->server_id,$ldapserver_dst->server_id,$snapshottree,$root_dn,$dn_dst);
|
2009-06-30 09:29:51 +00:00
|
|
|
|
2009-06-30 10:46:00 +00:00
|
|
|
$copy_message = array();
|
2009-06-30 08:09:20 +00:00
|
|
|
|
2009-06-30 09:29:51 +00:00
|
|
|
$copy_result = copy_dn($ldapserver_src,$ldapserver_dst,$root_dn,$dn_dst);
|
|
|
|
|
|
|
|
if (! $copy_result)
|
2009-06-30 08:05:37 +00:00
|
|
|
return false;
|
|
|
|
|
2009-06-30 10:46:00 +00:00
|
|
|
$copy_message[] = sprintf('%s DN: <b>%s</b> %s',_('Copy successful!'),htmlspecialchars($dn_dst),_('has been created.'));
|
2009-06-30 08:05:37 +00:00
|
|
|
|
2009-06-30 10:26:08 +00:00
|
|
|
$children = isset($snapshottree[$root_dn]) ? $snapshottree[$root_dn] : null;
|
2009-06-30 09:29:51 +00:00
|
|
|
if (is_array($children) && count($children) > 0) {
|
|
|
|
foreach($children as $child_dn) {
|
|
|
|
$child_rdn = get_rdn($child_dn);
|
|
|
|
$new_dest_dn = sprintf('%s,%s',$child_rdn,$dn_dst);
|
2009-06-30 10:46:00 +00:00
|
|
|
$copy_result = r_copy_dn($ldapserver_src,$ldapserver_dst,$snapshottree,$child_dn,$new_dest_dn);
|
|
|
|
$copy_message[] = array_shift($copy_result);
|
2009-06-30 08:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-30 10:46:00 +00:00
|
|
|
return $copy_message;
|
2009-06-30 08:05:37 +00:00
|
|
|
}
|
|
|
|
|
2009-06-30 09:29:51 +00:00
|
|
|
function copy_dn($ldapserver_src,$ldapserver_dst,$dn_src,$dn_dst) {
|
2009-06-30 10:46:00 +00:00
|
|
|
if (DEBUG_ENABLED)
|
|
|
|
debug_log('Entered with (%s,%s,%s,%s)',17,__FILE__,__LINE__,__METHOD__,
|
2009-06-30 09:40:37 +00:00
|
|
|
$ldapserver_src->server_id,$ldapserver_dst->server_id,$dn_src,$dn_dst);
|
2009-06-30 09:29:51 +00:00
|
|
|
|
2009-06-30 10:26:08 +00:00
|
|
|
$new_entry = $ldapserver_src->getDNAttrs($dn_src);
|
2009-06-30 09:29:51 +00:00
|
|
|
|
|
|
|
# modify the prefix-value (ie "bob" in cn=bob) to match the destination DN's value.
|
|
|
|
$rdn_attr = substr($dn_dst,0,strpos($dn_dst,'='));
|
|
|
|
$rdn_value = get_rdn($dn_dst);
|
|
|
|
$rdn_value = substr($rdn_value,strpos($rdn_value,'=') + 1);
|
|
|
|
$new_entry[$rdn_attr] = $rdn_value;
|
|
|
|
|
|
|
|
# don't need a dn attribute in the new entry
|
|
|
|
unset($new_entry['dn']);
|
|
|
|
|
|
|
|
# Check the user-defined custom call back first
|
2009-06-30 10:26:08 +00:00
|
|
|
if (run_hook('pre_entry_create',
|
2009-06-30 09:29:51 +00:00
|
|
|
array ('server_id'=>$ldapserver_dst->server_id,'dn'=>$dn_dst,'attrs'=>$new_entry))) {
|
|
|
|
|
2009-06-30 10:26:08 +00:00
|
|
|
$add_result = $ldapserver_dst->add($dn_dst,$new_entry);
|
2009-06-30 09:29:51 +00:00
|
|
|
if (! $add_result) {
|
2009-06-30 10:26:08 +00:00
|
|
|
echo '</small><br /><br />';
|
2009-06-30 11:52:55 +00:00
|
|
|
system_message(array(
|
|
|
|
'title'=>_('Failed to copy DN.').sprintf(' (%s)',$dn_dst),
|
|
|
|
'body'=>ldap_error_msg($ldapserver->error(),$ldapserver->errno()),
|
|
|
|
'type'=>'error'));
|
|
|
|
|
2009-06-30 10:46:00 +00:00
|
|
|
} else {
|
|
|
|
run_hook('post_entry_create',
|
|
|
|
array('server_id'=>$ldapserver_dst->server_id,'dn'=>$dn_dst,'attrs'=>$new_entry));
|
2009-06-30 09:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $add_result;
|
|
|
|
|
2009-06-30 08:09:20 +00:00
|
|
|
} else {
|
2009-06-30 09:29:51 +00:00
|
|
|
return false;
|
2009-06-30 08:09:20 +00:00
|
|
|
}
|
2009-06-30 08:05:37 +00:00
|
|
|
}
|
|
|
|
|
2009-06-30 09:29:51 +00:00
|
|
|
/**
|
|
|
|
* @param object $ldapserver
|
|
|
|
* @param dn $dn
|
|
|
|
* @param array $tree
|
|
|
|
* @param string $filter
|
|
|
|
*/
|
2009-06-30 10:26:08 +00:00
|
|
|
function build_tree($ldapserver,$dn,$buildtree) {
|
|
|
|
if (DEBUG_ENABLED)
|
2009-06-30 10:46:00 +00:00
|
|
|
debug_log('Entered with (%s,%s,%s)',1,__FILE__,__LINE__,__METHOD__,
|
2009-06-30 10:26:08 +00:00
|
|
|
$ldapserver->server_id,$dn,$buildtree);
|
|
|
|
|
2009-06-30 10:41:18 +00:00
|
|
|
# we search all children, not only the visible children in the tree
|
2009-06-30 10:26:08 +00:00
|
|
|
$children = $ldapserver->getContainerContents($dn,0);
|
2009-06-30 09:29:51 +00:00
|
|
|
|
|
|
|
if (is_array($children) && count($children) > 0) {
|
2009-06-30 10:26:08 +00:00
|
|
|
$buildtree[$dn] = $children;
|
2009-06-30 09:29:51 +00:00
|
|
|
foreach ($children as $child_dn)
|
2009-06-30 10:26:08 +00:00
|
|
|
$buildtree = build_tree($ldapserver,$child_dn,$buildtree);
|
2009-06-30 08:05:37 +00:00
|
|
|
}
|
2009-06-30 09:29:51 +00:00
|
|
|
|
2009-06-30 09:40:37 +00:00
|
|
|
if (DEBUG_ENABLED)
|
2009-06-30 10:46:00 +00:00
|
|
|
debug_log('Returning (%s)',1,__FILE__,__LINE__,__METHOD__,$buildtree);
|
2009-06-30 09:40:37 +00:00
|
|
|
|
2009-06-30 10:26:08 +00:00
|
|
|
return $buildtree;
|
2009-06-30 08:05:37 +00:00
|
|
|
}
|
2009-06-30 09:29:51 +00:00
|
|
|
?>
|