File: copy.php

package info (click to toggle)
phpldapadmin 1.2.6.7-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 5,528 kB
  • sloc: php: 17,684; javascript: 5,299; xml: 1,498; sh: 379; python: 148; makefile: 23
file content (209 lines) | stat: -rw-r--r-- 7,634 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
 * Copies a given object to create a new one.
 *
 * @package phpLDAPadmin
 * @subpackage Page
 */

/**
 */

require './common.php';

# 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'));

# Error checking
if (! trim($request['dnDST']))
	error(_('You left the destination DN blank.'),'error','index.php');

if ($ldap['DST']->isReadOnly())
	error(_('Destination server is currently READ-ONLY.'),'error','index.php');

if ($ldap['DST']->dnExists($request['dnDST']))
	error(sprintf(_('The destination entry (%s) already exists.'),pretty_print_dn($request['dnDST'])),'error','index.php');

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');

if (pla_compare_dns($request['dnSRC'],$request['dnDST']) == 0 && $ldap['SRC']->getIndex() == $ldap['DST']->getIndex())
	error(_('The source and destination DN are the same.'),'error','index.php');

$request['recursive'] = (get_request('recursive') == 'on') ? true : false;
$request['remove'] = (get_request('remove') == 'yes') ? true : false;

if ($request['recursive']) {
	$filter = get_request('filter','POST',false,'(objectClass=*)',false);

	# 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']);
	printf('<h3 class="subtitle">%s</h3>',_('Recursive copy progress'));
	print '<br /><br />';

	print '<small>';
	printf ('%s...',_('Building snapshot of tree to copy'));

	$ldap['tree'] = build_tree($ldap['SRC'],$request['dnSRC'],array(),$filter);
	printf('<span style="color:green">%s</span><br />',_('Success'));

	# 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']);
	$copy_message = $copy_result;
	print '</small>';

} else {
	if ($_SESSION[APPCONFIG]->getValue('confirm','copy') && !$request['remove']) {
		$request['pageSRC'] = new TemplateRender($ldap['SRC']->getIndex(),get_request('template','REQUEST',false,null));
		$request['pageSRC']->setDN($request['dnSRC']);
		$request['pageSRC']->accept(true);

		$request['pageDST'] = new TemplateRender($ldap['DST']->getIndex(),get_request('template','REQUEST',false,'none'));
		$request['pageDST']->setContainer($app['server']->getContainer($request['dnDST']));
		$request['pageDST']->accept(true);

		$request['templateSRC'] = $request['pageSRC']->getTemplate();
		$request['templateDST'] = $request['pageDST']->getTemplate();

		$request['templateDST']->copy($request['templateSRC'],get_rdn($request['dnDST']),true);

		# Set all attributes with a values as shown, and remove the add value options
		foreach ($request['templateDST']->getAttributes(true) as $sattribute)
			if ($sattribute->getValues() && ! $sattribute->isInternal()) {
				$sattribute->show();
				$sattribute->setMaxValueCount(count($sattribute->getValues()));
			}

		$request['pageDST']->accept();

		return;

	} else {
		$copy_result = copy_dn($ldap['SRC'],$ldap['DST'],$request['dnSRC'],$request['dnDST'],$request['remove']);

		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.'));
	}
}

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());

	system_message(array(
		'title'=>_('Copy Entry'),
		'body'=>$copy_message,
		'type'=>'info'),
		$redirect_url);
}

function r_copy_dn($serverSRC,$serverDST,$snapshottree,$dnSRC,$dnDST,$remove) {
	if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
		debug_log('Entered (%%)',1,0,__FILE__,__LINE__,__METHOD__,$fargs);

	$copy_message = array();

	$children = isset($snapshottree[$dnSRC]) ? $snapshottree[$dnSRC] : null;

	# 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;

		array_push($copy_message,sprintf('%s %s: <b>%s</b> %s',_('Copy successful'),_('DN'),$dnDST,_('has been created.')));

		$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.')));
		}

	} else {
		$copy_result = copy_dn($serverSRC,$serverDST,$dnSRC,$dnDST,$remove);

		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.')));
	}

	return $copy_message;
}

function copy_dn($serverSRC,$serverDST,$dnSRC,$dnDST,$remove) {
	if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
		debug_log('Entered (%%)',1,0,__FILE__,__LINE__,__METHOD__,$fargs);

	$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)
		return $serverDST->rename($request['templateSRC']->getDN(),$request['templateDST']->getRDN(),$serverDST->getContainer($dnDST),true);
	else
		return $serverDST->add($request['templateDST']->getDN(),$request['templateDST']->getLDAPadd());
}

function build_tree($server,$dn,$buildtree) {
	if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
		debug_log('Entered (%%)',1,0,__FILE__,__LINE__,__METHOD__,$fargs);

	# We search all children, not only the visible children in the tree
	$children = $server->getContainerContents($dn,null,0);

	if (count($children)) {
		$buildtree[$dn] = $children;
		foreach ($children as $child_dn)
			$buildtree = build_tree($server,$child_dn,$buildtree);
	}

	if (DEBUG_ENABLED)
		debug_log('Returning (%s)',1,0,__FILE__,__LINE__,__METHOD__,$buildtree);

	return $buildtree;
}
?>