/** * Returns true if and only if the fileextension of $value is not included in the * set extension list * * @param string $value Real file to check for extension * @param array $file File data from \Zend\File\Transfer\Transfer * @return boolean */ public function isValid($value, $file = null) { if ($file === null) { $file = array('name' => basename($value)); } // Is file readable ? if (!Loader::isReadable($value)) { return $this->_throw($file, self::NOT_FOUND); } if ($file !== null) { $info['extension'] = substr($file['name'], strrpos($file['name'], '.') + 1); } else { $info = pathinfo($value); } $extensions = $this->getExtension(); if ($this->getCase() and !in_array($info['extension'], $extensions)) { return true; } else { if (!$this->getCase()) { $found = false; foreach ($extensions as $extension) { if (strtolower($extension) == strtolower($info['extension'])) { $found = true; } } if (!$found) { return true; } } } return $this->_throw($file, self::FALSE_EXTENSION); }
/** * Sets new encryption options * * @param string|array $options (Optional) Encryption options * @return \Zend\Filter\Encrypt\Encrypt */ public function setAdapter($options = null) { if (is_string($options)) { $adapter = $options; } else { if (isset($options['adapter'])) { $adapter = $options['adapter']; unset($options['adapter']); } else { $adapter = 'Mcrypt'; } } if (!is_array($options)) { $options = array(); } if (\Zend\Loader::isReadable('Zend/Filter/Encrypt/' . ucfirst($adapter) . '.php')) { $adapter = 'Zend\\Filter\\Encrypt\\' . ucfirst($adapter); } if (!class_exists($adapter)) { \Zend\Loader::loadClass($adapter); } $this->_adapter = new $adapter($options); if (!$this->_adapter instanceof Encrypt\EncryptionAlgorithm) { throw new Exception\InvalidArgumentException("Encoding adapter '" . $adapter . "' does not implement Zend\\Filter\\Encrypt\\EncryptionAlgorithm"); } return $this; }
/** * Sets new encryption options * * @param string|array $options (Optional) Encryption options * @return Encrypt */ public function setAdapter($options = null) { if (is_string($options)) { $adapter = $options; } else { if (isset($options['adapter'])) { $adapter = $options['adapter']; unset($options['adapter']); } else { $adapter = 'Mcrypt'; } } if (!is_array($options)) { $options = array(); } if (Loader::isReadable('Zend/Filter/Encrypt/' . ucfirst($adapter) . '.php')) { $adapter = 'Zend\\Filter\\Encrypt\\' . ucfirst($adapter); } if (!class_exists($adapter)) { throw new Exception\DomainException(sprintf('%s expects a valid registry class name; received "%s", which did not resolve', __METHOD__, $adapter)); } $this->_adapter = new $adapter($options); if (!$this->_adapter instanceof Encrypt\EncryptionAlgorithmInterface) { throw new Exception\InvalidArgumentException("Encoding adapter '" . $adapter . "' does not implement Zend\\Filter\\Encrypt\\EncryptionAlgorithmInterface"); } return $this; }
/** * Returns true if the mimetype of the file does not matche the given ones. Also parts * of mimetypes can be checked. If you give for example "image" all image * mime types will not be accepted like "image/gif", "image/jpeg" and so on. * * @param string $value Real file to check for mimetype * @param array $file File data from \Zend\File\Transfer\Transfer * @return boolean */ public function isValid($value, $file = null) { if ($file === null) { $file = array( 'type' => null, 'name' => $value ); } // Is file readable ? if (!\Zend\Loader::isReadable($value)) { return $this->_throw($file, self::NOT_READABLE); } $mimefile = $this->getMagicFile(); if (class_exists('finfo', false)) { $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME; if (!empty($mimefile)) { $mime = new \finfo($const, $mimefile); } else { $mime = new \finfo($const); } if (!empty($mime)) { $this->_type = $mime->file($value); } unset($mime); } if (empty($this->_type)) { if (function_exists('mime_content_type') && ini_get('mime_magic.magicfile')) { $this->_type = mime_content_type($value); } elseif ($this->_headerCheck) { $this->_type = $file['type']; } } if (empty($this->_type)) { return $this->_throw($file, self::NOT_DETECTED); } $mimetype = $this->getMimeType(true); if (in_array($this->_type, $mimetype)) { return $this->_throw($file, self::FALSE_TYPE); } $types = explode('/', $this->_type); $types = array_merge($types, explode('-', $this->_type)); foreach($mimetype as $mime) { if (in_array($mime, $types)) { return $this->_throw($file, self::FALSE_TYPE); } } return true; }
/** * Sets a new adapter * * @param string $adapter Adapter to use * @param boolean $direction OPTIONAL False means Download, true means upload * @param array $options OPTIONAL Options to set for this adapter * @throws \Zend\File\Transfer\Exception */ public function setAdapter($adapter, $direction = false, $options = array()) { if (\Zend\Loader::isReadable('Zend/File/Transfer/Adapter/' . ucfirst($adapter) . '.php')) { $adapter = 'Zend\\File\\Transfer\\Adapter\\' . ucfirst($adapter); } if (!class_exists($adapter)) { \Zend\Loader::loadClass($adapter); } $direction = (int) $direction; $this->_adapter[$direction] = new $adapter($options); if (!$this->_adapter[$direction] instanceof Adapter\AbstractAdapter) { throw new Transfer\Exception("Adapter " . $adapter . " does not extend Zend_File_Transfer_Adapter_Abstract"); } return $this; }
/** * Defined by Zend_Validate_Interface * * Returns true if and only if the counted words are at least min and * not bigger than max (when max is not null). * * @param string $value Filename to check for word count * @param array $file File data from \Zend\File\Transfer\Transfer * @return boolean */ public function isValid($value, $file = null) { // Is file readable ? if (!\Zend\Loader::isReadable($value)) { return $this->_throw($file, self::NOT_FOUND); } $content = file_get_contents($value); $this->_count = str_word_count($content); if ($this->_max !== null && $this->_count > $this->_max) { return $this->_throw($file, self::TOO_MUCH); } if ($this->_min !== null && $this->_count < $this->_min) { return $this->_throw($file, self::TOO_LESS); } return true; }
/** * Returns true if the mimetype of the file does not matche the given ones. Also parts * of mimetypes can be checked. If you give for example "image" all image * mime types will not be accepted like "image/gif", "image/jpeg" and so on. * * @param string $value Real file to check for mimetype * @param array $file File data from \Zend\File\Transfer\Transfer * @return boolean */ public function isValid($value, $file = null) { if ($file === null) { $file = array('type' => null, 'name' => $value); } // Is file readable ? if (!Loader::isReadable($value)) { return $this->createError($file, self::NOT_READABLE); } $mimefile = $this->getMagicFile(); if (class_exists('finfo', false)) { $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME; if (!$this->isMagicFileDisabled() && (!empty($mimefile) && empty($this->finfo))) { $this->finfo = finfo_open($const, $mimefile); } if (empty($this->finfo)) { $this->finfo = finfo_open($const); } $this->type = null; if (!empty($this->finfo)) { $this->type = finfo_file($this->finfo, $value); } } if (empty($this->type) && (function_exists('mime_content_type') && ini_get('mime_magic.magicfile'))) { $this->type = mime_content_type($value); } if (empty($this->type) && $this->getHeaderCheck()) { $this->type = $file['type']; } if (empty($this->type)) { return $this->createError($file, self::NOT_DETECTED); } $mimetype = $this->getMimeType(true); if (in_array($this->type, $mimetype)) { return $this->createError($file, self::FALSE_TYPE); } $types = explode('/', $this->type); $types = array_merge($types, explode('-', $this->type)); $types = array_merge($types, explode(';', $this->type)); foreach ($mimetype as $mime) { if (in_array($mime, $types)) { return $this->createError($file, self::FALSE_TYPE); } } return true; }
/** * Sets a new barcode adapter * * @param string|\Zend\Validator\Barcode\Adapter $adapter Barcode adapter to use * @param array $options Options for this adapter * @return Zend\Validator\Barcode * @throws \Zend\Validator\Exception */ public function setAdapter($adapter, $options = null) { if (is_string($adapter)) { $adapter = ucfirst(strtolower($adapter)); $adapter = 'Zend\\Validator\\Barcode\\' . $adapter; if (\Zend\Loader::isReadable('Zend/Validator/Barcode/' . $adapter . '.php')) { $adapter = 'Zend\\Validator\\Barcode\\' . $adapter; } if (!class_exists($adapter)) { throw new Exception\InvalidArgumentException('Barcode adapter matching "' . $adapter . '" not found'); } $this->options['adapter'] = new $adapter($options); } if (!$this->options['adapter'] instanceof Barcode\AdapterInterface) { throw new Exception\InvalidArgumentException("Adapter " . $adapter . " does not implement Zend\\Validate\\Barcode\\AdapterInterface"); } return $this; }
/** * Returns true if and only if the counted words are at least min and * not bigger than max (when max is not null). * * @param string $value Filename to check for word count * @param array $file File data from \Zend\File\Transfer\Transfer * @return boolean */ public function isValid($value, $file = null) { if ($file === null) { $file = array('name' => basename($value)); } // Is file readable ? if (!Loader::isReadable($value)) { return $this->_throw($file, self::NOT_FOUND); } $content = file_get_contents($value); $this->_count = str_word_count($content); if ($this->getMax() !== null && $this->_count > $this->getMax()) { return $this->_throw($file, self::TOO_MUCH); } if ($this->getMin() !== null && $this->_count < $this->getMin()) { return $this->_throw($file, self::TOO_LESS); } return true; }
/** * @group ZF-9263 * @group ZF-9166 * @group ZF-9306 */ public function testIsReadableShouldFailEarlyWhenProvidedInvalidWindowsAbsolutePath() { if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') { $this->markTestSkipped('Windows-only test'); } $path = 'C:/this/file/should/not/exist.php'; $this->assertFalse(Loader::isReadable($path)); }
/** * Returns true if and only if the disk usage of all files is at least min and * not bigger than max (when max is not null). * * @param string|array $value Real file to check for size * @param array $file File data from \Zend\File\Transfer\Transfer * @return boolean */ public function isValid($value, $file = null) { if (is_string($value)) { $value = array($value); } $min = $this->getMin(true); $max = $this->getMax(true); $size = $this->_getSize(); foreach ($value as $files) { // Is file readable ? if (!Loader::isReadable($files)) { $this->_throw($file, self::NOT_READABLE); continue; } if (!isset($this->_files[$files])) { $this->_files[$files] = $files; } else { // file already counted... do not count twice continue; } // limited to 2GB files $size += @filesize($files); $this->_size = $size; if ($max !== null && $max < $size) { if ($this->getByteString()) { $this->options['max'] = $this->_toByteString($max); $this->_size = $this->_toByteString($size); $this->_throw($file, self::TOO_BIG); $this->options['max'] = $max; $this->_size = $size; } else { $this->_throw($file, self::TOO_BIG); } } } // Check that aggregate files are >= minimum size if ($min !== null && $size < $min) { if ($this->getByteString()) { $this->options['min'] = $this->_toByteString($min); $this->_size = $this->_toByteString($size); $this->_throw($file, self::TOO_SMALL); $this->options['min'] = $min; $this->_size = $size; } else { $this->_throw($file, self::TOO_SMALL); } } if (count($this->getMessages()) > 0) { return false; } return true; }
/** * Bootstrap the front controller * * Resets the front controller, and then bootstraps it. * * If {@link $bootstrap} is a callback, executes it; if it is a file, it include's * it. When done, sets the test case request and response objects into the * front controller. * * @return void */ public final function bootstrap() { $this->reset(); if (null !== $this->bootstrap) { if ($this->bootstrap instanceof Application\Application) { $this->bootstrap->bootstrap(); $this->_frontController = $this->bootstrap->getBootstrap()->getResource('frontcontroller'); } elseif (is_callable($this->bootstrap)) { call_user_func($this->bootstrap); } elseif (is_string($this->bootstrap)) { if (\Zend\Loader::isReadable($this->bootstrap)) { include $this->bootstrap; } } } $this->frontController->setRequest($this->getRequest())->setResponse($this->getResponse()); }
/** * @param mixed $value * @param string $classBaseName * @param array $args OPTIONAL * @param mixed $namespaces OPTIONAL * @return boolean * @throws \Zend\Validator\Exception */ public static function execute($value, $classBaseName, array $args = array(), $namespaces = array()) { $namespaces = array_merge((array) $namespaces, self::$_defaultNamespaces, array('Zend_Validate')); $className = ucfirst($classBaseName); try { if (!class_exists($className, false)) { foreach ($namespaces as $namespace) { $class = $namespace . '_' . $className; $file = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php'; if (\Zend\Loader::isReadable($file)) { \Zend\Loader::loadClass($class); $className = $class; break; } } } $class = new \ReflectionClass($className); if ($class->implementsInterface('Zend\\Validator\\Validator')) { if ($class->hasMethod('__construct')) { $keys = array_keys($args); $numeric = false; foreach ($keys as $key) { if (is_numeric($key)) { $numeric = true; break; } } if ($numeric) { $object = $class->newInstanceArgs($args); } else { $object = $class->newInstance($args); } } else { $object = $class->newInstance(); } return $object->isValid($value); } } catch (Exception $ze) { // if there is an exception while validating throw it throw $ze; } catch (\Exception $e) { // fallthrough and continue for missing validation classes } throw new Exception("Validate class not found from basename '{$classBaseName}'"); }
/** * @group ZF-9100 */ public function testIsReadableShouldReturnTrueForAbsolutePaths() { set_include_path(__DIR__ . '../../../'); $path = __DIR__; $this->assertTrue(Loader::isReadable($path)); }
/** * Load a controller class * * Attempts to load the controller class file from * {@link getControllerDirectory()}. If the controller belongs to a * module, looks for the module prefix to the controller class. * * @param string $className * @return string Class name loaded * @throws \Zend\Controller\Dispatcher\Exception if class not loaded */ public function loadClass($className) { $finalClass = $className; if ($this->_defaultModule != $this->_curModule || $this->getParam('prefixDefaultModule')) { $finalClass = $this->formatClassName($this->_curModule, $className); } if (class_exists($finalClass, false)) { return $finalClass; } $dispatchDir = $this->getDispatchDirectory(); $loadFile = $dispatchDir . DIRECTORY_SEPARATOR . $this->classToFilename($className); if (\Zend\Loader::isReadable($loadFile)) { include_once $loadFile; } else { throw new Exception('Cannot load controller class "' . $className . '" from file "' . $loadFile . "'"); } if (!class_exists($finalClass, false)) { throw new Exception('Invalid controller class ("' . $finalClass . '")'); } return $finalClass; }
/** * Defined by Zend_Validate_Interface * * Returns true if and only if the given file confirms the set hash * * @param string $value Filename to check for hash * @param array $file File data from \Zend\File\Transfer\Transfer * @return boolean */ public function isValid($value, $file = null) { // Is file readable ? if (!\Zend\Loader::isReadable($value)) { return $this->_throw($file, self::NOT_FOUND); } $hashes = array_unique(array_keys($this->_hash)); $filehash = hash_file('crc32', $value); if ($filehash === false) { return $this->_throw($file, self::NOT_DETECTED); } foreach ($hashes as $hash) { if ($filehash === $hash) { return true; } } return $this->_throw($file, self::DOES_NOT_MATCH); }
/** * Returns the current adapter, instantiating it if necessary * * @return string */ public function getAdapter() { if ($this->_adapter instanceof Compress\CompressionAlgorithm) { return $this->_adapter; } $adapter = $this->_adapter; $options = $this->getAdapterOptions(); if (!class_exists($adapter)) { if (\Zend\Loader::isReadable('Zend/Filter/Compress/' . ucfirst($adapter) . '.php')) { $adapter = 'Zend\\Filter\\Compress\\' . ucfirst($adapter); } \Zend\Loader::loadClass($adapter); } $this->_adapter = new $adapter($options); if (!$this->_adapter instanceof Compress\CompressionAlgorithm) { throw new Exception\InvalidArgumentException("Compression adapter '" . $adapter . "' does not implement Zend\\Filter\\Compress\\CompressionAlgorithm"); } return $this->_adapter; }
/** * Load a plugin via the name provided * * @param string $name * @param bool $throwExceptions Whether or not to throw exceptions if the * class is not resolved * @return string|false Class name of loaded class; false if $throwExceptions * if false and no class found * @throws \Zend\Loader\Exception if class not found */ public function load($name, $throwExceptions = true) { $name = $this->_formatName($name); if ($this->isLoaded($name)) { return $this->getClassName($name); } if ($this->_useStaticRegistry) { $registry = self::$_staticPrefixToPaths[$this->_useStaticRegistry]; } else { $registry = $this->_prefixToPaths; } $registry = array_reverse($registry, true); $found = false; $classFile = str_replace('\\', DIRECTORY_SEPARATOR, $name) . '.php'; $incFile = self::getIncludeFileCache(); foreach ($registry as $prefix => $paths) { $className = $prefix . $name; if (class_exists($className, true)) { $found = true; break; } $paths = array_reverse($paths, true); foreach ($paths as $path) { $loadFile = $path . $classFile; if (\Zend\Loader::isReadable($loadFile)) { include_once $loadFile; if (class_exists($className, false)) { if (null !== $incFile) { self::_appendIncFile($loadFile); } $found = true; break 2; } } } } if (!$found) { if (!$throwExceptions) { return false; } $message = "Plugin by name '{$name}' was not found in the registry; used paths:"; foreach ($registry as $prefix => $paths) { $message .= "\n{$prefix}: " . implode(PATH_SEPARATOR, $paths); } throw new Exception($message); } if ($this->_useStaticRegistry) { self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name] = $className; } else { $this->_loadedPlugins[$name] = $className; } return $className; }
/** * Sets a new adapter * * @param array|\Zend\Config $options Options to use * @throws \Zend\Translator\Exception\InvalidArgumentException */ public function setAdapter($options = array()) { if ($options instanceof \Zend\Config\Config) { $options = $options->toArray(); } elseif (func_num_args() > 1) { $args = func_get_args(); $options = array(); $options['adapter'] = array_shift($args); if (!empty($args)) { $options['content'] = array_shift($args); } if (!empty($args)) { $options['locale'] = array_shift($args); } if (!empty($args)) { $opt = array_shift($args); $options = array_merge($opt, $options); } } else { if (!is_array($options)) { $options = array('adapter' => $options); } } if (empty($options['adapter'])) { throw new InvalidArgumentException("No adapter given"); } if (\Zend\Loader::isReadable('Zend/Translator/Adapter/' . ucfirst($options['adapter']) . '.php')) { $options['adapter'] = 'Zend\\Translator\\Adapter\\' . ucfirst($options['adapter']); } if (!class_exists($options['adapter'])) { throw new InvalidArgumentException("Adapter " . $options['adapter'] . " does not exist and cannot be loaded"); } if (array_key_exists('cache', $options)) { Adapter::setCache($options['cache']); } $adapter = $options['adapter']; unset($options['adapter']); $this->_adapter = new $adapter($options); if (!$this->_adapter instanceof Adapter) { throw new InvalidArgumentException("Adapter " . $adapter . " does not extend Zend\\Translator\\Adapter"); } }
/** * Get class path for class with vendor prefix * * @param string $class * @return false|string */ public function getPrefixedClassPath($class) { $segments = explode('_', $class); $prefixTopLevel = $this->getPrefix(); $prefix = ''; if (!empty($prefixTopLevel)) { $prefix = array_shift($segments); if ($prefix != $prefixTopLevel) { // wrong prefix? we're done return false; } } if (count($segments) < 2) { // assumes all resources have a component and class name, minimum return false; } $final = array_pop($segments); $component = $prefix; $lastMatch = false; do { $segment = array_shift($segments); $component .= empty($component) ? $segment : '_' . $segment; if (isset($this->_components[$component])) { $lastMatch = $component; } } while (count($segments)); if (!$lastMatch) { return false; } $final = substr($class, strlen($lastMatch) + 1); $path = $this->_components[$lastMatch]; $classPath = $path . '/' . str_replace('_', '/', $final) . '.php'; if (\Zend\Loader::isReadable($classPath)) { return $classPath; } return false; }
/** * Retrieve default controller class * * Determines whether the default controller to use lies within the * requested module, or if the global default should be used. * * By default, will only use the module default unless that controller does * not exist; if this is the case, it falls back to the default controller * in the default module. * * @param \Zend\Controller\Request\AbstractRequest $request * @return string */ public function getDefaultControllerClass(Request\AbstractRequest $request) { $controller = $this->getDefaultControllerName(); $default = $this->formatControllerName($controller); $request->setControllerName($controller)->setActionName(null); $module = $request->getModuleName(); $controllerDirs = $this->getControllerDirectory(); $this->_curModule = $this->_defaultModule; $this->_curDirectory = $controllerDirs[$this->_defaultModule]; if ($this->isValidModule($module)) { $found = false; if (class_exists($default, false)) { $found = true; } else { $moduleDir = $controllerDirs[$module]; $fileSpec = $moduleDir . DIRECTORY_SEPARATOR . $this->classToFilename($default); if (\Zend\Loader::isReadable($fileSpec)) { $found = true; $this->_curDirectory = $moduleDir; } } if ($found) { $request->setModuleName($module); $this->_curModule = $this->formatModuleName($module); } } else { $request->setModuleName($this->_defaultModule); } return $default; }
/** * Defined by Zend_Validate_Interface * * Returns true if and only if the imagesize of $value is at least min and * not bigger than max * * @param string $value Real file to check for image size * @param array $file File data from \Zend\File\Transfer\Transfer * @return boolean */ public function isValid($value, $file = null) { // Is file readable ? if (!\Zend\Loader::isReadable($value)) { return $this->_throw($file, self::NOT_READABLE); } $size = @getimagesize($value); $this->_setValue($file); if (empty($size) or $size[0] === 0 or $size[1] === 0) { return $this->_throw($file, self::NOT_DETECTED); } $this->_width = $size[0]; $this->_height = $size[1]; if ($this->_width < $this->_minwidth) { $this->_throw($file, self::WIDTH_TOO_SMALL); } if ($this->_maxwidth !== null and $this->_maxwidth < $this->_width) { $this->_throw($file, self::WIDTH_TOO_BIG); } if ($this->_height < $this->_minheight) { $this->_throw($file, self::HEIGHT_TOO_SMALL); } if ($this->_maxheight !== null and $this->_maxheight < $this->_height) { $this->_throw($file, self::HEIGHT_TOO_BIG); } if (count($this->_messages) > 0) { return false; } return true; }
/** * Returns true if and only if the filesize of $value is at least min and * not bigger than max (when max is not null). * * @param string $value Real file to check for size * @param array $file File data from \Zend\File\Transfer\Transfer * @return boolean */ public function isValid($value, $file = null) { // Is file readable ? if (!\Zend\Loader::isReadable($value)) { return $this->_throw($file, self::NOT_FOUND); } // limited to 4GB files $size = sprintf("%u", @filesize($value)); $this->_size = $size; // Check to see if it's smaller than min size $min = $this->getMin(true); $max = $this->getMax(true); if (($min !== null) && ($size < $min)) { if ($this->useByteString()) { $this->_min = $this->_toByteString($min); $this->_size = $this->_toByteString($size); $this->_throw($file, self::TOO_SMALL); $this->_min = $min; $this->_size = $size; } else { $this->_throw($file, self::TOO_SMALL); } } // Check to see if it's larger than max size if (($max !== null) && ($max < $size)) { if ($this->useByteString()) { $this->_max = $this->_toByteString($max); $this->_size = $this->_toByteString($size); $this->_throw($file, self::TOO_BIG); $this->_max = $max; $this->_size = $size; } else { $this->_throw($file, self::TOO_BIG); } } if (count($this->_messages) > 0) { return false; } return true; }
/** * Tests that the (script|helper|filter) path array is properly * initialized after instantiation. * * @param string $pathType one of "script", "helper", or "filter". * @param boolean $testReadability check if the path is readable? */ protected function _testDefaultPath($pathType, $testReadability = true) { $view = new View(); $reflector = $view->getAllPaths(); $paths = $this->_filterPath($reflector[$pathType]); // test default helper path $this->assertType('array', $paths); if ('script' == $pathType) { $this->assertEquals(0, count($paths)); } else { $this->assertEquals(1, count($paths)); $prefix = 'Zend\\View\\' . ucfirst($pathType) . '\\'; $this->assertTrue(array_key_exists($prefix, $paths)); if ($testReadability) { $path = current($paths[$prefix]); if (substr(PHP_OS, 0, 3) != 'WIN') { $this->assertTrue(Loader::isReadable($path)); } else { $this->assertTrue(is_dir($path)); } } } }
/** * Returns true if and only if the imagesize of $value is at least min and * not bigger than max * * @param string $value Real file to check for image size * @param array $file File data from \Zend\File\Transfer\Transfer * @return boolean */ public function isValid($value, $file = null) { if ($file === null) { $file = array('name' => basename($value)); } // Is file readable ? if (!Loader::isReadable($value)) { return $this->_throw($file, self::NOT_READABLE); } $size = @getimagesize($value); $this->setValue($file); if (empty($size) or $size[0] === 0 or $size[1] === 0) { return $this->_throw($file, self::NOT_DETECTED); } $this->_width = $size[0]; $this->_height = $size[1]; if ($this->_width < $this->getMinWidth()) { $this->_throw($file, self::WIDTH_TOO_SMALL); } if ($this->getMaxWidth() !== null and $this->getMaxWidth() < $this->_width) { $this->_throw($file, self::WIDTH_TOO_BIG); } if ($this->_height < $this->getMinHeight()) { $this->_throw($file, self::HEIGHT_TOO_SMALL); } if ($this->getMaxHeight() !== null and $this->getMaxHeight() < $this->_height) { $this->_throw($file, self::HEIGHT_TOO_BIG); } if (count($this->getMessages()) > 0) { return false; } return true; }
/** * Returns true if and only if the filesize of $value is at least min and * not bigger than max (when max is not null). * * @param string $value Real file to check for size * @param array $file File data from \Zend\File\Transfer\Transfer * @return boolean */ public function isValid($value, $file = null) { if ($file === null) { $file = array('name' => basename($value)); } // Is file readable ? if (!Loader::isReadable($value)) { return $this->_throw($file, self::NOT_FOUND); } // limited to 4GB files $size = sprintf("%u", @filesize($value)); $this->_size = $size; // Check to see if it's smaller than min size $min = $this->getMin(true); $max = $this->getMax(true); if ($min !== null && $size < $min) { if ($this->getByteString()) { $this->options['min'] = $this->_toByteString($min); $this->_size = $this->_toByteString($size); $this->_throw($file, self::TOO_SMALL); $this->options['min'] = $min; $this->_size = $size; } else { $this->_throw($file, self::TOO_SMALL); } } // Check to see if it's larger than max size if ($max !== null && $max < $size) { if ($this->getByteString()) { $this->options['max'] = $this->_toByteString($max); $this->_size = $this->_toByteString($size); $this->_throw($file, self::TOO_BIG); $this->options['max'] = $max; $this->_size = $size; } else { $this->_throw($file, self::TOO_BIG); } } if (count($this->getMessages()) > 0) { return false; } return true; }
/** * Returns true if and only if the given file confirms the set hash * * @param string $value Filename to check for hash * @param array $file File data from \Zend\File\Transfer\Transfer * @return boolean */ public function isValid($value, $file = null) { if ($file === null) { $file = array('name' => basename($value)); } // Is file readable ? if (!Loader::isReadable($value)) { return $this->_throw($file, self::NOT_FOUND); } $algos = array_unique(array_values($this->getHash())); $hashes = array_unique(array_keys($this->getHash())); foreach ($algos as $algorithm) { $filehash = hash_file($algorithm, $value); if ($filehash === false) { return $this->_throw($file, self::NOT_DETECTED); } foreach ($hashes as $hash) { if ($filehash === $hash) { return true; } } } return $this->_throw($file, self::DOES_NOT_MATCH); }
/** * Returns a value filtered through a specified filter class, without requiring separate * instantiation of the filter object. * * The first argument of this method is a data input value, that you would have filtered. * The second argument is a string, which corresponds to the basename of the filter class, * relative to the Zend_Filter namespace. This method automatically loads the class, * creates an instance, and applies the filter() method to the data input. You can also pass * an array of constructor arguments, if they are needed for the filter class. * * @param mixed $value * @param string $classBaseName * @param array $args OPTIONAL * @param array|string $namespaces OPTIONAL * @return mixed * @throws \Zend\Filter\Exception */ public static function execute($value, $classBaseName, array $args = array(), $namespaces = array()) { $namespaces = array_merge((array) $namespaces, self::$_defaultNamespaces, array('Zend\\Filter')); foreach ($namespaces as $namespace) { $className = $namespace . '\\' . ucfirst($classBaseName); if (!class_exists($className, false)) { try { $file = str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php'; if (\Zend\Loader::isReadable($file)) { \Zend\Loader::loadClass($className); } else { continue; } } catch (\Zend\Loader\Exception $ze) { continue; } } $class = new \ReflectionClass($className); if ($class->implementsInterface('Zend\\Filter\\Filter')) { if ($class->hasMethod('__construct')) { $object = $class->newInstanceArgs($args); } else { $object = $class->newInstance(); } return $object($value); } } throw new Exception("Filter class not found from basename '{$classBaseName}'"); }