Source for file dftk_file_change.php
Documentation is available at dftk_file_change.php
1 <?php 2 /* 3 Copyright (c)2002-2003 DuckCorp(tm) and RtpNet(tm) 4 5 6 7 This file is part of DFTK. 8 9 DFTK is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 2 of the License, or 12 (at your option) any later version. 13 14 LMS is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with LMS; if not, write to the Free Software 21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 */ 23 24 25 /** 26 * @package dftk 27 * @author Duck <duck@DuckCorp.org> 28 * @author Rtp <rtp@rtp-net.org> 29 * @copyright Copyright (c)2003 DuckCorp(tm) and RtpNet(tm) 30 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 31 * @version 0.4.0 32 */ 33 34 35 /** 36 * DFTK File Change Class 37 * 38 * This classe is used to manage things like backup of 39 * the configuration file if modified, writing to and so on... 40 * 41 * @package dftk 42 * @subpackage dftk-file 43 * @author Rtp <rtp@rtp-net.org> 44 * 45 * @access public 46 */ 47 class DftkFileChange 48 { 49 /** 50 * Trace Manager 51 * 52 * @access private 53 * @var string 54 */ 55 var $_tracemgr; 56 57 /** 58 * Configuration file name 59 * 60 * @access private 61 * @var string 62 */ 63 var $_Filename; 64 65 /** 66 * File path 67 * 68 * @access private 69 * @var string 70 */ 71 var $_Path; 72 73 /** 74 * File pointer 75 * 76 * @access private 77 * @var resource 78 */ 79 var $_Fp; 80 81 /** 82 * Backup file extenstion 83 * 84 * @access private 85 * @var string 86 */ 87 var $_BackExt; 88 89 /** 90 * Constructor 91 * 92 * @access public 93 * 94 * @param object DftkDaTraceManager &$tracemgr Language Manager 95 * @param string $file Configuration file name 96 * @param string $path File path 97 * @param string $ext Backup extenstion 98 */ 99 function DftkFileChange(&$tracemgr, $file, $path="./", $ext="bak") 100 { 101 $this->_tracemgr =& $tracemgr; 102 103 if (!$this->_tracemgr->is_module("DFTK-FILE")) 104 $this->_tracemgr->register_traces("DFTK-FILE",DFTK_ROOT_PATH."/file/traces/"); 105 106 $this->_Filename=$file; 107 108 $this->_Path=$path; 109 /* Be sure that the path is ended with a slash */ 110 if (strcmp(substr($this->_Path,-1),'/') != 0) 111 $this->_Path=$this->_Path."/"; 112 113 $this->_BackExt=".".$ext; 114 115 $this->_Fp = false; 116 } 117 118 /** 119 * Open the file to handle 120 * 121 * @access public 122 * @return object DftkDaTrace $r Trace 123 */ 124 function &open() 125 { 126 $r =& $this->_tracemgr->create_trace(); 127 128 if ($this->_Fp) 129 $r->add_event('dftk-file_alrop'); 130 else 131 { 132 $origfile = $this->_Path.$this->_Filename; 133 $myfile = $origfile.".tmp"; 134 135 $this->_Fp=fopen($myfile,"w"); 136 if (!$this->_Fp) 137 $r->add_event('dftk-file_ioerr'); 138 } 139 140 return $r; 141 } 142 143 /** 144 * Close the file 145 * 146 * Result : 147 * + 'file_changed' : true if the file has changed 148 * 149 * @access public 150 * @return object DftkDaTrace $r Trace 151 */ 152 function &close() 153 { 154 $r =& $this->_tracemgr->create_trace(); 155 156 if ($this->_Fp) 157 { 158 if (!fclose($this->_Fp)) 159 $r->add_event('dftk-file_ioerr'); 160 else 161 { 162 $origfile = $this->_Path.$this->_Filename; 163 $backupfile = $origfile.$this->_BackExt; 164 $myfile = $origfile.".tmp"; 165 166 $maj = $this->_is_maj(); 167 $r->set_result('file_changed', $maj); 168 169 if ($maj) 170 { 171 if (file_exists($origfile)) 172 if (!copy($origfile, $backupfile)) 173 $r->add_event('dftk-file_ioerr'); 174 if (!$r->has_error()) 175 if (!copy($myfile, $origfile)) 176 $r->add_event('dftk-file_ioerr'); 177 } 178 179 if (!unlink($myfile)) 180 $r->add_event('dftk-file_ioerr'); 181 } 182 } 183 else 184 $r->add_event('dftk-file_notop'); 185 186 return $r; 187 } 188 189 /** 190 * Write to the file 191 * 192 * Result : 193 * + 'writen_bytes' : number of bytes really written 194 * 195 * @access public 196 * @param string $string String to write 197 * @param integer $length Number of bytes to write 198 * @return object DftkDaTrace $r Trace 199 */ 200 function &write($string, $length=0) 201 { 202 if ($this->_Fp) 203 { 204 $r =& $this->_tracemgr->create_trace(); 205 206 if ($length) 207 $n = fwrite($this->_Fp,$string,$length); 208 else 209 $n = fwrite($this->_Fp,$string); 210 211 if ($n) 212 $r->set_result('writen_bytes', $n); 213 else 214 $r->add_event('dftk-file_ioerr'); 215 } 216 else 217 $r->add_event('dftk-file_notop'); 218 219 return $r; 220 } 221 222 /** 223 * Check if the file was updated 224 * 225 * @access private 226 * @return boolean $err True if the file was updated 227 */ 228 function _is_maj() 229 { 230 $origfile = $this->_Path.$this->_Filename; 231 $myfile = $origfile.".tmp"; 232 233 234 if (!file_exists($origfile)) 235 return true; 236 237 /* php versions prior to 4.2.0 don't have a md5_file function */ 238 if (!function_exists('md5_file')) 239 { 240 $orig_md5=md5(implode("", file($origfile))); 241 $my_md5 =md5(implode("", file($myfile))); 242 } 243 else 244 { 245 $orig_md5=md5_file($origfile); 246 $my_md5 =md5_file($myfile); 247 } 248 249 if ($orig_md5 != $my_md5) 250 return true; 251 252 return false; 253 } 254 } 255 ?>
|