public function getAvailableValues()
 {
     if (!empty($this->fields['values'])) {
         $ldap_values = json_decode($this->fields['values']);
         $ldap_dropdown = new RuleRightParameter();
         if (!$ldap_dropdown->getFromDB($ldap_values->ldap_attribute)) {
             return array();
         }
         $attribute = array($ldap_dropdown->fields['value']);
         $config_ldap = new AuthLDAP();
         if (!$config_ldap->getFromDB($ldap_values->ldap_auth)) {
             return array();
         }
         if (!function_exists('warning_handler')) {
             function warning_handler($errno, $errstr, $errfile, $errline, array $errcontext)
             {
                 if (0 === error_reporting()) {
                     return false;
                 }
                 throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
             }
         }
         set_error_handler("warning_handler", E_WARNING);
         try {
             $tab_values = array();
             $ds = $config_ldap->connect();
             ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
             $cookie = '';
             do {
                 if (AuthLDAP::isLdapPageSizeAvailable($config_ldap)) {
                     ldap_control_paged_result($ds, $config_ldap->fields['pagesize'], true, $cookie);
                 }
                 $result = ldap_search($ds, $config_ldap->fields['basedn'], $ldap_values->ldap_filter, $attribute);
                 $entries = ldap_get_entries($ds, $result);
                 array_shift($entries);
                 foreach ($entries as $id => $attr) {
                     if (isset($attr[$attribute[0]]) && !in_array($attr[$attribute[0]][0], $tab_values)) {
                         $tab_values[$id] = $attr[$attribute[0]][0];
                     }
                 }
                 if (AuthLDAP::isLdapPageSizeAvailable($config_ldap)) {
                     ldap_control_paged_result_response($ds, $result, $cookie);
                 }
             } while ($cookie !== null && $cookie != '');
             if ($this->fields['show_empty']) {
                 $tab_values = array('' => '-----') + $tab_values;
             }
             asort($tab_values);
             return $tab_values;
         } catch (Exception $e) {
             return array();
         }
         restore_error_handler();
     } else {
         return array();
     }
 }
