1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbere\Model\Hacked; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Base class for the different ways that files can be hashed. |
7
|
|
|
* |
8
|
|
|
* Class HackedFileHasher |
9
|
|
|
* @package Cerbere\Model\Hacked |
10
|
|
|
*/ |
11
|
|
|
abstract class HackedFileHasher |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Returns a hash of the given filename. |
15
|
|
|
* Ignores file line endings |
16
|
|
|
* |
17
|
|
|
* @param string $filename |
18
|
|
|
* @return string|false |
19
|
|
|
*/ |
20
|
|
|
public function hash($filename) |
21
|
|
|
{ |
22
|
|
|
if (file_exists($filename)) { |
23
|
|
|
if ($hash = $this->getCache($filename)) { |
24
|
|
|
return $hash; |
25
|
|
|
} else { |
26
|
|
|
$hash = $this->performHash($filename); |
27
|
|
|
$this->setCache($filename, $hash); |
28
|
|
|
|
29
|
|
|
return $hash; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $filename |
38
|
|
|
* @param string $hash |
39
|
|
|
*/ |
40
|
|
|
public function setCache($filename, $hash) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
//cache_set($this->getCacheKey($filename), $hash, HACKED_CACHE_TABLE, strtotime('+7 days')); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $filename |
47
|
|
|
* @return string|false |
48
|
|
|
*/ |
49
|
|
|
public function getCache($filename) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$cache = false; //cache_get($this->getCacheKey($filename), HACKED_CACHE_TABLE); |
|
|
|
|
52
|
|
|
|
53
|
|
|
if (!empty($cache->data)) { |
54
|
|
|
return $cache->data; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $filename |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getCacheKey($filename) |
65
|
|
|
{ |
66
|
|
|
$key = array( |
67
|
|
|
'filename' => $filename, |
68
|
|
|
'mtime' => filemtime($filename), |
69
|
|
|
'class_name' => get_class($this), |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
return sha1(serialize($key)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Compute and return the hash of the given file. |
77
|
|
|
* |
78
|
|
|
* @param string $filename |
79
|
|
|
* A fully-qualified filename to hash. |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
* The computed hash of the given file. |
83
|
|
|
*/ |
84
|
|
|
abstract public function performHash($filename); |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Compute and return the lines of the given file. |
88
|
|
|
* |
89
|
|
|
* @param string $filename |
90
|
|
|
* A fully-qualified filename to return. |
91
|
|
|
* |
92
|
|
|
* @return array|false |
93
|
|
|
* The lines of the given filename or FALSE on failure. |
94
|
|
|
*/ |
95
|
|
|
abstract public function fetchLines($filename); |
96
|
|
|
} |
97
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.