ZF-1964: Add file validation method to Zend_Pdf

Description

Add a method (static?) to Zend_Pdf to validate whether or not a file is a PDF, regardless of assumed mime type or extension.

Comments

Assigning to [~alexander] to initiate issue review.

This doesn't appear to have been fixed in 1.5.0. Please update if this is not correct.

Unassigning Zend_Pdf issues currently assigned to me pending decision on ZF 2.0 release timeframe and potential contribution of comprehensive changeset.

Alex, please evaluate and decide if/how we should act on this.

Hi, i made this method to be added to Zend_Pdf to check if a file is a valid PDF:

 
    /**
     * Reads a file in binay and checks (translated for better legible to hexadecimal) the first 4 values
     * If the file is a valid PDF, in hex should be "25 50 44 46" (in ASCII should be  "%PDF")
     *
     * @param string $file
     * @return boolean
     */

    public function isValid($file)
    {

        require_once 'Zend/Pdf/Exception.php';

        $handle = fopen($file, 'r');

        if ($handle) {

            foreach (unpack('C*', fgets($handle, 5)) as $dec)
            {
                $tmp = dechex($dec);
                $this->hex[] .= strtoupper(str_repeat('0', 2 - strlen($tmp)) . $tmp);
            }


        } else {
            throw new Zend_Pdf_Exception("Can not open '$file' file for read.");
        }


        if(implode($this->hex) == '25504446'){

            return true;

        } else {

            return false;
        }

    }

ups, it seems the jira dont parse php code on wiki :(

i saw this method is not needed because zf always check this on construct method (so forget about my method):

on Library/Zend/Parser.php (lines 393-397)

$pdfVersionComment = $this->_stringParser->readComment(); if (substr($pdfVersionComment, 0, 5) != '%PDF-') { require_once 'Zend/Pdf/Exception.php'; throw new Zend_Pdf_Exception('File is not a PDF.'); }