Debian Codegraph @

templates/account/edit.php

raw
1 <?php
2 namespace LAM\ACCOUNT;
3 use accountContainer;
4 use DateTime;
5 use LAM\TYPES\TypeManager;
6
7 /*
8
9 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
10 Copyright (C) 2003 - 2006 Tilo Lutz
11 2005 - 2025 Roland Gruber
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27
28 LDAP Account Manager displays table for creating or modifying accounts in LDAP
29 */
30
31 /**
32 * Displays the account detail page.
33 *
34 * @package modules
35 * @author Tilo Lutz
36 * @author Roland Gruber
37 */
38
39 /** security functions */
40 include_once(__DIR__ . "/../../lib/security.inc");
41 /** configuration options */
42 include_once(__DIR__ . '/../../lib/config.inc');
43 /** functions to load and save profiles */
44 include_once(__DIR__ . '/../../lib/profiles.inc');
45 /** Return error-message */
46 include_once(__DIR__ . '/../../lib/status.inc');
47 /** Return a pdf-file */
48 include_once(__DIR__ . '/../../lib/pdf.inc');
49 /** module functions */
50 include_once(__DIR__ . '/../../lib/modules.inc');
51
52 // Start session
53 startSecureSession();
54 enforceUserIsLoggedIn();
55
56 // Redirect to startpage if user is not logged in
57 if (!isLoggedIn()) {
58 metaRefresh("../login.php");
59 exit;
60 }
61
62 // Set correct language, codepages, ....
63 setlanguage();
64
65 $sessionAccountPrefix = 'editContainer';
66 if (isset($_GET['editKey'])) {
67 $sessionKey = htmlspecialchars($_GET['editKey']);
68 }
69 else {
70 $sessionKey = $sessionAccountPrefix . (new DateTime('now', getTimeZone()))->getTimestamp() . generateRandomText();
71 }
72
73 // cleanup account containers in session
74 $cleanupCandidates = [];
75 foreach ($_SESSION as $key => $value) {
76 if (str_starts_with($key, $sessionAccountPrefix)) {
77 $cleanupCandidates[] = $key;
78 }
79 $candidateCount = count($cleanupCandidates);
80 if ($candidateCount > 100) {
81 $numToDelete = $candidateCount - 100;
82 natsort($cleanupCandidates);
83 for ($i = 0; $i < $numToDelete; $i++) {
84 $toDelete = array_shift($cleanupCandidates);
85 unset($_SESSION[$toDelete]);
86 }
87 }
88 }
89
90 $typeManager = new TypeManager();
91 //load account
92
93 if (!empty($_GET['DN'])) {
94 $type = $typeManager->getConfiguredType($_GET['type']);
95 $dn = cleanDn($_GET['DN']);
96 if (($type === null) || $type->isHidden()) {
97 logNewMessage(LOG_ERR, 'User tried to access hidden account type: ' . $_GET['type']);
98 die();
99 }
100 $suffix = strtolower($type->getSuffix());
101 $_SESSION[$sessionKey] = new accountContainer($type, $sessionKey);
102 $result = $_SESSION[$sessionKey]->load_account($dn);
103 if (count($result) > 0) {
104 include __DIR__ . '/../../lib/adminHeader.inc';
105 foreach ($result as $message) {
106 call_user_func_array(StatusMessage(...), $message);
107 }
108 include __DIR__ . '/../../lib/adminFooter.inc';
109 die();
110 }
111 }
112 // new account
113 elseif (empty($_POST)) {
114 $type = $typeManager->getConfiguredType($_GET['type']);
115 if (($type === null) || $type->isHidden()) {
116 logNewMessage(LOG_ERR, 'User tried to access hidden account type: ' . $_GET['type']);
117 die();
118 }
119 elseif (!checkIfNewEntriesAreAllowed($type->getId())) {
120 logNewMessage(LOG_ERR, 'User tried to create entry of forbidden account type: ' . $type->getId());
121 die();
122 }
123 $_SESSION[$sessionKey] = new accountContainer($type, $sessionKey);
124 $_SESSION[$sessionKey]->new_account();
125 if (!empty($_GET['copyDn'])) {
126 $copyDn = cleanDn($_GET['copyDn']);
127 $copyDnLower = strtolower($copyDn);
128 $suffix = strtolower($type->getSuffix());
129 if (strpos($copyDnLower, $suffix) !== (strlen($copyDnLower) - strlen($suffix))) {
130 logNewMessage(LOG_ERR, 'User tried to access entry of type ' . $type->getId() . ' outside suffix ' . $suffix);
131 die();
132 }
133 $_SESSION[$sessionKey]->copyFromExistingAccount($copyDn);
134 }
135 }
136
137 // show account page
138 $_SESSION[$sessionKey]->continue_main();
139
140 /**
141 * Cleans the given DN from GET.
142 *
143 * @param string $dn DN
144 * @return string cleaned DN
145 */
146 function cleanDn(string $dn) : string {
147 $cleanDn = str_replace("\\'", '', $dn);
148 if ($dn == $cleanDn) {
149 if (str_starts_with($cleanDn, "'")) {
150 $cleanDn = substr($cleanDn, 1);
151 }
152 if (str_ends_with($cleanDn, "'")) {
153 $cleanDn = substr($cleanDn, 0, -1);
154 }
155 }
156 return $cleanDn;
157 }