2014-09-02 14:42:24 +02:00
|
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-09-02 14:42:24 +02:00
|
|
|
|
|
|
|
|
|
namespace Icinga\Web\Form\Validator;
|
|
|
|
|
|
|
|
|
|
use Zend_Validate_Abstract;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validator that interprets the value as a filepath and checks if it's readable
|
|
|
|
|
*
|
|
|
|
|
* This validator should be preferred due to Zend_Validate_File_Exists is
|
|
|
|
|
* getting confused if there is another element in the form called `name'.
|
|
|
|
|
*/
|
|
|
|
|
class ReadablePathValidator extends Zend_Validate_Abstract
|
|
|
|
|
{
|
2015-02-12 09:05:40 +01:00
|
|
|
|
const NOT_READABLE = 'notReadable';
|
|
|
|
|
const DOES_NOT_EXIST = 'doesNotExist';
|
|
|
|
|
|
2014-09-02 14:42:24 +02:00
|
|
|
|
/**
|
|
|
|
|
* The messages to write on different error states
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*
|
|
|
|
|
* @see Zend_Validate_Abstract::$_messageTemplates‚
|
|
|
|
|
*/
|
2015-02-12 09:05:40 +01:00
|
|
|
|
protected $_messageTemplates = array(
|
|
|
|
|
self::NOT_READABLE => 'Path is not readable',
|
|
|
|
|
self::DOES_NOT_EXIST => 'Path does not exist'
|
|
|
|
|
);
|
2014-09-02 14:42:24 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check whether the given value is a readable filepath
|
|
|
|
|
*
|
|
|
|
|
* @param string $value The value submitted in the form
|
|
|
|
|
* @param mixed $context The context of the form
|
|
|
|
|
*
|
|
|
|
|
* @return bool Whether the value was successfully validated
|
|
|
|
|
*/
|
|
|
|
|
public function isValid($value, $context = null)
|
|
|
|
|
{
|
|
|
|
|
if (false === file_exists($value)) {
|
2015-02-12 09:05:40 +01:00
|
|
|
|
$this->_error(self::DOES_NOT_EXIST);
|
2014-09-02 14:42:24 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (false === is_readable($value)) {
|
2015-02-12 09:05:40 +01:00
|
|
|
|
$this->_error(self::NOT_READABLE);
|
|
|
|
|
return false;
|
2014-09-02 14:42:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|