File: create.php

package info (click to toggle)
phpldapadmin 1.1.0.5-6%2Blenny2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,008 kB
  • ctags: 3,949
  • sloc: php: 17,735; xml: 1,532; sh: 388; makefile: 46
file content (134 lines) | stat: -rw-r--r-- 5,152 bytes parent folder | download
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
<?php
// $Header: /cvsroot/phpldapadmin/phpldapadmin/htdocs/create.php,v 1.48.2.1 2007/12/26 09:26:32 wurley Exp $

/**
 * Creates a new object.
 *
 * Variables that come in as POST vars:
 *  - new_dn
 *  - required_attrs (an array with indices being the attributes,
 *		      and the values being their respective values)
 *  - object_classes (rawurlencoded, and serialized array of objectClasses)
 *
 * @package phpLDAPadmin
 */
/**
 * @todo: posixgroup with empty memberlist generates an error.
 */

require './common.php';

if ($ldapserver->isReadOnly())
	pla_error(_('You cannot perform updates while server is in read-only mode'), null, -1, true);

if (! $_SESSION[APPCONFIG]->isCommandAvailable('entry_create'))
	pla_error(sprintf('%s%s %s',_('This operation is not permitted by the configuration'),_(':'),_('create entry')));

$rdn_attr = isset($_POST['rdn_attribute']) ? $_POST['rdn_attribute'] : null;

$entryfactoryclass = $_SESSION[APPCONFIG]->GetValue('appearance','entry_factory');
eval('$entry_factory = new '.$entryfactoryclass.'();');
$entry = $entry_factory->newCreatingEntry('');

eval('$reader = new '.$_SESSION[APPCONFIG]->GetValue('appearance', 'entry_reader').'($ldapserver);');
$entry->accept($reader);

$container = $entry->getContainer();

if (!$container || !$ldapserver->dnExists($container))
	pla_error(sprintf(_('The container you specified (%s) does not exist. Please try again.'),htmlspecialchars($container)),null,-1,true);

$tree = get_cached_item($ldapserver->server_id,'tree');
if ($tree) {
	$container_entry = $tree->getEntry($container);
	if (!$container_entry)
		$tree->addEntry($container);

	$container_entry = $tree->getEntry($container);
	if ($container_entry->isLeaf())
		pla_error(sprintf(_('The container (%s) is a leaf.'), htmlspecialchars($container)), null, -1, true);
}

$entry->setRdnAttributeName($rdn_attr);
if (!$entry->getRdnAttribute())
	pla_error(sprintf(_('The Rdn attribute (%s) does not exist.'), htmlspecialchars($rdn_attr)), null, -1, true);

$new_dn = $entry->getDn();
if (! $new_dn)
	pla_error(_('You left the RDN field blank.'));

$redirect = isset($_POST['redirect']) ? $_POST['redirect'] : false;

$new_entry = array();
$attrs = $entry->getAttributes();
foreach ($attrs as $attr) {
	$vals = $attr->getValues();
	$new_vals = array();
	foreach ($vals as $val) {
		if (strlen($val) > 0)
			$new_vals[] = $val;
	}

	if ($attr->isRequired() && !$new_vals)
		pla_error(sprintf(_('You left the value blank for required attribute (%s).'), htmlspecialchars($attr->getName())));

	if ($new_vals)
		$new_entry[$attr->getName()] = $new_vals;
}

if (! in_array('top', $new_entry['objectClass']))
	$new_entry['objectClass'][] = 'top';

foreach ($new_entry as $attr => $vals) {
	# Check to see if this is a unique Attribute
	if ($badattr = $ldapserver->checkUniqueAttr($new_dn,$attr,$vals)) {
		$search_href = sprintf('?cmd=search&amp;search=true&amp;form=advanced&amp;server_id=%s&amp;filter=%s=%s', $ldapserver->server_id,$attr,$badattr);
		pla_error(sprintf(_('Your attempt to add <b>%s</b> (<i>%s</i>) to <br><b>%s</b><br> is NOT allowed. That attribute/value belongs to another entry.<p>You might like to <a href=\'%s\'>search</a> for that entry.'),$attr,$badattr,$new_dn,$search_href));
	}
}

# Check the user-defined custom call back first
if (run_hook('pre_entry_create',array('server_id'=>$ldapserver->server_id,'dn'=>$new_dn,'attrs'=>$new_entry)))
	$add_result = $ldapserver->add($new_dn,$new_entry);

if ($add_result) {
	run_hook('post_entry_create',array('server_id'=>$ldapserver->server_id,'dn'=>$new_dn,'attrs'=>$new_entry));

	$action_number = $_SESSION[APPCONFIG]->GetValue('appearance', 'action_after_creation');

	$container = get_container($new_dn,false);
	//$container_container = get_container($container);

	if ($redirect) {
		$redirect_url = $redirect;
	} else if ($action_number == 2) {
		$redirect_url = sprintf('cmd.php?cmd=template_engine&server_id=%s&container=%s', $ldapserver->server_id, rawurlencode($container));
	} else {
		$redirect_url = sprintf('cmd.php?cmd=template_engine&server_id=%s&dn=%s', $ldapserver->server_id, rawurlencode($new_dn));
	}

	if ($action_number == 1 || $action_number == 2)
		printf('<meta http-equiv="refresh" content="0; url=%s" />',$redirect_url);

	if ($action_number == 1 || $action_number == 2) {
		$create_message = sprintf('%s DN%s <b>%s</b> %s',_('Creation successful!'),_(':'),htmlspecialchars($new_dn),_('has been created.'));

		system_message(array(
			'title'=>_('Create Entry'),
			'body'=>$create_message,
			'type'=>'info'),
			$redirect_url);
	} else {
		printf('<h3 class="title">%s</h3>',_('Entry created'));
		echo '<br />';
		echo '<center>';
		printf('<a href="cmd.php?cmd=template_engine&server_id=%s&dn=%s">%s</a>.',$ldapserver->server_id,rawurlencode($new_dn),_('Display the new created entry'));
		echo '<br />';
		printf('<a href="cmd.php?cmd=template_engine&server_id=%s&container=%s">%s</a>.',$ldapserver->server_id,rawurlencode($container),_('Create another entry'));
		echo '</center>';
	}

} else {
	pla_error(_('Could not add the object to the LDAP server.'),$ldapserver->error(),$ldapserver->errno());
}
?>