|
5 | 5 | use Silex\Application; |
6 | 6 | use Symfony\Component\HttpFoundation\Request; |
7 | 7 |
|
| 8 | +/** |
| 9 | + * A class representing an LDP Resource |
| 10 | + */ |
8 | 11 | abstract class Resource |
9 | 12 | { |
10 | 13 | const LDP_NS = "http://www.w3.org/ns/ldp#"; |
@@ -42,60 +45,59 @@ abstract public function respond(Application $app, Request $request, array $opti |
42 | 45 | * @return string |
43 | 46 | * The SHA1 checksum of the file |
44 | 47 | */ |
45 | | - public function sha1() |
| 48 | + public function sha1($content = null) |
46 | 49 | { |
47 | | - if (is_file($this->path)) { |
| 50 | + if ($content !== null) { |
| 51 | + return sha1($content); |
| 52 | + } elseif (is_file($this->path)) { |
48 | 53 | return sha1_file($this->path); |
49 | 54 | } |
50 | 55 | return null; |
51 | 56 | } |
52 | 57 |
|
53 | 58 | /** |
54 | | - * Apply a want-digest header, if applicable |
| 59 | + * Compute the MD5 checksum of a file |
| 60 | + * @return string |
| 61 | + * The MD5 checksum of the file |
| 62 | + */ |
| 63 | + public function md5($content = null) |
| 64 | + { |
| 65 | + if ($content !== null) { |
| 66 | + return md5($content); |
| 67 | + } elseif (is_file($this->path)) { |
| 68 | + return md5_file($this->path); |
| 69 | + } |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Given a want-digest header, return the applicable algorithm |
55 | 75 | * |
56 | 76 | * @param $wantDigestHeader |
57 | 77 | * The Want-Digest header |
58 | 78 | * @return string |
59 | | - * The instance digest, if relevant |
| 79 | + * The algorithm name, if any |
60 | 80 | */ |
61 | | - protected function wantDigest($wantDigestHeader) |
| 81 | + protected function getDigestAlgorithm($wantDigestHeader) |
62 | 82 | { |
| 83 | + $validAlgorithms = ["md5", "sha1"]; |
| 84 | + $bestAlgorithm = null; |
63 | 85 | if ($wantDigestHeader) { |
64 | 86 | $maxQVal = 0.0; |
65 | | - $bestAlgorithm = null; |
66 | 87 | foreach (explode(",", $wantDigestHeader) as $algorithm) { |
67 | 88 | $parts = explode(";", $algorithm); |
68 | 89 | $qVal = 1.0; |
69 | 90 | if (count($parts) > 1 && strpos($parts[1], "q=") === 0) { |
70 | 91 | $qVal = floatval(trim(substr($parts[1], 2))); |
71 | 92 | } |
72 | 93 | $alg = strtolower(trim($parts[0])); |
73 | | - if ($qVal > $maxQVal && in_array($alg, ["md5", "sha1"])) { |
| 94 | + if ($qVal > $maxQVal && in_array($alg, $validAlgorithms)) { |
74 | 95 | $bestAlgorithm = $alg; |
75 | 96 | $maxQVal = $qVal; |
76 | 97 | } |
77 | 98 | } |
78 | | - switch($bestAlgorithm) { |
79 | | - case "md5": |
80 | | - return "md5=" . $this->md5(); |
81 | | - case "sha1": |
82 | | - return "sha1=" . $this->sha1(); |
83 | | - } |
84 | 99 | } |
85 | | - return null; |
86 | | - } |
87 | | - |
88 | | - /** |
89 | | - * Compute the MD5 checksum of a file |
90 | | - * @return string |
91 | | - * The MD5 checksum of the file |
92 | | - */ |
93 | | - public function md5() |
94 | | - { |
95 | | - if (is_file($this->path)) { |
96 | | - return md5_file($this->path); |
97 | | - } |
98 | | - return null; |
| 100 | + return $bestAlgorithm; |
99 | 101 | } |
100 | 102 |
|
101 | 103 | /** |
|
0 commit comments