1<?php 2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 3 4/** 5 * DNS Library for handling lookups and updates. 6 * 7 * PHP Version 5 8 * 9 * Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>. 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * * Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * * Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in 21 * the documentation and/or other materials provided with the 22 * distribution. 23 * 24 * * Neither the name of Mike Pultz nor the names of his contributors 25 * may be used to endorse or promote products derived from this 26 * software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 31 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 32 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 34 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 36 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 38 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39 * POSSIBILITY OF SUCH DAMAGE. 40 * 41 * @category Networking 42 * @package Net_DNS2 43 * @author Mike Pultz <mike@mikepultz.com> 44 * @copyright 2010 Mike Pultz <mike@mikepultz.com> 45 * @license http://www.opensource.org/licenses/bsd-license.php BSD License 46 * @version SVN: $Id$ 47 * @link http://pear.php.net/package/Net_DNS2 48 * @since File available since Release 1.1.0 49 * 50 */ 51 52/** 53 * File-based caching for the Net_DNS2_Cache class 54 * 55 * @category Networking 56 * @package Net_DNS2 57 * @author Mike Pultz <mike@mikepultz.com> 58 * @license http://www.opensource.org/licenses/bsd-license.php BSD License 59 * @link http://pear.php.net/package/Net_DNS2 60 * @see Net_DNS2_Packet 61 * 62 */ 63class Net_DNS2_Cache_File extends Net_DNS2_Cache 64{ 65 /** 66 * open a cache object 67 * 68 * @param string $cache_file path to a file to use for cache storage 69 * @param integer $size the size of the shared memory segment to create 70 * @param string $serializer the name of the cache serialize to use 71 * 72 * @throws Net_DNS2_Exception 73 * @access public 74 * @return void 75 * 76 */ 77 public function open($cache_file, $size, $serializer) 78 { 79 $this->cache_size = $size; 80 $this->cache_file = $cache_file; 81 $this->cache_serializer = $serializer; 82 83 // 84 // check that the file exists first 85 // 86 if ( ($this->cache_opened == false) 87 && (file_exists($this->cache_file) == true) 88 && (filesize($this->cache_file) > 0) 89 ) { 90 // 91 // open the file for reading 92 // 93 $fp = @fopen($this->cache_file, 'r'); 94 if ($fp !== false) { 95 96 // 97 // lock the file just in case 98 // 99 flock($fp, LOCK_EX); 100 101 // 102 // read the file contents 103 // 104 $data = fread($fp, filesize($this->cache_file)); 105 106 $decoded = null; 107 108 if ($this->cache_serializer == 'json') { 109 110 $decoded = json_decode($data, true); 111 } else { 112 113 $decoded = unserialize($data); 114 } 115 116 if (is_array($decoded) == true) { 117 118 $this->cache_data = $decoded; 119 } else { 120 121 $this->cache_data = array(); 122 } 123 124 // 125 // unlock 126 // 127 flock($fp, LOCK_UN); 128 129 // 130 // close the file 131 // 132 fclose($fp); 133 134 // 135 // clean up the data 136 // 137 $this->clean(); 138 139 // 140 // mark this so we don't read this contents more than once per instance. 141 // 142 $this->cache_opened = true; 143 } 144 } 145 } 146 147 /** 148 * Destructor 149 * 150 * @access public 151 * 152 */ 153 public function __destruct() 154 { 155 // 156 // if there's no cache file set, then there's nothing to do 157 // 158 if (strlen($this->cache_file) == 0) { 159 return; 160 } 161 162 // 163 // open the file for reading/writing 164 // 165 $fp = fopen($this->cache_file, 'a+'); 166 if ($fp !== false) { 167 168 // 169 // lock the file just in case 170 // 171 flock($fp, LOCK_EX); 172 173 // 174 // seek to the start of the file to read 175 // 176 fseek($fp, 0, SEEK_SET); 177 178 // 179 // read the file contents 180 // 181 $data = @fread($fp, filesize($this->cache_file)); 182 if ( ($data !== false) && (strlen($data) > 0) ) { 183 184 // 185 // unserialize and store the data 186 // 187 $c = $this->cache_data; 188 189 $decoded = null; 190 191 if ($this->cache_serializer == 'json') { 192 193 $decoded = json_decode($data, true); 194 } else { 195 196 $decoded = unserialize($data); 197 } 198 199 if (is_array($decoded) == true) { 200 201 $this->cache_data = array_merge($c, $decoded); 202 } 203 } 204 205 // 206 // trucate the file 207 // 208 ftruncate($fp, 0); 209 210 // 211 // clean the data 212 // 213 $this->clean(); 214 215 // 216 // resize the data 217 // 218 $data = $this->resize(); 219 if (!is_null($data)) { 220 221 // 222 // write the file contents 223 // 224 fwrite($fp, $data); 225 } 226 227 // 228 // unlock 229 // 230 flock($fp, LOCK_UN); 231 232 // 233 // close the file 234 // 235 fclose($fp); 236 } 237 } 238}; 239 240/* 241 * Local variables: 242 * tab-width: 4 243 * c-basic-offset: 4 244 * c-hanging-comment-ender-p: nil 245 * End: 246 */ 247?> 248