Skip to content

Commit

Permalink
refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
dmyios committed May 24, 2024
1 parent 73453bb commit 001271c
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 165 deletions.
19 changes: 9 additions & 10 deletions centreon/www/api/class/centreon_clapi.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*
*/

require_once __DIR__ . '/../../include/common/common-Func.php';
require_once __DIR__ . '/../../include/common/csvFunctions.php';
require_once dirname(__FILE__) . "/webService.class.php";

define('_CLAPI_LIB_', _CENTREON_PATH_ . '/lib');
Expand Down Expand Up @@ -174,27 +174,26 @@ public function postAction()
}


$delim_in_first_line = preg_match("/^.*;.*(?:\n|$)/", $contents);
if ($delim_in_first_line) {
if (preg_match("/^.*;.*(?:\n|$)/", $contents)) {
$result = parse_csv($contents);
if ($result === false) {
throw new RestInternalServerErrorException($contents);
}

$last_record = end($result);
if ($last_record && strpos($last_record[0], 'Return code end :') === 0) {
$lastRecord = end($result);
if ($lastRecord && strpos($lastRecord[0], 'Return code end :') === 0) {
array_pop($result);
}

$headers_nr = count($result[0]);
$headersNr = count($result[0]);
foreach ($result as &$record) {
if (count($record) > $headers_nr) {
$record[$headers_nr - 1] = implode(';', array_slice($record, $headers_nr - 1));
$record = array_slice($record, 0, $headers_nr);
if (count($record) > $headersNr) {
$record[$headersNr - 1] = implode(';', array_slice($record, $headersNr - 1));
$record = array_slice($record, 0, $headersNr);
}
}

csv_to_associative_array($result);
csvToAssociativeArray($result);

} else {
$result = array();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public function getparam($parameters = null)
}

if (!isset($exportedFields[$paramSearch])) {
$resultString .= $this->csv_escape($ret) . $this->delim;
$resultString .= $this->csvEscape($ret) . $this->delim;
$exportedFields[$paramSearch] = 1;
}
}
Expand Down
8 changes: 4 additions & 4 deletions centreon/www/class/centreon-clapi/centreonHost.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ public function getparam($parameters = null)
break;
}
if (!isset($exportedFields[$paramSearch])) {
$resultString .= $this->csv_escape($ret) . $this->delim;
$resultString .= $this->csvEscape($ret) . $this->delim;
$exportedFields[$paramSearch] = 1;
}
}
Expand Down Expand Up @@ -914,10 +914,10 @@ public function getmacro($hostName)
if ($macro["source"] == "fromTpl") {
$source = $macro["macroTpl"];
}
echo $this->csv_escape($macro['host_macro_name']) . $this->delim
. $this->csv_escape($macro['host_macro_value']) . $this->delim
echo $this->csvEscape($macro['host_macro_name']) . $this->delim
. $this->csvEscape($macro['host_macro_value']) . $this->delim
. $macro['is_password'] . $this->delim
. $this->csv_escape($macro['description']) . $this->delim
. $this->csvEscape($macro['description']) . $this->delim
. $source . "\n";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public function getparam($parameters = null)
$ret = $ret[$field];

if (!isset($exportedFields[$paramSearch])) {
$resultString .= $this->csv_escape($ret) . $this->delim;
$resultString .= $this->csvEscape($ret) . $this->delim;
$exportedFields[$paramSearch] = 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,8 @@ public function getmacro($parameters)
);
echo "macro name;macro value\n";
foreach ($macroList as $macro) {
echo $this->csv_escape($macro['svc_macro_name']) . $this->delim
. $this->csv_escape($macro['svc_macro_value']) . "\n";
echo $this->csvEscape($macro['svc_macro_name']) . $this->delim
. $this->csvEscape($macro['svc_macro_value']) . "\n";
}
}

Expand Down
4 changes: 2 additions & 2 deletions centreon/www/class/centreon-clapi/centreonObject.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public function getparam($parameters = null)
throw new CentreonClapiException(self::MISSINGPARAMETER);
}
$p = $this->object->getParameters($params[0], $params[1]);
print $this->csv_escape($p[$params[1]]) . "\n";
print $this->csvEscape($p[$params[1]]) . "\n";
}

/**
Expand Down Expand Up @@ -670,7 +670,7 @@ public static function getInstance($dependencyInjector = null)
* @param string $text The string to escape
* @return string The string sanitized
*/
protected function csv_escape($text)
protected function csvEscape($text)
{
if ($text[0] === '"' || strpos($text, $this->delim) !== false || strpos($text, "\n") !== false) {
$text = '"' . str_replace('"', '""', $text) . '"';
Expand Down
8 changes: 4 additions & 4 deletions centreon/www/class/centreon-clapi/centreonService.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ public function getparam($parameters = null)
}
}
if (!isset($exportedFields[$paramSearch])) {
$resultString .= $this->csv_escape($ret) . $this->delim;
$resultString .= $this->csvEscape($ret) . $this->delim;
$exportedFields[$paramSearch] = 1;
}
}
Expand Down Expand Up @@ -903,10 +903,10 @@ public function getmacro($parameters)
if ($macro["source"] == "fromTpl") {
$source = $macro["macroTpl"];
}
echo $this->csv_escape($macro['svc_macro_name']) . $this->delim
. $this->csv_escape($macro['svc_macro_value']) . $this->delim
echo $this->csvEscape($macro['svc_macro_name']) . $this->delim
. $this->csvEscape($macro['svc_macro_value']) . $this->delim
. $macro['is_password'] . $this->delim
. $this->csv_escape($macro['description']) . $this->delim
. $this->csvEscape($macro['description']) . $this->delim
. $source . "\n";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function getparam($parameters = null)
$ret = $ret[$field];

