|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Create an entry |
| 4 | + */ |
| 5 | + |
| 6 | +$result = ""; |
| 7 | +$dn = ""; |
| 8 | +$entry = ""; |
| 9 | +$action = "displayform"; |
| 10 | +$result = ""; |
| 11 | + |
| 12 | +if (isset($_POST["action"]) and $_POST["action"]) { |
| 13 | + $action = $_POST["action"]; |
| 14 | +} |
| 15 | + |
| 16 | +if ($result === "") { |
| 17 | + |
| 18 | + require_once("../conf/config.inc.php"); |
| 19 | + require __DIR__ . '/../vendor/autoload.php'; |
| 20 | + require_once("../lib/date.inc.php"); |
| 21 | + |
| 22 | + # Connect to LDAP |
| 23 | + $ldap_connection = $ldapInstance->connect(); |
| 24 | + |
| 25 | + $ldap = $ldap_connection[0]; |
| 26 | + $result = $ldap_connection[1]; |
| 27 | + |
| 28 | + if ($ldap) { |
| 29 | + |
| 30 | + # Create entry |
| 31 | + if ($action == "createentry") { |
| 32 | + |
| 33 | + # Get all data |
| 34 | + $create_attributes = array(); |
| 35 | + foreach ($create_items as $item) { |
| 36 | + $values = array(); |
| 37 | + $item_keys = preg_grep("/^$item(\d+)$/", array_keys($_POST)); |
| 38 | + foreach ($item_keys as $item_key) { |
| 39 | + if (isset($_POST[$item_key]) and !empty($_POST[$item_key])) { |
| 40 | + $value = $_POST[$item_key]; |
| 41 | + if ( $attributes_map[$item]['type'] == "date" || $attributes_map[$item]['type'] == "ad_date" ) { |
| 42 | + $value = $directory->getLdapDate(new DateTime($_POST[$item_key])); |
| 43 | + } |
| 44 | + $values[] = $value; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + $create_attributes[ $attributes_map[$item]['attribute'] ] = $values; |
| 49 | + } |
| 50 | + |
| 51 | + $create_attributes['objectclass'] = $create_objectclass; |
| 52 | + |
| 53 | + $dn = ""; |
| 54 | + |
| 55 | + foreach ($create_dn_items as $dn_item) { |
| 56 | + $attribute = $attributes_map[$dn_item]['attribute']; |
| 57 | + if ($dn) { $dn .= "+"; } |
| 58 | + $dn .= $attribute . "=" . ldap_escape($create_attributes[$attribute][0], "", LDAP_ESCAPE_DN); |
| 59 | + } |
| 60 | + |
| 61 | + $dn .= "," . $create_base; |
| 62 | + |
| 63 | + # Use macros |
| 64 | + foreach ($create_items_macros as $item => $macro) { |
| 65 | + $value = preg_replace_callback('/%(\w+)%/', |
| 66 | + function ($matches) use ($item, $create_attributes, $attributes_map) { |
| 67 | + return $create_attributes[ $attributes_map[$matches[1]]['attribute'] ][0]; |
| 68 | + }, |
| 69 | + $macro); |
| 70 | + error_log( "Use macro $macro for item $item: $value" ); |
| 71 | + $create_attributes[ $attributes_map[$item]['attribute'] ] = $value; |
| 72 | + } |
| 73 | + |
| 74 | + # Create entry |
| 75 | + if (!ldap_add($ldap, $dn, $create_attributes)) { |
| 76 | + error_log("LDAP - modify failed for $dn"); |
| 77 | + $result = "createfailed"; |
| 78 | + $action = "displayform"; |
| 79 | + } else { |
| 80 | + $errno = ldap_errno($ldap); |
| 81 | + if ( $errno ) { |
| 82 | + error_log("LDAP - create error $errno (".ldap_error($ldap).") for $dn"); |
| 83 | + $result = "createfailed"; |
| 84 | + $action = "displayform"; |
| 85 | + } else { |
| 86 | + $result = "createok"; |
| 87 | + $action = "displayentry"; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if ($audit_log_file) { |
| 92 | + auditlog($audit_log_file, $dn, $audit_admin, "createentry", $result, $comment); |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + # Display form |
| 98 | + if ($action == "displayform") { |
| 99 | + |
| 100 | + # Compute lists |
| 101 | + $item_list = array(); |
| 102 | + |
| 103 | + foreach ($create_items as $item) { |
| 104 | + if ( $attributes_map[$item]["type"] === "static_list") { |
| 105 | + $item_list[$item] = isset($attributes_static_list[$item]) ? $attributes_static_list[$item] : array(); |
| 106 | + } |
| 107 | + if ( $attributes_map[$item]["type"] === "list") { |
| 108 | + $item_list[$item] = $ldapInstance->get_list( $attributes_list[$item]["base"], $attributes_list[$item]["filter"], $attributes_list[$item]["key"], $attributes_list[$item]["value"] ); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +if ( $action == "displayentry" ) { |
| 117 | + $location = 'index.php?page=display&dn='.urlencode($dn).'&createresult='.$result; |
| 118 | + header('Location: '.$location); |
| 119 | +} |
| 120 | + |
| 121 | +$smarty->assign("entry", $entry); |
| 122 | +$smarty->assign("action", $action); |
| 123 | + |
| 124 | +$smarty->assign("item_list", $item_list); |
| 125 | + |
| 126 | +$smarty->assign("create_items", $create_items); |
| 127 | +$smarty->assign("show_undef", $display_show_undefined); |
| 128 | + |
| 129 | +?> |
0 commit comments