diff --git a/syntax.php b/syntax.php index f47ab6e..15f8445 100644 --- a/syntax.php +++ b/syntax.php @@ -60,6 +60,8 @@ function handle($match, $state, $pos, Doku_Handler &$handler) { 'hdr_cols' => 0, 'span_empty_cols' => 0, 'file' => '', + 'export' => '', + 'linkname' => 'Download CSV file', 'delim' => ',', 'enclosure' => '"', 'escape' => '"', @@ -71,9 +73,29 @@ function handle($match, $state, $pos, Doku_Handler &$handler) { // parse options $optsin = explode(' ', $optstr); + $populate = ''; foreach($optsin as $o) { $o = trim($o); - if(preg_match('/(\w+)=(.*)/', $o, $matches)) { + if ( $populate != '' ) { + // handle closing quote + $opt[$populate] .= ' '; + if ( substr($o, -1) == '"' ) { + $o = substr($o, 0, -1); + $opt[$populate] .= $o; + $populate = ''; + } + else + $opt[$populate] .= $o; + } + elseif (preg_match('/(\w+)=(.*)/', $o, $matches)) { + // strip leading quote + if ( substr($matches[2], 0, 1) == '"' ) { + $matches[2] = substr($matches[2], 1, -1); + if ( substr($matches[2], -1) == '"' ) + $matches[2] = substr($matches[2], 0, -1); + else + $populate = $matches[1]; + } $opt[$matches[1]] = $matches[2]; } elseif($o) { if(preg_match('/^https?:\/\//i', $o)) { @@ -128,6 +150,30 @@ function render($mode, Doku_Renderer &$renderer, $opt) { return true; } + // Export the csv file + $targetfile = ''; + $export = $opt['export']; + if ( $export != '' ) { + $targetfile = htmlspecialchars(trim($export)); + if ( auth_quickaclcheck(getNS($targetfile.':*')) < AUTH_EDIT) { + $renderer->cdata('Access denied: Could not create download link.'); + $targetfile = ''; + return true; + } else { + $file = mediaFN($targetfile); + if ( file_put_contents ($file, $content, LOCK_EX) > 0 ) { + $linkname = $opt['linkname']; + if ( $linkname == '' ) + $linkname = 'Download CSV file'; + } + else { + $targetfile = ''; + $renderer->cdata('Failed to write '.$file.': Could not create download link.'); + return true; + } + } + } + // get the first row - it will define the structure $row = $this->csv_explode_row($content, $opt['delim'], $opt['enclosure'], $opt['escape']); $maxcol = count($row); @@ -186,6 +232,9 @@ function render($mode, Doku_Renderer &$renderer, $opt) { } $renderer->table_close(); + if ( $targetfile != '' ) + $renderer->internalmedia($targetfile, $linkname); + return true; }