phpldapadmin/htdocs/copy.php

185 lines
6.4 KiB
PHP
Raw Normal View History

2009-06-30 08:09:20 +00:00
<?php
// $Header$
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.
*
2009-06-30 09:29:51 +00:00
* @package phpLDAPadmin
* @subpackage Page
2009-06-30 09:29:51 +00:00
*/
2009-06-30 09:29:51 +00:00
/**
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
# The DNs we are working with
$request = array();
$request['dnSRC'] = get_request('dn_src');
$request['dnDST'] = get_request('dn_dst');
$ldap = array();
$ldap['SRC'] = $_SESSION[APPCONFIG]->getServer(get_request('server_id_src'));
$ldap['DST'] = $_SESSION[APPCONFIG]->getServer(get_request('server_id_dst'));
2009-06-30 08:07:14 +00:00
# Error checking
if (! trim($request['dnDST']))
error(_('You left the destination DN blank.'),'error','index.php');
2009-06-30 10:46:00 +00:00
if ($ldap['DST']->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
if ($ldap['DST']->dnExists($request['dnDST']))
error(sprintf(_('The destination entry (%s) already exists.'),pretty_print_dn($request['dnDST'])),'error','index.php');
2009-06-30 08:05:37 +00:00
if (! $ldap['DST']->dnExists($ldap['DST']->getContainer($request['dnDST'])))
error(sprintf(_('The destination container (%s) does not exist.'),
pretty_print_dn($ldap['DST']->getContainer($request['dnDST']))),'error','index.php');
2009-06-30 09:29:51 +00:00
if (pla_compare_dns($request['dnSRC'],$request['dnDST']) == 0 && $ldap['SRC']->getIndex() == $ldap['DST']->getIndex())
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
$request['recursive'] = (get_request('recursive') == 'on') ? true : false;
$request['remove'] = (get_request('remove') == 'yes') ? true : false;
2009-06-30 09:29:51 +00:00
if ($request['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
$ldap['tree'] = array();
printf('<h3 class="title">%s%s</h3>',_('Copying '),$request['dnSRC']);
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 />';
2009-06-30 09:29:51 +00:00
print '<small>';
printf ('%s...',_('Building snapshot of tree to copy'));
2009-06-30 09:29:51 +00:00
$ldap['tree'] = build_tree($ldap['SRC'],$request['dnSRC'],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);
$copy_result = r_copy_dn($ldap['SRC'],$ldap['DST'],$ldap['tree'],$request['dnSRC'],$request['dnDST'],$request['remove']);
2009-06-30 10:46:00 +00:00
$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 {
$copy_result = copy_dn($ldap['SRC'],$ldap['DST'],$request['dnSRC'],$request['dnDST'],$request['remove']);
2009-07-06 05:09:17 +00:00
if ($copy_result)
$copy_message = sprintf('%s %s: <b>%s</b> %s',
$request['remove'] ? _('Move successful') : _('Copy successful'),
_('DN'),$request['dnDST'],_('has been created.'));
else
$copy_message = sprintf('%s %s: <b>%s</b> %s',
$request['remove'] ? _('Move NOT successful') : _('Copy NOT successful'),
_('DN'),$request['dnDST'],_('has NOT been created.'));
2009-06-30 08:05:37 +00:00
}
2009-06-30 09:29:51 +00:00
if ($copy_result) {
$redirect_url = sprintf('cmd.php?cmd=template_engine&server_id=%s&dn=%s&refresh=SID_%s_nodes&noheader=1',
$ldap['DST']->getIndex(),rawurlencode($request['dnDST']),$ldap['DST']->getIndex());
2009-06-30 10:46:00 +00:00
system_message(array(
'title'=>_('Copy Entry'),
'body'=>$copy_message,
'type'=>'info'),
$redirect_url);
2009-06-30 08:05:37 +00:00
}
function r_copy_dn($serverSRC,$serverDST,$snapshottree,$dnSRC,$dnDST,$remove) {
2009-08-19 03:39:37 +00:00
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',1,0,__FILE__,__LINE__,__METHOD__,$fargs);
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
$children = isset($snapshottree[$dnSRC]) ? $snapshottree[$dnSRC] : null;
2009-06-30 09:29:51 +00:00
# If we have children, then we need to copy, then delete for a move
if (is_array($children) && count($children)) {
$copy_result = copy_dn($serverSRC,$serverDST,$dnSRC,$dnDST,false);
if (! $copy_result)
return false;
2009-06-30 08:05:37 +00:00
array_push($copy_message,sprintf('%s %s: <b>%s</b> %s',_('Copy successful'),_('DN'),$dnDST,_('has been created.')));
2009-06-30 08:05:37 +00:00
$hadError = false;
foreach ($children as $child_dn) {
$dnDST_new = sprintf('%s,%s',get_rdn($child_dn),$dnDST);
$copy_result = r_copy_dn($serverSRC,$serverDST,$snapshottree,$child_dn,$dnDST_new,$remove);
$copy_message = array_merge($copy_message,array_values($copy_result));
if (! $copy_result)
$hadError = true;
}
if (! $hadError && $remove) {
$delete_result = $serverSRC->delete($dnSRC);
if ($delete_result)
array_push($copy_message,sprintf('%s %s: <b>%s</b> %s',_('Delete successful'),_('DN'),$dnDST,_('has been deleted.')));
2009-06-30 08:05:37 +00:00
}
} else {
$copy_result = copy_dn($serverSRC,$serverDST,$dnSRC,$dnDST,$remove);
2009-07-06 05:09:17 +00:00
if ($copy_result)
array_push($copy_message,sprintf('%s %s: <b>%s</b> %s',
$remove ? _('Move successful') : _('Copy successful'),
_('DN'),$dnDST,_('has been created.')));
else
array_push($copy_message,sprintf('%s %s: <b>%s</b> %s',
$remove ? _('Move NOT successful') : _('Copy NOT successful'),
_('DN'),$dnDST,_('has NOT been created.')));
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
}
function copy_dn($serverSRC,$serverDST,$dnSRC,$dnDST,$remove) {
2009-08-19 03:39:37 +00:00
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',1,0,__FILE__,__LINE__,__METHOD__,$fargs);
2009-06-30 09:29:51 +00:00
$request = array();
$request['pageSRC'] = new PageRender($serverSRC->getIndex(),get_request('template','REQUEST',false,'none'));
$request['pageSRC']->setDN($dnSRC);
$request['pageSRC']->accept();
$request['pageDST'] = new PageRender($serverDST->getIndex(),get_request('template','REQUEST',false,'none'));
$request['pageDST']->setContainer($serverDST->getContainer($dnDST));
$request['pageDST']->accept();
$request['templateSRC'] = $request['pageSRC']->getTemplate();
$request['templateDST'] = $request['pageDST']->getTemplate();
$request['templateDST']->copy($request['pageSRC']->getTemplate(),get_rdn($dnDST,0));
# Create of move the entry
if ($remove)
2009-07-06 05:09:17 +00:00
return $serverDST->rename($request['templateSRC']->getDN(),$request['templateDST']->getRDN(),$serverDST->getContainer($dnDST),true);
else
2009-07-06 05:09:17 +00:00
return $serverDST->add($request['templateDST']->getDN(),$request['templateDST']->getLDAPadd());
2009-06-30 08:05:37 +00:00
}
function build_tree($server,$dn,$buildtree) {
2009-08-19 03:39:37 +00:00
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',1,0,__FILE__,__LINE__,__METHOD__,$fargs);
2009-06-30 10:26:08 +00:00
# We search all children, not only the visible children in the tree
$children = $server->getContainerContents($dn,null,0);
2009-06-30 09:29:51 +00:00
if (count($children)) {
2009-06-30 10:26:08 +00:00
$buildtree[$dn] = $children;
2009-06-30 09:29:51 +00:00
foreach ($children as $child_dn)
$buildtree = build_tree($server,$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-08-19 03:39:37 +00:00
debug_log('Returning (%s)',1,0,__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
?>