link_title; $extraParms['SQL2EXCEL_WB_DLCNT'] = $wbParms->count + 1; $colHeadRow=1; // Filename $fileName = trim($wbParms->filename); $fileName = writeExcel::replace_vars($fileName, $substParms, $extraParms, $cmpParms ); $fileName = trim($fileName); $fileName = str_replace('.xls','.csv',$fileName); $fileName = str_replace('.XLS','.csv',$fileName); if ( $fileName == '' || $fileName == null ) { $fileName = 'download.csv'; } // Compression? $usingCompression = false; if ( !$noComp && ($wbParms->compress_wb == 'Yes' || ($wbParms->compress_wb == 'Global' && Sql2excelParms::get($cmpParms,'compress_wb', 0) ) )) { // Load ZIP class require_once ( JPATH_SITE.DS.'components'.DS.'com_sql2excel'.DS.'helpers'.DS.'zip.lib.php'); $usingCompression = true; } // Check extension $path_info = pathinfo($fileName); if ( !isset($path_info['extension']) || ( isset($path_info['extension']) && strtolower($path_info['extension']) != 'csv' ) ) { $fileName .= '.csv'; } $extraParms['SQL2EXCEL_WB_FN'] = $fileName; ////////////////////////////////////////////////////////////////// // Use Cache? ////////////////////////////////////////////////////////////////// $cacheEnabled = Sql2excelParms::get($cmpParms,'cache_enabled', 0); $cacheDir = writeExcel::get_cache_dir($cmpParms); $cacheTime = Sql2excelParms::get($cmpParms,'cache_time', 30); $wbCache = $wbParms->cache; $usingCache = false; if ( ( $useCache && ($cacheEnabled && $wbCache == 'Global') ) || ( $useCache && $wbCache == 'Yes' ) ) { $usingCache = true; } if ( $usingCache || $usingCompression ) { // Cache or Compression if ( file_exists($cacheDir) ) { $cacheFileName = writeExcel::get_cache_filename($wbParms, $wsParms); $wbFN = $cacheDir . 'download_csv_' . $cacheFileName . '.cache'; if ( $usingCompression ) { $wbFN .= '_c'; } $cacheFN = $wbFN; if ( $silent && $usingCompression ) { // Write out original filename (.csv), will be zippped later into the cache file $wbFN = $wbParms->filename; if ( substr($wbFN,strlen($wbFN)-4,4) != '.csv' ) { $wbFN .= '.csv'; } $wbFN = $cacheDir . $wbFN; } if ( $usingCache && file_exists($wbFN) && !$silent ) { // Serve from Cache!? $fileModified = filemtime($wbFN); $age = time() - $fileModified; $minutesOld = $age / 60; if ( $cacheTime >= $minutesOld ) { // Server from cache if ( $usingCompression ) { $fsize = @filesize($wbFN); $fh = fopen($wbFN, 'rb', false); if ($fh == false) { die("Can't read cache file."); } $data = fread($fh, $fsize); fclose($fh); // Write the ZIP header writeExcel::ZipHeader(str_replace('.csv','.zip', $fileName),strlen($data)); // Output cached file echo $data; } else { $fh = fopen($wbFN, "rb"); if ($fh == false) { die("Can't read cache file."); } fpassthru($fh); } return; } } } } //////////////////////////////////////////////////////////////// // Write to file? (Cache or Compression) $fh = -1; if ( $wbFN != '-' && $wbFN != '' ) { $fh = fopen($wbFN, "w"); } // Get which Worksheet to output if ( is_array($wsParms) ) { // Get first sheet $sheetID = $wsParms[0]->ws_id; // Sheet specified through URL? if ( $sheet > 0 ) { // Yes, check that the sheet is accessible $sheetID = 0; foreach ( $wsParms as $ws ) { if ( $ws->ws_id == $sheet ) { $sheetID = $sheet; } } } if ( $sheetID > 0 ) { // OK We have a Sheet! // Get WS Object $ws = new stdClass; foreach ( $wsParms as $wsheet ) { if ( $wsheet->ws_id == $sheetID ) { $ws = $wsheet; } } $sheetName = $ws->sheetname; if ( trim($sheetName) == '' ) { $sheetName = 'Sheet 1'; } $extraParms['SQL2EXCEL_WS_SN'] = $sheetName; $extraParms['SQL2EXCEL_WS_DLCNT'] = $ws->count + 1; $query = $ws->query; $extraParms['SQL2EXCEL_WS_SQL_RAW'] = $query; // Substitute variables in SQL query? if ( Sql2excelParms::get($cmpParms,'subst_sql', '1') ) { $query = writeExcel::replace_vars($query, $substParms,0,$cmpParms); } $extraParms['SQL2EXCEL_WS_SQL'] = $query; // Forumla columns $formulas = explode(',', $ws->formulas ); // Get Database Connection $querydb = writeExcel::getDB($ws); // Run query! if ( $querydb ) { $rows = writeExcel::getResults($querydb, $query, $cmpParms, $ws->db_type); if ( is_array($rows) ) { $removeCR = Sql2excelParms::get($cmpParms,'removereturnhtm', 4); $extraParms['SQL2EXCEL_WS_ROWS'] = count($rows); writeHTM::write_string("",$fh); writeHTM::write_string("",$fh); writeHTM::write_string("",$fh); writeHTM::write_string("",$fh); writeHTM::write_string("",$fh); writeHTM::write_string("" . $wbParms->title . ' - ' . $ws->title . "", $fh); writeHTM::write_string("",$fh); writeHTM::write_string("",$fh); writeHTM::write_string("

