Debian Codegraph @

lib/types/dhcp.inc

raw
1 <?php
2 /*
3
4 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
5 Copyright (C) 2008 Thomas Manninger
6 2009 - 2026 Roland Gruber
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 */
23
24
25 /**
26 * The account type for DHCP
27 *
28 * @package types
29 * @author Thomas Manninger
30 **/
31
32 use LAM\TYPES\ConfiguredType;
33
34 /**
35 * The account type for DHCP
36 *
37 * @package types
38 **/
39 class dhcp extends baseType {
40
41 /**
42 * Constructs a new DHCP type object.
43 *
44 * @param ConfiguredType $type configuration
45 */
46 public function __construct($type) {
47 parent::__construct($type);
48 $this->LABEL_CREATE_ANOTHER_ACCOUNT = _('Create another DHCP entry');
49 $this->LABEL_BACK_TO_ACCOUNT_LIST = _('Back to DHCP list');
50 }
51
52 /**
53 * {@inheritDoc}
54 */
55 public function getAlias(): string {
56 return _("DHCP");
57 }
58
59 /**
60 * {@inheritDoc}
61 */
62 public function getDescription(): string {
63 return _("DHCP administration");
64 }
65
66 /**
67 * {@inheritDoc}
68 */
69 public function getIcon(): string {
70 return 'world-color.svg';
71 }
72
73 /**
74 * {@inheritDoc}
75 */
76 public function getListClassName(): string {
77 return lamDHCPList::class;
78 }
79
80 /**
81 * {@inheritDoc}
82 */
83 public function getDefaultListAttributes(): string {
84 return "#cn;#dhcpRange;#fixed_ips";
85 }
86
87 /**
88 * {@inheritDoc}
89 */
90 public function getTitleBarTitle(accountContainer $container): ?string {
91 // get attributes
92 $attributes = null;
93 if ($container->getAccountModule('dhcp_settings') != null) {
94 $attributes = $container->getAccountModule('dhcp_settings')->getAttributes();
95 }
96 // check if a common name is set
97 if (isset($attributes['cn'][0])) {
98 return htmlspecialchars($attributes['cn'][0]);
99 }
100 // new entry
101 if ($container->isNewAccount) {
102 return _("New DHCP");
103 }
104 // fall back to default
105 return parent::getTitleBarTitle($container);
106 }
107
108 /**
109 * {@inheritDoc}
110 */
111 public function getTitleBarSubtitle(accountContainer $container): string {
112 // get attributes
113 $attributes = null;
114 if ($container->getAccountModule('dhcp_settings') != null) {
115 $attributes = $container->getAccountModule('dhcp_settings')->getAttributes();
116 }
117 if ($attributes == null) {
118 return '';
119 }
120 $subtitle = '';
121 // check if a description can be shown
122 if (isset($attributes['dhcpComments'][0])) {
123 $subtitle .= htmlspecialchars($attributes['dhcpComments'][0]);
124 }
125 return $subtitle;
126 }
127
128 }
129
130 /**
131 * Generates the list view.
132 *
133 * @package lists
134 * @author Thomas Manninger
135 *
136 */
137 class lamDHCPList extends lamList {
138
139 /**
140 * Constructor
141 *
142 * @param ConfiguredType $type account type
143 */
144 public function __construct(ConfiguredType $type) {
145 parent::__construct($type);
146 $this->labels = [
147 'nav' => _("DHCP count: %s"),
148 'error_noneFound' => _("No DHCPs found!"),
149 'newEntry' => _("New DHCP"),
150 'dhcpDefaults' => _("DHCP settings"),
151 'deleteEntry' => _("Delete selected DHCP entries")
152 ];
153 }
154
155 /**
156 * {@inheritDoc}
157 * @see lamList::isFilterMatching()
158 */
159 protected function isFilterMatching(&$data, $filterAttribute, $regex): bool {
160 if ($filterAttribute === "fixed_ips") {
161 if (empty($regex)) {
162 return true;
163 }
164 $entries = searchLDAP($data['dn'], 'objectClass=dhcpHost', ['dhcpstatements', 'dhcphwaddress', 'cn']);
165 if (count($entries) > 0) {
166 foreach ($entries as $entry) {
167 foreach ($entry as $attrValues) {
168 if (!is_array($attrValues)) {
169 continue;
170 }
171 foreach ($attrValues as $attrValue) {
172 if (preg_match($regex, $attrValue)) {
173 return true;
174 }
175 }
176 if (!empty($entry['dhcpstatements'])) {
177 $ip = fixed_ip::extractIP($entry['dhcpstatements']);
178 if (!empty($ip) && preg_match($regex, $ip)) {
179 return true;
180 }
181 }
182 }
183 }
184 }
185 return false;
186 }
187 elseif ($filterAttribute === "dhcprange") {
188 if (isset($data['dhcprange'])) {
189 foreach ($data['dhcprange'] as $range) {
190 if (preg_match($regex, $range)) {
191 return true;
192 }
193 }
194 }
195 $pooledRanges = searchLDAP($data['dn'], '(objectclass=dhcpPool)', ['dhcprange']);
196 foreach ($pooledRanges as $pooledRange) {
197 if (empty($pooledRange['dhcprange'])) {
198 continue;
199 }
200 foreach ($pooledRange['dhcprange'] as $range) {
201 if (preg_match($regex, $range)) {
202 return true;
203 }
204 }
205 }
206 }
207 return parent::isFilterMatching($data, $filterAttribute, $regex);
208 }
209
210 /**
211 * {@inheritDoc}
212 */
213 protected function addExtraInputElementsToTopArea(&$left, &$right): void {
214 if (checkIfWriteAccessIsAllowed($this->type->getId())) {
215 $dhcpButton = new htmlButton('dhcpDefaults', $this->labels['dhcpDefaults']);
216 $dhcpButton->setCSSClasses(['fullwidth-mobile-only lam-secondary']);
217 $left->addElement($dhcpButton);
218 }
219 }
220
221 /**
222 * {@inheritDoc}
223 */
224 protected function listDoPost(): ?htmlElement {
225 $fragment = parent::listDoPost();
226 if (isset($_POST['dhcpDefaults'])) {
227 metaRefresh("../account/edit.php?type=" . $this->type->getId() . "&DN='" . $this->type->getSuffix() . "'");
228 die();
229 }
230 else {
231 return $fragment;
232 }
233 }
234 }