1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
<?php
// IPplan v4.92a
// Aug 24, 2001
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// workaround for funky behaviour of nested includes with older
// versions of php
require_once(dirname(__FILE__)."/config.php");
// used for the data export functionality
// its functions are used by php files that display records
// and ipplan/user/exportview.php
class exportForm {
var $xprtRecords;
var $rowArray;
var $xprtInfo;
function exportForm(){
if (EXPORTENABLED == TRUE)
session_start();
$this->xprtRecords = NULL;
$this->rowArray = NULL;
$this->xprtInfo = NULL;
}
// set the xprtInfo array
function setInfo($array){
$this->xprtInfo = $array;
}
// add row to the xprtrecords array
function addRow($array){
if ($array == NULL) $this->rowArray = array();
else $this->rowArray = $array;
}
// add a cell to a row
function addCell($val){
array_push($this->rowArray, $val);
}
// save a row
function saveRow(){
if ($this->xprtRecords == NULL) $this->xprtRecords = array();
array_push($this->xprtRecords, $this->rowArray);
}
// make search criterion more readable if page includes one
function translateExpr($expr){
if ($expr == "START") $expression = "starts with";
elseif ($expr == "END") $expression = "ends with";
elseif ($expr == "LIKE") $expression = "contains";
elseif ($expr == "NLIKE") $expression = "does not contain";
elseif ($expr == "EXACT") $expression = "is";
elseif ($expr == "RLIKE") $expression = "contains regular expression";
elseif ($expr == "NRLIKE") $expression = "does not contain regular expression";
else $expression = "";
return $expression;
}
// initialize the class. pass in info and records arrays of variables from the page;
// $w holds the block where the export form will be inserted. creates the "export view" form
function createExportForm($w, $template=NULL) {
if (EXPORTENABLED == FALSE) return;
$page = $_SERVER['SCRIPT_NAME'];
//template handling
if ($template != NULL){
$userfld = $template->return_userfld();
foreach ($userfld as $field=>$val) {
array_push($this->xprtInfo[0], $field);
array_push($this->xprtInfo[1], isset($userfld["$field"]["value"]) ? $userfld["$field"]["value"] : $userfld["$field"]["default"]);
}
}
$_SESSION['info'] = $this->xprtInfo;
$_SESSION['records'] = $this->xprtRecords;
insert($w, $f = form(array("name"=>"EXPORT",
"method"=>"post",
"action"=>"../user/exportview.php")));
insert($f,hidden(array("name"=>"page", "value"=>"$page")));
// what other formats should we have?
$formatoptions = array("plain"=>my_("Plain text"),
"csv"=>my_("Comma delimited"),
"tab"=>my_("Tab delimited"),
"xml"=>my_("XML"));
insert($f, selectbox($formatoptions, array("name"=>"ftype")));
insert($f,submit(array("value"=>my_("Export view"), "onClick"=>"javascript:window.location.reload()")));
}
// writes data to the file in CSV format
function writeCSV($info, $records, $page) {
$tempfile="$page\n\n";
$first = 0;
foreach ($info as $info1) {
foreach ($info1 as $info2) {
if ($first != 0) $tempfile.=", ";
$data = "$info2";
$tempfile.=$data;
$first = 1;
}
$tempfile.="\n";
$first = 0;
}
if (isset($records)) {
$tempfile.="\n";
foreach ($records as $records1) {
foreach ($records1 as $records2) {
if ($first != 0) $tempfile.=", ";
$data = "$records2";
$tempfile.=$data;
$first = 1;
}
$tempfile.="\n";
$first = 0;
}
}
return $tempfile;
}
function writePlain($info, $records, $page) {
$tempfile="$page\n\n";
$temparray1 = $info[0];
unset($info[0]);
$count = 0;
foreach ($info as $info1) {
foreach ($info1 as $info2) {
$data = "$temparray1[$count]: $info2\n";
$tempfile.=$data;
$count = $count + 1;
}
$tempfile.="\n";
$count = 0;
}
if (isset($records)) {
$temparray2 = $records[0];
unset($records[0]);
foreach ($records as $records1) {
foreach ($records1 as $records2) {
$data = "$temparray2[$count]: $records2\n";
$tempfile.=$data;
$count = $count + 1;
}
$tempfile.="\n";
$count = 0;
}
}
return $tempfile;
}
function writeTab($info, $records, $page) {
$tempfile="$page\n\n";
$first = 0;
foreach ($info as $info1) {
foreach ($info1 as $info2) {
if ($first != 0) $tempfile.="\t";
$data = "$info2";
$tempfile.=$data;
$first = 1;
}
$tempfile.="\n";
$first = 0;
}
if (isset($records)) {
$tempfile.="\n";
foreach ($records as $records1) {
foreach ($records1 as $records2) {
if ($first != 0) $tempfile.="\t";
$data = "$records2";
$tempfile.=$data;
$first = 1;
}
$tempfile.="\n";
$first = 0;
}
}
return $tempfile;
}
function writeXML($info, $records, $page) {
$page = str_replace("/", "_", $page);
$tempfile="<?xml version='1.0'?>\n";
$tempfile.="<$page>\n";
$temparray1 = $info[0];
unset($info[0]);
$count = 0;
foreach ($info as $info1) {
$tempfile.="<info>\n";
foreach ($info1 as $info2) {
$data = "<".$temparray1[$count].">".$info2."</".$temparray1[$count].">\n";
$tempfile.=$data;
$count = $count + 1;
}
$tempfile.="</info>\n";
$count = 0;
}
if (isset($records)) {
$temparray2 = $records[0];
unset($records[0]);
foreach ($records as $records1) {
$tempfile.="<record>\n";
foreach ($records1 as $records2) {
$data = "<".$temparray2[$count].">".$records2."</".$temparray2[$count].">\n";
$tempfile.=$data;
$count = $count + 1;
}
$tempfile.="</record>\n";
$count = 0;
}
}
$tempfile.="</$page>\n";
return $tempfile;
}
// creates the file to be exported, calls the appropriate write function, and returns the file name
function createFile($info, $records, $ftype, $page) {
if ($ftype == "csv") $tempfile = exportForm::writeCSV($info, $records, $page);
elseif ($ftype == "plain") $tempfile = exportForm::writePlain($info, $records, $page);
elseif ($ftype == "tab") $tempfile = exportForm::writeTab($info, $records, $page);
elseif ($ftype == "xml") $tempfile = exportForm::writeXML($info, $records, $page);
return $tempfile;
}
// serve the file as a download to the user
function serveFile($file, $page, $ftype) {
$filename = getAuthUsername().$page.time();
// append file extension type
if ($ftype == "csv") $filename .= ".csv";
elseif ($ftype == "plain") $filename .= ".txt";
elseif ($ftype == "tab") $filename .= ".txt";
elseif ($ftype == "xml") $filename .= ".xml";
//$file = file_get_contents($tempfname);
// change content type based on file extension
if ($ftype == "csv") header('Content-Type: text/csv');
elseif ($ftype == "plain") header('Content-Type: text/plain');
elseif ($ftype == "tab") header('Content-Type: text/plain');
elseif ($ftype == "xml") header('Content-Type: application/xml');
header("Content-Disposition: attachment; filename=$filename");
header('Content-Length: ' . strlen($file));
echo $file;
}
}
?>
|