" . $wbParms->title . ' → ' . $ws->title . "

",$fh); writeHTM::write_string("",$fh); $maxdatarow = count($rows) + 1; // Rows + Column header // Get number of columns $colNames = writeExcel::getColumnNames($rows); //Number of Columns in user query (exclude JoomFish fields if any) $nrCols = writeExcel::getColumnCount($colNames, $query); // Add column header if ( $ws->show_colheader <> 0 ) { writeHTM::write_string("",$fh); $str = "\n"; for ($i=0; $i<$nrCols; $i++) { $colUser = $i + 1; $str .= writeHTM::format_value($colNames[$i], 1, $colUser, 0, $maxdatarow, 1, $removeCR, $ws->escape_chars_html); } $str .= ""; writeHTM::write_string($str,$fh); writeHTM::write_string("",$fh); $rowUser = 2; } else { $rowUser = 1; } foreach ( $rows as $row ) { $rowID = $rowUser % 2; $str = '' . "\n"; $colUser = 1; for ( $i=0; $i<$nrCols; $i++) { $str .= writeHTM::format_value($row->$colNames[$i], $rowUser, $colUser, $formulas, $maxdatarow,0, $removeCR, $ws->escape_chars_html); $colUser++; } $str .= "\n"; writeHTM::write_string($str,$fh); $rowUser++; } writeHTM::write_string("
\n",$fh); // Close output file - if needed if ( $fh != - 1 ) { fclose($fh); $fh = -1; } // Compression or Cache? if ( !is_null($wbFN) && is_string($wbFN) && $wbFN != '-' ) { if ( $usingCompression ) { // Compress the file with ZIP $zip = new zipfile(); $fsize = @filesize($wbFN); $fh = fopen($wbFN, 'rb', false); $data = fread($fh, $fsize); fclose($fh); $zip->addFile($data,$fileName); $zipcontents = $zip->file(); if ( !$silent ) { // Write the ZIP header writeExcel::ZipHeader(str_replace('.csv','.zip', $fileName),strlen($zipcontents)); // Output ZIP data to browser echo $zipcontents; } // Cleanup or Save ZIP file to cache!? if ( !$usingCache ) { unlink($wbFN); } else { $fh = fopen($cacheFN, 'wb', false); fputs($fh,$zipcontents); fclose($fh); } } elseif ( !$silent ) { // Stream file from cache $fh = fopen($wbFN, "rb"); if ($fh == false) { die("Can't read cache file."); } fpassthru($fh); } if ( $usingCache && $silent ) { // Copy cache file to ./scheduler subfolder for email attachment writeExcel::copyCacheFile($cacheFN, $fileName, $cacheDir); // Compress the cache file? if ( $wbParms->compress_wb == 'Yes' || ($wbParms->compress_wb == 'Global' && Sql2excelParms::get($cmpParms,'compress_wb', 0) ) ) { $zip = new zipfile(); $fsize = @filesize($cacheFN); $fh = fopen($cacheFN, 'rb', false); $data = fread($fh, $fsize); fclose($fh); $zip->addFile($data,$fileName); $zipcontents = $zip->file(); $fh = fopen($cacheFN.'_c', 'wb', false); fputs($fh,$zipcontents); fclose($fh); unlink($cacheFN); } } } } } else { $rows = JText::_( 'DB_CONNECT_ERROR'); } } else { JError::raiseError( 500, JText::_( 'Worksheet not found or access denied' ) ); } } // Unset database handle unset($querydb); unset($GLOBALS['querydb']); // Close output file if still open if ( $fh != - 1 ) { fclose($fh); } } function format_value($str, $rowUser, $colUser, $formulas, $maxdatarow, $head = 0, $removeCR=1, $escape_spec_chars=0) { // Formula? if ( is_array($formulas) && in_array($colUser, $formulas) ) { // Formula! => Parse and replace Row and Col ID's $str = writeExcel::replace_row_col_ids($str, $rowUser, $colUser, 1, $maxdatarow); } if ( $escape_spec_chars ) { $str = htmlspecialchars($str); } if ( $head ) { return '' . $str . ''; } else { if ( substr($str,0,4) == 'http' ) { return '' . $str . ''; } else { if ( $removeCR > 0 ) { // Replace CRLF return '' . writeExcel::removeCR($str,$removeCR) . ''; } else { return '' . $str . ''; } } } } function write_string($str,$fh) { if ( $fh != - 1 ) { fwrite($fh,$str ."\n"); } else { print $str ."\n"; } } } ?>