Open Journal Systems  3.3.0
bagit_manifest.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3 
46 {
47 
48  //{{{ properties
49 
56  var $pathPrefix;
57 
63  var $fileName;
64 
70  var $hashEncoding;
71 
77  var $fileEncoding;
78 
84  var $data;
85 
86  //}}}
87 
88  //{{{ Public Methods
89 
100  public function __construct(
101  $fileName, $pathPrefix='', $fileEncoding='UTF-8'
102  ) {
103  $this->fileName = $fileName;
104  $this->pathPrefix = $pathPrefix;
105  $this->fileEncoding = $fileEncoding;
106  $this->data = array();
107 
108  $this->hashEncoding = $this->_parseHashEncoding($fileName);
109 
110  if (file_exists($fileName)) {
111  $this->read();
112  } else if (is_dir(dirname($this->fileName))) {
113  touch($this->fileName);
114  }
115  }
116 
126  public function read($fileName=null)
127  {
128  $this->_resetFileName($fileName);
129 
130  $manifest = array();
131  $hashLen = ($this->hashEncoding == 'sha1') ? 40 : 32;
132  $lines = readLines($this->fileName, $this->fileEncoding);
133 
134  foreach ($lines as $line) {
135  $hash = trim(substr($line, 0, $hashLen));
136  $payload = trim(substr($line, $hashLen));
137 
138  if (strlen($payload) > 0) {
139  $manifest[$payload] = $hash;
140  }
141  }
142 
143  $this->data = $manifest;
144  return $manifest;
145  }
146 
152  public function clear()
153  {
154  $this->data = array();
155  file_put_contents($this->fileName, '');
156  }
157 
165  public function update($fileList)
166  {
167  $csums = array();
168 
169  foreach ($fileList as $file) {
170  if (file_exists($file)) {
171  $hash = $this->calculateHash($file);
172  $csums[$this->_makeRelative($file)] = $hash;
173  }
174  }
175 
176  $this->data = $csums;
177 
178  $this->write();
179 
180  return $csums;
181  }
182 
190  public function calculateHash($fileName)
191  {
192  return hash_file($this->hashEncoding, $fileName);
193  }
194 
204  public function write($fileName=null)
205  {
206  $this->_resetFileName($fileName);
207 
208  ksort($this->data);
209  $output = array();
210 
211  foreach ($this->data as $path => $hash) {
212  array_push($output, "$hash $path\n");
213  }
214 
215  writeFileText(
216  $this->fileName,
217  $this->fileEncoding,
218  implode('', $output)
219  );
220  }
221 
230  public function getHash($fileName)
231  {
232  if (array_key_exists($fileName, $this->data)) {
233  return $this->data[$fileName];
234  } else if (array_key_exists($this->_makeRelative($fileName), $this->data)) {
235  return $this->data[$this->_makeRelative($fileName)];
236  } else {
237  return null;
238  }
239  }
240 
246  public function getData()
247  {
248  return $this->data;
249  }
250 
256  public function getFileName()
257  {
258  return $this->fileName;
259  }
260 
267  public function getFileEncoding()
268  {
269  return $this->fileEncoding;
270  }
271 
285  public function setFileEncoding($fileEncoding)
286  {
287  $this->fileEncoding = $fileEncoding;
288  }
289 
295  public function getHashEncoding()
296  {
297  return $this->hashEncoding;
298  }
299 
309  public function setHashEncoding($hashEncoding)
310  {
311  $this->hashEncoding = $hashEncoding;
312 
313  $fileName = preg_replace(
314  '/-\w+\.txt$/',
315  "-$hashEncoding.txt",
316  $this->fileName
317  );
318 
319  if ($fileName != $this->fileName) {
320  rename($this->fileName, $fileName);
321  $this->fileName = $fileName;
322  }
323  }
324 
342  public function validate(&$errors)
343  {
344  $errLen = count($errors);
345 
346  // That the manifest file does exist;
347  if (! file_exists($this->fileName)) {
348  $basename = basename($this->fileName);
349  array_push(
350  $errors,
351  array($basename, "$basename does not exist.")
352  );
353  // There's no manifest file, so we might as well bail now.
354  return false;
355  }
356 
357  // That the files in the hash mapping do exist; and
358  // That the hashes in the mapping are correct.
359  foreach ($this->data as $fileName => $hash) {
360  $fullPath = $this->pathPrefix . $fileName;
361  if (! file_exists($fullPath)) {
362  array_push(
363  $errors,
364  array($fileName, 'Missing data file.')
365  );
366  } else if ($this->calculateHash($fullPath) != $hash) {
367  array_push(
368  $errors,
369  array($fileName, 'Checksum mismatch.')
370  );
371  }
372  }
373 
374  return ($errLen == count($errors));
375  }
376 
377  //}}}
378 
379  //{{{ Private Methods
380 
391  private function _parseHashEncoding($filename)
392  {
393  $matches = array();
394  if (preg_match('/-(sha1|md5)\.txt$/', $filename, $matches)) {
395  return $matches[1];
396  }
397  return null;
398  }
399 
411  private function _resetFileName($fileName=null)
412  {
413  if ($fileName === null) {
414  return $this->fileName;
415  } else {
416  $this->hashEncoding = $this->_parseHashEncoding($fileName);
417  $this->fileName = $fileName;
418  return $fileName;
419  }
420  }
421 
432  private function _makeRelative($filename)
433  {
434  $rel = substr($filename, strlen($this->pathPrefix));
435  if (! $rel) {
436  return '';
437  } else {
438  return $rel;
439  }
440  }
441  //}}}
442 
443 }
444 
445 
446 /*
447  * Local variables:
448  * tab-width: 4
449  * c-basic-offset: 4
450  * c-hanging-comment-ender-p: nil
451  * End:
452  */
453 
454 
455 ?>
BagItManifest\read
read($fileName=null)
Definition: bagit_manifest.php:141
BagItManifest\update
update($fileList)
Definition: bagit_manifest.php:180
BagItManifest\getHash
getHash($fileName)
Definition: bagit_manifest.php:245
BagItManifest\getFileEncoding
getFileEncoding()
Definition: bagit_manifest.php:282
BagItManifest\validate
validate(&$errors)
Definition: bagit_manifest.php:357
BagItManifest\__construct
__construct( $fileName, $pathPrefix='', $fileEncoding='UTF-8')
Definition: bagit_manifest.php:115
BagItManifest
Definition: bagit_manifest.php:45
BagItManifest\$fileEncoding
$fileEncoding
Definition: bagit_manifest.php:89
BagItManifest\$pathPrefix
$pathPrefix
Definition: bagit_manifest.php:59
BagItManifest\setFileEncoding
setFileEncoding($fileEncoding)
Definition: bagit_manifest.php:300
BagItManifest\getHashEncoding
getHashEncoding()
Definition: bagit_manifest.php:310
BagItManifest\setHashEncoding
setHashEncoding($hashEncoding)
Definition: bagit_manifest.php:324
BagItManifest\clear
clear()
Definition: bagit_manifest.php:167
BagItManifest\getFileName
getFileName()
Definition: bagit_manifest.php:271
BagItManifest\$data
$data
Definition: bagit_manifest.php:99
BagItManifest\write
write($fileName=null)
Definition: bagit_manifest.php:219
BagItManifest\getData
getData()
Definition: bagit_manifest.php:261
BagItManifest\$hashEncoding
$hashEncoding
Definition: bagit_manifest.php:79
BagItManifest\$fileName
$fileName
Definition: bagit_manifest.php:69
BagItManifest\calculateHash
calculateHash($fileName)
Definition: bagit_manifest.php:205