From 770533b7ecbe531a9f4494094a80ba72df2f34a2 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Mon, 30 Nov 2020 19:37:44 +0100 Subject: [PATCH] Ensure attributes values are always an array in LSattribute & LSattr_ldap --- src/includes/class/class.LSattr_ldap.php | 6 +- .../class/class.LSattr_ldap_boolean.php | 29 +++++---- ...class.LSattr_ldap_compositeValueToJSON.php | 38 +++++------- src/includes/class/class.LSattr_ldap_date.php | 23 +++---- .../class/class.LSattr_ldap_naiveDate.php | 15 ++--- .../class/class.LSattr_ldap_password.php | 61 ++++++------------- .../class/class.LSattr_ldap_postaladdress.php | 10 ++- .../class.LSattr_ldap_sambaAcctFlags.php | 7 +-- .../class/class.LSattr_ldap_shadowExpire.php | 12 ++-- src/includes/class/class.LSattribute.php | 36 +++-------- src/includes/functions.php | 8 +++ 11 files changed, 95 insertions(+), 150 deletions(-) diff --git a/src/includes/class/class.LSattr_ldap.php b/src/includes/class/class.LSattr_ldap.php index 59cbd055..11da7bea 100644 --- a/src/includes/class/class.LSattr_ldap.php +++ b/src/includes/class/class.LSattr_ldap.php @@ -70,7 +70,7 @@ class LSattr_ldap extends LSlog_staticLoggerClass { * @retval mixed La valeur traitée de l'attribut */ public function getUpdateData($data) { - return $data; + return ensureIsArray($data); } /** @@ -81,7 +81,7 @@ class LSattr_ldap extends LSlog_staticLoggerClass { * @retval mixed La valeur d'affichage de l'attribut */ public function getDisplayValue($data) { - return $data; + return ensureIsArray($data); } /** @@ -93,7 +93,7 @@ class LSattr_ldap extends LSlog_staticLoggerClass { * @retval boolean True uniquement si la valeur passer en paramètre différe de l'actuelle */ public function isUpdated($data) { - $data=$this -> getUpdateData($data); + $data = $this -> getUpdateData($data); if ($this -> attribute -> data != $data) { return true; } diff --git a/src/includes/class/class.LSattr_ldap_boolean.php b/src/includes/class/class.LSattr_ldap_boolean.php index ec03d694..4de08d46 100644 --- a/src/includes/class/class.LSattr_ldap_boolean.php +++ b/src/includes/class/class.LSattr_ldap_boolean.php @@ -48,11 +48,14 @@ class LSattr_ldap_boolean extends LSattr_ldap { * @retval mixed Attribute data **/ public function getUpdateData($data) { - if ($data[0]=='yes') { - return array($this -> getTrue()); - } - if ($data[0]=='no') { - return array($this -> getFalse()); + $data = ensureIsArray($data); + if ($data) { + if ($data[0] == 'yes') { + return array($this -> getTrue()); + } + if ($data[0] == 'no') { + return array($this -> getFalse()); + } } return array(); } @@ -65,13 +68,11 @@ class LSattr_ldap_boolean extends LSattr_ldap { * @retval boolean True or False **/ public function isTrue($data) { - if (!is_array($data)) { - $data=array($data); - } - if ($data[0] == $this -> getTrue()) { + $data = ensureIsArray($data); + if ($data && $data[0] == $this -> getTrue()) { return true; } - return; + return false; } /** @@ -82,13 +83,11 @@ class LSattr_ldap_boolean extends LSattr_ldap { * @retval boolean True or False **/ public function isFalse($data) { - if (!is_array($data)) { - $data=array($data); - } - if ($data[0] == $this -> getFalse()) { + $data = ensureIsArray($data); + if ($data && $data[0] == $this -> getFalse()) { return true; } - return; + return false; } /** diff --git a/src/includes/class/class.LSattr_ldap_compositeValueToJSON.php b/src/includes/class/class.LSattr_ldap_compositeValueToJSON.php index 5bac32c3..1d8e19ba 100644 --- a/src/includes/class/class.LSattr_ldap_compositeValueToJSON.php +++ b/src/includes/class/class.LSattr_ldap_compositeValueToJSON.php @@ -37,15 +37,10 @@ class LSattr_ldap_compositeValueToJSON extends LSattr_ldap { * @retval mixed La valeur d'affichage de l'attribut */ public function getDisplayValue($data) { - if ($data) { - if (!is_array($data)) - $data = array($data); - $ret = array(); - foreach($data as $key => $val) - $ret[$key] = json_encode(self :: parseValue($val)); - return $ret; - } - return $data; + $ret = array(); + foreach(ensureIsArray($data) as $key => $val) + $ret[$key] = json_encode(self :: parseValue($val)); + return $ret; } /** @@ -56,22 +51,17 @@ class LSattr_ldap_compositeValueToJSON extends LSattr_ldap { * @retval mixed La valeur traitée de l'attribut */ public function getUpdateData($data) { - if ($data) { - if (!is_array($data)) - $data = array($data); - $ret = array(); - foreach($data as $key => $val) - $ret[$key] = self :: encodeValue(json_decode($val, true)); - return $ret; - } - return $data; + $ret = array(); + foreach(ensureIsArray($data) as $key => $val) + $ret[$key] = self :: encodeValue(json_decode($val, true)); + return $ret; } public static function parseValue($value) { - if (preg_match_all('/\[([^=]*)=([^\]]*)\]/',$value,$matches)) { - $parseValue=array(); - for($i=0;$i $val) - $ret.="[$key=$val]"; + $ret .= "[$key=$val]"; return $ret; } return False; diff --git a/src/includes/class/class.LSattr_ldap_date.php b/src/includes/class/class.LSattr_ldap_date.php index 9a818922..d7b19b59 100644 --- a/src/includes/class/class.LSattr_ldap_date.php +++ b/src/includes/class/class.LSattr_ldap_date.php @@ -34,9 +34,7 @@ class LSattr_ldap_date extends LSattr_ldap { * @retval mixed La valeur d'affichage de l'attribut */ public function getDisplayValue($data) { - if(!is_array($data)) { - $data=array($data); - } + $data = ensureIsArray($data); if ($this -> getConfig('ldap_options.timestamp', false, 'bool')) { return $data; } @@ -58,22 +56,21 @@ class LSattr_ldap_date extends LSattr_ldap { * @retval mixed La valeur traitée de l'attribut */ public function getUpdateData($data) { + $data = ensureIsArray($data); if ($this -> getConfig('ldap_options.timestamp', false, 'bool')) { return $data; } $timezone = timezone_open($this -> getConfig('ldap_options.timezone', 'UTC', 'string')); - $retval=array(); - if(is_array($data)) { - foreach($data as $val) { - $datetime = date_create("@$val"); - $datetime -> setTimezone($timezone); - $datetime_string = $datetime -> format($this -> getFormat()); + $retval = array(); + foreach($data as $val) { + $datetime = date_create("@$val"); + $datetime -> setTimezone($timezone); + $datetime_string = $datetime -> format($this -> getFormat()); - // Replace +0000 or -0000 end by Z - $datetime_string = preg_replace('/[\+\-]0000$/', 'Z', $datetime_string); + // Replace +0000 or -0000 end by Z + $datetime_string = preg_replace('/[\+\-]0000$/', 'Z', $datetime_string); - $retval[] = $datetime_string; - } + $retval[] = $datetime_string; } return $retval; } diff --git a/src/includes/class/class.LSattr_ldap_naiveDate.php b/src/includes/class/class.LSattr_ldap_naiveDate.php index 1c25c6a3..a0b43033 100644 --- a/src/includes/class/class.LSattr_ldap_naiveDate.php +++ b/src/includes/class/class.LSattr_ldap_naiveDate.php @@ -38,11 +38,8 @@ class LSattr_ldap_naiveDate extends LSattr_ldap { * @retval mixed The display value ot the attribute */ public function getDisplayValue($data) { - if(!is_array($data)) { - $data=array($data); - } - $retval=array(); - foreach($data as $val) { + $retval = array(); + foreach(ensureIsArray($data) as $val) { $date = strptime($val, self::FORMAT); if (is_array($date)) { $retval[] = mktime( @@ -66,11 +63,9 @@ class LSattr_ldap_naiveDate extends LSattr_ldap { * @retval mixed The LDAP value of the attribute */ public function getUpdateData($data) { - $retval=array(); - if(is_array($data)) { - foreach($data as $val) { - $retval[] = strftime(self::FORMAT, $val).'Z'; - } + $retval = array(); + foreach(ensureIsArray($data) as $val) { + $retval[] = strftime(self::FORMAT, $val).'Z'; } return $retval; } diff --git a/src/includes/class/class.LSattr_ldap_password.php b/src/includes/class/class.LSattr_ldap_password.php index 27706834..a7c9b8cc 100644 --- a/src/includes/class/class.LSattr_ldap_password.php +++ b/src/includes/class/class.LSattr_ldap_password.php @@ -37,25 +37,17 @@ class LSattr_ldap_password extends LSattr_ldap { */ public function getDisplayValue($data) { if ($this -> getConfig('ldap_options.displayClearValue', false, 'bool')) { - if (is_array($data)) { - $ret=array(); - $wildcardPassword = $this -> getConfig('ldap_options.wildcardPassword'); - $encodedWildcardPassword = $this -> getConfig('ldap_options.encodedWildcardPassword'); - foreach($data as $p) { - if ($p == $wildcardPassword || $p == $encodedWildcardPassword) { - continue; - } - $ret[]=$p; - } - return $ret; - } - else { - return $data; + $ret=array(); + $wildcardPassword = $this -> getConfig('ldap_options.wildcardPassword'); + $encodedWildcardPassword = $this -> getConfig('ldap_options.encodedWildcardPassword'); + foreach(ensureIsArray($data) as $p) { + if ($p == $wildcardPassword || $p == $encodedWildcardPassword) + continue; + $ret[] = $p; } + return $ret; } - else { - return '********'; - } + return array('********'); } /** @@ -69,36 +61,21 @@ class LSattr_ldap_password extends LSattr_ldap { * @retval mixed The value of this attribute to be stocked */ public function getUpdateData($data) { - $this -> clearPassword = $data[0]; - $data=array(); - - $data[]=$this -> encodePassword($this -> clearPassword); + $ret = array(); + foreach(ensureIsArray($data) as $val) { + $this -> clearPassword = $val; + $ret[] = $this -> encodePassword($val); + } // Wildcard Password - $wildcardPassword = $this -> getConfig('ldap_options.wildcardPassword'); - if ($wildcardPassword) { - if (!is_array($wildcardPassword)) { - $data[] = $this -> encodePassword($wildcardPassword); - } - else { - foreach($wildcardPassword as $pwd) { - $data[] = $this -> encodePassword($pwd); - } - } - } + foreach(ensureIsArray($this -> getConfig('ldap_options.wildcardPassword')) as $pwd) + $ret[] = $this -> encodePassword($pwd); // Wildcard Password already encoded - $encodedWildcardPassword = $this -> getConfig('ldap_options.encodedWildcardPassword'); - if ($encodedWildcardPassword) { - if (!is_array($encodedWildcardPassword)) { - $data[] = $encodedWildcardPassword; - } - else { - $data = array_merge($data, $encodedWildcardPassword); - } - } + foreach(ensureIsArray($this -> getConfig('ldap_options.encodedWildcardPassword')) as $pwd) + $ret[] = $pwd; - return $data; + return $ret; } /** diff --git a/src/includes/class/class.LSattr_ldap_postaladdress.php b/src/includes/class/class.LSattr_ldap_postaladdress.php index 2d90409f..e063260d 100644 --- a/src/includes/class/class.LSattr_ldap_postaladdress.php +++ b/src/includes/class/class.LSattr_ldap_postaladdress.php @@ -34,7 +34,10 @@ class LSattr_ldap_postaladdress extends LSattr_ldap { * @retval mixed La valeur d'affichage de l'attribut */ public function getDisplayValue($data) { - return str_replace("$","\n",$data); + $ret = array(); + foreach(ensureIsArray($data) as $val) + $ret[] = str_replace("$", "\n", $val); + return $ret; } /** @@ -45,7 +48,10 @@ class LSattr_ldap_postaladdress extends LSattr_ldap { * @retval mixed La valeur traitée de l'attribut */ public function getUpdateData($data) { - return str_replace("\n","$",$data); + $ret = array(); + foreach(ensureIsArray($data) as $val) + $ret[] = str_replace("\n", "$", $val);; + return $ret; } } diff --git a/src/includes/class/class.LSattr_ldap_sambaAcctFlags.php b/src/includes/class/class.LSattr_ldap_sambaAcctFlags.php index cde24f6a..4dc1c77a 100644 --- a/src/includes/class/class.LSattr_ldap_sambaAcctFlags.php +++ b/src/includes/class/class.LSattr_ldap_sambaAcctFlags.php @@ -61,9 +61,7 @@ class LSattr_ldap_sambaAcctFlags extends LSattr_ldap { * @retval array Array of enabled flags **/ public static function parse_flags($data) { - if (!is_array($data)) { - $data = array($data); - } + $data = ensureIsArray($data); if (count($data) > 1) { LSerror :: addErrorCode('LSattr_ldap_sambaAcctFlags_01'); return; @@ -99,8 +97,7 @@ class LSattr_ldap_sambaAcctFlags extends LSattr_ldap { * @retval array Array of LDAP attribute value **/ public static function format_flags($flags) { - if (!is_array($flags)) - $flags = array($flags); + $flags = ensureIsArray($flags); foreach($flags as $flag) { if (!self :: check_flag($flag)) { LSerror :: addErrorCode('LSattr_ldap_sambaAcctFlags_03', $flag); diff --git a/src/includes/class/class.LSattr_ldap_shadowExpire.php b/src/includes/class/class.LSattr_ldap_shadowExpire.php index bc8333b2..fd3f91f9 100644 --- a/src/includes/class/class.LSattr_ldap_shadowExpire.php +++ b/src/includes/class/class.LSattr_ldap_shadowExpire.php @@ -36,10 +36,9 @@ class LSattr_ldap_shadowExpire extends LSattr_ldap { * @retval mixed Array of timestamp */ public function getDisplayValue($data) { - $ret=array(); - foreach($data as $val) { - $ret[]=((int)$val)*86400; - } + $ret = array(); + foreach(ensureIsArray($data) as $val) + $ret[] = intval($val)*86400; return $ret; } @@ -52,9 +51,8 @@ class LSattr_ldap_shadowExpire extends LSattr_ldap { */ public function getUpdateData($data) { $ret=array(); - foreach($data as $val) { - $ret[]=(string)round(((int)$val)/86400); - } + foreach(ensureIsArray($data) as $val) + $ret[] = strval(round(intval($val) / 86400)); return $ret; } diff --git a/src/includes/class/class.LSattribute.php b/src/includes/class/class.LSattribute.php index 19812c93..2c917503 100644 --- a/src/includes/class/class.LSattribute.php +++ b/src/includes/class/class.LSattribute.php @@ -115,11 +115,7 @@ class LSattribute extends LSlog_staticLoggerClass { * @retval boolean true */ public function loadData($attr_data) { - if (is_empty($attr_data) || $attr_data === false) - $attr_data = null; - elseif (!is_array($attr_data)) - $attr_data = array($attr_data); - $this -> data = $attr_data; + $this -> data = ensureIsArray($attr_data); return true; } @@ -131,12 +127,9 @@ class LSattribute extends LSlog_staticLoggerClass { * @retval boolean true */ public function reloadData($attr_data) { - if ((!is_array($attr_data))&&(!empty($attr_data))) { - $attr_data = array($attr_data); - } - $this -> data = $attr_data; - $this -> updateData=false; - $this -> is_validate=false; + $this -> data = ensureIsArray($attr_data); + $this -> updateData = false; + $this -> is_validate = false; return true; } @@ -392,14 +385,7 @@ class LSattribute extends LSlog_staticLoggerClass { * @retval string La valeur a afficher dans le formulaire. */ public function getFormVal() { - $data=$this -> html -> getFormVal($this -> data); - if ($data==NULL) { - $data=array(); - } - if(!is_array($data)) { - $data=array($data); - } - return $data; + return ensureIsArray($this -> html -> getFormVal($this -> data)); } /** @@ -413,7 +399,7 @@ class LSattribute extends LSlog_staticLoggerClass { */ public function setUpdateData($data) { if($this -> ldap -> isUpdated($data)) { - $this -> updateData=$data; + $this -> updateData = ensureIsArray($data); } } @@ -499,15 +485,7 @@ class LSattribute extends LSlog_staticLoggerClass { $value = $this -> ldapObject -> getFData($format); } if ($value !== false) { - if (!empty($value)) { - if (!is_array($value)) { - $value=array($value); - } - $this -> updateData=$value; - } - else { - $this -> updateData=array(); - } + $this -> updateData = ensureIsArray($value); return true; } return; diff --git a/src/includes/functions.php b/src/includes/functions.php index 19bbfbf5..0205bdb1 100644 --- a/src/includes/functions.php +++ b/src/includes/functions.php @@ -782,3 +782,11 @@ function is_empty($val) { } return empty($val); } + +function ensureIsArray($value) { + if (is_array($value)) + return $value; + if (is_empty($value)) + return array(); + return array($value); +}