From 4dd7985b57082485be79bf7a52dc5fdb7d31d6bd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come.chilliet@fusiondirectory.org>
Date: Tue, 22 Jun 2021 10:41:27 +0200
Subject: [PATCH] :ambulance: fix(core) Move Attribute class to namespace
 \FusionDirectory\Core\SimplePlugin

In a future version we should move all classes to namespace, but for now
 only moving Attribute class to avoid problems with PHP>=8 which have its
 own Attribute class.

issue #6167
---
 include/errors/class_SimplePluginError.inc             |  2 +-
 include/functions.inc                                  |  4 ++++
 .../management/snapshot/class_SnapshotAttribute.inc    |  2 +-
 .../attributes/class_BaseSelectorAttribute.inc         |  2 +-
 .../simpleplugin/attributes/class_BooleanAttribute.inc |  2 +-
 .../attributes/class_CompositeAttribute.inc            |  2 +-
 .../simpleplugin/attributes/class_DateAttribute.inc    |  2 +-
 .../simpleplugin/attributes/class_FileAttribute.inc    |  2 +-
 include/simpleplugin/attributes/class_IntAttribute.inc |  2 +-
 .../simpleplugin/attributes/class_SelectAttribute.inc  |  2 +-
 include/simpleplugin/attributes/class_SetAttribute.inc |  4 ++--
 .../simpleplugin/attributes/class_StringAttribute.inc  |  2 +-
 .../attributes/dialog/class_ButtonAttribute.inc        |  2 +-
 .../attributes/dialog/class_DialogAttribute.inc        |  2 +-
 .../dialog/class_DialogOrderedArrayAttribute.inc       |  2 +-
 include/simpleplugin/class_Attribute.inc               | 10 ++++++----
 include/simpleplugin/class_simplePlugin.inc            |  2 +-
 17 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/include/errors/class_SimplePluginError.inc b/include/errors/class_SimplePluginError.inc
