Skip to content

Commit

Permalink
Merge pull request #1531 from OCSInventory-NG/add_config_ldap
Browse files Browse the repository at this point in the history
Add config ldap
  • Loading branch information
Lea9250 committed Jun 30, 2023
2 parents fa56edf + 916ac4e commit f5ce869
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 24 deletions.
2 changes: 1 addition & 1 deletion backend/AUTH/methode/ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* */

connexion_local_read();
$sql = "select substr(NAME,7) as NAME,TVALUE from config where NAME like '%s'";
$sql = "select substr(NAME,7) as NAME,TVALUE from config_ldap where NAME like '%s'";
$arg = array('%CONEX%');
$res = mysql2_query_secure($sql, $_SESSION['OCS']["readServer"], $arg);

Expand Down
14 changes: 14 additions & 0 deletions files/update/7074.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Create the new config_ldap table
CREATE TABLE `config_ldap` (
`NAME` varchar(50) NOT NULL,
`IVALUE` int default NULL,
`TVALUE` text default NULL,
`COMMENTS` text default NULL,
PRIMARY KEY (`NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- Copy all ldap config from config table
INSERT INTO config_ldap SELECT * FROM config WHERE NAME LIKE '%CONEX%';

-- Remove all ldap config from config table
DELETE FROM config WHERE NAME LIKE '%CONEX%';
11 changes: 8 additions & 3 deletions require/function_commun.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@
//looking for default value of ocs config
//default_values => replace with your data if config data is null or empty
//default_values => array(array())// ex: array('LOCAL_SERVER'=>array('TVALUE'=>'http:\\localhost'))
function look_config_default_values($field_name, $like = '', $default_values = '') {
function look_config_default_values($field_name, $like = '', $default_values = '', $is_ldap = false) {
if ($is_ldap) {
$config = 'config_ldap';
} else {
$config = 'config';
}
if ($like == '') {
$sql = "select NAME,IVALUE,TVALUE,COMMENTS from config where NAME in ";
$sql = "select NAME,IVALUE,TVALUE,COMMENTS from $config where NAME in ";
$arg_sql = array();
$arg = mysql2_prepare($sql, $arg_sql, $field_name);
} else {
$arg['SQL'] = "select NAME,IVALUE,TVALUE,COMMENTS from config where NAME like '%s'";
$arg['SQL'] = "select NAME,IVALUE,TVALUE,COMMENTS from $config where NAME like '%s'";
$arg['ARG'] = $field_name;
}
$resdefaultvalues = mysql2_query_secure($arg['SQL'], $_SESSION['OCS']["readServer"], $arg['ARG']);
Expand Down
54 changes: 35 additions & 19 deletions require/function_config_generale.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,15 @@ function insert_update($name, $value, $default_value, $field) {
if ($default_value != $value) {
$arg = array($field, $value, $name);

$config = "config";
if (strpos($arg[2], "CONEX") !== false) {
$config = "config_ldap";
}

if ($default_value != '') {
$sql = "update config set %s = '%s' where NAME ='%s'";
$sql = "update $config set %s = '%s' where NAME ='%s'";
} else {
$sql = "insert into config (%s, NAME) value ('%s','%s')";
$sql = "insert into $config (%s, NAME) value ('%s','%s')";
}
mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"], $arg, $l->g(821));

Expand Down Expand Up @@ -420,18 +425,29 @@ function update_default_value($POST) {
//tableau des champs ou il faut interpréter la valeur retourner et mettre à jour ivalue
$array_interprete_ivalue = array('FREQUENCY' => 'FREQUENCY_edit', 'IPDISCOVER' => 'IPDISCOVER_edit');

//recherche des valeurs par défaut
$sql_exist = " select NAME,ivalue,tvalue from config ";
$result_exist = mysql2_query_secure($sql_exist, $_SESSION['OCS']["readServer"]);
while ($value_exist = mysqli_fetch_array($result_exist)) {
if ($value_exist["ivalue"] != null) {
$optexist[$value_exist["NAME"]] = $value_exist["ivalue"];
} elseif ($value_exist["tvalue"] != null) {
$optexist[$value_exist["NAME"]] = $value_exist["tvalue"];
} elseif ($value_exist["tvalue"] == null && $value_exist["ivalue"] == null) {
$optexist[$value_exist["NAME"]] = 'null';
function fetchValues($table_name, $readServer)
{
$optexist = array();

$sql_exist = "SELECT NAME, ivalue, tvalue FROM $table_name";
$result_exist = mysql2_query_secure($sql_exist, $readServer);

while ($value_exist = mysqli_fetch_array($result_exist)) {
if ($value_exist["ivalue"] != null) {
$optexist[$value_exist["NAME"]] = $value_exist["ivalue"];
} elseif ($value_exist["tvalue"] != null) {
$optexist[$value_exist["NAME"]] = $value_exist["tvalue"];
} elseif ($value_exist["tvalue"] == null && $value_exist["ivalue"] == null) {
$optexist[$value_exist["NAME"]] = 'null';
}
}

return $optexist;
}

$optexist = fetchValues("config", $_SESSION['OCS']["readServer"]);
$optexist = array_merge($optexist, fetchValues("config_ldap", $_SESSION['OCS']["readServer"]));

//pour obliger à prendre en compte
//le AUTO_DUPLICATE_LVL quand il est vide
//on doit l'initialiser tout le temps
Expand Down Expand Up @@ -548,7 +564,7 @@ function nb_ldap_filters($nb, $default = false) {

if ($default == false) {
// old values = from config table
$sql = "SELECT * FROM config WHERE NAME REGEXP '^CONEX_LDAP_FILTER[0-9]*$'";
$sql = "SELECT * FROM config_ldap WHERE NAME REGEXP '^CONEX_LDAP_FILTER[0-9]*$'";
$old_filters = mysql2_query_secure($sql, $_SESSION['OCS']["readServer"]);
$nb_old = $old_filters->num_rows;
$filters_result = mysqli_fetch_all($old_filters, MYSQLI_ASSOC);
Expand All @@ -568,15 +584,15 @@ function nb_ldap_filters($nb, $default = false) {
$i++;
$filter_name = "CONEX_LDAP_FILTER$i";
$filter_role = "CONEX_LDAP_FILTER".$i."_ROLE";
$sql = "INSERT INTO config VALUES ('$filter_name', '', '', NULL), ('$filter_role', '', '', NULL)";
$sql = "INSERT INTO config_ldap VALUES ('$filter_name', '', '', NULL), ('$filter_role', '', '', NULL)";
$ok = mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"]);

}
} elseif ($nb < $nb_old) { // filters to be removed
$i = $last_filter;
while ($i >= $nb + 1) {
$filter_name = "CONEX_LDAP_FILTER$i";
$sql = "DELETE FROM config WHERE NAME = 'CONEX_LDAP_FILTER$i' OR NAME = 'CONEX_LDAP_FILTER".$i."_ROLE'";
$sql = "DELETE FROM config_ldap WHERE NAME = 'CONEX_LDAP_FILTER$i' OR NAME = 'CONEX_LDAP_FILTER".$i."_ROLE'";
$ok = mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"]);
$i--;
}
Expand All @@ -585,7 +601,7 @@ function nb_ldap_filters($nb, $default = false) {

$ldap_filters = [];

$sql = "SELECT * FROM config WHERE NAME REGEXP '^CONEX_LDAP_FILTER[0-9]*'";
$sql = "SELECT * FROM config_ldap WHERE NAME REGEXP '^CONEX_LDAP_FILTER[0-9]*'";
$filters = mysql2_query_secure($sql, $_SESSION['OCS']["readServer"]);
$filters = mysqli_fetch_all($filters, MYSQLI_ASSOC);
// sort ldap filters
Expand Down Expand Up @@ -1059,7 +1075,7 @@ function pageConnexion() {
'CONEX_ROOT_PW' => 'CONEX_ROOT_PW',
'CONEX_LDAP_NB_FILTERS' => 'CONEX_LDAP_NB_FILTERS',
'CONEX_LDAP_CHECK_DEFAULT_ROLE' => 'CONEX_LDAP_CHECK_DEFAULT_ROLE');
$values = look_config_default_values($champs);
$values = look_config_default_values($champs, '', '', true);

$role1 = get_profile_labels();

Expand All @@ -1081,10 +1097,10 @@ function pageConnexion() {
ligne('CONEX_LDAP_CHECK_DEFAULT_ROLE', $l->g(1277), 'select', array('VALUE' => $values['tvalue']['CONEX_LDAP_CHECK_DEFAULT_ROLE'] ?? '', 'SELECT_VALUE' => $default_role));
ligne('CONEX_LDAP_NB_FILTERS', $l->g(9650), 'select', array('VALUE' => $values['tvalue']['CONEX_LDAP_NB_FILTERS'] ?? '', 'SELECT_VALUE' => $nb_filters));
foreach ($ldap_filters as $filter) {
ligne($filter[0]['NAME'], $l->g(1111), 'input', array('VALUE' => $filter[0]['TVALUE'], 'SIZE' => "30%", 'MAXLENGTH' => 255));
ligne($filter[0]['NAME'], $l->g(1111), 'input', array('VALUE' => $filter[0]['TVALUE'], 'SIZE' => "30%", 'MAXLENGTH' => ''));
ligne($filter[1]['NAME'], $l->g(1116), 'select', array('VALUE' => $filter[1]['TVALUE'], 'SELECT_VALUE' => $role1));
}

}

function pagesnmp() {
Expand Down
2 changes: 1 addition & 1 deletion var.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
/**
* OCS' MySQL database version
*/
define('GUI_VER', '7073');
define('GUI_VER', '7074');

/**
* GUI Version
Expand Down

0 comments on commit f5ce869

Please sign in to comment.