Skip to content

Commit 5dcdeab

Browse files
Merge pull request #154 from hannes427/csv-upload16
Outbound Routes: Uploaded csv-file is not deemed valid csv (Fixed)
2 parents b469733 + ce8d9a4 commit 5dcdeab

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

Core.class.php

+46-31
Original file line numberDiff line numberDiff line change
@@ -1068,53 +1068,68 @@ public function doConfigPageInit($page) {
10681068
// Check if they uploaded a CSV file for their route patterns
10691069
//
10701070
if (isset($_FILES['pattern_file']) && $_FILES['pattern_file']['tmp_name'] != '') {
1071-
$mimes = array('text/csv');
1072-
if (!in_array($_FILES['pattern_file']['type'], $mimes)) {
1073-
echo "<script>javascript:alert('" . _("Unsupported Pattern file format") . "')</script>";
1074-
return;
1075-
}
1076-
$fh = fopen($_FILES['pattern_file']['tmp_name'], 'r');
1077-
if ($fh !== false) {
1071+
$uploaded_file = file($_FILES['pattern_file']['tmp_name'], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
1072+
if (count($uploaded_file) !== 0) {
10781073
$csv_file = array();
10791074
$index = array();
1080-
1081-
// Check first row, ingoring empty rows and get indices setup
1082-
//
1083-
while (($row = fgetcsv($fh, 5000, ",", "\"")) !== false) {
1084-
if (count($row) == 1 && $row[0] == '') {
1085-
continue;
1086-
} else {
1087-
$count = count($row) > 4 ? 4 : count($row);
1088-
for ($i=0;$i<$count;$i++) {
1089-
switch (strtolower($row[$i])) {
1075+
}
1076+
$first_line = true;
1077+
$has_headers = false;
1078+
foreach($uploaded_file AS $line) {
1079+
$line = str_replace('"', '', $line);
1080+
$line_as_array = explode(',', $line);
1081+
if ($first_line) {
1082+
if (str_contains(strtolower($line), 'prepend') || str_contains(strtolower($line), 'prefix') || str_contains(strtolower($line), 'match pattern') || str_contains(strtolower($line), 'callerid')) {
1083+
$has_headers = true;
1084+
$count_headers = count($line_as_array);
1085+
for ($i=0;$i<$count_headers;$i++) {
1086+
switch (strtolower($line_as_array[$i])) {
10901087
case 'prepend':
10911088
case 'prefix':
10921089
case 'match pattern':
10931090
case 'callerid':
1094-
$index[strtolower($row[$i])] = $i;
1091+
$index[strtolower($line_as_array[$i])] = $i;
10951092
break;
10961093
default:
10971094
break;
10981095
}
10991096
}
1097+
$first_line = false;
1098+
continue;
1099+
} else if (preg_match("/^[XZN0-9\+\.\-\[\]]*,[XZN0-9\+\.\-\[\]]*,[XZN0-9\+\.\-\[\]]*,[XZN0-9\+\.\-\[\]]*$/i", $line)) {
1100+
$count = count($line_as_array);
11001101
// If no headers then assume standard order
1101-
if (count($index) == 0) {
1102-
$index['prepend'] = 0;
1103-
$index['prefix'] = 1;
1104-
$index['match pattern'] = 2;
1105-
$index['callerid'] = 3;
1106-
if ($count == 4) {
1107-
$csv_file[] = $row;
1108-
}
1102+
$index['prepend'] = 0;
1103+
$index['prefix'] = 1;
1104+
$index['match pattern'] = 2;
1105+
$index['callerid'] = 3;
1106+
if ($count == 4) {
1107+
$csv_file[] = $line_as_array;
11091108
}
1110-
break;
1109+
$first_line = false;
1110+
continue;
11111111
}
1112+
echo "<script>javascript:alert('" . _("Unsupported Pattern file format") . "')</script>";
1113+
return;
11121114
}
1113-
$row_count = count($index);
1114-
while (($row = fgetcsv($fh, 5000, ",", "\"")) !== false) {
1115-
if (count($row) == $row_count) {
1116-
$csv_file[] = $row;
1115+
if ($has_headers) {
1116+
if (!preg_match("/^[XZN0-9\+\.\-\[\]]*,{0,1}[XZN0-9\+\.\-\[\]]*,{0,1}[XZN0-9\+\.\-\[\]]*,{0,1}[XZN0-9\+\.\-\[\]]*$/i", $line)) {
1117+
echo "<script>javascript:alert('" . _("Unsupported Pattern file format") . "')</script>";
1118+
return;
1119+
} else if (count($line_as_array) != $count_headers) {
1120+
echo "<script>javascript:alert('" . _("Malformated csv file") . "')</script>";
1121+
return;
11171122
}
1123+
1124+
} else {
1125+
if (!preg_match("/^[XZN0-9\+\.\-\[\]]*,[XZN0-9\+\.\-\[\]]*,[XZN0-9\+\.\-\[\]]*,[XZN0-9\+\.\-\[\]]*$/i", $line)) {
1126+
echo "<script>javascript:alert('" . _("Unsupported Pattern file format") . "')</script>";
1127+
return;
1128+
}
1129+
}
1130+
$index_count = count($index);
1131+
if (count($line_as_array) == $index_count) {
1132+
$csv_file[] = $line_as_array;
11181133
}
11191134
}
11201135
}

0 commit comments

Comments
 (0)