Debian Codegraph @

lib/multiEditTool.inc

raw
1 <?php
2
3 namespace LAM\TOOLS\MULTI_EDIT;
4
5 /*
6
7 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
8 Copyright (C) 2023 - 2026 Roland Gruber
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
24 */
25
26 /**
27 * Multi edit tool functions that allows LDAP operations on multiple entries.
28 *
29 * @author Roland Gruber
30 * @package tools
31 */
32
33 /**
34 * Extracts any wildcards from the value field.
35 *
36 * @param string $value value field
37 * @return string[] wildcard attributes
38 */
39 function extractWildcards(string $value): array {
40 $value = strtolower($value);
41 $attributes = [];
42 // ((attr)) -> " "
43 $matches = [];
44 if (preg_match_all('/\\(\\(([a-z0-9-_]+)\\)\\)/i', $value, $matches) > 0) {
45 foreach ($matches[1] as $match) {
46 $attributes[] = strtolower($match);
47 }
48 }
49 // %attr% -> value
50 $matches = [];
51 if (preg_match_all('/%([a-z0-9-_]+)%/i', $value, $matches) > 0) {
52 foreach ($matches[1] as $match) {
53 $attributes[] = strtolower($match);
54 }
55 }
56 // @attr@ -> v
57 $matches = [];
58 if (preg_match_all('/@([a-z0-9-_]+)@/i', $value, $matches) > 0) {
59 foreach ($matches[1] as $match) {
60 $attributes[] = strtolower($match);
61 }
62 }
63 // ??attr?? -> value
64 $matches = [];
65 if (preg_match_all('/\\?\\?([a-z0-9-_]+)\\?\\?/i', $value, $matches) > 0) {
66 foreach ($matches[1] as $match) {
67 $attributes[] = strtolower($match);
68 }
69 }
70 // !!attr!! -> VALUE
71 $matches = [];
72 if (preg_match_all('/!!([a-z0-9-_]+)!!/i', $value, $matches) > 0) {
73 foreach ($matches[1] as $match) {
74 $attributes[] = strtolower($match);
75 }
76 }
77 // ?attr? -> v
78 $matches = [];
79 if (preg_match_all('/\\?([a-z0-9-_]+)\\?/i', $value, $matches) > 0) {
80 foreach ($matches[1] as $match) {
81 $attributes[] = strtolower($match);
82 }
83 }
84 // !attr! -> V
85 $matches = [];
86 if (preg_match_all('/!([a-z0-9-_]+)!/i', $value, $matches) > 0) {
87 foreach ($matches[1] as $match) {
88 $attributes[] = strtolower($match);
89 }
90 }
91 // §attr|;§ -> "val1;val2"
92 $matches = [];
93 if (preg_match_all('/§([a-z0-9-_]+)\\|([^§]+)§/i', $value, $matches) > 0) {
94 foreach ($matches[1] as $match) {
95 $attributes[] = strtolower($match);
96 }
97 }
98 return array_values(array_unique($attributes));
99 }
100
101 /**
102 * Replaces wildcards in the user entered value.
103 *
104 * @param string $value user entered value
105 * @param array<string, string[]|string> $entry LDAP entry
106 * @return string result
107 */
108 function replaceWildcards(string $value, array $entry): string {
109 $attrsLower = array_change_key_case($entry, CASE_LOWER);
110 $replacement = $value;
111 $attrsLower['dn'] = [$attrsLower['dn']];
112 // ((attr)) -> " "
113 $matches = [];
114 if (preg_match_all('/\\(\\(([a-z0-9-_]+)\\)\\)/i', $replacement, $matches) > 0) {
115 foreach ($matches[1] as $match) {
116 $attributeName = strtolower($match);
117 if (!empty($attrsLower[$attributeName][0])) {
118 $replacement = str_replace('((' . $match . '))', ' ', $replacement);
119 }
120 else {
121 $replacement = str_replace('((' . $match . '))', '', $replacement);
122 }
123 }
124 }
125 // %attr% -> value
126 $matches = [];
127 if (preg_match_all('/%([a-z0-9-_]+)%/i', $replacement, $matches) > 0) {
128 foreach ($matches[1] as $match) {
129 $attributeName = strtolower($match);
130 if (!empty($attrsLower[$attributeName][0])) {
131 $replacement = str_replace('%' . $match . '%', $attrsLower[$attributeName][0], $replacement);
132 }
133 else {
134 $replacement = str_replace('%' . $match . '%', '', $replacement);
135 }
136 }
137 }
138 // @attr@ -> v
139 $matches = [];
140 if (preg_match_all('/@([a-z0-9-_]+)@/i', $replacement, $matches) > 0) {
141 foreach ($matches[1] as $match) {
142 $attributeName = strtolower($match);
143 if (!empty($attrsLower[$attributeName][0])) {
144 $replacement = str_replace('@' . $match . '@', substr($attrsLower[$attributeName][0], 0, 1), $replacement);
145 }
146 else {
147 $replacement = str_replace('@' . $match . '@', '', $replacement);
148 }
149 }
150 }
151 // ??attr?? -> value
152 $matches = [];
153 if (preg_match_all('/\\?\\?([a-z0-9-_]+)\\?\\?/i', $replacement, $matches) > 0) {
154 foreach ($matches[1] as $match) {
155 $attributeName = strtolower($match);
156 if (!empty($attrsLower[$attributeName][0])) {
157 $replacement = str_replace('??' . $match . '??', strtolower($attrsLower[$attributeName][0]), $replacement);
158 }
159 else {
160 $replacement = str_replace('??' . $match . '??', '', $replacement);
161 }
162 }
163 }
164 // !!attr!! -> VALUE
165 $matches = [];
166 if (preg_match_all('/!!([a-z0-9-_]+)!!/i', $replacement, $matches) > 0) {
167 foreach ($matches[1] as $match) {
168 $attributeName = strtolower($match);
169 if (!empty($attrsLower[$attributeName][0])) {
170 $replacement = str_replace('!!' . $match . '!!', strtoupper($attrsLower[$attributeName][0]), $replacement);
171 }
172 else {
173 $replacement = str_replace('!!' . $match . '!!', '', $replacement);
174 }
175 }
176 }
177 // ?attr? -> v
178 $matches = [];
179 if (preg_match_all('/\\?([a-z0-9-_]+)\\?/i', $replacement, $matches) > 0) {
180 foreach ($matches[1] as $match) {
181 $attributeName = strtolower($match);
182 if (!empty($attrsLower[$attributeName][0])) {
183 $replacement = str_replace('?' . $match . '?', strtolower(substr($attrsLower[$attributeName][0], 0, 1)), $replacement);
184 }
185 else {
186 $replacement = str_replace('?' . $match . '?', '', $replacement);
187 }
188 }
189 }
190 // !attr! -> V
191 $matches = [];
192 if (preg_match_all('/!([a-z0-9-_]+)!/i', $replacement, $matches) > 0) {
193 foreach ($matches[1] as $match) {
194 $attributeName = strtolower($match);
195 if (!empty($attrsLower[$attributeName][0])) {
196 $replacement = str_replace('!' . $match . '!', strtoupper(substr($attrsLower[$attributeName][0], 0, 1)), $replacement);
197 }
198 else {
199 $replacement = str_replace('!' . $match . '!', '', $replacement);
200 }
201 }
202 }
203 // §attr|;§ -> "val1;val2"
204 $matches = [];
205 if (preg_match_all('/§([a-z0-9-_]+)\\|([^§]+)§/i', $replacement, $matches) > 0) {
206 for ($i = 0; $i < count($matches[1]); $i++) {
207 $attributeName = strtolower($matches[1][$i]);
208 $separator = $matches[2][$i];
209 $match = $matches[0][$i];
210 if (!empty($attrsLower[$attributeName][0])) {
211 $replacement = str_replace($match, implode($separator, $attrsLower[$attributeName]), $replacement);
212 }
213 else {
214 $replacement = str_replace($match, '', $replacement);
215 }
216 }
217 }
218 return $replacement;
219 }