Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Johannes Meyer
committed
Sep 2, 2014
1 parent
aedc8cc
commit 338f549
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
library/Icinga/Web/Form/Validator/ReadablePathValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
// {{{ICINGA_LICENSE_HEADER}}} | ||
// {{{ICINGA_LICENSE_HEADER}}} | ||
|
||
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 | ||
{ | ||
/** | ||
* The messages to write on different error states | ||
* | ||
* @var array | ||
* | ||
* @see Zend_Validate_Abstract::$_messageTemplates‚ | ||
*/ | ||
protected $_messageTemplates; | ||
|
||
/** | ||
* Initialize this validator | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->_messageTemplates = array( | ||
'NOT_READABLE' => t('Path is not readable'), | ||
'DOES_NOT_EXIST' => t('Path does not exist') | ||
); | ||
} | ||
|
||
/** | ||
* 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)) { | ||
$this->_error('DOES_NOT_EXIST'); | ||
return false; | ||
} | ||
|
||
if (false === is_readable($value)) { | ||
$this->_error('NOT_READABLE'); | ||
} | ||
|
||
return true; | ||
} | ||
} |