PageRenderTime 24ms CodeModel.GetById 20ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/alexandretaz/maniac_divers
PHP | 123 lines | 65 code | 13 blank | 45 comment | 8 complexity | bf8e7756f9a0fda3907c5aea4e72bf81 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-2013 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   => "A 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     * @return bool
 84     */
 85    public function isValid($value)
 86    {
 87        if (is_array($value)) {
 88            if (!isset($value['tmp_name']) || !isset($value['name'])) {
 89                throw new Exception\InvalidArgumentException(
 90                    'Value array must be in $_FILES format'
 91                );
 92            }
 93            $file     = $value['tmp_name'];
 94            $filename = $value['name'];
 95        } else {
 96            $file     = $value;
 97            $filename = basename($file);
 98        }
 99        $this->setValue($filename);
100
101        // Is file readable ?
102        if (false === stream_resolve_include_path($file)) {
103            $this->error(self::NOT_FOUND);
104            return false;
105        }
106
107        $hashes = array_unique(array_keys($this->getHash()));
108        $filehash = hash_file('md5', $file);
109        if ($filehash === false) {
110            $this->error(self::NOT_DETECTED);
111            return false;
112        }
113
114        foreach ($hashes as $hash) {
115            if ($filehash === $hash) {
116                return true;
117            }
118        }
119
120        $this->error(self::DOES_NOT_MATCH);
121        return false;
122    }
123}