00001 <?php
00006 define('TMP_PREFIX','TmG');
00007
00008
00029 class GenieGate_TempWriter {
00030
00031 var $RH;
00032 var $WH;
00033 var $tmp_name;
00034 var $filename;
00035 var $working_dir;
00036 var $cancel;
00037
00046 function GenieGate_TempWriter($filename){
00047
00048
00049 if(! file_exists($filename)){
00050 fclose(fopen($filename,"a")) or die("Can't open [$filename]");
00051 }
00052
00053 $this->filename = $filename;
00054 $this->cancel = FALSE;
00055
00056
00057 $this->RH = fopen($filename,'r+') or die("Can't open $filename");
00058 if($this->RH){
00059 flock($this->RH,LOCK_EX);
00060 $this->working_dir = dirname($filename);
00061
00062 $this->tmp_name = tempnam($this->working_dir,TMP_PREFIX);
00063 $this->WH = fopen($this->tmp_name,"w");
00064 }
00065 }
00066
00070 function getFilename(){ return($this->filename()); }
00071
00077 function getRead(){ return($this->RH); }
00083 function getWrite(){ return($this->WH); }
00084
00092 function cancel(){
00093 if(! $this->cancel){
00094 $this->_close();
00095 unlink($this->tmp_name);
00096 $this->cancel = TRUE;
00097 if(file_exists($this->tmp_name)){
00098 unlink($this->tmp_name);
00099 }
00100 return(TRUE);
00101 }
00102 return(FALSE);
00103 }
00107 function isCanceled(){ return($this->cancel); }
00108
00109
00110 function _close(){
00111 flock($this->RH,LOCK_UN);
00112 fclose($this->RH);
00113 fclose($this->WH);
00114 $this->WH = FALSE;
00115 $this->RH = FALSE;
00116 }
00125 function complete(){
00126 if($this->isCanceled()){
00127 return(FALSE);
00128 }
00129
00130 $perm = fileperms($this->filename);
00131
00132
00133 fflush($this->WH);
00134 $backup = $this->filename . ".Bck";
00135
00136 if(rename($this->filename,$backup)){
00137 if(rename($this->tmp_name,$this->filename)){
00138 unlink($backup);
00139 $this->_close();
00140 chmod($this->filename,$perm);
00141 return(TRUE);
00142 }else{
00143
00144 error_log("Could not rename [$this->tmp_name] to [$this->filename]");
00145 rename($backup,$this->filename);
00146 }
00147 }else{
00148
00149 error_log("Could not rename [$this->filename] to [$backup]");
00150 unlink($this->tmp_name);
00151 }
00152 $this->_close();
00153 return(FALSE);
00154 }
00155
00156 }
00157
00163 class GenieGate_XMLWriter {
00164
00165 var $TAGS = array();
00169 var $FORMAT = FALSE;
00170
00171 var $H;
00172
00178 function GenieGate_XMLWriter($header = TRUE, $format = FALSE){
00179 $this->FORMAT = $format;
00180 if($header){
00181 $this->out("<?xml version=\"1.0\"?>\n");
00182 }
00183 }
00184
00191 function out($raw){
00192 echo $raw;
00193 }
00200 function escape($string){
00201 return(htmlentities($string,ENT_QUOTES));
00202 }
00207 function text($string){
00208 $this->out($this->escape($string));
00209 if($this->FORMAT){
00210 $this->out("\n");
00211 }
00212 }
00219 function start($tag,$attr = array()){
00220 if($this->FORMAT){
00221 $tb = str_repeat("\t",count($this->TAGS));
00222 }
00223 array_push($this->TAGS,$tag);
00224 $xml = "$tb<$tag";
00225
00226 foreach($attr as $at => $val){
00227 $xml .= " $at=\"" . $this->escape($val) . "\"";
00228 }
00229
00230 $xml .= ">";
00231 if($this->FORMAT){
00232 $xml .= "\n";
00233 }
00234 $this->out($xml);
00235 }
00241 function atom($tag,$attr = array()){
00242 if($this->FORMAT){
00243 $tb = str_repeat("\t",count($this->TAGS));
00244 }
00245 $xml = "$tb<$tag";
00246 foreach($attr as $at => $val){
00247 $xml .= " $at=\"" . $this->escape($val) . "\"";
00248 }
00249 $xml .= "/>";
00250 if($this->FORMAT){
00251 $xml .= "\n";
00252 }
00253 $this->out($xml);
00254 }
00258 function close(){
00259 $str = array_pop($this->TAGS);
00260 if($this->FORMAT){
00261 $tb = str_repeat("\t",count($this->TAGS));
00262 $xml = "$tb</$str>\n";
00263 }else{
00264 $xml = "</$str>";
00265 }
00266 $this->out($xml);
00267 }
00272 function finish(){
00273 foreach($this->TAGS as $t){
00274 $this->close();
00275 }
00276 $this->out("\n");
00277 }
00278 }
00279
00280
00281 ?>