From 80b747a0c562c6f9bb19a26d190eb0fa28ed7246 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Mon, 11 Mar 2019 22:21:25 +0100 Subject: [PATCH] Add and use helper methods to access configuration parameters --- .../includes/class/class.LSattr_html.php | 22 ++- .../class/class.LSattr_html_image.php | 2 +- .../class/class.LSattr_html_maildir.php | 10 +- .../class/class.LSattr_html_password.php | 2 +- .../class/class.LSattr_html_select_list.php | 2 +- .../class/class.LSattr_html_select_object.php | 144 +++++++++-------- .../includes/class/class.LSattr_ldap.php | 15 +- .../class/class.LSattr_ldap_boolean.php | 15 +- .../includes/class/class.LSattr_ldap_date.php | 12 +- .../class/class.LSattr_ldap_password.php | 62 +++---- .../includes/class/class.LSattribute.php | 151 ++++++++---------- public_html/includes/class/class.LSconfig.php | 30 +++- .../includes/class/class.LSformElement.php | 39 +++-- .../class/class.LSformElement_boolean.php | 6 +- .../class/class.LSformElement_date.php | 29 ++-- ...s.LSformElement_jsonCompositeAttribute.php | 4 +- .../class.LSformElement_labeledValue.php | 10 +- .../class/class.LSformElement_mail.php | 2 +- .../class/class.LSformElement_mailQuota.php | 7 +- .../class/class.LSformElement_password.php | 76 +++++---- .../class.LSformElement_postaladdress.php | 24 +-- .../class/class.LSformElement_quota.php | 5 +- .../class/class.LSformElement_select.php | 4 +- .../class/class.LSformElement_select_box.php | 2 +- .../class.LSformElement_select_object.php | 29 ++-- .../class/class.LSformElement_text.php | 4 +- .../class.LSformElement_valueWithUnit.php | 22 +-- .../class/class.LSformElement_wysiwyg.php | 2 +- .../includes/class/class.LSldapObject.php | 76 +++++---- public_html/includes/class/class.LSsearch.php | 8 +- 30 files changed, 422 insertions(+), 394 deletions(-) diff --git a/public_html/includes/class/class.LSattr_html.php b/public_html/includes/class/class.LSattr_html.php index 5f0f327d..d0d1932f 100644 --- a/public_html/includes/class/class.LSattr_html.php +++ b/public_html/includes/class/class.LSattr_html.php @@ -61,12 +61,7 @@ class LSattr_html { * @retval string Le label de l'attribut. */ function getLabel() { - if ( $this -> config['label'] != '' ) { - return __($this -> config['label']); - } - else { - return __($this -> name); - } + return __($this -> getConfig('label', $this -> name)); } /** @@ -83,7 +78,7 @@ class LSattr_html { LSerror :: addErrorCode('LSattr_html_01',$this -> name); return; } - $element=$form -> addElement($this -> LSformElement_type, $this -> name, $this -> config['label'],$this -> config, $this); + $element=$form -> addElement($this -> LSformElement_type, $this -> name, $this -> getLabel(), $this -> config, $this); if(!$element) { LSerror :: addErrorCode('LSform_06',$this -> name); return; @@ -116,6 +111,19 @@ class LSattr_html { return $this -> attribute -> getDisplayValue(); } + /** + * Return a configuration parameter (or default value) + * + * @param[] $param The configuration parameter + * @param[] $default The default value (default : null) + * @param[] $cast Cast resulting value in specific type (default : disabled) + * + * @retval mixed The configuration parameter value or default value if not set + **/ + public function getConfig($param, $default=null, $cast=null) { + return LSconfig :: get($param, $default, $cast, $this -> config); + } + } /* diff --git a/public_html/includes/class/class.LSattr_html_image.php b/public_html/includes/class/class.LSattr_html_image.php index 4ba67247..11c31cca 100644 --- a/public_html/includes/class/class.LSattr_html_image.php +++ b/public_html/includes/class/class.LSattr_html_image.php @@ -37,7 +37,7 @@ class LSattr_html_image extends LSattr_html { * @retval LSformElement L'element du formulaire ajouté */ function addToForm (&$form,$idForm,$data=NULL) { - $element=$form -> addElement('image', $this -> name, $this -> config['label'],$this -> config, $this); + $element=$form -> addElement('image', $this -> name, $this -> getLabel(), $this -> config, $this); if(!$element) { LSerror :: addErrorCode('LSform_06',$this -> name); return; diff --git a/public_html/includes/class/class.LSattr_html_maildir.php b/public_html/includes/class/class.LSattr_html_maildir.php index f8e1d639..a004d08b 100644 --- a/public_html/includes/class/class.LSattr_html_maildir.php +++ b/public_html/includes/class/class.LSattr_html_maildir.php @@ -76,9 +76,10 @@ class LSattr_html_maildir extends LSattr_html { } public function deleteMaildirByFTP() { - if ($this -> config['html_options']['archiveNameFormat']) { + $archiveNameFormat = $this -> getConfig('html_options.archiveNameFormat'); + if ($archiveNameFormat) { LSdebug('LSformElement_maildir : archive'); - $newname=getFData($this -> config['html_options']['archiveNameFormat'],$this -> _toDo['old']); + $newname = getFData($archiveNameFormat, $this -> _toDo['old']); if ($newname) { if (renameMaildirByFTP($this -> _toDo['old'],$newname)) { LSsession :: addInfo(_("The mailbox has been archived successfully.")); @@ -112,9 +113,10 @@ class LSattr_html_maildir extends LSattr_html { $val = $this -> attribute -> getValue(); $val=$val[0]; } - if ($this -> config['html_options']['remoteRootPathRegex']) { + $remoteRootPathRegex = $this -> getConfig('html_options.remoteRootPathRegex'); + if ($remoteRootPathRegex) { if ( - preg_match($this -> config['html_options']['remoteRootPathRegex'],$val,$r) + preg_match($remoteRootPathRegex, $val, $r) || empty($val) ) diff --git a/public_html/includes/class/class.LSattr_html_password.php b/public_html/includes/class/class.LSattr_html_password.php index c1ffdbea..2a23194c 100644 --- a/public_html/includes/class/class.LSattr_html_password.php +++ b/public_html/includes/class/class.LSattr_html_password.php @@ -37,7 +37,7 @@ class LSattr_html_password extends LSattr_html { * @retval LSformElement L'element du formulaire ajouté */ function addToForm (&$form,$idForm,$data=NULL) { - $element=$form -> addElement('password', $this -> name, $this -> config['label'], $this -> config, $this); + $element=$form -> addElement('password', $this -> name, $this -> getLabel(), $this -> config, $this); if(!$element) { LSerror :: addErrorCode('LSform_06',$this -> name); return; diff --git a/public_html/includes/class/class.LSattr_html_select_list.php b/public_html/includes/class/class.LSattr_html_select_list.php index 4fdeb10f..68eb9833 100644 --- a/public_html/includes/class/class.LSattr_html_select_list.php +++ b/public_html/includes/class/class.LSattr_html_select_list.php @@ -82,7 +82,7 @@ class LSattr_html_select_list extends LSattr_html{ if (!$name) $name=$this -> name; if (!$ldapObject) $ldapObject=$this->attribute->ldapObject; $retInfos = array(); - if (is_array($options['possible_values'])) { + if (isset($options['possible_values']) && is_array($options['possible_values'])) { foreach($options['possible_values'] as $val_key => $val_label) { if($val_key==='OTHER_OBJECT') { $objInfos=self :: getLSobjectPossibleValues($val_label,$options,$name); diff --git a/public_html/includes/class/class.LSattr_html_select_object.php b/public_html/includes/class/class.LSattr_html_select_object.php index 5d9c33ac..148b327a 100644 --- a/public_html/includes/class/class.LSattr_html_select_object.php +++ b/public_html/includes/class/class.LSattr_html_select_object.php @@ -40,7 +40,7 @@ class LSattr_html_select_object extends LSattr_html{ */ function addToForm (&$form,$idForm,$data=NULL) { $this -> config['attrObject'] = $this; - $element=$form -> addElement('select_object', $this -> name, $this -> config['label'],$this -> config,$this); + $element=$form -> addElement('select_object', $this -> name, $this -> getLabel(), $this -> config, $this); if(!$element) { LSerror :: addErrorCode('LSform_06',$this -> name); return; @@ -54,7 +54,7 @@ class LSattr_html_select_object extends LSattr_html{ $element -> setValue($values); } } - $element -> setSelectableObject($this -> config['html_options']['selectable_object']['object_type']); + $element -> setSelectableObject($this -> getConfig('html_options.selectable_object.object_type')); return $element; } @@ -82,41 +82,45 @@ class LSattr_html_select_object extends LSattr_html{ * @retval array Tableau des valeurs de l'attribut */ function getValuesFromFormValues($values=NULL) { - $retValues = array(); - if (isset($this -> config['html_options']['selectable_object'])) { - $conf=$this -> config['html_options']['selectable_object']; + $conf = $this -> getConfig('html_options.selectable_object'); + if (is_array($conf) && is_array($values)) { + $retValues = array(); if (!isset($conf['object_type'])) { LSerror :: addErrorCode('LSattr_html_select_object_01',$this -> name); return; } + + if (!isset($conf['value_attribute'])) { + LSerror :: addErrorCode('LSattr_html_select_object_02',$this -> name); + return; + } if (!LSsession :: loadLSobject($conf['object_type'])) { return; } - - if (is_array($values)) { - $obj=new $conf['object_type'](); - foreach($values as $dn => $name) { - if ($obj -> loadData($dn)) { - if(($conf['value_attribute']=='dn')||($conf['value_attribute']=='%{dn}')) { - $val = $dn; - } - elseif (!isset($obj->attrs[$conf['value_attribute']])) { + + $obj=new $conf['object_type'](); + foreach($values as $dn => $name) { + if ($obj -> loadData($dn)) { + $val = ''; + if(($conf['value_attribute']=='dn')||($conf['value_attribute']=='%{dn}')) { + $val = $dn; + } + else { + if (!isset($obj->attrs[$conf['value_attribute']])) { LSerror :: addErrorCode('LSattr_html_select_object_02',$this -> name); return; } - else { - $val = $obj -> getValue($conf['value_attribute']); - $val = $val[0]; - } - if (empty($val)) { - continue; - } - $retValues[]=$val; + $val = $obj -> getValue($conf['value_attribute']); + $val = $val[0]; } + if (empty($val)) { + continue; + } + $retValues[] = $val; } - return $retValues; } + return $retValues; } return; } @@ -132,64 +136,65 @@ class LSattr_html_select_object extends LSattr_html{ * @retval array Tableau associatif des objects selectionés avec en clé * le DN et en valeur ce qui sera affiché. */ - function getFormValues($values=NULL,$fromDNs=false) { - $retInfos = array(); - $DNs=array(); - if (isset($this -> config['html_options']['selectable_object'])) { - $conf=$this -> config['html_options']['selectable_object']; + function getFormValues($values=NULL, $fromDNs=false) { + $conf = $this -> getConfig('html_options.selectable_object'); + if (is_array($conf) && is_array($values)) { if (!isset($conf['object_type'])) { LSerror :: addErrorCode('LSattr_html_select_object_01',$this -> name); return; } + + if (!isset($conf['value_attribute'])) { + LSerror :: addErrorCode('LSattr_html_select_object_02',$this -> name); + return; + } if (!LSsession :: loadLSobject($conf['object_type'])) { return; } - if (is_array($values)) { - $obj = new $conf['object_type'](); - if(($conf['value_attribute']=='dn')||($conf['value_attribute']=='%{dn}')||$fromDNs) { - $DNs=$values; - foreach($DNs as $dn) { - if($obj -> loadData($dn)) { - $retInfos[$dn] = $obj -> getDisplayName($conf['display_name_format']); - } + $retInfos = array(); + $DNs=array(); + + $obj = new $conf['object_type'](); + if(($conf['value_attribute']=='dn')||($conf['value_attribute']=='%{dn}')||$fromDNs) { + $DNs=$values; + foreach($DNs as $dn) { + if($obj -> loadData($dn)) { + $retInfos[$dn] = $obj -> getDisplayName($conf['display_name_format']); } } - else { - if (!isset($conf['value_attribute']) || (!is_array(LSconfig::get('LSobjects.'.$conf['object_type'].'.attrs.'.$conf['value_attribute'])))) { - LSerror :: addErrorCode('LSattr_html_select_object_02',$this -> name); - return; - } - $unrecognizedValues=array(); - foreach($values as $val) { - if (!empty($val)) { - $filter=Net_LDAP2_Filter::create($conf['value_attribute'],'equals',$val); - if (isset($conf['filter'])) $filter = LSldap::combineFilters('and',array($filter,$conf['filter'])); - $sparams=array(); - $sparams['onlyAccessible'] = (isset($conf['onlyAccessible'])?$conf['onlyAccessible']:False); - $listobj = $obj -> listObjectsName($filter,NULL,$sparams,$conf['display_name_format']); - if (count($listobj)==1) { - foreach($listobj as $dn => $name) { - $DNs[]=$dn; - $retInfos[$dn] = $name; - } - } - else { - $unrecognizedValues[]=$val; - if(count($listobj)>1) { - LSerror :: addErrorCode('LSattr_html_select_object_03',array('val' => $val, 'attribute' => $this -> name)); - } - } - } - } - $this -> unrecognizedValues=$unrecognizedValues; - } } else { - return false; + if (!is_array(LSconfig::get('LSobjects.'.$conf['object_type'].'.attrs.'.$conf['value_attribute']))) { + LSerror :: addErrorCode('LSattr_html_select_object_02', $this -> name); + return; + } + $unrecognizedValues=array(); + foreach($values as $val) { + if (!empty($val)) { + $filter=Net_LDAP2_Filter::create($conf['value_attribute'],'equals',$val); + if (isset($conf['filter'])) $filter = LSldap::combineFilters('and',array($filter,$conf['filter'])); + $sparams=array(); + $sparams['onlyAccessible'] = (isset($conf['onlyAccessible'])?$conf['onlyAccessible']:False); + $listobj = $obj -> listObjectsName($filter, NULL, $sparams, (isset($conf['display_name_format'])?$conf['display_name_format']:false)); + if (count($listobj)==1) { + foreach($listobj as $dn => $name) { + $DNs[]=$dn; + $retInfos[$dn] = $name; + } + } + else { + $unrecognizedValues[]=$val; + if(count($listobj)>1) { + LSerror :: addErrorCode('LSattr_html_select_object_03',array('val' => $val, 'attribute' => $this -> name)); + } + } + } + } + $this -> unrecognizedValues=$unrecognizedValues; } - $_SESSION['LSselect'][$conf['object_type']]=$DNs; + $_SESSION['LSselect'][$conf['object_type']] = $DNs; return $retInfos; } return false; @@ -205,8 +210,9 @@ class LSattr_html_select_object extends LSattr_html{ * le DN et en valeur ce qui sera affiché. */ function getValuesFromSession() { - if(is_array($_SESSION['LSselect'][$this -> config['html_options']['selectable_object']['object_type']])) { - return $this -> getFormValues($_SESSION['LSselect'][$this -> config['html_options']['selectable_object']['object_type']],true); + $obj_type = $this -> getConfig('html_options.selectable_object.object_type'); + if ( $obj_type && is_array($_SESSION['LSselect'][$obj_type]) ) { + return $this -> getFormValues($_SESSION['LSselect'][$obj_type], true); } return false; } diff --git a/public_html/includes/class/class.LSattr_ldap.php b/public_html/includes/class/class.LSattr_ldap.php index f4069bdf..9e84b83c 100644 --- a/public_html/includes/class/class.LSattr_ldap.php +++ b/public_html/includes/class/class.LSattr_ldap.php @@ -88,6 +88,19 @@ class LSattr_ldap { } return; } + + /** + * Return a configuration parameter (or default value) + * + * @param[] $param The configuration parameter + * @param[] $default The default value (default : null) + * @param[] $cast Cast resulting value in specific type (default : disabled) + * + * @retval mixed The configuration parameter value or default value if not set + **/ + public function getConfig($param, $default=null, $cast=null) { + return LSconfig :: get($param, $default, $cast, $this -> config); + } + } -?> diff --git a/public_html/includes/class/class.LSattr_ldap_boolean.php b/public_html/includes/class/class.LSattr_ldap_boolean.php index 857a4303..e9184fe0 100644 --- a/public_html/includes/class/class.LSattr_ldap_boolean.php +++ b/public_html/includes/class/class.LSattr_ldap_boolean.php @@ -97,12 +97,7 @@ class LSattr_ldap_boolean extends LSattr_ldap { * @retval string The True value **/ function getTrue() { - if (isset($this -> config['ldap_options']['true_value'])) { - return $this -> config['ldap_options']['true_value']; - } - else { - return 'TRUE'; - } + return $this -> getConfig('ldap_options.true_value', 'TRUE', 'string'); } /** @@ -111,13 +106,9 @@ class LSattr_ldap_boolean extends LSattr_ldap { * @retval string The False value **/ function getFalse() { - if (isset($this -> config['ldap_options']['false_value'])) { - return $this -> config['ldap_options']['false_value']; - } - else { - return 'FALSE'; - } + return $this -> getConfig('ldap_options.false_value', 'FALSE', 'string'); } + } ?> diff --git a/public_html/includes/class/class.LSattr_ldap_date.php b/public_html/includes/class/class.LSattr_ldap_date.php index c9a82938..e7dba216 100644 --- a/public_html/includes/class/class.LSattr_ldap_date.php +++ b/public_html/includes/class/class.LSattr_ldap_date.php @@ -37,7 +37,7 @@ class LSattr_ldap_date extends LSattr_ldap { if(!is_array($data)) { $data=array($data); } - if ($this -> config['ldap_options']['timestamp']==1) { + if ($this -> getConfig('ldap_options.timestamp', false, 'bool')) { return $data; } $retval=array(); @@ -58,7 +58,7 @@ class LSattr_ldap_date extends LSattr_ldap { * @retval mixed La valeur traitée de l'attribut */ function getUpdateData($data) { - if ($this -> config['ldap_options']['timestamp']==1) { + if ($this -> getConfig('ldap_options.timestamp', false, 'bool')) { return $data; } $retval=array(); @@ -76,13 +76,9 @@ class LSattr_ldap_date extends LSattr_ldap { * @retval string Le format de la date **/ function getFormat() { - if (isset($this -> config['ldap_options']['format'])) { - return $this -> config['ldap_options']['format']; - } - else { - return "%Y%m%d%H%M%SZ"; - } + return $this -> getConfig('ldap_options.format', '%Y%m%d%H%M%SZ'); } + } ?> diff --git a/public_html/includes/class/class.LSattr_ldap_password.php b/public_html/includes/class/class.LSattr_ldap_password.php index ae140761..6ac83921 100644 --- a/public_html/includes/class/class.LSattr_ldap_password.php +++ b/public_html/includes/class/class.LSattr_ldap_password.php @@ -36,11 +36,13 @@ class LSattr_ldap_password extends LSattr_ldap { * @retval mixed The display value of this attribute */ function getDisplayValue($data) { - if ($this -> config['ldap_options']['displayClearValue']) { + 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==$this -> config['ldap_options']['wildcardPassword'] || $p==$this -> config['ldap_options']['encodedWildcardPassword']) { + if ($p == $wildcardPassword || $p == $encodedWildcardPassword) { continue; } $ret[]=$p; @@ -73,24 +75,26 @@ class LSattr_ldap_password extends LSattr_ldap { $data[]=$this -> encodePassword($this -> clearPassword); // Wildcard Password - if (isset($this -> config['ldap_options']['wildcardPassword'])) { - if(!is_array($this -> config['ldap_options']['wildcardPassword'])) { - $data[]=$this -> encodePassword($this -> config['ldap_options']['wildcardPassword']); + $wildcardPassword = $this -> getConfig('ldap_options.wildcardPassword'); + if ($wildcardPassword) { + if (!is_array($wildcardPassword)) { + $data[] = $this -> encodePassword($wildcardPassword); } else { - foreach($this -> config['ldap_options']['wildcardPassword'] as $pwd) { - $data[]=$this -> encodePassword($pwd); + foreach($wildcardPassword as $pwd) { + $data[] = $this -> encodePassword($pwd); } } } // Wildcard Password already encoded - if (isset($this -> config['ldap_options']['encodedWildcardPassword'])) { - if(!is_array($this -> config['ldap_options']['encodedWildcardPassword'])) { - $data[]=$this -> config['ldap_options']['encodedWildcardPassword']; + $encodedWildcardPassword = $this -> getConfig('ldap_options.encodedWildcardPassword'); + if ($encodedWildcardPassword) { + if (!is_array($encodedWildcardPassword)) { + $data[] = $encodedWildcardPassword; } else { - $data=array_merge($data,$this -> config['ldap_options']['encodedWildcardPassword']); + $data = array_merge($data, $encodedWildcardPassword); } } @@ -108,21 +112,21 @@ class LSattr_ldap_password extends LSattr_ldap { * @retval strinf The encode password */ function encodePassword($clearPassword) { - if (isset($this -> config['ldap_options']['encode_function']) || $this -> config['ldap_options']['encode']=='function') { - if (!is_callable($this -> config['ldap_options']['encode_function'])) { - $this -> config['ldap_options']['encode'] = 'clear'; - LSerror :: addErrorCode('LSattr_ldap_password_02',$this -> config['ldap_options']['encode_function']); + $encode = $this -> getConfig('ldap_options.encode', 'md5crypt', 'string'); + $encode_function = $this -> getConfig('ldap_options.encode_function'); + if ($encode_function || $encode == 'function') { + if ( (!$encode_function) || (!is_callable($encode_function)) ) { + $encode = 'clear'; + $encode_function = null; + LSerror :: addErrorCode('LSattr_ldap_password_02', ($encode_function?$encode_function:__('undefined'))); } else { - $this -> config['ldap_options']['encode'] = 'function'; + $encode = 'function'; } } - elseif (!$this -> config['ldap_options']['encode']) { - $this -> config['ldap_options']['encode'] = 'md5crypt'; - } - switch($this -> config['ldap_options']['encode']) { + switch($encode) { case 'crypt': - if ($this -> config['ldap_options']['no_random_crypt_salt']) { + if ($this -> getConfig('ldap_options.no_random_crypt_salt')) { return '{CRYPT}' . crypt($clearPassword,substr($clearPassword,0,2)); } else { @@ -157,7 +161,7 @@ class LSattr_ldap_password extends LSattr_ldap { break; case 'sha256': case 'sha512': - switch($this -> config['ldap_options']['encode']) { + switch($encode) { case 'sha256': $mhash_type = MHASH_SHA256; break; @@ -166,15 +170,15 @@ class LSattr_ldap_password extends LSattr_ldap { break; } if( function_exists( 'mhash' ) ) { - return '{'.strtoupper($this -> config['ldap_options']['encode']).'}' . base64_encode( mhash( $mhash_type, $clearPassword ) ); + return '{'.strtoupper($encode).'}' . base64_encode( mhash( $mhash_type, $clearPassword ) ); } else { - LSerror :: addErrorCode('LSattr_ldap_password_01', $this -> config['ldap_options']['encode']); + LSerror :: addErrorCode('LSattr_ldap_password_01', $encode); } break; case 'ssha': case 'ssha256': case 'ssha512': - switch($this -> config['ldap_options']['encode']) { + switch($encode) { case 'ssha': $mhash_type = MHASH_SHA1; break; @@ -188,10 +192,10 @@ class LSattr_ldap_password extends LSattr_ldap { if( function_exists( 'mhash' ) && function_exists( 'mhash_keygen_s2k' ) ) { mt_srand( (double) microtime() * 1000000 ); $salt = mhash_keygen_s2k( $mhash_type, $clearPassword, substr( pack( "h*", md5( mt_rand() ) ), 0, 8 ), 4 ); - return "{".strtoupper($this -> config['ldap_options']['encode'])."}".base64_encode( mhash( $mhash_type, $clearPassword.$salt ).$salt ); + return "{".strtoupper($encode)."}".base64_encode( mhash( $mhash_type, $clearPassword.$salt ).$salt ); } else { - LSerror :: addErrorCode('LSattr_ldap_password_01', $this -> config['ldap_options']['encode']); + LSerror :: addErrorCode('LSattr_ldap_password_01', $encode); } break; case 'smd5': @@ -219,10 +223,10 @@ class LSattr_ldap_password extends LSattr_ldap { return $clearPassword; break; case 'function': - return call_user_func_array($this -> config['ldap_options']['encode_function'], array(&$this -> attribute -> ldapObject, $clearPassword)); + return call_user_func_array($encode_function, array(&$this -> attribute -> ldapObject, $clearPassword)); break; } - LSerror :: addErrorCode('LSattr_ldap_password_01',$this -> config['ldap_options']['encode']); + LSerror :: addErrorCode('LSattr_ldap_password_01', $encode); return $clearPassword; } diff --git a/public_html/includes/class/class.LSattribute.php b/public_html/includes/class/class.LSattribute.php index 63ad1850..6eb401a6 100644 --- a/public_html/includes/class/class.LSattribute.php +++ b/public_html/includes/class/class.LSattribute.php @@ -179,29 +179,21 @@ class LSattribute { $data = $this -> ldap -> getDisplayValue($this -> data); } - if (isset($this -> config['onDisplay'])) { - if (is_array($this -> config['onDisplay'])) { - $result=$data; - foreach($this -> config['onDisplay'] as $func) { - if (function_exists($func)) { - $result=call_user_func($func, $result); - } - else { - LSerror :: addErrorCode('LSattribute_02',array('attr' => $this->name,'func' => $func)); - return; - } - } - return $result; - } - else { - if (function_exists($this -> config['onDisplay'])) { - return $this -> config['onDisplay']($data); + $onDisplay = $this -> getConfig('onDisplay'); + if ($onDisplay) { + if (!is_array($onDisplay)) + $onDisplay = array($onDisplay); + $result = $data; + foreach($onDisplay as $func) { + if (function_exists($func)) { + $result = call_user_func($func, $result); } else { - LSerror :: addErrorCode('LSattribute_02',array('attr' => $this->name,'func' => $this -> config['onDisplay'])); + LSerror :: addErrorCode('LSattribute_02', array('attr' => $this->name, 'func' => $func)); return; } } + return $result; } return $data; } @@ -222,7 +214,7 @@ class LSattribute { * @retval boolean true si l'ajout a fonctionner ou qu'il n'est pas nécessaire, false sinon */ function addToForm(&$form,$idForm,&$obj=NULL,$value=NULL) { - if(isset($this -> config['form'][$idForm])) { + if($this -> getConfig("form.$idForm")) { if (!$this -> html) { LSerror :: addErrorCode('LSattribute_09',array('type' => 'html','name' => $this -> name)); return; @@ -234,13 +226,13 @@ class LSattribute { $data = $value; } else if($this -> data !='') { - $data=$this -> getFormVal(); + $data = $this -> getFormVal(); } - else if (isset($this -> config['default_value'])) { - $data=$obj -> getFData($this -> config['default_value']); + else if ($this -> getConfig('default_value')) { + $data = $obj -> getFData($this -> getConfig('default_value')); } else { - $data=NULL; + $data = NULL; } $element = $this -> html -> addToForm($form,$idForm,$data); @@ -248,35 +240,34 @@ class LSattribute { LSerror :: addErrorCode('LSform_06',$this -> name); } - if(isset($this -> config['required']) && $this -> config['required']==1) { + if($this -> getConfig('required')) { $form -> setRequired($this -> name); } - if (($this -> config['form'][$idForm]==0) || ($this -> myRights() == 'r')) { + if ( ($this -> getConfig("form.$idForm")==0) || ($this -> myRights() == 'r') ) { $element -> freeze(); } else { - if(isset($this -> config['check_data'])) { - if(is_array($this -> config['check_data'])) { - foreach ($this -> config['check_data'] as $rule => $rule_infos) { - if((!$form -> isRuleRegistered($rule))&&($rule!='')) { - LSerror :: addErrorCode('LSattribute_03',array('attr' => $this->name,'rule' => $rule)); - return; - } - if(!isset($rule_infos['msg'])) { - $rule_infos['msg']=getFData(_('The value of field %{label} is invalid.'),$this -> getLabel()); - } - else { - $rule_infos['msg']=__($rule_infos['msg']); - } - if(!isset($rule_infos['params'])) - $rule_infos['params']=NULL; - $form -> addRule($this -> name,$rule,array('msg' => $rule_infos['msg'], 'params' => $rule_infos['params'])); + $check_data = $this -> getConfig('check_data', array()); + if(is_array($check_data)) { + foreach ($check_data as $rule => $rule_infos) { + if((!$form -> isRuleRegistered($rule))&&($rule!='')) { + LSerror :: addErrorCode('LSattribute_03',array('attr' => $this->name,'rule' => $rule)); + return; } + if(!isset($rule_infos['msg'])) { + $rule_infos['msg']=getFData(_('The value of field %{label} is invalid.'),$this -> getLabel()); + } + else { + $rule_infos['msg']=__($rule_infos['msg']); + } + if(!isset($rule_infos['params'])) + $rule_infos['params']=NULL; + $form -> addRule($this -> name,$rule,array('msg' => $rule_infos['msg'], 'params' => $rule_infos['params'])); } - else { - LSerror :: addErrorCode('LSattribute_04',$this->name); - } + } + else { + LSerror :: addErrorCode('LSattribute_04',$this->name); } } } @@ -296,29 +287,10 @@ class LSattribute { $return='n'; $whoami = $this -> ldapObject -> whoami(); foreach($whoami as $who) { - switch ($who) { - case 'admin': - if($this -> config['rights']['admin']=='w') { - $return='w'; - break; - } - else { - $return='r'; - } - break; - default: - if (!isset($this -> config['rights'][$who])) break; - if ($this -> config['rights'][$who] == 'w') { - $return='w'; - break; - } - else if($this -> config['rights'][$who] == 'r') { - $return='r'; - } - break; - } - if ($return=='w') { - break; + $right = $this -> getConfig("rights.$who", ($who=='admin'?'r':null)); + if (in_array($right, array('r', 'w'))) { + $return = $right; + if ($return == 'w') break; } } $this -> _myRights = $return; @@ -337,7 +309,7 @@ class LSattribute { * @retval boolean true si l'ajout a fonctionner ou qu'il n'est pas nécessaire, false sinon */ function addToView(&$form) { - if((isset($this -> config['view'])) && ($this -> config['view']) && ($this -> myRights() != 'n') ) { + if ($this -> getConfig('view', false, 'bool') && ($this -> myRights() != 'n') ) { if (!$this -> html) { LSerror :: addErrorCode('LSattribute_09',array('type' => 'html','name' => $this -> name)); return; @@ -370,7 +342,7 @@ class LSattribute { * @retval boolean true si la valeur a été rafraichie ou que ce n'est pas nécessaire, false sinon */ function refreshForm(&$form,$idForm) { - if(isset($this -> config['form'][$idForm])&&($this -> myRights()!='n')) { + if ($this -> getConfig("form.$idForm") && ($this -> myRights() != 'n')) { if (!$this -> html) { LSerror :: addErrorCode('LSattribute_09',array('type' => 'html','name' => $this -> name)); return; @@ -461,7 +433,7 @@ class LSattribute { * @retval boolean true si l'attribut est obligatoire, false sinon */ function isRequired() { - return (isset($this -> config['required'])?(bool)$this -> config['required']:false); + return $this -> getConfig('required', false, 'bool'); } /** @@ -472,15 +444,14 @@ class LSattribute { * @retval boolean true si la valeur de l'attribut peut être générée, false sinon */ function canBeGenerated() { + $format = $this -> getConfig('generate_value_format', $this -> getConfig('default_value')); return ( - (function_exists($this -> config['generate_function'])) - || - (isset($this -> config['generate_value_format'])) + (function_exists($this -> getConfig('generate_function'))) || ( - (is_string($this -> config['default_value'])) + (is_string($format)) && - (strlen($this -> config['default_value'])>0) + (strlen($format) > 0) ) ); } @@ -493,17 +464,16 @@ class LSattribute { * @retval boolean true si la valeur à put être générée, false sinon */ function generateValue() { - $value=false; - if (function_exists($this -> config['generate_function'])) { - $value=call_user_func_array($this -> config['generate_function'],array(&$this -> ldapObject)); + $value = false; + $generate_function = $this -> getConfig('generate_function'); + $format = $this -> getConfig('generate_value_format', $this -> getConfig('default_value')); + if ($generate_function && function_exists($generate_function)) { + $value = call_user_func_array($generate_function, array(&$this -> ldapObject)); } - else if (isset($this -> config['generate_value_format'])) { - $value = $this -> ldapObject -> getFData($this -> config['generate_value_format']); + else if ($format) { + $value = $this -> ldapObject -> getFData($format); } - else if (is_string($this -> config['default_value']) && strlen($this -> config['default_value'])>0) { - $value = $this -> ldapObject -> getFData($this -> config['default_value']); - } - if ($value!==false) { + if ($value !== false) { if (!empty($value)) { if (!is_array($value)) { $value=array($value); @@ -721,6 +691,19 @@ class LSattribute { return $return; } + + /** + * Return a configuration parameter (or default value) + * + * @param[] $param The configuration parameter + * @param[] $default The default value (default : null) + * @param[] $cast Cast resulting value in specific type (default : disabled) + * + * @retval mixed The configuration parameter value or default value if not set + **/ + public function getConfig($param, $default=null, $cast=null) { + return LSconfig :: get($param, $default, $cast, $this -> config); + } } diff --git a/public_html/includes/class/class.LSconfig.php b/public_html/includes/class/class.LSconfig.php index 5d6b8700..a37890b0 100644 --- a/public_html/includes/class/class.LSconfig.php +++ b/public_html/includes/class/class.LSconfig.php @@ -60,17 +60,31 @@ class LSconfig { * * @retval mixed La valeur de la variable, ou false si son nom n'est parsable **/ - public static function get($var) { - $vars=explode('.',$var); - if(is_array($vars)) { - $data=self :: $data; + public static function get($var, $default=null, $cast=null, $data=null) { + $vars = explode('.', $var); + $value = $default; + if (is_array($vars)) { + $value = (is_array($data)?$data:self :: $data); foreach ($vars as $v) { - if (!isset($data[$v])) return; - $data=$data[$v]; + if (!is_array($value) || !isset($value[$v])) { + $value = $default; + break; + } + $value = $value[$v]; } - return $data; } - return; + switch($cast) { + case 'bool': + return boolval($value); + case 'int': + return intval($value); + case 'float': + return floatval($value); + case 'string': + return strval($value); + default: + return $value; + } } /** diff --git a/public_html/includes/class/class.LSformElement.php b/public_html/includes/class/class.LSformElement.php index baca7cca..1f01cc03 100644 --- a/public_html/includes/class/class.LSformElement.php +++ b/public_html/includes/class/class.LSformElement.php @@ -189,16 +189,15 @@ class LSformElement { $return['required']=true; } $return['label'] = $this -> getLabel(); - $help_info = ""; - if ( (isset($this -> params['displayAttrName']) && $this -> params['displayAttrName']) || (isset($this -> attr_html -> attribute -> ldapObject -> config['displayAttrName']) && $this -> attr_html -> attribute -> ldapObject -> config['displayAttrName']) ) { - $help_info=_("Attribute")." : ".$this -> name."\n"; + $help_infos = array(); + if ( $this -> getParam('displayAttrName', $this -> attr_html -> attribute -> ldapObject -> getConfig('displayAttrName', false, 'bool'), 'bool') ) { + $help_infos[] = _("Attribute")." : ".$this -> name."\n"; } - if (isset($this -> params['help_info'])) { - if (!empty($help_info)) $help_info .= " - "; - $help_info.=__($this -> params['help_info']); + if ($this -> getParam('help_info')) { + $help_infos[] = __($this -> getParam('help_info')); } - if (!empty($help_info)) - $return['help_info'] = $help_info; + if (!empty($help_infos)) + $return['help_info'] = implode(' - ', $help_infos); return $return; } @@ -246,12 +245,7 @@ class LSformElement { if ($this -> label != "") { return __($this -> label); } - else if ($this -> params['label']) { - return __($this -> params['label']); - } - else { - return __($this -> name); - } + return __($this -> getParam('label', $this -> name)); } /** @@ -260,7 +254,7 @@ class LSformElement { * @retval boolean True si le champ est à valeur multiple, False sinon */ function isMultiple() { - return ( (isset($this -> params['multiple']))?($this -> params['multiple'] == true):false ); + return $this -> getParam('multiple', false, 'bool'); } /** @@ -286,7 +280,7 @@ class LSformElement { 'value' => '', 'values' => $this -> values, 'attr_name' => $this -> name, - 'noValueTxt' => ( (isset($this -> params['no_value_label']))? __($this -> params['no_value_label']):_('No set value') ), + 'noValueTxt' => __($this -> getParam('no_value_label', 'No set value', 'string')), 'fieldTemplate' => $this -> fieldTemplate, 'fieldType' => get_class($this) ) @@ -302,6 +296,19 @@ class LSformElement { function getEmptyField() { return $this -> fetchTemplate($this -> fieldTemplate); } + + /** + * Return a parameter (or default value) + * + * @param[] $param The parameter + * @param[] $default The default value (default : null) + * @param[] $cast Cast resulting value in specific type (default : disabled) + * + * @retval mixed The parameter value or default value if not set + **/ + public function getParam($param, $default=null, $cast=null) { + return LSconfig :: get($param, $default, $cast, $this -> params); + } } ?> diff --git a/public_html/includes/class/class.LSformElement_boolean.php b/public_html/includes/class/class.LSformElement_boolean.php index b15e0cc0..530fbc53 100644 --- a/public_html/includes/class/class.LSformElement_boolean.php +++ b/public_html/includes/class/class.LSformElement_boolean.php @@ -57,13 +57,11 @@ class LSformElement_boolean extends LSformElement { $return['html'] = $this -> fetchTemplate( NULL, array( - 'yesTxt' => (isset($this -> params['html_options']['true_label']) && !empty($this -> params['html_options']['true_label']))?__($this -> params['html_options']['true_label']):_('Yes'), - 'noTxt' => (isset($this -> params['html_options']['false_label']) && !empty($this -> params['html_options']['false_label']))?__($this -> params['html_options']['false_label']):_('No'), + 'yesTxt' => __($this -> getParam('html_options.true_label', 'Yes')), + 'noTxt' => __($this -> getParam('html_options.false_label', 'No')), ) ); return $return; } } - -?> diff --git a/public_html/includes/class/class.LSformElement_date.php b/public_html/includes/class/class.LSformElement_date.php index 00db2bae..7c87dffa 100644 --- a/public_html/includes/class/class.LSformElement_date.php +++ b/public_html/includes/class/class.LSformElement_date.php @@ -115,15 +115,7 @@ class LSformElement_date extends LSformElement { * @retval string Le format de la date **/ function getFormat() { - if (isset($this -> params['html_options']['format'])) { - return $this -> params['html_options']['format']; - } - else { - if (isset($this -> params['html_options']['time']) && !$this -> params['html_options']['time']) { - return '%d/%m/%Y'; - } - return "%d/%m/%Y, %T"; - } + return $this -> getParam('html_options.format', ($this -> getParam('html_options.time', true)?'%d/%m/%Y, %T':'%d/%m/%Y')); } /** @@ -132,11 +124,12 @@ class LSformElement_date extends LSformElement { * @retval string The date picker style **/ function getStyle() { - if (isset($this -> params['html_options']['style'])) { - if (is_dir(LS_LIB_DIR.'arian-mootools-datepicker/datepicker_'.strval($this -> params['html_options']['style']))) { - return $this -> params['html_options']['style']; + $style = $this -> getParam('html_options.style', $this -> default_style, 'string'); + if ($style) { + if (is_dir(LS_LIB_DIR.'arian-mootools-datepicker/datepicker_'.$style)) { + return $style; } - LSdebug('LSformElement :: Date => unknown style parameter value '.strval($this -> params['html_options']['style'])); + LSdebug('LSformElement :: Date => unknown style parameter value '.$style); } return $this -> default_style; } @@ -164,12 +157,12 @@ class LSformElement_date extends LSformElement { $params = array( 'format' => $this -> php2js_format($this -> getFormat()), 'style' => $this -> getStyle(), - 'time' => (isset($this -> params['html_options']['time'])?$this -> params['html_options']['time']:true), - 'manual' => (isset($this -> params['html_options']['manual'])?$this -> params['html_options']['manual']:true), - 'showNowButton' => (isset($this -> params['html_options']['showNowButton'])?$this -> params['html_options']['showNowButton']:true), - 'showTodayButton' => (isset($this -> params['html_options']['showTodayButton'])?$this -> params['html_options']['showTodayButton']:true), + 'time' => $this -> getParam('html_options.time', true, 'bool'), + 'manual' => $this -> getParam('html_options.manual', true, 'bool'), + 'showNowButton' => $this -> getParam('html_options.showNowButton', true, 'bool'), + 'showTodayButton' => $this -> getParam('html_options.showTodayButton', true, 'bool'), ); - LSsession :: addJSconfigParam($this -> name,$params); + LSsession :: addJSconfigParam($this -> name, $params); $codeLang = str_replace('_','-',preg_replace('/\..*$/','',LSsession :: getLang())); diff --git a/public_html/includes/class/class.LSformElement_jsonCompositeAttribute.php b/public_html/includes/class/class.LSformElement_jsonCompositeAttribute.php index 62a579f8..43753072 100644 --- a/public_html/includes/class/class.LSformElement_jsonCompositeAttribute.php +++ b/public_html/includes/class/class.LSformElement_jsonCompositeAttribute.php @@ -37,9 +37,7 @@ class LSformElement_jsonCompositeAttribute extends LSformElement { function LSformElement_jsonCompositeAttribute (&$form, $name, $label, $params,&$attr_html){ parent :: LSformElement($form, $name, $label, $params,$attr_html); - if (is_array($this -> params['html_options']['components'])) { - $this -> components = $this -> params['html_options']['components']; - } + $this -> components = $this -> getParam('html_options.components', array()); } /* diff --git a/public_html/includes/class/class.LSformElement_labeledValue.php b/public_html/includes/class/class.LSformElement_labeledValue.php index 47333205..cf26b573 100644 --- a/public_html/includes/class/class.LSformElement_labeledValue.php +++ b/public_html/includes/class/class.LSformElement_labeledValue.php @@ -51,7 +51,7 @@ class LSformElement_labeledValue extends LSformElement { $parseValues[]=$this -> parseValue($val); } $return['html'] = $this -> fetchTemplate(NULL,array( - 'labels' => $this -> params['html_options']['labels'], + 'labels' => $this -> getParam('html_options.labels'), 'parseValues' => $parseValues, 'unrecognizedValueTxt' => __('(unrecognized value)'), 'unrecognizedLabelTxt' => __('(unrecognized label)'), @@ -66,11 +66,10 @@ class LSformElement_labeledValue extends LSformElement { */ function getEmptyField() { return $this -> fetchTemplate($this -> fieldTemplate,array( - 'labels' => $this -> params['html_options']['labels'], + 'labels' => $this -> getParam('html_options.labels'), )); } - /** * Parse une valeur * @@ -82,8 +81,9 @@ class LSformElement_labeledValue extends LSformElement { $ret=array('raw_value' => $value); if (preg_match('/^\[([^\]]*)\](.*)$/',$value,$m)) { $ret['label'] = $m[1]; - if (isset($this -> params['html_options']['labels'][$ret['label']])) - $ret['translated_label'] = $this -> params['html_options']['labels'][$ret['label']]; + $label = $this -> getParam('html_options.labels.'.$ret['label']); + if ($label) + $ret['translated_label'] = $label; $ret['value'] = $m[2]; } return $ret; diff --git a/public_html/includes/class/class.LSformElement_mail.php b/public_html/includes/class/class.LSformElement_mail.php index 9ef255a0..aeacc226 100644 --- a/public_html/includes/class/class.LSformElement_mail.php +++ b/public_html/includes/class/class.LSformElement_mail.php @@ -58,7 +58,7 @@ class LSformElement_mail extends LSformElement_text { } function fetchTemplate($template=NULL,$variables=array()) { - if (isset($this -> params['html_options']['disableMailSending']) && $this -> params['html_options']['disableMailSending']) { + if ($this -> getParam('html_options.disableMailSending', false, 'bool')) { $this -> fetchVariables['uriClass'] .= " LSformElement_mail_disableMailSending"; } return parent :: fetchTemplate($template,$variables); diff --git a/public_html/includes/class/class.LSformElement_mailQuota.php b/public_html/includes/class/class.LSformElement_mailQuota.php index c445f4e0..710d0924 100644 --- a/public_html/includes/class/class.LSformElement_mailQuota.php +++ b/public_html/includes/class/class.LSformElement_mailQuota.php @@ -114,12 +114,7 @@ class LSformElement_mailQuota extends LSformElement { * @retval string Suffix value **/ function getSuffix() { - if(isset($this -> params['html_options']['suffix'])){ - return strval($this -> params['html_options']['suffix']); - } - else { - return "S"; - } + return $this -> getParam('html_options.suffix', 'S', 'string'); } /** diff --git a/public_html/includes/class/class.LSformElement_password.php b/public_html/includes/class/class.LSformElement_password.php index 34ad3cb6..d06931c7 100644 --- a/public_html/includes/class/class.LSformElement_password.php +++ b/public_html/includes/class/class.LSformElement_password.php @@ -80,13 +80,13 @@ class LSformElement_password extends LSformElement { LSdebug ('send by form'); } } - else if (isset($this -> params['html_options']['mail']['isset']) && $this -> params['html_options']['mail']['send']==1) { + else if ($this -> getParam('html_options.mail.send')) { $this -> sendMail = true; LSdebug ('send by config'); } if ($this -> sendMail && LSsession :: loadLSaddon('mail')) { - $msg = $this -> params['html_options']['mail']['msg']; - $subject = $this -> params['html_options']['mail']['subject']; + $msg = $this -> getParam('html_options.mail.msg'); + $subject = $this -> getParam('html_options.mail.subject'); if (isset($_POST['LSformElement_password_'.$this -> name.'_msg'])) { $msgInfos = json_decode($_POST['LSformElement_password_'.$this -> name.'_msg']); if ($msgInfos -> subject) { @@ -122,7 +122,7 @@ class LSformElement_password extends LSformElement { LSsession :: addCssFile('LSformElement_password.css'); $return = $this -> getLabelInfos(); $pwd = ""; - if ($this -> params['html_options']['clearView'] or $this -> params['html_options']['clearEdit']) { + if ($this -> getParam('html_options.clearView') or $this -> getParam('html_options.clearEdit')) { $pwd = $this -> values[0]; } if (!$this -> isFreeze()) { @@ -142,35 +142,43 @@ class LSformElement_password extends LSformElement { ) ); - if (($this -> params['html_options']['generationTool'])&&($this -> params['html_options']['autoGenerate'])&&(empty($this -> values))) { + if ($this -> getParam('html_options.generationTool') && $this -> getParam('html_options.autoGenerate') && empty($this -> values)) { $pwd=$this->generatePassword($this -> params); } $params = array( - 'generate' => ($this -> params['html_options']['generationTool']==True), - 'clearEdit' => ($this -> params['html_options']['clearEdit']==True), - 'viewHash' => ($this -> params['html_options']['viewHash']==True), - 'verify' => ( (!$this -> attr_html -> attribute -> ldapObject-> isNew()) && ( (isset($this -> params['html_options']['verify']) && $this -> params['html_options']['verify']) || (!isset($this -> params['html_options']['verify'])) ) ) + 'generate' => $this -> getParam('html_options.generationTool', true, 'bool'), + 'clearEdit' => $this -> getParam('html_options.clearEdit', false, 'bool'), + 'viewHash' => $this -> getParam('html_options.viewHash', false, 'bool'), + 'verify' => ( (!$this -> attr_html -> attribute -> ldapObject-> isNew()) && $this -> getParam('html_options.verify', True, 'bool') ) ); - if (isset($this -> params['html_options']['mail'])) { - $params['mail'] = $this -> params['html_options']['mail']; + + if ($this -> getParam('html_options.mail')) { + $params['mail'] = $this -> getParam('html_options.mail'); $params['mail']['mail_attr'] = $this -> getMailAttrs(); } - LSsession :: addJSconfigParam($this -> name,$params); + LSsession :: addJSconfigParam($this -> name, $params); LSsession :: addJSscript('LSformElement_password_field.js'); LSsession :: addJSscript('LSformElement_password.js'); } - $return['html'] = $this -> fetchTemplate(NULL,array('pwd' => $pwd,'clearView' => $this -> params['html_options']['clearView'],'clearEdit' => $this -> params['html_options']['clearEdit'])); + $return['html'] = $this -> fetchTemplate ( + NULL, + array( + 'pwd' => $pwd, + 'clearView' => $this -> getParam('html_options.clearView'), + 'clearEdit' => $this -> getParam('html_options.clearEdit'), + ) + ); return $return; } function generatePassword($params=NULL) { - if ($params['html_options']['use_pwgen']) { - $args=(isset($params['html_options']['pwgen_opts'])?$params['html_options']['pwgen_opts']:''); - $len=(isset($params['html_options']['lenght'])?$params['html_options']['lenght']:8); - $bin=(isset($params['html_options']['pwgen_path'])?$params['html_options']['pwgen_path']:'pwgen'); - $cmd="$bin ".escapeshellcmd($args)." $len 1"; + if (LSconfig :: get('html_options.use_pwgen', false, null, $params)) { + $args = LSconfig :: get('html_options.pwgen_opts', '', 'string', $params); + $len = LSconfig :: get('html_options.lenght', 8, 'int', $params); + $bin = LSconfig :: get('html_options.pwgen_path', 'pwgen', 'string', $params); + $cmd = "$bin ".escapeshellcmd($args)." $len 1"; exec($cmd,$ret,$retcode); LSdebug("Generate password using pwgen. Cmd : '$cmd' / Return code : $retcode / Return : ".print_r($ret,1)); if ($retcode==0 && count($ret)>0) { @@ -180,7 +188,7 @@ class LSformElement_password extends LSformElement { LSerror :: addErrorCode('LSformElement_password_03'); } } - return generatePassword($params['html_options']['chars'],$params['html_options']['lenght']); + return generatePassword(LSconfig :: get('html_options.chars', null, null, $params), LSconfig :: get('html_options.lenght', 8, 'int', $params)); } function verifyPassword($pwd) { @@ -211,12 +219,13 @@ class LSformElement_password extends LSformElement { } function getMailAttrs() { - if (!isset($this -> params['html_options']['mail']) || !is_array($this -> params['html_options']['mail'])) + if (!$this -> getParam('html_options.mail')) return False; - if (isset($this -> params['html_options']['mail']['get_mail_attr_function'])) { - if (is_callable($this -> params['html_options']['mail']['get_mail_attr_function'])) { + if ($this -> getParam('html_options.mail.get_mail_attr_function')) { + $func = $this -> getParam('html_options.mail.get_mail_attr_function'); + if (is_callable($func)) { try { - return call_user_func_array($this -> params['html_options']['mail']['get_mail_attr_function'], array(&$this)); + return call_user_func_array($func, array(&$this)); } catch(Exception $e) { LSerror :: addErrorCode('LSformElement_password_05', $e->getMessage()); @@ -227,9 +236,7 @@ class LSformElement_password extends LSformElement { return False; } } - elseif (isset($this -> params['html_options']['mail']['mail_attr'])) { - return $this -> params['html_options']['mail']['mail_attr']; - } + return $this -> getParam('html_options.mail.mail_attr'); } function send($params) { @@ -266,15 +273,9 @@ class LSformElement_password extends LSformElement { if (checkEmail($mail,NULL,true)) { $this -> attr_html -> attribute -> ldapObject -> registerOtherValue('password',$this -> sendMail['pwd']); $msg = $this -> attr_html -> attribute -> ldapObject -> getFData($this -> sendMail['msg']); - if (isset($this -> params['html_options']['mail']['headers'])) { - $headers = $this -> params['html_options']['mail']['headers']; - } - else { - $headers = array(); - } - if ($this -> params['html_options']['mail']['bcc']) { - $headers['Bcc']=$this -> params['html_options']['mail']['bcc']; - } + $headers = $this -> getParam('html_options.mail.headers', array()); + $bcc = $this -> getParam('html_options.mail.bcc'); + if ($bcc) $headers['Bcc'] = $bcc; if (sendMail( $mail, $this -> sendMail['subject'], @@ -349,10 +350,7 @@ class LSformElement_password extends LSformElement { } public function isLoginPassword() { - if (!isset($this -> params['html_options']['isLoginPassword']) || $this -> params['html_options']['isLoginPassword']) { - return true; - } - return false; + return $this -> getParam('html_options.isLoginPassword', true); } } diff --git a/public_html/includes/class/class.LSformElement_postaladdress.php b/public_html/includes/class/class.LSformElement_postaladdress.php index 67f7375d..5b5f48f6 100644 --- a/public_html/includes/class/class.LSformElement_postaladdress.php +++ b/public_html/includes/class/class.LSformElement_postaladdress.php @@ -43,23 +43,25 @@ class LSformElement_postaladdress extends LSformElement_textarea { $return = parent :: getDisplay(); if ($this -> isFreeze()) { if (!empty($this->values)) { - $map_url_format=(isset($this -> params['html_options']['map_url_format'])?$this -> params['html_options']['map_url_format']:'http://nominatim.openstreetmap.org/search.php?q=%{pattern}'); - if (isset($this -> params['html_options']['map_url_pattern_generate_function'])) { - if (is_callable($this -> params['html_options']['map_url_pattern_generate_function'])) { - $this -> attr_html -> attribute -> ldapObject -> registerOtherValue('pattern',call_user_func($this -> params['html_options']['map_url_pattern_generate_function'],$this)); + $map_url_format = $this -> getParam('html_options.map_url_format', 'http://nominatim.openstreetmap.org/search.php?q=%{pattern}', 'string'); + $map_url_pattern_generate_function = $this -> getParam('html_options.map_url_pattern_generate_function'); + $map_url_pattern_format = $this -> getParam('html_options.map_url_pattern_format'); + if ($map_url_pattern_generate_function) { + if (is_callable($map_url_pattern_generate_function)) { + $this -> attr_html -> attribute -> ldapObject -> registerOtherValue('pattern', call_user_func($map_url_pattern_generate_function, $this)); } else { - LSerror::addErrorCode('LSformElement_postaladdress_01', $this -> params['html_options']['map_url_pattern_generate_function']); + LSerror::addErrorCode('LSformElement_postaladdress_01', $map_url_pattern_generate_function); } } - elseif (isset($this -> params['html_options']['map_url_pattern_format'])) { - $pattern=$this -> attr_html -> attribute -> ldapObject -> getFData($this -> params['html_options']['map_url_pattern_format']); - $pattern=str_replace("\n"," ",$pattern); - $pattern=urlencode($pattern); - $this -> attr_html -> attribute -> ldapObject -> registerOtherValue('pattern',$pattern); + elseif ($map_url_pattern_format) { + $pattern = $this -> attr_html -> attribute -> ldapObject -> getFData($map_url_pattern_format); + $pattern = str_replace("\n"," ",$pattern); + $pattern = urlencode($pattern); + $this -> attr_html -> attribute -> ldapObject -> registerOtherValue('pattern', $pattern); } else { - $this -> attr_html -> attribute -> ldapObject -> registerOtherValue('pattern',LSformElement_postaladdress__generate_pattern($this)); + $this -> attr_html -> attribute -> ldapObject -> registerOtherValue('pattern', LSformElement_postaladdress__generate_pattern($this)); } LSsession :: addJSconfigParam('LSformElement_postaladdress_'.$this -> name, array ( 'map_url' => $this -> attr_html -> attribute -> ldapObject -> getFData($map_url_format) diff --git a/public_html/includes/class/class.LSformElement_quota.php b/public_html/includes/class/class.LSformElement_quota.php index 176a6db7..43cad1bb 100644 --- a/public_html/includes/class/class.LSformElement_quota.php +++ b/public_html/includes/class/class.LSformElement_quota.php @@ -149,10 +149,7 @@ class LSformElement_quota extends LSformElement { } private function getFactor() { - if (isset($this -> params['html_options']['factor'])) { - return $this -> params['html_options']['factor']; - } - return 1; + return $this -> getParam('html_options.factor', 1); } } diff --git a/public_html/includes/class/class.LSformElement_select.php b/public_html/includes/class/class.LSformElement_select.php index fd2933f8..22b6c407 100644 --- a/public_html/includes/class/class.LSformElement_select.php +++ b/public_html/includes/class/class.LSformElement_select.php @@ -73,8 +73,8 @@ class LSformElement_select extends LSformElement { */ public function isValidValue($value,$possible_values=False) { if (!is_array($possible_values)) { - if (isset($this) && is_a($this, __CLASS__) && is_array($this -> params['text_possible_values'])) { - $possible_values=$this -> params['text_possible_values']; + if (isset($this) && is_a($this, __CLASS__) && $this -> getParam('text_possible_values')) { + $possible_values = $this -> getParam('text_possible_values'); } else { return False; diff --git a/public_html/includes/class/class.LSformElement_select_box.php b/public_html/includes/class/class.LSformElement_select_box.php index 46a01f63..c9f40ce8 100644 --- a/public_html/includes/class/class.LSformElement_select_box.php +++ b/public_html/includes/class/class.LSformElement_select_box.php @@ -47,7 +47,7 @@ class LSformElement_select_box extends LSformElement_select { if (!$this -> isFreeze()) { LSsession :: addCssFile('LSformElement_select_box.css'); } - $this -> fetchVariables['translate_labels'] = ((isset($this -> params['translate_labels']) && !$this -> params['translate_labels'])?false:true); + $this -> fetchVariables['translate_labels'] = $this -> getParam('translate_labels', true); return parent :: getDisplay(); } diff --git a/public_html/includes/class/class.LSformElement_select_object.php b/public_html/includes/class/class.LSformElement_select_object.php index 3f279f8c..882e3f50 100644 --- a/public_html/includes/class/class.LSformElement_select_object.php +++ b/public_html/includes/class/class.LSformElement_select_object.php @@ -69,9 +69,9 @@ class LSformElement_select_object extends LSformElement { 'deleteBtns' => _('Delete'), 'up_label' => _('Move up'), 'down_label' => _('Move down'), - 'ordered' => (($this -> params['html_options']['ordered'])?1:0), - 'multiple' => (($this -> params['multiple'])?1:0), - 'filter64' => (($this -> params['html_options']['selectable_object']['filter'])?base64_encode($this -> params['html_options']['selectable_object']['filter']):''), + 'ordered' => $this -> getParam('html_options.ordered', 0, 'int'), + 'multiple' => $this -> getParam('multiple', 0, 'int'), + 'filter64' => base64_encode($this -> getParam('html_options.selectable_object.filter', '', 'string')), 'noValueLabel' => _('No set value'), 'noResultLabel' => _('No result') ) @@ -93,7 +93,7 @@ class LSformElement_select_object extends LSformElement { } } - if ((!isset($this -> params['html_options']['sort']) || $this -> params['html_options']['sort']) && !$this -> params['html_options']['ordered']) { + if ($this -> getParam('html_options.sort', true) && !$this -> getParam('html_options.ordered', false, 'bool')) { uasort($this -> values,array($this,'_sortTwoValues')); } @@ -114,7 +114,7 @@ class LSformElement_select_object extends LSformElement { * @retval int Value for uasort **/ private function _sortTwoValues(&$va,&$vb) { - if (isset($this -> params['html_options']['sortDirection']) && $this -> params['html_options']['sortDirection']=='DESC') { + if ($this -> getParam('html_options.sortDirection') == 'DESC') { $dir=-1; } else { @@ -176,12 +176,21 @@ class LSformElement_select_object extends LSformElement { * @retval array(dn -> displayName) Found objects */ function searchAdd ($pattern) { - if (is_array($this -> params['html_options']['selectable_object'])) { - if (LSsession :: loadLSobject($this -> params['html_options']['selectable_object']['object_type'])) { - $obj = new $this -> params['html_options']['selectable_object']['object_type'](); + if ($this -> getParam('html_options.selectable_object')) { + $obj_type = $this -> getParam('html_options.selectable_object.object_type'); + if (LSsession :: loadLSobject($obj_type)) { + $obj = new $obj_type(); $sparams = array(); - $sparams['onlyAccessible'] = (isset($this -> params['html_options']['selectable_object']['onlyAccessible'])?$this -> params['html_options']['selectable_object']['onlyAccessible']:FALSE); - $ret = $obj -> getSelectArray($pattern,NULL,$this -> params['html_options']['selectable_object']['display_name_format'],false,true,(isset($this -> params['html_options']['selectable_object']['filter'])?$this -> params['html_options']['selectable_object']['filter']:NULL),$sparams); + $sparams['onlyAccessible'] = $this -> getParam('html_options.selectable_object.onlyAccessible', false, 'bool'); + $ret = $obj -> getSelectArray( + $pattern, + NULL, + $this -> getParam('html_options.selectable_object.display_name_format'), + false, + true, + $this -> getParam('html_options.selectable_object.filter'), + $sparams + ); if (is_array($ret)) { return $ret; } diff --git a/public_html/includes/class/class.LSformElement_text.php b/public_html/includes/class/class.LSformElement_text.php index 3cd484b6..1c2db4ee 100644 --- a/public_html/includes/class/class.LSformElement_text.php +++ b/public_html/includes/class/class.LSformElement_text.php @@ -48,8 +48,8 @@ class LSformElement_text extends LSformElement { $return = $this -> getLabelInfos(); // value if (!$this -> isFreeze()) { - if (isset($this -> params['html_options'])) { - LSsession :: addJSconfigParam($this -> name,$this -> params['html_options']); + if ($this -> getParam('html_options')) { + LSsession :: addJSconfigParam($this -> name, $this -> getParam('html_options')); } LSsession :: addHelpInfos( 'LSformElement_text', diff --git a/public_html/includes/class/class.LSformElement_valueWithUnit.php b/public_html/includes/class/class.LSformElement_valueWithUnit.php index 1c369d97..0995524b 100644 --- a/public_html/includes/class/class.LSformElement_valueWithUnit.php +++ b/public_html/includes/class/class.LSformElement_valueWithUnit.php @@ -41,15 +41,17 @@ class LSformElement_valueWithUnit extends LSformElement { * Si le parametre units n'est pas defini, cette fonction retournera False **/ function getUnits() { - if (isset($this -> params['html_options']['units']) && is_array($this -> params['html_options']['units'])) { - $units=array(); - foreach($this -> params['html_options']['units'] as $sill => $label) { - $units[$sill]=((!isset($this -> params['html_options']['translate_labels']) || $this -> params['html_options']['translate_labels'])?__($label):$label); + $units = $this -> getParam('html_options.units'); + if (is_array($units)) { + if ($this -> getParam('html_options.translate_labels', true)) { + foreach($units as $sill => $label) { + $units[$sill] = __($label); + } } krsort($units); return $units; } - LSerror :: addErrorCode('LSformElement_valueWithUnit_01',$this -> name); + LSerror :: addErrorCode('LSformElement_valueWithUnit_01', $this -> name); return; } @@ -66,9 +68,9 @@ class LSformElement_valueWithUnit extends LSformElement { function formatNumber($number) { if ((int)$number==$number) return $number; return number_format($number, - (isset($this -> params['html_options']['nb_decimals'])?$this -> params['html_options']['nb_decimals']:2), - (isset($this -> params['html_options']['dec_point'])?$this -> params['html_options']['dec_point']:","), - (isset($this -> params['html_options']['thousands_sep'])?$this -> params['html_options']['thousands_sep']:" ") + $this -> getParam('html_options.nb_decimals', 2, 'int'), + $this -> getParam('html_options.dec_point', ',', 'string'), + $this -> getParam('html_options.thousands_sep', ' ', 'string') ); } @@ -163,8 +165,8 @@ class LSformElement_valueWithUnit extends LSformElement { if (isset($_POST[$this -> name.'_unitFact'][$key]) && ($_POST[$this -> name.'_unitFact'][$key]!=1)) { $f = $_POST[$this -> name.'_unitFact'][$key]; } - if (isset($this -> params['html_options']['store_integer']) && $this -> params['html_options']['store_integer']) { - if (isset($this -> params['html_options']['round_down']) && $this -> params['html_options']['round_down']) { + if ($this -> getParam('html_options.store_integer'])) { + if ($this -> getParam('html_options.round_down')) { $return[$this -> name][$key] = floor($val*$f); } else { diff --git a/public_html/includes/class/class.LSformElement_wysiwyg.php b/public_html/includes/class/class.LSformElement_wysiwyg.php index a9f8d95d..7bd6b735 100644 --- a/public_html/includes/class/class.LSformElement_wysiwyg.php +++ b/public_html/includes/class/class.LSformElement_wysiwyg.php @@ -47,7 +47,7 @@ class LSformElement_wysiwyg extends LSformElement { LSsession :: addJSconfigParam( $this -> name, array( - 'extra_options' => (isset($this -> params['html_options']['extra_options'])?$this -> params['html_options']['extra_options']:array()), + 'extra_options' => $this -> getParam('html_options.extra_options', array()), ) ); } diff --git a/public_html/includes/class/class.LSldapObject.php b/public_html/includes/class/class.LSldapObject.php index abd14e85..254445d5 100644 --- a/public_html/includes/class/class.LSldapObject.php +++ b/public_html/includes/class/class.LSldapObject.php @@ -68,7 +68,7 @@ class LSldapObject { return; } - foreach($this -> config['attrs'] as $attr_name => $attr_config) { + foreach($this -> getConfig('attrs', array()) as $attr_name => $attr_config) { if(!$this -> attrs[$attr_name]=new LSattribute($attr_name,$attr_config,$this)) { return; } @@ -127,7 +127,7 @@ class LSldapObject { * @retval string Format d'affichage de l'objet. */ function getDisplayNameFormat() { - return $this -> config['display_name_format']; + return $this -> getConfig('display_name_format'); } /** @@ -589,7 +589,7 @@ class LSldapObject { $new = $this -> isNew(); foreach($this -> attrs as $attr) { if(($attr -> isUpdate())&&($attr -> isValidate())) { - if(($attr -> name == $this -> config['rdn'])&&(!$new)) { + if(($attr -> name == $this -> getConfig('rdn')) && (!$new)) { $new = true; LSdebug('Rename'); if (!$this -> fireEvent('before_rename')) { @@ -838,7 +838,7 @@ class LSldapObject { */ function searchObject($name,$basedn=NULL,$filter=NULL,$params=NULL) { if (!$filter) { - $filter = '('.$this -> config['rdn'].'='.$name.')'; + $filter = '('.$this -> getConfig('rdn').'='.$name.')'; } else { $filter = getFData($filter,$name); @@ -952,14 +952,14 @@ class LSldapObject { else { $container_dn=$this -> getContainerDn(); if ($container_dn) { - $rdn_attr=$this -> config['rdn']; - if( (isset($this -> config['rdn'])) && (isset($this -> attrs[$rdn_attr])) ) { + $rdn_attr = $this -> getConfig('rdn'); + if( $rdn_attr && isset($this -> attrs[$rdn_attr]) ) { $rdn_val=$this -> attrs[$rdn_attr] -> getUpdateData(); if (!empty($rdn_val)) { return $rdn_attr.'='.$rdn_val[0].','.$container_dn; } else { - LSerror :: addErrorCode('LSldapObject_12',$this -> config['rdn']); + LSerror :: addErrorCode('LSldapObject_12', $rdn_attr); return; } } @@ -983,10 +983,12 @@ class LSldapObject { */ function getContainerDn() { $topDn = LSsession :: getTopDn(); - if (isset($this -> config['generate_container_dn'])) { - if (is_callable($this -> config['generate_container_dn'])) { + $generate_container_dn = $this -> getConfig('generate_container_dn'); + $container_dn = $this -> getConfig('container_dn'); + if ($generate_container_dn) { + if (is_callable($generate_container_dn)) { try { - $container_dn=$this -> config['generate_container_dn']($this); + $container_dn = call_user_func_array($generate_container_dn, array(&$this)); return $container_dn.','.$topDn; } catch (Exception $e) { @@ -994,11 +996,11 @@ class LSldapObject { } } else { - LSerror :: addErrorCode('LSldapObject_33',$this -> config['generate_container_dn']); + LSerror :: addErrorCode('LSldapObject_33', $generate_container_dn); } } - else if ((isset($this -> config['container_dn'])) && ($topDn)) { - return $this -> config['container_dn'].','.$topDn; + else if ($container_dn && $topDn) { + return $container_dn.','.$topDn; } else { LSerror :: addErrorCode('LSldapObject_11',$this -> getType()); @@ -1118,11 +1120,12 @@ class LSldapObject { */ function updateLSrelationsCache() { $this -> _LSrelationsCache=array(); - if (is_array($this->config['LSrelation']) && LSsession :: loadLSclass('LSrelation')) { + $LSrelations = $this -> getConfig('LSrelation'); + if (is_array($LSrelations) && LSsession :: loadLSclass('LSrelation')) { $type = $this -> getType(); $me = new $type(); $me -> loadData($this -> getDn()); - foreach($this->config['LSrelation'] as $relation_name => $relation_conf) { + foreach($LSrelations as $relation_name => $relation_conf) { $relation = new LSrelation($me, $relation_name); $list = $relation -> listRelatedObjects(); if (is_array($list)) { @@ -1659,16 +1662,13 @@ class LSldapObject { $return = $this -> fireObjectEvent($event); // Config - if(isset($this -> config[$event])) { - if (!is_array($this -> config[$event])) { + $funcs = $this -> getConfig($event); + if($funcs) { + if (!is_array($funcs)) $funcs = array($this -> config[$event]); - } - else { - $funcs = $this -> config[$event]; - } foreach($funcs as $func) { if(function_exists($func)) { - if(!call_user_func_array($func,array(&$this))) { + if(!call_user_func_array($func, array(&$this))) { $return = false; LSerror :: addErrorCode('LSldapObject_07',array('func' => $func,'event' => $event)); } @@ -1801,8 +1801,9 @@ class LSldapObject { return $this -> cache['subDnName']; } elseif ($key=='rdn') { - if ($this -> config['rdn'] && isset($this -> attrs[ $this -> config['rdn'] ])) { - return $this -> attrs[ $this -> config['rdn'] ] -> getValue(); + $rdn_attr = $this -> getConfig('rdn'); + if ($rdn_attr && isset($this -> attrs[ $rdn_attr ])) { + return $this -> attrs[ $rdn_attr ] -> getValue(); } return false; } @@ -1815,9 +1816,10 @@ class LSldapObject { **/ function listValidIOformats() { $ret=array(); - if (isset($this -> config['ioFormat']) && is_array($this -> config['ioFormat'])) { - foreach($this -> config['ioFormat'] as $name => $conf) { - $ret[$name]=_((isset($conf['label'])?$conf['label']:$name)); + $ioFormats = $this -> getConfig('ioFormat'); + if (is_array($ioFormats)) { + foreach($ioFormats as $name => $conf) { + $ret[$name] = _((isset($conf['label'])?$conf['label']:$name)); } } return $ret; @@ -1831,12 +1833,22 @@ class LSldapObject { * @retval boolean True if it's a valid IOformat, false otherwise **/ function isValidIOformat($f) { - if (isset($this -> config['ioFormat']) && is_array($this -> config['ioFormat']) && isset($this -> config['ioFormat'][$f])) { - return True; - } - return False; + return is_array($this -> getConfig("ioFormat.$f")); } - + + /** + * Return a configuration parameter (or default value) + * + * @param[] $param The configuration parameter + * @param[] $default The default value (default : null) + * @param[] $cast Cast resulting value in specific type (default : disabled) + * + * @retval mixed The configuration parameter value or default value if not set + **/ + public function getConfig($param, $default=null, $cast=null) { + return LSconfig :: get($param, $default, $cast, $this -> config); + } + } /** diff --git a/public_html/includes/class/class.LSsearch.php b/public_html/includes/class/class.LSsearch.php index 1bdfc698..baa5df30 100644 --- a/public_html/includes/class/class.LSsearch.php +++ b/public_html/includes/class/class.LSsearch.php @@ -123,7 +123,7 @@ class LSsearch { */ private function loadConfig() { $this -> config = LSconfig::get("LSobjects.".$this -> LSobject.".LSsearch"); - if (is_array($this -> config['predefinedFilters'])) { + if (isset($this -> config['predefinedFilters']) && is_array($this -> config['predefinedFilters'])) { foreach($this -> config['predefinedFilters'] as $filter => $label) { if(!LSldap::isValidFilter($filter)) { LSerror::addErrorCode('LSsearch_15',array('label' => $label, 'filter' => $filter, 'type' => $this -> LSobject)); @@ -179,7 +179,7 @@ class LSsearch { foreach ($params as $param => $value) { if ( !isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context][$param]) || $_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context][$param]!=$value) { - LSdebug("S: $param => $value"); + LSdebug("S: $param => ".varDump($value)); $_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context][$param]=$value; } } @@ -372,8 +372,8 @@ class LSsearch { } else { $this -> params['sortBy'] = $params['sortBy']; - if (!is_string($params['sortDirection'])) { - $this -> params['sortDirection']='ASC'; + if (!isset($params['sortDirection']) || !is_string($params['sortDirection'])) { + $this -> params['sortDirection'] = 'ASC'; } } }