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
*/
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;
}

View file

@ -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;
}
/**

View file

@ -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<count($matches[0]);$i++) {
$parseValue[$matches[1][$i]]=$matches[2][$i];
if (preg_match_all('/\[([^=]*)=([^\]]*)\]/', $value, $matches)) {
$parseValue = array();
for($i=0; $i<count($matches[0]); $i++) {
$parseValue[$matches[1][$i]] = $matches[2][$i];
}
return $parseValue;
}
@ -80,9 +70,9 @@ class LSattr_ldap_compositeValueToJSON extends LSattr_ldap {
public static function encodeValue($value) {
if (is_array($value)) {
$ret="";
$ret = "";
foreach($value as $key => $val)
$ret.="[$key=$val]";
$ret .= "[$key=$val]";
return $ret;
}
return False;

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}
/**

View file

@ -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;
}
}

View file

@ -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);

View file

@ -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;
}

View file

@ -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;

View file

@ -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);
}