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