LDAPAccountManager/lam/lib/types/dhcp.inc

372 lines
10 KiB
PHP
Raw Normal View History

2008-08-05 19:17:15 +00:00
<?php
/*
2009-10-27 18:47:12 +00:00
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
2010-02-06 12:59:56 +00:00
Copyright (C) 2008 Thomas Manninger
2020-03-19 18:55:58 +00:00
2009 - 2020 Roland Gruber
2008-08-05 19:17:15 +00:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* The account type for DHCP
*
* @package types
* @author Thomas Manninger
**/
/**
* The account type for DHCP
*
* @package types
**/
class dhcp extends baseType {
2016-12-24 12:04:31 +00:00
2009-02-18 19:15:56 +00:00
/**
* Constructs a new DHCP type object.
2017-04-26 16:22:05 +00:00
*
* @param ConfiguredType $type configuration
2009-02-18 19:15:56 +00:00
*/
2017-04-26 16:22:05 +00:00
public function __construct($type) {
parent::__construct($type);
2009-02-18 19:15:56 +00:00
$this->LABEL_CREATE_ANOTHER_ACCOUNT = _('Create another DHCP entry');
$this->LABEL_BACK_TO_ACCOUNT_LIST = _('Back to DHCP list');
}
2016-12-24 12:04:31 +00:00
2008-08-05 19:17:15 +00:00
/**
* Returns the alias name of this account type.
*
* @return string alias name
*/
public function getAlias() {
return _("DHCP");
}
/**
* Returns the description of this account type.
*
* @return string description
*/
public function getDescription() {
return _("DHCP administration");
}
/**
* Returns the class name for the list object.
*
* @return string class name
*/
public function getListClassName() {
return "lamDHCPList";
}
/**
* Returns the default attribute list for this account type.
*
* @return string attribute list
*/
public function getDefaultListAttributes() {
return "#cn;#dhcpRange;#fixed_ips";
}
/**
* Returns a list of attributes which have a translated description.
* This is used for the head row in the list view.
*
* @return array list of descriptions
*/
public function getListAttributeDescriptions() {
2018-06-29 18:44:36 +00:00
return array_merge(
parent::getListAttributeDescriptions(),
array (
"cn" => _("Subnet"),
"dhcprange" => _("Ranges"),
"fixed_ips" => _("IP address") . ' / ' . _('MAC address') . ' / ' . _("Description")
));
2008-08-05 19:17:15 +00:00
}
2010-12-14 21:16:21 +00:00
/**
* Returns the the title text for the title bar on the new/edit page.
*
* @param accountContainer $container account container
2010-12-14 21:16:21 +00:00
* @return String title text
*/
public function getTitleBarTitle($container) {
// get attributes
$attributes = null;
if ($container->getAccountModule('dhcp_settings') != null) {
$attributes = $container->getAccountModule('dhcp_settings')->getAttributes();
2010-12-14 21:16:21 +00:00
}
// check if a common name is set
if (isset($attributes['cn'][0])) {
return htmlspecialchars($attributes['cn'][0]);
}
// new entry
if ($container->isNewAccount) {
return _("New DHCP");
}
2010-12-14 21:16:21 +00:00
// fall back to default
return parent::getTitleBarTitle($container);
2010-12-14 21:16:21 +00:00
}
/**
* Returns the the title text for the title bar on the new/edit page.
*
* @param accountContainer $container account container
2010-12-14 21:16:21 +00:00
* @return String title text
*/
public function getTitleBarSubtitle($container) {
// get attributes
$attributes = null;
if ($container->getAccountModule('dhcp_settings') != null) {
$attributes = $container->getAccountModule('dhcp_settings')->getAttributes();
}
2010-12-14 21:16:21 +00:00
if ($attributes == null) {
return null;
}
$subtitle = '';
// check if an description can be shown
if (isset($attributes['dhcpComments'][0])) {
$subtitle .= htmlspecialchars($attributes['dhcpComments'][0]);
}
if ($subtitle == '') {
return null;
}
return $subtitle;
}
2008-08-05 19:17:15 +00:00
}
/**
* Generates the list view.
*
* @package lists
* @author Thomas Manninger
*
*/
class lamDHCPList extends lamList {
/**
* Constructor
*
* @param string $type account type
* @return lamList list object
*/
public function __construct($type) {
parent::__construct($type);
$this->labels = array(
2012-02-09 17:08:39 +00:00
'nav' => _("DHCP count: %s"),
2008-08-05 19:17:15 +00:00
'error_noneFound' => _("No DHCPs found!"),
'newEntry' => _("New DHCP"),
'dhcpDefaults' => _("DHCP settings"),
2012-02-05 19:03:25 +00:00
'deleteEntry' => _("Delete selected DHCP entries"));
2008-08-05 19:17:15 +00:00
}
2018-11-11 12:28:36 +00:00
/**
* {@inheritDoc}
* @see lamList::isFilterMatching()
*/
protected function isFilterMatching(&$data, $filterAttribute, $regex) {
2018-11-11 18:56:15 +00:00
if ($filterAttribute === "fixed_ips") {
2018-11-11 12:28:36 +00:00
if (empty($regex)) {
return true;
}
$entries = searchLDAP($data['dn'], 'objectClass=dhcpHost', array('dhcpstatements', 'dhcphwaddress', 'cn'));
if (sizeof($entries) > 0) {
foreach ($entries as $entry) {
2020-03-19 18:55:58 +00:00
foreach ($entry as $attrValues) {
2018-11-11 12:28:36 +00:00
if (!is_array($attrValues)) {
continue;
}
foreach ($attrValues as $attrValue) {
if (preg_match($regex, $attrValue)) {
return true;
}
}
if (!empty($entry['dhcpstatements'])) {
$ip = fixed_ip::extractIP($entry['dhcpstatements']);
if (!empty($ip)) {
if (preg_match($regex, $ip)) {
return true;
}
}
}
}
}
}
return false;
}
2018-11-11 18:56:15 +00:00
elseif ($filterAttribute === "dhcprange") {
if (isset($entry['dhcprange'])) {
foreach ($entry['dhcprange'] as $range) {
if (preg_match($regex, $range)) {
return true;
}
}
}
$pooledRanges = searchLDAP($data['dn'], '(objectclass=dhcpPool)', array('dhcprange'));
foreach ($pooledRanges as $pooledRange) {
if (empty($pooledRange['dhcprange'])) {
continue;
}
foreach ($pooledRange['dhcprange'] as $range) {
if (preg_match($regex, $range)) {
return true;
}
}
}
}
2018-11-11 12:28:36 +00:00
return parent::isFilterMatching($data, $filterAttribute, $regex);
}
2008-08-05 19:17:15 +00:00
/**
2018-10-28 12:55:30 +00:00
* {@inheritDoc}
* @see lamList::getTableCellContent()
2008-08-05 19:17:15 +00:00
*/
2018-10-28 12:55:30 +00:00
public function getTableCellContent(&$entry, &$attribute) {
// Fixed IPs
2008-08-05 19:17:15 +00:00
if ($attribute=="fixed_ips") {
// find all fixed addresses:
$entries = searchLDAP($entry['dn'], 'objectClass=dhcpHost', array('dhcpstatements', 'dhcphwaddress', 'cn'));
if (sizeof($entries) > 0) {
// sort by IP
$order = array();
for ($i = 0; $i < sizeof($entries); $i++) {
$order[$i] = '';
if (!empty($entries[$i]['dhcpstatements'])) {
$order[$i] = fixed_ip::extractIP($entries[$i]['dhcpstatements']);
}
}
2018-10-28 12:55:30 +00:00
$group = new htmlGroup();
natcasesort($order);
2018-10-28 12:55:30 +00:00
for ($i = 0; $i < sizeof($order); $i++) {
$dhcpstatements = array();
2010-09-15 18:43:25 +00:00
if (isset($entries[$i]['dhcpstatements'][0])) {
$dhcpstatements = $entries[$i]['dhcpstatements'];
2010-09-15 18:43:25 +00:00
}
2018-10-28 12:55:30 +00:00
$cssClasses = array('nowrap');
if (!fixed_ip::isActive($dhcpstatements)) {
2018-10-28 12:55:30 +00:00
$cssClasses[] = 'strike-through';
}
2010-09-15 18:43:25 +00:00
$dhcphwaddress = explode(" ",$entries[$i]['dhcphwaddress'][0]);
2018-10-28 12:55:30 +00:00
$ipAddress = fixed_ip::extractIP($dhcpstatements);
$ip = new htmlOutputText($ipAddress);
$ip->setCSSClasses($cssClasses);
$group->addElement($ip);
if (!empty($ipAddress)) {
$group->addElement(new htmlOutputText('<br>', false));
}
$macAddress = array_pop($dhcphwaddress);
$mac = new htmlOutputText($macAddress);
$mac->setCSSClasses($cssClasses);
$group->addElement($mac);
if (!empty($macAddress)) {
$group->addElement(new htmlOutputText('<br>', false));
}
$name = new htmlOutputText($entries[$i]['cn'][0]);
$name->setCSSClasses($cssClasses);
$group->addElement($name);
$group->addElement(new htmlOutputText('<br>', false));
if ($i < (sizeof($order) - 1)) {
$group->addElement(new htmlOutputText('<br>', false));
2008-08-05 19:17:15 +00:00
}
}
2018-10-28 12:55:30 +00:00
return $group;
2008-08-05 19:17:15 +00:00
}
}
elseif ($attribute=="dhcprange") { // DHCP Range
2010-09-15 18:43:25 +00:00
if (isset($entry['dhcprange'])) {
2018-10-28 12:55:30 +00:00
$table = new htmlTable();
$table->setCSSClasses(array('nowrap'));
$ranges = array();
2020-03-19 18:55:58 +00:00
foreach($entry['dhcprange'] AS $value) {
2013-09-14 15:31:50 +00:00
if (!empty($value) && !is_numeric($value)) {
2008-08-05 19:17:15 +00:00
$ex = explode(" ", $value);
2018-10-28 12:55:30 +00:00
$row = new htmlTableRow(
array(
new htmlOutputText($ex[0]),
new htmlOutputText(' - '),
new htmlOutputText($ex[1])
)
);
$ranges[$ex[0] . ' - ' . $ex[1]] = $row;
2008-08-05 19:17:15 +00:00
}
}
2014-09-13 14:43:44 +00:00
$pooledRanges = searchLDAP($entry['dn'], '(objectclass=dhcpPool)', array('dhcprange'));
foreach ($pooledRanges as $pool) {
if (empty($pool['dhcprange'])) {
continue;
}
foreach($pool['dhcprange'] AS $id => $value) {
if (!empty($value) && !is_numeric($value)) {
$ex = explode(" ", $value);
2018-10-28 12:55:30 +00:00
$row = new htmlTableRow(
array(
new htmlOutputText($ex[0]),
new htmlOutputText(' - '),
new htmlOutputText($ex[1])
)
);
$ranges[$ex[0] . ' - ' . $ex[1]] = $row;
2014-09-13 14:43:44 +00:00
}
}
}
2018-10-28 12:55:30 +00:00
uksort($ranges, 'strnatcasecmp');
2020-03-19 18:55:58 +00:00
foreach ($ranges as $row) {
2018-10-28 12:55:30 +00:00
$table->addElement($row);
}
return $table;
2008-08-05 19:17:15 +00:00
}
}
2018-10-28 12:55:30 +00:00
else {
return parent::getTableCellContent($entry, $attribute);
2008-08-05 19:17:15 +00:00
}
}
2016-12-24 12:04:31 +00:00
2008-08-05 19:17:15 +00:00
/**
2012-10-07 13:35:10 +00:00
* Add DCP main settings button.
2016-12-24 12:04:31 +00:00
*
2012-10-07 13:35:10 +00:00
* @param htmlGroup $left left part
* @param htmlGroup $right right part
2008-08-05 19:17:15 +00:00
*/
2012-10-07 13:35:10 +00:00
protected function addExtraInputElementsToTopArea(&$left, &$right) {
2016-12-24 12:04:31 +00:00
if (checkIfWriteAccessIsAllowed($this->type->getId())) {
2012-10-07 13:35:10 +00:00
$dhcpButton = new htmlButton('dhcpDefaults', $this->labels['dhcpDefaults']);
$dhcpButton->setIconClass('settingsButton');
2018-10-23 18:25:45 +00:00
$dhcpButton->setCSSClasses(array('fullwidth-mobile-only'));
2012-10-07 13:35:10 +00:00
$left->addElement($dhcpButton);
2008-08-05 19:17:15 +00:00
}
}
2012-10-07 13:35:10 +00:00
2008-08-05 19:17:15 +00:00
/**
* Manages all POST actions (e.g. button pressed) for the account lists.
2016-12-24 12:04:31 +00:00
*
2013-05-05 13:50:19 +00:00
* @return String HTML fragment to insert into beginning of account list
2008-08-05 19:17:15 +00:00
*/
function listDoPost() {
2013-05-05 13:50:19 +00:00
$fragment = parent::listDoPost();
2008-08-05 19:17:15 +00:00
if (isset($_POST['dhcpDefaults'])) {
2017-05-02 19:20:16 +00:00
metaRefresh("../account/edit.php?type=" . $this->type->getId() . "&DN='" . $this->type->getSuffix() . "'");
2017-03-20 20:11:42 +00:00
die();
2008-08-05 19:17:15 +00:00
}
2013-05-05 13:50:19 +00:00
else {
return $fragment;
}
2008-08-05 19:17:15 +00:00
}
}
?>