LDAPAccountManager/lam/lib/types.inc

519 lines
12 KiB
PHP
Raw Normal View History

2006-01-01 16:30:05 +00:00
<?php
2016-12-19 20:32:08 +00:00
namespace LAM\TYPES;
2006-01-01 16:30:05 +00:00
/*
2009-10-27 18:47:12 +00:00
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
2018-08-10 18:02:41 +00:00
Copyright (C) 2005 - 2018 Roland Gruber
2006-01-01 16:30:05 +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
*/
/**
* This file is the interface to the different account types.
*
* @package types
* @author Roland Gruber
*/
/** parent class of account types */
2018-12-23 16:52:56 +00:00
include_once(__DIR__ . "/baseType.inc");
2006-01-01 16:30:05 +00:00
/** parent class of list views */
2018-12-23 16:52:56 +00:00
include_once(__DIR__ . "/lists.inc");
2007-11-05 18:12:53 +00:00
/** Used to check if this is a LAM Pro release. */
2018-12-23 16:52:56 +00:00
include_once(__DIR__ . "/selfService.inc");
2006-01-01 16:30:05 +00:00
/**
* This includes all type definitions.
*/
$typesINC_dirname = substr(__FILE__, 0, strlen(__FILE__) - 10) . "/types";
$typesINC_dir = dir($typesINC_dirname);
// get module names.
2018-12-23 10:11:23 +00:00
while ($entry = $typesINC_dir->read()) {
if ((substr($entry, strlen($entry) - 4, 4) == '.inc') && is_file($typesINC_dirname . '/'.$entry)) {
include_once($typesINC_dirname . '/'.$entry);
}
2006-01-01 16:30:05 +00:00
}
/**
* Returns a list of available account types.
*
* @return array list of types
*/
function getTypes() {
$dirname = substr(__FILE__, 0, strlen(__FILE__) - 10) . "/types";
$dir = dir($dirname);
$return = array();
// get type names.
2014-04-20 13:15:37 +00:00
while ($entry = $dir->read()) {
2006-01-01 16:30:05 +00:00
if ((substr($entry, strlen($entry) - 4, 4) == '.inc') && is_file($dirname . '/'.$entry)) {
$entry = substr($entry, 0, strpos($entry, '.'));
$return[] = $entry;
}
2014-04-20 13:15:37 +00:00
}
$dir->close();
2006-01-01 16:30:05 +00:00
return $return;
}
/**
* Returns the description of an account type.
*
* @param string $type type name
* @return string type description
*/
function getTypeDescription($type) {
2017-04-25 18:03:38 +00:00
$obj = new $type(null);
2006-01-01 16:30:05 +00:00
return $obj->getDescription();
}
2016-12-28 18:53:49 +00:00
/**
* Returns the account type for a given type id.
*
* @param string $typeId type id (e.g. user_1)
* @return string scope (e.g. user)
*/
function getScopeFromTypeId($typeId) {
$parts = explode('_', $typeId);
return $parts[0];
}
2016-12-23 19:58:01 +00:00
/**
* Represents a configured account type variant.
*
* @package types
* @author Roland Gruber
*/
class ConfiguredType {
private $scope;
private $id;
2017-01-06 10:38:52 +00:00
private $suffix = null;
2016-12-23 19:58:01 +00:00
2017-01-06 10:38:52 +00:00
private $attributes = null;
2016-12-23 19:58:01 +00:00
2017-01-06 10:38:52 +00:00
private $alias = null;
2016-12-23 19:58:01 +00:00
2017-01-06 10:38:52 +00:00
private $additionalLdapFilter = null;
2016-12-23 19:58:01 +00:00
2017-01-06 10:38:52 +00:00
private $hidden = null;
2016-12-23 19:58:01 +00:00
2016-12-24 12:04:31 +00:00
private $baseType;
2017-01-03 19:02:29 +00:00
private $typeManager;
2016-12-23 19:58:01 +00:00
/**
* Constructor
*
2017-01-03 19:02:29 +00:00
* @param TypeManager $typeManager type manager
2016-12-23 19:58:01 +00:00
* @param string $scope account type
* @param string $id unique ID for this configuration
*/
2017-01-06 10:38:52 +00:00
public function __construct(&$typeManager, $scope, $id) {
2017-01-03 19:02:29 +00:00
$this->typeManager = &$typeManager;
2016-12-23 19:58:01 +00:00
$this->scope = $scope;
$this->id = $id;
}
2017-01-03 19:02:29 +00:00
/**
* Returns the owning type manager.
*
* @return TypeManager type manager
*/
public function getTypeManager() {
return $this->typeManager;
}
2016-12-23 19:58:01 +00:00
/**
* Returns the account type (e.g. 'user').
*
2017-01-03 19:02:29 +00:00
* @return string account type
2016-12-23 19:58:01 +00:00
*/
public function getScope() {
return $this->scope;
}
/**
* Returns a unique id for this configuration.
*
2017-01-03 19:02:29 +00:00
* @return string unique id
2016-12-23 19:58:01 +00:00
*/
public function getId() {
return $this->id;
}
/**
* Returns the LDAP suffix.
*
2017-01-03 19:02:29 +00:00
* @return string LDAP suffix
2016-12-23 19:58:01 +00:00
*/
public function getSuffix() {
2017-01-06 10:38:52 +00:00
if ($this->suffix !== null) {
return $this->suffix;
}
$this->suffix = $this->typeManager->getConfig()->get_Suffix($this->id);
2016-12-23 19:58:01 +00:00
return $this->suffix;
}
/**
* Returns a list of configured attributes.
*
2017-01-03 19:02:29 +00:00
* @return ListAttribute[] list of ListAttribute
2016-12-23 19:58:01 +00:00
*/
public function getAttributes() {
2017-01-06 10:38:52 +00:00
if ($this->attributes !== null) {
return $this->attributes;
}
$attributeString = $this->typeManager->getConfig()->get_listAttributes($this->id);
$attributeSpecs = explode(';', $attributeString);
$attributes = array();
foreach ($attributeSpecs as $attributeSpec) {
2017-04-25 18:14:59 +00:00
$attributes[] = new ListAttribute($attributeSpec, $this);
2017-01-06 10:38:52 +00:00
}
$this->attributes = $attributes;
2016-12-23 19:58:01 +00:00
return $this->attributes;
}
/**
* Returns the alias name.
*
2017-01-03 19:02:29 +00:00
* @return string alias name
2016-12-23 19:58:01 +00:00
*/
public function getAlias() {
2017-01-06 10:38:52 +00:00
if ($this->alias !== null) {
return $this->alias;
}
2017-04-25 18:03:38 +00:00
$typeSettings = $this->typeManager->getConfig()->get_typeSettings();
if (!empty($typeSettings['customLabel_' . $this->id])) {
return $typeSettings['customLabel_' . $this->id];
}
return $this->getBaseType()->getAlias();
2016-12-23 19:58:01 +00:00
}
/**
* Returns the additional LDAP filter.
*
2017-01-03 19:02:29 +00:00
* @return string LDAP filter
2016-12-23 19:58:01 +00:00
*/
2016-12-24 12:04:31 +00:00
public function getAdditionalLdapFilter() {
2017-01-06 10:38:52 +00:00
if ($this->additionalLdapFilter !== null) {
return $this->additionalLdapFilter;
}
2017-03-21 17:47:05 +00:00
$typeSettings = $this->typeManager->getConfig()->get_typeSettings();
$this->additionalLdapFilter = isset($typeSettings['filter_' . $this->id]) ? $typeSettings['filter_' . $this->id] : '';
2016-12-24 12:04:31 +00:00
return $this->additionalLdapFilter;
2016-12-23 19:58:01 +00:00
}
/**
* Returns if this configuration is hidden.
*
2017-01-03 19:02:29 +00:00
* @return boolean hidden
2016-12-23 19:58:01 +00:00
*/
public function isHidden() {
2017-01-06 10:38:52 +00:00
if ($this->hidden !== null) {
return $this->hidden;
}
$this->hidden = isAccountTypeHidden($this->id);
2016-12-23 19:58:01 +00:00
return $this->hidden;
}
2016-12-24 12:04:31 +00:00
/**
* Returns the base type of this configured type.
*
2016-12-28 18:53:49 +00:00
* @return \baseType base type
2016-12-24 12:04:31 +00:00
*/
public function getBaseType() {
if ($this->baseType != null) {
return $this->baseType;
}
$scope = $this->scope;
2017-04-26 16:22:05 +00:00
$this->baseType = new $scope($this);
2016-12-24 12:04:31 +00:00
return $this->baseType;
}
2016-12-31 13:44:46 +00:00
/**
* Returns a list of LDAP suffixes for this type.
*
* @return array sorted list of possible suffixes for this type.
*/
public function getSuffixList() {
$connection = $_SESSION["ldap"]->server();
$ret = array();
$filter = $this->getBaseType()->getSuffixFilter();
2017-01-06 10:38:52 +00:00
$sr = @ldap_search($connection, escapeDN($this->getSuffix()), $filter, array('dn', 'objectClass'), 0, 0, 0, LDAP_DEREF_NEVER);
2016-12-31 13:44:46 +00:00
if ($sr) {
$units = ldap_get_entries($connection, $sr);
cleanLDAPResult($units);
// extract Dns
$count = sizeof($units);
for ($i = 0; $i < $count; $i++) {
if (in_array('container', $units[$i]['objectclass'])) {
// Active Directory fix, hide system containers
if (preg_match('/.*cn=system,dc=.+/i', $units[$i]['dn']) || preg_match('/.*CN=program data,dc=.+/i', $units[$i]['dn'])) {
continue;
}
}
$ret[] = $units[$i]['dn'];
}
}
// add root suffix if needed
$found = false;
for ($i = 0; $i < sizeof($ret); $i++) { // search suffix case-intensitive
2017-01-06 10:38:52 +00:00
if (strtolower($this->getSuffix()) == strtolower($ret[$i])) {
2016-12-31 13:44:46 +00:00
$found = true;
break;
}
}
if (!$found) {
2017-01-06 10:38:52 +00:00
$ret[] = $this->getSuffix();
2016-12-31 13:44:46 +00:00
}
usort($ret, 'compareDN');
return $ret;
}
2017-03-12 11:12:11 +00:00
/**
* Returns the names of the active modules for this type.
*
* @return string[] module names
*/
public function getModules() {
$typeSettings = $this->typeManager->getConfig()->get_typeSettings();
$modules = !empty($typeSettings['modules_' . $this->getId()]) ? $typeSettings['modules_' . $this->getId()] : '';
return explode(',', $modules);
}
/**
* Returns the file name of the type icon.
* It is 16x16px and located in graphics folder.
*
* @return string file name
*/
public function getIcon() {
$baseType = $this->getBaseType();
return $baseType->getIcon();
}
2016-12-23 19:58:01 +00:00
}
/**
* An attribute definition for the account list.
*
* @package types
* @author Roland Gruber
*/
class ListAttribute {
private $attributeSpec;
2017-04-25 18:14:59 +00:00
private $type;
2016-12-23 19:58:01 +00:00
/**
* Constructor.
*
* @param string $attributeSpec spec of attribute (e.g. '#uid' or 'uid:User')
2017-04-25 18:14:59 +00:00
* @param ConfiguredType $type account type (e.g. 'user')
2016-12-23 19:58:01 +00:00
*/
2017-04-25 18:14:59 +00:00
public function __construct($attributeSpec, $type) {
2016-12-23 19:58:01 +00:00
$this->attributeSpec = $attributeSpec;
2017-04-25 18:14:59 +00:00
$this->type = $type;
2016-12-23 19:58:01 +00:00
}
/**
* Returns the name of the LDAP attribute.
*
* @return string $attributeName name
*/
public function getAttributeName() {
if ($this->isPredefined()) {
return substr($this->attributeSpec, 1);
}
$parts = explode(':', $this->attributeSpec);
return $parts[0];
}
/**
* Returns the display value.
*
* @return string $alias display value
*/
public function getAlias() {
if ($this->isPredefined()) {
2017-01-11 20:01:37 +00:00
$name = strtolower(substr($this->attributeSpec, 1));
2017-04-25 18:14:59 +00:00
$hash_table = $this->type->getBaseType()->getListAttributeDescriptions();
2016-12-23 19:58:01 +00:00
$hash_table = array_change_key_case($hash_table, CASE_LOWER);
if (isset($hash_table[$name])) {
return $hash_table[$name];
}
return $name;
}
$parts = explode(':', $this->attributeSpec);
return $parts[1];
}
/**
* Returns if this is a predefined attribute name.
*
* @return boolean is predefined
*/
private function isPredefined() {
return strpos($this->attributeSpec, '#') === 0;
}
}
/**
* Provides utility functions to get e.g. configured types.
*
* @package types
* @author Roland Gruber
*/
class TypeManager {
2016-12-28 18:53:49 +00:00
private $config;
/**
* Constructor
*
* @param \LAMConfig $config configuration (uses $_SESSION['config'] by default)
*/
public function __construct(&$config = null) {
if ($config == null) {
$config = &$_SESSION['config'];
}
$this->config = &$config;
}
2016-12-24 14:39:02 +00:00
/**
* Returns the configured type with the given id or null.
*
* @param string $typeId type id
* @return \LAM\TYPES\ConfiguredType|NULL type
*/
2016-12-24 12:04:31 +00:00
public function getConfiguredType($typeId) {
2017-05-13 09:07:54 +00:00
if ($this->config == null) {
return null;
}
2016-12-28 18:53:49 +00:00
$activeTypes = $this->config->get_ActiveTypes();
2016-12-24 12:04:31 +00:00
if (in_array($typeId, $activeTypes)) {
return $this->buildConfiguredType($typeId);
}
return null;
}
2016-12-23 19:58:01 +00:00
/**
* Returns a list of configured account types.
*
* @return \LAM\TYPES\ConfiguredType[] list of ConfiguredType
*/
public function getConfiguredTypes() {
$configuredTypes = array();
2016-12-28 18:53:49 +00:00
$activeTypes = $this->config->get_ActiveTypes();
2016-12-23 19:58:01 +00:00
foreach ($activeTypes as $typeId) {
2018-08-10 18:02:41 +00:00
$type = $this->buildConfiguredType($typeId);
if ($type === null) {
continue;
}
$configuredTypes[] = $type;
2016-12-23 19:58:01 +00:00
}
return $configuredTypes;
}
2016-12-31 10:13:36 +00:00
/**
* Returns a list of configured types for this scope.
*
* @param string $scope scope (e.g. user)
* @return \LAM\TYPES\ConfiguredType[] list of ConfiguredType
*/
public function getConfiguredTypesForScope($scope) {
$allTypes = $this->getConfiguredTypes();
$scopedTypes = array();
foreach ($allTypes as $type) {
if ($type->getScope() == $scope) {
$scopedTypes[] = $type;
}
}
return $scopedTypes;
}
2017-03-16 18:44:01 +00:00
/**
* Returns a list of configured types for these scopes.
*
* @param array $scopes scopes (e.g. user)
* @return \LAM\TYPES\ConfiguredType[] list of ConfiguredType
*/
public function getConfiguredTypesForScopes($scopes) {
$allTypes = $this->getConfiguredTypes();
$scopedTypes = array();
foreach ($allTypes as $type) {
if (in_array($type->getScope(), $scopes)) {
$scopedTypes[] = $type;
}
}
return $scopedTypes;
}
2016-12-23 19:58:01 +00:00
/**
* Builds a configured account type.
*
* @param string $typeId type id
*/
private function buildConfiguredType($typeId) {
2016-12-28 18:53:49 +00:00
$scope = getScopeFromTypeId($typeId);
2018-08-10 18:02:41 +00:00
if (!class_exists($scope)) {
return null;
}
2017-01-06 10:38:52 +00:00
return new ConfiguredType($this, $scope, $typeId);
2016-12-23 19:58:01 +00:00
}
2016-12-28 18:53:49 +00:00
/**
* Generates a new unique type id for the given scope.
*
* @param string $scope account type (e.g. user)
*/
public function generateNewTypeId($scope) {
$activeTypes = $this->config->get_ActiveTypes();
if (!in_array($scope, $activeTypes)) {
return $scope;
}
$counter = 1;
while (in_array($scope . '_' . $counter, $activeTypes)) {
$counter++;
}
return $scope . '_' . $counter;
}
2017-01-03 19:02:29 +00:00
/**
* Returns the associated config object.
*
* @return \LAMConfig config
*/
public function getConfig() {
return $this->config;
}
2017-05-13 09:07:54 +00:00
/**
* Returns if configuration is loaded.
*
* @return boolean configured
*/
public function hasConfig() {
return !empty($this->config);
}
2016-12-23 19:58:01 +00:00
}
2006-01-01 16:30:05 +00:00
?>