PageRenderTime 41ms CodeModel.GetById 35ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/Validator/File/Md5.php

https://bitbucket.org/pcelta/zf2
PHP | 113 lines | 51 code | 12 blank | 50 comment | 5 complexity | e0169f4cb3f1bd59c0e586a23a48c21a MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework (http://framework.zend.com/)
  4 *
  5 * @link      http://github.com/zendframework/zf2 for the canonical source repository
  6 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7 * @license   http://framework.zend.com/license/new-bsd New BSD License
  8 * @package   Zend_Validator
  9 */
 10
 11namespace Zend\Validator\File;
 12
 13/**
 14 * Validator for the md5 hash of given files
 15 *
 16 * @category  Zend
 17 * @package   Zend_Validator
 18 */
 19class Md5 extends Hash
 20{
 21    /**
 22     * @const string Error constants
 23     */
 24    const DOES_NOT_MATCH = 'fileMd5DoesNotMatch';
 25    const NOT_DETECTED   = 'fileMd5NotDetected';
 26    const NOT_FOUND      = 'fileMd5NotFound';
 27
 28    /**
 29     * @var array Error message templates
 30     */
 31    protected $messageTemplates = array(
 32        self::DOES_NOT_MATCH => "File '%value%' does not match the given md5 hashes",
 33        self::NOT_DETECTED   => "A md5 hash could not be evaluated for the given file",
 34        self::NOT_FOUND      => "File '%value%' is not readable or does not exist",
 35    );
 36
 37    /**
 38     * Options for this validator
 39     *
 40     * @var string
 41     */
 42    protected $options = array(
 43        'algorithm' => 'md5',
 44        'hash'      => null,
 45    );
 46
 47    /**
 48     * Returns all set md5 hashes
 49     *
 50     * @return array
 51     */
 52    public function getMd5()
 53    {
 54        return $this->getHash();
 55    }
 56
 57    /**
 58     * Sets the md5 hash for one or multiple files
 59     *
 60     * @param  string|array $options
 61     * @return Hash Provides a fluent interface
 62     */
 63    public function setMd5($options)
 64    {
 65        $this->setHash($options);
 66        return $this;
 67    }
 68
 69    /**
 70     * Adds the md5 hash for one or multiple files
 71     *
 72     * @param  string|array $options
 73     * @return Hash Provides a fluent interface
 74     */
 75    public function addMd5($options)
 76    {
 77        $this->addHash($options);
 78        return $this;
 79    }
 80
 81    /**
 82     * Returns true if and only if the given file confirms the set hash
 83     *
 84     * @param  string $value Filename to check for hash
 85     * @param  array  $file  File data from \Zend\File\Transfer\Transfer
 86     * @return boolean
 87     */
 88    public function isValid($value, $file = null)
 89    {
 90        if ($file === null) {
 91            $file = array('name' => basename($value));
 92        }
 93
 94        // Is file readable ?
 95        if (false === stream_resolve_include_path($value)) {
 96            return $this->throwError($file, self::NOT_FOUND);
 97        }
 98
 99        $hashes = array_unique(array_keys($this->getHash()));
100        $filehash = hash_file('md5', $value);
101        if ($filehash === false) {
102            return $this->throwError($file, self::NOT_DETECTED);
103        }
104
105        foreach ($hashes as $hash) {
106            if ($filehash === $hash) {
107                return true;
108            }
109        }
110
111        return $this->throwError($file, self::DOES_NOT_MATCH);
112    }
113}