From: <gem...@li...> - 2012-05-18 14:38:30
|
Revision: 691 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=691&view=rev Author: michieltcs Date: 2012-05-18 14:38:23 +0000 (Fri, 18 May 2012) Log Message: ----------- Move PDF version validation to MUtil_Validate_Pdf Modified Paths: -------------- trunk/library/classes/Gems/Default/SurveyMaintenanceAction.php Added Paths: ----------- trunk/library/classes/MUtil/Validate/Pdf.php Modified: trunk/library/classes/Gems/Default/SurveyMaintenanceAction.php =================================================================== --- trunk/library/classes/Gems/Default/SurveyMaintenanceAction.php 2012-05-18 14:35:15 UTC (rev 690) +++ trunk/library/classes/Gems/Default/SurveyMaintenanceAction.php 2012-05-18 14:38:23 UTC (rev 691) @@ -130,7 +130,8 @@ 'destination', $this->loader->getPdf()->getUploadDir('survey_pdfs'), 'extension', 'pdf', 'filename', $data['gsu_id_survey'], - 'required', false); + 'required', false) + ->addValidator(new MUtil_Validate_Pdf()); $bridge->addExhibitor( 'track_count', 'label', $this->_('Usage'), 'value', $data['track_count']); @@ -242,21 +243,12 @@ } // Set the value of the field in the database. - $data['gsu_survey_pdf'] = null; $new_name = $data['gsu_id_survey'] . '.pdf'; - $pdfSource = $form->new_pdf->getDestination() . DIRECTORY_SEPARATOR . $new_name; - if (file_exists($pdfSource)) { - $objFactory = Zend_Pdf_ElementFactory::createFactory(1); - $parser = new Zend_Pdf_Parser($pdfSource, $objFactory, true); - $version = $parser->getPDFVersion(); - - if (version_compare($version, '1.4', '>')) { - $this->addMessage(sprintf($this->_('Unsupported PDF version %s - only versions 1.0 - 1.4 are supported.'), $version)); - return false; - } - + if (file_exists($form->new_pdf->getDestination() . DIRECTORY_SEPARATOR . $new_name)) { $data['gsu_survey_pdf'] = $new_name; + } else { + $data['gsu_survey_pdf'] = null; } $data['gtr_track_class'] = 'SingleSurveyEngine'; Added: trunk/library/classes/MUtil/Validate/Pdf.php =================================================================== --- trunk/library/classes/MUtil/Validate/Pdf.php (rev 0) +++ trunk/library/classes/MUtil/Validate/Pdf.php 2012-05-18 14:38:23 UTC (rev 691) @@ -0,0 +1,80 @@ +<?php + + +/** + * Copyright (c) 2012, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * @author Michiel Rook <mi...@to...> + * @version $Id$ + * @package MUtil + * @subpackage Validate + */ + +/** + * + * @author Michiel Rook <mi...@to...> + * @package MUtil + * @subpackage Validate + */ +class MUtil_Validate_Pdf extends Zend_Validate_Abstract +{ + /** + * Error constants + */ + const ERROR_INVALID_VERSION = 'invalidVersion'; + + /** + * @var array Message templates + */ + protected $_messageTemplates = array( + self::ERROR_INVALID_VERSION => 'Unsupported PDF version %value% - only versions 1.0 - 1.4 are supported.' + ); + + /** + * Returns true if and only if $value meets the validation requirements + * + * If $value fails validation, then this method returns false, and + * getMessages() will return an array of messages that explain why the + * validation failed. + * + * @param mixed $value + * @return boolean + * @throws Zend_Valid_Exception If validation of $value is impossible + */ + public function isValid($value, $context = array()) + { + $objFactory = Zend_Pdf_ElementFactory::createFactory(1); + $parser = new Zend_Pdf_Parser($value, $objFactory, true); + $version = $parser->getPDFVersion(); + + if (version_compare($version, '1.4', '>')) { + $this->_error(self::ERROR_INVALID_VERSION, $version); + return false; + } + + return true; + } +} Property changes on: trunk/library/classes/MUtil/Validate/Pdf.php ___________________________________________________________________ Added: svn:keywords + Id Rev Revision Date Author Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |