PageRenderTime 47ms CodeModel.GetById 42ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://github.com/zendframework/zf2
PHP | 128 lines | 68 code | 13 blank | 47 comment | 10 complexity | c0031cb53a6cc0e6c1b335d58af893ae 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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7 * @license   http://framework.zend.com/license/new-bsd New BSD License
  8 */
  9
 10namespace Zend\Validator\File;
 11
 12use Zend\Validator\Exception;
 13
 14/**
 15 * Validator for the md5 hash of given files
 16 */
 17class Md5 extends Hash
 18{
 19    /**
 20     * @const string Error constants
 21     */
 22    const DOES_NOT_MATCH = 'fileMd5DoesNotMatch';
 23    const NOT_DETECTED   = 'fileMd5NotDetected';
 24    const NOT_FOUND      = 'fileMd5NotFound';
 25
 26    /**
 27     * @var array Error message templates
 28     */
 29    protected $messageTemplates = array(
 30        self::DOES_NOT_MATCH => "File does not match the given md5 hashes",
 31        self::NOT_DETECTED   => "An md5 hash could not be evaluated for the given file",
 32        self::NOT_FOUND      => "File is not readable or does not exist",
 33    );
 34
 35    /**
 36     * Options for this validator
 37     *
 38     * @var string
 39     */
 40    protected $options = array(
 41        'algorithm' => 'md5',
 42        'hash'      => null,
 43    );
 44
 45    /**
 46     * Returns all set md5 hashes
 47     *
 48     * @return array
 49     */
 50    public function getMd5()
 51    {
 52        return $this->getHash();
 53    }
 54
 55    /**
 56     * Sets the md5 hash for one or multiple files
 57     *
 58     * @param  string|array $options
 59     * @return Hash Provides a fluent interface
 60     */
 61    public function setMd5($options)
 62    {
 63        $this->setHash($options);
 64        return $this;
 65    }
 66
 67    /**
 68     * Adds the md5 hash for one or multiple files
 69     *
 70     * @param  string|array $options
 71     * @return Hash Provides a fluent interface
 72     */
 73    public function addMd5($options)
 74    {
 75        $this->addHash($options);
 76        return $this;
 77    }
 78
 79    /**
 80     * Returns true if and only if the given file confirms the set hash
 81     *
 82     * @param  string|array $value Filename to check for hash
 83     * @param  array        $file  File data from \Zend\File\Transfer\Transfer (optional)
 84     * @return bool
 85     */
 86    public function isValid($value, $file = null)
 87    {
 88        if (is_string($value) && is_array($file)) {
 89            // Legacy Zend\Transfer API support
 90            $filename = $file['name'];
 91            $file     = $file['tmp_name'];
 92        } elseif (is_array($value)) {
 93            if (!isset($value['tmp_name']) || !isset($value['name'])) {
 94                throw new Exception\InvalidArgumentException(
 95                    'Value array must be in $_FILES format'
 96                );
 97            }
 98            $file     = $value['tmp_name'];
 99            $filename = $value['name'];
100        } else {
101            $file     = $value;
102            $filename = basename($file);
103        }
104        $this->setValue($filename);
105
106        // Is file readable ?
107        if (empty($file) || false === stream_resolve_include_path($file)) {
108            $this->error(self::NOT_FOUND);
109            return false;
110        }
111
112        $hashes = array_unique(array_keys($this->getHash()));
113        $filehash = hash_file('md5', $file);
114        if ($filehash === false) {
115            $this->error(self::NOT_DETECTED);
116            return false;
117        }
118
119        foreach ($hashes as $hash) {
120            if ($filehash === $hash) {
121                return true;
122            }
123        }
124
125        $this->error(self::DOES_NOT_MATCH);
126        return false;
127    }
128}