<?php
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
session_cache_limiter("none");
set_magic_quotes_runtime(0);
include("include/dbcommon.php");
include("include/_category_variables.php");
if(!@$_SESSION["UserID"])
{
$_SESSION["MyURL"]=$_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
header("Location: login.php?message=expired");
return;
}
if(!CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Export"))
{
echo "<p>"."You don't have permissions to access this table"."<a href=\"login.php\">"."Back to login page"."</a></p>";
return;
}
$conn=db_connect();
// Before Process event
if(function_exists("BeforeProcessExport"))
BeforeProcessExport($conn);
$strWhereClause="";
$options = "1";
if (@$_REQUEST["a"]!="")
{
$options = "";
$sWhere = "1=0";
// process selection
$selected_recs=array();
if (@$_REQUEST["mdelete"])
{
foreach(@$_REQUEST["mdelete"] as $ind)
{
$keys=array();
$keys["id"]=refine($_REQUEST["mdelete1"][$ind-1]);
$selected_recs[]=$keys;
}
}
elseif(@$_REQUEST["selection"])
{
foreach(@$_REQUEST["selection"] as $keyblock)
{
$arr=split("&",refine($keyblock));
if(count($arr)<1)
continue;
$keys=array();
$keys["id"]=urldecode($arr[0]);
$selected_recs[]=$keys;
}
}
foreach($selected_recs as $keys)
{
$sWhere = $sWhere . " or ";
$sWhere.=KeyWhere($keys);
}
$strSQL = gSQLWhere($sWhere);
$strWhereClause=$sWhere;
$_SESSION[$strTableName."_SelectedSQL"] = $strSQL;
$_SESSION[$strTableName."_SelectedWhere"] = $sWhere;
}
if ($_SESSION[$strTableName."_SelectedSQL"]!="" && @$_REQUEST["records"]=="")
{
$strSQL = $_SESSION[$strTableName."_SelectedSQL"];
$strWhereClause=@$_SESSION[$strTableName."_SelectedWhere"];
}
else
{
$strWhereClause=@$_SESSION[$strTableName."_where"];
$strSQL=gSQLWhere($strWhereClause);
}
$mypage=1;
if(@$_REQUEST["type"])
{
// order by
$strOrderBy=$_SESSION[$strTableName."_order"];
if(!$strOrderBy)
$strOrderBy=$gstrOrderBy;
$strSQL.=" ".trim($strOrderBy);
$strSQLbak = $strSQL;
if(function_exists("BeforeQueryExport"))
BeforeQueryExport($strSQL,$strWhereClause,$strOrderBy);
// Rebuild SQL if needed
if($strSQL!=$strSQLbak)
{
// changed $strSQL - old style
$numrows=GetRowCount($strSQL);
}
else
{
$strSQL = gSQLWhere($strWhereClause);
$strSQL.=" ".trim($strOrderBy);
$numrows=gSQLRowCount($strWhereClause);
}
LogInfo($strSQL);
// Pagination:
$nPageSize=0;
if(@$_REQUEST["records"]=="page" && $numrows)
{
$mypage=(integer)@$_SESSION[$strTableName."_pagenumber"];
$nPageSize=(integer)@$_SESSION[$strTableName."_pagesize"];
if($numrows<=($mypage-1)*$nPageSize)
$mypage=ceil($numrows/$nPageSize);
if(!$nPageSize)
$nPageSize=$gPageSize;
if(!$mypage)
$mypage=1;
$strSQL.=" limit ".(($mypage-1)*$nPageSize).",".$nPageSize;
}
$rs=db_query($strSQL,$conn);
if(!ini_get("safe_mode"))
set_time_limit(300);
if(@$_REQUEST["type"]=="excel")
ExportToExcel();
else if(@$_REQUEST["type"]=="word")
ExportToWord();
else if(@$_REQUEST["type"]=="xml")
ExportToXML();
else if(@$_REQUEST["type"]=="csv")
ExportToCSV();
else if(@$_REQUEST["type"]=="pdf")
ExportToPDF();
db_close($conn);
return;
}
header("Expires: Thu, 01 Jan 1970 00:00:01 GMT");
include('libs/Smarty.class.php');
$smarty = new Smarty();
$smarty->assign("options",$options);
$smarty->display("_category_export.htm");
function ExportToExcel()
{
global $cCharset;
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment;Filename=_category.xls");
echo "<html>";
echo "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";
echo "<body>";
echo "<table border=1>";
WriteTableData();
echo "</table>";
echo "</body>";
echo "</html>";
}
function ExportToWord()
{
global $cCharset;
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=_category.doc");
echo "<html>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";
echo "<body>";
echo "<table border=1>";
WriteTableData();
echo "</table>";
echo "</body>";
echo "</html>";
}
function ExportToXML()
{
global $nPageSize,$rs,$strTableName,$conn;
header("Content-type: text/xml");
header("Content-Disposition: attachment;Filename=_category.xml");
if(!($row=db_fetch_array($rs)))
return;
global $cCharset;
echo "<?xml version=\"1.0\" encoding=\"".$cCharset."\" standalone=\"yes\"?>\r\n";
echo "<table>\r\n";
$i=0;
while((!$nPageSize || $i<$nPageSize) && $row)
{
echo "<row>\r\n";
$field=htmlspecialchars(XMLNameEncode("id"));
echo "<".$field.">";
echo htmlspecialchars(GetData($row,"id",""));
echo "</".$field.">\r\n";
$field=htmlspecialchars(XMLNameEncode("category"));
echo "<".$field.">";
echo htmlspecialchars(GetData($row,"category",""));
echo "</".$field.">\r\n";
$field=htmlspecialchars(XMLNameEncode("info"));
echo "<".$field.">";
echo htmlspecialchars(GetData($row,"info",""));
echo "</".$field.">\r\n";
echo "</row>\r\n";
$i++;
$row=db_fetch_array($rs);
}
echo "</table>\r\n";
}
function ExportToCSV()
{
global $rs,$nPageSize,$strTableName,$conn;
header("Content-type: application/csv");
header("Content-Disposition: attachment;Filename=_category.csv");
if(!($row=db_fetch_array($rs)))
return;
$totals=array();
// write header
$outstr="";
if($outstr!="")
$outstr.=",";
$outstr.= "\"id\"";
if($outstr!="")
$outstr.=",";
$outstr.= "\"category\"";
if($outstr!="")
$outstr.=",";
$outstr.= "\"info\"";
echo $outstr;
echo "\r\n";
// write data rows
$iNumberOfRows = 0;
while((!$nPageSize || $iNumberOfRows<$nPageSize) && $row)
{
$outstr="";
if($outstr!="")
$outstr.=",";
$format="";
$outstr.='"'.htmlspecialchars(GetData($row,"id",$format)).'"';
if($outstr!="")
$outstr.=",";
$format="";
$outstr.='"'.htmlspecialchars(GetData($row,"category",$format)).'"';
if($outstr!="")
$outstr.=",";
$format="";
$outstr.='"'.htmlspecialchars(GetData($row,"info",$format)).'"';
echo $outstr;
echo "\r\n";
$iNumberOfRows++;
$row=db_fetch_array($rs);
}
// display totals
$first=true;
}
function WriteTableData()
{
global $rs,$nPageSize,$strTableName,$conn;
if(!($row=db_fetch_array($rs)))
return;
// write header
echo "<tr>";
if($_REQUEST["type"]=="excel")
{
echo '<td style="width: 100" x:str>'.PrepareForExcel("id").'</td>';
echo '<td style="width: 100" x:str>'.PrepareForExcel("category").'</td>';
echo '<td style="width: 100" x:str>'.PrepareForExcel("info").'</td>';
}
else
{
echo "<td>id</td>";
echo "<td>category</td>";
echo "<td>info</td>";
}
echo "</tr>";
$totals=array();
// write data rows
$iNumberOfRows = 0;
while((!$nPageSize || $iNumberOfRows<$nPageSize) && $row)
{
echo "<tr>";
echo '<td>';
$format="";
echo htmlspecialchars(GetData($row,"id",$format));
echo '</td>';
if($_REQUEST["type"]=="excel")
echo '<td x:str>';
else
echo '<td>';
$format="";
if($_REQUEST["type"]=="excel")
echo PrepareForExcel(GetData($row,"category",$format));
else
echo htmlspecialchars(GetData($row,"category",$format));
echo '</td>';
if($_REQUEST["type"]=="excel")
echo '<td x:str>';
else
echo '<td>';
$format="";
if($_REQUEST["type"]=="excel")
echo PrepareForExcel(GetData($row,"info",$format));
else
echo htmlspecialchars(GetData($row,"info",$format));
echo '</td>';
echo "</tr>";
$iNumberOfRows++;
$row=db_fetch_array($rs);
}
}
function XMLNameEncode($strValue)
{
$search=array(" ","#","'","/","\\","(",")",",","[","]","+","\"","-","_","|","}","{","=");
return str_replace($search,"",$strValue);
}
function PrepareForExcel($str)
{
$ret = htmlspecialchars($str);
if (substr($ret,0,1)== "=")
$ret = "=".substr($ret,1);
return $ret;
}
function ExportToPDF()
{
global $nPageSize,$rs,$strTableName,$conn;
global $colwidth,$leftmargin;
if(!($row=db_fetch_array($rs)))
return;
include("libs/fpdf.php");
class PDF extends FPDF
{
//Current column
var $col=0;
//Ordinate of column start
var $y0;
var $maxheight;
function AcceptPageBreak()
{
global $colwidth,$leftmargin;
if($this->y0+$this->rowheight>$this->PageBreakTrigger)
return true;
$x=$leftmargin;
if($this->maxheight<$this->PageBreakTrigger-$this->y0)
$this->maxheight=$this->PageBreakTrigger-$this->y0;
$this->Rect($x,$this->y0,$colwidth["id"],$this->maxheight);
$x+=$colwidth["id"];
$this->Rect($x,$this->y0,$colwidth["category"],$this->maxheight);
$x+=$colwidth["category"];
$this->Rect($x,$this->y0,$colwidth["info"],$this->maxheight);
$x+=$colwidth["info"];
$this->maxheight = $this->rowheight;
// draw frame
return true;
}
function Header()
{
global $colwidth,$leftmargin;
//Page header
$this->SetFillColor(192);
$this->SetX($leftmargin);
$this->Cell($colwidth["id"],$this->rowheight,"id",1,0,'C',1);
$this->Cell($colwidth["category"],$this->rowheight,"category",1,0,'C',1);
$this->Cell($colwidth["info"],$this->rowheight,"info",1,0,'C',1);
$this->Ln($this->rowheight);
$this->y0=$this->GetY();
}
}
$pdf=new PDF();
$leftmargin=5;
$pagewidth=200;
$pageheight=290;
$rowheight=5;
$defwidth=$pagewidth/3;
$colwidth=array();
$colwidth["id"]=$defwidth;
$colwidth["category"]=$defwidth;
$colwidth["info"]=$defwidth;
$pdf->AddFont('CourierNewPSMT','','courcp1253.php');
$pdf->rowheight=$rowheight;
$pdf->SetFont('CourierNewPSMT','',8);
$pdf->AddPage();
$i=0;
while((!$nPageSize || $i<$nPageSize) && $row)
{
$pdf->maxheight=$rowheight;
$x=$leftmargin;
$pdf->SetY($pdf->y0);
$pdf->SetX($x);
$pdf->MultiCell($colwidth["id"],$rowheight,GetData($row,"id",""));
$x+=$colwidth["id"];
if($pdf->GetY()-$pdf->y0>$pdf->maxheight)
$pdf->maxheight=$pdf->GetY()-$pdf->y0;
$pdf->SetY($pdf->y0);
$pdf->SetX($x);
$pdf->MultiCell($colwidth["category"],$rowheight,GetData($row,"category",""));
$x+=$colwidth["category"];
if($pdf->GetY()-$pdf->y0>$pdf->maxheight)
$pdf->maxheight=$pdf->GetY()-$pdf->y0;
$pdf->SetY($pdf->y0);
$pdf->SetX($x);
$pdf->MultiCell($colwidth["info"],$rowheight,GetData($row,"info",""));
$x+=$colwidth["info"];
if($pdf->GetY()-$pdf->y0>$pdf->maxheight)
$pdf->maxheight=$pdf->GetY()-$pdf->y0;
// draw fames
$x=$leftmargin;
$pdf->Rect($x,$pdf->y0,$colwidth["id"],$pdf->maxheight);
$x+=$colwidth["id"];
$pdf->Rect($x,$pdf->y0,$colwidth["category"],$pdf->maxheight);
$x+=$colwidth["category"];
$pdf->Rect($x,$pdf->y0,$colwidth["info"],$pdf->maxheight);
$x+=$colwidth["info"];
$pdf->y0+=$pdf->maxheight;
$i++;
$row=db_fetch_array($rs);
}
$pdf->Output();
}
?>