git-svn-id: https://192.168.0.254/svn/Proyectos.FundacionLQDVI_WebCongresos/trunk@2 94ccb1af-fd9d-d947-8d90-7f70ea60afc8
368 lines
11 KiB
PHP
368 lines
11 KiB
PHP
<?php
|
|
/*
|
|
* @component SQL 2 Excel Component
|
|
* @copyright Copyright (C) Joomla-R-Us, joomla-r-us.com
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
|
|
*/
|
|
|
|
// no direct access
|
|
defined( '_JEXEC' ) or die( 'Restricted access' );
|
|
|
|
require_once ( JPATH_SITE.DS.'components'.DS.'com_sql2excel'.DS.'helpers'.DS.'parms.php');
|
|
|
|
class writeHTM
|
|
{
|
|
|
|
function writeFile($wbParms, $wsParms, $sheet=0, $cmpParms, $substParms, $useCache=1, $silent=0, $wbFN='-', $noComp=0)
|
|
{
|
|
|
|
$db = & JFactory::getDBO();
|
|
$wbParms = $wbParms[0];
|
|
$extraParms = array();
|
|
$extraParms['SQL2EXCEL_WB_LINK'] = $wbParms->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("<html>",$fh);
|
|
writeHTM::write_string("<head>",$fh);
|
|
writeHTM::write_string("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />",$fh);
|
|
writeHTM::write_string("<link rel=\"stylesheet\" href=\"" . JURI::root() . "components/com_sql2excel/assets/css/sql2excel_html_body.css\" type=\"text/css\" />",$fh);
|
|
writeHTM::write_string("<link rel=\"stylesheet\" href=\"" . JURI::root() . "components/com_sql2excel/assets/css/sql2excel_html_table.css\" type=\"text/css\" />",$fh);
|
|
writeHTM::write_string("<title>" . $wbParms->title . ' - ' . $ws->title . "</title>", $fh);
|
|
writeHTM::write_string("</head>",$fh);
|
|
writeHTM::write_string("<body>",$fh);
|
|
writeHTM::write_string("<h1>" . $wbParms->title . ' → ' . $ws->title . "</h1>",$fh);
|
|
writeHTM::write_string("<table class=\"sql2excel\">",$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("<thead>",$fh);
|
|
$str = "<tr>\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 .= "</tr>";
|
|
writeHTM::write_string($str,$fh);
|
|
writeHTM::write_string("</thead>",$fh);
|
|
$rowUser = 2;
|
|
} else {
|
|
$rowUser = 1;
|
|
}
|
|
|
|
|
|
foreach ( $rows as $row ) {
|
|
$rowID = $rowUser % 2;
|
|
$str = '<tr class="row' . $rowID . '">' . "\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 .= "</tr>\n";
|
|
writeHTM::write_string($str,$fh);
|
|
$rowUser++;
|
|
}
|
|
|
|
writeHTM::write_string("</table></body>\n</html>",$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 '<th class="title">' . $str . '</th>';
|
|
} else {
|
|
if ( substr($str,0,4) == 'http' ) {
|
|
return '<td><a href="' . $str . '">' . $str . '</a></td>';
|
|
} else {
|
|
if ( $removeCR > 0 ) {
|
|
// Replace CRLF
|
|
return '<td>' . writeExcel::removeCR($str,$removeCR) . '</td>';
|
|
} else {
|
|
return '<td>' . $str . '</td>';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
function write_string($str,$fh) {
|
|
if ( $fh != - 1 ) {
|
|
fwrite($fh,$str ."\n");
|
|
} else {
|
|
print $str ."\n";
|
|
}
|
|
}
|
|
|
|
}
|
|
?>
|