Esempio n. 2
0
 /**
  * Validate form fields before add or update a question
  *
  * @param  Array $input Datas used to add the item
  *
  * @return Array        The modified $input array
  *
  * @param  [type] $input [description]
  * @return [type]        [description]
  */
 private function checkBeforeSave($input)
 {
     // Control fields values :
     // - name is required
     if (empty($input['name'])) {
         Session::addMessageAfterRedirect(__('The title is required', 'formcreator'), false, ERROR);
         return array();
     }
     // - field type is required
     if (empty($input['fieldtype'])) {
         Session::addMessageAfterRedirect(__('The field type is required', 'formcreator'), false, ERROR);
         return array();
     }
     // - section is required
     if (empty($input['plugin_formcreator_sections_id'])) {
         Session::addMessageAfterRedirect(__('The section is required', 'formcreator'), false, ERROR);
         return array();
     }
     // Values are required for GLPI dropdowns, dropdowns, multiple dropdowns, checkboxes, radios, LDAP
     $itemtypes = array('select', 'multiselect', 'checkboxes', 'radios', 'ldap');
     if (empty($input['values']) && in_array($input['fieldtype'], $itemtypes)) {
         Session::addMessageAfterRedirect(__('The field value is required:', 'formcreator') . ' ' . $input['name'], false, ERROR);
         return array();
     }
     // Fields are differents for dropdown lists, so we need to replace these values into the good ones
     if ($input['fieldtype'] == 'dropdown') {
         if (empty($input['dropdown_values'])) {
             Session::addMessageAfterRedirect(__('The field value is required:', 'formcreator') . ' ' . $input['name'], false, ERROR);
             return array();
         }
         $input['values'] = $input['dropdown_values'];
         $input['default_values'] = isset($input['dropdown_default_value']) ? $input['dropdown_default_value'] : '';
     }
     // Fields are differents for GLPI object lists, so we need to replace these values into the good ones
     if ($input['fieldtype'] == 'glpiselect') {
         if (empty($input['glpi_objects'])) {
             Session::addMessageAfterRedirect(__('The field value is required:', 'formcreator') . ' ' . $input['name'], false, ERROR);
             return array();
         }
         $input['values'] = $input['glpi_objects'];
         $input['default_values'] = isset($input['dropdown_default_value']) ? $input['dropdown_default_value'] : '';
     }
     // A description field should have a description
     if ($input['fieldtype'] == 'description' && empty($input['description'])) {
         Session::addMessageAfterRedirect(__('A description field should have a description:', 'formcreator') . ' ' . $input['name'], false, ERROR);
         return array();
     }
     // format values for numbers
     if ($input['fieldtype'] == 'integer' || $input['fieldtype'] == 'float') {
         $input['default_values'] = !empty($input['default_values']) ? (double) str_replace(',', '.', $input['default_values']) : null;
         $input['range_min'] = !empty($input['range_min']) ? (double) str_replace(',', '.', $input['range_min']) : null;
         $input['range_max'] = !empty($input['range_max']) ? (double) str_replace(',', '.', $input['range_max']) : null;
     }
     // LDAP fields validation
     if ($input['fieldtype'] == 'ldapselect') {
         // Fields are differents for dropdown lists, so we need to replace these values into the good ones
         if (!empty($input['ldap_auth'])) {
             $config_ldap = new AuthLDAP();
             $config_ldap->getFromDB($input['ldap_auth']);
             $ldap_dropdown = new RuleRightParameter();
             $ldap_dropdown->getFromDB($input['ldap_attribute']);
             $attribute = array($ldap_dropdown->fields['value']);
             // Set specific error handler too catch LDAP errors
             if (!function_exists('warning_handler')) {
                 function warning_handler($errno, $errstr, $errfile, $errline, array $errcontext)
                 {
                     if (0 === error_reporting()) {
                         return false;
                     }
                     throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
                 }
             }
             set_error_handler("warning_handler", E_WARNING);
             try {
                 $ds = $config_ldap->connect();
                 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
                 ldap_control_paged_result($ds, 1);
                 $sn = ldap_search($ds, $config_ldap->fields['basedn'], $input['ldap_filter'], $attribute);
                 $entries = ldap_get_entries($ds, $sn);
             } catch (Exception $e) {
                 Session::addMessageAfterRedirect(__('Cannot recover LDAP informations!', 'formcreator'), false, ERROR);
             }
             restore_error_handler();
             $input['values'] = json_encode(array('ldap_auth' => $input['ldap_auth'], 'ldap_filter' => $input['ldap_filter'], 'ldap_attribute' => strtolower($input['ldap_attribute'])));
         }
     }
     // Add leading and trailing regex marker automaticaly
     if (!empty($input['regex'])) {
         if (substr($input['regex'], 0, 1) != '/') {
             if (substr($input['regex'], 0, 1) != '^') {
                 $input['regex'] = '/^' . $input['regex'];
             } else {
                 $input['regex'] = '/' . $input['regex'];
             }
         }
         if (substr($input['regex'], -1, 1) != '/') {
             if (substr($input['regex'], -1, 1) != '$') {
                 $input['regex'] = $input['regex'] . '$/';
             } else {
                 $input['regex'] = $input['regex'] . '/';
             }
         }
     }
     return $input;
 }
 public static function displayValue($value, $values)
 {
     if (!empty($values)) {
         $ldap_values = json_decode($values);
         $ldap_dropdown = new RuleRightParameter();
         $ldap_dropdown->getFromDB($ldap_values->ldap_attribute);
         $attribute = array($ldap_dropdown->fields['value']);
         $config_ldap = new AuthLDAP();
         $config_ldap->getFromDB($ldap_values->ldap_auth);
         $ds = $config_ldap->connect();
         $sn = ldap_search($ds, $config_ldap->fields['basedn'], $ldap_values->ldap_filter, $attribute);
         $entries = ldap_get_entries($ds, $sn);
         array_shift($entries);
         $tab_values = array();
         foreach ($entries as $id => $attr) {
             if (isset($attr[$attribute[0]]) && !in_array($attr[$attribute[0]][0], $tab_values)) {
                 $tab_values[$id] = $attr[$attribute[0]][0];
             }
         }
         sort($tab_values);
     }
     return $value != '' ? $tab_values[$value] : '';
 }