/** * Defined by IfwPsn_Vendor_Zend_Validate_Interface * * 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 IfwPsn_Vendor_Zend_File_Transfer * @return boolean */ public function isValid($value, $file = null) { if ($file === null) { $file = array('type' => null, 'name' => $value); } // Is file readable ? require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Zend/Loader.php'; if (!IfwPsn_Zend_Loader::isReadable($value)) { return $this->_throw($file, self::NOT_READABLE); } $this->_type = $this->_detectMimeType($value); if (empty($this->_type) && $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; }
/** * Defined by IfwPsn_Vendor_Zend_Validate_Interface * * 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 IfwPsn_Vendor_Zend_File_Transfer * @return boolean */ public function isValid($value, $file = null) { // Is file readable ? require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Zend/Loader.php'; if (!IfwPsn_Zend_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->_case and !in_array($info['extension'], $extensions)) { return true; } else { if (!$this->_case) { $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 IfwPsn_Vendor_Zend_Filter_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 (IfwPsn_Zend_Loader::isReadable('IfwPsn/Vendor/Zend/Filter/Encrypt/' . ucfirst($adapter) . '.php')) { $adapter = 'IfwPsn_Vendor_Zend_Filter_Encrypt_' . ucfirst($adapter); } if (!class_exists($adapter)) { IfwPsn_Zend_Loader::loadClass($adapter); } $this->_adapter = new $adapter($options); if (!$this->_adapter instanceof IfwPsn_Vendor_Zend_Filter_Encrypt_Interface) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Filter/Exception.php'; throw new IfwPsn_Vendor_Zend_Filter_Exception("Encoding adapter '" . $adapter . "' does not implement IfwPsn_Vendor_Zend_Filter_Encrypt_Interface"); } return $this; }
/** * Defined by IfwPsn_Vendor_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 IfwPsn_Vendor_Zend_File_Transfer * @return boolean */ public function isValid($value, $file = null) { // Is file readable ? require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Zend/Loader.php'; if (!IfwPsn_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; }
/** * Defined by IfwPsn_Vendor_Zend_Validate_Interface * * 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 IfwPsn_Vendor_Zend_File_Transfer * @return boolean */ public function isValid($value, $file = null) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Zend/Loader.php'; 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 (!IfwPsn_Zend_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->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); } } } // Check that aggregate files are >= minimum size 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); } } if (count($this->_messages) > 0) { return false; } return true; }
/** * Defined by IfwPsn_Vendor_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 IfwPsn_Vendor_Zend_File_Transfer * @return boolean */ public function isValid($value, $file = null) { // Is file readable ? require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Zend/Loader.php'; if (!IfwPsn_Zend_Loader::isReadable($value)) { return $this->_throw($file, self::NOT_FOUND); } $hashes = array_unique(array_keys($this->_hash)); $filehash = hash_file('sha1', $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); }
/** * Sets a new barcode adapter * * @param string|IfwPsn_Vendor_Zend_Validate_Barcode $adapter Barcode adapter to use * @param array $options Options for this adapter * @return void * @throws IfwPsn_Vendor_Zend_Validate_Exception */ public function setAdapter($adapter, $options = null) { $adapter = ucfirst(strtolower($adapter)); require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Zend/Loader.php'; if (IfwPsn_Zend_Loader::isReadable('IfwPsn/Vendor/Zend/Validate/Barcode/' . $adapter . '.php')) { $adapter = 'IfwPsn_Vendor_Zend_Validate_Barcode_' . $adapter; } if (!class_exists($adapter)) { IfwPsn_Zend_Loader::loadClass($adapter); } $this->_adapter = new $adapter($options); if (!$this->_adapter instanceof IfwPsn_Vendor_Zend_Validate_Barcode_AdapterInterface) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Validate/Exception.php'; throw new IfwPsn_Vendor_Zend_Validate_Exception("Adapter " . $adapter . " does not implement IfwPsn_Vendor_Zend_Validate_Barcode_AdapterInterface"); } return $this; }
/** * @param mixed $value * @param string $classBaseName * @param array $args OPTIONAL * @param mixed $namespaces OPTIONAL * @return boolean * @throws IfwPsn_Vendor_Zend_Validate_Exception */ public static function is($value, $classBaseName, array $args = array(), $namespaces = array()) { $namespaces = array_merge((array) $namespaces, self::$_defaultNamespaces, array('IfwPsn_Vendor_Zend_Validate')); $className = ucfirst($classBaseName); try { if (!class_exists($className, false)) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Zend/Loader.php'; foreach ($namespaces as $namespace) { $class = $namespace . '_' . $className; $file = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php'; if (IfwPsn_Zend_Loader::isReadable($file)) { IfwPsn_Zend_Loader::loadClass($class); $className = $class; break; } } } $class = new ReflectionClass($className); if ($class->implementsInterface('IfwPsn_Vendor_Zend_Validate_Interface')) { 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 (IfwPsn_Vendor_Zend_Validate_Exception $ze) { // if there is an exception while validating throw it throw $ze; } catch (Exception $e) { // fallthrough and continue for missing validation classes } require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Validate/Exception.php'; throw new IfwPsn_Vendor_Zend_Validate_Exception("Validate class not found from basename '{$classBaseName}'"); }
/** * 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 IfwPsn_Vendor_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; if (false !== strpos($name, '\\')) { $classFile = str_replace('\\', DIRECTORY_SEPARATOR, $name) . '.php'; } else { $classFile = str_replace('_', DIRECTORY_SEPARATOR, $name) . '.php'; } $incFile = self::getIncludeFileCache(); foreach ($registry as $prefix => $paths) { $className = $prefix . $name; if (class_exists($className, false)) { $found = true; break; } $paths = array_reverse($paths, true); foreach ($paths as $path) { $loadFile = $path . $classFile; if (IfwPsn_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); } require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Loader/PluginLoader/Exception.php'; throw new IfwPsn_Vendor_Zend_Loader_PluginLoader_Exception($message); } if ($this->_useStaticRegistry) { self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name] = $className; } else { $this->_loadedPlugins[$name] = $className; } return $className; }
/** * 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 IfwPsn_Vendor_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 IfwPsn_Vendor_Zend_Filter_Exception */ public static function filterStatic($value, $classBaseName, array $args = array(), $namespaces = array()) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Zend/Loader.php'; $namespaces = array_merge((array) $namespaces, self::$_defaultNamespaces, array('IfwPsn_Vendor_Zend_Filter')); foreach ($namespaces as $namespace) { $className = $namespace . '_' . ucfirst($classBaseName); if (!class_exists($className, false)) { try { $file = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; if (IfwPsn_Zend_Loader::isReadable($file)) { IfwPsn_Zend_Loader::loadClass($className); } else { continue; } } catch (IfwPsn_Vendor_Zend_Exception $ze) { continue; } } $class = new ReflectionClass($className); if ($class->implementsInterface('IfwPsn_Vendor_Zend_Filter_Interface')) { if ($class->hasMethod('__construct')) { $object = $class->newInstanceArgs($args); } else { $object = $class->newInstance(); } return $object->filter($value); } } require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Filter/Exception.php'; throw new IfwPsn_Vendor_Zend_Filter_Exception("Filter class not found from basename '{$classBaseName}'"); }
/** * Helper method to calculate the correct class path * * @param string $class * @return False if not matched other wise the correct path */ public function getClassPath($class) { $segments = explode('_', $class); $namespaceTopLevel = $this->getNamespace(); $namespace = ''; if (!empty($namespaceTopLevel)) { $namespace = array(); $topLevelSegments = count(explode('_', $namespaceTopLevel)); for ($i = 0; $i < $topLevelSegments; $i++) { $namespace[] = array_shift($segments); } $namespace = implode('_', $namespace); if ($namespace != $namespaceTopLevel) { // 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 = $namespace; $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 (IfwPsn_Zend_Loader::isReadable($classPath)) { return $classPath; } return false; }
/** * Defined by IfwPsn_Vendor_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 IfwPsn_Vendor_Zend_File_Transfer * @return boolean */ public function isValid($value, $file = null) { // Is file readable ? require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Zend/Loader.php'; if (!IfwPsn_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; }
/** * Sets a new adapter * * @param array|IfwPsn_Vendor_Zend_Config $options Options to use * @throws IfwPsn_Vendor_Zend_Translate_Exception */ public function setAdapter($options = array()) { if ($options instanceof IfwPsn_Vendor_Zend_Config) { $options = $options->toArray(); } else { if (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 (IfwPsn_Zend_Loader::isReadable('IfwPsn/Vendor/Zend/Translate/Adapter/' . ucfirst($options['adapter']) . '.php')) { $options['adapter'] = 'IfwPsn_Vendor_Zend_Translate_Adapter_' . ucfirst($options['adapter']); } if (!class_exists($options['adapter'])) { IfwPsn_Zend_Loader::loadClass($options['adapter']); } if (array_key_exists('cache', $options)) { IfwPsn_Vendor_Zend_Translate_Adapter::setCache($options['cache']); } $adapter = $options['adapter']; unset($options['adapter']); $this->_adapter = new $adapter($options); if (!$this->_adapter instanceof IfwPsn_Vendor_Zend_Translate_Adapter) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Translate/Exception.php'; throw new IfwPsn_Vendor_Zend_Translate_Exception("Adapter " . $adapter . " does not extend IfwPsn_Vendor_Zend_Translate_Adapter"); } }
/** * Defined by IfwPsn_Vendor_Zend_Validate_Interface * * Returns true if and only if the fileextension of $value is included in the * set extension list * * @param string $value Real file to check for extension * @param array $file File data from IfwPsn_Vendor_Zend_File_Transfer * @return boolean */ public function isValid($value, $file = null) { // Is file readable ? require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Zend/Loader.php'; if (!IfwPsn_Zend_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); if (!array_key_exists('extension', $info)) { // From the manual at http://php.net/pathinfo: // "If the path does not have an extension, no extension element // will be returned (see second example below)." return false; } } $extensions = $this->getExtension(); if ($this->_case && in_array($info['extension'], $extensions)) { return true; } else { if (!$this->getCase()) { foreach ($extensions as $extension) { if (strtolower($extension) == strtolower($info['extension'])) { return true; } } } } return $this->_throw($file, self::FALSE_EXTENSION); }
/** * Returns the current adapter, instantiating it if necessary * * @return string */ public function getAdapter() { if ($this->_adapter instanceof IfwPsn_Vendor_Zend_Filter_Compress_CompressInterface) { return $this->_adapter; } $adapter = $this->_adapter; $options = $this->getAdapterOptions(); if (!class_exists($adapter)) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Zend/Loader.php'; if (IfwPsn_Zend_Loader::isReadable('IfwPsn/Vendor/Zend/Filter/Compress/' . ucfirst($adapter) . '.php')) { $adapter = 'IfwPsn_Vendor_Zend_Filter_Compress_' . ucfirst($adapter); } IfwPsn_Zend_Loader::loadClass($adapter); } $this->_adapter = new $adapter($options); if (!$this->_adapter instanceof IfwPsn_Vendor_Zend_Filter_Compress_CompressInterface) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Filter/Exception.php'; throw new IfwPsn_Vendor_Zend_Filter_Exception("Compression adapter '" . $adapter . "' does not implement IfwPsn_Vendor_Zend_Filter_Compress_CompressInterface"); } return $this->_adapter; }
/** * Defined by IfwPsn_Vendor_Zend_Validate_Interface * * 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 IfwPsn_Vendor_Zend_File_Transfer * @return boolean */ public function isValid($value, $file = null) { // Is file readable ? require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Zend/Loader.php'; if (!IfwPsn_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; }