43 private $inLdap = TRUE;
52 private $unique = FALSE;
56 private $uniqueFilter = NULL;
59 protected $htmlid_prefix =
'';
61 protected $visible = TRUE;
65 protected $disabled = FALSE;
68 protected $submitForm = FALSE;
74 protected $defaultValue;
76 protected $initialValue;
81 protected $managedAttributes = [];
83 protected $managedAttributesMultipleValues = [];
86 protected $managingAttributesOrders = [];
92 protected $isSubAttribute = FALSE;
103 function __construct (
string $label,
string $description,
string $ldapName,
bool $required = FALSE, $defaultValue =
'',
string $acl =
'')
105 $this->label = $label;
106 $this->description = $description;
107 $this->ldapName = $ldapName;
108 $this->required = $required;
109 $this->defaultValue = $defaultValue;
110 $this->value = $defaultValue;
111 $this->postValue = $this->value;
113 $this->plugin = NULL;
122 $this->plugin = $plugin;
123 $this->manageAttributes($this->getValue());
132 return $this->plugin;
135 function setIsSubAttribute (
bool $bool)
137 $this->isSubAttribute = $bool;
140 function setInLdap (
bool $inLdap)
142 $this->inLdap = $inLdap;
145 function setVisible (
bool $visible)
147 $this->visible = $visible;
150 function isVisible ():
bool 152 return $this->visible;
155 function isTemplate ():
bool 157 return (is_object($this->plugin) && $this->plugin->is_template);
160 function setUnique ($unique,
string $filter = NULL)
162 if ($unique === TRUE) {
163 trigger_error(
'Deprecated fallback');
164 $this->unique =
'one';
166 $this->unique = $unique;
168 $this->uniqueFilter = $filter;
171 function getUnique ():
string 173 return $this->unique;
176 function isInLdap ():
bool 178 return $this->inLdap;
190 function checkValue ($value)
195 function setValue ($value)
197 $this->checkValue($value);
198 $old_value = $this->value;
199 $this->value = $value;
200 if (($this->submitForm !== FALSE) && ($this->submitForm !== TRUE) && ($old_value != $value) && is_object($this->plugin)) {
201 $func = $this->submitForm;
202 $this->plugin->$func();
204 $this->manageAttributes($this->value);
210 if ($this->isVisible()) {
212 $this->manageAttributes($this->postValue);
226 $this->setValue($this->defaultValue);
235 function getArrayValue (): array
237 return [$this->displayValue($this->getValue())];
240 function getLdapName ():
string 242 return $this->ldapName;
245 function getHtmlId ():
string 247 return $this->htmlid_prefix.\userinfo::sanitizeAttributeName($this->getLdapName());
251 function getForHtmlId ():
string 253 return $this->getHtmlId();
256 function getLabel ():
string 261 function getDescription ():
string 263 return $this->description;
266 function getAcl ():
string 268 if (empty($this->
acl)) {
269 return $this->getHtmlId();
275 function setAcl (
string $acl)
280 function isRequired ():
bool 282 return $this->required;
285 function setRequired (
bool $bool)
287 $this->required = $bool;
290 protected function setLabel (
string $label)
292 $this->label = $label;
295 protected function setDescription (
string $description)
297 $this->description = $description;
300 function setDisabled (
bool $disabled)
302 $this->disabled = $disabled;
305 function isDisabled ():
bool 307 return $this->disabled;
310 function setManagingDisabled ($sender, $value)
312 $this->managingAttributesOrders[$sender] = $value;
313 $this->setDisabled(array_reduce($this->managingAttributesOrders,
321 function setSubmitForm ($submitForm)
323 $this->submitForm = $submitForm;
333 $this->initialValue = $this->getValue();
344 if (isset($attrs[$this->getLdapName()])) {
345 $this->setValue($this->
inputValue($attrs[$this->getLdapName()][0]));
349 function getInitialValue ()
351 return $this->initialValue;
354 function setInitialValue ($value)
356 $this->initialValue = $value;
359 function hasChanged ():
bool 361 return ($this->getValue() !== $this->initialValue);
364 function displayValue ($value):
string 378 function setDefaultValue ($value)
380 $this->defaultValue = $value;
388 if (isset($mAttributes[
'multiplevalues'])) {
389 $this->managedAttributesMultipleValues = $mAttributes[
'multiplevalues'];
390 unset($mAttributes[
'multiplevalues']);
392 $this->managedAttributesMultipleValues = [];
394 $this->managedAttributes = $mAttributes;
395 $this->manageAttributes($this->getValue());
398 protected function isValueManagingValue ($myvalue, $mavalue):
bool 400 if (isset($this->managedAttributesMultipleValues[$mavalue])) {
401 return in_array($myvalue, $this->managedAttributesMultipleValues[$mavalue]);
403 return ($myvalue == $mavalue);
407 function manageAttributes ($myvalue):
bool 409 if ($this->plugin === NULL) {
412 foreach ($this->managedAttributes as $array) {
413 foreach ($array as $value => $attributes) {
414 foreach ($attributes as $attribute) {
415 $disable = $this->isValueManagingValue($myvalue, $value);
416 $this->plugin->attributesAccess[$attribute]->setManagingDisabled($this->getLdapName(), $disable);
427 if ($this->isVisible()) {
428 $this->postValue = $this->value;
429 if (isset($_POST[$this->getHtmlId()])) {
439 if (!$this->disabled && $this->isVisible()) {
440 $this->setValue($this->postValue);
448 return $this->getValue();
457 if ($ldapValue !==
'') {
458 $attrs[$this->getLdapName()] = $ldapValue;
460 $attrs[$this->getLdapName()] = [];
469 foreach ($this->managedAttributes as $prefix => $array) {
470 if ($prefix !=
'erase') {
473 foreach ($array as $value => $attributes) {
474 $myvalue = $this->getValue();
475 $erase = $this->isValueManagingValue($myvalue, $value);
479 foreach ($attributes as $attribute) {
480 $attrs[$attribute] = [];
491 $currentValue = $this->getValue();
492 if ($this->isRequired() && !$this->disabled && (($currentValue ===
"") || ($currentValue === []))) {
493 return new \SimplePluginCheckError(
497 } elseif (($this->unique !== FALSE) && !$this->disabled) {
499 if (($ldapValue ===
"") || ($ldapValue === [])) {
502 $ldap = $config->get_ldap_link();
503 $base = $config->current[
'BASE'];
504 if ($this->unique !==
'whole') {
505 if (isset($this->plugin->base) && !empty($this->plugin->base)) {
506 $base = $this->plugin->base;
507 } elseif (isset($this->plugin->dn) && !empty($this->plugin->dn) && ($this->plugin->dn !=
'new')) {
508 $base =
dn2base($this->plugin->dn);
511 if (is_array($ldapValue)) {
512 $filter =
'(|('.$this->getLdapName().
'='.join(
')('.$this->getLdapName().
'=', array_map(
'ldap_escape_f', $ldapValue)).
'))';
514 $filter =
'('.$this->getLdapName().
'='.ldap_escape_f($ldapValue).
')';
516 $infos = \pluglist::pluginInfos(get_class($this->plugin));
517 if ($this->uniqueFilter === NULL) {
518 $objectTypeFilters = array_map(
521 if (!is_numeric($key)) {
525 $oinfos = \objects::infos($ot);
526 return $oinfos[
'filter'];
531 array_keys($infos[
'plObjectType']),
532 array_values($infos[
'plObjectType'])
535 if (!empty($objectTypeFilters)) {
536 $filters[] =
'(|'.implode($objectTypeFilters).
')';
538 $pluginFilter = call_user_func([get_class($this->plugin),
'getLdapFilter']);
539 if (!empty($pluginFilter)) {
540 $filters[] = $pluginFilter;
543 $filters = [$this->uniqueFilter];
545 $filter =
'(&'.$filter.implode($filters).
')';
546 $branches = array_filter(
550 if (!is_numeric($key)) {
554 $oinfos = \objects::infos($ot);
555 return $oinfos[
'ou'];
560 array_keys($infos[
'plObjectType']),
561 array_values($infos[
'plObjectType'])
565 return ($ou !== FALSE);
569 $ldap->search($filter, [$this->getLdapName()]);
570 while ($attrs = $ldap->fetch()) {
571 if ($attrs[
'dn'] != $this->plugin->dn) {
572 $dn_base = preg_replace(
'/^[^,]+,/',
'', $attrs[
'dn']);
574 if ($this->unique ===
'one') {
576 foreach ($branches as $branch) {
577 if ($branch.$base == $dn_base) {
578 $dn_base = preg_replace(
'/^'.preg_quote($branch,
'/').
'/',
'', $dn_base);
583 } elseif ($this->uniqueFilter === NULL) {
585 foreach ($branches as $branch) {
586 if (preg_match(
'/^'.preg_quote($branch,
'/').
'/', $dn_base)) {
587 $dn_base = preg_replace(
'/^'.preg_quote($branch,
'/').
'/',
'', $dn_base);
592 if (!in_array($dn_base, $config->getDepartmentList())) {
602 return new \SimplePluginCheckError(
609 $filter = \archivedObject::buildUniqueSearchFilter($this->getLdapName(), $ldapValue);
610 $ldap->search($filter, [$this->getLdapName()]);
611 if ($attrs = $ldap->fetch()) {
612 return new \SimplePluginCheckError(
631 function renderAttribute (array &$attributes,
bool $readOnly,
bool $readable,
bool $writable)
633 if ($this->visible) {
635 $currentValue = $this->getValue();
636 if (is_array($currentValue)) {
637 $input =
'{literal}'.implode(
'<br/>', array_map(
'htmlescape', $currentValue)).
'{/literal}';
639 $input =
'{literal}'.htmlescape($currentValue).
'{/literal}';
641 } elseif ($this->isTemplate()) {
642 $input = $this->renderTemplateInput();
644 $input = $this->renderFormInput();
646 $attributes[$this->getLdapName()] = [
647 'htmlid' => $this->getForHtmlId(),
648 'label' =>
'{literal}'.htmlescape($this->getLabel()).
'{/literal}',
649 'description' => ($this->isRequired() ? sprintf(_(
"%s (required)"), $this->getDescription()) : $this->getDescription()),
651 'subattribute' => $this->isSubAttribute,
652 'required' => $this->isRequired(),
653 'readable' => $readable,
654 'writable' => $writable,
666 if (!$form || $this->visible) {
667 $class = get_class($this);
669 while ($class != FALSE) {
671 $class = get_parent_class($class);
674 'htmlid' => $this->getHtmlId(),
675 'label' => $this->getLabel(),
676 'required' => $this->isRequired(),
677 'disabled' => $this->disabled,
678 'description' => $this->getDescription(),
684 $infos[
'inldap'] = $this->isInLdap();
685 $infos[
'visible'] = $this->visible;
686 $infos[
'htmlids'] = $this->htmlIds();
688 $attributes[$this->getLdapName()] = $infos;
698 if ($this->disabled) {
699 return new \SimplePluginError(
701 htmlescape(sprintf(_(
'Attribute %s is disabled, its value could not be set'), $this->getLdapName()))
704 $this->setValue($value);
713 if ($value === NULL) {
714 $value = $this->getValue();
725 return '{render aclName="'.$this->getAcl().
'" acl=$'.$this->getAcl().
"ACL}\n$display\n{/render}";
732 if (empty($this->
acl)) {
734 'name' => $this->getHtmlId(),
735 'desc' => $this->getDescription()
743 protected function changeStateJS ():
string 745 return implode(
'', array_map(
748 return 'changeState('.json_encode($id).
');';
754 public function htmlIds (): array
756 return [$this->getHtmlId()];
759 protected function managedAttributesJS ():
string 762 $id = $this->getHtmlId();
763 foreach ($this->managedAttributes as $array) {
764 foreach ($array as $value => $attributes) {
765 if (isset($this->managedAttributesMultipleValues[$value])) {
766 $js .=
'disableAttributes = inArray(document.getElementById('.json_encode($id).
').value,'.json_encode($this->managedAttributesMultipleValues[$value]).
');';
768 $js .=
'disableAttributes = (document.getElementById('.json_encode($id).
').value == '.json_encode($value).
');'.
"\n";
770 foreach ($attributes as $attribute) {
771 foreach ($this->plugin->attributesAccess[$attribute]->htmlIds() as $htmlId) {
772 $js .=
'if (document.getElementById('.json_encode($htmlId).
')) { document.getElementById('.json_encode($htmlId).
').disabled = disableAttributes; }'.
"\n";
780 function renderFormInput ():
string 782 throw new \FusionDirectoryException(
'Not implemented in base class (abstract method)');
785 function renderTemplateInput ():
string 787 return $this->renderFormInput();
790 function foreignKeyUpdate ($oldvalue, $newvalue, array $source)
792 if ($source[
'MODE'] ==
'move') {
793 if ($newvalue === NULL) {
795 } elseif ($source[
'FIELD'] ==
'dn') {
796 $initialValue = $this->getInitialValue();
797 $initialValue = preg_replace(
'/'.preg_quote($oldvalue,
'/').
'$/', $newvalue, $initialValue, -1, $count);
799 $this->setValue($initialValue);
801 } elseif ($this->getInitialValue() == $oldvalue) {
802 $this->setValue($newvalue);
807 function foreignKeyCheck ($value, array $source):
bool 809 return ($this->getValue() == $value);
812 protected function renderInputField (
string $type,
string $name, array $attributes = [],
bool $smartyEscape = TRUE):
string 814 $input =
'<input type="'.htmlescape($type).
'" '.
815 'name="'.htmlescape($name).
'" id="'.
htmlescape($name).
'"'.
816 ($this->disabled ?
' disabled="disabled"' :
'');
817 foreach ($attributes as $label => $value) {
818 $input .=
' '.$label.
'="'.
htmlescape($value).
'"';
821 return ($smartyEscape ?
'{literal}'.$input.
'{/literal}' : $input);
htmlescape(string $str)
Escape string for HTML output.
fillLdapValue(array &$attrs)
Fill LDAP value in the attrs array.
setParent(&$plugin)
Set the parent plugin for this attribute.
deserializeValue($value)
Apply value from RPC requests.
fillLdapValueHook(array &$attrs)
Post-modify the attrs array if needed (used for erasing managed attributes)
serializeAttribute(array &$attributes, bool $form=TRUE)
Serialize this attribute for RPC requests.
static required($name)
Display error about required field empty.
loadValue(array $attrs)
If in LDAP, loads this attribute value from the attrs array.
fixPostValue($value)
In case a treatment is needed on POSTÂ content.
loadPostValue()
Update this attributes postValue depending of the $_POST values.
loadAttrValue(array $attrs)
Loads this attribute value from the attrs array if present.
applyPostValue()
Apply this attribute postValue in value if this attribute is enabled.
dn2base($dn, $ou=NULL)
Return the base of a given DN.
static duplicated($name, $dn=NULL)
Display error about existing entry in the system.
renderAttribute(array &$attributes, bool $readOnly, bool $readable, bool $writable)
Render this attribute form input(s)
getExample()
Get an example value.
check()
Check the correctness of this attribute.
setPostValue($value)
Set the postValue.
This class contains all the function needed to manage acl.
serializeValue($value=NULL)
Serialize value for RPC requests.
getAclInfo()
Get ACL information about the ACL we need to create.
renderAcl(string $display)
Add ACL information around display.
computeLdapValue()
Computes LDAP value.
setManagedAttributes(array $mAttributes)
Set a list of attributes that are managed by this attributes. See FusionDirectory wiki for detailed d...
inputValue($ldapValue)
Return the ldap value in the correct intern format value.
getParent()
Get parent plugin instance, if any.
resetToDefault()
Reset this attribute to its default value.
This class allow to handle easily any kind of LDAP attribute.
class_available($name)
Checks if a class is available.
__construct(string $label, string $description, string $ldapName, bool $required=FALSE, $defaultValue='', string $acl='')
The constructor of Attribute.