Ensure attributes values are always an array in LSattribute & LSattr_ldap

This commit is contained in:
Benjamin Renard 2020-11-30 19:37:44 +01:00
parent f7231229f5
commit 770533b7ec
11 changed files with 95 additions and 150 deletions

View file

@ -70,7 +70,7 @@ class LSattr_ldap extends LSlog_staticLoggerClass {
* @retval mixed La valeur traitée de l'attribut * @retval mixed La valeur traitée de l'attribut
*/ */
public function getUpdateData($data) { 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 * @retval mixed La valeur d'affichage de l'attribut
*/ */
public function getDisplayValue($data) { 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 * @retval boolean True uniquement si la valeur passer en paramètre différe de l'actuelle
*/ */
public function isUpdated($data) { public function isUpdated($data) {
$data=$this -> getUpdateData($data); $data = $this -> getUpdateData($data);
if ($this -> attribute -> data != $data) { if ($this -> attribute -> data != $data) {
return true; return true;
} }

View file

@ -48,11 +48,14 @@ class LSattr_ldap_boolean extends LSattr_ldap {
* @retval mixed Attribute data * @retval mixed Attribute data
**/ **/
public function getUpdateData($data) { public function getUpdateData($data) {
if ($data[0]=='yes') { $data = ensureIsArray($data);
return array($this -> getTrue()); if ($data) {
} if ($data[0] == 'yes') {
if ($data[0]=='no') { return array($this -> getTrue());
return array($this -> getFalse()); }
if ($data[0] == 'no') {
return array($this -> getFalse());
}
} }
return array(); return array();
} }
@ -65,13 +68,11 @@ class LSattr_ldap_boolean extends LSattr_ldap {
* @retval boolean True or False * @retval boolean True or False
**/ **/
public function isTrue($data) { public function isTrue($data) {
if (!is_array($data)) { $data = ensureIsArray($data);
$data=array($data); if ($data && $data[0] == $this -> getTrue()) {
}
if ($data[0] == $this -> getTrue()) {
return true; return true;
} }
return; return false;
} }
/** /**
@ -82,13 +83,11 @@ class LSattr_ldap_boolean extends LSattr_ldap {
* @retval boolean True or False * @retval boolean True or False
**/ **/
public function isFalse($data) { public function isFalse($data) {
if (!is_array($data)) { $data = ensureIsArray($data);
$data=array($data); if ($data && $data[0] == $this -> getFalse()) {
}
if ($data[0] == $this -> getFalse()) {
return true; return true;
} }
return; return false;
} }
/** /**

View file

@ -37,15 +37,10 @@ class LSattr_ldap_compositeValueToJSON extends LSattr_ldap {
* @retval mixed La valeur d'affichage de l'attribut * @retval mixed La valeur d'affichage de l'attribut
*/ */
public function getDisplayValue($data) { public function getDisplayValue($data) {
if ($data) { $ret = array();
if (!is_array($data)) foreach(ensureIsArray($data) as $key => $val)
$data = array($data); $ret[$key] = json_encode(self :: parseValue($val));
$ret = array(); return $ret;
foreach($data as $key => $val)
$ret[$key] = json_encode(self :: parseValue($val));
return $ret;
}
return $data;
} }
/** /**
@ -56,22 +51,17 @@ class LSattr_ldap_compositeValueToJSON extends LSattr_ldap {
* @retval mixed La valeur traitée de l'attribut * @retval mixed La valeur traitée de l'attribut
*/ */
public function getUpdateData($data) { public function getUpdateData($data) {
if ($data) { $ret = array();
if (!is_array($data)) foreach(ensureIsArray($data) as $key => $val)
$data = array($data); $ret[$key] = self :: encodeValue(json_decode($val, true));
$ret = array(); return $ret;
foreach($data as $key => $val)
$ret[$key] = self :: encodeValue(json_decode($val, true));
return $ret;
}
return $data;
} }
public static function parseValue($value) { public static function parseValue($value) {
if (preg_match_all('/\[([^=]*)=([^\]]*)\]/',$value,$matches)) { if (preg_match_all('/\[([^=]*)=([^\]]*)\]/', $value, $matches)) {
$parseValue=array(); $parseValue = array();
for($i=0;$i<count($matches[0]);$i++) { for($i=0; $i<count($matches[0]); $i++) {
$parseValue[$matches[1][$i]]=$matches[2][$i]; $parseValue[$matches[1][$i]] = $matches[2][$i];
} }
return $parseValue; return $parseValue;
} }
@ -80,9 +70,9 @@ class LSattr_ldap_compositeValueToJSON extends LSattr_ldap {
public static function encodeValue($value) { public static function encodeValue($value) {
if (is_array($value)) { if (is_array($value)) {
$ret=""; $ret = "";
foreach($value as $key => $val) foreach($value as $key => $val)
$ret.="[$key=$val]"; $ret .= "[$key=$val]";
return $ret; return $ret;
} }
return False; return False;

View file

@ -34,9 +34,7 @@ class LSattr_ldap_date extends LSattr_ldap {
* @retval mixed La valeur d'affichage de l'attribut * @retval mixed La valeur d'affichage de l'attribut
*/ */
public function getDisplayValue($data) { public function getDisplayValue($data) {
if(!is_array($data)) { $data = ensureIsArray($data);
$data=array($data);
}
if ($this -> getConfig('ldap_options.timestamp', false, 'bool')) { if ($this -> getConfig('ldap_options.timestamp', false, 'bool')) {
return $data; return $data;
} }
@ -58,22 +56,21 @@ class LSattr_ldap_date extends LSattr_ldap {
* @retval mixed La valeur traitée de l'attribut * @retval mixed La valeur traitée de l'attribut
*/ */
public function getUpdateData($data) { public function getUpdateData($data) {
$data = ensureIsArray($data);
if ($this -> getConfig('ldap_options.timestamp', false, 'bool')) { if ($this -> getConfig('ldap_options.timestamp', false, 'bool')) {
return $data; return $data;
} }
$timezone = timezone_open($this -> getConfig('ldap_options.timezone', 'UTC', 'string')); $timezone = timezone_open($this -> getConfig('ldap_options.timezone', 'UTC', 'string'));
$retval=array(); $retval = array();
if(is_array($data)) { foreach($data as $val) {
foreach($data as $val) { $datetime = date_create("@$val");
$datetime = date_create("@$val"); $datetime -> setTimezone($timezone);
$datetime -> setTimezone($timezone); $datetime_string = $datetime -> format($this -> getFormat());
$datetime_string = $datetime -> format($this -> getFormat());
// Replace +0000 or -0000 end by Z // Replace +0000 or -0000 end by Z
$datetime_string = preg_replace('/[\+\-]0000$/', 'Z', $datetime_string); $datetime_string = preg_replace('/[\+\-]0000$/', 'Z', $datetime_string);
$retval[] = $datetime_string; $retval[] = $datetime_string;
}
} }
return $retval; return $retval;
} }

View file

@ -38,11 +38,8 @@ class LSattr_ldap_naiveDate extends LSattr_ldap {
* @retval mixed The display value ot the attribute * @retval mixed The display value ot the attribute
*/ */
public function getDisplayValue($data) { public function getDisplayValue($data) {
if(!is_array($data)) { $retval = array();
$data=array($data); foreach(ensureIsArray($data) as $val) {
}
$retval=array();
foreach($data as $val) {
$date = strptime($val, self::FORMAT); $date = strptime($val, self::FORMAT);
if (is_array($date)) { if (is_array($date)) {
$retval[] = mktime( $retval[] = mktime(
@ -66,11 +63,9 @@ class LSattr_ldap_naiveDate extends LSattr_ldap {
* @retval mixed The LDAP value of the attribute * @retval mixed The LDAP value of the attribute
*/ */
public function getUpdateData($data) { public function getUpdateData($data) {
$retval=array(); $retval = array();
if(is_array($data)) { foreach(ensureIsArray($data) as $val) {
foreach($data as $val) { $retval[] = strftime(self::FORMAT, $val).'Z';
$retval[] = strftime(self::FORMAT, $val).'Z';
}
} }
return $retval; return $retval;
} }

View file

@ -37,25 +37,17 @@ class LSattr_ldap_password extends LSattr_ldap {
*/ */
public function getDisplayValue($data) { public function getDisplayValue($data) {
if ($this -> getConfig('ldap_options.displayClearValue', false, 'bool')) { if ($this -> getConfig('ldap_options.displayClearValue', false, 'bool')) {
if (is_array($data)) { $ret=array();
$ret=array(); $wildcardPassword = $this -> getConfig('ldap_options.wildcardPassword');
$wildcardPassword = $this -> getConfig('ldap_options.wildcardPassword'); $encodedWildcardPassword = $this -> getConfig('ldap_options.encodedWildcardPassword');
$encodedWildcardPassword = $this -> getConfig('ldap_options.encodedWildcardPassword'); foreach(ensureIsArray($data) as $p) {
foreach($data as $p) { if ($p == $wildcardPassword || $p == $encodedWildcardPassword)
if ($p == $wildcardPassword || $p == $encodedWildcardPassword) { continue;
continue; $ret[] = $p;
}
$ret[]=$p;
}
return $ret;
}
else {
return $data;
} }
return $ret;
} }
else { return array('********');
return '********';
}
} }
/** /**
@ -69,36 +61,21 @@ class LSattr_ldap_password extends LSattr_ldap {
* @retval mixed The value of this attribute to be stocked * @retval mixed The value of this attribute to be stocked
*/ */
public function getUpdateData($data) { public function getUpdateData($data) {
$this -> clearPassword = $data[0]; $ret = array();
$data=array(); foreach(ensureIsArray($data) as $val) {
$this -> clearPassword = $val;
$data[]=$this -> encodePassword($this -> clearPassword); $ret[] = $this -> encodePassword($val);
}
// Wildcard Password // Wildcard Password
$wildcardPassword = $this -> getConfig('ldap_options.wildcardPassword'); foreach(ensureIsArray($this -> getConfig('ldap_options.wildcardPassword')) as $pwd)
if ($wildcardPassword) { $ret[] = $this -> encodePassword($pwd);
if (!is_array($wildcardPassword)) {
$data[] = $this -> encodePassword($wildcardPassword);
}
else {
foreach($wildcardPassword as $pwd) {
$data[] = $this -> encodePassword($pwd);
}
}
}
// Wildcard Password already encoded // Wildcard Password already encoded
$encodedWildcardPassword = $this -> getConfig('ldap_options.encodedWildcardPassword'); foreach(ensureIsArray($this -> getConfig('ldap_options.encodedWildcardPassword')) as $pwd)
if ($encodedWildcardPassword) { $ret[] = $pwd;
if (!is_array($encodedWildcardPassword)) {
$data[] = $encodedWildcardPassword;
}
else {
$data = array_merge($data, $encodedWildcardPassword);
}
}
return $data; return $ret;
} }
/** /**

View file

@ -34,7 +34,10 @@ class LSattr_ldap_postaladdress extends LSattr_ldap {
* @retval mixed La valeur d'affichage de l'attribut * @retval mixed La valeur d'affichage de l'attribut
*/ */
public function getDisplayValue($data) { 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 * @retval mixed La valeur traitée de l'attribut
*/ */
public function getUpdateData($data) { public function getUpdateData($data) {
return str_replace("\n","$",$data); $ret = array();
foreach(ensureIsArray($data) as $val)
$ret[] = str_replace("\n", "$", $val);;
return $ret;
} }
} }

View file

@ -61,9 +61,7 @@ class LSattr_ldap_sambaAcctFlags extends LSattr_ldap {
* @retval array Array of enabled flags * @retval array Array of enabled flags
**/ **/
public static function parse_flags($data) { public static function parse_flags($data) {
if (!is_array($data)) { $data = ensureIsArray($data);
$data = array($data);
}
if (count($data) > 1) { if (count($data) > 1) {
LSerror :: addErrorCode('LSattr_ldap_sambaAcctFlags_01'); LSerror :: addErrorCode('LSattr_ldap_sambaAcctFlags_01');
return; return;
@ -99,8 +97,7 @@ class LSattr_ldap_sambaAcctFlags extends LSattr_ldap {
* @retval array Array of LDAP attribute value * @retval array Array of LDAP attribute value
**/ **/
public static function format_flags($flags) { public static function format_flags($flags) {
if (!is_array($flags)) $flags = ensureIsArray($flags);
$flags = array($flags);
foreach($flags as $flag) { foreach($flags as $flag) {
if (!self :: check_flag($flag)) { if (!self :: check_flag($flag)) {
LSerror :: addErrorCode('LSattr_ldap_sambaAcctFlags_03', $flag); LSerror :: addErrorCode('LSattr_ldap_sambaAcctFlags_03', $flag);

View file

@ -36,10 +36,9 @@ class LSattr_ldap_shadowExpire extends LSattr_ldap {
* @retval mixed Array of timestamp * @retval mixed Array of timestamp
*/ */
public function getDisplayValue($data) { public function getDisplayValue($data) {
$ret=array(); $ret = array();
foreach($data as $val) { foreach(ensureIsArray($data) as $val)
$ret[]=((int)$val)*86400; $ret[] = intval($val)*86400;
}
return $ret; return $ret;
} }
@ -52,9 +51,8 @@ class LSattr_ldap_shadowExpire extends LSattr_ldap {
*/ */
public function getUpdateData($data) { public function getUpdateData($data) {
$ret=array(); $ret=array();
foreach($data as $val) { foreach(ensureIsArray($data) as $val)
$ret[]=(string)round(((int)$val)/86400); $ret[] = strval(round(intval($val) / 86400));
}
return $ret; return $ret;
} }

View file

@ -115,11 +115,7 @@ class LSattribute extends LSlog_staticLoggerClass {
* @retval boolean true * @retval boolean true
*/ */
public function loadData($attr_data) { public function loadData($attr_data) {
if (is_empty($attr_data) || $attr_data === false) $this -> data = ensureIsArray($attr_data);
$attr_data = null;
elseif (!is_array($attr_data))
$attr_data = array($attr_data);
$this -> data = $attr_data;
return true; return true;
} }
@ -131,12 +127,9 @@ class LSattribute extends LSlog_staticLoggerClass {
* @retval boolean true * @retval boolean true
*/ */
public function reloadData($attr_data) { public function reloadData($attr_data) {
if ((!is_array($attr_data))&&(!empty($attr_data))) { $this -> data = ensureIsArray($attr_data);
$attr_data = array($attr_data); $this -> updateData = false;
} $this -> is_validate = false;
$this -> data = $attr_data;
$this -> updateData=false;
$this -> is_validate=false;
return true; return true;
} }
@ -392,14 +385,7 @@ class LSattribute extends LSlog_staticLoggerClass {
* @retval string La valeur a afficher dans le formulaire. * @retval string La valeur a afficher dans le formulaire.
*/ */
public function getFormVal() { public function getFormVal() {
$data=$this -> html -> getFormVal($this -> data); return ensureIsArray($this -> html -> getFormVal($this -> data));
if ($data==NULL) {
$data=array();
}
if(!is_array($data)) {
$data=array($data);
}
return $data;
} }
/** /**
@ -413,7 +399,7 @@ class LSattribute extends LSlog_staticLoggerClass {
*/ */
public function setUpdateData($data) { public function setUpdateData($data) {
if($this -> ldap -> isUpdated($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); $value = $this -> ldapObject -> getFData($format);
} }
if ($value !== false) { if ($value !== false) {
if (!empty($value)) { $this -> updateData = ensureIsArray($value);
if (!is_array($value)) {
$value=array($value);
}
$this -> updateData=$value;
}
else {
$this -> updateData=array();
}
return true; return true;
} }
return; return;

View file

@ -782,3 +782,11 @@ function is_empty($val) {
} }
return empty($val); return empty($val);
} }
function ensureIsArray($value) {
if (is_array($value))
return $value;
if (is_empty($value))
return array();
return array($value);
}