PHPIDS
Current file: | /home/lstrojny/Projekte/php-ids/trunk/lib/IDS/Caching/File.php
Legend: | executed not executed dead code

| Coverage
| Classes | Methods | Lines
Total |
| 100.00% | 1 / 1 |
| 100.00% | 5 / 5 |
| 77.50% | 31 / 40
IDS_Caching_File |
| 100.00% | 1 / 1 |
| 100.00% | 5 / 5 |
| 76.92% | 30 / 39
public function __construct($type, $init) |
| 100.00% | 1 / 1 |
| 62.50% | 5 / 8
public static function getInstance($type, $init) |
| 100.00% | 1 / 1 |
| 100.00% | 4 / 4
public function setCache(array $data) |
| 100.00% | 1 / 1 |
| 71.43% | 10 / 14
public function getCache() |
| 100.00% | 1 / 1 |
| 100.00% | 5 / 5
protected function readFile($filename) |
| 100.00% | 1 / 1 |
| 71.43% | 5 / 7

           1                 : <?php
           2                 :
           3                 : /**
           4                 :  * PHPIDS
           5                 :  *
           6                 :  * Requirements: PHP5, SimpleXML
           7                 :  *
           8                 :  * Copyright (c) 2008 PHPIDS group (http://php-ids.org)
           9                 :  *
          10                 :  * PHPIDS is free software; you can redistribute it and/or modify
          11                 :  * it under the terms of the GNU Lesser General Public License as published by
          12                 :  * the Free Software Foundation, version 3 of the License, or
          13                 :  * (at your option) any later version.
          14                 :  *
          15                 :  * PHPIDS is distributed in the hope that it will be useful,
          16                 :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
          17                 :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
          18                 :  * GNU Lesser General Public License for more details.
          19                 :  *
          20                 :  * You should have received a copy of the GNU Lesser General Public License
          21                 :  * along with PHPIDS. If not, see <http://www.gnu.org/licenses/>.
          22                 :  *
          23                 :  * PHP version 5.1.6+
          24                 :  *
          25                 :  * @category Security
          26                 :  * @package  PHPIDS
          27                 :  * @author   Mario Heiderich <mario.heiderich@gmail.com>
          28                 :  * @author   Christian Matthies <ch0012@gmail.com>
          29                 :  * @author   Lars Strojny <lars@strojny.net>
          30                 :  * @license  http://www.gnu.org/licenses/lgpl.html LGPL
          31                 :  * @link     http://php-ids.org/
          32                 :  */
          33                 :
          34               1 : require_once 'IDS/Caching/Interface.php';
          35                 :
          36                 : /**
          37                 :  * File caching wrapper
          38                 :  *
          39                 :  * This class inhabits functionality to get and set cache via a static flatfile.
          40                 :  *
          41                 :  * @category  Security
          42                 :  * @package   PHPIDS
          43                 :  * @author    Christian Matthies <ch0012@gmail.com>
          44                 :  * @author    Mario Heiderich <mario.heiderich@gmail.com>
          45                 :  * @author    Lars Strojny <lars@strojny.net>
          46                 :  * @copyright 2007 The PHPIDS Group
          47                 :  * @license   http://www.gnu.org/licenses/lgpl.html LGPL
          48                 :  * @version   Release: $Id:File.php 517 2007-09-15 15:04:13Z mario $
          49                 :  * @link      http://php-ids.org/
          50                 :  * @since     Version 0.4
          51                 :  */
          52               1 : class IDS_Caching_File implements IDS_Caching_Interface
          53                 : {
          54                 :
          55                 :     /**
          56                 :      * Caching type
          57                 :      *
          58                 :      * @var string
          59                 :      */
          60                 :     private $type = null;
          61                 :
          62                 :     /**
          63                 :      * Cache configuration
          64                 :      *
          65                 :      * @var array
          66                 :      */
          67                 :     private $config = null;
          68                 :
          69                 :     /**
          70                 :      * Path to cache file
          71                 :      *
          72                 :      * @var string
          73                 :      */
          74                 :     private $path = null;
          75                 :
          76                 :     /**
          77                 :      * Holds an instance of this class
          78                 :      *
          79                 :      * @var object
          80                 :      */
          81                 :     private static $cachingInstance = null;
          82                 :
          83                 :     /**
          84                 :      * Constructor
          85                 :      *
          86                 :      * @param string $type caching type
          87                 :      * @param array  $init the IDS_Init object
          88                 :      *
          89                 :      * @return void
          90                 :      */
          91                 :     public function __construct($type, $init)
          92                 :     {
          93                 :
          94               1 :         $this->type   = $type;
          95               1 :         $this->config = $init->config['Caching'];
          96               1 :         $this->path   = $init->getBasePath() . $this->config['path'];
          97                 :
          98               1 :         if (file_exists($this->path) && !is_writable($this->path)) {
          99               0 :             throw new Exception('Make sure all files in ' .
         100               0 :             htmlspecialchars($this->path, ENT_QUOTES, 'UTF-8') .
         101               0 :                 'are writeable!');
         102                 :         }
         103               1 :     }
         104                 :
         105                 :     /**
         106                 :      * Returns an instance of this class
         107                 :      *
         108                 :      * @param string $type caching type
         109                 :      * @param array  $init the IDS_Init object
         110                 :      *
         111                 :      * @return object $this
         112                 :      */
         113                 :     public static function getInstance($type, $init)
         114                 :     {
         115              45 :         if (!self::$cachingInstance) {
         116               1 :             self::$cachingInstance = new IDS_Caching_File($type, $init);
         117               1 :         }
         118                 :
         119              45 :         return self::$cachingInstance;
         120                 :     }
         121                 :
         122                 :     /**
         123                 :      * Writes cache data into the file
         124                 :      *
         125                 :      * @param array $data the cache data
         126                 :      *
         127                 :      * @throws Exception if cache file couldn't be created
         128                 :      * @return object $this
         129                 :      */
         130                 :     public function setCache(array $data)
         131                 :     {
         132              44 :         if (!is_writable(preg_replace('/[\/][^\/]+\.[^\/]++$/', null,
         133              44 :             $this->path))) {
         134               0 :             throw new Exception('Temp directory ' .
         135               0 :             htmlspecialchars($this->path, ENT_QUOTES, 'UTF-8') .
         136               0 :             ' seems not writable');
         137                 :         }
         138                 :
         139              44 :         if ((!file_exists($this->path) || (time()-filectime($this->path)) >
         140              44 :             $this->config['expiration_time'])) {
         141               3 :             $handle = @fopen($this->path, 'w+');
         142                 :
         143               3 :             if (!$handle) {
         144               0 :                 throw new Exception("Cache file couldn't be created");
         145                 :             }
         146                 :
         147               3 :             fwrite($handle, serialize($data));
         148               3 :             fclose($handle);
         149               3 :         }
         150                 :
         151              44 :         return $this;
         152                 :     }
         153                 :
         154                 :     /**
         155                 :      * Returns the cached data
         156                 :      *
         157                 :      * Note that this method returns false if either type or file cache is
         158                 :      * not set
         159                 :      *
         160                 :      * @return mixed cache data or false
         161                 :      */
         162                 :     public function getCache()
         163                 :     {
         164                 :
         165                 :         // make sure filters are parsed again if cache expired
         166              43 :         if (file_exists($this->path) && (time()-filectime($this->path)) <
         167              43 :             $this->config['expiration_time']) {
         168              42 :             $data = unserialize($this->readFile($this->path));
         169              42 :             return $data;
         170                 :         }
         171                 :
         172               1 :         return false;
         173                 :     }
         174                 :
         175                 :     /**
         176                 :      * Read file and stripslashes if required
         177                 :      *
         178                 :      * @param string $filename Name of the file to read
         179                 :      * @return string
         180                 :      */
         181                 :     protected function readFile($filename)
         182                 :     {
         183              42 :         @set_magic_quotes_runtime(0);
         184              42 :         $content = file_get_contents($filename);
         185              42 :         if (function_exists('get_magic_quotes_runtime') and
         186              42 :             get_magic_quotes_runtime()) {
         187                 :
         188               0 :             $content = stripslashes($content);
         189               0 :         }
         190              42 :         return $content;
         191                 :     }
         192                 : }
         193                 :
         194                 : /*
         195                 :  * Local variables:
         196                 :  * tab-width: 4
         197                 :  * c-basic-offset: 4
         198                 :  * End:
         199                 :  */

Generated by PHPUnit 3.3.1 and Xdebug 2.1.0-dev at Mon Dec 22 13:57:42 CET 2008.