index 0bb239ac5..a2d8ef1ef 100644
--- a/include/errors/class_SimplePluginError.inc
+++ b/include/errors/class_SimplePluginError.inc
@@ -36,7 +36,7 @@ class SimplePluginError extends FusionDirectoryError
 
   public function setOrigin ($origin)
   {
-    if ($origin instanceof Attribute) {
+    if ($origin instanceof \FusionDirectory\Core\SimplePlugin\Attribute) {
       $this->attribute  = $origin;
       $this->tab        = $origin->getParent();
       $this->object     = $this->tab->parent;
diff --git a/include/functions.inc b/include/functions.inc
index c681e4f71..e75dfa313 100644
--- a/include/functions.inc
+++ b/include/functions.inc
@@ -61,6 +61,10 @@ function __fusiondirectory_autoload ($class_name)
     return;
   }
 
+  if (strpos($class_name, 'FusionDirectory\\') === 0) {
+    $class_name = preg_replace('/^.+\\\\([^\\\\]+)$/', '\\1', "$class_name");
+  }
+
   if (isset($class_mapping["$class_name"])) {
     require_once($BASE_DIR.'/'.$class_mapping["$class_name"]);
   } else {
diff --git a/include/management/snapshot/class_SnapshotAttribute.inc b/include/management/snapshot/class_SnapshotAttribute.inc
index 17c22fd8e..895e2e677 100644
--- a/include/management/snapshot/class_SnapshotAttribute.inc
+++ b/include/management/snapshot/class_SnapshotAttribute.inc
@@ -27,7 +27,7 @@ class SnapshotsAttribute extends OrderedArrayAttribute
 {
   function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = [], $acl = '')
   {
-    Attribute::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
+    \FusionDirectory\Core\SimplePlugin\Attribute::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
     $this->edit_enabled = FALSE;
     $this->order        = FALSE;
     $this->attribute    = FALSE;
diff --git a/include/simpleplugin/attributes/class_BaseSelectorAttribute.inc b/include/simpleplugin/attributes/class_BaseSelectorAttribute.inc
index 19644d559..0e02df61e 100644
--- a/include/simpleplugin/attributes/class_BaseSelectorAttribute.inc
+++ b/include/simpleplugin/attributes/class_BaseSelectorAttribute.inc
@@ -21,7 +21,7 @@
 /*! \brief This class allow to handle easily an Base selector attribute
  *
  */
-class BaseSelectorAttribute extends Attribute
+class BaseSelectorAttribute extends \FusionDirectory\Core\SimplePlugin\Attribute
 {
   private $baseSelector = NULL;
   private $orig_dn      = NULL;
diff --git a/include/simpleplugin/attributes/class_BooleanAttribute.inc b/include/simpleplugin/attributes/class_BooleanAttribute.inc
index a89c136ae..4fb458950 100644
--- a/include/simpleplugin/attributes/class_BooleanAttribute.inc
+++ b/include/simpleplugin/attributes/class_BooleanAttribute.inc
@@ -21,7 +21,7 @@
 /*! \brief This class allow to handle easily a Boolean LDAP attribute
  *
  */
-class BooleanAttribute extends Attribute
+class BooleanAttribute extends \FusionDirectory\Core\SimplePlugin\Attribute
 {
   public $trueValue;
   public $falseValue;
diff --git a/include/simpleplugin/attributes/class_CompositeAttribute.inc b/include/simpleplugin/attributes/class_CompositeAttribute.inc
index 4277ed01e..fbf4fda71 100644
--- a/include/simpleplugin/attributes/class_CompositeAttribute.inc
+++ b/include/simpleplugin/attributes/class_CompositeAttribute.inc
@@ -25,7 +25,7 @@
  * you should inherit this class and write your own readValues and writeValues method
  *
  */
-class CompositeAttribute extends Attribute
+class CompositeAttribute extends \FusionDirectory\Core\SimplePlugin\Attribute
 {
   public $attributes;
   protected $readFormat;
diff --git a/include/simpleplugin/attributes/class_DateAttribute.inc b/include/simpleplugin/attributes/class_DateAttribute.inc
index 7cd2c315f..fa7981fce 100644
--- a/include/simpleplugin/attributes/class_DateAttribute.inc
+++ b/include/simpleplugin/attributes/class_DateAttribute.inc
@@ -23,7 +23,7 @@
  *
  * We are using UTC timezone because we don't care about time, we just want date.
  */
-class DateAttribute extends Attribute
+class DateAttribute extends \FusionDirectory\Core\SimplePlugin\Attribute
 {
   protected $format;
   protected $minDate = NULL;
diff --git a/include/simpleplugin/attributes/class_FileAttribute.inc b/include/simpleplugin/attributes/class_FileAttribute.inc
index 05abb5e68..0a3ce8660 100644
--- a/include/simpleplugin/attributes/class_FileAttribute.inc
+++ b/include/simpleplugin/attributes/class_FileAttribute.inc
@@ -21,7 +21,7 @@
 /*! \brief This class allow to handle easily an File LDAP attribute
  *
  */
-class FileAttribute extends Attribute
+class FileAttribute extends \FusionDirectory\Core\SimplePlugin\Attribute
 {
   protected $binary = TRUE;
 
diff --git a/include/simpleplugin/attributes/class_IntAttribute.inc b/include/simpleplugin/attributes/class_IntAttribute.inc
index 077db5bc6..a057292c1 100644
--- a/include/simpleplugin/attributes/class_IntAttribute.inc
+++ b/include/simpleplugin/attributes/class_IntAttribute.inc
@@ -21,7 +21,7 @@
 /*! \brief This class allow to handle easily an Integer LDAP attribute
  *
  */
-class IntAttribute extends Attribute
+class IntAttribute extends \FusionDirectory\Core\SimplePlugin\Attribute
 {
   protected $min;
   protected $max;
diff --git a/include/simpleplugin/attributes/class_SelectAttribute.inc b/include/simpleplugin/attributes/class_SelectAttribute.inc
index 431eafa94..0e67fc98c 100644
--- a/include/simpleplugin/attributes/class_SelectAttribute.inc
+++ b/include/simpleplugin/attributes/class_SelectAttribute.inc
@@ -27,7 +27,7 @@
 /*! \brief This class allow to handle easily a Select LDAP attribute with a set of choices
  *
  */
-class SelectAttribute extends Attribute
+class SelectAttribute extends \FusionDirectory\Core\SimplePlugin\Attribute
 {
   protected $choices;
   protected $outputs  = NULL;
diff --git a/include/simpleplugin/attributes/class_SetAttribute.inc b/include/simpleplugin/attributes/class_SetAttribute.inc
index dadfc1799..cb0a7de62 100644
--- a/include/simpleplugin/attributes/class_SetAttribute.inc
+++ b/include/simpleplugin/attributes/class_SetAttribute.inc
@@ -21,7 +21,7 @@
 /*! \brief This class allow to handle easily a multi-valuated attribute
  *
  */
-class SetAttribute extends Attribute
+class SetAttribute extends \FusionDirectory\Core\SimplePlugin\Attribute
 {
   public $attribute;
   protected $valueUnicity     = TRUE;
@@ -35,7 +35,7 @@ class SetAttribute extends Attribute
    *  \param array $values The default values
    *  \param boolean $valueUnicity Should the value unicity be checked
    */
-  function __construct (Attribute $attribute, array $values = [], bool $valueUnicity = TRUE)
+  function __construct (\FusionDirectory\Core\SimplePlugin\Attribute $attribute, array $values = [], bool $valueUnicity = TRUE)
   {
     parent::__construct(
       $attribute->getLabel(),     $attribute->getDescription(),
diff --git a/include/simpleplugin/attributes/class_StringAttribute.inc b/include/simpleplugin/attributes/class_StringAttribute.inc
index e5cf499b3..ca3ab88bf 100644
--- a/include/simpleplugin/attributes/class_StringAttribute.inc
+++ b/include/simpleplugin/attributes/class_StringAttribute.inc
@@ -21,7 +21,7 @@
 /*! \brief This class allow to handle easily a String LDAP attribute
  *
  */
-class StringAttribute extends Attribute
+class StringAttribute extends \FusionDirectory\Core\SimplePlugin\Attribute
 {
   protected $pattern;
   protected $example;
diff --git a/include/simpleplugin/attributes/dialog/class_ButtonAttribute.inc b/include/simpleplugin/attributes/dialog/class_ButtonAttribute.inc
index 5861fdafa..dbe84570e 100644
--- a/include/simpleplugin/attributes/dialog/class_ButtonAttribute.inc
+++ b/include/simpleplugin/attributes/dialog/class_ButtonAttribute.inc
@@ -21,7 +21,7 @@
 /*!
  * \brief Attribute showing a button
  */
-class ButtonAttribute extends Attribute
+class ButtonAttribute extends \FusionDirectory\Core\SimplePlugin\Attribute
 {
   protected $buttonText = NULL;
   protected $action;
diff --git a/include/simpleplugin/attributes/dialog/class_DialogAttribute.inc b/include/simpleplugin/attributes/dialog/class_DialogAttribute.inc
index c0bca3ee9..a44d79deb 100644
--- a/include/simpleplugin/attributes/dialog/class_DialogAttribute.inc
+++ b/include/simpleplugin/attributes/dialog/class_DialogAttribute.inc
@@ -39,7 +39,7 @@ abstract class DialogAttribute extends SetAttribute
    */
   function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = [], $acl = "")
   {
-    Attribute::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
+    \FusionDirectory\Core\SimplePlugin\Attribute::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
     $this->attribute = FALSE;
   }
 
diff --git a/include/simpleplugin/attributes/dialog/class_DialogOrderedArrayAttribute.inc b/include/simpleplugin/attributes/dialog/class_DialogOrderedArrayAttribute.inc
index fc667d022..7818821a0 100644
--- a/include/simpleplugin/attributes/dialog/class_DialogOrderedArrayAttribute.inc
+++ b/include/simpleplugin/attributes/dialog/class_DialogOrderedArrayAttribute.inc
@@ -27,7 +27,7 @@ abstract class DialogOrderedArrayAttribute extends OrderedArrayAttribute
 
   function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = [], $acl = "")
   {
-    Attribute::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
+    \FusionDirectory\Core\SimplePlugin\Attribute::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
     $this->edit_enabled = TRUE;
     $this->attribute    = FALSE;
   }
diff --git a/include/simpleplugin/class_Attribute.inc b/include/simpleplugin/class_Attribute.inc
index 2d298f490..0bf4242a9 100644
--- a/include/simpleplugin/class_Attribute.inc
+++ b/include/simpleplugin/class_Attribute.inc
@@ -19,6 +19,8 @@
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */
 
+namespace FusionDirectory\Core\SimplePlugin;
+
 /*!
  * \file class_Attribute.inc
  * Source code for the main Attribute class
@@ -819,7 +821,7 @@ class Attribute
 /*!
  * \brief Attribute hidden from the user
  */
-class HiddenAttribute extends Attribute
+class HiddenAttribute extends \FusionDirectory\Core\SimplePlugin\Attribute
 {
   /*! \brief The constructor of HiddenAttribute
    *
@@ -868,7 +870,7 @@ class HiddenArrayAttribute extends HiddenAttribute
 /*!
  * \brief Dummy attribute class in order to give stats information to the template
  */
-class FakeAttribute extends Attribute
+class FakeAttribute extends \FusionDirectory\Core\SimplePlugin\Attribute
 {
   function __construct (string $ldapName)
   {
@@ -887,7 +889,7 @@ class FakeAttribute extends Attribute
  * It can be used to display an attribute value the user is never allowed to modify.
  * (But FD might edit it)
  */
-class DisplayLDAPAttribute extends Attribute
+class DisplayLDAPAttribute extends \FusionDirectory\Core\SimplePlugin\Attribute
 {
   protected $allowHTML    = FALSE;
   protected $allowSmarty  = FALSE;
@@ -932,7 +934,7 @@ class ReadOnlyLDAPAttribute extends DisplayLDAPAttribute
  *
  * It can be used to display an attribute value the user is never allowed to modify.
  */
-class DisplayLDAPArrayAttribute extends Attribute
+class DisplayLDAPArrayAttribute extends \FusionDirectory\Core\SimplePlugin\Attribute
 {
   protected function loadAttrValue (array $attrs)
   {
diff --git a/include/simpleplugin/class_simplePlugin.inc b/include/simpleplugin/class_simplePlugin.inc
index 3efe722f6..00dcaa3d0 100644
--- a/include/simpleplugin/class_simplePlugin.inc
+++ b/include/simpleplugin/class_simplePlugin.inc
@@ -502,7 +502,7 @@ class simplePlugin implements SimpleTab
     return $attr.'='.ldap_escape_dn($this->attributesAccess[$attr]->computeLdapValue()).','.$ou.$base;
   }
 
-  protected function addAttribute (string $section, Attribute $attr)
+  protected function addAttribute (string $section, \FusionDirectory\Core\SimplePlugin\Attribute $attr)
   {
     $name = $attr->getLdapName();
     $this->attributesInfo[$section]['attrs'][$name] = $attr;
-- 
GitLab