@@ -1068,53 +1068,68 @@ public function doConfigPageInit($page) {
1068
1068
// Check if they uploaded a CSV file for their route patterns
1069
1069
//
1070
1070
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 ) {
1078
1073
$ csv_file = array ();
1079
1074
$ 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 ])) {
1090
1087
case 'prepend ' :
1091
1088
case 'prefix ' :
1092
1089
case 'match pattern ' :
1093
1090
case 'callerid ' :
1094
- $ index [strtolower ($ row [$ i ])] = $ i ;
1091
+ $ index [strtolower ($ line_as_array [$ i ])] = $ i ;
1095
1092
break ;
1096
1093
default :
1097
1094
break ;
1098
1095
}
1099
1096
}
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 );
1100
1101
// 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 ;
1109
1108
}
1110
- break ;
1109
+ $ first_line = false ;
1110
+ continue ;
1111
1111
}
1112
+ echo "<script>javascript:alert(' " . _ ("Unsupported Pattern file format " ) . "')</script> " ;
1113
+ return ;
1112
1114
}
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 ;
1117
1122
}
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 ;
1118
1133
}
1119
1134
}
1120
1135
}
0 commit comments