<?php
/*
* ARBS - Advanced Resource Booking System
* Copyright (C) 2005-2007 ITMC der TU Dortmund
* Based on MRBS by Daniel Gardner <http://mrbs.sourceforge.net/>
*
* 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 Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
//saves insert,update and delte operations in xml format at $instance/history.xml
//needed for restore and debugging
function saveHistoryTicket($sql,$affrows){
global $instance;
$reger="([\w\d\)\(\,\'\= äöüÃ\";.:\-<>_\/]*)";
$regspace="([\s]*)";
$sql=stripslashes(trim($sql));
$op=strtoupper(substr($sql,0,6));
if($op=="INSERT"){
$values="";
$con="<where/>\n";
eregi(" into () VALUES (.)",$sql,$reg);
preg_match("(INSERT".$regspace."INTO".$regspace."([\d\w]+)".$regspace."\(([\w\d\)\(\, ]*)\)".$regspace."VALUES".$regspace."\(".$reger."\))",$sql,$reg);
$table=$reg[3];
$fields=explode(",",$reg[5]);
$pp=splitByComma($reg[8]);
foreach($pp as $key=>$val){
$values.="<value field=\"".$fields[$key]."\">".xmlencode($val)."</value>\n";
}
}
elseif($op=="DELETE"){
$values="";
$con="";
preg_match("(DELETE".$regspace."FROM".$regspace."([\d\w]+)".$regspace."WHERE".$reger.")",$sql,$reg);
$table=$reg[3];
$con="<where>".xmlencode(trim($reg[5]))."</where>\n";
}
elseif($op=="UPDATE"){
$values="";
$con="";
preg_match("(UPDATE".$regspace."([\d\w]+)".$regspace."SET".$reger."WHERE".$regspace."".$reger.")",$sql,$reg);
$table=$reg[2];
$values="";
$pp=splitByComma($reg[4]);
foreach($pp as $val){
$n=strpos($val,"=");
$values.="<value field=\"".substr($val,0,$n)."\">".xmlencode(substr($val,$n+1))."</value>\n";
}
$con="<where>".xmlencode($reg[6])."</where>\n";
}
//on all other ops: return
else
return;
$xml="<$op instance=\"$instance\" table=\"$table\" rows=\"$affrows\" created=\"".time()."\" script=\"".$_SERVER['PHP_SELF']."\">\n";
$xml.="<query>".xmlencode($sql)."</query>\n";
$xml.=$values;
$xml.=$con;
$xml.="</$op>\n\n";
$fd=fopen($instance."/history.xml","a");
fwrite($fd,$xml);
fclose($fd);
}
#splits an sql-like string input or update string into an array . in contrast to split($input,"'")
#commans withhin highquotes are ignored
#eg ('peter','hey,foo') becomes [0]=>'peter' [1]=>'hey,foo'
#name='peter,foo', age=3 [0]=>name='peter,foo' [1]=>age=3
function splitByComma($input,$sep=","){
$parts=array();
$exat=array(-1);
$quoteopen=false;
for($n=0;$n<strlen($input);$n++){
$quoteopen^=(substr($input,$n,1)=="'" && substr($input,$n-1,1)!="\\");
if(!$quoteopen&&substr($input,$n,1)==$sep)
$exat[]=$n;
}
$exat[]=strlen($input);
foreach($exat as $key=>$val){
if($key==0)
continue;
$parts[]=trim(substr($input,$exat[$key-1]+1,$val-$exat[$key-1]-1));
}
return $parts;
}
//clear and initialize the history xml file
function createNewHistory(){
global $instance;
$fd=fopen($instance."/history.xml","w");
fwrite($fd,"<?xml version=\"1.0\"?>\n<history start=\"".time()."\">");
fclose($fd);
}
function xmlencode($str){
return urlencode($str);
}
function xmldecode($str){
return urldecode($str);
}
?>