if (!isset($exportedFields[$paramSearch])) {
$resultString .= $this->csv_escape($ret) . $this->delim;
$resultString .= $this->csvEscape($ret) . $this->delim;
$exportedFields[$paramSearch] = 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public function getparam($parameters = null)
break;
}
if (!isset($exportedFields[$paramSearch])) {
$resultString .= $this->csv_escape($ret) . $this->delim;
$resultString .= $this->csvEscape($ret) . $this->delim;
$exportedFields[$paramSearch] = 1;
}
}
Expand Down Expand Up @@ -665,9 +665,9 @@ public function getmacro($parameters)
echo "macro name;macro value;description;is_password\n";
foreach ($macroList as $macro) {
$password = !empty($macro['is_password']) ? (int)$macro['is_password'] : 0;
echo $this->csv_escape($this->extractMacroName($macro['svc_macro_name'])) . $this->delim
. $this->csv_escape($macro['svc_macro_value']) . $this->delim
. $this->csv_escape($macro['description']) . $this->delim
echo $this->csvEscape($this->extractMacroName($macro['svc_macro_name'])) . $this->delim
. $this->csvEscape($macro['svc_macro_value']) . $this->delim
. $this->csvEscape($macro['description']) . $this->delim
. $password . "\n";
}
}
Expand Down
136 changes: 0 additions & 136 deletions centreon/www/include/common/common-Func.php
Original file line number Diff line number Diff line change
Expand Up @@ -2092,139 +2092,3 @@ function signalConfigurationChange(

definePollersToUpdated(array_merge($pollerIds, $previousPollers));
}


/**
* Transform plain CSV data in an associative array (first line acts as headers)
*
* @param array $records
*/
function csv_to_associative_array(&$records)
{
$headers = array_shift($records);
foreach ($records as &$record) {
$record = array_combine($headers, $record);
}
}

/**
* Return array of parsed CSV
*
* @param string $text
* @param string $delim (default ';')
* @param bool $ignore_empty_lines (default true)
* @return bool|array False if there was a problem, otherwise the records
*/
function parse_csv(&$text, $delim = ';', $ignore_empty_lines = true)
{
$records = array();
$record = array();

$pos_field_start = 0;
$pos_cur = 0;
$rec_nr = 0;
$prev_char_is_dq = false;
$inside_dq = 0; // 0: to determine, 1: no, 2: yes

$CR = "\r";
$LF = "\n";

$enders_field = array(
$delim => $delim,
$CR => $CR,
$LF => $LF,
'' => '',
);
$enders_record = array(
$CR => $CR,
$LF => $LF,
'' => '',
);

$last_char_ends_record = array_key_exists($text[-1], $enders_record);
$text_len = strlen($text) + ($last_char_ends_record ? 0 : 1);
while ($pos_cur < $text_len) {
$c = $text[$pos_cur] ?? '';

switch ($inside_dq) {
case 0:
if ($c == '"') {
$inside_dq = 2;
$pos_field_start = $pos_cur + 1;
break;
} else
$inside_dq = 1;
// fall through

case 1:
if (array_key_exists($c, $enders_field)) {
$inside_dq = 0;
$is_end_rec = array_key_exists($c, $enders_record);
if ($is_end_rec && $pos_field_start == $pos_cur && $ignore_empty_lines && !$record) {
} else
$record[] = substr($text, $pos_field_start, $pos_cur - $pos_field_start);

$pos_field_start = $pos_cur + 1;

if ($is_end_rec) {
if ($c == $CR) {
$pos_field_start++;
$pos_cur++;
}

if ($record) {
$records[$rec_nr] = $record;
$record = array();
$rec_nr++;
}
}
}
break;

case 2:
if ($prev_char_is_dq) {
if ($c == '"') {
$prev_char_is_dq = false;
} elseif (array_key_exists($c, $enders_field)) {
$prev_char_is_dq = false;
$is_end_rec = array_key_exists($c, $enders_record);
if ($is_end_rec && $pos_field_start == $pos_cur && $ignore_empty_lines && !$record) {
} else
$record[] = str_replace('""', '"', substr($text, $pos_field_start, $pos_cur - $pos_field_start - 1));

$pos_field_start = $pos_cur + 1;

if ($is_end_rec) {
if ($c == $CR) {
$pos_field_start++;
$pos_cur++;
}

if ($record) {
$records[$rec_nr] = $record;
$record = array();
$rec_nr++;
}
}
$inside_dq = 0;
} else {
return false; // malformed csv
}
break;
}
if ($c == '"') {
$prev_char_is_dq = true;
} elseif ($c == '') {
return false; // malformed csv
}
break;
}

$pos_cur++;
}

if ($inside_dq == 2) // truncated input
return false;

return $records;
}
Loading

0 comments on commit 001271c

Please sign in to comment.