Freya Database is a Web 2.0 (AJAX) based software for money management. Works with various users, accounts, operations, with stats, charts and with a high security level. It needs a web server, PHP 5 and MySQL to run on a local computer, or a web hosting to run on the internet.
<?php
// Zen Database Manager (Database Import/Export For ZenCart)
// Copyright (C) 2005 Jarrod Connolly
//
// 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
// include library for working with zip files
require_once('pclzip.lib.php');
function cleartempfolder()
{
global $tempdir, $tablenamelist;
foreach ($tablenamelist as $i => $tablename)
{
$filename = $tempdir.$tablename.'.xml';
if( file_exists($filename) )
{
unlink( $filename );
}
}
if( file_exists($tempdir.'export.zip') )
{
unlink( $tempdir.'export.zip' );
}
}
function createexportfile()
{
global $tempdir, $tablenamelist;
// list of xml files for zip
$filelist = array();
foreach ($tablenamelist as $i => $tablename)
{
$filename = $tempdir.$tablename.'.xml';
// query database and save xml file
$xml = BuildZenXML('select * from '.$tablename, $tablename);
//save xml data to file
WriteXMLDataToFile( $xml, $filename );
// add filename to array
$filelist[]=$filename;
}
// create zip archive for download
$archive = new PclZip($tempdir.'export.zip');
$v_list = $archive->add( $filelist, PCLZIP_OPT_REMOVE_PATH, $tempdir );
if ($v_list == 0)
{
die("Error : ".$archive->errorInfo(true));
}
}
function downloadfile()
{
global $tempdir;
$fileName = "export.zip"; // supply a file name.
$fileString=$tempdir.$fileName; // combine the path and file
// translate file name properly for Internet Explorer.
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
$fileName = preg_replace('/\./', '%2e', $fileName, substr_count($fileName, '.') - 1);
}
// make sure the file exists before sending headers
if(!$fdl=fopen($fileString,'r'))
{
die("Cannot Open File!");
}
else
{
header("Cache-Control: ");// leave blank to avoid IE errors
header("Pragma: ");// leave blank to avoid IE errors
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$fileName."\"");
header("Content-length:".(string)(filesize($fileString)));
sleep(1);
fpassthru($fdl);
fclose($fd1);
}
}
function testtempdir()
{
global $tempdir;
if (!is_dir("$tempdir"))
{
echo "<br>The directory <b>(" . $tempdir . ")</b> doesn't exist";
}
if (!is_writeable("$tempdir"))
{
echo "<br>The directory <b>(" . $tempdir . ")</b> is NOT writable, Please Chmod (777)";
}
}
function moveuploadfile()
{
global $tempdir;
if (is_uploaded_file($_FILES['uploadedfile']['tmp_name']))
{
//get file size
$size = $_FILES['uploadedfile']['size'];
//get filename
//$filename = $_FILES['uploadedfile']['name'];
$filename = 'export.zip';
// put uploaded file in temp folder
move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$tempdir.$filename);
}
}
function unpackuploadfile()
{
global $tempdir;
$archive = new PclZip($tempdir.'export.zip');
$archive->extract(PCLZIP_OPT_PATH, $tempdir);
}
function makedatabasechanges()
{
global $tempdir, $tablenamelist;
foreach ($tablenamelist as $i => $tablename)
{
$filename = $tempdir.$tablename.'.xml';
$data = GetXMLDataFromFile( $filename );
UpdateDataBase( $data, $tablename );
}
}
function UpdateDataBase( $data, $tablename )
{
global $db;
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $values, $tags);
xml_parser_free($parser);
foreach ($tags as $key=>$val)
{
if( $key == $tablename )
{
$rsTableMeta = $db->MetaColumns($tablename);
for ($i=0; $i < count($val); $i+=2)
{
$query = 'update '.$tablename.' set ';
$whereclause = '';
$offset = $val[$i] + 1;
$len = $val[$i + 1] - $offset;
$tvalues = array_slice($values, $offset, $len);
for ($j=0; $j < count($tvalues); $j++)
{
$fieldname = $tvalues[$j]["tag"];
$fieldvalue = mysql_escape_string( $tvalues[$j]["value"] );
$fieldflags = $rsTableMeta[strtoupper($fieldname)]->flags;
$flagarray = explode( " ", $fieldflags );
if( in_array( "primary_key", $flagarray ) )
{
if( $whereclause == "" )
{
$whereclause = ' where ';
}
else
{
$whereclause .= ' and ';
}
$whereclause .= $fieldname."='".$fieldvalue."'";
}
else
{
$query .= $fieldname."='".$fieldvalue."',";
}
}
$query = substr($query,0,strlen($query)-1);
$query .= $whereclause;
$db->Execute($query);
}
}
}
}
function GetXMLDataFromFile( $filename )
{
$data = '';
if (file_exists($filename))
{
$f = fopen($filename,'r');
$data = fread($f,filesize($filename));
fclose($f);
}
return $data;
}
function WriteXMLDataToFile( $xmldata, $filename )
{
$f = @fopen($filename,'w');
@fwrite($f,$xmldata);
@fclose($f);
}
function & BuildZenXML($query, $tablename)
{
global $db;
$rs = $db->Execute($query);
$xml .= '<ZEN>'."\n";
while (!$rs->EOF)
{
$xml .= "\t".'<'.$tablename.'>'."\n";
$keys = $rs->fields;
foreach($keys as $FieldName => $FieldValue)
{
$content = $FieldValue;
if( $content != NULL )
{
$xml .= "\t\t".'<'.strtolower($FieldName).'>';
$content =& str_replace( "&", "&amp;", $content );
$content =& str_replace( ">", "&gt;", $content );
$content =& str_replace( "<", "&lt;", $content );
$content =& str_replace( "'", "&apos;", $content );
$content =& str_replace( '"', "&quot;", $content );
$xml .= $content;
$xml .= '</'.$FieldName.'>'."\n";
}
}
$xml .= "\t".'</'.$tablename.'>'."\n";
$rs->MoveNext();
$j++;
}
$xml .= '</ZEN>'."\n";
//@$rs->Move(0);
return $xml;
}
?>
&nbsp;