diff --git a/tests/php/test_add_monitors_csv_escape.php b/tests/php/test_add_monitors_csv_escape.php new file mode 100644 index 0000000000..0374dcc43f --- /dev/null +++ b/tests/php/test_add_monitors_csv_escape.php @@ -0,0 +1,83 @@ +data = $d; parent::__construct('responded'); } } + function canEdit($p) { return true; } + function ajaxResponse($d) { throw new Responded($d); } + function ajaxError($m) { throw new Responded(array('error' => $m)); } + function validHtmlStr($s) { return $s; } + function probe($bits) { return array(); } + + $failures = 0; + function check($name, $got, $want) { + global $failures; + if ($got === $want) { echo "ok - $name\n"; } + else { $failures++; echo "FAIL - $name\n got: ".var_export($got, true)."\n want: ".var_export($want, true)."\n"; } + } + + $src = file_get_contents(__DIR__.'/../../web/ajax/add_monitors.php'); + + // Count the arguments of every fgetcsv() call. + $tokens = token_get_all($src); + $argCounts = array(); + for ($i = 0, $n = count($tokens); $i < $n; $i++) { + if (!is_array($tokens[$i]) || $tokens[$i][0] != T_STRING || $tokens[$i][1] != 'fgetcsv') continue; + $depth = 0; + $args = 1; + for ($j = $i + 1; $j < $n; $j++) { + $t = $tokens[$j]; + if ($t === '(' || $t === '[') $depth++; + elseif ($t === ')' || $t === ']') { if (--$depth == 0) break; } + elseif ($t === ',' && $depth == 1) $args++; + } + $argCounts[] = $args; + } + check('every fgetcsv() call passes the $escape argument', $argCounts, array_fill(0, count($argCounts), 5)); + check('add_monitors.php still reads a CSV with fgetcsv()', count($argCounts) > 0, true); + + $start = strpos($src, "if (canEdit('Monitors')) {"); + $code = substr($src, $start); + $code = str_replace('is_uploaded_file(', 'is_file(', $code); + $code = preg_replace('/\?>\s*$/', '', $code); + + $deprecations = array(); + set_error_handler(function($errno, $errstr) use (&$deprecations) { + $deprecations[] = $errstr; + return true; + }); + $csv = tempnam(sys_get_temp_dir(), 'zmcsv'); + file_put_contents($csv, "Name,URL,Group\n\"Door, front\",rtsp://192.0.2.10/a\nGate,rtsp://192.0.2.11/b\n"); + $_REQUEST = array('action' => 'import'); + $_FILES = array('import_file' => array('tmp_name' => $csv, 'error' => 0)); + $user = null; + try { + eval($code); + $r = null; + } catch (Responded $e) { + $r = $e->data; + } + unlink($csv); + restore_error_handler(); + $streams = isset($r['Streams']) ? $r['Streams'] : array(); + check('the import reads the header and both rows', count($streams), 2); + check('a quoted name keeps its comma', isset($streams[0]) ? $streams[0]['camera']['Name'] : null, 'Door, front'); + check('the import raises no PHP deprecation', $deprecations, array()); + + exit($failures ? 1 : 0); +} diff --git a/web/ajax/add_monitors.php b/web/ajax/add_monitors.php index ba4afa2193..8bfb929bf4 100644 --- a/web/ajax/add_monitors.php +++ b/web/ajax/add_monitors.php @@ -170,7 +170,7 @@ if (canEdit('Monitors')) { $available_streams = array(); $row = 0; - while ( ($data = fgetcsv($handle, 0, ',')) !== FALSE ) { + while ( ($data = fgetcsv($handle, 0, ',', '"', '\\')) !== FALSE ) { $row ++; if ( count($data) < 2 ) { ZM\Info("Skipping line $row, expected at least Name,URL columns");