diff --git a/libs/Zend/Auth/Adapter/DbTable.php b/libs/Zend/Auth/Adapter/DbTable.php index 30bbd2478d144dcb0f7afd055ad8e55709dc505a..efad58f29bf804b33def63c99707f093a193e5e9 100644 --- a/libs/Zend/Auth/Adapter/DbTable.php +++ b/libs/Zend/Auth/Adapter/DbTable.php @@ -17,7 +17,7 @@ * @subpackage Adapter * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id: DbTable.php 20096 2010-01-06 02:05:09Z bkarwin $ + * @version $Id: DbTable.php 22458 2010-06-18 22:47:46Z ralph $ */ @@ -118,17 +118,17 @@ class Zend_Auth_Adapter_DbTable implements Zend_Auth_Adapter_Interface /** * __construct() - Sets configuration options * - * @param Zend_Db_Adapter_Abstract $zendDb + * @param Zend_Db_Adapter_Abstract $zendDb If null, default database adapter assumed * @param string $tableName * @param string $identityColumn * @param string $credentialColumn * @param string $credentialTreatment * @return void */ - public function __construct(Zend_Db_Adapter_Abstract $zendDb, $tableName = null, $identityColumn = null, + public function __construct(Zend_Db_Adapter_Abstract $zendDb = null, $tableName = null, $identityColumn = null, $credentialColumn = null, $credentialTreatment = null) { - $this->_zendDb = $zendDb; + $this->_setDbAdapter($zendDb); if (null !== $tableName) { $this->setTableName($tableName); @@ -147,6 +147,32 @@ class Zend_Auth_Adapter_DbTable implements Zend_Auth_Adapter_Interface } } + /** + * _setDbAdapter() - set the database adapter to be used for quering + * + * @param Zend_Db_Adapter_Abstract + * @throws Zend_Auth_Adapter_Exception + * @return Zend_Auth_Adapter_DbTable + */ + protected function _setDbAdapter(Zend_Db_Adapter_Abstract $zendDb = null) + { + $this->_zendDb = $zendDb; + + /** + * If no adapter is specified, fetch default database adapter. + */ + if(null === $this->_zendDb) { + // require_once 'Zend/Db/Table/Abstract.php'; + $this->_zendDb = Zend_Db_Table_Abstract::getDefaultAdapter(); + if (null === $this->_zendDb) { + // require_once 'Zend/Auth/Adapter/Exception.php'; + throw new Zend_Auth_Adapter_Exception('No database adapter present'); + } + } + + return $this; + } + /** * setTableName() - set the table name to be used in the select query * @@ -305,7 +331,7 @@ class Zend_Auth_Adapter_DbTable implements Zend_Auth_Adapter_Interface $dbSelect = $this->_authenticateCreateSelect(); $resultIdentities = $this->_authenticateQuerySelect($dbSelect); - if ( ($authResult = $this->_authenticateValidateResultset($resultIdentities)) instanceof Zend_Auth_Result) { + if ( ($authResult = $this->_authenticateValidateResultSet($resultIdentities)) instanceof Zend_Auth_Result) { return $authResult; } diff --git a/libs/Zend/Feed/Rss.php b/libs/Zend/Feed/Rss.php index 19eaa8ec19b2303ef84fae86fb87418b434a7008..cfed8cc059d329902d40078ce51c8e72686a59e0 100644 --- a/libs/Zend/Feed/Rss.php +++ b/libs/Zend/Feed/Rss.php @@ -282,12 +282,11 @@ class Zend_Feed_Rss extends Zend_Feed_Abstract $author = ''; $email = ''; if (isset($array->itunes->owner)) { - $itunesOwner = $array->itunes->owner; - if (isset($itunesOwner['name'])) { - $author = $itunesOwner['name']; + if (isset($array->itunes->owner['name'])) { + $author = $array->itunes->owner['name']; } - if (isset($itunesOwner['email'])) { - $email = $itunesOwner['email']; + if (isset($array->itunes->owner['email'])) { + $email = $array->itunes->owner['email']; } } if (empty($author) && isset($array->author)) { diff --git a/libs/Zend/Filter.php b/libs/Zend/Filter.php new file mode 100644 index 0000000000000000000000000000000000000000..585f07fb6dfe36599f29c5286aedebd04a6590f6 --- /dev/null +++ b/libs/Zend/Filter.php @@ -0,0 +1,239 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Filter.php 21097 2010-02-19 20:11:34Z thomas $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter implements Zend_Filter_Interface +{ + + const CHAIN_APPEND = 'append'; + const CHAIN_PREPEND = 'prepend'; + + /** + * Filter chain + * + * @var array + */ + protected $_filters = array(); + + /** + * Default Namespaces + * + * @var array + */ + protected static $_defaultNamespaces = array(); + + /** + * Adds a filter to the chain + * + * @param Zend_Filter_Interface $filter + * @param string $placement + * @return Zend_Filter Provides a fluent interface + */ + public function addFilter(Zend_Filter_Interface $filter, $placement = self::CHAIN_APPEND) + { + if ($placement == self::CHAIN_PREPEND) { + array_unshift($this->_filters, $filter); + } else { + $this->_filters[] = $filter; + } + return $this; + } + + /** + * Add a filter to the end of the chain + * + * @param Zend_Filter_Interface $filter + * @return Zend_Filter Provides a fluent interface + */ + public function appendFilter(Zend_Filter_Interface $filter) + { + return $this->addFilter($filter, self::CHAIN_APPEND); + } + + /** + * Add a filter to the start of the chain + * + * @param Zend_Filter_Interface $filter + * @return Zend_Filter Provides a fluent interface + */ + public function prependFilter(Zend_Filter_Interface $filter) + { + return $this->addFilter($filter, self::CHAIN_PREPEND); + } + + /** + * Get all the filters + * + * @return array + */ + public function getFilters() + { + return $this->_filters; + } + + /** + * Returns $value filtered through each filter in the chain + * + * Filters are run in the order in which they were added to the chain (FIFO) + * + * @param mixed $value + * @return mixed + */ + public function filter($value) + { + $valueFiltered = $value; + foreach ($this->_filters as $filter) { + $valueFiltered = $filter->filter($valueFiltered); + } + return $valueFiltered; + } + + /** + * Returns the set default namespaces + * + * @return array + */ + public static function getDefaultNamespaces() + { + return self::$_defaultNamespaces; + } + + /** + * Sets new default namespaces + * + * @param array|string $namespace + * @return null + */ + public static function setDefaultNamespaces($namespace) + { + if (!is_array($namespace)) { + $namespace = array((string) $namespace); + } + + self::$_defaultNamespaces = $namespace; + } + + /** + * Adds a new default namespace + * + * @param array|string $namespace + * @return null + */ + public static function addDefaultNamespaces($namespace) + { + if (!is_array($namespace)) { + $namespace = array((string) $namespace); + } + + self::$_defaultNamespaces = array_unique(array_merge(self::$_defaultNamespaces, $namespace)); + } + + /** + * Returns true when defaultNamespaces are set + * + * @return boolean + */ + public static function hasDefaultNamespaces() + { + return (!empty(self::$_defaultNamespaces)); + } + + /** + * @deprecated + * @see Zend_Filter::filterStatic() + * + * @param mixed $value + * @param string $classBaseName + * @param array $args OPTIONAL + * @param array|string $namespaces OPTIONAL + * @return mixed + * @throws Zend_Filter_Exception + */ + public static function get($value, $classBaseName, array $args = array(), $namespaces = array()) + { + trigger_error( + 'Zend_Filter::get() is deprecated as of 1.9.0; please update your code to utilize Zend_Filter::filterStatic()', + E_USER_NOTICE + ); + + return self::filterStatic($value, $classBaseName, $args, $namespaces); + } + + /** + * 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 filterStatic($value, $classBaseName, array $args = array(), $namespaces = array()) + { + // require_once 'Zend/Loader.php'; + $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_Exception $ze) { + // continue; + // } + // } + + $class = new ReflectionClass($className); + if ($class->implementsInterface('Zend_Filter_Interface')) { + if ($class->hasMethod('__construct')) { + $object = $class->newInstanceArgs($args); + } else { + $object = $class->newInstance(); + } + return $object->filter($value); + } + } + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Filter class not found from basename '$classBaseName'"); + } +} diff --git a/libs/Zend/Filter/Alnum.php b/libs/Zend/Filter/Alnum.php new file mode 100644 index 0000000000000000000000000000000000000000..8a6ad4a0e5e8b8efea607ff8cc71b8b141cb411f --- /dev/null +++ b/libs/Zend/Filter/Alnum.php @@ -0,0 +1,146 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Alnum.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; +/** + * @see Zend_Locale + */ +// require_once 'Zend/Locale.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Alnum implements Zend_Filter_Interface +{ + /** + * Whether to allow white space characters; off by default + * + * @var boolean + * @deprecated + */ + public $allowWhiteSpace; + + /** + * Is PCRE is compiled with UTF-8 and Unicode support + * + * @var mixed + **/ + protected static $_unicodeEnabled; + + /** + * Locale in browser. + * + * @var Zend_Locale object + */ + protected $_locale; + + /** + * The Alphabet means english alphabet. + * + * @var boolean + */ + protected static $_meansEnglishAlphabet; + + /** + * Sets default option values for this instance + * + * @param boolean $allowWhiteSpace + * @return void + */ + public function __construct($allowWhiteSpace = false) + { + if ($allowWhiteSpace instanceof Zend_Config) { + $allowWhiteSpace = $allowWhiteSpace->toArray(); + } else if (is_array($allowWhiteSpace)) { + if (array_key_exists('allowwhitespace', $allowWhiteSpace)) { + $allowWhiteSpace = $allowWhiteSpace['allowwhitespace']; + } else { + $allowWhiteSpace = false; + } + } + + $this->allowWhiteSpace = (boolean) $allowWhiteSpace; + if (null === self::$_unicodeEnabled) { + self::$_unicodeEnabled = (@preg_match('/\pL/u', 'a')) ? true : false; + } + + if (null === self::$_meansEnglishAlphabet) { + $this->_locale = new Zend_Locale('auto'); + self::$_meansEnglishAlphabet = in_array($this->_locale->getLanguage(), + array('ja', 'ko', 'zh') + ); + } + + } + + /** + * Returns the allowWhiteSpace option + * + * @return boolean + */ + public function getAllowWhiteSpace() + { + return $this->allowWhiteSpace; + } + + /** + * Sets the allowWhiteSpace option + * + * @param boolean $allowWhiteSpace + * @return Zend_Filter_Alnum Provides a fluent interface + */ + public function setAllowWhiteSpace($allowWhiteSpace) + { + $this->allowWhiteSpace = (boolean) $allowWhiteSpace; + return $this; + } + + /** + * Defined by Zend_Filter_Interface + * + * Returns the string $value, removing all but alphabetic and digit characters + * + * @param string $value + * @return string + */ + public function filter($value) + { + $whiteSpace = $this->allowWhiteSpace ? '\s' : ''; + if (!self::$_unicodeEnabled) { + // POSIX named classes are not supported, use alternative a-zA-Z0-9 match + $pattern = '/[^a-zA-Z0-9' . $whiteSpace . ']/'; + } else if (self::$_meansEnglishAlphabet) { + //The Alphabet means english alphabet. + $pattern = '/[^a-zA-Z0-9' . $whiteSpace . ']/u'; + } else { + //The Alphabet means each language's alphabet. + $pattern = '/[^\p{L}\p{N}' . $whiteSpace . ']/u'; + } + + return preg_replace($pattern, '', (string) $value); + } +} diff --git a/libs/Zend/Filter/Alpha.php b/libs/Zend/Filter/Alpha.php new file mode 100644 index 0000000000000000000000000000000000000000..cdb01e1660d857d4ef29b7b5955283a0bd0b7f60 --- /dev/null +++ b/libs/Zend/Filter/Alpha.php @@ -0,0 +1,146 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Alpha.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; +/** + * @see Zend_Locale + */ +// require_once 'Zend/Locale.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Alpha implements Zend_Filter_Interface +{ + /** + * Whether to allow white space characters; off by default + * + * @var boolean + * @deprecated + */ + public $allowWhiteSpace; + + /** + * Is PCRE is compiled with UTF-8 and Unicode support + * + * @var mixed + **/ + protected static $_unicodeEnabled; + + /** + * Locale in browser. + * + * @var Zend_Locale object + */ + protected $_locale; + + /** + * The Alphabet means english alphabet. + * + * @var boolean + */ + protected static $_meansEnglishAlphabet; + + /** + * Sets default option values for this instance + * + * @param boolean $allowWhiteSpace + * @return void + */ + public function __construct($allowWhiteSpace = false) + { + if ($allowWhiteSpace instanceof Zend_Config) { + $allowWhiteSpace = $allowWhiteSpace->toArray(); + } else if (is_array($allowWhiteSpace)) { + if (array_key_exists('allowwhitespace', $allowWhiteSpace)) { + $allowWhiteSpace = $allowWhiteSpace['allowwhitespace']; + } else { + $allowWhiteSpace = false; + } + } + + $this->allowWhiteSpace = (boolean) $allowWhiteSpace; + if (null === self::$_unicodeEnabled) { + self::$_unicodeEnabled = (@preg_match('/\pL/u', 'a')) ? true : false; + } + + if (null === self::$_meansEnglishAlphabet) { + $this->_locale = new Zend_Locale('auto'); + self::$_meansEnglishAlphabet = in_array($this->_locale->getLanguage(), + array('ja', 'ko', 'zh') + ); + } + + } + + /** + * Returns the allowWhiteSpace option + * + * @return boolean + */ + public function getAllowWhiteSpace() + { + return $this->allowWhiteSpace; + } + + /** + * Sets the allowWhiteSpace option + * + * @param boolean $allowWhiteSpace + * @return Zend_Filter_Alpha Provides a fluent interface + */ + public function setAllowWhiteSpace($allowWhiteSpace) + { + $this->allowWhiteSpace = (boolean) $allowWhiteSpace; + return $this; + } + + /** + * Defined by Zend_Filter_Interface + * + * Returns the string $value, removing all but alphabetic characters + * + * @param string $value + * @return string + */ + public function filter($value) + { + $whiteSpace = $this->allowWhiteSpace ? '\s' : ''; + if (!self::$_unicodeEnabled) { + // POSIX named classes are not supported, use alternative a-zA-Z match + $pattern = '/[^a-zA-Z' . $whiteSpace . ']/'; + } else if (self::$_meansEnglishAlphabet) { + //The Alphabet means english alphabet. + $pattern = '/[^a-zA-Z' . $whiteSpace . ']/u'; + } else { + //The Alphabet means each language's alphabet. + $pattern = '/[^\p{L}' . $whiteSpace . ']/u'; + } + + return preg_replace($pattern, '', (string) $value); + } +} diff --git a/libs/Zend/Filter/BaseName.php b/libs/Zend/Filter/BaseName.php new file mode 100644 index 0000000000000000000000000000000000000000..aad6a7b78e01af49cb63b668b24079c20936bacd --- /dev/null +++ b/libs/Zend/Filter/BaseName.php @@ -0,0 +1,50 @@ +<?php + +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: BaseName.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_BaseName implements Zend_Filter_Interface +{ + /** + * Defined by Zend_Filter_Interface + * + * Returns basename($value) + * + * @param string $value + * @return string + */ + public function filter($value) + { + return basename((string) $value); + } +} diff --git a/libs/Zend/Filter/Boolean.php b/libs/Zend/Filter/Boolean.php new file mode 100644 index 0000000000000000000000000000000000000000..e697f064bbb23aefb5a2ccb5a6488d48006da084 --- /dev/null +++ b/libs/Zend/Filter/Boolean.php @@ -0,0 +1,375 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id:$ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Boolean implements Zend_Filter_Interface +{ + const BOOLEAN = 1; + const INTEGER = 2; + const FLOAT = 4; + const STRING = 8; + const ZERO = 16; + const EMPTY_ARRAY = 32; + const NULL = 64; + const PHP = 127; + const FALSE_STRING = 128; + const YES = 256; + const ALL = 511; + + protected $_constants = array( + self::BOOLEAN => 'boolean', + self::INTEGER => 'integer', + self::FLOAT => 'float', + self::STRING => 'string', + self::ZERO => 'zero', + self::EMPTY_ARRAY => 'array', + self::NULL => 'null', + self::PHP => 'php', + self::FALSE_STRING => 'false', + self::YES => 'yes', + self::ALL => 'all', + ); + + /** + * Internal type to detect + * + * @var integer + */ + protected $_type = self::PHP; + + /** + * Internal locale + * + * @var array + */ + protected $_locale = array('auto'); + + /** + * Internal mode + * + * @var boolean + */ + protected $_casting = true; + + /** + * Constructor + * + * @param string|array|Zend_Config $options OPTIONAL + */ + public function __construct($options = null) + { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } elseif (!is_array($options)) { + $options = func_get_args(); + $temp = array(); + if (!empty($options)) { + $temp['type'] = array_shift($options); + } + + if (!empty($options)) { + $temp['casting'] = array_shift($options); + } + + if (!empty($options)) { + $temp['locale'] = array_shift($options); + } + + $options = $temp; + } + + if (array_key_exists('type', $options)) { + $this->setType($options['type']); + } + + if (array_key_exists('casting', $options)) { + $this->setCasting($options['casting']); + } + + if (array_key_exists('locale', $options)) { + $this->setLocale($options['locale']); + } + } + + /** + * Returns the set null types + * + * @return int + */ + public function getType() + { + return $this->_type; + } + + /** + * Set the null types + * + * @param integer|array $type + * @throws Zend_Filter_Exception + * @return Zend_Filter_Boolean + */ + public function setType($type = null) + { + if (is_array($type)) { + $detected = 0; + foreach($type as $value) { + if (is_int($value)) { + $detected += $value; + } elseif (in_array($value, $this->_constants)) { + $detected += array_search($value, $this->_constants); + } + } + + $type = $detected; + } elseif (is_string($type) && in_array($type, $this->_constants)) { + $type = array_search($type, $this->_constants); + } + + if (!is_int($type) || ($type < 0) || ($type > self::ALL)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Unknown type'); + } + + $this->_type = $type; + return $this; + } + + /** + * Returns the set locale + * + * @return array + */ + public function getLocale() + { + return $this->_locale; + } + + /** + * Set the locales which are accepted + * + * @param string|array|Zend_Locale $locale + * @throws Zend_Filter_Exception + * @return Zend_Filter_Boolean + */ + public function setLocale($locale = null) + { + if (is_string($locale)) { + $locale = array($locale); + } elseif ($locale instanceof Zend_Locale) { + $locale = array($locale->toString()); + } elseif (!is_array($locale)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Locale has to be string, array or an instance of Zend_Locale'); + } + + // require_once 'Zend/Locale.php'; + foreach ($locale as $single) { + if (!Zend_Locale::isLocale($single)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Unknown locale '$single'"); + } + } + + $this->_locale = $locale; + return $this; + } + + /** + * Returns the casting option + * + * @return boolean + */ + public function getCasting() + { + return $this->_casting; + } + + /** + * Set the working mode + * + * @param boolean $locale When true this filter works like cast + * When false it recognises only true and false + * and all other values are returned as is + * @throws Zend_Filter_Exception + * @return Zend_Filter_Boolean + */ + public function setCasting($casting = true) + { + $this->_casting = (boolean) $casting; + return $this; + } + + /** + * Defined by Zend_Filter_Interface + * + * Returns a boolean representation of $value + * + * @param string $value + * @return string + */ + public function filter($value) + { + $type = $this->getType(); + $casting = $this->getCasting(); + + // STRING YES (Localized) + if ($type >= self::YES) { + $type -= self::YES; + if (is_string($value)) { + // require_once 'Zend/Locale.php'; + $locales = $this->getLocale(); + foreach ($locales as $locale) { + if ($this->_getLocalizedQuestion($value, false, $locale) === false) { + return false; + } + + if (!$casting && ($this->_getLocalizedQuestion($value, true, $locale) === true)) { + return true; + } + } + } + } + + // STRING FALSE ('false') + if ($type >= self::FALSE_STRING) { + $type -= self::FALSE_STRING; + if (is_string($value) && (strtolower($value) == 'false')) { + return false; + } + + if ((!$casting) && is_string($value) && (strtolower($value) == 'true')) { + return true; + } + } + + // NULL (null) + if ($type >= self::NULL) { + $type -= self::NULL; + if (is_null($value)) { + return false; + } + } + + // EMPTY_ARRAY (array()) + if ($type >= self::EMPTY_ARRAY) { + $type -= self::EMPTY_ARRAY; + if (is_array($value) && ($value == array())) { + return false; + } + } + + // ZERO ('0') + if ($type >= self::ZERO) { + $type -= self::ZERO; + if (is_string($value) && ($value == '0')) { + return false; + } + + if ((!$casting) && (is_string($value)) && ($value == '1')) { + return true; + } + } + + // STRING ('') + if ($type >= self::STRING) { + $type -= self::STRING; + if (is_string($value) && ($value == '')) { + return false; + } + } + + // FLOAT (0.0) + if ($type >= self::FLOAT) { + $type -= self::FLOAT; + if (is_float($value) && ($value == 0.0)) { + return false; + } + + if ((!$casting) && is_float($value) && ($value == 1.0)) { + return true; + } + } + + // INTEGER (0) + if ($type >= self::INTEGER) { + $type -= self::INTEGER; + if (is_int($value) && ($value == 0)) { + return false; + } + + if ((!$casting) && is_int($value) && ($value == 1)) { + return true; + } + } + + // BOOLEAN (false) + if ($type >= self::BOOLEAN) { + $type -= self::BOOLEAN; + if (is_bool($value)) { + return $value; + } + } + + if ($casting) { + return true; + } + + return $value; + } + + /** + * Determine the value of a localized string, and compare it to a given value + * + * @param string $value + * @param boolean $yes + * @param array $locale + * @return boolean + */ + protected function _getLocalizedQuestion($value, $yes, $locale) + { + if ($yes == true) { + $question = 'yes'; + $return = true; + } else { + $question = 'no'; + $return = false; + } + $str = Zend_Locale::getTranslation($question, 'question', $locale); + $str = explode(':', $str); + if (!empty($str)) { + foreach($str as $no) { + if (($no == $value) || (strtolower($no) == strtolower($value))) { + return $return; + } + } + } + } +} diff --git a/libs/Zend/Filter/Callback.php b/libs/Zend/Filter/Callback.php new file mode 100644 index 0000000000000000000000000000000000000000..ee091427bac5ac9f05b5d3c59b00cc5c4e690545 --- /dev/null +++ b/libs/Zend/Filter/Callback.php @@ -0,0 +1,152 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Callback.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Callback implements Zend_Filter_Interface +{ + /** + * Callback in a call_user_func format + * + * @var string|array + */ + protected $_callback = null; + + /** + * Default options to set for the filter + * + * @var mixed + */ + protected $_options = null; + + /** + * Constructor + * + * @param string|array $callback Callback in a call_user_func format + * @param mixed $options (Optional) Default options for this filter + */ + public function __construct($options) + { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } else if (!is_array($options) || !array_key_exists('callback', $options)) { + $options = func_get_args(); + $temp['callback'] = array_shift($options); + if (!empty($options)) { + $temp['options'] = array_shift($options); + } + + $options = $temp; + } + + if (!array_key_exists('callback', $options)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Missing callback to use'); + } + + $this->setCallback($options['callback']); + if (array_key_exists('options', $options)) { + $this->setOptions($options['options']); + } + } + + /** + * Returns the set callback + * + * @return string|array Set callback + */ + public function getCallback() + { + return $this->_callback; + } + + /** + * Sets a new callback for this filter + * + * @param unknown_type $callback + * @return unknown + */ + public function setCallback($callback, $options = null) + { + if (!is_callable($callback)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Callback can not be accessed'); + } + + $this->_callback = $callback; + $this->setOptions($options); + return $this; + } + + /** + * Returns the set default options + * + * @return mixed + */ + public function getOptions() + { + return $this->_options; + } + + /** + * Sets new default options to the callback filter + * + * @param mixed $options Default options to set + * @return Zend_Filter_Callback + */ + public function setOptions($options) + { + $this->_options = $options; + return $this; + } + + /** + * Calls the filter per callback + * + * @param $value mixed Options for the set callback + * @return mixed Result from the filter which was callbacked + */ + public function filter($value) + { + $options = array(); + + if ($this->_options !== null) { + if (!is_array($this->_options)) { + $options = array($this->_options); + } else { + $options = $this->_options; + } + } + + array_unshift($options, $value); + + return call_user_func_array($this->_callback, $options); + } +} \ No newline at end of file diff --git a/libs/Zend/Filter/Compress.php b/libs/Zend/Filter/Compress.php new file mode 100644 index 0000000000000000000000000000000000000000..3fbb78e08797ded57c3340b8c2ff4b3f1b28ea73 --- /dev/null +++ b/libs/Zend/Filter/Compress.php @@ -0,0 +1,197 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Compress.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + +/** + * Compresses a given string + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Compress implements Zend_Filter_Interface +{ + /** + * Compression adapter + */ + protected $_adapter = 'Gz'; + + /** + * Compression adapter constructor options + */ + protected $_adapterOptions = array(); + + /** + * Class constructor + * + * @param string|array $options (Optional) Options to set + */ + public function __construct($options = null) + { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } + if (is_string($options)) { + $this->setAdapter($options); + } elseif ($options instanceof Zend_Filter_Compress_CompressInterface) { + $this->setAdapter($options); + } elseif (is_array($options)) { + $this->setOptions($options); + } + } + + /** + * Set filter setate + * + * @param array $options + * @return Zend_Filter_Compress + */ + public function setOptions(array $options) + { + foreach ($options as $key => $value) { + if ($key == 'options') { + $key = 'adapterOptions'; + } + $method = 'set' . ucfirst($key); + if (method_exists($this, $method)) { + $this->$method($value); + } + } + return $this; + } + + /** + * Returns the current adapter, instantiating it if necessary + * + * @return string + */ + public function getAdapter() + { + if ($this->_adapter instanceof Zend_Filter_Compress_CompressInterface) { + return $this->_adapter; + } + + $adapter = $this->_adapter; + $options = $this->getAdapterOptions(); + // if (!class_exists($adapter)) { + // require_once 'Zend/Loader.php'; + // 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 Zend_Filter_Compress_CompressInterface) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Compression adapter '" . $adapter . "' does not implement Zend_Filter_Compress_CompressInterface"); + } + return $this->_adapter; + } + + /** + * Retrieve adapter name + * + * @return string + */ + public function getAdapterName() + { + return $this->getAdapter()->toString(); + } + + /** + * Sets compression adapter + * + * @param string|Zend_Filter_Compress_CompressInterface $adapter Adapter to use + * @return Zend_Filter_Compress + */ + public function setAdapter($adapter) + { + if ($adapter instanceof Zend_Filter_Compress_CompressInterface) { + $this->_adapter = $adapter; + return $this; + } + if (!is_string($adapter)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Invalid adapter provided; must be string or instance of Zend_Filter_Compress_CompressInterface'); + } + $this->_adapter = $adapter; + + return $this; + } + + /** + * Retrieve adapter options + * + * @return array + */ + public function getAdapterOptions() + { + return $this->_adapterOptions; + } + + /** + * Set adapter options + * + * @param array $options + * @return void + */ + public function setAdapterOptions(array $options) + { + $this->_adapterOptions = $options; + return $this; + } + + /** + * Calls adapter methods + * + * @param string $method Method to call + * @param string|array $options Options for this method + */ + public function __call($method, $options) + { + $adapter = $this->getAdapter(); + if (!method_exists($adapter, $method)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Unknown method '{$method}'"); + } + + return call_user_func_array(array($adapter, $method), $options); + } + + /** + * Defined by Zend_Filter_Interface + * + * Compresses the content $value with the defined settings + * + * @param string $value Content to compress + * @return string The compressed content + */ + public function filter($value) + { + return $this->getAdapter()->compress($value); + } +} diff --git a/libs/Zend/Filter/Compress/Bz2.php b/libs/Zend/Filter/Compress/Bz2.php new file mode 100644 index 0000000000000000000000000000000000000000..8241a0899152cfd2482cfa1d04a04863651b1a51 --- /dev/null +++ b/libs/Zend/Filter/Compress/Bz2.php @@ -0,0 +1,188 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Bz2.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Compress_CompressAbstract + */ +// require_once 'Zend/Filter/Compress/CompressAbstract.php'; + +/** + * Compression adapter for Bz2 + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Compress_Bz2 extends Zend_Filter_Compress_CompressAbstract +{ + /** + * Compression Options + * array( + * 'blocksize' => Blocksize to use from 0-9 + * 'archive' => Archive to use + * ) + * + * @var array + */ + protected $_options = array( + 'blocksize' => 4, + 'archive' => null, + ); + + /** + * Class constructor + * + * @param array|Zend_Config $options (Optional) Options to set + */ + public function __construct($options = null) + { + if (!extension_loaded('bz2')) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('This filter needs the bz2 extension'); + } + parent::__construct($options); + } + + /** + * Returns the set blocksize + * + * @return integer + */ + public function getBlocksize() + { + return $this->_options['blocksize']; + } + + /** + * Sets a new blocksize + * + * @param integer $level + * @return Zend_Filter_Compress_Bz2 + */ + public function setBlocksize($blocksize) + { + if (($blocksize < 0) || ($blocksize > 9)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Blocksize must be between 0 and 9'); + } + + $this->_options['blocksize'] = (int) $blocksize; + return $this; + } + + /** + * Returns the set archive + * + * @return string + */ + public function getArchive() + { + return $this->_options['archive']; + } + + /** + * Sets the archive to use for de-/compression + * + * @param string $archive Archive to use + * @return Zend_Filter_Compress_Bz2 + */ + public function setArchive($archive) + { + $this->_options['archive'] = (string) $archive; + return $this; + } + + /** + * Compresses the given content + * + * @param string $content + * @return string + */ + public function compress($content) + { + $archive = $this->getArchive(); + if (!empty($archive)) { + $file = bzopen($archive, 'w'); + if (!$file) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Error opening the archive '" . $archive . "'"); + } + + bzwrite($file, $content); + bzclose($file); + $compressed = true; + } else { + $compressed = bzcompress($content, $this->getBlocksize()); + } + + if (is_int($compressed)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Error during compression'); + } + + return $compressed; + } + + /** + * Decompresses the given content + * + * @param string $content + * @return string + */ + public function decompress($content) + { + $archive = $this->getArchive(); + if (file_exists($content)) { + $archive = $content; + } + + if (file_exists($archive)) { + $file = bzopen($archive, 'r'); + if (!$file) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Error opening the archive '" . $content . "'"); + } + + $compressed = bzread($file); + bzclose($file); + } else { + $compressed = bzdecompress($content); + } + + if (is_int($compressed)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Error during decompression'); + } + + return $compressed; + } + + /** + * Returns the adapter name + * + * @return string + */ + public function toString() + { + return 'Bz2'; + } +} diff --git a/libs/Zend/Filter/Compress/CompressAbstract.php b/libs/Zend/Filter/Compress/CompressAbstract.php new file mode 100644 index 0000000000000000000000000000000000000000..692aee4f09b77b3ee30c1f99f75695d2c4d15876 --- /dev/null +++ b/libs/Zend/Filter/Compress/CompressAbstract.php @@ -0,0 +1,89 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: CompressAbstract.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Compress_CompressInterface + */ +// require_once 'Zend/Filter/Compress/CompressInterface.php'; + +/** + * Abstract compression adapter + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +abstract class Zend_Filter_Compress_CompressAbstract implements Zend_Filter_Compress_CompressInterface +{ + /** + * Class constructor + * + * @param array|Zend_Config $options (Optional) Options to set + */ + public function __construct($options = null) + { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } + + if (is_array($options)) { + $this->setOptions($options); + } + } + + /** + * Returns one or all set options + * + * @param string $option (Optional) Option to return + * @return mixed + */ + public function getOptions($option = null) + { + if ($option === null) { + return $this->_options; + } + + if (!array_key_exists($option, $this->_options)) { + return null; + } + + return $this->_options[$option]; + } + + /** + * Sets all or one option + * + * @param array $options + * @return Zend_Filter_Compress_Bz2 + */ + public function setOptions(array $options) + { + foreach ($options as $key => $option) { + $method = 'set' . $key; + if (method_exists($this, $method)) { + $this->$method($option); + } + } + + return $this; + } +} diff --git a/libs/Zend/Filter/Compress/CompressInterface.php b/libs/Zend/Filter/Compress/CompressInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..2503398811d3acf778940d5d8ba4ce58d74671fa --- /dev/null +++ b/libs/Zend/Filter/Compress/CompressInterface.php @@ -0,0 +1,54 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: CompressInterface.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * Compression interface + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +interface Zend_Filter_Compress_CompressInterface +{ + /** + * Compresses $value with the defined settings + * + * @param string $value Data to compress + * @return string The compressed data + */ + public function compress($value); + + /** + * Decompresses $value with the defined settings + * + * @param string $value Data to decompress + * @return string The decompressed data + */ + public function decompress($value); + + /** + * Return the adapter name + * + * @return string + */ + public function toString(); +} diff --git a/libs/Zend/Filter/Compress/Gz.php b/libs/Zend/Filter/Compress/Gz.php new file mode 100644 index 0000000000000000000000000000000000000000..8ad8378275da2a7262515bc735e028fb25685818 --- /dev/null +++ b/libs/Zend/Filter/Compress/Gz.php @@ -0,0 +1,228 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Gz.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Compress_CompressAbstract + */ +// require_once 'Zend/Filter/Compress/CompressAbstract.php'; + +/** + * Compression adapter for Gzip (ZLib) + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Compress_Gz extends Zend_Filter_Compress_CompressAbstract +{ + /** + * Compression Options + * array( + * 'level' => Compression level 0-9 + * 'mode' => Compression mode, can be 'compress', 'deflate' + * 'archive' => Archive to use + * ) + * + * @var array + */ + protected $_options = array( + 'level' => 9, + 'mode' => 'compress', + 'archive' => null, + ); + + /** + * Class constructor + * + * @param array|Zend_Config|null $options (Optional) Options to set + */ + public function __construct($options = null) + { + if (!extension_loaded('zlib')) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('This filter needs the zlib extension'); + } + parent::__construct($options); + } + + /** + * Returns the set compression level + * + * @return integer + */ + public function getLevel() + { + return $this->_options['level']; + } + + /** + * Sets a new compression level + * + * @param integer $level + * @return Zend_Filter_Compress_Gz + */ + public function setLevel($level) + { + if (($level < 0) || ($level > 9)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Level must be between 0 and 9'); + } + + $this->_options['level'] = (int) $level; + return $this; + } + + /** + * Returns the set compression mode + * + * @return string + */ + public function getMode() + { + return $this->_options['mode']; + } + + /** + * Sets a new compression mode + * + * @param string $mode Supported are 'compress', 'deflate' and 'file' + */ + public function setMode($mode) + { + if (($mode != 'compress') && ($mode != 'deflate')) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Given compression mode not supported'); + } + + $this->_options['mode'] = $mode; + return $this; + } + + /** + * Returns the set archive + * + * @return string + */ + public function getArchive() + { + return $this->_options['archive']; + } + + /** + * Sets the archive to use for de-/compression + * + * @param string $archive Archive to use + * @return Zend_Filter_Compress_Gz + */ + public function setArchive($archive) + { + $this->_options['archive'] = (string) $archive; + return $this; + } + + /** + * Compresses the given content + * + * @param string $content + * @return string + */ + public function compress($content) + { + $archive = $this->getArchive(); + if (!empty($archive)) { + $file = gzopen($archive, 'w' . $this->getLevel()); + if (!$file) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Error opening the archive '" . $this->_options['archive'] . "'"); + } + + gzwrite($file, $content); + gzclose($file); + $compressed = true; + } else if ($this->_options['mode'] == 'deflate') { + $compressed = gzdeflate($content, $this->getLevel()); + } else { + $compressed = gzcompress($content, $this->getLevel()); + } + + if (!$compressed) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Error during compression'); + } + + return $compressed; + } + + /** + * Decompresses the given content + * + * @param string $content + * @return string + */ + public function decompress($content) + { + $archive = $this->getArchive(); + $mode = $this->getMode(); + if (file_exists($content)) { + $archive = $content; + } + + if (file_exists($archive)) { + $handler = fopen($archive, "rb"); + if (!$handler) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Error opening the archive '" . $archive . "'"); + } + + fseek($handler, -4, SEEK_END); + $packet = fread($handler, 4); + $bytes = unpack("V", $packet); + $size = end($bytes); + fclose($handler); + + $file = gzopen($archive, 'r'); + $compressed = gzread($file, $size); + gzclose($file); + } else if ($mode == 'deflate') { + $compressed = gzinflate($content); + } else { + $compressed = gzuncompress($content); + } + + if (!$compressed) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Error during compression'); + } + + return $compressed; + } + + /** + * Returns the adapter name + * + * @return string + */ + public function toString() + { + return 'Gz'; + } +} diff --git a/libs/Zend/Filter/Compress/Lzf.php b/libs/Zend/Filter/Compress/Lzf.php new file mode 100644 index 0000000000000000000000000000000000000000..e81e1a7b6271458dd75a978bee329b6f66a8f816 --- /dev/null +++ b/libs/Zend/Filter/Compress/Lzf.php @@ -0,0 +1,91 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Lzf.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Compress_CompressInterface + */ +// require_once 'Zend/Filter/Compress/CompressInterface.php'; + +/** + * Compression adapter for Lzf + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Compress_Lzf implements Zend_Filter_Compress_CompressInterface +{ + /** + * Class constructor + */ + public function __construct() + { + if (!extension_loaded('lzf')) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('This filter needs the lzf extension'); + } + } + + /** + * Compresses the given content + * + * @param string $content + * @return string + */ + public function compress($content) + { + $compressed = lzf_compress($content); + if (!$compressed) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Error during compression'); + } + + return $compressed; + } + + /** + * Decompresses the given content + * + * @param string $content + * @return string + */ + public function decompress($content) + { + $compressed = lzf_decompress($content); + if (!$compressed) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Error during compression'); + } + + return $compressed; + } + + /** + * Returns the adapter name + * + * @return string + */ + public function toString() + { + return 'Lzf'; + } +} diff --git a/libs/Zend/Filter/Compress/Rar.php b/libs/Zend/Filter/Compress/Rar.php new file mode 100644 index 0000000000000000000000000000000000000000..35c67719dc01fdddd972d46098e0954987be104b --- /dev/null +++ b/libs/Zend/Filter/Compress/Rar.php @@ -0,0 +1,252 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Rar.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Compress_CompressAbstract + */ +// require_once 'Zend/Filter/Compress/CompressAbstract.php'; + +/** + * Compression adapter for Rar + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Compress_Rar extends Zend_Filter_Compress_CompressAbstract +{ + /** + * Compression Options + * array( + * 'callback' => Callback for compression + * 'archive' => Archive to use + * 'password' => Password to use + * 'target' => Target to write the files to + * ) + * + * @var array + */ + protected $_options = array( + 'callback' => null, + 'archive' => null, + 'password' => null, + 'target' => '.', + ); + + /** + * Class constructor + * + * @param array $options (Optional) Options to set + */ + public function __construct($options = null) + { + if (!extension_loaded('rar')) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('This filter needs the rar extension'); + } + parent::__construct($options); + } + + /** + * Returns the set callback for compression + * + * @return string + */ + public function getCallback() + { + return $this->_options['callback']; + } + + /** + * Sets the callback to use + * + * @param string $callback + * @return Zend_Filter_Compress_Rar + */ + public function setCallback($callback) + { + if (!is_callable($callback)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Callback can not be accessed'); + } + + $this->_options['callback'] = $callback; + return $this; + } + + /** + * Returns the set archive + * + * @return string + */ + public function getArchive() + { + return $this->_options['archive']; + } + + /** + * Sets the archive to use for de-/compression + * + * @param string $archive Archive to use + * @return Zend_Filter_Compress_Rar + */ + public function setArchive($archive) + { + $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $archive); + $this->_options['archive'] = (string) $archive; + + return $this; + } + + /** + * Returns the set password + * + * @return string + */ + public function getPassword() + { + return $this->_options['password']; + } + + /** + * Sets the password to use + * + * @param string $password + * @return Zend_Filter_Compress_Rar + */ + public function setPassword($password) + { + $this->_options['password'] = (string) $password; + return $this; + } + + /** + * Returns the set targetpath + * + * @return string + */ + public function getTarget() + { + return $this->_options['target']; + } + + /** + * Sets the targetpath to use + * + * @param string $target + * @return Zend_Filter_Compress_Rar + */ + public function setTarget($target) + { + if (!file_exists(dirname($target))) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("The directory '$target' does not exist"); + } + + $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $target); + $this->_options['target'] = (string) $target; + return $this; + } + + /** + * Compresses the given content + * + * @param string|array $content + * @return string + */ + public function compress($content) + { + $callback = $this->getCallback(); + if (is_null($callback)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('No compression callback available'); + } + + $options = $this->getOptions(); + unset($options['callback']); + + $result = call_user_func($callback, $options, $content); + if ($result !== true) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Error compressing the RAR Archive'); + } + + return $this->getArchive(); + } + + /** + * Decompresses the given content + * + * @param string $content + * @return boolean + */ + public function decompress($content) + { + $archive = $this->getArchive(); + if (file_exists($content)) { + $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content)); + } elseif (empty($archive) || !file_exists($archive)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('RAR Archive not found'); + } + + $password = $this->getPassword(); + if (!is_null($password)) { + $archive = rar_open($archive, $password); + } else { + $archive = rar_open($archive); + } + + if (!$archive) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Error opening the RAR Archive"); + } + + $target = $this->getTarget(); + if (!is_dir($target)) { + $target = dirname($target); + } + + $filelist = rar_list($archive); + if (!$filelist) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Error reading the RAR Archive"); + } + + foreach($filelist as $file) { + $file->extract($target); + } + + rar_close($archive); + return true; + } + + /** + * Returns the adapter name + * + * @return string + */ + public function toString() + { + return 'Rar'; + } +} diff --git a/libs/Zend/Filter/Compress/Tar.php b/libs/Zend/Filter/Compress/Tar.php new file mode 100644 index 0000000000000000000000000000000000000000..4d865aafa820e88a3622d12756c016e62f532752 --- /dev/null +++ b/libs/Zend/Filter/Compress/Tar.php @@ -0,0 +1,245 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Tar.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Compress_CompressAbstract + */ +// require_once 'Zend/Filter/Compress/CompressAbstract.php'; + +/** + * Compression adapter for Tar + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Compress_Tar extends Zend_Filter_Compress_CompressAbstract +{ + /** + * Compression Options + * array( + * 'archive' => Archive to use + * 'target' => Target to write the files to + * ) + * + * @var array + */ + protected $_options = array( + 'archive' => null, + 'target' => '.', + 'mode' => null, + ); + + /** + * Class constructor + * + * @param array $options (Optional) Options to set + */ + public function __construct($options = null) + { + // if (!class_exists('Archive_Tar')) { + // require_once 'Zend/Loader.php'; + // try { + // Zend_Loader::loadClass('Archive_Tar'); + // } catch (Zend_Exception $e) { + // require_once 'Zend/Filter/Exception.php'; + // throw new Zend_Filter_Exception('This filter needs PEARs Archive_Tar', 0, $e); + // } + // } + + parent::__construct($options); + } + + /** + * Returns the set archive + * + * @return string + */ + public function getArchive() + { + return $this->_options['archive']; + } + + /** + * Sets the archive to use for de-/compression + * + * @param string $archive Archive to use + * @return Zend_Filter_Compress_Tar + */ + public function setArchive($archive) + { + $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $archive); + $this->_options['archive'] = (string) $archive; + + return $this; + } + + /** + * Returns the set targetpath + * + * @return string + */ + public function getTarget() + { + return $this->_options['target']; + } + + /** + * Sets the targetpath to use + * + * @param string $target + * @return Zend_Filter_Compress_Tar + */ + public function setTarget($target) + { + if (!file_exists(dirname($target))) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("The directory '$target' does not exist"); + } + + $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $target); + $this->_options['target'] = (string) $target; + return $this; + } + + /** + * Returns the set compression mode + */ + public function getMode() + { + return $this->_options['mode']; + } + + /** + * Compression mode to use + * Eighter Gz or Bz2 + * + * @param string $mode + */ + public function setMode($mode) + { + $mode = ucfirst(strtolower($mode)); + if (($mode != 'Bz2') && ($mode != 'Gz')) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("The mode '$mode' is unknown"); + } + + if (($mode == 'Bz2') && (!extension_loaded('bz2'))) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('This mode needs the bz2 extension'); + } + + if (($mode == 'Gz') && (!extension_loaded('zlib'))) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('This mode needs the zlib extension'); + } + } + + /** + * Compresses the given content + * + * @param string $content + * @return string + */ + public function compress($content) + { + $archive = new Archive_Tar($this->getArchive(), $this->getMode()); + if (!file_exists($content)) { + $file = $this->getTarget(); + if (is_dir($file)) { + $file .= DIRECTORY_SEPARATOR . "tar.tmp"; + } + + $result = file_put_contents($file, $content); + if ($result === false) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Error creating the temporary file'); + } + + $content = $file; + } + + if (is_dir($content)) { + // collect all file infos + foreach (new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($content, RecursiveDirectoryIterator::KEY_AS_PATHNAME), + RecursiveIteratorIterator::SELF_FIRST + ) as $directory => $info + ) { + if ($info->isFile()) { + $file[] = $directory; + } + } + + $content = $file; + } + + $result = $archive->create($content); + if ($result === false) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Error creating the Tar archive'); + } + + return $this->getArchive(); + } + + /** + * Decompresses the given content + * + * @param string $content + * @return boolean + */ + public function decompress($content) + { + $archive = $this->getArchive(); + if (file_exists($content)) { + $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content)); + } elseif (empty($archive) || !file_exists($archive)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Tar Archive not found'); + } + + $archive = new Archive_Tar($archive, $this->getMode()); + $target = $this->getTarget(); + if (!is_dir($target)) { + $target = dirname($target); + } + + $result = $archive->extract($target); + if ($result === false) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Error while extracting the Tar archive'); + } + + return true; + } + + /** + * Returns the adapter name + * + * @return string + */ + public function toString() + { + return 'Tar'; + } +} diff --git a/libs/Zend/Filter/Compress/Zip.php b/libs/Zend/Filter/Compress/Zip.php new file mode 100644 index 0000000000000000000000000000000000000000..a99fce786baf87278feaf2a1a2fe2e2c8dde65f8 --- /dev/null +++ b/libs/Zend/Filter/Compress/Zip.php @@ -0,0 +1,355 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Zip.php 20126 2010-01-07 18:10:58Z ralph $ + */ + +/** + * @see Zend_Filter_Compress_CompressAbstract + */ +// require_once 'Zend/Filter/Compress/CompressAbstract.php'; + +/** + * Compression adapter for zip + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Compress_Zip extends Zend_Filter_Compress_CompressAbstract +{ + /** + * Compression Options + * array( + * 'archive' => Archive to use + * 'password' => Password to use + * 'target' => Target to write the files to + * ) + * + * @var array + */ + protected $_options = array( + 'archive' => null, + 'target' => null, + ); + + /** + * Class constructor + * + * @param string|array $options (Optional) Options to set + */ + public function __construct($options = null) + { + if (!extension_loaded('zip')) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('This filter needs the zip extension'); + } + parent::__construct($options); + } + + /** + * Returns the set archive + * + * @return string + */ + public function getArchive() + { + return $this->_options['archive']; + } + + /** + * Sets the archive to use for de-/compression + * + * @param string $archive Archive to use + * @return Zend_Filter_Compress_Rar + */ + public function setArchive($archive) + { + $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $archive); + $this->_options['archive'] = (string) $archive; + + return $this; + } + + /** + * Returns the set targetpath + * + * @return string + */ + public function getTarget() + { + return $this->_options['target']; + } + + /** + * Sets the target to use + * + * @param string $target + * @return Zend_Filter_Compress_Rar + */ + public function setTarget($target) + { + if (!file_exists(dirname($target))) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("The directory '$target' does not exist"); + } + + $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $target); + $this->_options['target'] = (string) $target; + return $this; + } + + /** + * Compresses the given content + * + * @param string $content + * @return string Compressed archive + */ + public function compress($content) + { + $zip = new ZipArchive(); + $res = $zip->open($this->getArchive(), ZipArchive::CREATE | ZipArchive::OVERWRITE); + + if ($res !== true) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception($this->_errorString($res)); + } + + if (file_exists($content)) { + $content = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content)); + $basename = substr($content, strrpos($content, DIRECTORY_SEPARATOR) + 1); + if (is_dir($content)) { + $index = strrpos($content, DIRECTORY_SEPARATOR) + 1; + $content .= DIRECTORY_SEPARATOR; + $stack = array($content); + while (!empty($stack)) { + $current = array_pop($stack); + $files = array(); + + $dir = dir($current); + while (false !== ($node = $dir->read())) { + if (($node == '.') || ($node == '..')) { + continue; + } + + if (is_dir($current . $node)) { + array_push($stack, $current . $node . DIRECTORY_SEPARATOR); + } + + if (is_file($current . $node)) { + $files[] = $node; + } + } + + $local = substr($current, $index); + $zip->addEmptyDir(substr($local, 0, -1)); + + foreach ($files as $file) { + $zip->addFile($current . $file, $local . $file); + if ($res !== true) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception($this->_errorString($res)); + } + } + } + } else { + $res = $zip->addFile($content, $basename); + if ($res !== true) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception($this->_errorString($res)); + } + } + } else { + $file = $this->getTarget(); + if (!is_dir($file)) { + $file = basename($file); + } else { + $file = "zip.tmp"; + } + + $res = $zip->addFromString($file, $content); + if ($res !== true) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception($this->_errorString($res)); + } + } + + $zip->close(); + return $this->_options['archive']; + } + + /** + * Decompresses the given content + * + * @param string $content + * @return string + */ + public function decompress($content) + { + $archive = $this->getArchive(); + if (file_exists($content)) { + $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content)); + } elseif (empty($archive) || !file_exists($archive)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('ZIP Archive not found'); + } + + $zip = new ZipArchive(); + $res = $zip->open($archive); + + $target = $this->getTarget(); + + if (!empty($target) && !is_dir($target)) { + $target = dirname($target); + } + + if (!empty($target)) { + $target = rtrim($target, '/\\') . DIRECTORY_SEPARATOR; + } + + if (empty($target) || !is_dir($target)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('No target for ZIP decompression set'); + } + + if ($res !== true) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception($this->_errorString($res)); + } + + if (version_compare(PHP_VERSION, '5.2.8', '<')) { + for ($i = 0; $i < $zip->numFiles; $i++) { + $statIndex = $zip->statIndex($i); + $currName = $statIndex['name']; + if (($currName{0} == '/') || + (substr($currName, 0, 2) == '..') || + (substr($currName, 0, 4) == './..') + ) + { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Upward directory traversal was detected inside ' . $archive + . ' please use PHP 5.2.8 or greater to take advantage of path resolution features of ' + . 'the zip extension in this decompress() method.' + ); + } + } + } + + $res = @$zip->extractTo($target); + if ($res !== true) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception($this->_errorString($res)); + } + + $zip->close(); + return $target; + } + + /** + * Returns the proper string based on the given error constant + * + * @param string $error + */ + protected function _errorString($error) + { + switch($error) { + case ZipArchive::ER_MULTIDISK : + return 'Multidisk ZIP Archives not supported'; + + case ZipArchive::ER_RENAME : + return 'Failed to rename the temporary file for ZIP'; + + case ZipArchive::ER_CLOSE : + return 'Failed to close the ZIP Archive'; + + case ZipArchive::ER_SEEK : + return 'Failure while seeking the ZIP Archive'; + + case ZipArchive::ER_READ : + return 'Failure while reading the ZIP Archive'; + + case ZipArchive::ER_WRITE : + return 'Failure while writing the ZIP Archive'; + + case ZipArchive::ER_CRC : + return 'CRC failure within the ZIP Archive'; + + case ZipArchive::ER_ZIPCLOSED : + return 'ZIP Archive already closed'; + + case ZipArchive::ER_NOENT : + return 'No such file within the ZIP Archive'; + + case ZipArchive::ER_EXISTS : + return 'ZIP Archive already exists'; + + case ZipArchive::ER_OPEN : + return 'Can not open ZIP Archive'; + + case ZipArchive::ER_TMPOPEN : + return 'Failure creating temporary ZIP Archive'; + + case ZipArchive::ER_ZLIB : + return 'ZLib Problem'; + + case ZipArchive::ER_MEMORY : + return 'Memory allocation problem while working on a ZIP Archive'; + + case ZipArchive::ER_CHANGED : + return 'ZIP Entry has been changed'; + + case ZipArchive::ER_COMPNOTSUPP : + return 'Compression method not supported within ZLib'; + + case ZipArchive::ER_EOF : + return 'Premature EOF within ZIP Archive'; + + case ZipArchive::ER_INVAL : + return 'Invalid argument for ZLIB'; + + case ZipArchive::ER_NOZIP : + return 'Given file is no zip archive'; + + case ZipArchive::ER_INTERNAL : + return 'Internal error while working on a ZIP Archive'; + + case ZipArchive::ER_INCONS : + return 'Inconsistent ZIP archive'; + + case ZipArchive::ER_REMOVE : + return 'Can not remove ZIP Archive'; + + case ZipArchive::ER_DELETED : + return 'ZIP Entry has been deleted'; + + default : + return 'Unknown error within ZIP Archive'; + } + } + + /** + * Returns the adapter name + * + * @return string + */ + public function toString() + { + return 'Zip'; + } +} diff --git a/libs/Zend/Filter/Decompress.php b/libs/Zend/Filter/Decompress.php new file mode 100644 index 0000000000000000000000000000000000000000..038f9d6a0ea5375bb6467899702293836038ee15 --- /dev/null +++ b/libs/Zend/Filter/Decompress.php @@ -0,0 +1,49 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Decompress.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Compress + */ +// require_once 'Zend/Filter/Compress.php'; + +/** + * Decompresses a given string + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Decompress extends Zend_Filter_Compress +{ + /** + * Defined by Zend_Filter_Interface + * + * Decompresses the content $value with the defined settings + * + * @param string $value Content to decompress + * @return string The decompressed content + */ + public function filter($value) + { + return $this->getAdapter()->decompress($value); + } +} diff --git a/libs/Zend/Filter/Decrypt.php b/libs/Zend/Filter/Decrypt.php new file mode 100644 index 0000000000000000000000000000000000000000..7d48f495a8d5460a38ae66036b39062c9f24de5c --- /dev/null +++ b/libs/Zend/Filter/Decrypt.php @@ -0,0 +1,49 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Decrypt.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Encrypt + */ +// require_once 'Zend/Filter/Encrypt.php'; + +/** + * Decrypts a given string + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Decrypt extends Zend_Filter_Encrypt +{ + /** + * Defined by Zend_Filter_Interface + * + * Decrypts the content $value with the defined settings + * + * @param string $value Content to decrypt + * @return string The decrypted content + */ + public function filter($value) + { + return $this->_adapter->decrypt($value); + } +} diff --git a/libs/Zend/Filter/Digits.php b/libs/Zend/Filter/Digits.php new file mode 100644 index 0000000000000000000000000000000000000000..c21c4f72754552b6351b80cf509563cf904c6231 --- /dev/null +++ b/libs/Zend/Filter/Digits.php @@ -0,0 +1,82 @@ +<?php + +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Digits.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Digits implements Zend_Filter_Interface +{ + /** + * Is PCRE is compiled with UTF-8 and Unicode support + * + * @var mixed + **/ + protected static $_unicodeEnabled; + + /** + * Class constructor + * + * Checks if PCRE is compiled with UTF-8 and Unicode support + * + * @return void + */ + public function __construct() + { + if (null === self::$_unicodeEnabled) { + self::$_unicodeEnabled = (@preg_match('/\pL/u', 'a')) ? true : false; + } + } + + /** + * Defined by Zend_Filter_Interface + * + * Returns the string $value, removing all but digit characters + * + * @param string $value + * @return string + */ + public function filter($value) + { + if (!self::$_unicodeEnabled) { + // POSIX named classes are not supported, use alternative 0-9 match + $pattern = '/[^0-9]/'; + } else if (extension_loaded('mbstring')) { + // Filter for the value with mbstring + $pattern = '/[^[:digit:]]/'; + } else { + // Filter for the value without mbstring + $pattern = '/[\p{^N}]/'; + } + + return preg_replace($pattern, '', (string) $value); + } +} diff --git a/libs/Zend/Filter/Dir.php b/libs/Zend/Filter/Dir.php new file mode 100644 index 0000000000000000000000000000000000000000..d2208651a828093c9d6cbab397b061222b41f501 --- /dev/null +++ b/libs/Zend/Filter/Dir.php @@ -0,0 +1,50 @@ +<?php + +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Dir.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Dir implements Zend_Filter_Interface +{ + /** + * Defined by Zend_Filter_Interface + * + * Returns dirname($value) + * + * @param string $value + * @return string + */ + public function filter($value) + { + return dirname((string) $value); + } +} diff --git a/libs/Zend/Filter/Encrypt.php b/libs/Zend/Filter/Encrypt.php new file mode 100644 index 0000000000000000000000000000000000000000..18325bbeb590608534b86a4ea4747d72de690b38 --- /dev/null +++ b/libs/Zend/Filter/Encrypt.php @@ -0,0 +1,138 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Encrypt.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + +/** + * @see Zend_Loader + */ +// require_once 'Zend/Loader.php'; + +/** + * Encrypts a given string + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Encrypt implements Zend_Filter_Interface +{ + /** + * Encryption adapter + */ + protected $_adapter; + + /** + * Class constructor + * + * @param string|array $options (Optional) Options to set, if null mcrypt is used + */ + public function __construct($options = null) + { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } + + $this->setAdapter($options); + } + + /** + * Returns the name of the set adapter + * + * @return string + */ + public function getAdapter() + { + return $this->_adapter->toString(); + } + + /** + * Sets new encryption options + * + * @param string|array $options (Optional) Encryption options + * @return 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 (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 Zend_Filter_Encrypt_Interface) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Encoding adapter '" . $adapter . "' does not implement Zend_Filter_Encrypt_Interface"); + } + + return $this; + } + + /** + * Calls adapter methods + * + * @param string $method Method to call + * @param string|array $options Options for this method + */ + public function __call($method, $options) + { + $part = substr($method, 0, 3); + if ((($part != 'get') and ($part != 'set')) or !method_exists($this->_adapter, $method)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Unknown method '{$method}'"); + } + + return call_user_func_array(array($this->_adapter, $method), $options); + } + + /** + * Defined by Zend_Filter_Interface + * + * Encrypts the content $value with the defined settings + * + * @param string $value Content to encrypt + * @return string The encrypted content + */ + public function filter($value) + { + return $this->_adapter->encrypt($value); + } +} diff --git a/libs/Zend/Filter/Encrypt/Interface.php b/libs/Zend/Filter/Encrypt/Interface.php new file mode 100644 index 0000000000000000000000000000000000000000..949d3d6f5af0cf56fdb90660462a0c7d7f9fdf5a --- /dev/null +++ b/libs/Zend/Filter/Encrypt/Interface.php @@ -0,0 +1,47 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Interface.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * Encryption interface + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +interface Zend_Filter_Encrypt_Interface +{ + /** + * Encrypts $value with the defined settings + * + * @param string $value Data to encrypt + * @return string The encrypted data + */ + public function encrypt($value); + + /** + * Decrypts $value with the defined settings + * + * @param string $value Data to decrypt + * @return string The decrypted data + */ + public function decrypt($value); +} diff --git a/libs/Zend/Filter/Encrypt/Mcrypt.php b/libs/Zend/Filter/Encrypt/Mcrypt.php new file mode 100644 index 0000000000000000000000000000000000000000..73a9c9cb93ac45dda2ef9f873073bf3c70d91b64 --- /dev/null +++ b/libs/Zend/Filter/Encrypt/Mcrypt.php @@ -0,0 +1,312 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Mcrypt.php 20132 2010-01-07 21:33:50Z ralph $ + */ + +/** + * @see Zend_Filter_Encrypt_Interface + */ +// require_once 'Zend/Filter/Encrypt/Interface.php'; + +/** + * Encryption adapter for mcrypt + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Encrypt_Mcrypt implements Zend_Filter_Encrypt_Interface +{ + /** + * Definitions for encryption + * array( + * 'key' => encryption key string + * 'algorithm' => algorithm to use + * 'algorithm_directory' => directory where to find the algorithm + * 'mode' => encryption mode to use + * 'modedirectory' => directory where to find the mode + * ) + */ + protected $_encryption = array( + 'key' => 'ZendFramework', + 'algorithm' => 'blowfish', + 'algorithm_directory' => '', + 'mode' => 'cbc', + 'mode_directory' => '', + 'vector' => null, + 'salt' => false + ); + + protected static $_srandCalled = false; + + /** + * Class constructor + * + * @param string|array|Zend_Config $options Cryption Options + */ + public function __construct($options) + { + if (!extension_loaded('mcrypt')) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('This filter needs the mcrypt extension'); + } + + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } elseif (is_string($options)) { + $options = array('key' => $options); + } elseif (!is_array($options)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Invalid options argument provided to filter'); + } + + $this->setEncryption($options); + } + + /** + * Returns the set encryption options + * + * @return array + */ + public function getEncryption() + { + return $this->_encryption; + } + + /** + * Sets new encryption options + * + * @param string|array $options Encryption options + * @return Zend_Filter_File_Encryption + */ + public function setEncryption($options) + { + if (is_string($options)) { + $options = array('key' => $options); + } + + if (!is_array($options)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Invalid options argument provided to filter'); + } + + $options = $options + $this->getEncryption(); + $algorithms = mcrypt_list_algorithms($options['algorithm_directory']); + if (!in_array($options['algorithm'], $algorithms)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("The algorithm '{$options['algorithm']}' is not supported"); + } + + $modes = mcrypt_list_modes($options['mode_directory']); + if (!in_array($options['mode'], $modes)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("The mode '{$options['mode']}' is not supported"); + } + + if (!mcrypt_module_self_test($options['algorithm'], $options['algorithm_directory'])) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('The given algorithm can not be used due an internal mcrypt problem'); + } + + if (!isset($options['vector'])) { + $options['vector'] = null; + } + + $this->_encryption = $options; + $this->setVector($options['vector']); + + return $this; + } + + /** + * Returns the set vector + * + * @return string + */ + public function getVector() + { + return $this->_encryption['vector']; + } + + /** + * Sets the initialization vector + * + * @param string $vector (Optional) Vector to set + * @return Zend_Filter_Encrypt_Mcrypt + */ + public function setVector($vector = null) + { + $cipher = $this->_openCipher(); + $size = mcrypt_enc_get_iv_size($cipher); + if (empty($vector)) { + $this->_srand(); + if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && version_compare(PHP_VERSION, '5.3.0', '<')) { + $method = MCRYPT_RAND; + } else { + if (file_exists('/dev/urandom') || (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')) { + $method = MCRYPT_DEV_URANDOM; + } elseif (file_exists('/dev/random')) { + $method = MCRYPT_DEV_RANDOM; + } else { + $method = MCRYPT_RAND; + } + } + $vector = mcrypt_create_iv($size, $method); + } else if (strlen($vector) != $size) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('The given vector has a wrong size for the set algorithm'); + } + + $this->_encryption['vector'] = $vector; + $this->_closeCipher($cipher); + + return $this; + } + + /** + * Defined by Zend_Filter_Interface + * + * Encrypts the file $value with the defined settings + * + * @param string $value Full path of file to change + * @return string The filename which has been set, or false when there were errors + */ + public function encrypt($value) + { + $cipher = $this->_openCipher(); + $this->_initCipher($cipher); + $encrypted = mcrypt_generic($cipher, $value); + mcrypt_generic_deinit($cipher); + $this->_closeCipher($cipher); + + return $encrypted; + } + + /** + * Defined by Zend_Filter_Interface + * + * Decrypts the file $value with the defined settings + * + * @param string $value Full path of file to change + * @return string The filename which has been set, or false when there were errors + */ + public function decrypt($value) + { + $cipher = $this->_openCipher(); + $this->_initCipher($cipher); + $decrypted = mdecrypt_generic($cipher, $value); + mcrypt_generic_deinit($cipher); + $this->_closeCipher($cipher); + + return $decrypted; + } + + /** + * Returns the adapter name + * + * @return string + */ + public function toString() + { + return 'Mcrypt'; + } + + /** + * Open a cipher + * + * @throws Zend_Filter_Exception When the cipher can not be opened + * @return resource Returns the opened cipher + */ + protected function _openCipher() + { + $cipher = mcrypt_module_open( + $this->_encryption['algorithm'], + $this->_encryption['algorithm_directory'], + $this->_encryption['mode'], + $this->_encryption['mode_directory']); + + if ($cipher === false) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Mcrypt can not be opened with your settings'); + } + + return $cipher; + } + + /** + * Close a cipher + * + * @param resource $cipher Cipher to close + * @return Zend_Filter_Encrypt_Mcrypt + */ + protected function _closeCipher($cipher) + { + mcrypt_module_close($cipher); + + return $this; + } + + /** + * Initialises the cipher with the set key + * + * @param resource $cipher + * @throws + * @return resource + */ + protected function _initCipher($cipher) + { + $key = $this->_encryption['key']; + + $keysizes = mcrypt_enc_get_supported_key_sizes($cipher); + if (empty($keysizes) || ($this->_encryption['salt'] == true)) { + $this->_srand(); + $keysize = mcrypt_enc_get_key_size($cipher); + $key = substr(md5($key), 0, $keysize); + } else if (!in_array(strlen($key), $keysizes)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('The given key has a wrong size for the set algorithm'); + } + + $result = mcrypt_generic_init($cipher, $key, $this->_encryption['vector']); + if ($result < 0) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Mcrypt could not be initialize with the given setting'); + } + + return $this; + } + + /** + * _srand() interception + * + * @see ZF-8742 + */ + protected function _srand() + { + if (version_compare(PHP_VERSION, '5.3.0', '>=')) { + return; + } + + if (!self::$_srandCalled) { + srand((double) microtime() * 1000000); + self::$_srandCalled = true; + } + } +} diff --git a/libs/Zend/Filter/Encrypt/Openssl.php b/libs/Zend/Filter/Encrypt/Openssl.php new file mode 100644 index 0000000000000000000000000000000000000000..751d3f7d866cead21724188cab50566d91e6364a --- /dev/null +++ b/libs/Zend/Filter/Encrypt/Openssl.php @@ -0,0 +1,353 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Openssl.php 20288 2010-01-14 20:15:43Z thomas $ + */ + +/** + * @see Zend_Filter_Encrypt_Interface + */ +// require_once 'Zend/Filter/Encrypt/Interface.php'; + +/** + * Encryption adapter for openssl + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Encrypt_Openssl implements Zend_Filter_Encrypt_Interface +{ + /** + * Definitions for encryption + * array( + * 'public' => public keys + * 'private' => private keys + * 'envelope' => resulting envelope keys + * ) + */ + protected $_keys = array( + 'public' => array(), + 'private' => array(), + 'envelope' => array() + ); + + /** + * Internal passphrase + * + * @var string + */ + protected $_passphrase; + + /** + * Class constructor + * Available options + * 'public' => public key + * 'private' => private key + * 'envelope' => envelope key + * 'passphrase' => passphrase + * + * @param string|array $options Options for this adapter + */ + public function __construct($options = array()) + { + if (!extension_loaded('openssl')) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('This filter needs the openssl extension'); + } + + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } + + if (!is_array($options)) { + $options = array('public' => $options); + } + + if (array_key_exists('passphrase', $options)) { + $this->setPassphrase($options['passphrase']); + unset($options['passphrase']); + } + + $this->_setKeys($options); + } + + /** + * Sets the encryption keys + * + * @param string|array $keys Key with type association + * @return Zend_Filter_Encrypt_Openssl + */ + protected function _setKeys($keys) + { + if (!is_array($keys)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Invalid options argument provided to filter'); + } + + foreach ($keys as $type => $key) { + if (is_file($key) and is_readable($key)) { + $file = fopen($key, 'r'); + $cert = fread($file, 8192); + fclose($file); + } else { + $cert = $key; + $key = count($this->_keys[$type]); + } + + switch ($type) { + case 'public': + $test = openssl_pkey_get_public($cert); + if ($test === false) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Public key '{$cert}' not valid"); + } + + openssl_free_key($test); + $this->_keys['public'][$key] = $cert; + break; + case 'private': + $test = openssl_pkey_get_private($cert, $this->_passphrase); + if ($test === false) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Private key '{$cert}' not valid"); + } + + openssl_free_key($test); + $this->_keys['private'][$key] = $cert; + break; + case 'envelope': + $this->_keys['envelope'][$key] = $cert; + break; + default: + break; + } + } + + return $this; + } + + /** + * Returns all public keys + * + * @return array + */ + public function getPublicKey() + { + return $this->_keys['public']; + } + + /** + * Sets public keys + * + * @param string|array $key Public keys + * @return Zend_Filter_Encrypt_Openssl + */ + public function setPublicKey($key) + { + if (is_array($key)) { + foreach($key as $type => $option) { + if ($type !== 'public') { + $key['public'] = $option; + unset($key[$type]); + } + } + } else { + $key = array('public' => $key); + } + + return $this->_setKeys($key); + } + + /** + * Returns all private keys + * + * @return array + */ + public function getPrivateKey() + { + return $this->_keys['private']; + } + + /** + * Sets private keys + * + * @param string $key Private key + * @param string $passphrase + * @return Zend_Filter_Encrypt_Openssl + */ + public function setPrivateKey($key, $passphrase = null) + { + if (is_array($key)) { + foreach($key as $type => $option) { + if ($type !== 'private') { + $key['private'] = $option; + unset($key[$type]); + } + } + } else { + $key = array('private' => $key); + } + + if ($passphrase !== null) { + $this->setPassphrase($passphrase); + } + + return $this->_setKeys($key); + } + + /** + * Returns all envelope keys + * + * @return array + */ + public function getEnvelopeKey() + { + return $this->_keys['envelope']; + } + + /** + * Sets envelope keys + * + * @param string|array $options Envelope keys + * @return Zend_Filter_Encrypt_Openssl + */ + public function setEnvelopeKey($key) + { + if (is_array($key)) { + foreach($key as $type => $option) { + if ($type !== 'envelope') { + $key['envelope'] = $option; + unset($key[$type]); + } + } + } else { + $key = array('envelope' => $key); + } + + return $this->_setKeys($key); + } + + /** + * Returns the passphrase + * + * @return string + */ + public function getPassphrase() + { + return $this->_passphrase; + } + + /** + * Sets a new passphrase + * + * @param string $passphrase + * @return Zend_Filter_Encrypt_Openssl + */ + public function setPassphrase($passphrase) + { + $this->_passphrase = $passphrase; + return $this; + } + + /** + * Encrypts the file $value with the defined settings + * Note that you also need the "encrypted" keys to be able to decrypt + * + * @param string $value Content to encrypt + * @return string The encrypted content + * @throws Zend_Filter_Exception + */ + public function encrypt($value) + { + $encrypted = array(); + $encryptedkeys = array(); + + if (count($this->_keys['public']) == 0) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Openssl can not encrypt without public keys'); + } + + foreach($this->_keys['public'] as $key => $cert) { + $keys[$key] = openssl_pkey_get_public($cert); + } + + $crypt = openssl_seal($value, $encrypted, $encryptedkeys, $keys); + foreach ($keys as $key) { + openssl_free_key($key); + } + + if ($crypt === false) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Openssl was not able to encrypt you content with the given options'); + } + + $this->_keys['envelope'] = $encryptedkeys; + return $encrypted; + } + + /** + * Defined by Zend_Filter_Interface + * + * Decrypts the file $value with the defined settings + * + * @param string $value Content to decrypt + * @return string The decrypted content + * @throws Zend_Filter_Exception + */ + public function decrypt($value) + { + $decrypted = ""; + $envelope = current($this->getEnvelopeKey()); + + if (count($this->_keys['private']) !== 1) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Openssl can only decrypt with one private key'); + } + + if (empty($envelope)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Openssl can only decrypt with one envelope key'); + } + + foreach($this->_keys['private'] as $key => $cert) { + $keys = openssl_pkey_get_private($cert, $this->getPassphrase()); + } + + $crypt = openssl_open($value, $decrypted, $envelope, $keys); + openssl_free_key($keys); + + if ($crypt === false) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Openssl was not able to decrypt you content with the given options'); + } + + return $decrypted; + } + + /** + * Returns the adapter name + * + * @return string + */ + public function toString() + { + return 'Openssl'; + } +} diff --git a/libs/Zend/Filter/Exception.php b/libs/Zend/Filter/Exception.php new file mode 100644 index 0000000000000000000000000000000000000000..97cdb9201ed900cc73c2af462d9b8bf436edc345 --- /dev/null +++ b/libs/Zend/Filter/Exception.php @@ -0,0 +1,37 @@ +<?php + +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Exception.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + + +/** + * @see Zend_Exception + */ +// require_once 'Zend/Exception.php'; + + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Exception extends Zend_Exception +{} diff --git a/libs/Zend/Filter/File/Decrypt.php b/libs/Zend/Filter/File/Decrypt.php new file mode 100644 index 0000000000000000000000000000000000000000..b45a8021700cc862649911da662eb1343065b7ec --- /dev/null +++ b/libs/Zend/Filter/File/Decrypt.php @@ -0,0 +1,106 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Decrypt.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Decrypt + */ +// require_once 'Zend/Filter/Decrypt.php'; + +/** + * Decrypts a given file and stores the decrypted file content + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_File_Decrypt extends Zend_Filter_Decrypt +{ + /** + * New filename to set + * + * @var string + */ + protected $_filename; + + /** + * Returns the new filename where the content will be stored + * + * @return string + */ + public function getFilename() + { + return $this->_filename; + } + + /** + * Sets the new filename where the content will be stored + * + * @param string $filename (Optional) New filename to set + * @return Zend_Filter_File_Encryt + */ + public function setFilename($filename = null) + { + $this->_filename = $filename; + return $this; + } + + /** + * Defined by Zend_Filter_Interface + * + * Decrypts the file $value with the defined settings + * + * @param string $value Full path of file to change + * @return string The filename which has been set, or false when there were errors + */ + public function filter($value) + { + if (!file_exists($value)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("File '$value' not found"); + } + + if (!isset($this->_filename)) { + $this->_filename = $value; + } + + if (file_exists($this->_filename) and !is_writable($this->_filename)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("File '{$this->_filename}' is not writable"); + } + + $content = file_get_contents($value); + if (!$content) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Problem while reading file '$value'"); + } + + $decrypted = parent::filter($content); + $result = file_put_contents($this->_filename, $decrypted); + + if (!$result) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Problem while writing file '{$this->_filename}'"); + } + + return $this->_filename; + } +} diff --git a/libs/Zend/Filter/File/Encrypt.php b/libs/Zend/Filter/File/Encrypt.php new file mode 100644 index 0000000000000000000000000000000000000000..6570ce9a926835953da3e02479ea0111ba89856c --- /dev/null +++ b/libs/Zend/Filter/File/Encrypt.php @@ -0,0 +1,106 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Encrypt.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Encrypt + */ +// require_once 'Zend/Filter/Encrypt.php'; + +/** + * Encrypts a given file and stores the encrypted file content + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_File_Encrypt extends Zend_Filter_Encrypt +{ + /** + * New filename to set + * + * @var string + */ + protected $_filename; + + /** + * Returns the new filename where the content will be stored + * + * @return string + */ + public function getFilename() + { + return $this->_filename; + } + + /** + * Sets the new filename where the content will be stored + * + * @param string $filename (Optional) New filename to set + * @return Zend_Filter_File_Encryt + */ + public function setFilename($filename = null) + { + $this->_filename = $filename; + return $this; + } + + /** + * Defined by Zend_Filter_Interface + * + * Encrypts the file $value with the defined settings + * + * @param string $value Full path of file to change + * @return string The filename which has been set, or false when there were errors + */ + public function filter($value) + { + if (!file_exists($value)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("File '$value' not found"); + } + + if (!isset($this->_filename)) { + $this->_filename = $value; + } + + if (file_exists($this->_filename) and !is_writable($this->_filename)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("File '{$this->_filename}' is not writable"); + } + + $content = file_get_contents($value); + if (!$content) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Problem while reading file '$value'"); + } + + $encrypted = parent::filter($content); + $result = file_put_contents($this->_filename, $encrypted); + + if (!$result) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Problem while writing file '{$this->_filename}'"); + } + + return $this->_filename; + } +} diff --git a/libs/Zend/Filter/File/LowerCase.php b/libs/Zend/Filter/File/LowerCase.php new file mode 100644 index 0000000000000000000000000000000000000000..dc3d34444e0f0586abdf13e8146befcbf343766b --- /dev/null +++ b/libs/Zend/Filter/File/LowerCase.php @@ -0,0 +1,84 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: LowerCase.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_StringToLower + */ +// require_once 'Zend/Filter/StringToLower.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_File_LowerCase extends Zend_Filter_StringToLower +{ + /** + * Adds options to the filter at initiation + * + * @param string $options + */ + public function __construct($options = null) + { + if (!empty($options)) { + $this->setEncoding($options); + } + } + + /** + * Defined by Zend_Filter_Interface + * + * Does a lowercase on the content of the given file + * + * @param string $value Full path of file to change + * @return string The given $value + * @throws Zend_Filter_Exception + */ + public function filter($value) + { + if (!file_exists($value)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("File '$value' not found"); + } + + if (!is_writable($value)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("File '$value' is not writable"); + } + + $content = file_get_contents($value); + if (!$content) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Problem while reading file '$value'"); + } + + $content = parent::filter($content); + $result = file_put_contents($value, $content); + + if (!$result) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Problem while writing file '$value'"); + } + + return $value; + } +} diff --git a/libs/Zend/Filter/File/Rename.php b/libs/Zend/Filter/File/Rename.php new file mode 100644 index 0000000000000000000000000000000000000000..16aac70f7d490b8ad41f9877bb0243be020c6654 --- /dev/null +++ b/libs/Zend/Filter/File/Rename.php @@ -0,0 +1,304 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Rename.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_File_Rename implements Zend_Filter_Interface +{ + /** + * Internal array of array(source, target, overwrite) + */ + protected $_files = array(); + + /** + * Class constructor + * + * Options argument may be either a string, a Zend_Config object, or an array. + * If an array or Zend_Config object, it accepts the following keys: + * 'source' => Source filename or directory which will be renamed + * 'target' => Target filename or directory, the new name of the sourcefile + * 'overwrite' => Shall existing files be overwritten ? + * + * @param string|array $options Target file or directory to be renamed + * @param string $target Source filename or directory (deprecated) + * @param bool $overwrite Should existing files be overwritten (deprecated) + * @return void + */ + public function __construct($options) + { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } elseif (is_string($options)) { + $options = array('target' => $options); + } elseif (!is_array($options)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Invalid options argument provided to filter'); + } + + if (1 < func_num_args()) { + $argv = func_get_args(); + array_shift($argv); + $source = array_shift($argv); + $overwrite = false; + if (!empty($argv)) { + $overwrite = array_shift($argv); + } + $options['source'] = $source; + $options['overwrite'] = $overwrite; + } + + $this->setFile($options); + } + + /** + * Returns the files to rename and their new name and location + * + * @return array + */ + public function getFile() + { + return $this->_files; + } + + /** + * Sets a new file or directory as target, deleting existing ones + * + * Array accepts the following keys: + * 'source' => Source filename or directory which will be renamed + * 'target' => Target filename or directory, the new name of the sourcefile + * 'overwrite' => Shall existing files be overwritten ? + * + * @param string|array $options Old file or directory to be rewritten + * @return Zend_Filter_File_Rename + */ + public function setFile($options) + { + $this->_files = array(); + $this->addFile($options); + + return $this; + } + + /** + * Adds a new file or directory as target to the existing ones + * + * Array accepts the following keys: + * 'source' => Source filename or directory which will be renamed + * 'target' => Target filename or directory, the new name of the sourcefile + * 'overwrite' => Shall existing files be overwritten ? + * + * @param string|array $options Old file or directory to be rewritten + * @return Zend_Filter_File_Rename + */ + public function addFile($options) + { + if (is_string($options)) { + $options = array('target' => $options); + } elseif (!is_array($options)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception ('Invalid options to rename filter provided'); + } + + $this->_convertOptions($options); + + return $this; + } + + /** + * Returns only the new filename without moving it + * But existing files will be erased when the overwrite option is true + * + * @param string $value Full path of file to change + * @param boolean $source Return internal informations + * @return string The new filename which has been set + */ + public function getNewName($value, $source = false) + { + $file = $this->_getFileName($value); + if ($file['source'] == $file['target']) { + return $value; + } + + if (!file_exists($file['source'])) { + return $value; + } + + if (($file['overwrite'] == true) && (file_exists($file['target']))) { + unlink($file['target']); + } + + if (file_exists($file['target'])) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. It already exists.", $value)); + } + + if ($source) { + return $file; + } + + return $file['target']; + } + + /** + * Defined by Zend_Filter_Interface + * + * Renames the file $value to the new name set before + * Returns the file $value, removing all but digit characters + * + * @param string $value Full path of file to change + * @throws Zend_Filter_Exception + * @return string The new filename which has been set, or false when there were errors + */ + public function filter($value) + { + $file = $this->getNewName($value, true); + if (is_string($file)) { + return $file; + } + + $result = rename($file['source'], $file['target']); + + if ($result === true) { + return $file['target']; + } + + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. An error occured while processing the file.", $value)); + } + + /** + * Internal method for creating the file array + * Supports single and nested arrays + * + * @param array $options + * @return array + */ + protected function _convertOptions($options) { + $files = array(); + foreach ($options as $key => $value) { + if (is_array($value)) { + $this->_convertOptions($value); + continue; + } + + switch ($key) { + case "source": + $files['source'] = (string) $value; + break; + + case 'target' : + $files['target'] = (string) $value; + break; + + case 'overwrite' : + $files['overwrite'] = (boolean) $value; + break; + + default: + break; + } + } + + if (empty($files)) { + return $this; + } + + if (empty($files['source'])) { + $files['source'] = '*'; + } + + if (empty($files['target'])) { + $files['target'] = '*'; + } + + if (empty($files['overwrite'])) { + $files['overwrite'] = false; + } + + $found = false; + foreach ($this->_files as $key => $value) { + if ($value['source'] == $files['source']) { + $this->_files[$key] = $files; + $found = true; + } + } + + if (!$found) { + $count = count($this->_files); + $this->_files[$count] = $files; + } + + return $this; + } + + /** + * Internal method to resolve the requested source + * and return all other related parameters + * + * @param string $file Filename to get the informations for + * @return array + */ + protected function _getFileName($file) + { + $rename = array(); + foreach ($this->_files as $value) { + if ($value['source'] == '*') { + if (!isset($rename['source'])) { + $rename = $value; + $rename['source'] = $file; + } + } + + if ($value['source'] == $file) { + $rename = $value; + } + } + + if (!isset($rename['source'])) { + return $file; + } + + if (!isset($rename['target']) or ($rename['target'] == '*')) { + $rename['target'] = $rename['source']; + } + + if (is_dir($rename['target'])) { + $name = basename($rename['source']); + $last = $rename['target'][strlen($rename['target']) - 1]; + if (($last != '/') and ($last != '\\')) { + $rename['target'] .= DIRECTORY_SEPARATOR; + } + + $rename['target'] .= $name; + } + + return $rename; + } +} diff --git a/libs/Zend/Filter/File/UpperCase.php b/libs/Zend/Filter/File/UpperCase.php new file mode 100644 index 0000000000000000000000000000000000000000..636e26c8ff5e10519f163fd44118bacec1c67509 --- /dev/null +++ b/libs/Zend/Filter/File/UpperCase.php @@ -0,0 +1,84 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: UpperCase.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_StringToUpper + */ +// require_once 'Zend/Filter/StringToUpper.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_File_UpperCase extends Zend_Filter_StringToUpper +{ + /** + * Adds options to the filter at initiation + * + * @param string $options + */ + public function __construct($options = null) + { + if (!empty($options)) { + $this->setEncoding($options); + } + } + + /** + * Defined by Zend_Filter_Interface + * + * Does a lowercase on the content of the given file + * + * @param string $value Full path of file to change + * @return string The given $value + * @throws Zend_Filter_Exception + */ + public function filter($value) + { + if (!file_exists($value)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("File '$value' not found"); + } + + if (!is_writable($value)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("File '$value' is not writable"); + } + + $content = file_get_contents($value); + if (!$content) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Problem while reading file '$value'"); + } + + $content = parent::filter($content); + $result = file_put_contents($value, $content); + + if (!$result) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Problem while writing file '$value'"); + } + + return $value; + } +} diff --git a/libs/Zend/Filter/HtmlEntities.php b/libs/Zend/Filter/HtmlEntities.php new file mode 100644 index 0000000000000000000000000000000000000000..df7599927390127e6c5b82e47d9f6c9f6f2ead2f --- /dev/null +++ b/libs/Zend/Filter/HtmlEntities.php @@ -0,0 +1,202 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: HtmlEntities.php 21061 2010-02-15 21:56:54Z thomas $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_HtmlEntities implements Zend_Filter_Interface +{ + /** + * Corresponds to the second htmlentities() argument + * + * @var integer + */ + protected $_quoteStyle; + + /** + * Corresponds to the third htmlentities() argument + * + * @var string + */ + protected $_encoding; + + /** + * Corresponds to the forth htmlentities() argument + * + * @var unknown_type + */ + protected $_doubleQuote; + + /** + * Sets filter options + * + * @param integer|array $quoteStyle + * @param string $charSet + * @return void + */ + public function __construct($options = array()) + { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } else if (!is_array($options)) { + $options = func_get_args(); + $temp['quotestyle'] = array_shift($options); + if (!empty($options)) { + $temp['charset'] = array_shift($options); + } + + $options = $temp; + } + + if (!isset($options['quotestyle'])) { + $options['quotestyle'] = ENT_COMPAT; + } + + if (!isset($options['encoding'])) { + $options['encoding'] = 'UTF-8'; + } + if (isset($options['charset'])) { + $options['encoding'] = $options['charset']; + } + + if (!isset($options['doublequote'])) { + $options['doublequote'] = true; + } + + $this->setQuoteStyle($options['quotestyle']); + $this->setEncoding($options['encoding']); + $this->setDoubleQuote($options['doublequote']); + } + + /** + * Returns the quoteStyle option + * + * @return integer + */ + public function getQuoteStyle() + { + return $this->_quoteStyle; + } + + /** + * Sets the quoteStyle option + * + * @param integer $quoteStyle + * @return Zend_Filter_HtmlEntities Provides a fluent interface + */ + public function setQuoteStyle($quoteStyle) + { + $this->_quoteStyle = $quoteStyle; + return $this; + } + + + /** + * Get encoding + * + * @return string + */ + public function getEncoding() + { + return $this->_encoding; + } + + /** + * Set encoding + * + * @param string $value + * @return Zend_Filter_HtmlEntities + */ + public function setEncoding($value) + { + $this->_encoding = (string) $value; + return $this; + } + + /** + * Returns the charSet option + * + * Proxies to {@link getEncoding()} + * + * @return string + */ + public function getCharSet() + { + return $this->getEncoding(); + } + + /** + * Sets the charSet option + * + * Proxies to {@link setEncoding()} + * + * @param string $charSet + * @return Zend_Filter_HtmlEntities Provides a fluent interface + */ + public function setCharSet($charSet) + { + return $this->setEncoding($charSet); + } + + /** + * Returns the doubleQuote option + * + * @return boolean + */ + public function getDoubleQuote() + { + return $this->_doubleQuote; + } + + /** + * Sets the doubleQuote option + * + * @param boolean $doubleQuote + * @return Zend_Filter_HtmlEntities Provides a fluent interface + */ + public function setDoubleQuote($doubleQuote) + { + $this->_doubleQuote = (boolean) $doubleQuote; + return $this; + } + + /** + * Defined by Zend_Filter_Interface + * + * Returns the string $value, converting characters to their corresponding HTML entity + * equivalents where they exist + * + * @param string $value + * @return string + */ + public function filter($value) + { + return htmlentities((string) $value, $this->getQuoteStyle(), $this->getEncoding(), $this->getDoubleQuote()); + } +} diff --git a/libs/Zend/Filter/Inflector.php b/libs/Zend/Filter/Inflector.php new file mode 100644 index 0000000000000000000000000000000000000000..054017ec4d3c81f13b22c7a07a7ad81ee406a0de --- /dev/null +++ b/libs/Zend/Filter/Inflector.php @@ -0,0 +1,527 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Inflector.php 21372 2010-03-07 19:58:08Z thomas $ + */ + +/** + * @see Zend_Filter + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter.php'; + +/** + * @see Zend_Loader_PluginLoader + */ +// require_once 'Zend/Loader/PluginLoader.php'; + +/** + * Filter chain for string inflection + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Inflector implements Zend_Filter_Interface +{ + /** + * @var Zend_Loader_PluginLoader_Interface + */ + protected $_pluginLoader = null; + + /** + * @var string + */ + protected $_target = null; + + /** + * @var bool + */ + protected $_throwTargetExceptionsOn = true; + + /** + * @var string + */ + protected $_targetReplacementIdentifier = ':'; + + /** + * @var array + */ + protected $_rules = array(); + + /** + * Constructor + * + * @param string|array $options Options to set + */ + public function __construct($options = null) + { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } else if (!is_array($options)) { + $options = func_get_args(); + $temp = array(); + + if (!empty($options)) { + $temp['target'] = array_shift($options); + } + + if (!empty($options)) { + $temp['rules'] = array_shift($options); + } + + if (!empty($options)) { + $temp['throwTargetExceptionsOn'] = array_shift($options); + } + + if (!empty($options)) { + $temp['targetReplacementIdentifier'] = array_shift($options); + } + + $options = $temp; + } + + $this->setOptions($options); + } + + /** + * Retreive PluginLoader + * + * @return Zend_Loader_PluginLoader_Interface + */ + public function getPluginLoader() + { + if (!$this->_pluginLoader instanceof Zend_Loader_PluginLoader_Interface) { + $this->_pluginLoader = new Zend_Loader_PluginLoader(array('Zend_Filter_' => 'Zend/Filter/'), __CLASS__); + } + + return $this->_pluginLoader; + } + + /** + * Set PluginLoader + * + * @param Zend_Loader_PluginLoader_Interface $pluginLoader + * @return Zend_Filter_Inflector + */ + public function setPluginLoader(Zend_Loader_PluginLoader_Interface $pluginLoader) + { + $this->_pluginLoader = $pluginLoader; + return $this; + } + + /** + * Use Zend_Config object to set object state + * + * @deprecated Use setOptions() instead + * @param Zend_Config $config + * @return Zend_Filter_Inflector + */ + public function setConfig(Zend_Config $config) + { + return $this->setOptions($config); + } + + /** + * Set options + * + * @param array $options + * @return Zend_Filter_Inflector + */ + public function setOptions($options) { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } + + // Set Präfix Path + if (array_key_exists('filterPrefixPath', $options)) { + if (!is_scalar($options['filterPrefixPath'])) { + foreach ($options['filterPrefixPath'] as $prefix => $path) { + $this->addFilterPrefixPath($prefix, $path); + } + } + } + + if (array_key_exists('throwTargetExceptionsOn', $options)) { + $this->setThrowTargetExceptionsOn($options['throwTargetExceptionsOn']); + } + + if (array_key_exists('targetReplacementIdentifier', $options)) { + $this->setTargetReplacementIdentifier($options['targetReplacementIdentifier']); + } + + if (array_key_exists('target', $options)) { + $this->setTarget($options['target']); + } + + if (array_key_exists('rules', $options)) { + $this->addRules($options['rules']); + } + + return $this; + } + + /** + * Convienence method to add prefix and path to PluginLoader + * + * @param string $prefix + * @param string $path + * @return Zend_Filter_Inflector + */ + public function addFilterPrefixPath($prefix, $path) + { + $this->getPluginLoader()->addPrefixPath($prefix, $path); + return $this; + } + + /** + * Set Whether or not the inflector should throw an exception when a replacement + * identifier is still found within an inflected target. + * + * @param bool $throwTargetExceptions + * @return Zend_Filter_Inflector + */ + public function setThrowTargetExceptionsOn($throwTargetExceptionsOn) + { + $this->_throwTargetExceptionsOn = ($throwTargetExceptionsOn == true) ? true : false; + return $this; + } + + /** + * Will exceptions be thrown? + * + * @return bool + */ + public function isThrowTargetExceptionsOn() + { + return $this->_throwTargetExceptionsOn; + } + + /** + * Set the Target Replacement Identifier, by default ':' + * + * @param string $targetReplacementIdentifier + * @return Zend_Filter_Inflector + */ + public function setTargetReplacementIdentifier($targetReplacementIdentifier) + { + if ($targetReplacementIdentifier) { + $this->_targetReplacementIdentifier = (string) $targetReplacementIdentifier; + } + + return $this; + } + + /** + * Get Target Replacement Identifier + * + * @return string + */ + public function getTargetReplacementIdentifier() + { + return $this->_targetReplacementIdentifier; + } + + /** + * Set a Target + * ex: 'scripts/:controller/:action.:suffix' + * + * @param string + * @return Zend_Filter_Inflector + */ + public function setTarget($target) + { + $this->_target = (string) $target; + return $this; + } + + /** + * Retrieve target + * + * @return string + */ + public function getTarget() + { + return $this->_target; + } + + /** + * Set Target Reference + * + * @param reference $target + * @return Zend_Filter_Inflector + */ + public function setTargetReference(&$target) + { + $this->_target =& $target; + return $this; + } + + /** + * SetRules() is the same as calling addRules() with the exception that it + * clears the rules before adding them. + * + * @param array $rules + * @return Zend_Filter_Inflector + */ + public function setRules(Array $rules) + { + $this->clearRules(); + $this->addRules($rules); + return $this; + } + + /** + * AddRules(): multi-call to setting filter rules. + * + * If prefixed with a ":" (colon), a filter rule will be added. If not + * prefixed, a static replacement will be added. + * + * ex: + * array( + * ':controller' => array('CamelCaseToUnderscore','StringToLower'), + * ':action' => array('CamelCaseToUnderscore','StringToLower'), + * 'suffix' => 'phtml' + * ); + * + * @param array + * @return Zend_Filter_Inflector + */ + public function addRules(Array $rules) + { + $keys = array_keys($rules); + foreach ($keys as $spec) { + if ($spec[0] == ':') { + $this->addFilterRule($spec, $rules[$spec]); + } else { + $this->setStaticRule($spec, $rules[$spec]); + } + } + + return $this; + } + + /** + * Get rules + * + * By default, returns all rules. If a $spec is provided, will return those + * rules if found, false otherwise. + * + * @param string $spec + * @return array|false + */ + public function getRules($spec = null) + { + if (null !== $spec) { + $spec = $this->_normalizeSpec($spec); + if (isset($this->_rules[$spec])) { + return $this->_rules[$spec]; + } + return false; + } + + return $this->_rules; + } + + /** + * getRule() returns a rule set by setFilterRule(), a numeric index must be provided + * + * @param string $spec + * @param int $index + * @return Zend_Filter_Interface|false + */ + public function getRule($spec, $index) + { + $spec = $this->_normalizeSpec($spec); + if (isset($this->_rules[$spec]) && is_array($this->_rules[$spec])) { + if (isset($this->_rules[$spec][$index])) { + return $this->_rules[$spec][$index]; + } + } + return false; + } + + /** + * ClearRules() clears the rules currently in the inflector + * + * @return Zend_Filter_Inflector + */ + public function clearRules() + { + $this->_rules = array(); + return $this; + } + + /** + * Set a filtering rule for a spec. $ruleSet can be a string, Filter object + * or an array of strings or filter objects. + * + * @param string $spec + * @param array|string|Zend_Filter_Interface $ruleSet + * @return Zend_Filter_Inflector + */ + public function setFilterRule($spec, $ruleSet) + { + $spec = $this->_normalizeSpec($spec); + $this->_rules[$spec] = array(); + return $this->addFilterRule($spec, $ruleSet); + } + + /** + * Add a filter rule for a spec + * + * @param mixed $spec + * @param mixed $ruleSet + * @return void + */ + public function addFilterRule($spec, $ruleSet) + { + $spec = $this->_normalizeSpec($spec); + if (!isset($this->_rules[$spec])) { + $this->_rules[$spec] = array(); + } + + if (!is_array($ruleSet)) { + $ruleSet = array($ruleSet); + } + + if (is_string($this->_rules[$spec])) { + $temp = $this->_rules[$spec]; + $this->_rules[$spec] = array(); + $this->_rules[$spec][] = $temp; + } + + foreach ($ruleSet as $rule) { + $this->_rules[$spec][] = $this->_getRule($rule); + } + + return $this; + } + + /** + * Set a static rule for a spec. This is a single string value + * + * @param string $name + * @param string $value + * @return Zend_Filter_Inflector + */ + public function setStaticRule($name, $value) + { + $name = $this->_normalizeSpec($name); + $this->_rules[$name] = (string) $value; + return $this; + } + + /** + * Set Static Rule Reference. + * + * This allows a consuming class to pass a property or variable + * in to be referenced when its time to build the output string from the + * target. + * + * @param string $name + * @param mixed $reference + * @return Zend_Filter_Inflector + */ + public function setStaticRuleReference($name, &$reference) + { + $name = $this->_normalizeSpec($name); + $this->_rules[$name] =& $reference; + return $this; + } + + /** + * Inflect + * + * @param string|array $source + * @return string + */ + public function filter($source) + { + // clean source + foreach ( (array) $source as $sourceName => $sourceValue) { + $source[ltrim($sourceName, ':')] = $sourceValue; + } + + $pregQuotedTargetReplacementIdentifier = preg_quote($this->_targetReplacementIdentifier, '#'); + $processedParts = array(); + + foreach ($this->_rules as $ruleName => $ruleValue) { + if (isset($source[$ruleName])) { + if (is_string($ruleValue)) { + // overriding the set rule + $processedParts['#'.$pregQuotedTargetReplacementIdentifier.$ruleName.'#'] = str_replace('\\', '\\\\', $source[$ruleName]); + } elseif (is_array($ruleValue)) { + $processedPart = $source[$ruleName]; + foreach ($ruleValue as $ruleFilter) { + $processedPart = $ruleFilter->filter($processedPart); + } + $processedParts['#'.$pregQuotedTargetReplacementIdentifier.$ruleName.'#'] = str_replace('\\', '\\\\', $processedPart); + } + } elseif (is_string($ruleValue)) { + $processedParts['#'.$pregQuotedTargetReplacementIdentifier.$ruleName.'#'] = str_replace('\\', '\\\\', $ruleValue); + } + } + + // all of the values of processedParts would have been str_replace('\\', '\\\\', ..)'d to disable preg_replace backreferences + $inflectedTarget = preg_replace(array_keys($processedParts), array_values($processedParts), $this->_target); + + if ($this->_throwTargetExceptionsOn && (preg_match('#(?='.$pregQuotedTargetReplacementIdentifier.'[A-Za-z]{1})#', $inflectedTarget) == true)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('A replacement identifier ' . $this->_targetReplacementIdentifier . ' was found inside the inflected target, perhaps a rule was not satisfied with a target source? Unsatisfied inflected target: ' . $inflectedTarget); + } + + return $inflectedTarget; + } + + /** + * Normalize spec string + * + * @param string $spec + * @return string + */ + protected function _normalizeSpec($spec) + { + return ltrim((string) $spec, ':&'); + } + + /** + * Resolve named filters and convert them to filter objects. + * + * @param string $rule + * @return Zend_Filter_Interface + */ + protected function _getRule($rule) + { + if ($rule instanceof Zend_Filter_Interface) { + return $rule; + } + + $rule = (string) $rule; + + $className = $this->getPluginLoader()->load($rule); + $ruleObject = new $className(); + if (!$ruleObject instanceof Zend_Filter_Interface) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('No class named ' . $rule . ' implementing Zend_Filter_Interface could be found'); + } + + return $ruleObject; + } +} diff --git a/libs/Zend/Filter/Input.php b/libs/Zend/Filter/Input.php new file mode 100644 index 0000000000000000000000000000000000000000..6db594d7558a44a816cdcad60f81c1825f3769c0 --- /dev/null +++ b/libs/Zend/Filter/Input.php @@ -0,0 +1,1126 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Input.php 22473 2010-06-20 08:30:04Z thomas $ + */ + +/** + * @see Zend_Loader + */ +// require_once 'Zend/Loader.php'; + +/** + * @see Zend_Filter + */ +// require_once 'Zend/Filter.php'; + +/** + * @see Zend_Validate + */ +// require_once 'Zend/Validate.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Input +{ + + const ALLOW_EMPTY = 'allowEmpty'; + const BREAK_CHAIN = 'breakChainOnFailure'; + const DEFAULT_VALUE = 'default'; + const MESSAGES = 'messages'; + const ESCAPE_FILTER = 'escapeFilter'; + const FIELDS = 'fields'; + const FILTER = 'filter'; + const FILTER_CHAIN = 'filterChain'; + const MISSING_MESSAGE = 'missingMessage'; + const INPUT_NAMESPACE = 'inputNamespace'; + const VALIDATOR_NAMESPACE = 'validatorNamespace'; + const FILTER_NAMESPACE = 'filterNamespace'; + const NOT_EMPTY_MESSAGE = 'notEmptyMessage'; + const PRESENCE = 'presence'; + const PRESENCE_OPTIONAL = 'optional'; + const PRESENCE_REQUIRED = 'required'; + const RULE = 'rule'; + const RULE_WILDCARD = '*'; + const VALIDATE = 'validate'; + const VALIDATOR = 'validator'; + const VALIDATOR_CHAIN = 'validatorChain'; + const VALIDATOR_CHAIN_COUNT = 'validatorChainCount'; + + /** + * @var array Input data, before processing. + */ + protected $_data = array(); + + /** + * @var array Association of rules to filters. + */ + protected $_filterRules = array(); + + /** + * @var array Association of rules to validators. + */ + protected $_validatorRules = array(); + + /** + * @var array After processing data, this contains mapping of valid fields + * to field values. + */ + protected $_validFields = array(); + + /** + * @var array After processing data, this contains mapping of validation + * rules that did not pass validation to the array of messages returned + * by the validator chain. + */ + protected $_invalidMessages = array(); + + /** + * @var array After processing data, this contains mapping of validation + * rules that did not pass validation to the array of error identifiers + * returned by the validator chain. + */ + protected $_invalidErrors = array(); + + /** + * @var array After processing data, this contains mapping of validation + * rules in which some fields were missing to the array of messages + * indicating which fields were missing. + */ + protected $_missingFields = array(); + + /** + * @var array After processing, this contains a copy of $_data elements + * that were not mentioned in any validation rule. + */ + protected $_unknownFields = array(); + + /** + * @var Zend_Filter_Interface The filter object that is run on values + * returned by the getEscaped() method. + */ + protected $_defaultEscapeFilter = null; + + /** + * Plugin loaders + * @var array + */ + protected $_loaders = array(); + + /** + * @var array Default values to use when processing filters and validators. + */ + protected $_defaults = array( + self::ALLOW_EMPTY => false, + self::BREAK_CHAIN => false, + self::ESCAPE_FILTER => 'HtmlEntities', + self::MISSING_MESSAGE => "Field '%field%' is required by rule '%rule%', but the field is missing", + self::NOT_EMPTY_MESSAGE => "You must give a non-empty value for field '%field%'", + self::PRESENCE => self::PRESENCE_OPTIONAL + ); + + /** + * @var boolean Set to False initially, this is set to True after the + * input data have been processed. Reset to False in setData() method. + */ + protected $_processed = false; + + /** + * Translation object + * @var Zend_Translate + */ + protected $_translator; + + /** + * Is translation disabled? + * @var Boolean + */ + protected $_translatorDisabled = false; + + /** + * @param array $filterRules + * @param array $validatorRules + * @param array $data OPTIONAL + * @param array $options OPTIONAL + */ + public function __construct($filterRules, $validatorRules, array $data = null, array $options = null) + { + if ($options) { + $this->setOptions($options); + } + + $this->_filterRules = (array) $filterRules; + $this->_validatorRules = (array) $validatorRules; + + if ($data) { + $this->setData($data); + } + } + + /** + * @param mixed $namespaces + * @return Zend_Filter_Input + * @deprecated since 1.5.0RC1 - use addFilterPrefixPath() or addValidatorPrefixPath instead. + */ + public function addNamespace($namespaces) + { + if (!is_array($namespaces)) { + $namespaces = array($namespaces); + } + + foreach ($namespaces as $namespace) { + $prefix = $namespace; + $path = str_replace('_', DIRECTORY_SEPARATOR, $prefix); + $this->addFilterPrefixPath($prefix, $path); + $this->addValidatorPrefixPath($prefix, $path); + } + + return $this; + } + + /** + * Add prefix path for all elements + * + * @param string $prefix + * @param string $path + * @return Zend_Filter_Input + */ + public function addFilterPrefixPath($prefix, $path) + { + $this->getPluginLoader(self::FILTER)->addPrefixPath($prefix, $path); + + return $this; + } + + /** + * Add prefix path for all elements + * + * @param string $prefix + * @param string $path + * @return Zend_Filter_Input + */ + public function addValidatorPrefixPath($prefix, $path) + { + $this->getPluginLoader(self::VALIDATE)->addPrefixPath($prefix, $path); + + return $this; + } + + /** + * Set plugin loaders for use with decorators and elements + * + * @param Zend_Loader_PluginLoader_Interface $loader + * @param string $type 'filter' or 'validate' + * @return Zend_Filter_Input + * @throws Zend_Filter_Exception on invalid type + */ + public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type) + { + $type = strtolower($type); + switch ($type) { + case self::FILTER: + case self::VALIDATE: + $this->_loaders[$type] = $loader; + return $this; + default: + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type)); + } + + return $this; + } + + /** + * Retrieve plugin loader for given type + * + * $type may be one of: + * - filter + * - validator + * + * If a plugin loader does not exist for the given type, defaults are + * created. + * + * @param string $type 'filter' or 'validate' + * @return Zend_Loader_PluginLoader_Interface + * @throws Zend_Filter_Exception on invalid type + */ + public function getPluginLoader($type) + { + $type = strtolower($type); + if (!isset($this->_loaders[$type])) { + switch ($type) { + case self::FILTER: + $prefixSegment = 'Zend_Filter_'; + $pathSegment = 'Zend/Filter/'; + break; + case self::VALIDATE: + $prefixSegment = 'Zend_Validate_'; + $pathSegment = 'Zend/Validate/'; + break; + default: + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type)); + } + + // require_once 'Zend/Loader/PluginLoader.php'; + $this->_loaders[$type] = new Zend_Loader_PluginLoader( + array($prefixSegment => $pathSegment) + ); + } + + return $this->_loaders[$type]; + } + + /** + * @return array + */ + public function getMessages() + { + $this->_process(); + return array_merge($this->_invalidMessages, $this->_missingFields); + } + + /** + * @return array + */ + public function getErrors() + { + $this->_process(); + return $this->_invalidErrors; + } + + /** + * @return array + */ + public function getInvalid() + { + $this->_process(); + return $this->_invalidMessages; + } + + /** + * @return array + */ + public function getMissing() + { + $this->_process(); + return $this->_missingFields; + } + + /** + * @return array + */ + public function getUnknown() + { + $this->_process(); + return $this->_unknownFields; + } + + /** + * @param string $fieldName OPTIONAL + * @return mixed + */ + public function getEscaped($fieldName = null) + { + $this->_process(); + $this->_getDefaultEscapeFilter(); + + if ($fieldName === null) { + return $this->_escapeRecursive($this->_validFields); + } + if (array_key_exists($fieldName, $this->_validFields)) { + return $this->_escapeRecursive($this->_validFields[$fieldName]); + } + return null; + } + + /** + * @param mixed $value + * @return mixed + */ + protected function _escapeRecursive($data) + { + if($data === null) { + return $data; + } + + if (!is_array($data)) { + return $this->_getDefaultEscapeFilter()->filter($data); + } + foreach ($data as &$element) { + $element = $this->_escapeRecursive($element); + } + return $data; + } + + /** + * @param string $fieldName OPTIONAL + * @return mixed + */ + public function getUnescaped($fieldName = null) + { + $this->_process(); + if ($fieldName === null) { + return $this->_validFields; + } + if (array_key_exists($fieldName, $this->_validFields)) { + return $this->_validFields[$fieldName]; + } + return null; + } + + /** + * @param string $fieldName + * @return mixed + */ + public function __get($fieldName) + { + return $this->getEscaped($fieldName); + } + + /** + * @return boolean + */ + public function hasInvalid() + { + $this->_process(); + return !(empty($this->_invalidMessages)); + } + + /** + * @return boolean + */ + public function hasMissing() + { + $this->_process(); + return !(empty($this->_missingFields)); + } + + /** + * @return boolean + */ + public function hasUnknown() + { + $this->_process(); + return !(empty($this->_unknownFields)); + } + + /** + * @return boolean + */ + public function hasValid() + { + $this->_process(); + return !(empty($this->_validFields)); + } + + /** + * @param string $fieldName + * @return boolean + */ + public function isValid($fieldName = null) + { + $this->_process(); + if ($fieldName === null) { + return !($this->hasMissing() || $this->hasInvalid()); + } + return array_key_exists($fieldName, $this->_validFields); + } + + /** + * @param string $fieldName + * @return boolean + */ + public function __isset($fieldName) + { + $this->_process(); + return isset($this->_validFields[$fieldName]); + } + + /** + * @return Zend_Filter_Input + * @throws Zend_Filter_Exception + */ + public function process() + { + $this->_process(); + if ($this->hasInvalid()) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Input has invalid fields"); + } + if ($this->hasMissing()) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Input has missing fields"); + } + + return $this; + } + + /** + * @param array $data + * @return Zend_Filter_Input + */ + public function setData(array $data) + { + $this->_data = $data; + + /** + * Reset to initial state + */ + $this->_validFields = array(); + $this->_invalidMessages = array(); + $this->_invalidErrors = array(); + $this->_missingFields = array(); + $this->_unknownFields = array(); + + $this->_processed = false; + + return $this; + } + + /** + * @param mixed $escapeFilter + * @return Zend_Filter_Interface + */ + public function setDefaultEscapeFilter($escapeFilter) + { + if (is_string($escapeFilter) || is_array($escapeFilter)) { + $escapeFilter = $this->_getFilter($escapeFilter); + } + if (!$escapeFilter instanceof Zend_Filter_Interface) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Escape filter specified does not implement Zend_Filter_Interface'); + } + $this->_defaultEscapeFilter = $escapeFilter; + return $escapeFilter; + } + + /** + * @param array $options + * @return Zend_Filter_Input + * @throws Zend_Filter_Exception if an unknown option is given + */ + public function setOptions(array $options) + { + foreach ($options as $option => $value) { + switch ($option) { + case self::ESCAPE_FILTER: + $this->setDefaultEscapeFilter($value); + break; + case self::INPUT_NAMESPACE: + $this->addNamespace($value); + break; + case self::VALIDATOR_NAMESPACE: + if(is_string($value)) { + $value = array($value); + } + + foreach($value AS $prefix) { + $this->addValidatorPrefixPath( + $prefix, + str_replace('_', DIRECTORY_SEPARATOR, $prefix) + ); + } + break; + case self::FILTER_NAMESPACE: + if(is_string($value)) { + $value = array($value); + } + + foreach($value AS $prefix) { + $this->addFilterPrefixPath( + $prefix, + str_replace('_', DIRECTORY_SEPARATOR, $prefix) + ); + } + break; + case self::ALLOW_EMPTY: + case self::BREAK_CHAIN: + case self::MISSING_MESSAGE: + case self::NOT_EMPTY_MESSAGE: + case self::PRESENCE: + $this->_defaults[$option] = $value; + break; + default: + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Unknown option '$option'"); + break; + } + } + + return $this; + } + + /** + * Set translation object + * + * @param Zend_Translate|Zend_Translate_Adapter|null $translator + * @return Zend_Filter_Input + */ + public function setTranslator($translator = null) + { + if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) { + $this->_translator = $translator; + } elseif ($translator instanceof Zend_Translate) { + $this->_translator = $translator->getAdapter(); + } else { + // require_once 'Zend/Validate/Exception.php'; + throw new Zend_Validate_Exception('Invalid translator specified'); + } + + return $this; + } + + /** + * Return translation object + * + * @return Zend_Translate_Adapter|null + */ + public function getTranslator() + { + if ($this->translatorIsDisabled()) { + return null; + } + + if ($this->_translator === null) { + // require_once 'Zend/Registry.php'; + if (Zend_Registry::isRegistered('Zend_Translate')) { + $translator = Zend_Registry::get('Zend_Translate'); + if ($translator instanceof Zend_Translate_Adapter) { + return $translator; + } elseif ($translator instanceof Zend_Translate) { + return $translator->getAdapter(); + } + } + } + + return $this->_translator; + } + + /** + * Indicate whether or not translation should be disabled + * + * @param bool $flag + * @return Zend_Filter_Input + */ + public function setDisableTranslator($flag) + { + $this->_translatorDisabled = (bool) $flag; + return $this; + } + + /** + * Is translation disabled? + * + * @return bool + */ + public function translatorIsDisabled() + { + return $this->_translatorDisabled; + } + + /* + * Protected methods + */ + + /** + * @return void + */ + protected function _filter() + { + foreach ($this->_filterRules as $ruleName => &$filterRule) { + /** + * Make sure we have an array representing this filter chain. + * Don't typecast to (array) because it might be a Zend_Filter object + */ + if (!is_array($filterRule)) { + $filterRule = array($filterRule); + } + + /** + * Filters are indexed by integer, metacommands are indexed by string. + * Pick out the filters. + */ + $filterList = array(); + foreach ($filterRule as $key => $value) { + if (is_int($key)) { + $filterList[] = $value; + } + } + + /** + * Use defaults for filter metacommands. + */ + $filterRule[self::RULE] = $ruleName; + if (!isset($filterRule[self::FIELDS])) { + $filterRule[self::FIELDS] = $ruleName; + } + + /** + * Load all the filter classes and add them to the chain. + */ + if (!isset($filterRule[self::FILTER_CHAIN])) { + $filterRule[self::FILTER_CHAIN] = new Zend_Filter(); + foreach ($filterList as $filter) { + if (is_string($filter) || is_array($filter)) { + $filter = $this->_getFilter($filter); + } + $filterRule[self::FILTER_CHAIN]->addFilter($filter); + } + } + + /** + * If the ruleName is the special wildcard rule, + * then apply the filter chain to all input data. + * Else just process the field named by the rule. + */ + if ($ruleName == self::RULE_WILDCARD) { + foreach (array_keys($this->_data) as $field) { + $this->_filterRule(array_merge($filterRule, array(self::FIELDS => $field))); + } + } else { + $this->_filterRule($filterRule); + } + } + } + + /** + * @param array $filterRule + * @return void + */ + protected function _filterRule(array $filterRule) + { + $field = $filterRule[self::FIELDS]; + if (!array_key_exists($field, $this->_data)) { + return; + } + if (is_array($this->_data[$field])) { + foreach ($this->_data[$field] as $key => $value) { + $this->_data[$field][$key] = $filterRule[self::FILTER_CHAIN]->filter($value); + } + } else { + $this->_data[$field] = + $filterRule[self::FILTER_CHAIN]->filter($this->_data[$field]); + } + } + + /** + * @return Zend_Filter_Interface + */ + protected function _getDefaultEscapeFilter() + { + if ($this->_defaultEscapeFilter !== null) { + return $this->_defaultEscapeFilter; + } + return $this->setDefaultEscapeFilter($this->_defaults[self::ESCAPE_FILTER]); + } + + /** + * @param string $rule + * @param string $field + * @return string + */ + protected function _getMissingMessage($rule, $field) + { + $message = $this->_defaults[self::MISSING_MESSAGE]; + + if (null !== ($translator = $this->getTranslator())) { + if ($translator->isTranslated(self::MISSING_MESSAGE)) { + $message = $translator->translate(self::MISSING_MESSAGE); + } else { + $message = $translator->translate($message); + } + } + + $message = str_replace('%rule%', $rule, $message); + $message = str_replace('%field%', $field, $message); + return $message; + } + + /** + * @return string + */ + protected function _getNotEmptyMessage($rule, $field) + { + $message = $this->_defaults[self::NOT_EMPTY_MESSAGE]; + + if (null !== ($translator = $this->getTranslator())) { + if ($translator->isTranslated(self::NOT_EMPTY_MESSAGE)) { + $message = $translator->translate(self::NOT_EMPTY_MESSAGE); + } else { + $message = $translator->translate($message); + } + } + + $message = str_replace('%rule%', $rule, $message); + $message = str_replace('%field%', $field, $message); + return $message; + } + + /** + * @return void + */ + protected function _process() + { + if ($this->_processed === false) { + $this->_filter(); + $this->_validate(); + $this->_processed = true; + } + } + + /** + * @return void + */ + protected function _validate() + { + /** + * Special case: if there are no validators, treat all fields as valid. + */ + if (!$this->_validatorRules) { + $this->_validFields = $this->_data; + $this->_data = array(); + return; + } + + foreach ($this->_validatorRules as $ruleName => &$validatorRule) { + /** + * Make sure we have an array representing this validator chain. + * Don't typecast to (array) because it might be a Zend_Validate object + */ + if (!is_array($validatorRule)) { + $validatorRule = array($validatorRule); + } + + /** + * Validators are indexed by integer, metacommands are indexed by string. + * Pick out the validators. + */ + $validatorList = array(); + foreach ($validatorRule as $key => $value) { + if (is_int($key)) { + $validatorList[$key] = $value; + } + } + + /** + * Use defaults for validation metacommands. + */ + $validatorRule[self::RULE] = $ruleName; + if (!isset($validatorRule[self::FIELDS])) { + $validatorRule[self::FIELDS] = $ruleName; + } + if (!isset($validatorRule[self::BREAK_CHAIN])) { + $validatorRule[self::BREAK_CHAIN] = $this->_defaults[self::BREAK_CHAIN]; + } + if (!isset($validatorRule[self::PRESENCE])) { + $validatorRule[self::PRESENCE] = $this->_defaults[self::PRESENCE]; + } + if (!isset($validatorRule[self::ALLOW_EMPTY])) { + $validatorRule[self::ALLOW_EMPTY] = $this->_defaults[self::ALLOW_EMPTY]; + } + + if (!isset($validatorRule[self::MESSAGES])) { + $validatorRule[self::MESSAGES] = array(); + } else if (!is_array($validatorRule[self::MESSAGES])) { + $validatorRule[self::MESSAGES] = array($validatorRule[self::MESSAGES]); + } else if (array_intersect_key($validatorList, $validatorRule[self::MESSAGES])) { + // There are now corresponding numeric keys in the validation rule messages array + // Treat it as a named messages list for all rule validators + $unifiedMessages = $validatorRule[self::MESSAGES]; + $validatorRule[self::MESSAGES] = array(); + + foreach ($validatorList as $key => $validator) { + if (array_key_exists($key, $unifiedMessages)) { + $validatorRule[self::MESSAGES][$key] = $unifiedMessages[$key]; + } + } + } + + /** + * Load all the validator classes and add them to the chain. + */ + if (!isset($validatorRule[self::VALIDATOR_CHAIN])) { + $validatorRule[self::VALIDATOR_CHAIN] = new Zend_Validate(); + + foreach ($validatorList as $key => $validator) { + if (is_string($validator) || is_array($validator)) { + $validator = $this->_getValidator($validator); + } + + if (isset($validatorRule[self::MESSAGES][$key])) { + $value = $validatorRule[self::MESSAGES][$key]; + if (is_array($value)) { + $validator->setMessages($value); + } else { + $validator->setMessage($value); + } + + if ($validator instanceof Zend_Validate_NotEmpty) { + $this->_defaults[self::NOT_EMPTY_MESSAGE] = $value; + } + } + + $validatorRule[self::VALIDATOR_CHAIN]->addValidator($validator, $validatorRule[self::BREAK_CHAIN]); + } + $validatorRule[self::VALIDATOR_CHAIN_COUNT] = count($validatorList); + } + + /** + * If the ruleName is the special wildcard rule, + * then apply the validator chain to all input data. + * Else just process the field named by the rule. + */ + if ($ruleName == self::RULE_WILDCARD) { + foreach (array_keys($this->_data) as $field) { + $this->_validateRule(array_merge($validatorRule, array(self::FIELDS => $field))); + } + } else { + $this->_validateRule($validatorRule); + } + } + + /** + * Unset fields in $_data that have been added to other arrays. + * We have to wait until all rules have been processed because + * a given field may be referenced by multiple rules. + */ + foreach (array_merge(array_keys($this->_missingFields), array_keys($this->_invalidMessages)) as $rule) { + foreach ((array) $this->_validatorRules[$rule][self::FIELDS] as $field) { + unset($this->_data[$field]); + } + } + foreach ($this->_validFields as $field => $value) { + unset($this->_data[$field]); + } + + /** + * Anything left over in $_data is an unknown field. + */ + $this->_unknownFields = $this->_data; + } + + /** + * @param array $validatorRule + * @return void + */ + protected function _validateRule(array $validatorRule) + { + /** + * Get one or more data values from input, and check for missing fields. + * Apply defaults if fields are missing. + */ + $data = array(); + foreach ((array) $validatorRule[self::FIELDS] as $key => $field) { + if (array_key_exists($field, $this->_data)) { + $data[$field] = $this->_data[$field]; + } else if (isset($validatorRule[self::DEFAULT_VALUE])) { + /** @todo according to this code default value can't be an array. It has to be reviewed */ + if (!is_array($validatorRule[self::DEFAULT_VALUE])) { + // Default value is a scalar + $data[$field] = $validatorRule[self::DEFAULT_VALUE]; + } else { + // Default value is an array. Search for corresponding key + if (isset($validatorRule[self::DEFAULT_VALUE][$key])) { + $data[$field] = $validatorRule[self::DEFAULT_VALUE][$key]; + } else if ($validatorRule[self::PRESENCE] == self::PRESENCE_REQUIRED) { + // Default value array is provided, but it doesn't have an entry for current field + // and presence is required + $this->_missingFields[$validatorRule[self::RULE]][] = + $this->_getMissingMessage($validatorRule[self::RULE], $field); + } + } + } else if ($validatorRule[self::PRESENCE] == self::PRESENCE_REQUIRED) { + $this->_missingFields[$validatorRule[self::RULE]][] = + $this->_getMissingMessage($validatorRule[self::RULE], $field); + } + } + + /** + * If any required fields are missing, break the loop. + */ + if (isset($this->_missingFields[$validatorRule[self::RULE]]) && count($this->_missingFields[$validatorRule[self::RULE]]) > 0) { + return; + } + + /** + * Evaluate the inputs against the validator chain. + */ + if (count((array) $validatorRule[self::FIELDS]) > 1) { + if (!$validatorRule[self::ALLOW_EMPTY]) { + $emptyFieldsFound = false; + $errorsList = array(); + $messages = array(); + + foreach ($data as $fieldKey => $field) { + $notEmptyValidator = $this->_getValidator('NotEmpty'); + $notEmptyValidator->setMessage($this->_getNotEmptyMessage($validatorRule[self::RULE], $fieldKey)); + + if (!$notEmptyValidator->isValid($field)) { + foreach ($notEmptyValidator->getMessages() as $messageKey => $message) { + if (!isset($messages[$messageKey])) { + $messages[$messageKey] = $message; + } else { + $messages[] = $message; + } + } + $errorsList[] = $notEmptyValidator->getErrors(); + $emptyFieldsFound = true; + } + } + + if ($emptyFieldsFound) { + $this->_invalidMessages[$validatorRule[self::RULE]] = $messages; + $this->_invalidErrors[$validatorRule[self::RULE]] = array_unique(call_user_func_array('array_merge', $errorsList)); + return; + } + } + + if (!$validatorRule[self::VALIDATOR_CHAIN]->isValid($data)) { + $this->_invalidMessages[$validatorRule[self::RULE]] = $validatorRule[self::VALIDATOR_CHAIN]->getMessages(); + $this->_invalidErrors[$validatorRule[self::RULE]] = $validatorRule[self::VALIDATOR_CHAIN]->getErrors(); + return; + } + } else if (count($data) > 0) { + // $data is actually a one element array + $fieldNames = array_keys($data); + $fieldName = reset($fieldNames); + $field = reset($data); + + $failed = false; + if (!is_array($field)) { + $field = array($field); + } + + $notEmptyValidator = $this->_getValidator('NotEmpty'); + $notEmptyValidator->setMessage($this->_getNotEmptyMessage($validatorRule[self::RULE], $fieldName)); + if ($validatorRule[self::ALLOW_EMPTY]) { + $validatorChain = $validatorRule[self::VALIDATOR_CHAIN]; + } else { + $validatorChain = new Zend_Validate(); + $validatorChain->addValidator($notEmptyValidator, true /* Always break on failure */); + $validatorChain->addValidator($validatorRule[self::VALIDATOR_CHAIN]); + } + + foreach ($field as $value) { + if ($validatorRule[self::ALLOW_EMPTY] && !$notEmptyValidator->isValid($value)) { + // Field is empty AND it's allowed. Do nothing. + continue; + } + + if (!$validatorChain->isValid($value)) { + if (isset($this->_invalidMessages[$validatorRule[self::RULE]])) { + $collectedMessages = $this->_invalidMessages[$validatorRule[self::RULE]]; + } else { + $collectedMessages = array(); + } + + foreach ($validatorChain->getMessages() as $messageKey => $message) { + if (!isset($collectedMessages[$messageKey])) { + $collectedMessages[$messageKey] = $message; + } else { + $collectedMessages[] = $message; + } + } + + $this->_invalidMessages[$validatorRule[self::RULE]] = $collectedMessages; + if (isset($this->_invalidErrors[$validatorRule[self::RULE]])) { + $this->_invalidErrors[$validatorRule[self::RULE]] = array_merge($this->_invalidErrors[$validatorRule[self::RULE]], + $validatorChain->getErrors()); + } else { + $this->_invalidErrors[$validatorRule[self::RULE]] = $validatorChain->getErrors(); + } + unset($this->_validFields[$fieldName]); + $failed = true; + if ($validatorRule[self::BREAK_CHAIN]) { + return; + } + } + } + if ($failed) { + return; + } + } + + /** + * If we got this far, the inputs for this rule pass validation. + */ + foreach ((array) $validatorRule[self::FIELDS] as $field) { + if (array_key_exists($field, $data)) { + $this->_validFields[$field] = $data[$field]; + } + } + } + + /** + * @param mixed $classBaseName + * @return Zend_Filter_Interface + */ + protected function _getFilter($classBaseName) + { + return $this->_getFilterOrValidator(self::FILTER, $classBaseName); + } + + /** + * @param mixed $classBaseName + * @return Zend_Validate_Interface + */ + protected function _getValidator($classBaseName) + { + return $this->_getFilterOrValidator(self::VALIDATE, $classBaseName); + } + + /** + * @param string $type + * @param mixed $classBaseName + * @return Zend_Filter_Interface|Zend_Validate_Interface + * @throws Zend_Filter_Exception + */ + protected function _getFilterOrValidator($type, $classBaseName) + { + $args = array(); + + if (is_array($classBaseName)) { + $args = $classBaseName; + $classBaseName = array_shift($args); + } + + $interfaceName = 'Zend_' . ucfirst($type) . '_Interface'; + $className = $this->getPluginLoader($type)->load(ucfirst($classBaseName)); + + $class = new ReflectionClass($className); + + if (!$class->implementsInterface($interfaceName)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("Class '$className' based on basename '$classBaseName' must implement the '$interfaceName' interface"); + } + + if ($class->hasMethod('__construct')) { + $object = $class->newInstanceArgs($args); + } else { + $object = $class->newInstance(); + } + + return $object; + } + +} diff --git a/libs/Zend/Filter/Int.php b/libs/Zend/Filter/Int.php new file mode 100644 index 0000000000000000000000000000000000000000..b4060ec0de6bc6e0de042cf039db9c661b231688 --- /dev/null +++ b/libs/Zend/Filter/Int.php @@ -0,0 +1,50 @@ +<?php + +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Int.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Int implements Zend_Filter_Interface +{ + /** + * Defined by Zend_Filter_Interface + * + * Returns (int) $value + * + * @param string $value + * @return integer + */ + public function filter($value) + { + return (int) ((string) $value); + } +} diff --git a/libs/Zend/Filter/Interface.php b/libs/Zend/Filter/Interface.php new file mode 100644 index 0000000000000000000000000000000000000000..c059967f77968db6dbb2754991aaa692bec63b8a --- /dev/null +++ b/libs/Zend/Filter/Interface.php @@ -0,0 +1,40 @@ +<?php + +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Interface.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +interface Zend_Filter_Interface +{ + /** + * Returns the result of filtering $value + * + * @param mixed $value + * @throws Zend_Filter_Exception If filtering $value is impossible + * @return mixed + */ + public function filter($value); +} diff --git a/libs/Zend/Filter/LocalizedToNormalized.php b/libs/Zend/Filter/LocalizedToNormalized.php new file mode 100644 index 0000000000000000000000000000000000000000..1d939975765ce2ce8c2a92a3aaf7f89ae41d943e --- /dev/null +++ b/libs/Zend/Filter/LocalizedToNormalized.php @@ -0,0 +1,112 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: LocalizedToNormalized.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + +/** + * @see Zend_Loader + */ +// require_once 'Zend/Locale/Format.php'; + +/** + * Normalizes given localized input + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_LocalizedToNormalized implements Zend_Filter_Interface +{ + /** + * Set options + * @var array + */ + protected $_options = array( + 'locale' => null, + 'date_format' => null, + 'precision' => null + ); + + /** + * Class constructor + * + * @param string|Zend_Locale $locale (Optional) Locale to set + */ + public function __construct($options = null) + { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } + + if (null !== $options) { + $this->setOptions($options); + } + } + + /** + * Returns the set options + * + * @return array + */ + public function getOptions() + { + return $this->_options; + } + + /** + * Sets options to use + * + * @param array $options (Optional) Options to use + * @return Zend_Filter_LocalizedToNormalized + */ + public function setOptions(array $options = null) + { + $this->_options = $options + $this->_options; + return $this; + } + + /** + * Defined by Zend_Filter_Interface + * + * Normalizes the given input + * + * @param string $value Value to normalized + * @return string|array The normalized value + */ + public function filter($value) + { + if (Zend_Locale_Format::isNumber($value, $this->_options)) { + return Zend_Locale_Format::getNumber($value, $this->_options); + } else if (($this->_options['date_format'] === null) && (strpos($value, ':') !== false)) { + // Special case, no date format specified, detect time input + return Zend_Locale_Format::getTime($value, $this->_options); + } else if (Zend_Locale_Format::checkDateFormat($value, $this->_options)) { + // Detect date or time input + return Zend_Locale_Format::getDate($value, $this->_options); + } + + return $value; + } +} diff --git a/libs/Zend/Filter/NormalizedToLocalized.php b/libs/Zend/Filter/NormalizedToLocalized.php new file mode 100644 index 0000000000000000000000000000000000000000..d623c3e00b50e61f2c5a20401b202e4d817ced4f --- /dev/null +++ b/libs/Zend/Filter/NormalizedToLocalized.php @@ -0,0 +1,111 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: NormalizedToLocalized.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + +/** + * @see Zend_Loader + */ +// require_once 'Zend/Locale/Format.php'; + +/** + * Localizes given normalized input + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_NormalizedToLocalized implements Zend_Filter_Interface +{ + /** + * Set options + */ + protected $_options = array( + 'locale' => null, + 'date_format' => null, + 'precision' => null + ); + + /** + * Class constructor + * + * @param string|Zend_Locale $locale (Optional) Locale to set + */ + public function __construct($options = null) + { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } + + if (null !== $options) { + $this->setOptions($options); + } + } + + /** + * Returns the set options + * + * @return array + */ + public function getOptions() + { + return $this->_options; + } + + /** + * Sets options to use + * + * @param array $options (Optional) Options to use + * @return Zend_Filter_LocalizedToNormalized + */ + public function setOptions(array $options = null) + { + $this->_options = $options + $this->_options; + return $this; + } + + /** + * Defined by Zend_Filter_Interface + * + * Normalizes the given input + * + * @param string $value Value to normalized + * @return string|array The normalized value + */ + public function filter($value) + { + if (is_array($value)) { + // require_once 'Zend/Date.php'; + $date = new Zend_Date($value, $this->_options['locale']); + return $date->toString($this->_options['date_format']); + } else if ($this->_options['precision'] === 0) { + return Zend_Locale_Format::toInteger($value, $this->_options); + } else if ($this->_options['precision'] === null) { + return Zend_Locale_Format::toFloat($value, $this->_options); + } + + return Zend_Locale_Format::toNumber($value, $this->_options); + } +} diff --git a/libs/Zend/Filter/Null.php b/libs/Zend/Filter/Null.php new file mode 100644 index 0000000000000000000000000000000000000000..6839dc45e1d5f0378bc5bfff08c480391eba505c --- /dev/null +++ b/libs/Zend/Filter/Null.php @@ -0,0 +1,183 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Null.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Null implements Zend_Filter_Interface +{ + const BOOLEAN = 1; + const INTEGER = 2; + const EMPTY_ARRAY = 4; + const STRING = 8; + const ZERO = 16; + const ALL = 31; + + protected $_constants = array( + self::BOOLEAN => 'boolean', + self::INTEGER => 'integer', + self::EMPTY_ARRAY => 'array', + self::STRING => 'string', + self::ZERO => 'zero', + self::ALL => 'all' + ); + + /** + * Internal type to detect + * + * @var integer + */ + protected $_type = self::ALL; + + /** + * Constructor + * + * @param string|array|Zend_Config $options OPTIONAL + */ + public function __construct($options = null) + { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } else if (!is_array($options)) { + $options = func_get_args(); + $temp = array(); + if (!empty($options)) { + $temp = array_shift($options); + } + $options = $temp; + } else if (is_array($options) && array_key_exists('type', $options)) { + $options = $options['type']; + } + + if (!empty($options)) { + $this->setType($options); + } + } + + /** + * Returns the set null types + * + * @return array + */ + public function getType() + { + return $this->_type; + } + + /** + * Set the null types + * + * @param integer|array $type + * @throws Zend_Filter_Exception + * @return Zend_Filter_Null + */ + public function setType($type = null) + { + if (is_array($type)) { + $detected = 0; + foreach($type as $value) { + if (is_int($value)) { + $detected += $value; + } else if (in_array($value, $this->_constants)) { + $detected += array_search($value, $this->_constants); + } + } + + $type = $detected; + } else if (is_string($type)) { + if (in_array($type, $this->_constants)) { + $type = array_search($type, $this->_constants); + } + } + + if (!is_int($type) || ($type < 0) || ($type > self::ALL)) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('Unknown type'); + } + + $this->_type = $type; + return $this; + } + + /** + * Defined by Zend_Filter_Interface + * + * Returns null representation of $value, if value is empty and matches + * types that should be considered null. + * + * @param string $value + * @return string + */ + public function filter($value) + { + $type = $this->getType(); + + // STRING ZERO ('0') + if ($type >= self::ZERO) { + $type -= self::ZERO; + if (is_string($value) && ($value == '0')) { + return null; + } + } + + // STRING ('') + if ($type >= self::STRING) { + $type -= self::STRING; + if (is_string($value) && ($value == '')) { + return null; + } + } + + // EMPTY_ARRAY (array()) + if ($type >= self::EMPTY_ARRAY) { + $type -= self::EMPTY_ARRAY; + if (is_array($value) && ($value == array())) { + return null; + } + } + + // INTEGER (0) + if ($type >= self::INTEGER) { + $type -= self::INTEGER; + if (is_int($value) && ($value == 0)) { + return null; + } + } + + // BOOLEAN (false) + if ($type >= self::BOOLEAN) { + $type -= self::BOOLEAN; + if (is_bool($value) && ($value == false)) { + return null; + } + } + + return $value; + } +} diff --git a/libs/Zend/Filter/PregReplace.php b/libs/Zend/Filter/PregReplace.php new file mode 100644 index 0000000000000000000000000000000000000000..fcde06fbaca4b7aa159c3d3939ea64ec59aadcc7 --- /dev/null +++ b/libs/Zend/Filter/PregReplace.php @@ -0,0 +1,174 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: PregReplace.php 21086 2010-02-18 21:10:39Z thomas $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_PregReplace implements Zend_Filter_Interface +{ + /** + * Pattern to match + * @var mixed + */ + protected $_matchPattern = null; + + /** + * Replacement pattern + * @var mixed + */ + protected $_replacement = ''; + + /** + * Is unicode enabled? + * + * @var bool + */ + static protected $_unicodeSupportEnabled = null; + + /** + * Is Unicode Support Enabled Utility function + * + * @return bool + */ + static public function isUnicodeSupportEnabled() + { + if (self::$_unicodeSupportEnabled === null) { + self::_determineUnicodeSupport(); + } + + return self::$_unicodeSupportEnabled; + } + + /** + * Method to cache the regex needed to determine if unicode support is available + * + * @return bool + */ + static protected function _determineUnicodeSupport() + { + self::$_unicodeSupportEnabled = (@preg_match('/\pL/u', 'a')) ? true : false; + } + + /** + * Constructor + * Supported options are + * 'match' => matching pattern + * 'replace' => replace with this + * + * @param string|array $options + * @return void + */ + public function __construct($options = null) + { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } else if (!is_array($options)) { + $options = func_get_args(); + $temp = array(); + if (!empty($options)) { + $temp['match'] = array_shift($options); + } + + if (!empty($options)) { + $temp['replace'] = array_shift($options); + } + + $options = $temp; + } + + if (array_key_exists('match', $options)) { + $this->setMatchPattern($options['match']); + } + + if (array_key_exists('replace', $options)) { + $this->setReplacement($options['replace']); + } + } + + /** + * Set the match pattern for the regex being called within filter() + * + * @param mixed $match - same as the first argument of preg_replace + * @return Zend_Filter_PregReplace + */ + public function setMatchPattern($match) + { + $this->_matchPattern = $match; + return $this; + } + + /** + * Get currently set match pattern + * + * @return string + */ + public function getMatchPattern() + { + return $this->_matchPattern; + } + + /** + * Set the Replacement pattern/string for the preg_replace called in filter + * + * @param mixed $replacement - same as the second argument of preg_replace + * @return Zend_Filter_PregReplace + */ + public function setReplacement($replacement) + { + $this->_replacement = $replacement; + return $this; + } + + /** + * Get currently set replacement value + * + * @return string + */ + public function getReplacement() + { + return $this->_replacement; + } + + /** + * Perform regexp replacement as filter + * + * @param string $value + * @return string + */ + public function filter($value) + { + if ($this->_matchPattern == null) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception(get_class($this) . ' does not have a valid MatchPattern set.'); + } + + return preg_replace($this->_matchPattern, $this->_replacement, $value); + } + +} diff --git a/libs/Zend/Filter/RealPath.php b/libs/Zend/Filter/RealPath.php new file mode 100644 index 0000000000000000000000000000000000000000..829535421d039d67885f19bbf35f1134ea972d44 --- /dev/null +++ b/libs/Zend/Filter/RealPath.php @@ -0,0 +1,134 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: RealPath.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_RealPath implements Zend_Filter_Interface +{ + /** + * @var boolean $_pathExists + */ + protected $_exists = true; + + /** + * Class constructor + * + * @param boolean|Zend_Config $options Options to set + */ + public function __construct($options = true) + { + $this->setExists($options); + } + + /** + * Returns true if the filtered path must exist + * + * @return boolean + */ + public function getExists() + { + return $this->_exists; + } + + /** + * Sets if the path has to exist + * TRUE when the path must exist + * FALSE when not existing paths can be given + * + * @param boolean|Zend_Config $exists Path must exist + * @return Zend_Filter_RealPath + */ + public function setExists($exists) + { + if ($exists instanceof Zend_Config) { + $exists = $exists->toArray(); + } + + if (is_array($exists)) { + if (isset($exists['exists'])) { + $exists = (boolean) $exists['exists']; + } + } + + $this->_exists = (boolean) $exists; + return $this; + } + + /** + * Defined by Zend_Filter_Interface + * + * Returns realpath($value) + * + * @param string $value + * @return string + */ + public function filter($value) + { + $path = (string) $value; + if ($this->_exists) { + return realpath($path); + } + + $realpath = @realpath($path); + if ($realpath) { + return $realpath; + } + + $drive = ''; + if (substr(PHP_OS, 0, 3) == 'WIN') { + $path = preg_replace('/[\\\\\/]/', DIRECTORY_SEPARATOR, $path); + if (preg_match('/([a-zA-Z]\:)(.*)/', $path, $matches)) { + list($fullMatch, $drive, $path) = $matches; + } else { + $cwd = getcwd(); + $drive = substr($cwd, 0, 2); + if (substr($path, 0, 1) != DIRECTORY_SEPARATOR) { + $path = substr($cwd, 3) . DIRECTORY_SEPARATOR . $path; + } + } + } elseif (substr($path, 0, 1) != DIRECTORY_SEPARATOR) { + $path = getcwd() . DIRECTORY_SEPARATOR . $path; + } + + $stack = array(); + $parts = explode(DIRECTORY_SEPARATOR, $path); + foreach ($parts as $dir) { + if (strlen($dir) && $dir !== '.') { + if ($dir == '..') { + array_pop($stack); + } else { + array_push($stack, $dir); + } + } + } + + return $drive . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $stack); + } +} diff --git a/libs/Zend/Filter/StringToLower.php b/libs/Zend/Filter/StringToLower.php new file mode 100644 index 0000000000000000000000000000000000000000..a780305d658d3f601a6d60759cce17e197860ef3 --- /dev/null +++ b/libs/Zend/Filter/StringToLower.php @@ -0,0 +1,117 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: StringToLower.php 20912 2010-02-04 19:44:42Z thomas $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_StringToLower implements Zend_Filter_Interface +{ + /** + * Encoding for the input string + * + * @var string + */ + protected $_encoding = null; + + /** + * Constructor + * + * @param string|array|Zend_Config $options OPTIONAL + */ + public function __construct($options = null) + { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } else if (!is_array($options)) { + $options = func_get_args(); + $temp = array(); + if (!empty($options)) { + $temp['encoding'] = array_shift($options); + } + $options = $temp; + } + + if (array_key_exists('encoding', $options)) { + $this->setEncoding($options['encoding']); + } + } + + /** + * Returns the set encoding + * + * @return string + */ + public function getEncoding() + { + return $this->_encoding; + } + + /** + * Set the input encoding for the given string + * + * @param string $encoding + * @return Zend_Filter_StringToLower Provides a fluent interface + * @throws Zend_Filter_Exception + */ + public function setEncoding($encoding = null) + { + if ($encoding !== null) { + if (!function_exists('mb_strtolower')) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('mbstring is required for this feature'); + } + + $encoding = (string) $encoding; + if (!in_array(strtolower($encoding), array_map('strtolower', mb_list_encodings()))) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("The given encoding '$encoding' is not supported by mbstring"); + } + } + + $this->_encoding = $encoding; + return $this; + } + + /** + * Defined by Zend_Filter_Interface + * + * Returns the string $value, converting characters to lowercase as necessary + * + * @param string $value + * @return string + */ + public function filter($value) + { + if ($this->_encoding !== null) { + return mb_strtolower((string) $value, $this->_encoding); + } + + return strtolower((string) $value); + } +} diff --git a/libs/Zend/Filter/StringToUpper.php b/libs/Zend/Filter/StringToUpper.php new file mode 100644 index 0000000000000000000000000000000000000000..f733dfc7df13af4cd150d4662d9830983c4d7307 --- /dev/null +++ b/libs/Zend/Filter/StringToUpper.php @@ -0,0 +1,117 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: StringToUpper.php 20912 2010-02-04 19:44:42Z thomas $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_StringToUpper implements Zend_Filter_Interface +{ + /** + * Encoding for the input string + * + * @var string + */ + protected $_encoding = null; + + /** + * Constructor + * + * @param string|array $options OPTIONAL + */ + public function __construct($options = null) + { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } else if (!is_array($options)) { + $options = func_get_args(); + $temp = array(); + if (!empty($options)) { + $temp['encoding'] = array_shift($options); + } + $options = $temp; + } + + if (array_key_exists('encoding', $options)) { + $this->setEncoding($options['encoding']); + } + } + + /** + * Returns the set encoding + * + * @return string + */ + public function getEncoding() + { + return $this->_encoding; + } + + /** + * Set the input encoding for the given string + * + * @param string $encoding + * @return Zend_Filter_StringToUpper Provides a fluent interface + * @throws Zend_Filter_Exception + */ + public function setEncoding($encoding = null) + { + if ($encoding !== null) { + if (!function_exists('mb_strtoupper')) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('mbstring is required for this feature'); + } + + $encoding = (string) $encoding; + if (!in_array(strtolower($encoding), array_map('strtolower', mb_list_encodings()))) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception("The given encoding '$encoding' is not supported by mbstring"); + } + } + + $this->_encoding = $encoding; + return $this; + } + + /** + * Defined by Zend_Filter_Interface + * + * Returns the string $value, converting characters to uppercase as necessary + * + * @param string $value + * @return string + */ + public function filter($value) + { + if ($this->_encoding) { + return mb_strtoupper((string) $value, $this->_encoding); + } + + return strtoupper((string) $value); + } +} diff --git a/libs/Zend/Filter/StringTrim.php b/libs/Zend/Filter/StringTrim.php new file mode 100644 index 0000000000000000000000000000000000000000..cfbc7ba41ab9851f5347e9b4bdfe1a4469b69965 --- /dev/null +++ b/libs/Zend/Filter/StringTrim.php @@ -0,0 +1,124 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: StringTrim.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_StringTrim implements Zend_Filter_Interface +{ + /** + * List of characters provided to the trim() function + * + * If this is null, then trim() is called with no specific character list, + * and its default behavior will be invoked, trimming whitespace. + * + * @var string|null + */ + protected $_charList; + + /** + * Sets filter options + * + * @param string|array|Zend_Config $charList + * @return void + */ + public function __construct($charList = null) + { + if ($charList instanceof Zend_Config) { + $charList = $charList->toArray(); + } else if (!is_array($charList)) { + $options = func_get_args(); + $temp['charlist'] = array_shift($options); + $options = $temp; + } + + if (array_key_exists('charlist', $options)) { + $this->setCharList($options['charlist']); + } + } + + /** + * Returns the charList option + * + * @return string|null + */ + public function getCharList() + { + return $this->_charList; + } + + /** + * Sets the charList option + * + * @param string|null $charList + * @return Zend_Filter_StringTrim Provides a fluent interface + */ + public function setCharList($charList) + { + $this->_charList = $charList; + return $this; + } + + /** + * Defined by Zend_Filter_Interface + * + * Returns the string $value with characters stripped from the beginning and end + * + * @param string $value + * @return string + */ + public function filter($value) + { + if (null === $this->_charList) { + return $this->_unicodeTrim((string) $value); + } else { + return $this->_unicodeTrim((string) $value, $this->_charList); + } + } + + /** + * Unicode aware trim method + * Fixes a PHP problem + * + * @param string $value + * @param string $charlist + * @return string + */ + protected function _unicodeTrim($value, $charlist = '\\\\s') + { + $chars = preg_replace( + array( '/[\^\-\]\\\]/S', '/\\\{4}/S', '/\//'), + array( '\\\\\\0', '\\', '\/' ), + $charlist + ); + + $pattern = '^[' . $chars . ']*|[' . $chars . ']*$'; + return preg_replace("/$pattern/sSD", '', $value); + } +} diff --git a/libs/Zend/Filter/StripNewlines.php b/libs/Zend/Filter/StripNewlines.php new file mode 100644 index 0000000000000000000000000000000000000000..840669240de0141ad19231b3a92e1804c4781d46 --- /dev/null +++ b/libs/Zend/Filter/StripNewlines.php @@ -0,0 +1,48 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: StripNewlines.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_StripNewlines implements Zend_Filter_Interface +{ + + /** + * Defined by Zend_Filter_Interface + * + * Returns $value without newline control characters + * + * @param string $value + * @return string + */ + public function filter ($value) + { + return str_replace(array("\n", "\r"), '', $value); + } +} diff --git a/libs/Zend/Filter/StripTags.php b/libs/Zend/Filter/StripTags.php new file mode 100644 index 0000000000000000000000000000000000000000..ebc213e60f0512fcc60b9653669773a3c626b3f0 --- /dev/null +++ b/libs/Zend/Filter/StripTags.php @@ -0,0 +1,345 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: StripTags.php 22174 2010-05-14 22:12:50Z thomas $ + */ + + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Interface.php'; + + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_StripTags implements Zend_Filter_Interface +{ + /** + * Unique ID prefix used for allowing comments + */ + const UNIQUE_ID_PREFIX = '__Zend_Filter_StripTags__'; + + /** + * Whether comments are allowed + * + * If false (the default), then comments are removed from the input string. + * + * This setting is now deprecated, and ignored internally. + * + * @deprecated + * @var boolean + */ + public $commentsAllowed = false; + + /** + * Array of allowed tags and allowed attributes for each allowed tag + * + * Tags are stored in the array keys, and the array values are themselves + * arrays of the attributes allowed for the corresponding tag. + * + * @var array + */ + protected $_tagsAllowed = array(); + + /** + * Array of allowed attributes for all allowed tags + * + * Attributes stored here are allowed for all of the allowed tags. + * + * @var array + */ + protected $_attributesAllowed = array(); + + /** + * Sets the filter options + * Allowed options are + * 'allowTags' => Tags which are allowed + * 'allowAttribs' => Attributes which are allowed + * 'allowComments' => Are comments allowed ? + * + * @param string|array|Zend_Config $options + * @return void + */ + public function __construct($options = null) + { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } else if ((!is_array($options)) || (is_array($options) && !array_key_exists('allowTags', $options) && + !array_key_exists('allowAttribs', $options) && !array_key_exists('allowComments', $options))) { + $options = func_get_args(); + $temp['allowTags'] = array_shift($options); + if (!empty($options)) { + $temp['allowAttribs'] = array_shift($options); + } + + if (!empty($options)) { + $temp['allowComments'] = array_shift($options); + } + + $options = $temp; + } + + if (array_key_exists('allowTags', $options)) { + $this->setTagsAllowed($options['allowTags']); + } + + if (array_key_exists('allowAttribs', $options)) { + $this->setAttributesAllowed($options['allowAttribs']); + } + + if (array_key_exists('allowComments', $options)) { + $this->setCommentsAllowed($options['allowComments']); + } + } + + /** + * Returns the commentsAllowed option + * + * This setting is now deprecated and ignored internally. + * + * @deprecated + * @return bool + */ + public function getCommentsAllowed() + { + return $this->commentsAllowed; + } + + /** + * Sets the commentsAllowed option + * + * This setting is now deprecated and ignored internally. + * + * @deprecated + * @param boolean $commentsAllowed + * @return Zend_Filter_StripTags Provides a fluent interface + */ + public function setCommentsAllowed($commentsAllowed) + { + $this->commentsAllowed = (boolean) $commentsAllowed; + return $this; + } + + /** + * Returns the tagsAllowed option + * + * @return array + */ + public function getTagsAllowed() + { + return $this->_tagsAllowed; + } + + /** + * Sets the tagsAllowed option + * + * @param array|string $tagsAllowed + * @return Zend_Filter_StripTags Provides a fluent interface + */ + public function setTagsAllowed($tagsAllowed) + { + if (!is_array($tagsAllowed)) { + $tagsAllowed = array($tagsAllowed); + } + + foreach ($tagsAllowed as $index => $element) { + // If the tag was provided without attributes + if (is_int($index) && is_string($element)) { + // Canonicalize the tag name + $tagName = strtolower($element); + // Store the tag as allowed with no attributes + $this->_tagsAllowed[$tagName] = array(); + } + // Otherwise, if a tag was provided with attributes + else if (is_string($index) && (is_array($element) || is_string($element))) { + // Canonicalize the tag name + $tagName = strtolower($index); + // Canonicalize the attributes + if (is_string($element)) { + $element = array($element); + } + // Store the tag as allowed with the provided attributes + $this->_tagsAllowed[$tagName] = array(); + foreach ($element as $attribute) { + if (is_string($attribute)) { + // Canonicalize the attribute name + $attributeName = strtolower($attribute); + $this->_tagsAllowed[$tagName][$attributeName] = null; + } + } + } + } + + return $this; + } + + /** + * Returns the attributesAllowed option + * + * @return array + */ + public function getAttributesAllowed() + { + return $this->_attributesAllowed; + } + + /** + * Sets the attributesAllowed option + * + * @param array|string $attributesAllowed + * @return Zend_Filter_StripTags Provides a fluent interface + */ + public function setAttributesAllowed($attributesAllowed) + { + if (!is_array($attributesAllowed)) { + $attributesAllowed = array($attributesAllowed); + } + + // Store each attribute as allowed + foreach ($attributesAllowed as $attribute) { + if (is_string($attribute)) { + // Canonicalize the attribute name + $attributeName = strtolower($attribute); + $this->_attributesAllowed[$attributeName] = null; + } + } + + return $this; + } + + /** + * Defined by Zend_Filter_Interface + * + * @todo improve docblock descriptions + * + * @param string $value + * @return string + */ + public function filter($value) + { + $value = (string) $value; + + // Strip HTML comments first + while (strpos($value, '<!--') !== false) { + $pos = strrpos($value, '<!--'); + $start = substr($value, 0, $pos); + $value = substr($value, $pos); + $value = preg_replace('/<(?:!(?:--[\s\S]*?--\s*)?(>))/us', '', $value); + $value = $start . $value; + } + + // Initialize accumulator for filtered data + $dataFiltered = ''; + // Parse the input data iteratively as regular pre-tag text followed by a + // tag; either may be empty strings + preg_match_all('/([^<]*)(<?[^>]*>?)/', (string) $value, $matches); + + // Iterate over each set of matches + foreach ($matches[1] as $index => $preTag) { + // If the pre-tag text is non-empty, strip any ">" characters from it + if (strlen($preTag)) { + $preTag = str_replace('>', '', $preTag); + } + // If a tag exists in this match, then filter the tag + $tag = $matches[2][$index]; + if (strlen($tag)) { + $tagFiltered = $this->_filterTag($tag); + } else { + $tagFiltered = ''; + } + // Add the filtered pre-tag text and filtered tag to the data buffer + $dataFiltered .= $preTag . $tagFiltered; + } + + // Return the filtered data + return $dataFiltered; + } + + /** + * Filters a single tag against the current option settings + * + * @param string $tag + * @return string + */ + protected function _filterTag($tag) + { + // Parse the tag into: + // 1. a starting delimiter (mandatory) + // 2. a tag name (if available) + // 3. a string of attributes (if available) + // 4. an ending delimiter (if available) + $isMatch = preg_match('~(</?)(\w*)((/(?!>)|[^/>])*)(/?>)~', $tag, $matches); + + // If the tag does not match, then strip the tag entirely + if (!$isMatch) { + return ''; + } + + // Save the matches to more meaningfully named variables + $tagStart = $matches[1]; + $tagName = strtolower($matches[2]); + $tagAttributes = $matches[3]; + $tagEnd = $matches[5]; + + // If the tag is not an allowed tag, then remove the tag entirely + if (!isset($this->_tagsAllowed[$tagName])) { + return ''; + } + + // Trim the attribute string of whitespace at the ends + $tagAttributes = trim($tagAttributes); + + // If there are non-whitespace characters in the attribute string + if (strlen($tagAttributes)) { + // Parse iteratively for well-formed attributes + preg_match_all('/(\w+)\s*=\s*(?:(")(.*?)"|(\')(.*?)\')/s', $tagAttributes, $matches); + + // Initialize valid attribute accumulator + $tagAttributes = ''; + + // Iterate over each matched attribute + foreach ($matches[1] as $index => $attributeName) { + $attributeName = strtolower($attributeName); + $attributeDelimiter = empty($matches[2][$index]) ? $matches[4][$index] : $matches[2][$index]; + $attributeValue = empty($matches[3][$index]) ? $matches[5][$index] : $matches[3][$index]; + + // If the attribute is not allowed, then remove it entirely + if (!array_key_exists($attributeName, $this->_tagsAllowed[$tagName]) + && !array_key_exists($attributeName, $this->_attributesAllowed)) { + continue; + } + // Add the attribute to the accumulator + $tagAttributes .= " $attributeName=" . $attributeDelimiter + . $attributeValue . $attributeDelimiter; + } + } + + // Reconstruct tags ending with "/>" as backwards-compatible XHTML tag + if (strpos($tagEnd, '/') !== false) { + $tagEnd = " $tagEnd"; + } + + // Return the filtered tag + return $tagStart . $tagName . $tagAttributes . $tagEnd; + } +} diff --git a/libs/Zend/Filter/Word/CamelCaseToDash.php b/libs/Zend/Filter/Word/CamelCaseToDash.php new file mode 100644 index 0000000000000000000000000000000000000000..838c372f424c6768e59ad0121a4c9bd91701fd72 --- /dev/null +++ b/libs/Zend/Filter/Word/CamelCaseToDash.php @@ -0,0 +1,44 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: CamelCaseToDash.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Word/CamelCaseToSeparator.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Word_CamelCaseToDash extends Zend_Filter_Word_CamelCaseToSeparator +{ + /** + * Constructor + * + * @return void + */ + public function __construct() + { + parent::__construct('-'); + } +} diff --git a/libs/Zend/Filter/Word/CamelCaseToSeparator.php b/libs/Zend/Filter/Word/CamelCaseToSeparator.php new file mode 100644 index 0000000000000000000000000000000000000000..5097460bb8b03f1e2613300febe0046fcce0b526 --- /dev/null +++ b/libs/Zend/Filter/Word/CamelCaseToSeparator.php @@ -0,0 +1,49 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: CamelCaseToSeparator.php 21089 2010-02-19 06:49:15Z thomas $ + */ + +/** + * @see Zend_Filter_PregReplace + */ +// require_once 'Zend/Filter/Word/Separator/Abstract.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Word_CamelCaseToSeparator extends Zend_Filter_Word_Separator_Abstract +{ + + public function filter($value) + { + if (self::isUnicodeSupportEnabled()) { + parent::setMatchPattern(array('#(?<=(?:\p{Lu}))(\p{Lu}\p{Ll})#','#(?<=(?:\p{Ll}|\p{Nd}))(\p{Lu})#')); + parent::setReplacement(array($this->_separator . '\1', $this->_separator . '\1')); + } else { + parent::setMatchPattern(array('#(?<=(?:[A-Z]))([A-Z]+)([A-Z][A-z])#', '#(?<=(?:[a-z0-9]))([A-Z])#')); + parent::setReplacement(array('\1' . $this->_separator . '\2', $this->_separator . '\1')); + } + + return parent::filter($value); + } + +} diff --git a/libs/Zend/Filter/Word/CamelCaseToUnderscore.php b/libs/Zend/Filter/Word/CamelCaseToUnderscore.php new file mode 100644 index 0000000000000000000000000000000000000000..5fbba3b44f5589d9435298775b5385fae517f196 --- /dev/null +++ b/libs/Zend/Filter/Word/CamelCaseToUnderscore.php @@ -0,0 +1,44 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: CamelCaseToUnderscore.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_CamelCaseToSeparator + */ +// require_once 'Zend/Filter/Word/CamelCaseToSeparator.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Word_CamelCaseToUnderscore extends Zend_Filter_Word_CamelCaseToSeparator +{ + /** + * Constructor + * + * @return void + */ + public function __construct() + { + parent::__construct('_'); + } +} diff --git a/libs/Zend/Filter/Word/DashToCamelCase.php b/libs/Zend/Filter/Word/DashToCamelCase.php new file mode 100644 index 0000000000000000000000000000000000000000..63d1bd6cffb6cb408fa4e8ece38235abdbcadc6f --- /dev/null +++ b/libs/Zend/Filter/Word/DashToCamelCase.php @@ -0,0 +1,44 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: DashToCamelCase.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Word/SeparatorToCamelCase.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Word_DashToCamelCase extends Zend_Filter_Word_SeparatorToCamelCase +{ + /** + * Constructor + * + * @return void + */ + public function __construct() + { + parent::__construct('-'); + } +} diff --git a/libs/Zend/Filter/Word/DashToSeparator.php b/libs/Zend/Filter/Word/DashToSeparator.php new file mode 100644 index 0000000000000000000000000000000000000000..e9fd2814bcfcc00b091221e06458abd6297717b4 --- /dev/null +++ b/libs/Zend/Filter/Word/DashToSeparator.php @@ -0,0 +1,42 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: DashToSeparator.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_PregReplace + */ +// require_once 'Zend/Filter/Word/Separator/Abstract.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Word_DashToSeparator extends Zend_Filter_Word_Separator_Abstract +{ + + public function filter($value) + { + $this->setMatchPattern('#-#'); + $this->setReplacement($this->_separator); + return parent::filter($value); + } +} diff --git a/libs/Zend/Filter/Word/DashToUnderscore.php b/libs/Zend/Filter/Word/DashToUnderscore.php new file mode 100644 index 0000000000000000000000000000000000000000..b5c583048ceb105649ec7bfbc2649ba6e7f90f84 --- /dev/null +++ b/libs/Zend/Filter/Word/DashToUnderscore.php @@ -0,0 +1,45 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: DashToUnderscore.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_PregReplace + */ +// require_once 'Zend/Filter/Word/SeparatorToSeparator.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Word_DashToUnderscore extends Zend_Filter_Word_SeparatorToSeparator +{ + /** + * Constructor + * + * @param string $separator Space by default + * @return void + */ + public function __construct() + { + parent::__construct('-', '_'); + } +} \ No newline at end of file diff --git a/libs/Zend/Filter/Word/Separator/Abstract.php b/libs/Zend/Filter/Word/Separator/Abstract.php new file mode 100644 index 0000000000000000000000000000000000000000..e3b7d4f8b746555b0bec6c20a7cd45d687d230df --- /dev/null +++ b/libs/Zend/Filter/Word/Separator/Abstract.php @@ -0,0 +1,76 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Abstract.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_PregReplace + */ +// require_once 'Zend/Filter/PregReplace.php'; + +/** + * @category Zend + * @package Zend_Filter + * @uses Zend_Filter_PregReplace + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +abstract class Zend_Filter_Word_Separator_Abstract extends Zend_Filter_PregReplace +{ + + protected $_separator = null; + + /** + * Constructor + * + * @param string $separator Space by default + * @return void + */ + public function __construct($separator = ' ') + { + $this->setSeparator($separator); + } + + /** + * Sets a new seperator + * + * @param string $separator Seperator + * @return $this + */ + public function setSeparator($separator) + { + if ($separator == null) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('"' . $separator . '" is not a valid separator.'); + } + $this->_separator = $separator; + return $this; + } + + /** + * Returns the actual set seperator + * + * @return string + */ + public function getSeparator() + { + return $this->_separator; + } + +} \ No newline at end of file diff --git a/libs/Zend/Filter/Word/SeparatorToCamelCase.php b/libs/Zend/Filter/Word/SeparatorToCamelCase.php new file mode 100644 index 0000000000000000000000000000000000000000..d4076566635ec6e0c5d98d15632c191d61d05efe --- /dev/null +++ b/libs/Zend/Filter/Word/SeparatorToCamelCase.php @@ -0,0 +1,52 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: SeparatorToCamelCase.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_PregReplace + */ +// require_once 'Zend/Filter/Word/Separator/Abstract.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Word_SeparatorToCamelCase extends Zend_Filter_Word_Separator_Abstract +{ + + public function filter($value) + { + // a unicode safe way of converting characters to \x00\x00 notation + $pregQuotedSeparator = preg_quote($this->_separator, '#'); + + if (self::isUnicodeSupportEnabled()) { + parent::setMatchPattern(array('#('.$pregQuotedSeparator.')(\p{L}{1})#e','#(^\p{Ll}{1})#e')); + parent::setReplacement(array("strtoupper('\\2')","strtoupper('\\1')")); + } else { + parent::setMatchPattern(array('#('.$pregQuotedSeparator.')([A-Za-z]{1})#e','#(^[A-Za-z]{1})#e')); + parent::setReplacement(array("strtoupper('\\2')","strtoupper('\\1')")); + } + + return parent::filter($value); + } + +} diff --git a/libs/Zend/Filter/Word/SeparatorToDash.php b/libs/Zend/Filter/Word/SeparatorToDash.php new file mode 100644 index 0000000000000000000000000000000000000000..852b9c33c371926e3ed9eb5c7df7c4b9d8ed1b8b --- /dev/null +++ b/libs/Zend/Filter/Word/SeparatorToDash.php @@ -0,0 +1,46 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: SeparatorToDash.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_SeperatorToSeparator + */ +// require_once 'Zend/Filter/Word/SeparatorToSeparator.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Word_SeparatorToDash extends Zend_Filter_Word_SeparatorToSeparator +{ + /** + * Constructor + * + * @param string $searchSeparator Seperator to search for change + * @return void + */ + public function __construct($searchSeparator = ' ') + { + parent::__construct($searchSeparator, '-'); + } + +} \ No newline at end of file diff --git a/libs/Zend/Filter/Word/SeparatorToSeparator.php b/libs/Zend/Filter/Word/SeparatorToSeparator.php new file mode 100644 index 0000000000000000000000000000000000000000..66b2df0b499b9a4067ae0d100ea9e2bb3702530b --- /dev/null +++ b/libs/Zend/Filter/Word/SeparatorToSeparator.php @@ -0,0 +1,129 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: SeparatorToSeparator.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_PregReplace + */ +// require_once 'Zend/Filter/PregReplace.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Word_SeparatorToSeparator extends Zend_Filter_PregReplace +{ + + protected $_searchSeparator = null; + protected $_replacementSeparator = null; + + /** + * Constructor + * + * @param string $searchSeparator Seperator to search for + * @param string $replacementSeperator Seperator to replace with + * @return void + */ + public function __construct($searchSeparator = ' ', $replacementSeparator = '-') + { + $this->setSearchSeparator($searchSeparator); + $this->setReplacementSeparator($replacementSeparator); + } + + /** + * Sets a new seperator to search for + * + * @param string $separator Seperator to search for + * @return $this + */ + public function setSearchSeparator($separator) + { + $this->_searchSeparator = $separator; + return $this; + } + + /** + * Returns the actual set seperator to search for + * + * @return string + */ + public function getSearchSeparator() + { + return $this->_searchSeparator; + } + + /** + * Sets a new seperator which replaces the searched one + * + * @param string $separator Seperator which replaces the searched one + * @return $this + */ + public function setReplacementSeparator($separator) + { + $this->_replacementSeparator = $separator; + return $this; + } + + /** + * Returns the actual set seperator which replaces the searched one + * + * @return string + */ + public function getReplacementSeparator() + { + return $this->_replacementSeparator; + } + + /** + * Defined by Zend_Filter_Interface + * + * Returns the string $value, replacing the searched seperators with the defined ones + * + * @param string $value + * @return string + */ + public function filter($value) + { + return $this->_separatorToSeparatorFilter($value); + } + + /** + * Do the real work, replaces the seperator to search for with the replacement seperator + * + * Returns the replaced string + * + * @param string $value + * @return string + */ + protected function _separatorToSeparatorFilter($value) + { + if ($this->_searchSeparator == null) { + // require_once 'Zend/Filter/Exception.php'; + throw new Zend_Filter_Exception('You must provide a search separator for this filter to work.'); + } + + $this->setMatchPattern('#' . preg_quote($this->_searchSeparator, '#') . '#'); + $this->setReplacement($this->_replacementSeparator); + return parent::filter($value); + } + +} \ No newline at end of file diff --git a/libs/Zend/Filter/Word/UnderscoreToCamelCase.php b/libs/Zend/Filter/Word/UnderscoreToCamelCase.php new file mode 100644 index 0000000000000000000000000000000000000000..d5bae5088b15ea31c0dfbaf1eff3d67639588528 --- /dev/null +++ b/libs/Zend/Filter/Word/UnderscoreToCamelCase.php @@ -0,0 +1,44 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: UnderscoreToCamelCase.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_Interface + */ +// require_once 'Zend/Filter/Word/SeparatorToCamelCase.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Word_UnderscoreToCamelCase extends Zend_Filter_Word_SeparatorToCamelCase +{ + /** + * Constructor + * + * @return void + */ + public function __construct() + { + parent::__construct('_'); + } +} diff --git a/libs/Zend/Filter/Word/UnderscoreToDash.php b/libs/Zend/Filter/Word/UnderscoreToDash.php new file mode 100644 index 0000000000000000000000000000000000000000..52a2f7e3fb562820e91e6283e79b1dcb48587f68 --- /dev/null +++ b/libs/Zend/Filter/Word/UnderscoreToDash.php @@ -0,0 +1,45 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: UnderscoreToDash.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_PregReplace + */ +// require_once 'Zend/Filter/Word/SeparatorToSeparator.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Word_UnderscoreToDash extends Zend_Filter_Word_SeparatorToSeparator +{ + /** + * Constructor + * + * @param string $separator Space by default + * @return void + */ + public function __construct() + { + parent::__construct('_', '-'); + } +} \ No newline at end of file diff --git a/libs/Zend/Filter/Word/UnderscoreToSeparator.php b/libs/Zend/Filter/Word/UnderscoreToSeparator.php new file mode 100644 index 0000000000000000000000000000000000000000..c8fee7d10088914d79fa74954c8132dba7d5da0f --- /dev/null +++ b/libs/Zend/Filter/Word/UnderscoreToSeparator.php @@ -0,0 +1,45 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: UnderscoreToSeparator.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ + +/** + * @see Zend_Filter_PregReplace + */ +// require_once 'Zend/Filter/Word/SeparatorToSeparator.php'; + +/** + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Word_UnderscoreToSeparator extends Zend_Filter_Word_SeparatorToSeparator +{ + /** + * Constructor + * + * @param string $separator Space by default + * @return void + */ + public function __construct($replacementSeparator = ' ') + { + parent::__construct('_', $replacementSeparator); + } +} diff --git a/libs/Zend/Form.php b/libs/Zend/Form.php new file mode 100644 index 0000000000000000000000000000000000000000..c8f6e796f7170198ea6ae2e6e614d6aa3e7c24a5 --- /dev/null +++ b/libs/Zend/Form.php @@ -0,0 +1,3366 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** @see Zend_Validate_Interface */ +// require_once 'Zend/Validate/Interface.php'; + +/** + * Zend_Form + * + * @category Zend + * @package Zend_Form + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Form.php 22465 2010-06-19 17:41:03Z alab $ + */ +class Zend_Form implements Iterator, Countable, Zend_Validate_Interface +{ + /**#@+ + * Plugin loader type constants + */ + const DECORATOR = 'DECORATOR'; + const ELEMENT = 'ELEMENT'; + /**#@-*/ + + /**#@+ + * Method type constants + */ + const METHOD_DELETE = 'delete'; + const METHOD_GET = 'get'; + const METHOD_POST = 'post'; + const METHOD_PUT = 'put'; + /**#@-*/ + + /**#@+ + * Encoding type constants + */ + const ENCTYPE_URLENCODED = 'application/x-www-form-urlencoded'; + const ENCTYPE_MULTIPART = 'multipart/form-data'; + /**#@-*/ + + /** + * Form metadata and attributes + * @var array + */ + protected $_attribs = array(); + + /** + * Decorators for rendering + * @var array + */ + protected $_decorators = array(); + + /** + * Default display group class + * @var string + */ + protected $_defaultDisplayGroupClass = 'Zend_Form_DisplayGroup'; + + /** + * Form description + * @var string + */ + protected $_description; + + /** + * Should we disable loading the default decorators? + * @var bool + */ + protected $_disableLoadDefaultDecorators = false; + + /** + * Display group prefix paths + * @var array + */ + protected $_displayGroupPrefixPaths = array(); + + /** + * Groups of elements grouped for display purposes + * @var array + */ + protected $_displayGroups = array(); + + /** + * Global decorators to apply to all elements + * @var null|array + */ + protected $_elementDecorators; + + /** + * Prefix paths to use when creating elements + * @var array + */ + protected $_elementPrefixPaths = array(); + + /** + * Form elements + * @var array + */ + protected $_elements = array(); + + /** + * Array to which elements belong (if any) + * @var string + */ + protected $_elementsBelongTo; + + /** + * Custom form-level error messages + * @var array + */ + protected $_errorMessages = array(); + + /** + * Are there errors in the form? + * @var bool + */ + protected $_errorsExist = false; + + /** + * Has the form been manually flagged as an error? + * @var bool + */ + protected $_errorsForced = false; + + /** + * Form order + * @var int|null + */ + protected $_formOrder; + + /** + * Whether or not form elements are members of an array + * @var bool + */ + protected $_isArray = false; + + /** + * Form legend + * @var string + */ + protected $_legend; + + /** + * Plugin loaders + * @var array + */ + protected $_loaders = array(); + + /** + * Allowed form methods + * @var array + */ + protected $_methods = array('delete', 'get', 'post', 'put'); + + /** + * Order in which to display and iterate elements + * @var array + */ + protected $_order = array(); + + /** + * Whether internal order has been updated or not + * @var bool + */ + protected $_orderUpdated = false; + + /** + * Sub form prefix paths + * @var array + */ + protected $_subFormPrefixPaths = array(); + + /** + * Sub forms + * @var array + */ + protected $_subForms = array(); + + /** + * @var Zend_Translate + */ + protected $_translator; + + /** + * Global default translation adapter + * @var Zend_Translate + */ + protected static $_translatorDefault; + + /** + * is the translator disabled? + * @var bool + */ + protected $_translatorDisabled = false; + + /** + * @var Zend_View_Interface + */ + protected $_view; + + /** + * @var bool + */ + protected $_isRendered = false; + + /** + * Constructor + * + * Registers form view helper as decorator + * + * @param mixed $options + * @return void + */ + public function __construct($options = null) + { + if (is_array($options)) { + $this->setOptions($options); + } elseif ($options instanceof Zend_Config) { + $this->setConfig($options); + } + + // Extensions... + $this->init(); + + $this->loadDefaultDecorators(); + } + + /** + * Clone form object and all children + * + * @return void + */ + public function __clone() + { + $elements = array(); + foreach ($this->getElements() as $name => $element) { + $elements[] = clone $element; + } + $this->setElements($elements); + + $subForms = array(); + foreach ($this->getSubForms() as $name => $subForm) { + $subForms[$name] = clone $subForm; + } + $this->setSubForms($subForms); + + $displayGroups = array(); + foreach ($this->_displayGroups as $group) { + $clone = clone $group; + $elements = array(); + foreach ($clone->getElements() as $name => $e) { + $elements[] = $this->getElement($name); + } + $clone->setElements($elements); + $displayGroups[] = $clone; + } + $this->setDisplayGroups($displayGroups); + } + + /** + * Reset values of form + * + * @return Zend_Form + */ + public function reset() + { + foreach ($this->getElements() as $element) { + $element->setValue(null); + } + foreach ($this->getSubForms() as $subForm) { + $subForm->reset(); + } + + return $this; + } + + /** + * Initialize form (used by extending classes) + * + * @return void + */ + public function init() + { + } + + /** + * Set form state from options array + * + * @param array $options + * @return Zend_Form + */ + public function setOptions(array $options) + { + if (isset($options['prefixPath'])) { + $this->addPrefixPaths($options['prefixPath']); + unset($options['prefixPath']); + } + + if (isset($options['elementPrefixPath'])) { + $this->addElementPrefixPaths($options['elementPrefixPath']); + unset($options['elementPrefixPath']); + } + + if (isset($options['displayGroupPrefixPath'])) { + $this->addDisplayGroupPrefixPaths($options['displayGroupPrefixPath']); + unset($options['displayGroupPrefixPath']); + } + + if (isset($options['elementDecorators'])) { + $this->_elementDecorators = $options['elementDecorators']; + unset($options['elementDecorators']); + } + + if (isset($options['elements'])) { + $this->setElements($options['elements']); + unset($options['elements']); + } + + if (isset($options['defaultDisplayGroupClass'])) { + $this->setDefaultDisplayGroupClass($options['defaultDisplayGroupClass']); + unset($options['defaultDisplayGroupClass']); + } + + if (isset($options['displayGroupDecorators'])) { + $displayGroupDecorators = $options['displayGroupDecorators']; + unset($options['displayGroupDecorators']); + } + + if (isset($options['elementsBelongTo'])) { + $elementsBelongTo = $options['elementsBelongTo']; + unset($options['elementsBelongTo']); + } + + if (isset($options['attribs'])) { + $this->addAttribs($options['attribs']); + unset($options['attribs']); + } + + $forbidden = array( + 'Options', 'Config', 'PluginLoader', 'SubForms', 'View', 'Translator', + 'Attrib', 'Default', + ); + + foreach ($options as $key => $value) { + $normalized = ucfirst($key); + if (in_array($normalized, $forbidden)) { + continue; + } + + $method = 'set' . $normalized; + if (method_exists($this, $method)) { + $this->$method($value); + } else { + $this->setAttrib($key, $value); + } + } + + if (isset($displayGroupDecorators)) { + $this->setDisplayGroupDecorators($displayGroupDecorators); + } + + if (isset($elementsBelongTo)) { + $this->setElementsBelongTo($elementsBelongTo); + } + + return $this; + } + + /** + * Set form state from config object + * + * @param Zend_Config $config + * @return Zend_Form + */ + public function setConfig(Zend_Config $config) + { + return $this->setOptions($config->toArray()); + } + + + // Loaders + + /** + * Set plugin loaders for use with decorators and elements + * + * @param Zend_Loader_PluginLoader_Interface $loader + * @param string $type 'decorator' or 'element' + * @return Zend_Form + * @throws Zend_Form_Exception on invalid type + */ + public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type = null) + { + $type = strtoupper($type); + switch ($type) { + case self::DECORATOR: + case self::ELEMENT: + $this->_loaders[$type] = $loader; + return $this; + default: + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type)); + } + } + + /** + * Retrieve plugin loader for given type + * + * $type may be one of: + * - decorator + * - element + * + * If a plugin loader does not exist for the given type, defaults are + * created. + * + * @param string $type + * @return Zend_Loader_PluginLoader_Interface + */ + public function getPluginLoader($type = null) + { + $type = strtoupper($type); + if (!isset($this->_loaders[$type])) { + switch ($type) { + case self::DECORATOR: + $prefixSegment = 'Form_Decorator'; + $pathSegment = 'Form/Decorator'; + break; + case self::ELEMENT: + $prefixSegment = 'Form_Element'; + $pathSegment = 'Form/Element'; + break; + default: + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type)); + } + + // require_once 'Zend/Loader/PluginLoader.php'; + $this->_loaders[$type] = new Zend_Loader_PluginLoader( + array('Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/') + ); + } + + return $this->_loaders[$type]; + } + + /** + * Add prefix path for plugin loader + * + * If no $type specified, assumes it is a base path for both filters and + * validators, and sets each according to the following rules: + * - decorators: $prefix = $prefix . '_Decorator' + * - elements: $prefix = $prefix . '_Element' + * + * Otherwise, the path prefix is set on the appropriate plugin loader. + * + * If $type is 'decorator', sets the path in the decorator plugin loader + * for all elements. Additionally, if no $type is provided, + * the prefix and path is added to both decorator and element + * plugin loader with following settings: + * $prefix . '_Decorator', $path . '/Decorator/' + * $prefix . '_Element', $path . '/Element/' + * + * @param string $prefix + * @param string $path + * @param string $type + * @return Zend_Form + * @throws Zend_Form_Exception for invalid type + */ + public function addPrefixPath($prefix, $path, $type = null) + { + $type = strtoupper($type); + switch ($type) { + case self::DECORATOR: + case self::ELEMENT: + $loader = $this->getPluginLoader($type); + $loader->addPrefixPath($prefix, $path); + return $this; + case null: + $prefix = rtrim($prefix, '_'); + $path = rtrim($path, DIRECTORY_SEPARATOR); + foreach (array(self::DECORATOR, self::ELEMENT) as $type) { + $cType = ucfirst(strtolower($type)); + $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR; + $pluginPrefix = $prefix . '_' . $cType; + $loader = $this->getPluginLoader($type); + $loader->addPrefixPath($pluginPrefix, $pluginPath); + } + return $this; + default: + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type)); + } + } + + /** + * Add many prefix paths at once + * + * @param array $spec + * @return Zend_Form + */ + public function addPrefixPaths(array $spec) + { + if (isset($spec['prefix']) && isset($spec['path'])) { + return $this->addPrefixPath($spec['prefix'], $spec['path']); + } + foreach ($spec as $type => $paths) { + if (is_numeric($type) && is_array($paths)) { + $type = null; + if (isset($paths['prefix']) && isset($paths['path'])) { + if (isset($paths['type'])) { + $type = $paths['type']; + } + $this->addPrefixPath($paths['prefix'], $paths['path'], $type); + } + } elseif (!is_numeric($type)) { + if (!isset($paths['prefix']) || !isset($paths['path'])) { + continue; + } + $this->addPrefixPath($paths['prefix'], $paths['path'], $type); + } + } + return $this; + } + + /** + * Add prefix path for all elements + * + * @param string $prefix + * @param string $path + * @param string $type + * @return Zend_Form + */ + public function addElementPrefixPath($prefix, $path, $type = null) + { + $this->_elementPrefixPaths[] = array( + 'prefix' => $prefix, + 'path' => $path, + 'type' => $type, + ); + + foreach ($this->getElements() as $element) { + $element->addPrefixPath($prefix, $path, $type); + } + + foreach ($this->getSubForms() as $subForm) { + $subForm->addElementPrefixPath($prefix, $path, $type); + } + + return $this; + } + + /** + * Add prefix paths for all elements + * + * @param array $spec + * @return Zend_Form + */ + public function addElementPrefixPaths(array $spec) + { + $this->_elementPrefixPaths = $this->_elementPrefixPaths + $spec; + + foreach ($this->getElements() as $element) { + $element->addPrefixPaths($spec); + } + + return $this; + } + + /** + * Add prefix path for all display groups + * + * @param string $prefix + * @param string $path + * @return Zend_Form + */ + public function addDisplayGroupPrefixPath($prefix, $path) + { + $this->_displayGroupPrefixPaths[] = array( + 'prefix' => $prefix, + 'path' => $path, + ); + + foreach ($this->getDisplayGroups() as $group) { + $group->addPrefixPath($prefix, $path); + } + + return $this; + } + + /** + * Add multiple display group prefix paths at once + * + * @param array $spec + * @return Zend_Form + */ + public function addDisplayGroupPrefixPaths(array $spec) + { + foreach ($spec as $key => $value) { + if (is_string($value) && !is_numeric($key)) { + $this->addDisplayGroupPrefixPath($key, $value); + continue; + } + + if (is_string($value) && is_numeric($key)) { + continue; + } + + if (is_array($value)) { + $count = count($value); + if (array_keys($value) === range(0, $count - 1)) { + if ($count < 2) { + continue; + } + $prefix = array_shift($value); + $path = array_shift($value); + $this->addDisplayGroupPrefixPath($prefix, $path); + continue; + } + if (array_key_exists('prefix', $value) && array_key_exists('path', $value)) { + $this->addDisplayGroupPrefixPath($value['prefix'], $value['path']); + } + } + } + return $this; + } + + // Form metadata: + + /** + * Set form attribute + * + * @param string $key + * @param mixed $value + * @return Zend_Form + */ + public function setAttrib($key, $value) + { + $key = (string) $key; + $this->_attribs[$key] = $value; + return $this; + } + + /** + * Add multiple form attributes at once + * + * @param array $attribs + * @return Zend_Form + */ + public function addAttribs(array $attribs) + { + foreach ($attribs as $key => $value) { + $this->setAttrib($key, $value); + } + return $this; + } + + /** + * Set multiple form attributes at once + * + * Overwrites any previously set attributes. + * + * @param array $attribs + * @return Zend_Form + */ + public function setAttribs(array $attribs) + { + $this->clearAttribs(); + return $this->addAttribs($attribs); + } + + /** + * Retrieve a single form attribute + * + * @param string $key + * @return mixed + */ + public function getAttrib($key) + { + $key = (string) $key; + if (!isset($this->_attribs[$key])) { + return null; + } + + return $this->_attribs[$key]; + } + + /** + * Retrieve all form attributes/metadata + * + * @return array + */ + public function getAttribs() + { + return $this->_attribs; + } + + /** + * Remove attribute + * + * @param string $key + * @return bool + */ + public function removeAttrib($key) + { + if (isset($this->_attribs[$key])) { + unset($this->_attribs[$key]); + return true; + } + + return false; + } + + /** + * Clear all form attributes + * + * @return Zend_Form + */ + public function clearAttribs() + { + $this->_attribs = array(); + return $this; + } + + /** + * Set form action + * + * @param string $action + * @return Zend_Form + */ + public function setAction($action) + { + return $this->setAttrib('action', (string) $action); + } + + /** + * Get form action + * + * Sets default to '' if not set. + * + * @return string + */ + public function getAction() + { + $action = $this->getAttrib('action'); + if (null === $action) { + $action = ''; + $this->setAction($action); + } + return $action; + } + + /** + * Set form method + * + * Only values in {@link $_methods()} allowed + * + * @param string $method + * @return Zend_Form + * @throws Zend_Form_Exception + */ + public function setMethod($method) + { + $method = strtolower($method); + if (!in_array($method, $this->_methods)) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('"%s" is an invalid form method', $method)); + } + $this->setAttrib('method', $method); + return $this; + } + + /** + * Retrieve form method + * + * @return string + */ + public function getMethod() + { + if (null === ($method = $this->getAttrib('method'))) { + $method = self::METHOD_POST; + $this->setAttrib('method', $method); + } + return strtolower($method); + } + + /** + * Set encoding type + * + * @param string $value + * @return Zend_Form + */ + public function setEnctype($value) + { + $this->setAttrib('enctype', $value); + return $this; + } + + /** + * Get encoding type + * + * @return string + */ + public function getEnctype() + { + if (null === ($enctype = $this->getAttrib('enctype'))) { + $enctype = self::ENCTYPE_URLENCODED; + $this->setAttrib('enctype', $enctype); + } + return $this->getAttrib('enctype'); + } + + /** + * Filter a name to only allow valid variable characters + * + * @param string $value + * @param bool $allowBrackets + * @return string + */ + public function filterName($value, $allowBrackets = false) + { + $charset = '^a-zA-Z0-9_\x7f-\xff'; + if ($allowBrackets) { + $charset .= '\[\]'; + } + return preg_replace('/[' . $charset . ']/', '', (string) $value); + } + + /** + * Set form name + * + * @param string $name + * @return Zend_Form + */ + public function setName($name) + { + $name = $this->filterName($name); + if ('' === (string)$name) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty'); + } + + return $this->setAttrib('name', $name); + } + + /** + * Get name attribute + * + * @return null|string + */ + public function getName() + { + return $this->getAttrib('name'); + } + + /** + * Get fully qualified name + * + * Places name as subitem of array and/or appends brackets. + * + * @return string + */ + public function getFullyQualifiedName() + { + return $this->getName(); + } + + /** + * Get element id + * + * @return string + */ + public function getId() + { + if (null !== ($id = $this->getAttrib('id'))) { + return $id; + } + + $id = $this->getFullyQualifiedName(); + + // Bail early if no array notation detected + if (!strstr($id, '[')) { + return $id; + } + + // Strip array notation + if ('[]' == substr($id, -2)) { + $id = substr($id, 0, strlen($id) - 2); + } + $id = str_replace('][', '-', $id); + $id = str_replace(array(']', '['), '-', $id); + $id = trim($id, '-'); + + return $id; + } + + /** + * Set form legend + * + * @param string $value + * @return Zend_Form + */ + public function setLegend($value) + { + $this->_legend = (string) $value; + return $this; + } + + /** + * Get form legend + * + * @return string + */ + public function getLegend() + { + return $this->_legend; + } + + /** + * Set form description + * + * @param string $value + * @return Zend_Form + */ + public function setDescription($value) + { + $this->_description = (string) $value; + return $this; + } + + /** + * Retrieve form description + * + * @return string + */ + public function getDescription() + { + return $this->_description; + } + + /** + * Set form order + * + * @param int $index + * @return Zend_Form + */ + public function setOrder($index) + { + $this->_formOrder = (int) $index; + return $this; + } + + /** + * Get form order + * + * @return int|null + */ + public function getOrder() + { + return $this->_formOrder; + } + + /** + * When calling renderFormElements or render this method + * is used to set $_isRendered member to prevent repeatedly + * merging belongsTo setting + */ + protected function _setIsRendered() + { + $this->_isRendered = true; + return $this; + } + + /** + * Get the value of $_isRendered member + */ + protected function _getIsRendered() + { + return (bool)$this->_isRendered; + } + + // Element interaction: + + /** + * Add a new element + * + * $element may be either a string element type, or an object of type + * Zend_Form_Element. If a string element type is provided, $name must be + * provided, and $options may be optionally provided for configuring the + * element. + * + * If a Zend_Form_Element is provided, $name may be optionally provided, + * and any provided $options will be ignored. + * + * @param string|Zend_Form_Element $element + * @param string $name + * @param array|Zend_Config $options + * @return Zend_Form + */ + public function addElement($element, $name = null, $options = null) + { + if (is_string($element)) { + if (null === $name) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Elements specified by string must have an accompanying name'); + } + + if (is_array($this->_elementDecorators)) { + if (null === $options) { + $options = array('decorators' => $this->_elementDecorators); + } elseif ($options instanceof Zend_Config) { + $options = $options->toArray(); + } + if (is_array($options) + && !array_key_exists('decorators', $options) + ) { + $options['decorators'] = $this->_elementDecorators; + } + } + + $this->_elements[$name] = $this->createElement($element, $name, $options); + } elseif ($element instanceof Zend_Form_Element) { + $prefixPaths = array(); + $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths(); + if (!empty($this->_elementPrefixPaths)) { + $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths); + } + + if (null === $name) { + $name = $element->getName(); + } + + $this->_elements[$name] = $element; + $this->_elements[$name]->addPrefixPaths($prefixPaths); + } + + $this->_order[$name] = $this->_elements[$name]->getOrder(); + $this->_orderUpdated = true; + $this->_setElementsBelongTo($name); + + return $this; + } + + /** + * Create an element + * + * Acts as a factory for creating elements. Elements created with this + * method will not be attached to the form, but will contain element + * settings as specified in the form object (including plugin loader + * prefix paths, default decorators, etc.). + * + * @param string $type + * @param string $name + * @param array|Zend_Config $options + * @return Zend_Form_Element + */ + public function createElement($type, $name, $options = null) + { + if (!is_string($type)) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Element type must be a string indicating type'); + } + + if (!is_string($name)) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Element name must be a string'); + } + + $prefixPaths = array(); + $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths(); + if (!empty($this->_elementPrefixPaths)) { + $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths); + } + + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } + + if ((null === $options) || !is_array($options)) { + $options = array('prefixPath' => $prefixPaths); + } elseif (is_array($options)) { + if (array_key_exists('prefixPath', $options)) { + $options['prefixPath'] = array_merge($prefixPaths, $options['prefixPath']); + } else { + $options['prefixPath'] = $prefixPaths; + } + } + + $class = $this->getPluginLoader(self::ELEMENT)->load($type); + $element = new $class($name, $options); + + return $element; + } + + /** + * Add multiple elements at once + * + * @param array $elements + * @return Zend_Form + */ + public function addElements(array $elements) + { + foreach ($elements as $key => $spec) { + $name = null; + if (!is_numeric($key)) { + $name = $key; + } + + if (is_string($spec) || ($spec instanceof Zend_Form_Element)) { + $this->addElement($spec, $name); + continue; + } + + if (is_array($spec)) { + $argc = count($spec); + $options = array(); + if (isset($spec['type'])) { + $type = $spec['type']; + if (isset($spec['name'])) { + $name = $spec['name']; + } + if (isset($spec['options'])) { + $options = $spec['options']; + } + $this->addElement($type, $name, $options); + } else { + switch ($argc) { + case 0: + continue; + case (1 <= $argc): + $type = array_shift($spec); + case (2 <= $argc): + if (null === $name) { + $name = array_shift($spec); + } else { + $options = array_shift($spec); + } + case (3 <= $argc): + if (empty($options)) { + $options = array_shift($spec); + } + default: + $this->addElement($type, $name, $options); + } + } + } + } + return $this; + } + + /** + * Set form elements (overwrites existing elements) + * + * @param array $elements + * @return Zend_Form + */ + public function setElements(array $elements) + { + $this->clearElements(); + return $this->addElements($elements); + } + + /** + * Retrieve a single element + * + * @param string $name + * @return Zend_Form_Element|null + */ + public function getElement($name) + { + if (array_key_exists($name, $this->_elements)) { + return $this->_elements[$name]; + } + return null; + } + + /** + * Retrieve all elements + * + * @return array + */ + public function getElements() + { + return $this->_elements; + } + + /** + * Remove element + * + * @param string $name + * @return boolean + */ + public function removeElement($name) + { + $name = (string) $name; + if (isset($this->_elements[$name])) { + unset($this->_elements[$name]); + if (array_key_exists($name, $this->_order)) { + unset($this->_order[$name]); + $this->_orderUpdated = true; + } else { + foreach ($this->_displayGroups as $group) { + if (null !== $group->getElement($name)) { + $group->removeElement($name); + } + } + } + return true; + } + + return false; + } + + /** + * Remove all form elements + * + * @return Zend_Form + */ + public function clearElements() + { + foreach (array_keys($this->_elements) as $key) { + if (array_key_exists($key, $this->_order)) { + unset($this->_order[$key]); + } + } + $this->_elements = array(); + $this->_orderUpdated = true; + return $this; + } + + /** + * Set default values for elements + * + * Sets values for all elements specified in the array of $defaults. + * + * @param array $defaults + * @return Zend_Form + */ + public function setDefaults(array $defaults) + { + $eBelongTo = null; + + if ($this->isArray()) { + $eBelongTo = $this->getElementsBelongTo(); + $defaults = $this->_dissolveArrayValue($defaults, $eBelongTo); + } + foreach ($this->getElements() as $name => $element) { + $check = $defaults; + if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) { + $check = $this->_dissolveArrayValue($defaults, $belongsTo); + } + if (array_key_exists($name, $check)) { + $this->setDefault($name, $check[$name]); + $defaults = $this->_dissolveArrayUnsetKey($defaults, $belongsTo, $name); + } + } + foreach ($this->getSubForms() as $name => $form) { + if (!$form->isArray() && array_key_exists($name, $defaults)) { + $form->setDefaults($defaults[$name]); + } else { + $form->setDefaults($defaults); + } + } + return $this; + } + + /** + * Set default value for an element + * + * @param string $name + * @param mixed $value + * @return Zend_Form + */ + public function setDefault($name, $value) + { + $name = (string) $name; + if ($element = $this->getElement($name)) { + $element->setValue($value); + } else { + if (is_scalar($value)) { + foreach ($this->getSubForms() as $subForm) { + $subForm->setDefault($name, $value); + } + } elseif (is_array($value) && ($subForm = $this->getSubForm($name))) { + $subForm->setDefaults($value); + } + } + return $this; + } + + /** + * Retrieve value for single element + * + * @param string $name + * @return mixed + */ + public function getValue($name) + { + if ($element = $this->getElement($name)) { + return $element->getValue(); + } + + if ($subForm = $this->getSubForm($name)) { + return $subForm->getValues(true); + } + + foreach ($this->getSubForms() as $subForm) { + if ($name == $subForm->getElementsBelongTo()) { + return $subForm->getValues(true); + } + } + return null; + } + + /** + * Retrieve all form element values + * + * @param bool $suppressArrayNotation + * @return array + */ + public function getValues($suppressArrayNotation = false) + { + $values = array(); + $eBelongTo = null; + + if ($this->isArray()) { + $eBelongTo = $this->getElementsBelongTo(); + } + + foreach ($this->getElements() as $key => $element) { + if (!$element->getIgnore()) { + $merge = array(); + if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) { + if ('' !== (string)$belongsTo) { + $key = $belongsTo . '[' . $key . ']'; + } + } + $merge = $this->_attachToArray($element->getValue(), $key); + $values = $this->_array_replace_recursive($values, $merge); + } + } + foreach ($this->getSubForms() as $key => $subForm) { + $merge = array(); + if (!$subForm->isArray()) { + $merge[$key] = $subForm->getValues(); + } else { + $merge = $this->_attachToArray($subForm->getValues(true), + $subForm->getElementsBelongTo()); + } + $values = $this->_array_replace_recursive($values, $merge); + } + + if (!$suppressArrayNotation && + $this->isArray() && + !$this->_getIsRendered()) { + $values = $this->_attachToArray($values, $this->getElementsBelongTo()); + } + + return $values; + } + + /** + * Returns only the valid values from the given form input. + * + * For models that can be saved in a partially valid state, for example when following the builder, + * prototype or state patterns it is particularly interessting to retrieve all the current valid + * values to persist them. + * + * @param array $data + * @param bool $suppressArrayNotation + * @return array + */ + public function getValidValues($data, $suppressArrayNotation = false) + { + $values = array(); + $eBelongTo = null; + + if ($this->isArray()) { + $eBelongTo = $this->getElementsBelongTo(); + $data = $this->_dissolveArrayValue($data, $eBelongTo); + } + $context = $data; + foreach ($this->getElements() as $key => $element) { + if (!$element->getIgnore()) { + $check = $data; + if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) { + $check = $this->_dissolveArrayValue($data, $belongsTo); + } + if (isset($check[$key])) { + if($element->isValid($check[$key], $context)) { + $merge = array(); + if ($belongsTo !== $eBelongTo && '' !== (string)$belongsTo) { + $key = $belongsTo . '[' . $key . ']'; + } + $merge = $this->_attachToArray($element->getValue(), $key); + $values = $this->_array_replace_recursive($values, $merge); + } + $data = $this->_dissolveArrayUnsetKey($data, $belongsTo, $key); + } + } + } + foreach ($this->getSubForms() as $key => $form) { + $merge = array(); + if (isset($data[$key]) && !$form->isArray()) { + $tmp = $form->getValidValues($data[$key]); + if (!empty($tmp)) { + $merge[$key] = $tmp; + } + } else { + $tmp = $form->getValidValues($data, true); + if (!empty($tmp)) { + $merge = $this->_attachToArray($tmp, $form->getElementsBelongTo()); + } + } + $values = $this->_array_replace_recursive($values, $merge); + } + if (!$suppressArrayNotation && + $this->isArray() && + !empty($values) && + !$this->_getIsRendered()) { + $values = $this->_attachToArray($values, $this->getElementsBelongTo()); + } + + return $values; + } + + /** + * Get unfiltered element value + * + * @param string $name + * @return mixed + */ + public function getUnfilteredValue($name) + { + if ($element = $this->getElement($name)) { + return $element->getUnfilteredValue(); + } + return null; + } + + /** + * Retrieve all unfiltered element values + * + * @return array + */ + public function getUnfilteredValues() + { + $values = array(); + foreach ($this->getElements() as $key => $element) { + $values[$key] = $element->getUnfilteredValue(); + } + + return $values; + } + + /** + * Set all elements' filters + * + * @param array $filters + * @return Zend_Form + */ + public function setElementFilters(array $filters) + { + foreach ($this->getElements() as $element) { + $element->setFilters($filters); + } + return $this; + } + + /** + * Set name of array elements belong to + * + * @param string $array + * @return Zend_Form + */ + public function setElementsBelongTo($array) + { + $origName = $this->getElementsBelongTo(); + $name = $this->filterName($array, true); + if ('' === $name) { + $name = null; + } + $this->_elementsBelongTo = $name; + + if (null === $name) { + $this->setIsArray(false); + if (null !== $origName) { + $this->_setElementsBelongTo(); + } + } else { + $this->setIsArray(true); + $this->_setElementsBelongTo(); + } + + return $this; + } + + /** + * Set array to which elements belong + * + * @param string $name Element name + * @return void + */ + protected function _setElementsBelongTo($name = null) + { + $array = $this->getElementsBelongTo(); + + if (null === $array) { + return; + } + + if (null === $name) { + foreach ($this->getElements() as $element) { + $element->setBelongsTo($array); + } + } else { + if (null !== ($element = $this->getElement($name))) { + $element->setBelongsTo($array); + } + } + } + + /** + * Get name of array elements belong to + * + * @return string|null + */ + public function getElementsBelongTo() + { + if ((null === $this->_elementsBelongTo) && $this->isArray()) { + $name = $this->getName(); + if ('' !== (string)$name) { + return $name; + } + } + return $this->_elementsBelongTo; + } + + /** + * Set flag indicating elements belong to array + * + * @param bool $flag Value of flag + * @return Zend_Form + */ + public function setIsArray($flag) + { + $this->_isArray = (bool) $flag; + return $this; + } + + /** + * Get flag indicating if elements belong to an array + * + * @return bool + */ + public function isArray() + { + return $this->_isArray; + } + + // Element groups: + + /** + * Add a form group/subform + * + * @param Zend_Form $form + * @param string $name + * @param int $order + * @return Zend_Form + */ + public function addSubForm(Zend_Form $form, $name, $order = null) + { + $name = (string) $name; + foreach ($this->_loaders as $type => $loader) { + $loaderPaths = $loader->getPaths(); + foreach ($loaderPaths as $prefix => $paths) { + foreach ($paths as $path) { + $form->addPrefixPath($prefix, $path, $type); + } + } + } + + if (!empty($this->_elementPrefixPaths)) { + foreach ($this->_elementPrefixPaths as $spec) { + list($prefix, $path, $type) = array_values($spec); + $form->addElementPrefixPath($prefix, $path, $type); + } + } + + if (!empty($this->_displayGroupPrefixPaths)) { + foreach ($this->_displayGroupPrefixPaths as $spec) { + list($prefix, $path) = array_values($spec); + $form->addDisplayGroupPrefixPath($prefix, $path); + } + } + + if (null !== $order) { + $form->setOrder($order); + } + + if (($oldName = $form->getName()) && + $oldName !== $name && + $oldName === $form->getElementsBelongTo()) { + $form->setElementsBelongTo($name); + } + + $form->setName($name); + $this->_subForms[$name] = $form; + $this->_order[$name] = $order; + $this->_orderUpdated = true; + return $this; + } + + /** + * Add multiple form subForms/subforms at once + * + * @param array $subForms + * @return Zend_Form + */ + public function addSubForms(array $subForms) + { + foreach ($subForms as $key => $spec) { + $name = null; + if (!is_numeric($key)) { + $name = $key; + } + + if ($spec instanceof Zend_Form) { + $this->addSubForm($spec, $name); + continue; + } + + if (is_array($spec)) { + $argc = count($spec); + $order = null; + switch ($argc) { + case 0: + continue; + case (1 <= $argc): + $subForm = array_shift($spec); + case (2 <= $argc): + $name = array_shift($spec); + case (3 <= $argc): + $order = array_shift($spec); + default: + $this->addSubForm($subForm, $name, $order); + } + } + } + return $this; + } + + /** + * Set multiple form subForms/subforms (overwrites) + * + * @param array $subForms + * @return Zend_Form + */ + public function setSubForms(array $subForms) + { + $this->clearSubForms(); + return $this->addSubForms($subForms); + } + + /** + * Retrieve a form subForm/subform + * + * @param string $name + * @return Zend_Form|null + */ + public function getSubForm($name) + { + $name = (string) $name; + if (isset($this->_subForms[$name])) { + return $this->_subForms[$name]; + } + return null; + } + + /** + * Retrieve all form subForms/subforms + * + * @return array + */ + public function getSubForms() + { + return $this->_subForms; + } + + /** + * Remove form subForm/subform + * + * @param string $name + * @return boolean + */ + public function removeSubForm($name) + { + $name = (string) $name; + if (array_key_exists($name, $this->_subForms)) { + unset($this->_subForms[$name]); + if (array_key_exists($name, $this->_order)) { + unset($this->_order[$name]); + $this->_orderUpdated = true; + } + return true; + } + + return false; + } + + /** + * Remove all form subForms/subforms + * + * @return Zend_Form + */ + public function clearSubForms() + { + foreach (array_keys($this->_subForms) as $key) { + if (array_key_exists($key, $this->_order)) { + unset($this->_order[$key]); + } + } + $this->_subForms = array(); + $this->_orderUpdated = true; + return $this; + } + + + // Display groups: + + /** + * Set default display group class + * + * @param string $class + * @return Zend_Form + */ + public function setDefaultDisplayGroupClass($class) + { + $this->_defaultDisplayGroupClass = (string) $class; + return $this; + } + + /** + * Retrieve default display group class + * + * @return string + */ + public function getDefaultDisplayGroupClass() + { + return $this->_defaultDisplayGroupClass; + } + + /** + * Add a display group + * + * Groups named elements for display purposes. + * + * If a referenced element does not yet exist in the form, it is omitted. + * + * @param array $elements + * @param string $name + * @param array|Zend_Config $options + * @return Zend_Form + * @throws Zend_Form_Exception if no valid elements provided + */ + public function addDisplayGroup(array $elements, $name, $options = null) + { + $group = array(); + foreach ($elements as $element) { + if (isset($this->_elements[$element])) { + $add = $this->getElement($element); + if (null !== $add) { + unset($this->_order[$element]); + $group[] = $add; + } + } + } + if (empty($group)) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('No valid elements specified for display group'); + } + + $name = (string) $name; + + if (is_array($options)) { + $options['elements'] = $group; + } elseif ($options instanceof Zend_Config) { + $options = $options->toArray(); + $options['elements'] = $group; + } else { + $options = array('elements' => $group); + } + + if (isset($options['displayGroupClass'])) { + $class = $options['displayGroupClass']; + unset($options['displayGroupClass']); + } else { + $class = $this->getDefaultDisplayGroupClass(); + } + + // if (!class_exists($class)) { + // require_once 'Zend/Loader.php'; + // Zend_Loader::loadClass($class); + // } + $this->_displayGroups[$name] = new $class( + $name, + $this->getPluginLoader(self::DECORATOR), + $options + ); + + if (!empty($this->_displayGroupPrefixPaths)) { + $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths); + } + + $this->_order[$name] = $this->_displayGroups[$name]->getOrder(); + $this->_orderUpdated = true; + return $this; + } + + /** + * Add a display group object (used with cloning) + * + * @param Zend_Form_DisplayGroup $group + * @param string|null $name + * @return Zend_Form + */ + protected function _addDisplayGroupObject(Zend_Form_DisplayGroup $group, $name = null) + { + if (null === $name) { + $name = $group->getName(); + if ('' === (string)$name) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid display group added; requires name'); + } + } + + $this->_displayGroups[$name] = $group; + + if (!empty($this->_displayGroupPrefixPaths)) { + $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths); + } + + $this->_order[$name] = $this->_displayGroups[$name]->getOrder(); + $this->_orderUpdated = true; + return $this; + } + + /** + * Add multiple display groups at once + * + * @param array $groups + * @return Zend_Form + */ + public function addDisplayGroups(array $groups) + { + foreach ($groups as $key => $spec) { + $name = null; + if (!is_numeric($key)) { + $name = $key; + } + + if ($spec instanceof Zend_Form_DisplayGroup) { + $this->_addDisplayGroupObject($spec); + } + + if (!is_array($spec) || empty($spec)) { + continue; + } + + $argc = count($spec); + $options = array(); + + if (isset($spec['elements'])) { + $elements = $spec['elements']; + if (isset($spec['name'])) { + $name = $spec['name']; + } + if (isset($spec['options'])) { + $options = $spec['options']; + } + $this->addDisplayGroup($elements, $name, $options); + } else { + switch ($argc) { + case (1 <= $argc): + $elements = array_shift($spec); + if (!is_array($elements) && (null !== $name)) { + $elements = array_merge((array) $elements, $spec); + $this->addDisplayGroup($elements, $name); + break; + } + case (2 <= $argc): + if (null !== $name) { + $options = array_shift($spec); + $this->addDisplayGroup($elements, $name, $options); + break; + } + $name = array_shift($spec); + case (3 <= $argc): + $options = array_shift($spec); + default: + $this->addDisplayGroup($elements, $name, $options); + } + } + } + return $this; + } + + /** + * Add multiple display groups (overwrites) + * + * @param array $groups + * @return Zend_Form + */ + public function setDisplayGroups(array $groups) + { + return $this->clearDisplayGroups() + ->addDisplayGroups($groups); + } + + /** + * Return a display group + * + * @param string $name + * @return Zend_Form_DisplayGroup|null + */ + public function getDisplayGroup($name) + { + $name = (string) $name; + if (isset($this->_displayGroups[$name])) { + return $this->_displayGroups[$name]; + } + + return null; + } + + /** + * Return all display groups + * + * @return array + */ + public function getDisplayGroups() + { + return $this->_displayGroups; + } + + /** + * Remove a display group by name + * + * @param string $name + * @return boolean + */ + public function removeDisplayGroup($name) + { + $name = (string) $name; + if (array_key_exists($name, $this->_displayGroups)) { + foreach ($this->_displayGroups[$name] as $key => $element) { + if (array_key_exists($key, $this->_elements)) { + $this->_order[$key] = $element->getOrder(); + $this->_orderUpdated = true; + } + } + unset($this->_displayGroups[$name]); + + if (array_key_exists($name, $this->_order)) { + unset($this->_order[$name]); + $this->_orderUpdated = true; + } + return true; + } + + return false; + } + + /** + * Remove all display groups + * + * @return Zend_Form + */ + public function clearDisplayGroups() + { + foreach ($this->_displayGroups as $key => $group) { + if (array_key_exists($key, $this->_order)) { + unset($this->_order[$key]); + } + foreach ($group as $name => $element) { + if (isset($this->_elements[$name])) { + $this->_order[$name] = $element->getOrder(); + } + $this->_order[$name] = $element->getOrder(); + } + } + $this->_displayGroups = array(); + $this->_orderUpdated = true; + return $this; + } + + + // Processing + + /** + * Populate form + * + * Proxies to {@link setDefaults()} + * + * @param array $values + * @return Zend_Form + */ + public function populate(array $values) + { + return $this->setDefaults($values); + } + + /** + * Determine array key name from given value + * + * Given a value such as foo[bar][baz], returns the last element (in this case, 'baz'). + * + * @param string $value + * @return string + */ + protected function _getArrayName($value) + { + if (!is_string($value) || '' === $value) { + return $value; + } + + if (!strstr($value, '[')) { + return $value; + } + + $endPos = strlen($value) - 1; + if (']' != $value[$endPos]) { + return $value; + } + + $start = strrpos($value, '[') + 1; + $name = substr($value, $start, $endPos - $start); + return $name; + } + + /** + * Extract the value by walking the array using given array path. + * + * Given an array path such as foo[bar][baz], returns the value of the last + * element (in this case, 'baz'). + * + * @param array $value Array to walk + * @param string $arrayPath Array notation path of the part to extract + * @return string + */ + protected function _dissolveArrayValue($value, $arrayPath) + { + // As long as we have more levels + while ($arrayPos = strpos($arrayPath, '[')) { + // Get the next key in the path + $arrayKey = trim(substr($arrayPath, 0, $arrayPos), ']'); + + // Set the potentially final value or the next search point in the array + if (isset($value[$arrayKey])) { + $value = $value[$arrayKey]; + } + + // Set the next search point in the path + $arrayPath = trim(substr($arrayPath, $arrayPos + 1), ']'); + } + + if (isset($value[$arrayPath])) { + $value = $value[$arrayPath]; + } + + return $value; + } + + /** + * Given an array, an optional arrayPath and a key this method + * dissolves the arrayPath and unsets the key within the array + * if it exists. + * + * @param array $array + * @param string|null $arrayPath + * @param string $key + * @return array + */ + protected function _dissolveArrayUnsetKey($array, $arrayPath, $key) + { + $unset =& $array; + $path = trim(strtr((string)$arrayPath, array('[' => '/', ']' => '')), '/'); + $segs = ('' !== $path) ? explode('/', $path) : array(); + + foreach ($segs as $seg) { + if (!array_key_exists($seg, (array)$unset)) { + return $array; + } + $unset =& $unset[$seg]; + } + if (array_key_exists($key, (array)$unset)) { + unset($unset[$key]); + } + return $array; + } + + /** + * Converts given arrayPath to an array and attaches given value at the end of it. + * + * @param mixed $value The value to attach + * @param string $arrayPath Given array path to convert and attach to. + * @return array + */ + protected function _attachToArray($value, $arrayPath) + { + // As long as we have more levels + while ($arrayPos = strrpos($arrayPath, '[')) { + // Get the next key in the path + $arrayKey = trim(substr($arrayPath, $arrayPos + 1), ']'); + + // Attach + $value = array($arrayKey => $value); + + // Set the next search point in the path + $arrayPath = trim(substr($arrayPath, 0, $arrayPos), ']'); + } + + $value = array($arrayPath => $value); + + return $value; + } + + /** + * Returns a one dimensional numerical indexed array with the + * Elements, SubForms and Elements from DisplayGroups as Values. + * + * Subitems are inserted based on their order Setting if set, + * otherwise they are appended, the resulting numerical index + * may differ from the order value. + * + * @access protected + * @return array + */ + public function getElementsAndSubFormsOrdered() + { + $ordered = array(); + foreach ($this->_order as $name => $order) { + $order = isset($order) ? $order : count($ordered); + if ($this->$name instanceof Zend_Form_Element || + $this->$name instanceof Zend_Form) { + array_splice($ordered, $order, 0, array($this->$name)); + } else if ($this->$name instanceof Zend_Form_DisplayGroup) { + $subordered = array(); + foreach ($this->$name->getElements() as $element) { + $suborder = $element->getOrder(); + $suborder = (null !== $suborder) ? $suborder : count($subordered); + array_splice($subordered, $suborder, 0, array($element)); + } + if (!empty($subordered)) { + array_splice($ordered, $order, 0, $subordered); + } + } + } + return $ordered; + } + + /** + * This is a helper function until php 5.3 is widespreaded + * + * @param array $into + * @access protected + * @return void + */ + protected function _array_replace_recursive(array $into) + { + $fromArrays = array_slice(func_get_args(),1); + + foreach ($fromArrays as $from) { + foreach ($from as $key => $value) { + if (is_array($value)) { + if (!isset($into[$key])) { + $into[$key] = array(); + } + $into[$key] = $this->_array_replace_recursive($into[$key], $from[$key]); + } else { + $into[$key] = $value; + } + } + } + return $into; + } + + /** + * Validate the form + * + * @param array $data + * @return boolean + */ + public function isValid($data) + { + if (!is_array($data)) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(__METHOD__ . ' expects an array'); + } + $translator = $this->getTranslator(); + $valid = true; + $eBelongTo = null; + + if ($this->isArray()) { + $eBelongTo = $this->getElementsBelongTo(); + $data = $this->_dissolveArrayValue($data, $eBelongTo); + } + $context = $data; + foreach ($this->getElements() as $key => $element) { + if (null !== $translator && !$element->hasTranslator()) { + $element->setTranslator($translator); + } + $check = $data; + if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) { + $check = $this->_dissolveArrayValue($data, $belongsTo); + } + if (!isset($check[$key])) { + $valid = $element->isValid(null, $context) && $valid; + } else { + $valid = $element->isValid($check[$key], $context) && $valid; + $data = $this->_dissolveArrayUnsetKey($data, $belongsTo, $key); + } + } + foreach ($this->getSubForms() as $key => $form) { + if (null !== $translator && !$form->hasTranslator()) { + $form->setTranslator($translator); + } + if (isset($data[$key]) && !$form->isArray()) { + $valid = $form->isValid($data[$key]) && $valid; + } else { + $valid = $form->isValid($data) && $valid; + } + } + + $this->_errorsExist = !$valid; + + // If manually flagged as an error, return invalid status + if ($this->_errorsForced) { + return false; + } + + return $valid; + } + + /** + * Validate a partial form + * + * Does not check for required flags. + * + * @param array $data + * @return boolean + */ + public function isValidPartial(array $data) + { + $eBelongTo = null; + + if ($this->isArray()) { + $eBelongTo = $this->getElementsBelongTo(); + $data = $this->_dissolveArrayValue($data, $eBelongTo); + } + + $translator = $this->getTranslator(); + $valid = true; + $context = $data; + + foreach ($this->getElements() as $key => $element) { + $check = $data; + if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) { + $check = $this->_dissolveArrayValue($data, $belongsTo); + } + if (isset($check[$key])) { + if (null !== $translator && !$element->hasTranslator()) { + $element->setTranslator($translator); + } + $valid = $element->isValid($check[$key], $context) && $valid; + $data = $this->_dissolveArrayUnsetKey($data, $belongsTo, $key); + } + } + foreach ($this->getSubForms() as $key => $form) { + if (null !== $translator && !$form->hasTranslator()) { + $form->setTranslator($translator); + } + if (isset($data[$key]) && !$form->isArray()) { + $valid = $form->isValidPartial($data[$key]) && $valid; + } else { + $valid = $form->isValidPartial($data) && $valid; + } + } + + $this->_errorsExist = !$valid; + return $valid; + } + + /** + * Process submitted AJAX data + * + * Checks if provided $data is valid, via {@link isValidPartial()}. If so, + * it returns JSON-encoded boolean true. If not, it returns JSON-encoded + * error messages (as returned by {@link getMessages()}). + * + * @param array $data + * @return string JSON-encoded boolean true or error messages + */ + public function processAjax(array $data) + { + // require_once 'Zend/Json.php'; + if ($this->isValidPartial($data)) { + return Zend_Json::encode(true); + } + $messages = $this->getMessages(); + return Zend_Json::encode($messages); + } + + /** + * Add a custom error message to return in the event of failed validation + * + * @param string $message + * @return Zend_Form + */ + public function addErrorMessage($message) + { + $this->_errorMessages[] = (string) $message; + return $this; + } + + /** + * Add multiple custom error messages to return in the event of failed validation + * + * @param array $messages + * @return Zend_Form + */ + public function addErrorMessages(array $messages) + { + foreach ($messages as $message) { + $this->addErrorMessage($message); + } + return $this; + } + + /** + * Same as addErrorMessages(), but clears custom error message stack first + * + * @param array $messages + * @return Zend_Form + */ + public function setErrorMessages(array $messages) + { + $this->clearErrorMessages(); + return $this->addErrorMessages($messages); + } + + /** + * Retrieve custom error messages + * + * @return array + */ + public function getErrorMessages() + { + return $this->_errorMessages; + } + + /** + * Clear custom error messages stack + * + * @return Zend_Form + */ + public function clearErrorMessages() + { + $this->_errorMessages = array(); + return $this; + } + + /** + * Mark the element as being in a failed validation state + * + * @return Zend_Form + */ + public function markAsError() + { + $this->_errorsExist = true; + $this->_errorsForced = true; + return $this; + } + + /** + * Add an error message and mark element as failed validation + * + * @param string $message + * @return Zend_Form + */ + public function addError($message) + { + $this->addErrorMessage($message); + $this->markAsError(); + return $this; + } + + /** + * Add multiple error messages and flag element as failed validation + * + * @param array $messages + * @return Zend_Form + */ + public function addErrors(array $messages) + { + foreach ($messages as $message) { + $this->addError($message); + } + return $this; + } + + /** + * Overwrite any previously set error messages and flag as failed validation + * + * @param array $messages + * @return Zend_Form + */ + public function setErrors(array $messages) + { + $this->clearErrorMessages(); + return $this->addErrors($messages); + } + + + public function persistData() + { + } + + /** + * Are there errors in the form? + * + * @return bool + */ + public function isErrors() + { + return $this->_errorsExist; + } + + /** + * Get error codes for all elements failing validation + * + * @param string $name + * @return array + */ + public function getErrors($name = null, $suppressArrayNotation = false) + { + $errors = array(); + if (null !== $name) { + if (isset($this->_elements[$name])) { + return $this->getElement($name)->getErrors(); + } else if (isset($this->_subForms[$name])) { + return $this->getSubForm($name)->getErrors(null, true); + } + } + + foreach ($this->_elements as $key => $element) { + $errors[$key] = $element->getErrors(); + } + foreach ($this->getSubForms() as $key => $subForm) { + $merge = array(); + if (!$subForm->isArray()) { + $merge[$key] = $subForm->getErrors(); + } else { + $merge = $this->_attachToArray($subForm->getErrors(null, true), + $subForm->getElementsBelongTo()); + } + $errors = $this->_array_replace_recursive($errors, $merge); + } + + if (!$suppressArrayNotation && + $this->isArray() && + !$this->_getIsRendered()) { + $errors = $this->_attachToArray($errors, $this->getElementsBelongTo()); + } + + return $errors; + } + + /** + * Retrieve error messages from elements failing validations + * + * @param string $name + * @param bool $suppressArrayNotation + * @return array + */ + public function getMessages($name = null, $suppressArrayNotation = false) + { + if (null !== $name) { + if (isset($this->_elements[$name])) { + return $this->getElement($name)->getMessages(); + } else if (isset($this->_subForms[$name])) { + return $this->getSubForm($name)->getMessages(null, true); + } + foreach ($this->getSubForms() as $key => $subForm) { + if ($subForm->isArray()) { + $belongTo = $subForm->getElementsBelongTo(); + if ($name == $this->_getArrayName($belongTo)) { + return $subForm->getMessages(null, true); + } + } + } + } + + $customMessages = $this->_getErrorMessages(); + if ($this->isErrors() && !empty($customMessages)) { + return $customMessages; + } + + $messages = array(); + + foreach ($this->getElements() as $name => $element) { + $eMessages = $element->getMessages(); + if (!empty($eMessages)) { + $messages[$name] = $eMessages; + } + } + + foreach ($this->getSubForms() as $key => $subForm) { + $merge = $subForm->getMessages(null, true); + if (!empty($merge)) { + if (!$subForm->isArray()) { + $merge = array($key => $merge); + } else { + $merge = $this->_attachToArray($merge, + $subForm->getElementsBelongTo()); + } + $messages = $this->_array_replace_recursive($messages, $merge); + } + } + + if (!$suppressArrayNotation && + $this->isArray() && + !$this->_getIsRendered()) { + $messages = $this->_attachToArray($messages, $this->getElementsBelongTo()); + } + + return $messages; + } + + /** + * Retrieve translated custom error messages + * Proxies to {@link _getErrorMessages()}. + * + * @return array + */ + public function getCustomMessages() + { + return $this->_getErrorMessages(); + } + + + // Rendering + + /** + * Set view object + * + * @param Zend_View_Interface $view + * @return Zend_Form + */ + public function setView(Zend_View_Interface $view = null) + { + $this->_view = $view; + return $this; + } + + /** + * Retrieve view object + * + * If none registered, attempts to pull from ViewRenderer. + * + * @return Zend_View_Interface|null + */ + public function getView() + { + if (null === $this->_view) { + // require_once 'Zend/Controller/Action/HelperBroker.php'; + $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); + $this->setView($viewRenderer->view); + } + + return $this->_view; + } + + /** + * Instantiate a decorator based on class name or class name fragment + * + * @param string $name + * @param null|array $options + * @return Zend_Form_Decorator_Interface + */ + protected function _getDecorator($name, $options) + { + $class = $this->getPluginLoader(self::DECORATOR)->load($name); + if (null === $options) { + $decorator = new $class; + } else { + $decorator = new $class($options); + } + + return $decorator; + } + + /** + * Add a decorator for rendering the element + * + * @param string|Zend_Form_Decorator_Interface $decorator + * @param array|Zend_Config $options Options with which to initialize decorator + * @return Zend_Form + */ + public function addDecorator($decorator, $options = null) + { + if ($decorator instanceof Zend_Form_Decorator_Interface) { + $name = get_class($decorator); + } elseif (is_string($decorator)) { + $name = $decorator; + $decorator = array( + 'decorator' => $name, + 'options' => $options, + ); + } elseif (is_array($decorator)) { + foreach ($decorator as $name => $spec) { + break; + } + if (is_numeric($name)) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid alias provided to addDecorator; must be alphanumeric string'); + } + if (is_string($spec)) { + $decorator = array( + 'decorator' => $spec, + 'options' => $options, + ); + } elseif ($spec instanceof Zend_Form_Decorator_Interface) { + $decorator = $spec; + } + } else { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid decorator provided to addDecorator; must be string or Zend_Form_Decorator_Interface'); + } + + $this->_decorators[$name] = $decorator; + + return $this; + } + + /** + * Add many decorators at once + * + * @param array $decorators + * @return Zend_Form + */ + public function addDecorators(array $decorators) + { + foreach ($decorators as $decoratorName => $decoratorInfo) { + if (is_string($decoratorInfo) || + $decoratorInfo instanceof Zend_Form_Decorator_Interface) { + if (!is_numeric($decoratorName)) { + $this->addDecorator(array($decoratorName => $decoratorInfo)); + } else { + $this->addDecorator($decoratorInfo); + } + } elseif (is_array($decoratorInfo)) { + $argc = count($decoratorInfo); + $options = array(); + if (isset($decoratorInfo['decorator'])) { + $decorator = $decoratorInfo['decorator']; + if (isset($decoratorInfo['options'])) { + $options = $decoratorInfo['options']; + } + $this->addDecorator($decorator, $options); + } else { + switch (true) { + case (0 == $argc): + break; + case (1 <= $argc): + $decorator = array_shift($decoratorInfo); + case (2 <= $argc): + $options = array_shift($decoratorInfo); + default: + $this->addDecorator($decorator, $options); + break; + } + } + } else { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid decorator passed to addDecorators()'); + } + } + + return $this; + } + + /** + * Overwrite all decorators + * + * @param array $decorators + * @return Zend_Form + */ + public function setDecorators(array $decorators) + { + $this->clearDecorators(); + return $this->addDecorators($decorators); + } + + /** + * Retrieve a registered decorator + * + * @param string $name + * @return false|Zend_Form_Decorator_Abstract + */ + public function getDecorator($name) + { + if (!isset($this->_decorators[$name])) { + $len = strlen($name); + foreach ($this->_decorators as $localName => $decorator) { + if ($len > strlen($localName)) { + continue; + } + + if (0 === substr_compare($localName, $name, -$len, $len, true)) { + if (is_array($decorator)) { + return $this->_loadDecorator($decorator, $localName); + } + return $decorator; + } + } + return false; + } + + if (is_array($this->_decorators[$name])) { + return $this->_loadDecorator($this->_decorators[$name], $name); + } + + return $this->_decorators[$name]; + } + + /** + * Retrieve all decorators + * + * @return array + */ + public function getDecorators() + { + foreach ($this->_decorators as $key => $value) { + if (is_array($value)) { + $this->_loadDecorator($value, $key); + } + } + return $this->_decorators; + } + + /** + * Remove a single decorator + * + * @param string $name + * @return bool + */ + public function removeDecorator($name) + { + $decorator = $this->getDecorator($name); + if ($decorator) { + if (array_key_exists($name, $this->_decorators)) { + unset($this->_decorators[$name]); + } else { + $class = get_class($decorator); + if (!array_key_exists($class, $this->_decorators)) { + return false; + } + unset($this->_decorators[$class]); + } + return true; + } + + return false; + } + + /** + * Clear all decorators + * + * @return Zend_Form + */ + public function clearDecorators() + { + $this->_decorators = array(); + return $this; + } + + /** + * Set all element decorators as specified + * + * @param array $decorators + * @param array|null $elements Specific elements to decorate or exclude from decoration + * @param bool $include Whether $elements is an inclusion or exclusion list + * @return Zend_Form + */ + public function setElementDecorators(array $decorators, array $elements = null, $include = true) + { + if (is_array($elements)) { + if ($include) { + $elementObjs = array(); + foreach ($elements as $name) { + if (null !== ($element = $this->getElement($name))) { + $elementObjs[] = $element; + } + } + } else { + $elementObjs = $this->getElements(); + foreach ($elements as $name) { + if (array_key_exists($name, $elementObjs)) { + unset($elementObjs[$name]); + } + } + } + } else { + $elementObjs = $this->getElements(); + } + + foreach ($elementObjs as $element) { + $element->setDecorators($decorators); + } + + $this->_elementDecorators = $decorators; + + return $this; + } + + /** + * Set all display group decorators as specified + * + * @param array $decorators + * @return Zend_Form + */ + public function setDisplayGroupDecorators(array $decorators) + { + foreach ($this->getDisplayGroups() as $group) { + $group->setDecorators($decorators); + } + + return $this; + } + + /** + * Set all subform decorators as specified + * + * @param array $decorators + * @return Zend_Form + */ + public function setSubFormDecorators(array $decorators) + { + foreach ($this->getSubForms() as $form) { + $form->setDecorators($decorators); + } + + return $this; + } + + /** + * Render form + * + * @param Zend_View_Interface $view + * @return string + */ + public function render(Zend_View_Interface $view = null) + { + if (null !== $view) { + $this->setView($view); + } + + $content = ''; + foreach ($this->getDecorators() as $decorator) { + $decorator->setElement($this); + $content = $decorator->render($content); + } + $this->_setIsRendered(); + return $content; + } + + /** + * Serialize as string + * + * Proxies to {@link render()}. + * + * @return string + */ + public function __toString() + { + try { + $return = $this->render(); + return $return; + } catch (Exception $e) { + $message = "Exception caught by form: " . $e->getMessage() + . "\nStack Trace:\n" . $e->getTraceAsString(); + trigger_error($message, E_USER_WARNING); + return ''; + } + } + + + // Localization: + + /** + * Set translator object + * + * @param Zend_Translate|Zend_Translate_Adapter|null $translator + * @return Zend_Form + */ + public function setTranslator($translator = null) + { + if (null === $translator) { + $this->_translator = null; + } elseif ($translator instanceof Zend_Translate_Adapter) { + $this->_translator = $translator; + } elseif ($translator instanceof Zend_Translate) { + $this->_translator = $translator->getAdapter(); + } else { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid translator specified'); + } + + return $this; + } + + /** + * Set global default translator object + * + * @param Zend_Translate|Zend_Translate_Adapter|null $translator + * @return void + */ + public static function setDefaultTranslator($translator = null) + { + if (null === $translator) { + self::$_translatorDefault = null; + } elseif ($translator instanceof Zend_Translate_Adapter) { + self::$_translatorDefault = $translator; + } elseif ($translator instanceof Zend_Translate) { + self::$_translatorDefault = $translator->getAdapter(); + } else { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid translator specified'); + } + } + + /** + * Retrieve translator object + * + * @return Zend_Translate|null + */ + public function getTranslator() + { + if ($this->translatorIsDisabled()) { + return null; + } + + if (null === $this->_translator) { + return self::getDefaultTranslator(); + } + + return $this->_translator; + } + + /** + * Does this form have its own specific translator? + * + * @return bool + */ + public function hasTranslator() + { + return (bool)$this->_translator; + } + + /** + * Get global default translator object + * + * @return null|Zend_Translate + */ + public static function getDefaultTranslator() + { + if (null === self::$_translatorDefault) { + // require_once 'Zend/Registry.php'; + if (Zend_Registry::isRegistered('Zend_Translate')) { + $translator = Zend_Registry::get('Zend_Translate'); + if ($translator instanceof Zend_Translate_Adapter) { + return $translator; + } elseif ($translator instanceof Zend_Translate) { + return $translator->getAdapter(); + } + } + } + return self::$_translatorDefault; + } + + /** + * Is there a default translation object set? + * + * @return boolean + */ + public static function hasDefaultTranslator() + { + return (bool)self::$_translatorDefault; + } + + /** + * Indicate whether or not translation should be disabled + * + * @param bool $flag + * @return Zend_Form + */ + public function setDisableTranslator($flag) + { + $this->_translatorDisabled = (bool) $flag; + return $this; + } + + /** + * Is translation disabled? + * + * @return bool + */ + public function translatorIsDisabled() + { + return $this->_translatorDisabled; + } + + /** + * Overloading: access to elements, form groups, and display groups + * + * @param string $name + * @return Zend_Form_Element|Zend_Form|null + */ + public function __get($name) + { + if (isset($this->_elements[$name])) { + return $this->_elements[$name]; + } elseif (isset($this->_subForms[$name])) { + return $this->_subForms[$name]; + } elseif (isset($this->_displayGroups[$name])) { + return $this->_displayGroups[$name]; + } + + return null; + } + + /** + * Overloading: access to elements, form groups, and display groups + * + * @param string $name + * @param Zend_Form_Element|Zend_Form $value + * @return void + * @throws Zend_Form_Exception for invalid $value + */ + public function __set($name, $value) + { + if ($value instanceof Zend_Form_Element) { + $this->addElement($value, $name); + return; + } elseif ($value instanceof Zend_Form) { + $this->addSubForm($value, $name); + return; + } elseif (is_array($value)) { + $this->addDisplayGroup($value, $name); + return; + } + + // require_once 'Zend/Form/Exception.php'; + if (is_object($value)) { + $type = get_class($value); + } else { + $type = gettype($value); + } + throw new Zend_Form_Exception('Only form elements and groups may be overloaded; variable of type "' . $type . '" provided'); + } + + /** + * Overloading: access to elements, form groups, and display groups + * + * @param string $name + * @return boolean + */ + public function __isset($name) + { + if (isset($this->_elements[$name]) + || isset($this->_subForms[$name]) + || isset($this->_displayGroups[$name])) + { + return true; + } + + return false; + } + + /** + * Overloading: access to elements, form groups, and display groups + * + * @param string $name + * @return void + */ + public function __unset($name) + { + if (isset($this->_elements[$name])) { + unset($this->_elements[$name]); + } elseif (isset($this->_subForms[$name])) { + unset($this->_subForms[$name]); + } elseif (isset($this->_displayGroups[$name])) { + unset($this->_displayGroups[$name]); + } + } + + /** + * Overloading: allow rendering specific decorators + * + * Call renderDecoratorName() to render a specific decorator. + * + * @param string $method + * @param array $args + * @return string + * @throws Zend_Form_Exception for invalid decorator or invalid method call + */ + public function __call($method, $args) + { + if ('render' == substr($method, 0, 6)) { + $decoratorName = substr($method, 6); + if (false !== ($decorator = $this->getDecorator($decoratorName))) { + $decorator->setElement($this); + $seed = ''; + if (0 < count($args)) { + $seed = array_shift($args); + } + if ($decoratorName === 'FormElements' || + $decoratorName === 'PrepareElements') { + $this->_setIsRendered(); + } + return $decorator->render($seed); + } + + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Decorator by name %s does not exist', $decoratorName)); + } + + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Method %s does not exist', $method)); + } + + // Interfaces: Iterator, Countable + + /** + * Current element/subform/display group + * + * @return Zend_Form_Element|Zend_Form_DisplayGroup|Zend_Form + */ + public function current() + { + $this->_sort(); + current($this->_order); + $key = key($this->_order); + + if (isset($this->_elements[$key])) { + return $this->getElement($key); + } elseif (isset($this->_subForms[$key])) { + return $this->getSubForm($key); + } elseif (isset($this->_displayGroups[$key])) { + return $this->getDisplayGroup($key); + } else { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Corruption detected in form; invalid key ("%s") found in internal iterator', (string) $key)); + } + } + + /** + * Current element/subform/display group name + * + * @return string + */ + public function key() + { + $this->_sort(); + return key($this->_order); + } + + /** + * Move pointer to next element/subform/display group + * + * @return void + */ + public function next() + { + $this->_sort(); + next($this->_order); + } + + /** + * Move pointer to beginning of element/subform/display group loop + * + * @return void + */ + public function rewind() + { + $this->_sort(); + reset($this->_order); + } + + /** + * Determine if current element/subform/display group is valid + * + * @return bool + */ + public function valid() + { + $this->_sort(); + return (current($this->_order) !== false); + } + + /** + * Count of elements/subforms that are iterable + * + * @return int + */ + public function count() + { + return count($this->_order); + } + + /** + * Set flag to disable loading default decorators + * + * @param bool $flag + * @return Zend_Form + */ + public function setDisableLoadDefaultDecorators($flag) + { + $this->_disableLoadDefaultDecorators = (bool) $flag; + return $this; + } + + /** + * Should we load the default decorators? + * + * @return bool + */ + public function loadDefaultDecoratorsIsDisabled() + { + return $this->_disableLoadDefaultDecorators; + } + + /** + * Load the default decorators + * + * @return void + */ + public function loadDefaultDecorators() + { + if ($this->loadDefaultDecoratorsIsDisabled()) { + return $this; + } + + $decorators = $this->getDecorators(); + if (empty($decorators)) { + $this->addDecorator('FormElements') + ->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')) + ->addDecorator('Form'); + } + return $this; + } + + /** + * Sort items according to their order + * + * @return void + */ + protected function _sort() + { + if ($this->_orderUpdated) { + $items = array(); + $index = 0; + foreach ($this->_order as $key => $order) { + if (null === $order) { + if (null === ($order = $this->{$key}->getOrder())) { + while (array_search($index, $this->_order, true)) { + ++$index; + } + $items[$index] = $key; + ++$index; + } else { + $items[$order] = $key; + } + } else { + $items[$order] = $key; + } + } + + $items = array_flip($items); + asort($items); + $this->_order = $items; + $this->_orderUpdated = false; + } + } + + /** + * Lazy-load a decorator + * + * @param array $decorator Decorator type and options + * @param mixed $name Decorator name or alias + * @return Zend_Form_Decorator_Interface + */ + protected function _loadDecorator(array $decorator, $name) + { + $sameName = false; + if ($name == $decorator['decorator']) { + $sameName = true; + } + + $instance = $this->_getDecorator($decorator['decorator'], $decorator['options']); + if ($sameName) { + $newName = get_class($instance); + $decoratorNames = array_keys($this->_decorators); + $order = array_flip($decoratorNames); + $order[$newName] = $order[$name]; + $decoratorsExchange = array(); + unset($order[$name]); + asort($order); + foreach ($order as $key => $index) { + if ($key == $newName) { + $decoratorsExchange[$key] = $instance; + continue; + } + $decoratorsExchange[$key] = $this->_decorators[$key]; + } + $this->_decorators = $decoratorsExchange; + } else { + $this->_decorators[$name] = $instance; + } + + return $instance; + } + + /** + * Retrieve optionally translated custom error messages + * + * @return array + */ + protected function _getErrorMessages() + { + $messages = $this->getErrorMessages(); + $translator = $this->getTranslator(); + if (null !== $translator) { + foreach ($messages as $key => $message) { + $messages[$key] = $translator->translate($message); + } + } + return $messages; + } +} diff --git a/libs/Zend/Form/Decorator/Abstract.php b/libs/Zend/Form/Decorator/Abstract.php new file mode 100644 index 0000000000000000000000000000000000000000..a81dc6659b6e000c0d128fb04ddd5fdde838de2f --- /dev/null +++ b/libs/Zend/Form/Decorator/Abstract.php @@ -0,0 +1,254 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Decorator_Interface */ +// require_once 'Zend/Form/Decorator/Interface.php'; + +/** + * Zend_Form_Decorator_Abstract + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Abstract.php 21147 2010-02-23 14:42:04Z yoshida@zend.co.jp $ + */ +abstract class Zend_Form_Decorator_Abstract implements Zend_Form_Decorator_Interface +{ + /** + * Placement constants + */ + const APPEND = 'APPEND'; + const PREPEND = 'PREPEND'; + + /** + * Default placement: append + * @var string + */ + protected $_placement = 'APPEND'; + + /** + * @var Zend_Form_Element|Zend_Form + */ + protected $_element; + + /** + * Decorator options + * @var array + */ + protected $_options = array(); + + /** + * Separator between new content and old + * @var string + */ + protected $_separator = PHP_EOL; + + /** + * Constructor + * + * @param array|Zend_Config $options + * @return void + */ + public function __construct($options = null) + { + if (is_array($options)) { + $this->setOptions($options); + } elseif ($options instanceof Zend_Config) { + $this->setConfig($options); + } + } + + /** + * Set options + * + * @param array $options + * @return Zend_Form_Decorator_Abstract + */ + public function setOptions(array $options) + { + $this->_options = $options; + return $this; + } + + /** + * Set options from config object + * + * @param Zend_Config $config + * @return Zend_Form_Decorator_Abstract + */ + public function setConfig(Zend_Config $config) + { + return $this->setOptions($config->toArray()); + } + + /** + * Set option + * + * @param string $key + * @param mixed $value + * @return Zend_Form_Decorator_Abstract + */ + public function setOption($key, $value) + { + $this->_options[(string) $key] = $value; + return $this; + } + + /** + * Get option + * + * @param string $key + * @return mixed + */ + public function getOption($key) + { + $key = (string) $key; + if (isset($this->_options[$key])) { + return $this->_options[$key]; + } + + return null; + } + + /** + * Retrieve options + * + * @return array + */ + public function getOptions() + { + return $this->_options; + } + + /** + * Remove single option + * + * @param mixed $key + * @return void + */ + public function removeOption($key) + { + if (null !== $this->getOption($key)) { + unset($this->_options[$key]); + return true; + } + + return false; + } + + /** + * Clear all options + * + * @return Zend_Form_Decorator_Abstract + */ + public function clearOptions() + { + $this->_options = array(); + return $this; + } + + /** + * Set current form element + * + * @param Zend_Form_Element|Zend_Form $element + * @return Zend_Form_Decorator_Abstract + * @throws Zend_Form_Decorator_Exception on invalid element type + */ + public function setElement($element) + { + if ((!$element instanceof Zend_Form_Element) + && (!$element instanceof Zend_Form) + && (!$element instanceof Zend_Form_DisplayGroup)) + { + // require_once 'Zend/Form/Decorator/Exception.php'; + throw new Zend_Form_Decorator_Exception('Invalid element type passed to decorator'); + } + + $this->_element = $element; + return $this; + } + + /** + * Retrieve current element + * + * @return Zend_Form_Element|Zend_Form + */ + public function getElement() + { + return $this->_element; + } + + /** + * Determine if decorator should append or prepend content + * + * @return string + */ + public function getPlacement() + { + $placement = $this->_placement; + if (null !== ($placementOpt = $this->getOption('placement'))) { + $placementOpt = strtoupper($placementOpt); + switch ($placementOpt) { + case self::APPEND: + case self::PREPEND: + $placement = $this->_placement = $placementOpt; + break; + case false: + $placement = $this->_placement = null; + break; + default: + break; + } + $this->removeOption('placement'); + } + + return $placement; + } + + /** + * Retrieve separator to use between old and new content + * + * @return string + */ + public function getSeparator() + { + $separator = $this->_separator; + if (null !== ($separatorOpt = $this->getOption('separator'))) { + $separator = $this->_separator = (string) $separatorOpt; + $this->removeOption('separator'); + } + return $separator; + } + + /** + * Decorate content and/or element + * + * @param string $content + * @return string + * @throws Zend_Form_Decorator_Exception when unimplemented + */ + public function render($content) + { + // require_once 'Zend/Form/Decorator/Exception.php'; + throw new Zend_Form_Decorator_Exception('render() not implemented'); + } +} diff --git a/libs/Zend/Form/Decorator/Callback.php b/libs/Zend/Form/Decorator/Callback.php new file mode 100644 index 0000000000000000000000000000000000000000..56bb0779c1c0311a6d258ed4b4c187b22f640d67 --- /dev/null +++ b/libs/Zend/Form/Decorator/Callback.php @@ -0,0 +1,128 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Decorator_Abstract */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** + * Zend_Form_Decorator_Callback + * + * Execute an arbitrary callback to decorate an element. Callbacks should take + * three arguments, $content, $element, and $options: + * + * function mycallback($content, $element, array $options) + * { + * } + * + * and should return a string. ($options are whatever options were provided to + * the decorator.) + * + * To specify a callback, pass a valid callback as the 'callback' option. + * + * Callback results will be either appended, prepended, or replace the provided + * content. To replace the content, specify a placement of boolean false; + * defaults to append content. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Callback.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Decorator_Callback extends Zend_Form_Decorator_Abstract +{ + /** + * Callback + * @var string|array + */ + protected $_callback; + + /** + * Set callback + * + * @param callback $callback + * @return Zend_Form_Decorator_Callback + * @throws Zend_Form_Exception + */ + public function setCallback($callback) + { + if (!is_callable($callback)) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid callback provided to callback decorator'); + } + $this->_callback = $callback; + return $this; + } + + /** + * Get registered callback + * + * If not previously registered, checks to see if it exists in registered + * options. + * + * @return null|string|array + */ + public function getCallback() + { + if (null === $this->_callback) { + if (null !== ($callback = $this->getOption('callback'))) { + $this->setCallback($callback); + $this->removeOption('callback'); + } + } + + return $this->_callback; + } + + /** + * Render + * + * If no callback registered, returns callback. Otherwise, gets return + * value of callback and either appends, prepends, or replaces passed in + * content. + * + * @param string $content + * @return string + */ + public function render($content) + { + $callback = $this->getCallback(); + if (null === $callback) { + return $content; + } + + $placement = $this->getPlacement(); + $separator = $this->getSeparator(); + + $response = call_user_func($callback, $content, $this->getElement(), $this->getOptions()); + + switch ($placement) { + case self::APPEND: + return $content . $separator . $response; + case self::PREPEND: + return $response . $separator . $content; + default: + // replace content + return $response; + } + } +} diff --git a/libs/Zend/Form/Decorator/Captcha.php b/libs/Zend/Form/Decorator/Captcha.php new file mode 100644 index 0000000000000000000000000000000000000000..0c2705559c6fd819543ffc76cbc4f3a77f9b69ac --- /dev/null +++ b/libs/Zend/Form/Decorator/Captcha.php @@ -0,0 +1,72 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** @see Zend_Form_Decorator_Abstract */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** + * Captcha generic decorator + * + * Adds captcha adapter output + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Captcha.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Decorator_Captcha extends Zend_Form_Decorator_Abstract +{ + /** + * Render captcha + * + * @param string $content + * @return string + */ + public function render($content) + { + $element = $this->getElement(); + if (!method_exists($element, 'getCaptcha')) { + return $content; + } + + $view = $element->getView(); + if (null === $view) { + return $content; + } + + $placement = $this->getPlacement(); + $separator = $this->getSeparator(); + + $captcha = $element->getCaptcha(); + $markup = $captcha->render($view, $element); + switch ($placement) { + case 'PREPEND': + $content = $markup . $separator . $content; + break; + case 'APPEND': + default: + $content = $content . $separator . $markup; + } + return $content; + } +} diff --git a/libs/Zend/Form/Decorator/Captcha/Word.php b/libs/Zend/Form/Decorator/Captcha/Word.php new file mode 100644 index 0000000000000000000000000000000000000000..ca6f3cc12c72b7eff74076dcaceb5ee710374cf8 --- /dev/null +++ b/libs/Zend/Form/Decorator/Captcha/Word.php @@ -0,0 +1,78 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** @see Zend_Form_Decorator_Abstract */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** + * Word-based captcha decorator + * + * Adds hidden field for ID and text input field for captcha text + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Word.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Decorator_Captcha_Word extends Zend_Form_Decorator_Abstract +{ + /** + * Render captcha + * + * @param string $content + * @return string + */ + public function render($content) + { + $element = $this->getElement(); + $view = $element->getView(); + if (null === $view) { + return $content; + } + + $name = $element->getFullyQualifiedName(); + + $hiddenName = $name . '[id]'; + $textName = $name . '[input]'; + + $label = $element->getDecorator("Label"); + if($label) { + $label->setOption("id", $element->getId()."-input"); + } + + $placement = $this->getPlacement(); + $separator = $this->getSeparator(); + + $hidden = $view->formHidden($hiddenName, $element->getValue(), $element->getAttribs()); + $text = $view->formText($textName, '', $element->getAttribs()); + switch ($placement) { + case 'PREPEND': + $content = $hidden . $separator . $text . $separator . $content; + break; + case 'APPEND': + default: + $content = $content . $separator . $hidden . $separator . $text; + } + return $content; + } +} diff --git a/libs/Zend/Form/Decorator/Description.php b/libs/Zend/Form/Decorator/Description.php new file mode 100644 index 0000000000000000000000000000000000000000..0175c1405d822f89092cfa15426e58d451178640 --- /dev/null +++ b/libs/Zend/Form/Decorator/Description.php @@ -0,0 +1,199 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Decorator_Abstract */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** + * Zend_Form_Decorator_Description + * + * Accepts the options: + * - separator: separator to use between label and content (defaults to PHP_EOL) + * - placement: whether to append or prepend label to content (defaults to prepend) + * - tag: if set, used to wrap the label in an additional HTML tag + * - class: if set, override default class used with HTML tag + * - escape: whether or not to escape description (true by default) + * + * Any other options passed will be used as HTML attributes of the HTML tag used. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Description.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Decorator_Description extends Zend_Form_Decorator_Abstract +{ + /** + * Whether or not to escape the description + * @var bool + */ + protected $_escape; + + /** + * Default placement: append + * @var string + */ + protected $_placement = 'APPEND'; + + /** + * HTML tag with which to surround description + * @var string + */ + protected $_tag; + + /** + * Set HTML tag with which to surround description + * + * @param string $tag + * @return Zend_Form_Decorator_Description + */ + public function setTag($tag) + { + $this->_tag = (string) $tag; + return $this; + } + + /** + * Get HTML tag, if any, with which to surround description + * + * @return string + */ + public function getTag() + { + if (null === $this->_tag) { + $tag = $this->getOption('tag'); + if (null !== $tag) { + $this->removeOption('tag'); + } else { + $tag = 'p'; + } + + $this->setTag($tag); + return $tag; + } + + return $this->_tag; + } + + /** + * Get class with which to define description + * + * Defaults to 'hint' + * + * @return string + */ + public function getClass() + { + $class = $this->getOption('class'); + if (null === $class) { + $class = 'hint'; + $this->setOption('class', $class); + } + + return $class; + } + + /** + * Set whether or not to escape description + * + * @param bool $flag + * @return Zend_Form_Decorator_Description + */ + public function setEscape($flag) + { + $this->_escape = (bool) $flag; + return $this; + } + + /** + * Get escape flag + * + * @return true + */ + public function getEscape() + { + if (null === $this->_escape) { + if (null !== ($escape = $this->getOption('escape'))) { + $this->setEscape($escape); + $this->removeOption('escape'); + } else { + $this->setEscape(true); + } + } + + return $this->_escape; + } + + /** + * Render a description + * + * @param string $content + * @return string + */ + public function render($content) + { + $element = $this->getElement(); + $view = $element->getView(); + if (null === $view) { + return $content; + } + + $description = $element->getDescription(); + $description = trim($description); + + if (!empty($description) && (null !== ($translator = $element->getTranslator()))) { + $description = $translator->translate($description); + } + + if (empty($description)) { + return $content; + } + + $separator = $this->getSeparator(); + $placement = $this->getPlacement(); + $tag = $this->getTag(); + $class = $this->getClass(); + $escape = $this->getEscape(); + + $options = $this->getOptions(); + + if ($escape) { + $description = $view->escape($description); + } + + if (!empty($tag)) { + // require_once 'Zend/Form/Decorator/HtmlTag.php'; + $options['tag'] = $tag; + $decorator = new Zend_Form_Decorator_HtmlTag($options); + $description = $decorator->render($description); + } + + switch ($placement) { + case self::PREPEND: + return $description . $separator . $content; + case self::APPEND: + default: + return $content . $separator . $description; + } + } +} diff --git a/libs/Zend/Form/Decorator/DtDdWrapper.php b/libs/Zend/Form/Decorator/DtDdWrapper.php new file mode 100644 index 0000000000000000000000000000000000000000..0d645172b9ef4d55b74a11d5a52e634be8d9f3a9 --- /dev/null +++ b/libs/Zend/Form/Decorator/DtDdWrapper.php @@ -0,0 +1,70 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Decorator_Abstract */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** + * Zend_Form_Decorator_DtDdWrapper + * + * Creates an empty <dt> item, and wraps the content in a <dd>. Used as a + * default decorator for subforms and display groups. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: DtDdWrapper.php 22129 2010-05-06 11:20:39Z alab $ + */ +class Zend_Form_Decorator_DtDdWrapper extends Zend_Form_Decorator_Abstract +{ + /** + * Default placement: surround content + * @var string + */ + protected $_placement = null; + + /** + * Render + * + * Renders as the following: + * <dt>$dtLabel</dt> + * <dd>$content</dd> + * + * $dtLabel can be set via 'dtLabel' option, defaults to '\ ' + * + * @param string $content + * @return string + */ + public function render($content) + { + $elementName = $this->getElement()->getName(); + + $dtLabel = $this->getOption('dtLabel'); + if( null === $dtLabel ) { + $dtLabel = ' '; + } + + return '<dt id="' . $elementName . '-label">' . $dtLabel . '</dt>' . + '<dd id="' . $elementName . '-element">' . $content . '</dd>'; + } +} diff --git a/libs/Zend/Form/Decorator/Errors.php b/libs/Zend/Form/Decorator/Errors.php new file mode 100644 index 0000000000000000000000000000000000000000..9818dc4e72905604f8a358d5286eecd3ab11dfdf --- /dev/null +++ b/libs/Zend/Form/Decorator/Errors.php @@ -0,0 +1,69 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Decorator_Abstract */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** + * Zend_Form_Decorator_Errors + * + * Any options passed will be used as HTML attributes of the ul tag for the errors. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Errors.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Decorator_Errors extends Zend_Form_Decorator_Abstract +{ + /** + * Render errors + * + * @param string $content + * @return string + */ + public function render($content) + { + $element = $this->getElement(); + $view = $element->getView(); + if (null === $view) { + return $content; + } + + $errors = $element->getMessages(); + if (empty($errors)) { + return $content; + } + + $separator = $this->getSeparator(); + $placement = $this->getPlacement(); + $errors = $view->formErrors($errors, $this->getOptions()); + + switch ($placement) { + case self::APPEND: + return $content . $separator . $errors; + case self::PREPEND: + return $errors . $separator . $content; + } + } +} diff --git a/libs/Zend/Form/Decorator/Exception.php b/libs/Zend/Form/Decorator/Exception.php new file mode 100644 index 0000000000000000000000000000000000000000..6f85840d7f0f648401166f1edf8f34388de82063 --- /dev/null +++ b/libs/Zend/Form/Decorator/Exception.php @@ -0,0 +1,37 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @version $Id: Exception.php 20096 2010-01-06 02:05:09Z bkarwin $ + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Exception */ +// require_once 'Zend/Form/Exception.php'; + +/** + * Exception for Zend_Form component. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Form_Decorator_Exception extends Zend_Form_Exception +{ +} diff --git a/libs/Zend/Form/Decorator/Fieldset.php b/libs/Zend/Form/Decorator/Fieldset.php new file mode 100644 index 0000000000000000000000000000000000000000..93a1255474de763fcaeaf96bd90f13362fa3e5d4 --- /dev/null +++ b/libs/Zend/Form/Decorator/Fieldset.php @@ -0,0 +1,155 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Decorator_Abstract */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** + * Zend_Form_Decorator_Fieldset + * + * Any options passed will be used as HTML attributes of the fieldset tag. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Fieldset.php 21967 2010-04-21 23:22:00Z alab $ + */ +class Zend_Form_Decorator_Fieldset extends Zend_Form_Decorator_Abstract +{ + /** + * Attribs that should be removed prior to rendering + * @var array + */ + public $stripAttribs = array( + 'action', + 'enctype', + 'helper', + 'method', + 'name', + ); + + /** + * Fieldset legend + * @var string + */ + protected $_legend; + + /** + * Default placement: surround content + * @var string + */ + protected $_placement = null; + + /** + * Get options + * + * Merges in element attributes as well. + * + * @return array + */ + public function getOptions() + { + $options = parent::getOptions(); + if (null !== ($element = $this->getElement())) { + $attribs = $element->getAttribs(); + $options = array_merge($options, $attribs); + $this->setOptions($options); + } + return $options; + } + + /** + * Set legend + * + * @param string $value + * @return Zend_Form_Decorator_Fieldset + */ + public function setLegend($value) + { + $this->_legend = (string) $value; + return $this; + } + + /** + * Get legend + * + * @return string + */ + public function getLegend() + { + $legend = $this->_legend; + if ((null === $legend) && (null !== ($element = $this->getElement()))) { + if (method_exists($element, 'getLegend')) { + $legend = $element->getLegend(); + $this->setLegend($legend); + } + } + if ((null === $legend) && (null !== ($legend = $this->getOption('legend')))) { + $this->setLegend($legend); + $this->removeOption('legend'); + } + + return $legend; + } + + /** + * Render a fieldset + * + * @param string $content + * @return string + */ + public function render($content) + { + $element = $this->getElement(); + $view = $element->getView(); + if (null === $view) { + return $content; + } + + $legend = $this->getLegend(); + $attribs = $this->getOptions(); + $name = $element->getFullyQualifiedName(); + $id = (string)$element->getId(); + + if (!array_key_exists('id', $attribs) && '' !== $id) { + $attribs['id'] = 'fieldset-' . $id; + } + + if (null !== $legend) { + if (null !== ($translator = $element->getTranslator())) { + $legend = $translator->translate($legend); + } + + $attribs['legend'] = $legend; + } + + foreach (array_keys($attribs) as $attrib) { + $testAttrib = strtolower($attrib); + if (in_array($testAttrib, $this->stripAttribs)) { + unset($attribs[$attrib]); + } + } + + return $view->fieldset($name, $content, $attribs); + } +} diff --git a/libs/Zend/Form/Decorator/File.php b/libs/Zend/Form/Decorator/File.php new file mode 100644 index 0000000000000000000000000000000000000000..926f4901863c7169b8a08cc022331df0914f033d --- /dev/null +++ b/libs/Zend/Form/Decorator/File.php @@ -0,0 +1,142 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Decorator_Abstract */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** Zend_Form_Decorator_Marker_File_Interface */ +// require_once 'Zend/Form/Decorator/Marker/File/Interface.php'; + +/** Zend_File_Transfer_Adapter_Http */ +// require_once 'Zend/File/Transfer/Adapter/Http.php'; + +/** + * Zend_Form_Decorator_File + * + * Fixes the rendering for all subform and multi file elements + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: File.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Decorator_File + extends Zend_Form_Decorator_Abstract + implements Zend_Form_Decorator_Marker_File_Interface +{ + /** + * Attributes that should not be passed to helper + * @var array + */ + protected $_attribBlacklist = array('helper', 'placement', 'separator', 'value'); + + /** + * Default placement: append + * @var string + */ + protected $_placement = 'APPEND'; + + /** + * Get attributes to pass to file helper + * + * @return array + */ + public function getAttribs() + { + $attribs = $this->getOptions(); + + if (null !== ($element = $this->getElement())) { + $attribs = array_merge($attribs, $element->getAttribs()); + } + + foreach ($this->_attribBlacklist as $key) { + if (array_key_exists($key, $attribs)) { + unset($attribs[$key]); + } + } + + return $attribs; + } + + /** + * Render a form file + * + * @param string $content + * @return string + */ + public function render($content) + { + $element = $this->getElement(); + if (!$element instanceof Zend_Form_Element) { + return $content; + } + + $view = $element->getView(); + if (!$view instanceof Zend_View_Interface) { + return $content; + } + + $name = $element->getName(); + $attribs = $this->getAttribs(); + if (!array_key_exists('id', $attribs)) { + $attribs['id'] = $name; + } + + $separator = $this->getSeparator(); + $placement = $this->getPlacement(); + $markup = array(); + $size = $element->getMaxFileSize(); + if ($size > 0) { + $element->setMaxFileSize(0); + $markup[] = $view->formHidden('MAX_FILE_SIZE', $size); + } + + if (Zend_File_Transfer_Adapter_Http::isApcAvailable()) { + $markup[] = $view->formHidden(ini_get('apc.rfc1867_name'), uniqid(), array('id' => 'progress_key')); + } else if (Zend_File_Transfer_Adapter_Http::isUploadProgressAvailable()) { + $markup[] = $view->formHidden('UPLOAD_IDENTIFIER', uniqid(), array('id' => 'progress_key')); + } + + if ($element->isArray()) { + $name .= "[]"; + $count = $element->getMultiFile(); + for ($i = 0; $i < $count; ++$i) { + $htmlAttribs = $attribs; + $htmlAttribs['id'] .= '-' . $i; + $markup[] = $view->formFile($name, $htmlAttribs); + } + } else { + $markup[] = $view->formFile($name, $attribs); + } + + $markup = implode($separator, $markup); + + switch ($placement) { + case self::PREPEND: + return $markup . $separator . $content; + case self::APPEND: + default: + return $content . $separator . $markup; + } + } +} diff --git a/libs/Zend/Form/Decorator/Form.php b/libs/Zend/Form/Decorator/Form.php new file mode 100644 index 0000000000000000000000000000000000000000..c6dbd6c12fb8e0789414ea70c2821fd539dc0615 --- /dev/null +++ b/libs/Zend/Form/Decorator/Form.php @@ -0,0 +1,134 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Decorator_Abstract */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** + * Zend_Form_Decorator_Form + * + * Render a Zend_Form object. + * + * Accepts following options: + * - separator: Separator to use between elements + * - helper: which view helper to use when rendering form. Should accept three + * arguments, string content, a name, and an array of attributes. + * + * Any other options passed will be used as HTML attributes of the form tag. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Form.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Decorator_Form extends Zend_Form_Decorator_Abstract +{ + /** + * Default view helper + * @var string + */ + protected $_helper = 'form'; + + /** + * Set view helper for rendering form + * + * @param string $helper + * @return Zend_Form_Decorator_Form + */ + public function setHelper($helper) + { + $this->_helper = (string) $helper; + return $this; + } + + /** + * Get view helper for rendering form + * + * @return string + */ + public function getHelper() + { + if (null !== ($helper = $this->getOption('helper'))) { + $this->setHelper($helper); + $this->removeOption('helper'); + } + return $this->_helper; + } + + /** + * Retrieve decorator options + * + * Assures that form action and method are set, and sets appropriate + * encoding type if current method is POST. + * + * @return array + */ + public function getOptions() + { + if (null !== ($element = $this->getElement())) { + if ($element instanceof Zend_Form) { + $element->getAction(); + $method = $element->getMethod(); + if ($method == Zend_Form::METHOD_POST) { + $this->setOption('enctype', 'application/x-www-form-urlencoded'); + } + foreach ($element->getAttribs() as $key => $value) { + $this->setOption($key, $value); + } + } elseif ($element instanceof Zend_Form_DisplayGroup) { + foreach ($element->getAttribs() as $key => $value) { + $this->setOption($key, $value); + } + } + } + + if (isset($this->_options['method'])) { + $this->_options['method'] = strtolower($this->_options['method']); + } + + return $this->_options; + } + + /** + * Render a form + * + * Replaces $content entirely from currently set element. + * + * @param string $content + * @return string + */ + public function render($content) + { + $form = $this->getElement(); + $view = $form->getView(); + if (null === $view) { + return $content; + } + + $helper = $this->getHelper(); + $attribs = $this->getOptions(); + $name = $form->getFullyQualifiedName(); + $attribs['id'] = $form->getId(); + return $view->$helper($name, $attribs, $content); + } +} diff --git a/libs/Zend/Form/Decorator/FormElements.php b/libs/Zend/Form/Decorator/FormElements.php new file mode 100644 index 0000000000000000000000000000000000000000..5e80dccfa5cb2e447610a4eb82ff126143ba33f3 --- /dev/null +++ b/libs/Zend/Form/Decorator/FormElements.php @@ -0,0 +1,126 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Decorator_Abstract */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** + * Zend_Form_Decorator_FormElements + * + * Render all form elements registered with current form + * + * Accepts following options: + * - separator: Separator to use between elements + * + * Any other options passed will be used as HTML attributes of the form tag. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: FormElements.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Decorator_FormElements extends Zend_Form_Decorator_Abstract +{ + /** + * Merges given two belongsTo (array notation) strings + * + * @param string $baseBelongsTo + * @param string $belongsTo + * @return string + */ + public function mergeBelongsTo($baseBelongsTo, $belongsTo) + { + $endOfArrayName = strpos($belongsTo, '['); + + if ($endOfArrayName === false) { + return $baseBelongsTo . '[' . $belongsTo . ']'; + } + + $arrayName = substr($belongsTo, 0, $endOfArrayName); + + return $baseBelongsTo . '[' . $arrayName . ']' . substr($belongsTo, $endOfArrayName); + } + + /** + * Render form elements + * + * @param string $content + * @return string + */ + public function render($content) + { + $form = $this->getElement(); + if ((!$form instanceof Zend_Form) && (!$form instanceof Zend_Form_DisplayGroup)) { + return $content; + } + + $belongsTo = ($form instanceof Zend_Form) ? $form->getElementsBelongTo() : null; + $elementContent = ''; + $separator = $this->getSeparator(); + $translator = $form->getTranslator(); + $items = array(); + $view = $form->getView(); + foreach ($form as $item) { + $item->setView($view) + ->setTranslator($translator); + if ($item instanceof Zend_Form_Element) { + $item->setBelongsTo($belongsTo); + } elseif (!empty($belongsTo) && ($item instanceof Zend_Form)) { + if ($item->isArray()) { + $name = $this->mergeBelongsTo($belongsTo, $item->getElementsBelongTo()); + $item->setElementsBelongTo($name, true); + } else { + $item->setElementsBelongTo($belongsTo, true); + } + } elseif (!empty($belongsTo) && ($item instanceof Zend_Form_DisplayGroup)) { + foreach ($item as $element) { + $element->setBelongsTo($belongsTo); + } + } + + $items[] = $item->render(); + + if (($item instanceof Zend_Form_Element_File) + || (($item instanceof Zend_Form) + && (Zend_Form::ENCTYPE_MULTIPART == $item->getEnctype())) + || (($item instanceof Zend_Form_DisplayGroup) + && (Zend_Form::ENCTYPE_MULTIPART == $item->getAttrib('enctype'))) + ) { + if ($form instanceof Zend_Form) { + $form->setEnctype(Zend_Form::ENCTYPE_MULTIPART); + } elseif ($form instanceof Zend_Form_DisplayGroup) { + $form->setAttrib('enctype', Zend_Form::ENCTYPE_MULTIPART); + } + } + } + $elementContent = implode($separator, $items); + + switch ($this->getPlacement()) { + case self::PREPEND: + return $elementContent . $separator . $content; + case self::APPEND: + default: + return $content . $separator . $elementContent; + } + } +} diff --git a/libs/Zend/Form/Decorator/FormErrors.php b/libs/Zend/Form/Decorator/FormErrors.php new file mode 100644 index 0000000000000000000000000000000000000000..31a3d1570a22a0eaea1a82ec00e64e3c0d7b60dc --- /dev/null +++ b/libs/Zend/Form/Decorator/FormErrors.php @@ -0,0 +1,461 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Decorator_Abstract */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** + * Zend_Form_Decorator_FormErrors + * + * Displays all form errors in one view. + * + * Any options passed will be used as HTML attributes of the ul tag for the errors. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: FormErrors.php 22317 2010-05-29 10:13:31Z alab $ + */ +class Zend_Form_Decorator_FormErrors extends Zend_Form_Decorator_Abstract +{ + /** + * Default values for markup options + * @var array + */ + protected $_defaults = array( + 'ignoreSubForms' => false, + 'showCustomFormErrors' => true, + 'onlyCustomFormErrors' => false, + 'markupElementLabelEnd' => '</b>', + 'markupElementLabelStart' => '<b>', + 'markupListEnd' => '</ul>', + 'markupListItemEnd' => '</li>', + 'markupListItemStart' => '<li>', + 'markupListStart' => '<ul class="form-errors">', + ); + + /**#@+ + * Markup options + * @var string + */ + protected $_ignoreSubForms; + protected $_showCustomFormErrors; + protected $_onlyCustomFormErrors; + protected $_markupElementLabelEnd; + protected $_markupElementLabelStart; + protected $_markupListEnd; + protected $_markupListItemEnd; + protected $_markupListItemStart; + protected $_markupListStart; + /**#@-*/ + + /** + * Render errors + * + * @param string $content + * @return string + */ + public function render($content) + { + $form = $this->getElement(); + if (!$form instanceof Zend_Form) { + return $content; + } + + $view = $form->getView(); + if (null === $view) { + return $content; + } + + $this->initOptions(); + $markup = $this->_recurseForm($form, $view); + + if (empty($markup)) { + return $content; + } + + $markup = $this->getMarkupListStart() + . $markup + . $this->getMarkupListEnd(); + + switch ($this->getPlacement()) { + case self::APPEND: + return $content . $this->getSeparator() . $markup; + case self::PREPEND: + return $markup . $this->getSeparator() . $content; + } + } + + /** + * Initialize options + * + * @return void + */ + public function initOptions() + { + $this->getMarkupElementLabelEnd(); + $this->getMarkupElementLabelStart(); + $this->getMarkupListEnd(); + $this->getMarkupListItemEnd(); + $this->getMarkupListItemStart(); + $this->getMarkupListStart(); + $this->getPlacement(); + $this->getSeparator(); + $this->ignoreSubForms(); + $this->getShowCustomFormErrors(); + $this->getOnlyCustomFormErrors(); + } + + /** + * Retrieve markupElementLabelStart + * + * @return string + */ + public function getMarkupElementLabelStart() + { + if (null === $this->_markupElementLabelStart) { + if (null === ($markupElementLabelStart = $this->getOption('markupElementLabelStart'))) { + $this->setMarkupElementLabelStart($this->_defaults['markupElementLabelStart']); + } else { + $this->setMarkupElementLabelStart($markupElementLabelStart); + $this->removeOption('markupElementLabelStart'); + } + } + + return $this->_markupElementLabelStart; + } + + /** + * Set markupElementLabelStart + * + * @param string $markupElementLabelStart + * @return Zend_Form_Decorator_FormErrors + */ + public function setMarkupElementLabelStart($markupElementLabelStart) + { + $this->_markupElementLabelStart = $markupElementLabelStart; + return $this; + } + + /** + * Retrieve markupElementLabelEnd + * + * @return string + */ + public function getMarkupElementLabelEnd() + { + if (null === $this->_markupElementLabelEnd) { + if (null === ($markupElementLabelEnd = $this->getOption('markupElementLabelEnd'))) { + $this->setMarkupElementLabelEnd($this->_defaults['markupElementLabelEnd']); + } else { + $this->setMarkupElementLabelEnd($markupElementLabelEnd); + $this->removeOption('markupElementLabelEnd'); + } + } + + return $this->_markupElementLabelEnd; + } + + /** + * Set markupElementLabelEnd + * + * @param string $markupElementLabelEnd + * @return Zend_Form_Decorator_FormErrors + */ + public function setMarkupElementLabelEnd($markupElementLabelEnd) + { + $this->_markupElementLabelEnd = $markupElementLabelEnd; + return $this; + } + + /** + * Retrieve markupListStart + * + * @return string + */ + public function getMarkupListStart() + { + if (null === $this->_markupListStart) { + if (null === ($markupListStart = $this->getOption('markupListStart'))) { + $this->setMarkupListStart($this->_defaults['markupListStart']); + } else { + $this->setMarkupListStart($markupListStart); + $this->removeOption('markupListStart'); + } + } + + return $this->_markupListStart; + } + + /** + * Set markupListStart + * + * @param string $markupListStart + * @return Zend_Form_Decorator_FormErrors + */ + public function setMarkupListStart($markupListStart) + { + $this->_markupListStart = $markupListStart; + return $this; + } + + /** + * Retrieve markupListEnd + * + * @return string + */ + public function getMarkupListEnd() + { + if (null === $this->_markupListEnd) { + if (null === ($markupListEnd = $this->getOption('markupListEnd'))) { + $this->setMarkupListEnd($this->_defaults['markupListEnd']); + } else { + $this->setMarkupListEnd($markupListEnd); + $this->removeOption('markupListEnd'); + } + } + + return $this->_markupListEnd; + } + + /** + * Set markupListEnd + * + * @param string $markupListEnd + * @return Zend_Form_Decorator_FormErrors + */ + public function setMarkupListEnd($markupListEnd) + { + $this->_markupListEnd = $markupListEnd; + return $this; + } + + /** + * Retrieve markupListItemStart + * + * @return string + */ + public function getMarkupListItemStart() + { + if (null === $this->_markupListItemStart) { + if (null === ($markupListItemStart = $this->getOption('markupListItemStart'))) { + $this->setMarkupListItemStart($this->_defaults['markupListItemStart']); + } else { + $this->setMarkupListItemStart($markupListItemStart); + $this->removeOption('markupListItemStart'); + } + } + + return $this->_markupListItemStart; + } + + /** + * Set markupListItemStart + * + * @param string $markupListItemStart + * @return Zend_Form_Decorator_FormErrors + */ + public function setMarkupListItemStart($markupListItemStart) + { + $this->_markupListItemStart = $markupListItemStart; + return $this; + } + + /** + * Retrieve markupListItemEnd + * + * @return string + */ + public function getMarkupListItemEnd() + { + if (null === $this->_markupListItemEnd) { + if (null === ($markupListItemEnd = $this->getOption('markupListItemEnd'))) { + $this->setMarkupListItemEnd($this->_defaults['markupListItemEnd']); + } else { + $this->setMarkupListItemEnd($markupListItemEnd); + $this->removeOption('markupListItemEnd'); + } + } + + return $this->_markupListItemEnd; + } + + /** + * Set markupListItemEnd + * + * @param string $markupListItemEnd + * @return Zend_Form_Decorator_FormErrors + */ + public function setMarkupListItemEnd($markupListItemEnd) + { + $this->_markupListItemEnd = $markupListItemEnd; + return $this; + } + + /** + * Retrieve ignoreSubForms + * + * @return bool + */ + public function ignoreSubForms() + { + if (null === $this->_ignoreSubForms) { + if (null === ($ignoreSubForms = $this->getOption('ignoreSubForms'))) { + $this->setIgnoreSubForms($this->_defaults['ignoreSubForms']); + } else { + $this->setIgnoreSubForms($ignoreSubForms); + $this->removeOption('ignoreSubForms'); + } + } + + return $this->_ignoreSubForms; + } + + /** + * Set ignoreSubForms + * + * @param bool $ignoreSubForms + * @return Zend_Form_Decorator_FormErrors + */ + public function setIgnoreSubForms($ignoreSubForms) + { + $this->_ignoreSubForms = (bool) $ignoreSubForms; + return $this; + } + + /** + * Get showCustomFormErrors + * + * @return bool + */ + public function getShowCustomFormErrors() + { + if (null === $this->_showCustomFormErrors) { + if (null === ($how = $this->getOption('showCustomFormErrors'))) { + $this->setShowCustomFormErrors($this->_defaults['showCustomFormErrors']); + } else { + $this->setShowCustomFormErrors($show); + $this->removeOption('showCustomFormErrors'); + } + } + return $this->_showCustomFormErrors; + } + + /** + * Set showCustomFormErrors + * + * @param bool $showCustomFormErrors + * @return Zend_Form_Decorator_FormErrors + */ + public function setShowCustomFormErrors($showCustomFormErrors) + { + $this->_showCustomFormErrors = (bool)$showCustomFormErrors; + return $this; + } + + /** + * Get onlyCustomFormErrors + * + * @return bool + */ + public function getOnlyCustomFormErrors() + { + if (null === $this->_onlyCustomFormErrors) { + if (null === ($show = $this->getOption('onlyCustomFormErrors'))) { + $this->setOnlyCustomFormErrors($this->_defaults['onlyCustomFormErrors']); + } else { + $this->setOnlyCustomFormErrors($show); + $this->removeOption('onlyCustomFormErrors'); + } + } + return $this->_onlyCustomFormErrors; + } + + /** + * Set onlyCustomFormErrors, whether to display elements messages + * in addition to custom form messages. + * + * @param bool $onlyCustomFormErrors + * @return Zend_Form_Decorator_FormErrors + */ + public function setOnlyCustomFormErrors($onlyCustomFormErrors) + { + $this->_onlyCustomFormErrors = (bool)$onlyCustomFormErrors; + return $this; + } + + /** + * Render element label + * + * @param Zend_Form_Element $element + * @param Zend_View_Interface $view + * @return string + */ + public function renderLabel(Zend_Form_Element $element, Zend_View_Interface $view) + { + $label = $element->getLabel(); + if (empty($label)) { + $label = $element->getName(); + } + + return $this->getMarkupElementLabelStart() + . $view->escape($label) + . $this->getMarkupElementLabelEnd(); + } + + /** + * Recurse through a form object, rendering errors + * + * @param Zend_Form $form + * @param Zend_View_Interface $view + * @return string + */ + protected function _recurseForm(Zend_Form $form, Zend_View_Interface $view) + { + $content = ''; + + $custom = $form->getCustomMessages(); + if ($this->getShowCustomFormErrors() && count($custom)) { + $content .= $this->getMarkupListItemStart() + . $view->formErrors($custom, $this->getOptions()) + . $this->getMarkupListItemEnd(); + } + foreach ($form->getElementsAndSubFormsOrdered() as $subitem) { + if ($subitem instanceof Zend_Form_Element && !$this->getOnlyCustomFormErrors()) { + $messages = $subitem->getMessages(); + if (count($messages)) { + $subitem->setView($view); + $content .= $this->getMarkupListItemStart() + . $this->renderLabel($subitem, $view) + . $view->formErrors($messages, $this->getOptions()) + . $this->getMarkupListItemEnd(); + } + } else if ($subitem instanceof Zend_Form && !$this->ignoreSubForms()) { + $content .= $this->getMarkupListStart() + . $this->_recurseForm($subitem, $view) + . $this->getMarkupListEnd(); + } + } + return $content; + } +} diff --git a/libs/Zend/Form/Decorator/HtmlTag.php b/libs/Zend/Form/Decorator/HtmlTag.php new file mode 100644 index 0000000000000000000000000000000000000000..111ae768ec71ac4e64fabc5538923ad07a75e306 --- /dev/null +++ b/libs/Zend/Form/Decorator/HtmlTag.php @@ -0,0 +1,251 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** + * @see Zend_Form_Decorator_Abstract + */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** + * Zend_Form_Decorator_Element_HtmlTag + * + * Wraps content in an HTML block tag. + * + * Options accepted are: + * - tag: tag to use in decorator + * - noAttribs: do not render attributes in the opening tag + * - placement: 'append' or 'prepend'. If 'append', renders opening and + * closing tag after content; if prepend, renders opening and closing tag + * before content. + * - openOnly: render opening tag only + * - closeOnly: render closing tag only + * + * Any other options passed are processed as HTML attributes of the tag. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: HtmlTag.php 20104 2010-01-06 21:26:01Z matthew $ + */ +class Zend_Form_Decorator_HtmlTag extends Zend_Form_Decorator_Abstract +{ + /** + * Character encoding to use when escaping attributes + * @var string + */ + protected $_encoding; + + /** + * Placement; default to surround content + * @var string + */ + protected $_placement = null; + + /** + * HTML tag to use + * @var string + */ + protected $_tag; + + /** + * @var Zend_Filter + */ + protected $_tagFilter; + + /** + * Convert options to tag attributes + * + * @return string + */ + protected function _htmlAttribs(array $attribs) + { + $xhtml = ''; + $enc = $this->_getEncoding(); + foreach ((array) $attribs as $key => $val) { + $key = htmlspecialchars($key, ENT_COMPAT, $enc); + if (is_array($val)) { + $val = implode(' ', $val); + } + $val = htmlspecialchars($val, ENT_COMPAT, $enc); + $xhtml .= " $key=\"$val\""; + } + return $xhtml; + } + + /** + * Normalize tag + * + * Ensures tag is alphanumeric characters only, and all lowercase. + * + * @param string $tag + * @return string + */ + public function normalizeTag($tag) + { + if (!isset($this->_tagFilter)) { + // require_once 'Zend/Filter.php'; + // require_once 'Zend/Filter/Alnum.php'; + // require_once 'Zend/Filter/StringToLower.php'; + $this->_tagFilter = new Zend_Filter(); + $this->_tagFilter->addFilter(new Zend_Filter_Alnum()) + ->addFilter(new Zend_Filter_StringToLower()); + } + return $this->_tagFilter->filter($tag); + } + + /** + * Set tag to use + * + * @param string $tag + * @return Zend_Form_Decorator_HtmlTag + */ + public function setTag($tag) + { + $this->_tag = $this->normalizeTag($tag); + return $this; + } + + /** + * Get tag + * + * If no tag is registered, either via setTag() or as an option, uses 'div'. + * + * @return string + */ + public function getTag() + { + if (null === $this->_tag) { + if (null === ($tag = $this->getOption('tag'))) { + $this->setTag('div'); + } else { + $this->setTag($tag); + $this->removeOption('tag'); + } + } + + return $this->_tag; + } + + /** + * Get the formatted open tag + * + * @param string $tag + * @param array $attribs + * @return string + */ + protected function _getOpenTag($tag, array $attribs = null) + { + $html = '<' . $tag; + if (null !== $attribs) { + $html .= $this->_htmlAttribs($attribs); + } + $html .= '>'; + return $html; + } + + /** + * Get formatted closing tag + * + * @param string $tag + * @return string + */ + protected function _getCloseTag($tag) + { + return '</' . $tag . '>'; + } + + /** + * Render content wrapped in an HTML tag + * + * @param string $content + * @return string + */ + public function render($content) + { + $tag = $this->getTag(); + $placement = $this->getPlacement(); + $noAttribs = $this->getOption('noAttribs'); + $openOnly = $this->getOption('openOnly'); + $closeOnly = $this->getOption('closeOnly'); + $this->removeOption('noAttribs'); + $this->removeOption('openOnly'); + $this->removeOption('closeOnly'); + + $attribs = null; + if (!$noAttribs) { + $attribs = $this->getOptions(); + } + + switch ($placement) { + case self::APPEND: + if ($closeOnly) { + return $content . $this->_getCloseTag($tag); + } + if ($openOnly) { + return $content . $this->_getOpenTag($tag, $attribs); + } + return $content + . $this->_getOpenTag($tag, $attribs) + . $this->_getCloseTag($tag); + case self::PREPEND: + if ($closeOnly) { + return $this->_getCloseTag($tag) . $content; + } + if ($openOnly) { + return $this->_getOpenTag($tag, $attribs) . $content; + } + return $this->_getOpenTag($tag, $attribs) + . $this->_getCloseTag($tag) + . $content; + default: + return (($openOnly || !$closeOnly) ? $this->_getOpenTag($tag, $attribs) : '') + . $content + . (($closeOnly || !$openOnly) ? $this->_getCloseTag($tag) : ''); + } + } + + /** + * Get encoding for use with htmlspecialchars() + * + * @return string + */ + protected function _getEncoding() + { + if (null !== $this->_encoding) { + return $this->_encoding; + } + + if (null === ($element = $this->getElement())) { + $this->_encoding = 'UTF-8'; + } elseif (null === ($view = $element->getView())) { + $this->_encoding = 'UTF-8'; + } elseif (!$view instanceof Zend_View_Abstract + && !method_exists($view, 'getEncoding') + ) { + $this->_encoding = 'UTF-8'; + } else { + $this->_encoding = $view->getEncoding(); + } + return $this->_encoding; + } +} diff --git a/libs/Zend/Form/Decorator/Image.php b/libs/Zend/Form/Decorator/Image.php new file mode 100644 index 0000000000000000000000000000000000000000..de0dc0021c40adebc932302e0c6a44a3d4298032 --- /dev/null +++ b/libs/Zend/Form/Decorator/Image.php @@ -0,0 +1,154 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Decorator_Abstract */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** + * Zend_Form_Decorator_Image + * + * Accepts the options: + * - separator: separator to use between image and content (defaults to PHP_EOL) + * - placement: whether to append or prepend label to content (defaults to append) + * - tag: if set, used to wrap the label in an additional HTML tag + * + * Any other options passed will be used as HTML attributes of the image tag. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Image.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Decorator_Image extends Zend_Form_Decorator_Abstract +{ + /** + * Attributes that should not be passed to helper + * @var array + */ + protected $_attribBlacklist = array('helper', 'placement', 'separator', 'tag'); + + /** + * Default placement: append + * @var string + */ + protected $_placement = 'APPEND'; + + /** + * HTML tag with which to surround image + * @var string + */ + protected $_tag; + + /** + * Set HTML tag with which to surround label + * + * @param string $tag + * @return Zend_Form_Decorator_Image + */ + public function setTag($tag) + { + $this->_tag = (string) $tag; + return $this; + } + + /** + * Get HTML tag, if any, with which to surround label + * + * @return void + */ + public function getTag() + { + if (null === $this->_tag) { + $tag = $this->getOption('tag'); + if (null !== $tag) { + $this->removeOption('tag'); + $this->setTag($tag); + } + return $tag; + } + + return $this->_tag; + } + + /** + * Get attributes to pass to image helper + * + * @return array + */ + public function getAttribs() + { + $attribs = $this->getOptions(); + + if (null !== ($element = $this->getElement())) { + $attribs['alt'] = $element->getLabel(); + $attribs = array_merge($attribs, $element->getAttribs()); + } + + foreach ($this->_attribBlacklist as $key) { + if (array_key_exists($key, $attribs)) { + unset($attribs[$key]); + } + } + + return $attribs; + } + + /** + * Render a form image + * + * @param string $content + * @return string + */ + public function render($content) + { + $element = $this->getElement(); + $view = $element->getView(); + if (null === $view) { + return $content; + } + + $tag = $this->getTag(); + $placement = $this->getPlacement(); + $separator = $this->getSeparator(); + $name = $element->getFullyQualifiedName(); + $attribs = $this->getAttribs(); + $attribs['id'] = $element->getId(); + + $image = $view->formImage($name, $element->getImageValue(), $attribs); + + if (null !== $tag) { + // require_once 'Zend/Form/Decorator/HtmlTag.php'; + $decorator = new Zend_Form_Decorator_HtmlTag(); + $decorator->setOptions(array('tag' => $tag)); + $image = $decorator->render($image); + } + + switch ($placement) { + case self::PREPEND: + return $image . $separator . $content; + case self::APPEND: + default: + return $content . $separator . $image; + } + } +} diff --git a/libs/Zend/Form/Decorator/Interface.php b/libs/Zend/Form/Decorator/Interface.php new file mode 100644 index 0000000000000000000000000000000000000000..4a7d37263db99bcd1977f62170bf37c8fb82c40f --- /dev/null +++ b/libs/Zend/Form/Decorator/Interface.php @@ -0,0 +1,123 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** + * Zend_Form_Decorator_Interface + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Interface.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +interface Zend_Form_Decorator_Interface +{ + /** + * Constructor + * + * Accept options during initialization. + * + * @param array|Zend_Config $options + * @return void + */ + public function __construct($options = null); + + /** + * Set an element to decorate + * + * While the name is "setElement", a form decorator could decorate either + * an element or a form object. + * + * @param mixed $element + * @return Zend_Form_Decorator_Interface + */ + public function setElement($element); + + /** + * Retrieve current element + * + * @return mixed + */ + public function getElement(); + + /** + * Set decorator options from an array + * + * @param array $options + * @return Zend_Form_Decorator_Interface + */ + public function setOptions(array $options); + + /** + * Set decorator options from a config object + * + * @param Zend_Config $config + * @return Zend_Form_Decorator_Interface + */ + public function setConfig(Zend_Config $config); + + /** + * Set a single option + * + * @param string $key + * @param mixed $value + * @return Zend_Form_Decorator_Interface + */ + public function setOption($key, $value); + + /** + * Retrieve a single option + * + * @param string $key + * @return mixed + */ + public function getOption($key); + + /** + * Retrieve decorator options + * + * @return array + */ + public function getOptions(); + + /** + * Delete a single option + * + * @param string $key + * @return bool + */ + public function removeOption($key); + + /** + * Clear all options + * + * @return Zend_Form_Decorator_Interface + */ + public function clearOptions(); + + /** + * Render the element + * + * @param string $content Content to decorate + * @return string + */ + public function render($content); +} diff --git a/libs/Zend/Form/Decorator/Label.php b/libs/Zend/Form/Decorator/Label.php new file mode 100644 index 0000000000000000000000000000000000000000..5840e8f3d537a7e069c5eaf4e110ab1c88891037 --- /dev/null +++ b/libs/Zend/Form/Decorator/Label.php @@ -0,0 +1,332 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Decorator_Abstract */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** + * Zend_Form_Decorator_Label + * + * Accepts the options: + * - separator: separator to use between label and content (defaults to PHP_EOL) + * - placement: whether to append or prepend label to content (defaults to prepend) + * - tag: if set, used to wrap the label in an additional HTML tag + * - opt(ional)Prefix: a prefix to the label to use when the element is optional + * - opt(iona)lSuffix: a suffix to the label to use when the element is optional + * - req(uired)Prefix: a prefix to the label to use when the element is required + * - req(uired)Suffix: a suffix to the label to use when the element is required + * + * Any other options passed will be used as HTML attributes of the label tag. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Label.php 22129 2010-05-06 11:20:39Z alab $ + */ +class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract +{ + /** + * Default placement: prepend + * @var string + */ + protected $_placement = 'PREPEND'; + + /** + * HTML tag with which to surround label + * @var string + */ + protected $_tag; + + /** + * Set element ID + * + * @param string $id + * @return Zend_Form_Decorator_Label + */ + public function setId($id) + { + $this->setOption('id', $id); + return $this; + } + + /** + * Retrieve element ID (used in 'for' attribute) + * + * If none set in decorator, looks first for element 'id' attribute, and + * defaults to element name. + * + * @return string + */ + public function getId() + { + $id = $this->getOption('id'); + if (null === $id) { + if (null !== ($element = $this->getElement())) { + $id = $element->getId(); + $this->setId($id); + } + } + + return $id; + } + + /** + * Set HTML tag with which to surround label + * + * @param string $tag + * @return Zend_Form_Decorator_Label + */ + public function setTag($tag) + { + if (empty($tag)) { + $this->_tag = null; + } else { + $this->_tag = (string) $tag; + } + + $this->removeOption('tag'); + + return $this; + } + + /** + * Get HTML tag, if any, with which to surround label + * + * @return void + */ + public function getTag() + { + if (null === $this->_tag) { + $tag = $this->getOption('tag'); + if (null !== $tag) { + $this->removeOption('tag'); + $this->setTag($tag); + } + return $tag; + } + + return $this->_tag; + } + + /** + * Get class with which to define label + * + * Appends either 'optional' or 'required' to class, depending on whether + * or not the element is required. + * + * @return string + */ + public function getClass() + { + $class = ''; + $element = $this->getElement(); + + $decoratorClass = $this->getOption('class'); + if (!empty($decoratorClass)) { + $class .= ' ' . $decoratorClass; + } + + $type = $element->isRequired() ? 'required' : 'optional'; + + if (!strstr($class, $type)) { + $class .= ' ' . $type; + $class = trim($class); + } + + return $class; + } + + /** + * Load an optional/required suffix/prefix key + * + * @param string $key + * @return void + */ + protected function _loadOptReqKey($key) + { + if (!isset($this->$key)) { + $value = $this->getOption($key); + $this->$key = (string) $value; + if (null !== $value) { + $this->removeOption($key); + } + } + } + + /** + * Overloading + * + * Currently overloads: + * + * - getOpt(ional)Prefix() + * - getOpt(ional)Suffix() + * - getReq(uired)Prefix() + * - getReq(uired)Suffix() + * - setOpt(ional)Prefix() + * - setOpt(ional)Suffix() + * - setReq(uired)Prefix() + * - setReq(uired)Suffix() + * + * @param string $method + * @param array $args + * @return mixed + * @throws Zend_Form_Exception for unsupported methods + */ + public function __call($method, $args) + { + $tail = substr($method, -6); + $head = substr($method, 0, 3); + if (in_array($head, array('get', 'set')) + && (('Prefix' == $tail) || ('Suffix' == $tail)) + ) { + $position = substr($method, -6); + $type = strtolower(substr($method, 3, 3)); + switch ($type) { + case 'req': + $key = 'required' . $position; + break; + case 'opt': + $key = 'optional' . $position; + break; + default: + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Invalid method "%s" called in Label decorator, and detected as type %s', $method, $type)); + } + + switch ($head) { + case 'set': + if (0 === count($args)) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Method "%s" requires at least one argument; none provided', $method)); + } + $value = array_shift($args); + $this->$key = $value; + return $this; + case 'get': + default: + if (null === ($element = $this->getElement())) { + $this->_loadOptReqKey($key); + } elseif (isset($element->$key)) { + $this->$key = (string) $element->$key; + } else { + $this->_loadOptReqKey($key); + } + return $this->$key; + } + } + + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Invalid method "%s" called in Label decorator', $method)); + } + + /** + * Get label to render + * + * @return void + */ + public function getLabel() + { + if (null === ($element = $this->getElement())) { + return ''; + } + + $label = $element->getLabel(); + $label = trim($label); + + if (empty($label)) { + return ''; + } + + if (null !== ($translator = $element->getTranslator())) { + $label = $translator->translate($label); + } + + $optPrefix = $this->getOptPrefix(); + $optSuffix = $this->getOptSuffix(); + $reqPrefix = $this->getReqPrefix(); + $reqSuffix = $this->getReqSuffix(); + $separator = $this->getSeparator(); + + if (!empty($label)) { + if ($element->isRequired()) { + $label = $reqPrefix . $label . $reqSuffix; + } else { + $label = $optPrefix . $label . $optSuffix; + } + } + + return $label; + } + + + /** + * Render a label + * + * @param string $content + * @return string + */ + public function render($content) + { + $element = $this->getElement(); + $view = $element->getView(); + if (null === $view) { + return $content; + } + + $label = $this->getLabel(); + $separator = $this->getSeparator(); + $placement = $this->getPlacement(); + $tag = $this->getTag(); + $id = $this->getId(); + $class = $this->getClass(); + $options = $this->getOptions(); + + + if (empty($label) && empty($tag)) { + return $content; + } + + if (!empty($label)) { + $options['class'] = $class; + $label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options); + } else { + $label = ' '; + } + + if (null !== $tag) { + // require_once 'Zend/Form/Decorator/HtmlTag.php'; + $decorator = new Zend_Form_Decorator_HtmlTag(); + $decorator->setOptions(array('tag' => $tag, + 'id' => $this->getElement()->getName() . '-label')); + + $label = $decorator->render($label); + } + + switch ($placement) { + case self::APPEND: + return $content . $separator . $label; + case self::PREPEND: + return $label . $separator . $content; + } + } +} diff --git a/libs/Zend/Form/Decorator/Marker/File/Interface.php b/libs/Zend/Form/Decorator/Marker/File/Interface.php new file mode 100644 index 0000000000000000000000000000000000000000..47f9b75afb408b358bc87070f6ecf48430067f0a --- /dev/null +++ b/libs/Zend/Form/Decorator/Marker/File/Interface.php @@ -0,0 +1,33 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** + * Zend_Form_Decorator_Marker_File_Interface + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Interface.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +interface Zend_Form_Decorator_Marker_File_Interface +{ +} diff --git a/libs/Zend/Form/Decorator/PrepareElements.php b/libs/Zend/Form/Decorator/PrepareElements.php new file mode 100644 index 0000000000000000000000000000000000000000..5a50537b9cfca7c704ef12f8f14010974d1ef8a8 --- /dev/null +++ b/libs/Zend/Form/Decorator/PrepareElements.php @@ -0,0 +1,90 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Decorator_FormElements */ +// require_once 'Zend/Form/Decorator/FormElements.php'; + +/** + * Zend_Form_Decorator_PrepareElements + * + * Render all form elements registered with current form + * + * Accepts following options: + * - separator: Separator to use between elements + * + * Any other options passed will be used as HTML attributes of the form tag. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: PrepareElements.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Decorator_PrepareElements extends Zend_Form_Decorator_FormElements +{ + /** + * Render form elements + * + * @param string $content + * @return string + */ + public function render($content) + { + $form = $this->getElement(); + if ((!$form instanceof Zend_Form) && (!$form instanceof Zend_Form_DisplayGroup)) { + return $content; + } + + $this->_recursivelyPrepareForm($form); + + return $content; + } + + protected function _recursivelyPrepareForm(Zend_Form $form) + { + $belongsTo = ($form instanceof Zend_Form) ? $form->getElementsBelongTo() : null; + $elementContent = ''; + $separator = $this->getSeparator(); + $translator = $form->getTranslator(); + $view = $form->getView(); + + foreach ($form as $item) { + $item->setView($view) + ->setTranslator($translator); + if ($item instanceof Zend_Form_Element) { + $item->setBelongsTo($belongsTo); + } elseif (!empty($belongsTo) && ($item instanceof Zend_Form)) { + if ($item->isArray()) { + $name = $this->mergeBelongsTo($belongsTo, $item->getElementsBelongTo()); + $item->setElementsBelongTo($name, true); + } else { + $item->setElementsBelongTo($belongsTo, true); + } + $this->_recursivelyPrepareForm($item); + } elseif (!empty($belongsTo) && ($item instanceof Zend_Form_DisplayGroup)) { + foreach ($item as $element) { + $element->setBelongsTo($belongsTo); + } + } + } + } +} diff --git a/libs/Zend/Form/Decorator/Tooltip.php b/libs/Zend/Form/Decorator/Tooltip.php new file mode 100644 index 0000000000000000000000000000000000000000..807f78f2240e121b9e8b6ae24a7686fcb4f9196e --- /dev/null +++ b/libs/Zend/Form/Decorator/Tooltip.php @@ -0,0 +1,58 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Decorator_Abstract */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** + * Zend_Form_Decorator_Tooltip + * + * Will translate the title attribute, if available + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Tooltip.php$ + */ +class Zend_Form_Decorator_Tooltip extends Zend_Form_Decorator_Abstract +{ + /** + * Translates the title attribute if it is available, if the translator is available + * and if the translator is not disable on the element being rendered. + * + * @param string $content + * @return string + */ + public function render($content) + { + if (null !== ($title = $this->getElement()->getAttrib('title'))) { + if (null !== ($translator = $this->getElement()->getTranslator())) { + $title = $translator->translate($title); + } + } + + $this->getElement()->setAttrib('title', $title); + return $content; + } + +} \ No newline at end of file diff --git a/libs/Zend/Form/Decorator/ViewHelper.php b/libs/Zend/Form/Decorator/ViewHelper.php new file mode 100644 index 0000000000000000000000000000000000000000..a84ad60dc5f2950d8c6c457eb20b1b6107209ae1 --- /dev/null +++ b/libs/Zend/Form/Decorator/ViewHelper.php @@ -0,0 +1,256 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Decorator_Abstract */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** + * Zend_Form_Decorator_ViewHelper + * + * Decorate an element by using a view helper to render it. + * + * Accepts the following options: + * - separator: string with which to separate passed in content and generated content + * - placement: whether to append or prepend the generated content to the passed in content + * - helper: the name of the view helper to use + * + * Assumes the view helper accepts three parameters, the name, value, and + * optional attributes; these will be provided by the element. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: ViewHelper.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Decorator_ViewHelper extends Zend_Form_Decorator_Abstract +{ + /** + * Element types that represent buttons + * @var array + */ + protected $_buttonTypes = array( + 'Zend_Form_Element_Button', + 'Zend_Form_Element_Reset', + 'Zend_Form_Element_Submit', + ); + + /** + * View helper to use when rendering + * @var string + */ + protected $_helper; + + /** + * Set view helper to use when rendering + * + * @param string $helper + * @return Zend_Form_Decorator_Element_ViewHelper + */ + public function setHelper($helper) + { + $this->_helper = (string) $helper; + return $this; + } + + /** + * Retrieve view helper for rendering element + * + * @return string + */ + public function getHelper() + { + if (null === $this->_helper) { + $options = $this->getOptions(); + if (isset($options['helper'])) { + $this->setHelper($options['helper']); + $this->removeOption('helper'); + } else { + $element = $this->getElement(); + if (null !== $element) { + if (null !== ($helper = $element->getAttrib('helper'))) { + $this->setHelper($helper); + } else { + $type = $element->getType(); + if ($pos = strrpos($type, '_')) { + $type = substr($type, $pos + 1); + } + $this->setHelper('form' . ucfirst($type)); + } + } + } + } + + return $this->_helper; + } + + /** + * Get name + * + * If element is a Zend_Form_Element, will attempt to namespace it if the + * element belongs to an array. + * + * @return string + */ + public function getName() + { + if (null === ($element = $this->getElement())) { + return ''; + } + + $name = $element->getName(); + + if (!$element instanceof Zend_Form_Element) { + return $name; + } + + if (null !== ($belongsTo = $element->getBelongsTo())) { + $name = $belongsTo . '[' + . $name + . ']'; + } + + if ($element->isArray()) { + $name .= '[]'; + } + + return $name; + } + + /** + * Retrieve element attributes + * + * Set id to element name and/or array item. + * + * @return array + */ + public function getElementAttribs() + { + if (null === ($element = $this->getElement())) { + return null; + } + + $attribs = $element->getAttribs(); + if (isset($attribs['helper'])) { + unset($attribs['helper']); + } + + if (method_exists($element, 'getSeparator')) { + if (null !== ($listsep = $element->getSeparator())) { + $attribs['listsep'] = $listsep; + } + } + + if (isset($attribs['id'])) { + return $attribs; + } + + $id = $element->getName(); + + if ($element instanceof Zend_Form_Element) { + if (null !== ($belongsTo = $element->getBelongsTo())) { + $belongsTo = preg_replace('/\[([^\]]+)\]/', '-$1', $belongsTo); + $id = $belongsTo . '-' . $id; + } + } + + $element->setAttrib('id', $id); + $attribs['id'] = $id; + + return $attribs; + } + + /** + * Get value + * + * If element type is one of the button types, returns the label. + * + * @param Zend_Form_Element $element + * @return string|null + */ + public function getValue($element) + { + if (!$element instanceof Zend_Form_Element) { + return null; + } + + foreach ($this->_buttonTypes as $type) { + if ($element instanceof $type) { + if (stristr($type, 'button')) { + $element->content = $element->getLabel(); + return null; + } + return $element->getLabel(); + } + } + + return $element->getValue(); + } + + /** + * Render an element using a view helper + * + * Determine view helper from 'viewHelper' option, or, if none set, from + * the element type. Then call as + * helper($element->getName(), $element->getValue(), $element->getAttribs()) + * + * @param string $content + * @return string + * @throws Zend_Form_Decorator_Exception if element or view are not registered + */ + public function render($content) + { + $element = $this->getElement(); + + $view = $element->getView(); + if (null === $view) { + // require_once 'Zend/Form/Decorator/Exception.php'; + throw new Zend_Form_Decorator_Exception('ViewHelper decorator cannot render without a registered view object'); + } + + if (method_exists($element, 'getMultiOptions')) { + $element->getMultiOptions(); + } + + $helper = $this->getHelper(); + $separator = $this->getSeparator(); + $value = $this->getValue($element); + $attribs = $this->getElementAttribs(); + $name = $element->getFullyQualifiedName(); + $id = $element->getId(); + $attribs['id'] = $id; + + $helperObject = $view->getHelper($helper); + if (method_exists($helperObject, 'setTranslator')) { + $helperObject->setTranslator($element->getTranslator()); + } + + $elementContent = $view->$helper($name, $value, $attribs, $element->options); + switch ($this->getPlacement()) { + case self::APPEND: + return $content . $separator . $elementContent; + case self::PREPEND: + return $elementContent . $separator . $content; + default: + return $elementContent; + } + } +} diff --git a/libs/Zend/Form/Decorator/ViewScript.php b/libs/Zend/Form/Decorator/ViewScript.php new file mode 100644 index 0000000000000000000000000000000000000000..ebfc70c07b427724393f2b95f1751ed36a4cac7c --- /dev/null +++ b/libs/Zend/Form/Decorator/ViewScript.php @@ -0,0 +1,144 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Decorator_Abstract */ +// require_once 'Zend/Form/Decorator/Abstract.php'; + +/** + * Zend_Form_Decorator_ViewScript + * + * Render a view script as a decorator + * + * Accepts the options: + * - separator: separator to use between view script content and provided content (defaults to PHP_EOL) + * - placement: whether to append or prepend view script content to provided content (defaults to prepend) + * - viewScript: view script to use + * + * The view script is rendered as a partial; the element being decorated is + * passed in as the 'element' variable: + * <code> + * // in view script: + * echo $this->element->getLabel(); + * </code> + * + * Any options other than separator, placement, and viewScript are passed to + * the partial as local variables. + * + * @category Zend + * @package Zend_Form + * @subpackage Decorator + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: ViewScript.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Decorator_ViewScript extends Zend_Form_Decorator_Abstract +{ + /** + * Default placement: append + * @var string + */ + protected $_placement = 'APPEND'; + + /** + * View script to render + * @var string + */ + protected $_viewScript; + + /** + * Set view script + * + * @param string $script + * @return Zend_Form_Decorator_ViewScript + */ + public function setViewScript($script) + { + $this->_viewScript = (string) $script; + return $this; + } + + /** + * Get view script + * + * @return string|null + */ + public function getViewScript() + { + if (null === $this->_viewScript) { + if (null !== ($element = $this->getElement())) { + if (null !== ($viewScript = $element->getAttrib('viewScript'))) { + $this->setViewScript($viewScript); + return $viewScript; + } + } + + if (null !== ($viewScript = $this->getOption('viewScript'))) { + $this->setViewScript($viewScript) + ->removeOption('viewScript'); + } + } + + return $this->_viewScript; + } + + /** + * Render a view script + * + * @param string $content + * @return string + */ + public function render($content) + { + $element = $this->getElement(); + $view = $element->getView(); + if (null === $view) { + return $content; + } + + $viewScript = $this->getViewScript(); + if (empty($viewScript)) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('No view script registered with ViewScript decorator'); + } + + $separator = $this->getSeparator(); + $placement = $this->getPlacement(); + + $vars = $this->getOptions(); + $vars['element'] = $element; + $vars['content'] = $content; + $vars['decorator'] = $this; + + $renderedContent = $view->partial($viewScript, $vars); + + // Get placement again to see if it has changed + $placement = $this->getPlacement(); + + switch ($placement) { + case self::PREPEND: + return $renderedContent . $separator . $content; + case self::APPEND: + return $content . $separator . $renderedContent; + default: + return $renderedContent; + } + } +} diff --git a/libs/Zend/Form/DisplayGroup.php b/libs/Zend/Form/DisplayGroup.php new file mode 100644 index 0000000000000000000000000000000000000000..88253b40a9606724f1ea9257189a11b512902272 --- /dev/null +++ b/libs/Zend/Form/DisplayGroup.php @@ -0,0 +1,1130 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** + * Zend_Form_DisplayGroup + * + * @category Zend + * @package Zend_Form + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: DisplayGroup.php 22465 2010-06-19 17:41:03Z alab $ + */ +class Zend_Form_DisplayGroup implements Iterator,Countable +{ + /** + * Group attributes + * @var array + */ + protected $_attribs = array(); + + /** + * Display group decorators + * @var array + */ + protected $_decorators = array(); + + /** + * Description + * @var string + */ + protected $_description; + + /** + * Should we disable loading the default decorators? + * @var bool + */ + protected $_disableLoadDefaultDecorators = false; + + /** + * Element order + * @var array + */ + protected $_elementOrder = array(); + + /** + * Elements + * @var array + */ + protected $_elements = array(); + + /** + * Whether or not a new element has been added to the group + * @var bool + */ + protected $_groupUpdated = false; + + /** + * Plugin loader for decorators + * @var Zend_Loader_PluginLoader + */ + protected $_loader; + + /** + * Group name + * @var string + */ + protected $_name; + + /** + * Group order + * @var int + */ + protected $_order; + + /** + * @var Zend_Translate + */ + protected $_translator; + + /** + * Is translation disabled? + * @var bool + */ + protected $_translatorDisabled = false; + + /** + * @var Zend_View_Interface + */ + protected $_view; + + /** + * Constructor + * + * @param string $name + * @param Zend_Loader_PluginLoader $loader + * @param array|Zend_Config $options + * @return void + */ + public function __construct($name, Zend_Loader_PluginLoader $loader, $options = null) + { + $this->setName($name); + + $this->setPluginLoader($loader); + + if (is_array($options)) { + $this->setOptions($options); + } elseif ($options instanceof Zend_Config) { + $this->setConfig($options); + } + + // Extensions... + $this->init(); + + $this->loadDefaultDecorators(); + } + + /** + * Initialize object; used by extending classes + * + * @return void + */ + public function init() + { + } + + /** + * Set options + * + * @param array $options + * @return Zend_Form_DisplayGroup + */ + public function setOptions(array $options) + { + $forbidden = array( + 'Options', 'Config', 'PluginLoader', 'View', + 'Translator', 'Attrib' + ); + foreach ($options as $key => $value) { + $normalized = ucfirst($key); + + if (in_array($normalized, $forbidden)) { + continue; + } + + $method = 'set' . $normalized; + if (method_exists($this, $method)) { + $this->$method($value); + } else { + $this->setAttrib($key, $value); + } + } + return $this; + } + + /** + * Set options from config object + * + * @param Zend_Config $config + * @return Zend_Form_DisplayGroup + */ + public function setConfig(Zend_Config $config) + { + return $this->setOptions($config->toArray()); + } + + /** + * Set group attribute + * + * @param string $key + * @param mixed $value + * @return Zend_Form_DisplayGroup + */ + public function setAttrib($key, $value) + { + $key = (string) $key; + $this->_attribs[$key] = $value; + return $this; + } + + /** + * Add multiple form attributes at once + * + * @param array $attribs + * @return Zend_Form_DisplayGroup + */ + public function addAttribs(array $attribs) + { + foreach ($attribs as $key => $value) { + $this->setAttrib($key, $value); + } + return $this; + } + + /** + * Set multiple form attributes at once + * + * Overwrites any previously set attributes. + * + * @param array $attribs + * @return Zend_Form_DisplayGroup + */ + public function setAttribs(array $attribs) + { + $this->clearAttribs(); + return $this->addAttribs($attribs); + } + + /** + * Retrieve a single form attribute + * + * @param string $key + * @return mixed + */ + public function getAttrib($key) + { + $key = (string) $key; + if (!isset($this->_attribs[$key])) { + return null; + } + + return $this->_attribs[$key]; + } + + /** + * Retrieve all form attributes/metadata + * + * @return array + */ + public function getAttribs() + { + return $this->_attribs; + } + + /** + * Remove attribute + * + * @param string $key + * @return bool + */ + public function removeAttrib($key) + { + if (array_key_exists($key, $this->_attribs)) { + unset($this->_attribs[$key]); + return true; + } + + return false; + } + + /** + * Clear all form attributes + * + * @return Zend_Form + */ + public function clearAttribs() + { + $this->_attribs = array(); + return $this; + } + + /** + * Filter a name to only allow valid variable characters + * + * @param string $value + * @return string + */ + public function filterName($value) + { + return preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '', (string) $value); + } + + /** + * Set group name + * + * @param string $name + * @return Zend_Form_DisplayGroup + */ + public function setName($name) + { + $name = $this->filtername($name); + if (('0' !== $name) && empty($name)) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty'); + } + + $this->_name = $name; + return $this; + } + + /** + * Retrieve group name + * + * @return string + */ + public function getName() + { + return $this->_name; + } + + /** + * Get fully qualified name + * + * Places name as subitem of array and/or appends brackets. + * + * @return string + */ + public function getFullyQualifiedName() + { + return $this->getName(); + } + + /** + * Get element id + * + * @return string + */ + public function getId() + { + if (isset($this->id)) { + return $this->id; + } + + $id = $this->getFullyQualifiedName(); + + // Bail early if no array notation detected + if (!strstr($id, '[')) { + return $id; + } + + // Strip array notation + if ('[]' == substr($id, -2)) { + $id = substr($id, 0, strlen($id) - 2); + } + $id = str_replace('][', '-', $id); + $id = str_replace(array(']', '['), '-', $id); + $id = trim($id, '-'); + + return $id; + } + + /** + * Set group legend + * + * @param string $legend + * @return Zend_Form_DisplayGroup + */ + public function setLegend($legend) + { + return $this->setAttrib('legend', (string) $legend); + } + + /** + * Retrieve group legend + * + * @return string + */ + public function getLegend() + { + return $this->getAttrib('legend'); + } + + /** + * Set description + * + * @param string $value + * @return Zend_Form_DisplayGroup + */ + public function setDescription($value) + { + $this->_description = (string) $value; + return $this; + } + + /** + * Get description + * + * @return string + */ + public function getDescription() + { + return $this->_description; + } + + /** + * Set group order + * + * @param int $order + * @return Zend_Form_Element + */ + public function setOrder($order) + { + $this->_order = (int) $order; + return $this; + } + + /** + * Retrieve group order + * + * @return int + */ + public function getOrder() + { + return $this->_order; + } + + // Elements + + /** + * Add element to stack + * + * @param Zend_Form_Element $element + * @return Zend_Form_DisplayGroup + */ + public function addElement(Zend_Form_Element $element) + { + $this->_elements[$element->getName()] = $element; + $this->_groupUpdated = true; + return $this; + } + + /** + * Add multiple elements at once + * + * @param array $elements + * @return Zend_Form_DisplayGroup + * @throws Zend_Form_Exception if any element is not a Zend_Form_Element + */ + public function addElements(array $elements) + { + foreach ($elements as $element) { + if (!$element instanceof Zend_Form_Element) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('elements passed via array to addElements() must be Zend_Form_Elements only'); + } + $this->addElement($element); + } + return $this; + } + + /** + * Set multiple elements at once (overwrites) + * + * @param array $elements + * @return Zend_Form_DisplayGroup + */ + public function setElements(array $elements) + { + $this->clearElements(); + return $this->addElements($elements); + } + + /** + * Retrieve element + * + * @param string $name + * @return Zend_Form_Element|null + */ + public function getElement($name) + { + $name = (string) $name; + if (isset($this->_elements[$name])) { + return $this->_elements[$name]; + } + + return null; + } + + /** + * Retrieve elements + * @return array + */ + public function getElements() + { + return $this->_elements; + } + + /** + * Remove a single element + * + * @param string $name + * @return boolean + */ + public function removeElement($name) + { + $name = (string) $name; + if (array_key_exists($name, $this->_elements)) { + unset($this->_elements[$name]); + $this->_groupUpdated = true; + return true; + } + + return false; + } + + /** + * Remove all elements + * + * @return Zend_Form_DisplayGroup + */ + public function clearElements() + { + $this->_elements = array(); + $this->_groupUpdated = true; + return $this; + } + + // Plugin loader (for decorators) + + /** + * Set plugin loader + * + * @param Zend_Loader_PluginLoader $loader + * @return Zend_Form_DisplayGroup + */ + public function setPluginLoader(Zend_Loader_PluginLoader $loader) + { + $this->_loader = $loader; + return $this; + } + + /** + * Retrieve plugin loader + * + * @return Zend_Loader_PluginLoader + */ + public function getPluginLoader() + { + return $this->_loader; + } + + /** + * Add a prefix path for the plugin loader + * + * @param string $prefix + * @param string $path + * @return Zend_Form_DisplayGroup + */ + public function addPrefixPath($prefix, $path) + { + $this->getPluginLoader()->addPrefixPath($prefix, $path); + return $this; + } + + /** + * Add several prefix paths at once + * + * @param array $spec + * @return Zend_Form_DisplayGroup + */ + public function addPrefixPaths(array $spec) + { + if (isset($spec['prefix']) && isset($spec['path'])) { + return $this->addPrefixPath($spec['prefix'], $spec['path']); + } + foreach ($spec as $prefix => $paths) { + if (is_numeric($prefix) && is_array($paths)) { + $prefix = null; + if (isset($paths['prefix']) && isset($paths['path'])) { + $this->addPrefixPath($paths['prefix'], $paths['path']); + } + } elseif (!is_numeric($prefix)) { + if (is_string($paths)) { + $this->addPrefixPath($prefix, $paths); + } elseif (is_array($paths)) { + foreach ($paths as $path) { + $this->addPrefixPath($prefix, $path); + } + } + } + } + return $this; + } + + // Decorators + + /** + * Set flag to disable loading default decorators + * + * @param bool $flag + * @return Zend_Form_Element + */ + public function setDisableLoadDefaultDecorators($flag) + { + $this->_disableLoadDefaultDecorators = (bool) $flag; + return $this; + } + + /** + * Should we load the default decorators? + * + * @return bool + */ + public function loadDefaultDecoratorsIsDisabled() + { + return $this->_disableLoadDefaultDecorators; + } + + /** + * Load default decorators + * + * @return void + */ + public function loadDefaultDecorators() + { + if ($this->loadDefaultDecoratorsIsDisabled()) { + return $this; + } + + $decorators = $this->getDecorators(); + if (empty($decorators)) { + $this->addDecorator('FormElements') + ->addDecorator('HtmlTag', array('tag' => 'dl')) + ->addDecorator('Fieldset') + ->addDecorator('DtDdWrapper'); + } + return $this; + } + + /** + * Instantiate a decorator based on class name or class name fragment + * + * @param string $name + * @param null|array $options + * @return Zend_Form_Decorator_Interface + */ + protected function _getDecorator($name, $options = null) + { + $class = $this->getPluginLoader()->load($name); + if (null === $options) { + $decorator = new $class; + } else { + $decorator = new $class($options); + } + + return $decorator; + } + + /** + * Add a decorator for rendering the group + * + * @param string|Zend_Form_Decorator_Interface $decorator + * @param array|Zend_Config $options Options with which to initialize decorator + * @return Zend_Form_DisplayGroup + */ + public function addDecorator($decorator, $options = null) + { + if ($decorator instanceof Zend_Form_Decorator_Interface) { + $name = get_class($decorator); + } elseif (is_string($decorator)) { + $name = $decorator; + $decorator = array( + 'decorator' => $name, + 'options' => $options, + ); + } elseif (is_array($decorator)) { + foreach ($decorator as $name => $spec) { + break; + } + if (is_numeric($name)) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid alias provided to addDecorator; must be alphanumeric string'); + } + if (is_string($spec)) { + $decorator = array( + 'decorator' => $spec, + 'options' => $options, + ); + } elseif ($spec instanceof Zend_Form_Decorator_Interface) { + $decorator = $spec; + } + } else { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid decorator provided to addDecorator; must be string or Zend_Form_Decorator_Interface'); + } + + $this->_decorators[$name] = $decorator; + + return $this; + } + + /** + * Add many decorators at once + * + * @param array $decorators + * @return Zend_Form_DisplayGroup + */ + public function addDecorators(array $decorators) + { + foreach ($decorators as $decoratorName => $decoratorInfo) { + if (is_string($decoratorInfo) || + $decoratorInfo instanceof Zend_Form_Decorator_Interface) { + if (!is_numeric($decoratorName)) { + $this->addDecorator(array($decoratorName => $decoratorInfo)); + } else { + $this->addDecorator($decoratorInfo); + } + } elseif (is_array($decoratorInfo)) { + $argc = count($decoratorInfo); + $options = array(); + if (isset($decoratorInfo['decorator'])) { + $decorator = $decoratorInfo['decorator']; + if (isset($decoratorInfo['options'])) { + $options = $decoratorInfo['options']; + } + $this->addDecorator($decorator, $options); + } else { + switch (true) { + case (0 == $argc): + break; + case (1 <= $argc): + $decorator = array_shift($decoratorInfo); + case (2 <= $argc): + $options = array_shift($decoratorInfo); + default: + $this->addDecorator($decorator, $options); + break; + } + } + } else { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid decorator passed to addDecorators()'); + } + } + + return $this; + } + + /** + * Overwrite all decorators + * + * @param array $decorators + * @return Zend_Form_DisplayGroup + */ + public function setDecorators(array $decorators) + { + $this->clearDecorators(); + return $this->addDecorators($decorators); + } + + /** + * Retrieve a registered decorator + * + * @param string $name + * @return false|Zend_Form_Decorator_Abstract + */ + public function getDecorator($name) + { + if (!isset($this->_decorators[$name])) { + $len = strlen($name); + foreach ($this->_decorators as $localName => $decorator) { + if ($len > strlen($localName)) { + continue; + } + + if (0 === substr_compare($localName, $name, -$len, $len, true)) { + if (is_array($decorator)) { + return $this->_loadDecorator($decorator, $localName); + } + return $decorator; + } + } + return false; + } + + if (is_array($this->_decorators[$name])) { + return $this->_loadDecorator($this->_decorators[$name], $name); + } + + return $this->_decorators[$name]; + } + + /** + * Retrieve all decorators + * + * @return array + */ + public function getDecorators() + { + foreach ($this->_decorators as $key => $value) { + if (is_array($value)) { + $this->_loadDecorator($value, $key); + } + } + return $this->_decorators; + } + + /** + * Remove a single decorator + * + * @param string $name + * @return bool + */ + public function removeDecorator($name) + { + $decorator = $this->getDecorator($name); + if ($decorator) { + if (array_key_exists($name, $this->_decorators)) { + unset($this->_decorators[$name]); + } else { + $class = get_class($decorator); + unset($this->_decorators[$class]); + } + return true; + } + + return false; + } + + /** + * Clear all decorators + * + * @return Zend_Form_DisplayGroup + */ + public function clearDecorators() + { + $this->_decorators = array(); + return $this; + } + + /** + * Set view + * + * @param Zend_View_Interface $view + * @return Zend_Form_DisplayGroup + */ + public function setView(Zend_View_Interface $view = null) + { + $this->_view = $view; + return $this; + } + + /** + * Retrieve view + * + * @return Zend_View_Interface + */ + public function getView() + { + if (null === $this->_view) { + // require_once 'Zend/Controller/Action/HelperBroker.php'; + $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); + $this->setView($viewRenderer->view); + } + + return $this->_view; + } + + /** + * Render display group + * + * @return string + */ + public function render(Zend_View_Interface $view = null) + { + if (null !== $view) { + $this->setView($view); + } + $content = ''; + foreach ($this->getDecorators() as $decorator) { + $decorator->setElement($this); + $content = $decorator->render($content); + } + return $content; + } + + /** + * String representation of group + * + * @return string + */ + public function __toString() + { + try { + $return = $this->render(); + return $return; + } catch (Exception $e) { + trigger_error($e->getMessage(), E_USER_WARNING); + return ''; + } + } + + /** + * Set translator object + * + * @param Zend_Translate|Zend_Translate_Adapter|null $translator + * @return Zend_Form_DisplayGroup + */ + public function setTranslator($translator = null) + { + if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) { + $this->_translator = $translator; + } elseif ($translator instanceof Zend_Translate) { + $this->_translator = $translator->getAdapter(); + } else { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid translator specified'); + } + return $this; + } + + /** + * Retrieve translator object + * + * @return Zend_Translate_Adapter|null + */ + public function getTranslator() + { + if ($this->translatorIsDisabled()) { + return null; + } + + if (null === $this->_translator) { + // require_once 'Zend/Form.php'; + return Zend_Form::getDefaultTranslator(); + } + + return $this->_translator; + } + + /** + * Indicate whether or not translation should be disabled + * + * @param bool $flag + * @return Zend_Form_DisplayGroup + */ + public function setDisableTranslator($flag) + { + $this->_translatorDisabled = (bool) $flag; + return $this; + } + + /** + * Is translation disabled? + * + * @return bool + */ + public function translatorIsDisabled() + { + return $this->_translatorDisabled; + } + + /** + * Overloading: allow rendering specific decorators + * + * Call renderDecoratorName() to render a specific decorator. + * + * @param string $method + * @param array $args + * @return string + * @throws Zend_Form_Exception for invalid decorator or invalid method call + */ + public function __call($method, $args) + { + if ('render' == substr($method, 0, 6)) { + $decoratorName = substr($method, 6); + if (false !== ($decorator = $this->getDecorator($decoratorName))) { + $decorator->setElement($this); + $seed = ''; + if (0 < count($args)) { + $seed = array_shift($args); + } + return $decorator->render($seed); + } + + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Decorator by name %s does not exist', $decoratorName)); + } + + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Method %s does not exist', $method)); + } + + // Interfaces: Iterator, Countable + + /** + * Current element + * + * @return Zend_Form_Element + */ + public function current() + { + $this->_sort(); + current($this->_elementOrder); + $key = key($this->_elementOrder); + return $this->getElement($key); + } + + /** + * Current element + * + * @return string + */ + public function key() + { + $this->_sort(); + return key($this->_elementOrder); + } + + /** + * Move pointer to next element + * + * @return void + */ + public function next() + { + $this->_sort(); + next($this->_elementOrder); + } + + /** + * Move pointer to beginning of element loop + * + * @return void + */ + public function rewind() + { + $this->_sort(); + reset($this->_elementOrder); + } + + /** + * Determine if current element/subform/display group is valid + * + * @return bool + */ + public function valid() + { + $this->_sort(); + return (current($this->_elementOrder) !== false); + } + + /** + * Count of elements/subforms that are iterable + * + * @return int + */ + public function count() + { + return count($this->_elements); + } + + /** + * Sort items according to their order + * + * @return void + */ + protected function _sort() + { + if ($this->_groupUpdated || !is_array($this->_elementOrder)) { + $elementOrder = array(); + foreach ($this->getElements() as $key => $element) { + $elementOrder[$key] = $element->getOrder(); + } + + $items = array(); + $index = 0; + foreach ($elementOrder as $key => $order) { + if (null === $order) { + while (array_search($index, $elementOrder, true)) { + ++$index; + } + $items[$index] = $key; + ++$index; + } else { + $items[$order] = $key; + } + } + + $items = array_flip($items); + asort($items); + $this->_elementOrder = $items; + $this->_groupUpdated = false; + } + } + + /** + * Lazy-load a decorator + * + * @param array $decorator Decorator type and options + * @param mixed $name Decorator name or alias + * @return Zend_Form_Decorator_Interface + */ + protected function _loadDecorator(array $decorator, $name) + { + $sameName = false; + if ($name == $decorator['decorator']) { + $sameName = true; + } + + $instance = $this->_getDecorator($decorator['decorator'], $decorator['options']); + if ($sameName) { + $newName = get_class($instance); + $decoratorNames = array_keys($this->_decorators); + $order = array_flip($decoratorNames); + $order[$newName] = $order[$name]; + $decoratorsExchange = array(); + unset($order[$name]); + asort($order); + foreach ($order as $key => $index) { + if ($key == $newName) { + $decoratorsExchange[$key] = $instance; + continue; + } + $decoratorsExchange[$key] = $this->_decorators[$key]; + } + $this->_decorators = $decoratorsExchange; + } else { + $this->_decorators[$name] = $instance; + } + + return $instance; + } +} diff --git a/libs/Zend/Form/Element.php b/libs/Zend/Form/Element.php new file mode 100644 index 0000000000000000000000000000000000000000..7333c0b5d4f5bc145968df8def7d90cd775cebf5 --- /dev/null +++ b/libs/Zend/Form/Element.php @@ -0,0 +1,2247 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** @see Zend_Filter */ +// require_once 'Zend/Filter.php'; + +/** @see Zend_Form */ +// require_once 'Zend/Form.php'; + +/** @see Zend_Validate_Interface */ +// require_once 'Zend/Validate/Interface.php'; + +/** @see Zend_Validate_Abstract */ +// require_once 'Zend/Validate/Abstract.php'; + +/** + * Zend_Form_Element + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Element.php 22465 2010-06-19 17:41:03Z alab $ + */ +class Zend_Form_Element implements Zend_Validate_Interface +{ + /** + * Element Constants + */ + const DECORATOR = 'DECORATOR'; + const FILTER = 'FILTER'; + const VALIDATE = 'VALIDATE'; + + /** + * Default view helper to use + * @var string + */ + public $helper = 'formText'; + + /** + * 'Allow empty' flag + * @var bool + */ + protected $_allowEmpty = true; + + /** + * Flag indicating whether or not to insert NotEmpty validator when element is required + * @var bool + */ + protected $_autoInsertNotEmptyValidator = true; + + /** + * Array to which element belongs + * @var string + */ + protected $_belongsTo; + + /** + * Element decorators + * @var array + */ + protected $_decorators = array(); + + /** + * Element description + * @var string + */ + protected $_description; + + /** + * Should we disable loading the default decorators? + * @var bool + */ + protected $_disableLoadDefaultDecorators = false; + + /** + * Custom error messages + * @var array + */ + protected $_errorMessages = array(); + + /** + * Validation errors + * @var array + */ + protected $_errors = array(); + + /** + * Separator to use when concatenating aggregate error messages (for + * elements having array values) + * @var string + */ + protected $_errorMessageSeparator = '; '; + + /** + * Element filters + * @var array + */ + protected $_filters = array(); + + /** + * Ignore flag (used when retrieving values at form level) + * @var bool + */ + protected $_ignore = false; + + /** + * Does the element represent an array? + * @var bool + */ + protected $_isArray = false; + + /** + * Is the error marked as in an invalid state? + * @var bool + */ + protected $_isError = false; + + /** + * Has the element been manually marked as invalid? + * @var bool + */ + protected $_isErrorForced = false; + + /** + * Element label + * @var string + */ + protected $_label; + + /** + * Plugin loaders for filter and validator chains + * @var array + */ + protected $_loaders = array(); + + /** + * Formatted validation error messages + * @var array + */ + protected $_messages = array(); + + /** + * Element name + * @var string + */ + protected $_name; + + /** + * Order of element + * @var int + */ + protected $_order; + + /** + * Required flag + * @var bool + */ + protected $_required = false; + + /** + * @var Zend_Translate + */ + protected $_translator; + + /** + * Is translation disabled? + * @var bool + */ + protected $_translatorDisabled = false; + + /** + * Element type + * @var string + */ + protected $_type; + + /** + * Array of initialized validators + * @var array Validators + */ + protected $_validators = array(); + + /** + * Array of un-initialized validators + * @var array + */ + protected $_validatorRules = array(); + + /** + * Element value + * @var mixed + */ + protected $_value; + + /** + * @var Zend_View_Interface + */ + protected $_view; + + /** + * Is a specific decorator being rendered via the magic renderDecorator()? + * + * This is to allow execution of logic inside the render() methods of child + * elements during the magic call while skipping the parent render() method. + * + * @var bool + */ + protected $_isPartialRendering = false; + + /** + * Constructor + * + * $spec may be: + * - string: name of element + * - array: options with which to configure element + * - Zend_Config: Zend_Config with options for configuring element + * + * @param string|array|Zend_Config $spec + * @param array|Zend_Config $options + * @return void + * @throws Zend_Form_Exception if no element name after initialization + */ + public function __construct($spec, $options = null) + { + if (is_string($spec)) { + $this->setName($spec); + } elseif (is_array($spec)) { + $this->setOptions($spec); + } elseif ($spec instanceof Zend_Config) { + $this->setConfig($spec); + } + + if (is_string($spec) && is_array($options)) { + $this->setOptions($options); + } elseif (is_string($spec) && ($options instanceof Zend_Config)) { + $this->setConfig($options); + } + + if (null === $this->getName()) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Zend_Form_Element requires each element to have a name'); + } + + /** + * Extensions + */ + $this->init(); + + /** + * Register ViewHelper decorator by default + */ + $this->loadDefaultDecorators(); + } + + /** + * Initialize object; used by extending classes + * + * @return void + */ + public function init() + { + } + + /** + * Set flag to disable loading default decorators + * + * @param bool $flag + * @return Zend_Form_Element + */ + public function setDisableLoadDefaultDecorators($flag) + { + $this->_disableLoadDefaultDecorators = (bool) $flag; + return $this; + } + + /** + * Should we load the default decorators? + * + * @return bool + */ + public function loadDefaultDecoratorsIsDisabled() + { + return $this->_disableLoadDefaultDecorators; + } + + /** + * Load default decorators + * + * @return Zend_Form_Element + */ + public function loadDefaultDecorators() + { + if ($this->loadDefaultDecoratorsIsDisabled()) { + return $this; + } + + $decorators = $this->getDecorators(); + if (empty($decorators)) { + $this->addDecorator('ViewHelper') + ->addDecorator('Errors') + ->addDecorator('Description', array('tag' => 'p', 'class' => 'description')) + ->addDecorator('HtmlTag', array('tag' => 'dd', + 'id' => $this->getName() . '-element')) + ->addDecorator('Label', array('tag' => 'dt')); + } + return $this; + } + + /** + * Set object state from options array + * + * @param array $options + * @return Zend_Form_Element + */ + public function setOptions(array $options) + { + if (isset($options['prefixPath'])) { + $this->addPrefixPaths($options['prefixPath']); + unset($options['prefixPath']); + } + + if (isset($options['disableTranslator'])) { + $this->setDisableTranslator($options['disableTranslator']); + unset($options['disableTranslator']); + } + + unset($options['options']); + unset($options['config']); + + foreach ($options as $key => $value) { + $method = 'set' . ucfirst($key); + + if (in_array($method, array('setTranslator', 'setPluginLoader', 'setView'))) { + if (!is_object($value)) { + continue; + } + } + + if (method_exists($this, $method)) { + // Setter exists; use it + $this->$method($value); + } else { + // Assume it's metadata + $this->setAttrib($key, $value); + } + } + return $this; + } + + /** + * Set object state from Zend_Config object + * + * @param Zend_Config $config + * @return Zend_Form_Element + */ + public function setConfig(Zend_Config $config) + { + return $this->setOptions($config->toArray()); + } + + + // Localization: + + /** + * Set translator object for localization + * + * @param Zend_Translate|null $translator + * @return Zend_Form_Element + */ + public function setTranslator($translator = null) + { + if (null === $translator) { + $this->_translator = null; + } elseif ($translator instanceof Zend_Translate_Adapter) { + $this->_translator = $translator; + } elseif ($translator instanceof Zend_Translate) { + $this->_translator = $translator->getAdapter(); + } else { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid translator specified'); + } + return $this; + } + + /** + * Retrieve localization translator object + * + * @return Zend_Translate_Adapter|null + */ + public function getTranslator() + { + if ($this->translatorIsDisabled()) { + return null; + } + + if (null === $this->_translator) { + return Zend_Form::getDefaultTranslator(); + } + return $this->_translator; + } + + /** + * Does this element have its own specific translator? + * + * @return bool + */ + public function hasTranslator() + { + return (bool)$this->_translator; + } + + /** + * Indicate whether or not translation should be disabled + * + * @param bool $flag + * @return Zend_Form_Element + */ + public function setDisableTranslator($flag) + { + $this->_translatorDisabled = (bool) $flag; + return $this; + } + + /** + * Is translation disabled? + * + * @return bool + */ + public function translatorIsDisabled() + { + return $this->_translatorDisabled; + } + + // Metadata + + /** + * Filter a name to only allow valid variable characters + * + * @param string $value + * @param bool $allowBrackets + * @return string + */ + public function filterName($value, $allowBrackets = false) + { + $charset = '^a-zA-Z0-9_\x7f-\xff'; + if ($allowBrackets) { + $charset .= '\[\]'; + } + return preg_replace('/[' . $charset . ']/', '', (string) $value); + } + + /** + * Set element name + * + * @param string $name + * @return Zend_Form_Element + */ + public function setName($name) + { + $name = $this->filterName($name); + if ('' === $name) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty'); + } + + $this->_name = $name; + return $this; + } + + /** + * Return element name + * + * @return string + */ + public function getName() + { + return $this->_name; + } + + /** + * Get fully qualified name + * + * Places name as subitem of array and/or appends brackets. + * + * @return string + */ + public function getFullyQualifiedName() + { + $name = $this->getName(); + + if (null !== ($belongsTo = $this->getBelongsTo())) { + $name = $belongsTo . '[' . $name . ']'; + } + + if ($this->isArray()) { + $name .= '[]'; + } + + return $name; + } + + /** + * Get element id + * + * @return string + */ + public function getId() + { + if (isset($this->id)) { + return $this->id; + } + + $id = $this->getFullyQualifiedName(); + + // Bail early if no array notation detected + if (!strstr($id, '[')) { + return $id; + } + + // Strip array notation + if ('[]' == substr($id, -2)) { + $id = substr($id, 0, strlen($id) - 2); + } + $id = str_replace('][', '-', $id); + $id = str_replace(array(']', '['), '-', $id); + $id = trim($id, '-'); + + return $id; + } + + /** + * Set element value + * + * @param mixed $value + * @return Zend_Form_Element + */ + public function setValue($value) + { + $this->_value = $value; + return $this; + } + + /** + * Filter a value + * + * @param string $value + * @param string $key + * @return void + */ + protected function _filterValue(&$value, &$key) + { + foreach ($this->getFilters() as $filter) { + $value = $filter->filter($value); + } + } + + /** + * Retrieve filtered element value + * + * @return mixed + */ + public function getValue() + { + $valueFiltered = $this->_value; + + if ($this->isArray() && is_array($valueFiltered)) { + array_walk_recursive($valueFiltered, array($this, '_filterValue')); + } else { + $this->_filterValue($valueFiltered, $valueFiltered); + } + + return $valueFiltered; + } + + /** + * Retrieve unfiltered element value + * + * @return mixed + */ + public function getUnfilteredValue() + { + return $this->_value; + } + + /** + * Set element label + * + * @param string $label + * @return Zend_Form_Element + */ + public function setLabel($label) + { + $this->_label = (string) $label; + return $this; + } + + /** + * Retrieve element label + * + * @return string + */ + public function getLabel() + { + $translator = $this->getTranslator(); + if (null !== $translator) { + return $translator->translate($this->_label); + } + + return $this->_label; + } + + /** + * Set element order + * + * @param int $order + * @return Zend_Form_Element + */ + public function setOrder($order) + { + $this->_order = (int) $order; + return $this; + } + + /** + * Retrieve element order + * + * @return int + */ + public function getOrder() + { + return $this->_order; + } + + /** + * Set required flag + * + * @param bool $flag Default value is true + * @return Zend_Form_Element + */ + public function setRequired($flag = true) + { + $this->_required = (bool) $flag; + return $this; + } + + /** + * Is the element required? + * + * @return bool + */ + public function isRequired() + { + return $this->_required; + } + + /** + * Set flag indicating whether a NotEmpty validator should be inserted when element is required + * + * @param bool $flag + * @return Zend_Form_Element + */ + public function setAutoInsertNotEmptyValidator($flag) + { + $this->_autoInsertNotEmptyValidator = (bool) $flag; + return $this; + } + + /** + * Get flag indicating whether a NotEmpty validator should be inserted when element is required + * + * @return bool + */ + public function autoInsertNotEmptyValidator() + { + return $this->_autoInsertNotEmptyValidator; + } + + /** + * Set element description + * + * @param string $description + * @return Zend_Form_Element + */ + public function setDescription($description) + { + $this->_description = (string) $description; + return $this; + } + + /** + * Retrieve element description + * + * @return string + */ + public function getDescription() + { + return $this->_description; + } + + /** + * Set 'allow empty' flag + * + * When the allow empty flag is enabled and the required flag is false, the + * element will validate with empty values. + * + * @param bool $flag + * @return Zend_Form_Element + */ + public function setAllowEmpty($flag) + { + $this->_allowEmpty = (bool) $flag; + return $this; + } + + /** + * Get 'allow empty' flag + * + * @return bool + */ + public function getAllowEmpty() + { + return $this->_allowEmpty; + } + + /** + * Set ignore flag (used when retrieving values at form level) + * + * @param bool $flag + * @return Zend_Form_Element + */ + public function setIgnore($flag) + { + $this->_ignore = (bool) $flag; + return $this; + } + + /** + * Get ignore flag (used when retrieving values at form level) + * + * @return bool + */ + public function getIgnore() + { + return $this->_ignore; + } + + /** + * Set flag indicating if element represents an array + * + * @param bool $flag + * @return Zend_Form_Element + */ + public function setIsArray($flag) + { + $this->_isArray = (bool) $flag; + return $this; + } + + /** + * Is the element representing an array? + * + * @return bool + */ + public function isArray() + { + return $this->_isArray; + } + + /** + * Set array to which element belongs + * + * @param string $array + * @return Zend_Form_Element + */ + public function setBelongsTo($array) + { + $array = $this->filterName($array, true); + if (!empty($array)) { + $this->_belongsTo = $array; + } + + return $this; + } + + /** + * Return array name to which element belongs + * + * @return string + */ + public function getBelongsTo() + { + return $this->_belongsTo; + } + + /** + * Return element type + * + * @return string + */ + public function getType() + { + if (null === $this->_type) { + $this->_type = get_class($this); + } + + return $this->_type; + } + + /** + * Set element attribute + * + * @param string $name + * @param mixed $value + * @return Zend_Form_Element + * @throws Zend_Form_Exception for invalid $name values + */ + public function setAttrib($name, $value) + { + $name = (string) $name; + if ('_' == $name[0]) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Invalid attribute "%s"; must not contain a leading underscore', $name)); + } + + if (null === $value) { + unset($this->$name); + } else { + $this->$name = $value; + } + + return $this; + } + + /** + * Set multiple attributes at once + * + * @param array $attribs + * @return Zend_Form_Element + */ + public function setAttribs(array $attribs) + { + foreach ($attribs as $key => $value) { + $this->setAttrib($key, $value); + } + + return $this; + } + + /** + * Retrieve element attribute + * + * @param string $name + * @return string + */ + public function getAttrib($name) + { + $name = (string) $name; + if (isset($this->$name)) { + return $this->$name; + } + + return null; + } + + /** + * Return all attributes + * + * @return array + */ + public function getAttribs() + { + $attribs = get_object_vars($this); + foreach ($attribs as $key => $value) { + if ('_' == substr($key, 0, 1)) { + unset($attribs[$key]); + } + } + + return $attribs; + } + + /** + * Overloading: retrieve object property + * + * Prevents access to properties beginning with '_'. + * + * @param string $key + * @return mixed + */ + public function __get($key) + { + if ('_' == $key[0]) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Cannot retrieve value for protected/private property "%s"', $key)); + } + + if (!isset($this->$key)) { + return null; + } + + return $this->$key; + } + + /** + * Overloading: set object property + * + * @param string $key + * @param mixed $value + * @return voide + */ + public function __set($key, $value) + { + $this->setAttrib($key, $value); + } + + /** + * Overloading: allow rendering specific decorators + * + * Call renderDecoratorName() to render a specific decorator. + * + * @param string $method + * @param array $args + * @return string + * @throws Zend_Form_Exception for invalid decorator or invalid method call + */ + public function __call($method, $args) + { + if ('render' == substr($method, 0, 6)) { + $this->_isPartialRendering = true; + $this->render(); + $this->_isPartialRendering = false; + $decoratorName = substr($method, 6); + if (false !== ($decorator = $this->getDecorator($decoratorName))) { + $decorator->setElement($this); + $seed = ''; + if (0 < count($args)) { + $seed = array_shift($args); + } + return $decorator->render($seed); + } + + // require_once 'Zend/Form/Element/Exception.php'; + throw new Zend_Form_Element_Exception(sprintf('Decorator by name %s does not exist', $decoratorName)); + } + + // require_once 'Zend/Form/Element/Exception.php'; + throw new Zend_Form_Element_Exception(sprintf('Method %s does not exist', $method)); + } + + // Loaders + + /** + * Set plugin loader to use for validator or filter chain + * + * @param Zend_Loader_PluginLoader_Interface $loader + * @param string $type 'decorator', 'filter', or 'validate' + * @return Zend_Form_Element + * @throws Zend_Form_Exception on invalid type + */ + public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type) + { + $type = strtoupper($type); + switch ($type) { + case self::DECORATOR: + case self::FILTER: + case self::VALIDATE: + $this->_loaders[$type] = $loader; + return $this; + default: + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type)); + } + } + + /** + * Retrieve plugin loader for validator or filter chain + * + * Instantiates with default rules if none available for that type. Use + * 'decorator', 'filter', or 'validate' for $type. + * + * @param string $type + * @return Zend_Loader_PluginLoader + * @throws Zend_Loader_Exception on invalid type. + */ + public function getPluginLoader($type) + { + $type = strtoupper($type); + switch ($type) { + case self::FILTER: + case self::VALIDATE: + $prefixSegment = ucfirst(strtolower($type)); + $pathSegment = $prefixSegment; + case self::DECORATOR: + if (!isset($prefixSegment)) { + $prefixSegment = 'Form_Decorator'; + $pathSegment = 'Form/Decorator'; + } + if (!isset($this->_loaders[$type])) { + // require_once 'Zend/Loader/PluginLoader.php'; + $this->_loaders[$type] = new Zend_Loader_PluginLoader( + array('Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/') + ); + } + return $this->_loaders[$type]; + default: + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type)); + } + } + + /** + * Add prefix path for plugin loader + * + * If no $type specified, assumes it is a base path for both filters and + * validators, and sets each according to the following rules: + * - decorators: $prefix = $prefix . '_Decorator' + * - filters: $prefix = $prefix . '_Filter' + * - validators: $prefix = $prefix . '_Validate' + * + * Otherwise, the path prefix is set on the appropriate plugin loader. + * + * @param string $prefix + * @param string $path + * @param string $type + * @return Zend_Form_Element + * @throws Zend_Form_Exception for invalid type + */ + public function addPrefixPath($prefix, $path, $type = null) + { + $type = strtoupper($type); + switch ($type) { + case self::DECORATOR: + case self::FILTER: + case self::VALIDATE: + $loader = $this->getPluginLoader($type); + $loader->addPrefixPath($prefix, $path); + return $this; + case null: + $prefix = rtrim($prefix, '_'); + $path = rtrim($path, DIRECTORY_SEPARATOR); + foreach (array(self::DECORATOR, self::FILTER, self::VALIDATE) as $type) { + $cType = ucfirst(strtolower($type)); + $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR; + $pluginPrefix = $prefix . '_' . $cType; + $loader = $this->getPluginLoader($type); + $loader->addPrefixPath($pluginPrefix, $pluginPath); + } + return $this; + default: + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type)); + } + } + + /** + * Add many prefix paths at once + * + * @param array $spec + * @return Zend_Form_Element + */ + public function addPrefixPaths(array $spec) + { + if (isset($spec['prefix']) && isset($spec['path'])) { + return $this->addPrefixPath($spec['prefix'], $spec['path']); + } + foreach ($spec as $type => $paths) { + if (is_numeric($type) && is_array($paths)) { + $type = null; + if (isset($paths['prefix']) && isset($paths['path'])) { + if (isset($paths['type'])) { + $type = $paths['type']; + } + $this->addPrefixPath($paths['prefix'], $paths['path'], $type); + } + } elseif (!is_numeric($type)) { + if (!isset($paths['prefix']) || !isset($paths['path'])) { + foreach ($paths as $prefix => $spec) { + if (is_array($spec)) { + foreach ($spec as $path) { + if (!is_string($path)) { + continue; + } + $this->addPrefixPath($prefix, $path, $type); + } + } elseif (is_string($spec)) { + $this->addPrefixPath($prefix, $spec, $type); + } + } + } else { + $this->addPrefixPath($paths['prefix'], $paths['path'], $type); + } + } + } + return $this; + } + + // Validation + + /** + * Add validator to validation chain + * + * Note: will overwrite existing validators if they are of the same class. + * + * @param string|Zend_Validate_Interface $validator + * @param bool $breakChainOnFailure + * @param array $options + * @return Zend_Form_Element + * @throws Zend_Form_Exception if invalid validator type + */ + public function addValidator($validator, $breakChainOnFailure = false, $options = array()) + { + if ($validator instanceof Zend_Validate_Interface) { + $name = get_class($validator); + + if (!isset($validator->zfBreakChainOnFailure)) { + $validator->zfBreakChainOnFailure = $breakChainOnFailure; + } + } elseif (is_string($validator)) { + $name = $validator; + $validator = array( + 'validator' => $validator, + 'breakChainOnFailure' => $breakChainOnFailure, + 'options' => $options, + ); + } else { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid validator provided to addValidator; must be string or Zend_Validate_Interface'); + } + + + $this->_validators[$name] = $validator; + + return $this; + } + + /** + * Add multiple validators + * + * @param array $validators + * @return Zend_Form_Element + */ + public function addValidators(array $validators) + { + foreach ($validators as $validatorInfo) { + if (is_string($validatorInfo)) { + $this->addValidator($validatorInfo); + } elseif ($validatorInfo instanceof Zend_Validate_Interface) { + $this->addValidator($validatorInfo); + } elseif (is_array($validatorInfo)) { + $argc = count($validatorInfo); + $breakChainOnFailure = false; + $options = array(); + if (isset($validatorInfo['validator'])) { + $validator = $validatorInfo['validator']; + if (isset($validatorInfo['breakChainOnFailure'])) { + $breakChainOnFailure = $validatorInfo['breakChainOnFailure']; + } + if (isset($validatorInfo['options'])) { + $options = $validatorInfo['options']; + } + $this->addValidator($validator, $breakChainOnFailure, $options); + } else { + switch (true) { + case (0 == $argc): + break; + case (1 <= $argc): + $validator = array_shift($validatorInfo); + case (2 <= $argc): + $breakChainOnFailure = array_shift($validatorInfo); + case (3 <= $argc): + $options = array_shift($validatorInfo); + default: + $this->addValidator($validator, $breakChainOnFailure, $options); + break; + } + } + } else { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid validator passed to addValidators()'); + } + } + + return $this; + } + + /** + * Set multiple validators, overwriting previous validators + * + * @param array $validators + * @return Zend_Form_Element + */ + public function setValidators(array $validators) + { + $this->clearValidators(); + return $this->addValidators($validators); + } + + /** + * Retrieve a single validator by name + * + * @param string $name + * @return Zend_Validate_Interface|false False if not found, validator otherwise + */ + public function getValidator($name) + { + if (!isset($this->_validators[$name])) { + $len = strlen($name); + foreach ($this->_validators as $localName => $validator) { + if ($len > strlen($localName)) { + continue; + } + if (0 === substr_compare($localName, $name, -$len, $len, true)) { + if (is_array($validator)) { + return $this->_loadValidator($validator); + } + return $validator; + } + } + return false; + } + + if (is_array($this->_validators[$name])) { + return $this->_loadValidator($this->_validators[$name]); + } + + return $this->_validators[$name]; + } + + /** + * Retrieve all validators + * + * @return array + */ + public function getValidators() + { + $validators = array(); + foreach ($this->_validators as $key => $value) { + if ($value instanceof Zend_Validate_Interface) { + $validators[$key] = $value; + continue; + } + $validator = $this->_loadValidator($value); + $validators[get_class($validator)] = $validator; + } + return $validators; + } + + /** + * Remove a single validator by name + * + * @param string $name + * @return bool + */ + public function removeValidator($name) + { + if (isset($this->_validators[$name])) { + unset($this->_validators[$name]); + } else { + $len = strlen($name); + foreach (array_keys($this->_validators) as $validator) { + if ($len > strlen($validator)) { + continue; + } + if (0 === substr_compare($validator, $name, -$len, $len, true)) { + unset($this->_validators[$validator]); + break; + } + } + } + + return $this; + } + + /** + * Clear all validators + * + * @return Zend_Form_Element + */ + public function clearValidators() + { + $this->_validators = array(); + return $this; + } + + /** + * Validate element value + * + * If a translation adapter is registered, any error messages will be + * translated according to the current locale, using the given error code; + * if no matching translation is found, the original message will be + * utilized. + * + * Note: The *filtered* value is validated. + * + * @param mixed $value + * @param mixed $context + * @return boolean + */ + public function isValid($value, $context = null) + { + $this->setValue($value); + $value = $this->getValue(); + + if ((('' === $value) || (null === $value)) + && !$this->isRequired() + && $this->getAllowEmpty() + ) { + return true; + } + + if ($this->isRequired() + && $this->autoInsertNotEmptyValidator() + && !$this->getValidator('NotEmpty')) + { + $validators = $this->getValidators(); + $notEmpty = array('validator' => 'NotEmpty', 'breakChainOnFailure' => true); + array_unshift($validators, $notEmpty); + $this->setValidators($validators); + } + + // Find the correct translator. Zend_Validate_Abstract::getDefaultTranslator() + // will get either the static translator attached to Zend_Validate_Abstract + // or the 'Zend_Translate' from Zend_Registry. + if (Zend_Validate_Abstract::hasDefaultTranslator() && + !Zend_Form::hasDefaultTranslator()) + { + $translator = Zend_Validate_Abstract::getDefaultTranslator(); + if ($this->hasTranslator()) { + // only pick up this element's translator if it was attached directly. + $translator = $this->getTranslator(); + } + } else { + $translator = $this->getTranslator(); + } + + $this->_messages = array(); + $this->_errors = array(); + $result = true; + $isArray = $this->isArray(); + foreach ($this->getValidators() as $key => $validator) { + if (method_exists($validator, 'setTranslator')) { + if (method_exists($validator, 'hasTranslator')) { + if (!$validator->hasTranslator()) { + $validator->setTranslator($translator); + } + } else { + $validator->setTranslator($translator); + } + } + + if (method_exists($validator, 'setDisableTranslator')) { + $validator->setDisableTranslator($this->translatorIsDisabled()); + } + + if ($isArray && is_array($value)) { + $messages = array(); + $errors = array(); + foreach ($value as $val) { + if (!$validator->isValid($val, $context)) { + $result = false; + if ($this->_hasErrorMessages()) { + $messages = $this->_getErrorMessages(); + $errors = $messages; + } else { + $messages = array_merge($messages, $validator->getMessages()); + $errors = array_merge($errors, $validator->getErrors()); + } + } + } + if ($result) { + continue; + } + } elseif ($validator->isValid($value, $context)) { + continue; + } else { + $result = false; + if ($this->_hasErrorMessages()) { + $messages = $this->_getErrorMessages(); + $errors = $messages; + } else { + $messages = $validator->getMessages(); + $errors = array_keys($messages); + } + } + + $result = false; + $this->_messages = array_merge($this->_messages, $messages); + $this->_errors = array_merge($this->_errors, $errors); + + if ($validator->zfBreakChainOnFailure) { + break; + } + } + + // If element manually flagged as invalid, return false + if ($this->_isErrorForced) { + return false; + } + + return $result; + } + + /** + * Add a custom error message to return in the event of failed validation + * + * @param string $message + * @return Zend_Form_Element + */ + public function addErrorMessage($message) + { + $this->_errorMessages[] = (string) $message; + return $this; + } + + /** + * Add multiple custom error messages to return in the event of failed validation + * + * @param array $messages + * @return Zend_Form_Element + */ + public function addErrorMessages(array $messages) + { + foreach ($messages as $message) { + $this->addErrorMessage($message); + } + return $this; + } + + /** + * Same as addErrorMessages(), but clears custom error message stack first + * + * @param array $messages + * @return Zend_Form_Element + */ + public function setErrorMessages(array $messages) + { + $this->clearErrorMessages(); + return $this->addErrorMessages($messages); + } + + /** + * Retrieve custom error messages + * + * @return array + */ + public function getErrorMessages() + { + return $this->_errorMessages; + } + + /** + * Clear custom error messages stack + * + * @return Zend_Form_Element + */ + public function clearErrorMessages() + { + $this->_errorMessages = array(); + return $this; + } + + /** + * Get errorMessageSeparator + * + * @return string + */ + public function getErrorMessageSeparator() + { + return $this->_errorMessageSeparator; + } + + /** + * Set errorMessageSeparator + * + * @param string $separator + * @return Zend_Form_Element + */ + public function setErrorMessageSeparator($separator) + { + $this->_errorMessageSeparator = $separator; + return $this; + } + + /** + * Mark the element as being in a failed validation state + * + * @return Zend_Form_Element + */ + public function markAsError() + { + $messages = $this->getMessages(); + $customMessages = $this->_getErrorMessages(); + $messages = $messages + $customMessages; + if (empty($messages)) { + $this->_isError = true; + } else { + $this->_messages = $messages; + } + $this->_isErrorForced = true; + return $this; + } + + /** + * Add an error message and mark element as failed validation + * + * @param string $message + * @return Zend_Form_Element + */ + public function addError($message) + { + $this->addErrorMessage($message); + $this->markAsError(); + return $this; + } + + /** + * Add multiple error messages and flag element as failed validation + * + * @param array $messages + * @return Zend_Form_Element + */ + public function addErrors(array $messages) + { + foreach ($messages as $message) { + $this->addError($message); + } + return $this; + } + + /** + * Overwrite any previously set error messages and flag as failed validation + * + * @param array $messages + * @return Zend_Form_Element + */ + public function setErrors(array $messages) + { + $this->clearErrorMessages(); + return $this->addErrors($messages); + } + + /** + * Are there errors registered? + * + * @return bool + */ + public function hasErrors() + { + return (!empty($this->_messages) || $this->_isError); + } + + /** + * Retrieve validator chain errors + * + * @return array + */ + public function getErrors() + { + return $this->_errors; + } + + /** + * Retrieve error messages + * + * @return array + */ + public function getMessages() + { + return $this->_messages; + } + + + // Filtering + + /** + * Add a filter to the element + * + * @param string|Zend_Filter_Interface $filter + * @return Zend_Form_Element + */ + public function addFilter($filter, $options = array()) + { + if ($filter instanceof Zend_Filter_Interface) { + $name = get_class($filter); + } elseif (is_string($filter)) { + $name = $filter; + $filter = array( + 'filter' => $filter, + 'options' => $options, + ); + $this->_filters[$name] = $filter; + } else { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid filter provided to addFilter; must be string or Zend_Filter_Interface'); + } + + $this->_filters[$name] = $filter; + + return $this; + } + + /** + * Add filters to element + * + * @param array $filters + * @return Zend_Form_Element + */ + public function addFilters(array $filters) + { + foreach ($filters as $filterInfo) { + if (is_string($filterInfo)) { + $this->addFilter($filterInfo); + } elseif ($filterInfo instanceof Zend_Filter_Interface) { + $this->addFilter($filterInfo); + } elseif (is_array($filterInfo)) { + $argc = count($filterInfo); + $options = array(); + if (isset($filterInfo['filter'])) { + $filter = $filterInfo['filter']; + if (isset($filterInfo['options'])) { + $options = $filterInfo['options']; + } + $this->addFilter($filter, $options); + } else { + switch (true) { + case (0 == $argc): + break; + case (1 <= $argc): + $filter = array_shift($filterInfo); + case (2 <= $argc): + $options = array_shift($filterInfo); + default: + $this->addFilter($filter, $options); + break; + } + } + } else { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid filter passed to addFilters()'); + } + } + + return $this; + } + + /** + * Add filters to element, overwriting any already existing + * + * @param array $filters + * @return Zend_Form_Element + */ + public function setFilters(array $filters) + { + $this->clearFilters(); + return $this->addFilters($filters); + } + + /** + * Retrieve a single filter by name + * + * @param string $name + * @return Zend_Filter_Interface + */ + public function getFilter($name) + { + if (!isset($this->_filters[$name])) { + $len = strlen($name); + foreach ($this->_filters as $localName => $filter) { + if ($len > strlen($localName)) { + continue; + } + + if (0 === substr_compare($localName, $name, -$len, $len, true)) { + if (is_array($filter)) { + return $this->_loadFilter($filter); + } + return $filter; + } + } + return false; + } + + if (is_array($this->_filters[$name])) { + return $this->_loadFilter($this->_filters[$name]); + } + + return $this->_filters[$name]; + } + + /** + * Get all filters + * + * @return array + */ + public function getFilters() + { + $filters = array(); + foreach ($this->_filters as $key => $value) { + if ($value instanceof Zend_Filter_Interface) { + $filters[$key] = $value; + continue; + } + $filter = $this->_loadFilter($value); + $filters[get_class($filter)] = $filter; + } + return $filters; + } + + /** + * Remove a filter by name + * + * @param string $name + * @return Zend_Form_Element + */ + public function removeFilter($name) + { + if (isset($this->_filters[$name])) { + unset($this->_filters[$name]); + } else { + $len = strlen($name); + foreach (array_keys($this->_filters) as $filter) { + if ($len > strlen($filter)) { + continue; + } + if (0 === substr_compare($filter, $name, -$len, $len, true)) { + unset($this->_filters[$filter]); + break; + } + } + } + + return $this; + } + + /** + * Clear all filters + * + * @return Zend_Form_Element + */ + public function clearFilters() + { + $this->_filters = array(); + return $this; + } + + // Rendering + + /** + * Set view object + * + * @param Zend_View_Interface $view + * @return Zend_Form_Element + */ + public function setView(Zend_View_Interface $view = null) + { + $this->_view = $view; + return $this; + } + + /** + * Retrieve view object + * + * Retrieves from ViewRenderer if none previously set. + * + * @return null|Zend_View_Interface + */ + public function getView() + { + if (null === $this->_view) { + // require_once 'Zend/Controller/Action/HelperBroker.php'; + $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); + $this->setView($viewRenderer->view); + } + return $this->_view; + } + + /** + * Instantiate a decorator based on class name or class name fragment + * + * @param string $name + * @param null|array $options + * @return Zend_Form_Decorator_Interface + */ + protected function _getDecorator($name, $options) + { + $class = $this->getPluginLoader(self::DECORATOR)->load($name); + if (null === $options) { + $decorator = new $class; + } else { + $decorator = new $class($options); + } + + return $decorator; + } + + /** + * Add a decorator for rendering the element + * + * @param string|Zend_Form_Decorator_Interface $decorator + * @param array|Zend_Config $options Options with which to initialize decorator + * @return Zend_Form_Element + */ + public function addDecorator($decorator, $options = null) + { + if ($decorator instanceof Zend_Form_Decorator_Interface) { + $name = get_class($decorator); + } elseif (is_string($decorator)) { + $name = $decorator; + $decorator = array( + 'decorator' => $name, + 'options' => $options, + ); + } elseif (is_array($decorator)) { + foreach ($decorator as $name => $spec) { + break; + } + if (is_numeric($name)) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid alias provided to addDecorator; must be alphanumeric string'); + } + if (is_string($spec)) { + $decorator = array( + 'decorator' => $spec, + 'options' => $options, + ); + } elseif ($spec instanceof Zend_Form_Decorator_Interface) { + $decorator = $spec; + } + } else { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid decorator provided to addDecorator; must be string or Zend_Form_Decorator_Interface'); + } + + $this->_decorators[$name] = $decorator; + + return $this; + } + + /** + * Add many decorators at once + * + * @param array $decorators + * @return Zend_Form_Element + */ + public function addDecorators(array $decorators) + { + foreach ($decorators as $decoratorName => $decoratorInfo) { + if (is_string($decoratorInfo) || + $decoratorInfo instanceof Zend_Form_Decorator_Interface) { + if (!is_numeric($decoratorName)) { + $this->addDecorator(array($decoratorName => $decoratorInfo)); + } else { + $this->addDecorator($decoratorInfo); + } + } elseif (is_array($decoratorInfo)) { + $argc = count($decoratorInfo); + $options = array(); + if (isset($decoratorInfo['decorator'])) { + $decorator = $decoratorInfo['decorator']; + if (isset($decoratorInfo['options'])) { + $options = $decoratorInfo['options']; + } + $this->addDecorator($decorator, $options); + } else { + switch (true) { + case (0 == $argc): + break; + case (1 <= $argc): + $decorator = array_shift($decoratorInfo); + case (2 <= $argc): + $options = array_shift($decoratorInfo); + default: + $this->addDecorator($decorator, $options); + break; + } + } + } else { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception('Invalid decorator passed to addDecorators()'); + } + } + + return $this; + } + + /** + * Overwrite all decorators + * + * @param array $decorators + * @return Zend_Form_Element + */ + public function setDecorators(array $decorators) + { + $this->clearDecorators(); + return $this->addDecorators($decorators); + } + + /** + * Retrieve a registered decorator + * + * @param string $name + * @return false|Zend_Form_Decorator_Abstract + */ + public function getDecorator($name) + { + if (!isset($this->_decorators[$name])) { + $len = strlen($name); + foreach ($this->_decorators as $localName => $decorator) { + if ($len > strlen($localName)) { + continue; + } + + if (0 === substr_compare($localName, $name, -$len, $len, true)) { + if (is_array($decorator)) { + return $this->_loadDecorator($decorator, $localName); + } + return $decorator; + } + } + return false; + } + + if (is_array($this->_decorators[$name])) { + return $this->_loadDecorator($this->_decorators[$name], $name); + } + + return $this->_decorators[$name]; + } + + /** + * Retrieve all decorators + * + * @return array + */ + public function getDecorators() + { + foreach ($this->_decorators as $key => $value) { + if (is_array($value)) { + $this->_loadDecorator($value, $key); + } + } + return $this->_decorators; + } + + /** + * Remove a single decorator + * + * @param string $name + * @return Zend_Form_Element + */ + public function removeDecorator($name) + { + if (isset($this->_decorators[$name])) { + unset($this->_decorators[$name]); + } else { + $len = strlen($name); + foreach (array_keys($this->_decorators) as $decorator) { + if ($len > strlen($decorator)) { + continue; + } + if (0 === substr_compare($decorator, $name, -$len, $len, true)) { + unset($this->_decorators[$decorator]); + break; + } + } + } + + return $this; + } + + /** + * Clear all decorators + * + * @return Zend_Form_Element + */ + public function clearDecorators() + { + $this->_decorators = array(); + return $this; + } + + /** + * Render form element + * + * @param Zend_View_Interface $view + * @return string + */ + public function render(Zend_View_Interface $view = null) + { + if ($this->_isPartialRendering) { + return ''; + } + + if (null !== $view) { + $this->setView($view); + } + + $content = ''; + foreach ($this->getDecorators() as $decorator) { + $decorator->setElement($this); + $content = $decorator->render($content); + } + return $content; + } + + /** + * String representation of form element + * + * Proxies to {@link render()}. + * + * @return string + */ + public function __toString() + { + try { + $return = $this->render(); + return $return; + } catch (Exception $e) { + trigger_error($e->getMessage(), E_USER_WARNING); + return ''; + } + } + + /** + * Lazy-load a filter + * + * @param array $filter + * @return Zend_Filter_Interface + */ + protected function _loadFilter(array $filter) + { + $origName = $filter['filter']; + $name = $this->getPluginLoader(self::FILTER)->load($filter['filter']); + + if (array_key_exists($name, $this->_filters)) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Filter instance already exists for filter "%s"', $origName)); + } + + if (empty($filter['options'])) { + $instance = new $name; + } else { + $r = new ReflectionClass($name); + if ($r->hasMethod('__construct')) { + $instance = $r->newInstanceArgs((array) $filter['options']); + } else { + $instance = $r->newInstance(); + } + } + + if ($origName != $name) { + $filterNames = array_keys($this->_filters); + $order = array_flip($filterNames); + $order[$name] = $order[$origName]; + $filtersExchange = array(); + unset($order[$origName]); + asort($order); + foreach ($order as $key => $index) { + if ($key == $name) { + $filtersExchange[$key] = $instance; + continue; + } + $filtersExchange[$key] = $this->_filters[$key]; + } + $this->_filters = $filtersExchange; + } else { + $this->_filters[$name] = $instance; + } + + return $instance; + } + + /** + * Lazy-load a validator + * + * @param array $validator Validator definition + * @return Zend_Validate_Interface + */ + protected function _loadValidator(array $validator) + { + $origName = $validator['validator']; + $name = $this->getPluginLoader(self::VALIDATE)->load($validator['validator']); + + if (array_key_exists($name, $this->_validators)) { + // require_once 'Zend/Form/Exception.php'; + throw new Zend_Form_Exception(sprintf('Validator instance already exists for validator "%s"', $origName)); + } + + $messages = false; + if (isset($validator['options']) && array_key_exists('messages', (array)$validator['options'])) { + $messages = $validator['options']['messages']; + unset($validator['options']['messages']); + } + + if (empty($validator['options'])) { + $instance = new $name; + } else { + $r = new ReflectionClass($name); + if ($r->hasMethod('__construct')) { + $numeric = false; + if (is_array($validator['options'])) { + $keys = array_keys($validator['options']); + foreach($keys as $key) { + if (is_numeric($key)) { + $numeric = true; + break; + } + } + } + + if ($numeric) { + $instance = $r->newInstanceArgs((array) $validator['options']); + } else { + $instance = $r->newInstance($validator['options']); + } + } else { + $instance = $r->newInstance(); + } + } + + if ($messages) { + if (is_array($messages)) { + $instance->setMessages($messages); + } elseif (is_string($messages)) { + $instance->setMessage($messages); + } + } + $instance->zfBreakChainOnFailure = $validator['breakChainOnFailure']; + + if ($origName != $name) { + $validatorNames = array_keys($this->_validators); + $order = array_flip($validatorNames); + $order[$name] = $order[$origName]; + $validatorsExchange = array(); + unset($order[$origName]); + asort($order); + foreach ($order as $key => $index) { + if ($key == $name) { + $validatorsExchange[$key] = $instance; + continue; + } + $validatorsExchange[$key] = $this->_validators[$key]; + } + $this->_validators = $validatorsExchange; + } else { + $this->_validators[$name] = $instance; + } + + return $instance; + } + + /** + * Lazy-load a decorator + * + * @param array $decorator Decorator type and options + * @param mixed $name Decorator name or alias + * @return Zend_Form_Decorator_Interface + */ + protected function _loadDecorator(array $decorator, $name) + { + $sameName = false; + if ($name == $decorator['decorator']) { + $sameName = true; + } + + $instance = $this->_getDecorator($decorator['decorator'], $decorator['options']); + if ($sameName) { + $newName = get_class($instance); + $decoratorNames = array_keys($this->_decorators); + $order = array_flip($decoratorNames); + $order[$newName] = $order[$name]; + $decoratorsExchange = array(); + unset($order[$name]); + asort($order); + foreach ($order as $key => $index) { + if ($key == $newName) { + $decoratorsExchange[$key] = $instance; + continue; + } + $decoratorsExchange[$key] = $this->_decorators[$key]; + } + $this->_decorators = $decoratorsExchange; + } else { + $this->_decorators[$name] = $instance; + } + + return $instance; + } + + /** + * Retrieve error messages and perform translation and value substitution + * + * @return array + */ + protected function _getErrorMessages() + { + $translator = $this->getTranslator(); + $messages = $this->getErrorMessages(); + $value = $this->getValue(); + foreach ($messages as $key => $message) { + if (null !== $translator) { + $message = $translator->translate($message); + } + if (($this->isArray() || is_array($value)) + && !empty($value) + ) { + $aggregateMessages = array(); + foreach ($value as $val) { + $aggregateMessages[] = str_replace('%value%', $val, $message); + } + $messages[$key] = implode($this->getErrorMessageSeparator(), $aggregateMessages); + } else { + $messages[$key] = str_replace('%value%', $value, $message); + } + } + return $messages; + } + + /** + * Are there custom error messages registered? + * + * @return bool + */ + protected function _hasErrorMessages() + { + return !empty($this->_errorMessages); + } +} diff --git a/libs/Zend/Form/Element/Button.php b/libs/Zend/Form/Element/Button.php new file mode 100644 index 0000000000000000000000000000000000000000..e8413f43062ca57cf614e1358deb7ed448533f0e --- /dev/null +++ b/libs/Zend/Form/Element/Button.php @@ -0,0 +1,42 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element_Submit */ +// require_once 'Zend/Form/Element/Submit.php'; + +/** + * Button form element + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Button.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Element_Button extends Zend_Form_Element_Submit +{ + /** + * Use formButton view helper by default + * @var string + */ + public $helper = 'formButton'; +} diff --git a/libs/Zend/Form/Element/Captcha.php b/libs/Zend/Form/Element/Captcha.php new file mode 100644 index 0000000000000000000000000000000000000000..8d11d26a9f839ddde93c50e726383d1a39f3f3e0 --- /dev/null +++ b/libs/Zend/Form/Element/Captcha.php @@ -0,0 +1,306 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Captcha.php 22329 2010-05-30 15:12:58Z bittarman $ + */ + +/** @see Zend_Form_Element_Xhtml */ +// require_once 'Zend/Form/Element/Xhtml.php'; + +/** @see Zend_Captcha_Adapter */ +// require_once 'Zend/Captcha/Adapter.php'; + +/** + * Generic captcha element + * + * This element allows to insert CAPTCHA into the form in order + * to validate that human is submitting the form. The actual + * logic is contained in the captcha adapter. + * + * @see http://en.wikipedia.org/wiki/Captcha + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Form_Element_Captcha extends Zend_Form_Element_Xhtml +{ + /** + * Captcha plugin type constant + */ + const CAPTCHA = 'CAPTCHA'; + + /** + * Captcha adapter + * + * @var Zend_Captcha_Adapter + */ + protected $_captcha; + + /** + * Get captcha adapter + * + * @return Zend_Captcha_Adapter + */ + public function getCaptcha() + { + return $this->_captcha; + } + + /** + * Set captcha adapter + * + * @param string|array|Zend_Captcha_Adapter $captcha + * @param array $options + */ + public function setCaptcha($captcha, $options = array()) + { + if ($captcha instanceof Zend_Captcha_Adapter) { + $instance = $captcha; + } else { + if (is_array($captcha)) { + if (array_key_exists('captcha', $captcha)) { + $name = $captcha['captcha']; + unset($captcha['captcha']); + } else { + $name = array_shift($captcha); + } + $options = array_merge($options, $captcha); + } else { + $name = $captcha; + } + + $name = $this->getPluginLoader(self::CAPTCHA)->load($name); + if (empty($options)) { + $instance = new $name; + } else { + $r = new ReflectionClass($name); + if ($r->hasMethod('__construct')) { + $instance = $r->newInstanceArgs(array($options)); + } else { + $instance = $r->newInstance(); + } + } + } + + $this->_captcha = $instance; + $this->_captcha->setName($this->getName()); + return $this; + } + + /** + * Constructor + * + * $spec may be: + * - string: name of element + * - array: options with which to configure element + * - Zend_Config: Zend_Config with options for configuring element + * + * @param string|array|Zend_Config $spec + * @return void + */ + public function __construct($spec, $options = null) + { + parent::__construct($spec, $options); + $this->setAllowEmpty(true) + ->setRequired(true) + ->setAutoInsertNotEmptyValidator(false) + ->addValidator($this->getCaptcha(), true); + } + + /** + * Return all attributes + * + * @return array + */ + public function getAttribs() + { + $attribs = get_object_vars($this); + unset($attribs['helper']); + foreach ($attribs as $key => $value) { + if ('_' == substr($key, 0, 1)) { + unset($attribs[$key]); + } + } + + return $attribs; + } + + /** + * Set options + * + * Overrides to allow passing captcha options + * + * @param array $options + * @return Zend_Form_Element_Captcha + */ + public function setOptions(array $options) + { + if (array_key_exists('captcha', $options)) { + if (array_key_exists('captchaOptions', $options)) { + $this->setCaptcha($options['captcha'], $options['captchaOptions']); + unset($options['captchaOptions']); + } else { + $this->setCaptcha($options['captcha']); + } + unset($options['captcha']); + } + parent::setOptions($options); + return $this; + } + + /** + * Render form element + * + * @param Zend_View_Interface $view + * @return string + */ + public function render(Zend_View_Interface $view = null) + { + $captcha = $this->getCaptcha(); + $captcha->setName($this->getFullyQualifiedName()); + + $decorators = $this->getDecorators(); + + $decorator = $captcha->getDecorator(); + if (!empty($decorator)) { + array_unshift($decorators, $decorator); + } + + $decorator = array('Captcha', array('captcha' => $captcha)); + array_unshift($decorators, $decorator); + + $this->setDecorators($decorators); + + $this->setValue($this->getCaptcha()->generate()); + + return parent::render($view); + } + + /** + * Retrieve plugin loader for validator or filter chain + * + * Support for plugin loader for Captcha adapters + * + * @param string $type + * @return Zend_Loader_PluginLoader + * @throws Zend_Loader_Exception on invalid type. + */ + public function getPluginLoader($type) + { + $type = strtoupper($type); + if ($type == self::CAPTCHA) { + if (!isset($this->_loaders[$type])) { + // require_once 'Zend/Loader/PluginLoader.php'; + $this->_loaders[$type] = new Zend_Loader_PluginLoader( + array('Zend_Captcha' => 'Zend/Captcha/') + ); + } + return $this->_loaders[$type]; + } else { + return parent::getPluginLoader($type); + } + } + + /** + * Add prefix path for plugin loader for captcha adapters + * + * This method handles the captcha type, the rest is handled by + * the parent + * @param string $prefix + * @param string $path + * @param string $type + * @return Zend_Form_Element + * @see Zend_Form_Element::addPrefixPath + */ + public function addPrefixPath($prefix, $path, $type = null) + { + $type = strtoupper($type); + switch ($type) { + case null: + $loader = $this->getPluginLoader(self::CAPTCHA); + $cPrefix = rtrim($prefix, '_') . '_Captcha'; + $cPath = rtrim($path, '/\\') . '/Captcha'; + $loader->addPrefixPath($cPrefix, $cPath); + return parent::addPrefixPath($prefix, $path); + case self::CAPTCHA: + $loader = $this->getPluginLoader($type); + $loader->addPrefixPath($prefix, $path); + return $this; + default: + return parent::addPrefixPath($prefix, $path, $type); + } + } + + /** + * Load default decorators + * + * @return void + */ + public function loadDefaultDecorators() + { + if ($this->loadDefaultDecoratorsIsDisabled()) { + return $this; + } + + $decorators = $this->getDecorators(); + if (empty($decorators)) { + $this->addDecorator('Errors') + ->addDecorator('Description', array('tag' => 'p', 'class' => 'description')) + ->addDecorator('HtmlTag', array('tag' => 'dd')) + ->addDecorator('Label', array('tag' => 'dt')); + } + return $this; + } + + /** + * Is the captcha valid? + * + * @param mixed $value + * @param mixed $context + * @return boolean + */ + public function isValid($value, $context = null) + { + $this->getCaptcha()->setName($this->getName()); + $belongsTo = $this->getBelongsTo(); + if (empty($belongsTo) || !is_array($context)) { + return parent::isValid($value, $context); + } + + $name = $this->getFullyQualifiedName(); + $root = substr($name, 0, strpos($name, '[')); + $segments = substr($name, strpos($name, '[')); + $segments = ltrim($segments, '['); + $segments = rtrim($segments, ']'); + $segments = explode('][', $segments); + array_unshift($segments, $root); + array_pop($segments); + $newContext = $context; + foreach ($segments as $segment) { + if (array_key_exists($segment, $newContext)) { + $newContext = $newContext[$segment]; + } + } + + return parent::isValid($value, $newContext); + } +} diff --git a/libs/Zend/Form/Element/Checkbox.php b/libs/Zend/Form/Element/Checkbox.php new file mode 100644 index 0000000000000000000000000000000000000000..2d52bcc56cc93c8781efe42f8e1b1c2d953cdc84 --- /dev/null +++ b/libs/Zend/Form/Element/Checkbox.php @@ -0,0 +1,203 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element_Xhtml */ +// require_once 'Zend/Form/Element/Xhtml.php'; + +/** + * Checkbox form element + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Checkbox.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Element_Checkbox extends Zend_Form_Element_Xhtml +{ + /** + * Is the checkbox checked? + * @var bool + */ + public $checked = false; + + /** + * Use formCheckbox view helper by default + * @var string + */ + public $helper = 'formCheckbox'; + + /** + * Options that will be passed to the view helper + * @var array + */ + public $options = array( + 'checkedValue' => '1', + 'uncheckedValue' => '0', + ); + + /** + * Value when checked + * @var string + */ + protected $_checkedValue = '1'; + + /** + * Value when not checked + * @var string + */ + protected $_uncheckedValue = '0'; + + /** + * Current value + * @var string 0 or 1 + */ + protected $_value = '0'; + + /** + * Set options + * + * Intercept checked and unchecked values and set them early; test stored + * value against checked and unchecked values after configuration. + * + * @param array $options + * @return Zend_Form_Element_Checkbox + */ + public function setOptions(array $options) + { + if (array_key_exists('checkedValue', $options)) { + $this->setCheckedValue($options['checkedValue']); + unset($options['checkedValue']); + } + if (array_key_exists('uncheckedValue', $options)) { + $this->setUncheckedValue($options['uncheckedValue']); + unset($options['uncheckedValue']); + } + parent::setOptions($options); + + $curValue = $this->getValue(); + $test = array($this->getCheckedValue(), $this->getUncheckedValue()); + if (!in_array($curValue, $test)) { + $this->setValue($curValue); + } + + return $this; + } + + /** + * Set value + * + * If value matches checked value, sets to that value, and sets the checked + * flag to true. + * + * Any other value causes the unchecked value to be set as the current + * value, and the checked flag to be set as false. + * + * + * @param mixed $value + * @return Zend_Form_Element_Checkbox + */ + public function setValue($value) + { + if ($value == $this->getCheckedValue()) { + parent::setValue($value); + $this->checked = true; + } else { + parent::setValue($this->getUncheckedValue()); + $this->checked = false; + } + return $this; + } + + /** + * Set checked value + * + * @param string $value + * @return Zend_Form_Element_Checkbox + */ + public function setCheckedValue($value) + { + $this->_checkedValue = (string) $value; + $this->options['checkedValue'] = $value; + return $this; + } + + /** + * Get value when checked + * + * @return string + */ + public function getCheckedValue() + { + return $this->_checkedValue; + } + + /** + * Set unchecked value + * + * @param string $value + * @return Zend_Form_Element_Checkbox + */ + public function setUncheckedValue($value) + { + $this->_uncheckedValue = (string) $value; + $this->options['uncheckedValue'] = $value; + return $this; + } + + /** + * Get value when not checked + * + * @return string + */ + public function getUncheckedValue() + { + return $this->_uncheckedValue; + } + + /** + * Set checked flag + * + * @param bool $flag + * @return Zend_Form_Element_Checkbox + */ + public function setChecked($flag) + { + $this->checked = (bool) $flag; + if ($this->checked) { + $this->setValue($this->getCheckedValue()); + } else { + $this->setValue($this->getUncheckedValue()); + } + return $this; + } + + /** + * Get checked flag + * + * @return bool + */ + public function isChecked() + { + return $this->checked; + } +} diff --git a/libs/Zend/Form/Element/Exception.php b/libs/Zend/Form/Element/Exception.php new file mode 100644 index 0000000000000000000000000000000000000000..6e3c9a6c248486c74aa58e5801fdec1aca9e8cb0 --- /dev/null +++ b/libs/Zend/Form/Element/Exception.php @@ -0,0 +1,37 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @version $Id: Exception.php 20096 2010-01-06 02:05:09Z bkarwin $ + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Exception */ +// require_once 'Zend/Form/Exception.php'; + +/** + * Exception for Zend_Form component. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Form_Element_Exception extends Zend_Form_Exception +{ +} diff --git a/libs/Zend/Form/Element/File.php b/libs/Zend/Form/Element/File.php new file mode 100644 index 0000000000000000000000000000000000000000..dc990bc2a5ddf51e1dac69fbbe4ecacd7bfb2149 --- /dev/null +++ b/libs/Zend/Form/Element/File.php @@ -0,0 +1,907 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element_Xhtml */ +// require_once 'Zend/Form/Element/Xhtml.php'; + +/** + * Zend_Form_Element + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: File.php 22372 2010-06-04 20:17:58Z thomas $ + */ +class Zend_Form_Element_File extends Zend_Form_Element_Xhtml +{ + /** + * Plugin loader type + */ + const TRANSFER_ADAPTER = 'TRANSFER_ADAPTER'; + + /** + * @var string Default view helper + */ + public $helper = 'formFile'; + + /** + * @var Zend_File_Transfer_Adapter_Abstract + */ + protected $_adapter; + + /** + * @var boolean Already validated ? + */ + protected $_validated = false; + + /** + * @var boolean Disable value to be equal to file content + */ + protected $_valueDisabled = false; + + /** + * @var integer Internal multifile counter + */ + protected $_counter = 1; + + /** + * @var integer Maximum file size for MAX_FILE_SIZE attribut of form + */ + protected static $_maxFileSize = -1; + + /** + * Load default decorators + * + * @return void + */ + public function loadDefaultDecorators() + { + if ($this->loadDefaultDecoratorsIsDisabled()) { + return $this; + } + + $decorators = $this->getDecorators(); + if (empty($decorators)) { + $this->addDecorator('File') + ->addDecorator('Errors') + ->addDecorator('Description', array('tag' => 'p', 'class' => 'description')) + ->addDecorator('HtmlTag', array('tag' => 'dd')) + ->addDecorator('Label', array('tag' => 'dt')); + } + return $this; + } + + /** + * Set plugin loader + * + * @param Zend_Loader_PluginLoader_Interface $loader + * @param string $type + * @return Zend_Form_Element_File + */ + public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type) + { + $type = strtoupper($type); + + if ($type != self::TRANSFER_ADAPTER) { + return parent::setPluginLoader($loader, $type); + } + + $this->_loaders[$type] = $loader; + return $this; + } + + /** + * Get Plugin Loader + * + * @param string $type + * @return Zend_Loader_PluginLoader_Interface + */ + public function getPluginLoader($type) + { + $type = strtoupper($type); + + if ($type != self::TRANSFER_ADAPTER) { + return parent::getPluginLoader($type); + } + + if (!array_key_exists($type, $this->_loaders)) { + // require_once 'Zend/Loader/PluginLoader.php'; + $loader = new Zend_Loader_PluginLoader(array( + 'Zend_File_Transfer_Adapter' => 'Zend/File/Transfer/Adapter/', + )); + $this->setPluginLoader($loader, self::TRANSFER_ADAPTER); + } + + return $this->_loaders[$type]; + } + + /** + * Add prefix path for plugin loader + * + * @param string $prefix + * @param string $path + * @param string $type + * @return Zend_Form_Element_File + */ + public function addPrefixPath($prefix, $path, $type = null) + { + $type = strtoupper($type); + if (!empty($type) && ($type != self::TRANSFER_ADAPTER)) { + return parent::addPrefixPath($prefix, $path, $type); + } + + if (empty($type)) { + $pluginPrefix = rtrim($prefix, '_') . '_Transfer_Adapter'; + $pluginPath = rtrim($path, DIRECTORY_SEPARATOR) . '/Transfer/Adapter/'; + $loader = $this->getPluginLoader(self::TRANSFER_ADAPTER); + $loader->addPrefixPath($pluginPrefix, $pluginPath); + return parent::addPrefixPath($prefix, $path, null); + } + + $loader = $this->getPluginLoader($type); + $loader->addPrefixPath($prefix, $path); + return $this; + } + + /** + * Set transfer adapter + * + * @param string|Zend_File_Transfer_Adapter_Abstract $adapter + * @return Zend_Form_Element_File + */ + public function setTransferAdapter($adapter) + { + if ($adapter instanceof Zend_File_Transfer_Adapter_Abstract) { + $this->_adapter = $adapter; + } elseif (is_string($adapter)) { + $loader = $this->getPluginLoader(self::TRANSFER_ADAPTER); + $class = $loader->load($adapter); + $this->_adapter = new $class; + } else { + // require_once 'Zend/Form/Element/Exception.php'; + throw new Zend_Form_Element_Exception('Invalid adapter specified'); + } + + foreach (array('filter', 'validate') as $type) { + $loader = $this->getPluginLoader($type); + $this->_adapter->setPluginLoader($loader, $type); + } + + return $this; + } + + /** + * Get transfer adapter + * + * Lazy loads HTTP transfer adapter when no adapter registered. + * + * @return Zend_File_Transfer_Adapter_Abstract + */ + public function getTransferAdapter() + { + if (null === $this->_adapter) { + $this->setTransferAdapter('Http'); + } + return $this->_adapter; + } + + /** + * Add Validator; proxy to adapter + * + * @param string|Zend_Validate_Interface $validator + * @param bool $breakChainOnFailure + * @param mixed $options + * @return Zend_Form_Element_File + */ + public function addValidator($validator, $breakChainOnFailure = false, $options = array()) + { + $adapter = $this->getTransferAdapter(); + $adapter->addValidator($validator, $breakChainOnFailure, $options, $this->getName()); + $this->_validated = false; + + return $this; + } + + /** + * Add multiple validators at once; proxy to adapter + * + * @param array $validators + * @return Zend_Form_Element_File + */ + public function addValidators(array $validators) + { + $adapter = $this->getTransferAdapter(); + $adapter->addValidators($validators, $this->getName()); + $this->_validated = false; + + return $this; + } + + /** + * Add multiple validators at once, overwriting; proxy to adapter + * + * @param array $validators + * @return Zend_Form_Element_File + */ + public function setValidators(array $validators) + { + $adapter = $this->getTransferAdapter(); + $adapter->setValidators($validators, $this->getName()); + $this->_validated = false; + + return $this; + } + + /** + * Retrieve validator by name; proxy to adapter + * + * @param string $name + * @return Zend_Validate_Interface|null + */ + public function getValidator($name) + { + $adapter = $this->getTransferAdapter(); + return $adapter->getValidator($name); + } + + /** + * Retrieve all validators; proxy to adapter + * + * @return array + */ + public function getValidators() + { + $adapter = $this->getTransferAdapter(); + $validators = $adapter->getValidators($this->getName()); + if ($validators === null) { + $validators = array(); + } + + return $validators; + } + + /** + * Remove validator by name; proxy to adapter + * + * @param string $name + * @return Zend_Form_Element_File + */ + public function removeValidator($name) + { + $adapter = $this->getTransferAdapter(); + $adapter->removeValidator($name); + $this->_validated = false; + + return $this; + } + + /** + * Remove all validators; proxy to adapter + * + * @return Zend_Form_Element_File + */ + public function clearValidators() + { + $adapter = $this->getTransferAdapter(); + $adapter->clearValidators(); + $this->_validated = false; + + return $this; + } + + /** + * Add Filter; proxy to adapter + * + * @param string|array $filter Type of filter to add + * @param string|array $options Options to set for the filter + * @return Zend_Form_Element_File + */ + public function addFilter($filter, $options = null) + { + $adapter = $this->getTransferAdapter(); + $adapter->addFilter($filter, $options, $this->getName()); + + return $this; + } + + /** + * Add Multiple filters at once; proxy to adapter + * + * @param array $filters + * @return Zend_Form_Element_File + */ + public function addFilters(array $filters) + { + $adapter = $this->getTransferAdapter(); + $adapter->addFilters($filters, $this->getName()); + + return $this; + } + + /** + * Sets a filter for the class, erasing all previous set; proxy to adapter + * + * @param string|array $filter Filter to set + * @return Zend_Form_Element_File + */ + public function setFilters(array $filters) + { + $adapter = $this->getTransferAdapter(); + $adapter->setFilters($filters, $this->getName()); + + return $this; + } + + /** + * Retrieve individual filter; proxy to adapter + * + * @param string $name + * @return Zend_Filter_Interface|null + */ + public function getFilter($name) + { + $adapter = $this->getTransferAdapter(); + return $adapter->getFilter($name); + } + + /** + * Returns all set filters; proxy to adapter + * + * @return array List of set filters + */ + public function getFilters() + { + $adapter = $this->getTransferAdapter(); + $filters = $adapter->getFilters($this->getName()); + + if ($filters === null) { + $filters = array(); + } + return $filters; + } + + /** + * Remove an individual filter; proxy to adapter + * + * @param string $name + * @return Zend_Form_Element_File + */ + public function removeFilter($name) + { + $adapter = $this->getTransferAdapter(); + $adapter->removeFilter($name); + + return $this; + } + + /** + * Remove all filters; proxy to adapter + * + * @return Zend_Form_Element_File + */ + public function clearFilters() + { + $adapter = $this->getTransferAdapter(); + $adapter->clearFilters(); + + return $this; + } + + /** + * Validate upload + * + * @param string $value File, can be optional, give null to validate all files + * @param mixed $context + * @return bool + */ + public function isValid($value, $context = null) + { + if ($this->_validated) { + return true; + } + + $adapter = $this->getTransferAdapter(); + $translator = $this->getTranslator(); + if ($translator !== null) { + $adapter->setTranslator($translator); + } + + if (!$this->isRequired()) { + $adapter->setOptions(array('ignoreNoFile' => true), $this->getName()); + } else { + $adapter->setOptions(array('ignoreNoFile' => false), $this->getName()); + if ($this->autoInsertNotEmptyValidator() && !$this->getValidator('NotEmpty')) { + $this->addValidator = array('validator' => 'NotEmpty', 'breakChainOnFailure' => true); + } + } + + if($adapter->isValid($this->getName())) { + $this->_validated = true; + return true; + } + + $this->_validated = false; + return false; + } + + /** + * Receive the uploaded file + * + * @return boolean + */ + public function receive() + { + if (!$this->_validated) { + if (!$this->isValid($this->getName())) { + return false; + } + } + + $adapter = $this->getTransferAdapter(); + if ($adapter->receive($this->getName())) { + return true; + } + + return false; + } + + /** + * Retrieve error codes; proxy to transfer adapter + * + * @return array + */ + public function getErrors() + { + return parent::getErrors() + $this->getTransferAdapter()->getErrors(); + } + + /** + * Are there errors registered? + * + * @return bool + */ + public function hasErrors() + { + return (parent::hasErrors() || $this->getTransferAdapter()->hasErrors()); + } + + /** + * Retrieve error messages; proxy to transfer adapter + * + * @return array + */ + public function getMessages() + { + return parent::getMessages() + $this->getTransferAdapter()->getMessages(); + } + + /** + * Set the upload destination + * + * @param string $path + * @return Zend_Form_Element_File + */ + public function setDestination($path) + { + $this->getTransferAdapter()->setDestination($path, $this->getName()); + return $this; + } + + /** + * Get the upload destination + * + * @return string + */ + public function getDestination() + { + return $this->getTransferAdapter()->getDestination($this->getName()); + } + + /** + * Get the final filename + * + * @param string $value (Optional) Element or file to return + * @param boolean $path (Optional) Return also the path, defaults to true + * @return string + */ + public function getFileName($value = null, $path = true) + { + if (empty($value)) { + $value = $this->getName(); + } + + return $this->getTransferAdapter()->getFileName($value, $path); + } + + /** + * Get internal file informations + * + * @param string $value (Optional) Element or file to return + * @return array + */ + public function getFileInfo($value = null) + { + if (empty($value)) { + $value = $this->getName(); + } + + return $this->getTransferAdapter()->getFileInfo($value); + } + + /** + * Set a multifile element + * + * @param integer $count Number of file elements + * @return Zend_Form_Element_File Provides fluent interface + */ + public function setMultiFile($count) + { + if ((integer) $count < 2) { + $this->setIsArray(false); + $this->_counter = 1; + } else { + $this->setIsArray(true); + $this->_counter = (integer) $count; + } + + return $this; + } + + /** + * Returns the multifile element number + * + * @return integer + */ + public function getMultiFile() + { + return $this->_counter; + } + + /** + * Sets the maximum file size of the form + * + * @return integer + */ + public function getMaxFileSize() + { + if (self::$_maxFileSize < 0) { + $ini = $this->_convertIniToInteger(trim(ini_get('post_max_size'))); + $max = $this->_convertIniToInteger(trim(ini_get('upload_max_filesize'))); + $min = max($ini, $max); + if ($ini > 0) { + $min = min($min, $ini); + } + + if ($max > 0) { + $min = min($min, $max); + } + + self::$_maxFileSize = $min; + } + + return self::$_maxFileSize; + } + + /** + * Sets the maximum file size of the form + * + * @param integer $size + * @return integer + */ + public function setMaxFileSize($size) + { + $ini = $this->_convertIniToInteger(trim(ini_get('post_max_size'))); + $max = $this->_convertIniToInteger(trim(ini_get('upload_max_filesize'))); + + if (($max > -1) && ($size > $max)) { + trigger_error("Your 'upload_max_filesize' config setting limits the maximum filesize to '$max'. You tried to set '$size'.", E_USER_NOTICE); + $size = $max; + } + + if (($ini > -1) && ($size > $ini)) { + trigger_error("Your 'post_max_size' config setting limits the maximum filesize to '$ini'. You tried to set '$size'.", E_USER_NOTICE); + $size = $ini; + } + + self::$_maxFileSize = $size; + return $this; + } + + /** + * Converts a ini setting to a integer value + * + * @param string $setting + * @return integer + */ + private function _convertIniToInteger($setting) + { + if (!is_numeric($setting)) { + $type = strtoupper(substr($setting, -1)); + $setting = (integer) substr($setting, 0, -1); + + switch ($type) { + case 'K' : + $setting *= 1024; + break; + + case 'M' : + $setting *= 1024 * 1024; + break; + + case 'G' : + $setting *= 1024 * 1024 * 1024; + break; + + default : + break; + } + } + + return (integer) $setting; + } + + /** + * Set if the file will be uploaded when getting the value + * This defaults to false which will force receive() when calling getValues() + * + * @param boolean $flag Sets if the file is handled as the elements value + * @return Zend_Form_Element_File + */ + public function setValueDisabled($flag) + { + $this->_valueDisabled = (bool) $flag; + return $this; + } + + /** + * Returns if the file will be uploaded when calling getValues() + * + * @return boolean Receive the file on calling getValues()? + */ + public function isValueDisabled() + { + return $this->_valueDisabled; + } + + /** + * Processes the file, returns null or the filename only + * For the complete path, use getFileName + * + * @return null|string + */ + public function getValue() + { + if ($this->_value !== null) { + return $this->_value; + } + + $content = $this->getTransferAdapter()->getFileName($this->getName()); + if (empty($content)) { + return null; + } + + if (!$this->isValid(null)) { + return null; + } + + if (!$this->_valueDisabled && !$this->receive()) { + return null; + } + + return $this->getFileName(null, false); + } + + /** + * Disallow setting the value + * + * @param mixed $value + * @return Zend_Form_Element_File + */ + public function setValue($value) + { + return $this; + } + + /** + * Set translator object for localization + * + * @param Zend_Translate|null $translator + * @return Zend_Form_Element_File + */ + public function setTranslator($translator = null) + { + $adapter = $this->getTransferAdapter(); + $adapter->setTranslator($translator); + parent::setTranslator($translator); + + return $this; + } + + /** + * Retrieve localization translator object + * + * @return Zend_Translate_Adapter|null + */ + public function getTranslator() + { + if ($this->translatorIsDisabled()) { + return null; + } + + $translator = $this->getTransferAdapter()->getTranslator(); + if (null === $translator) { + // require_once 'Zend/Form.php'; + return Zend_Form::getDefaultTranslator(); + } + + return $translator; + } + + /** + * Indicate whether or not translation should be disabled + * + * @param bool $flag + * @return Zend_Form_Element_File + */ + public function setDisableTranslator($flag) + { + $adapter = $this->getTransferAdapter(); + $adapter->setDisableTranslator($flag); + $this->_translatorDisabled = (bool) $flag; + + return $this; + } + + /** + * Is translation disabled? + * + * @return bool + */ + public function translatorIsDisabled() + { + $adapter = $this->getTransferAdapter(); + return $adapter->translatorIsDisabled(); + } + + /** + * Was the file received? + * + * @return bool + */ + public function isReceived() + { + $adapter = $this->getTransferAdapter(); + return $adapter->isReceived($this->getName()); + } + + /** + * Was the file uploaded? + * + * @return bool + */ + public function isUploaded() + { + $adapter = $this->getTransferAdapter(); + return $adapter->isUploaded($this->getName()); + } + + /** + * Has the file been filtered? + * + * @return bool + */ + public function isFiltered() + { + $adapter = $this->getTransferAdapter(); + return $adapter->isFiltered($this->getName()); + } + + /** + * Returns the hash for this file element + * + * @param string $hash (Optional) Hash algorithm to use + * @return string|array Hashstring + */ + public function getHash($hash = 'crc32') + { + $adapter = $this->getTransferAdapter(); + return $adapter->getHash($hash, $this->getName()); + } + + /** + * Returns the filesize for this file element + * + * @return string|array Filesize + */ + public function getFileSize() + { + $adapter = $this->getTransferAdapter(); + return $adapter->getFileSize($this->getName()); + } + + /** + * Returns the mimetype for this file element + * + * @return string|array Mimetype + */ + public function getMimeType() + { + $adapter = $this->getTransferAdapter(); + return $adapter->getMimeType($this->getName()); + } + + /** + * Render form element + * Checks for decorator interface to prevent errors + * + * @param Zend_View_Interface $view + * @return string + */ + public function render(Zend_View_Interface $view = null) + { + $marker = false; + foreach ($this->getDecorators() as $decorator) { + if ($decorator instanceof Zend_Form_Decorator_Marker_File_Interface) { + $marker = true; + } + } + + if (!$marker) { + // require_once 'Zend/Form/Element/Exception.php'; + throw new Zend_Form_Element_Exception('No file decorator found... unable to render file element'); + } + + return parent::render($view); + } + + /** + * Retrieve error messages and perform translation and value substitution + * + * @return array + */ + protected function _getErrorMessages() + { + $translator = $this->getTranslator(); + $messages = $this->getErrorMessages(); + $value = $this->getFileName(); + foreach ($messages as $key => $message) { + if (null !== $translator) { + $message = $translator->translate($message); + } + + if ($this->isArray() || is_array($value)) { + $aggregateMessages = array(); + foreach ($value as $val) { + $aggregateMessages[] = str_replace('%value%', $val, $message); + } + + if (!empty($aggregateMessages)) { + $messages[$key] = $aggregateMessages; + } + } else { + $messages[$key] = str_replace('%value%', $value, $message); + } + } + + return $messages; + } +} diff --git a/libs/Zend/Form/Element/Hash.php b/libs/Zend/Form/Element/Hash.php new file mode 100644 index 0000000000000000000000000000000000000000..c6501b55466346d4c531d169eb2ddeee40bec6c4 --- /dev/null +++ b/libs/Zend/Form/Element/Hash.php @@ -0,0 +1,259 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element_Xhtml */ +// require_once 'Zend/Form/Element/Xhtml.php'; + +/** + * CSRF form protection + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Hash.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Element_Hash extends Zend_Form_Element_Xhtml +{ + /** + * Use formHidden view helper by default + * @var string + */ + public $helper = 'formHidden'; + + /** + * Actual hash used. + * + * @var mixed + */ + protected $_hash; + + /** + * Salt for CSRF token + * @var string + */ + protected $_salt = 'salt'; + + /** + * @var Zend_Session_Namespace + */ + protected $_session; + + /** + * TTL for CSRF token + * @var int + */ + protected $_timeout = 300; + + /** + * Constructor + * + * Creates session namespace for CSRF token, and adds validator for CSRF + * token. + * + * @param string|array|Zend_Config $spec + * @param array|Zend_Config $options + * @return void + */ + public function __construct($spec, $options = null) + { + parent::__construct($spec, $options); + + $this->setAllowEmpty(false) + ->setRequired(true) + ->initCsrfValidator(); + } + + /** + * Set session object + * + * @param Zend_Session_Namespace $session + * @return Zend_Form_Element_Hash + */ + public function setSession($session) + { + $this->_session = $session; + return $this; + } + + /** + * Get session object + * + * Instantiate session object if none currently exists + * + * @return Zend_Session_Namespace + */ + public function getSession() + { + if (null === $this->_session) { + // require_once 'Zend/Session/Namespace.php'; + $this->_session = new Zend_Session_Namespace($this->getSessionName()); + } + return $this->_session; + } + + /** + * Initialize CSRF validator + * + * Creates Session namespace, and initializes CSRF token in session. + * Additionally, adds validator for validating CSRF token. + * + * @return Zend_Form_Element_Hash + */ + public function initCsrfValidator() + { + $session = $this->getSession(); + if (isset($session->hash)) { + $rightHash = $session->hash; + } else { + $rightHash = null; + } + + $this->addValidator('Identical', true, array($rightHash)); + return $this; + } + + /** + * Salt for CSRF token + * + * @param string $salt + * @return Zend_Form_Element_Hash + */ + public function setSalt($salt) + { + $this->_salt = (string) $salt; + return $this; + } + + /** + * Retrieve salt for CSRF token + * + * @return string + */ + public function getSalt() + { + return $this->_salt; + } + + /** + * Retrieve CSRF token + * + * If no CSRF token currently exists, generates one. + * + * @return string + */ + public function getHash() + { + if (null === $this->_hash) { + $this->_generateHash(); + } + return $this->_hash; + } + + /** + * Get session namespace for CSRF token + * + * Generates a session namespace based on salt, element name, and class. + * + * @return string + */ + public function getSessionName() + { + return __CLASS__ . '_' . $this->getSalt() . '_' . $this->getName(); + } + + /** + * Set timeout for CSRF session token + * + * @param int $ttl + * @return Zend_Form_Element_Hash + */ + public function setTimeout($ttl) + { + $this->_timeout = (int) $ttl; + return $this; + } + + /** + * Get CSRF session token timeout + * + * @return int + */ + public function getTimeout() + { + return $this->_timeout; + } + + /** + * Override getLabel() to always be empty + * + * @return null + */ + public function getLabel() + { + return null; + } + + /** + * Initialize CSRF token in session + * + * @return void + */ + public function initCsrfToken() + { + $session = $this->getSession(); + $session->setExpirationHops(1, null, true); + $session->setExpirationSeconds($this->getTimeout()); + $session->hash = $this->getHash(); + } + + /** + * Render CSRF token in form + * + * @param Zend_View_Interface $view + * @return string + */ + public function render(Zend_View_Interface $view = null) + { + $this->initCsrfToken(); + return parent::render($view); + } + + /** + * Generate CSRF token + * + * Generates CSRF token and stores both in {@link $_hash} and element + * value. + * + * @return void + */ + protected function _generateHash() + { + $this->_hash = md5( + mt_rand(1,1000000) + . $this->getSalt() + . $this->getName() + . mt_rand(1,1000000) + ); + $this->setValue($this->_hash); + } +} diff --git a/libs/Zend/Form/Element/Hidden.php b/libs/Zend/Form/Element/Hidden.php new file mode 100644 index 0000000000000000000000000000000000000000..28bae576df7cd4cb996e197cd87e920cfe7502c0 --- /dev/null +++ b/libs/Zend/Form/Element/Hidden.php @@ -0,0 +1,42 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element_Xhtml */ +// require_once 'Zend/Form/Element/Xhtml.php'; + +/** + * Hidden form element + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Hidden.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Element_Hidden extends Zend_Form_Element_Xhtml +{ + /** + * Use formHidden view helper by default + * @var string + */ + public $helper = 'formHidden'; +} diff --git a/libs/Zend/Form/Element/Image.php b/libs/Zend/Form/Element/Image.php new file mode 100644 index 0000000000000000000000000000000000000000..67b72d79aa340ec1c5595ddb9ea15c793d609ad2 --- /dev/null +++ b/libs/Zend/Form/Element/Image.php @@ -0,0 +1,132 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element_Xhtml */ +// require_once 'Zend/Form/Element/Xhtml.php'; + +/** + * Image form element + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Image.php 22329 2010-05-30 15:12:58Z bittarman $ + */ +class Zend_Form_Element_Image extends Zend_Form_Element_Xhtml +{ + /** + * What view helper to use when using view helper decorator + * @var string + */ + public $helper = 'formImage'; + + /** + * Image source + * @var string + */ + public $src; + + /** + * Image value + * @var mixed + */ + protected $_imageValue; + + /** + * Load default decorators + * + * @return void + */ + public function loadDefaultDecorators() + { + if ($this->loadDefaultDecoratorsIsDisabled()) { + return $this; + } + + $decorators = $this->getDecorators(); + if (empty($decorators)) { + $this->addDecorator('Tooltip') + ->addDecorator('Image') + ->addDecorator('Errors') + ->addDecorator('HtmlTag', array('tag' => 'dd')) + ->addDecorator('Label', array('tag' => 'dt')); + } + return $this; + } + + /** + * Set image path + * + * @param string $path + * @return Zend_Form_Element_Image + */ + public function setImage($path) + { + $this->src = (string) $path; + return $this; + } + + /** + * Get image path + * + * @return string + */ + public function getImage() + { + return $this->src; + } + + /** + * Set image value to use when submitted + * + * @param mixed $value + * @return Zend_Form_Element_Image + */ + public function setImageValue($value) + { + $this->_imageValue = $value; + return $this; + } + + /** + * Get image value to use when submitted + * + * @return mixed + */ + public function getImageValue() + { + return $this->_imageValue; + } + + /** + * Was this element used to submit the form? + * + * @return bool + */ + public function isChecked() + { + $imageValue = $this->getImageValue(); + return ((null !== $imageValue) && ($this->getValue() == $imageValue)); + } + +} diff --git a/libs/Zend/Form/Element/Multi.php b/libs/Zend/Form/Element/Multi.php new file mode 100644 index 0000000000000000000000000000000000000000..09b49807f7c0929ce0325eed12aa65d0f91e92e5 --- /dev/null +++ b/libs/Zend/Form/Element/Multi.php @@ -0,0 +1,317 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element_Xhtml */ +// require_once 'Zend/Form/Element/Xhtml.php'; + +/** + * Base class for multi-option form elements + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Multi.php 22323 2010-05-30 11:15:38Z thomas $ + */ +abstract class Zend_Form_Element_Multi extends Zend_Form_Element_Xhtml +{ + /** + * Array of options for multi-item + * @var array + */ + public $options = array(); + + /** + * Flag: autoregister inArray validator? + * @var bool + */ + protected $_registerInArrayValidator = true; + + /** + * Separator to use between options; defaults to '<br />'. + * @var string + */ + protected $_separator = '<br />'; + + /** + * Which values are translated already? + * @var array + */ + protected $_translated = array(); + + /** + * Retrieve separator + * + * @return mixed + */ + public function getSeparator() + { + return $this->_separator; + } + + /** + * Set separator + * + * @param mixed $separator + * @return self + */ + public function setSeparator($separator) + { + $this->_separator = $separator; + return $this; + } + + /** + * Retrieve options array + * + * @return array + */ + protected function _getMultiOptions() + { + if (null === $this->options || !is_array($this->options)) { + $this->options = array(); + } + + return $this->options; + } + + /** + * Add an option + * + * @param string $option + * @param string $value + * @return Zend_Form_Element_Multi + */ + public function addMultiOption($option, $value = '') + { + $option = (string) $option; + $this->_getMultiOptions(); + if (!$this->_translateOption($option, $value)) { + $this->options[$option] = $value; + } + + return $this; + } + + /** + * Add many options at once + * + * @param array $options + * @return Zend_Form_Element_Multi + */ + public function addMultiOptions(array $options) + { + foreach ($options as $option => $value) { + if (is_array($value) + && array_key_exists('key', $value) + && array_key_exists('value', $value) + ) { + $this->addMultiOption($value['key'], $value['value']); + } else { + $this->addMultiOption($option, $value); + } + } + return $this; + } + + /** + * Set all options at once (overwrites) + * + * @param array $options + * @return Zend_Form_Element_Multi + */ + public function setMultiOptions(array $options) + { + $this->clearMultiOptions(); + return $this->addMultiOptions($options); + } + + /** + * Retrieve single multi option + * + * @param string $option + * @return mixed + */ + public function getMultiOption($option) + { + $option = (string) $option; + $this->_getMultiOptions(); + if (isset($this->options[$option])) { + $this->_translateOption($option, $this->options[$option]); + return $this->options[$option]; + } + + return null; + } + + /** + * Retrieve options + * + * @return array + */ + public function getMultiOptions() + { + $this->_getMultiOptions(); + foreach ($this->options as $option => $value) { + $this->_translateOption($option, $value); + } + return $this->options; + } + + /** + * Remove a single multi option + * + * @param string $option + * @return bool + */ + public function removeMultiOption($option) + { + $option = (string) $option; + $this->_getMultiOptions(); + if (isset($this->options[$option])) { + unset($this->options[$option]); + if (isset($this->_translated[$option])) { + unset($this->_translated[$option]); + } + return true; + } + + return false; + } + + /** + * Clear all options + * + * @return Zend_Form_Element_Multi + */ + public function clearMultiOptions() + { + $this->options = array(); + $this->_translated = array(); + return $this; + } + + /** + * Set flag indicating whether or not to auto-register inArray validator + * + * @param bool $flag + * @return Zend_Form_Element_Multi + */ + public function setRegisterInArrayValidator($flag) + { + $this->_registerInArrayValidator = (bool) $flag; + return $this; + } + + /** + * Get status of auto-register inArray validator flag + * + * @return bool + */ + public function registerInArrayValidator() + { + return $this->_registerInArrayValidator; + } + + /** + * Is the value provided valid? + * + * Autoregisters InArray validator if necessary. + * + * @param string $value + * @param mixed $context + * @return bool + */ + public function isValid($value, $context = null) + { + if ($this->registerInArrayValidator()) { + if (!$this->getValidator('InArray')) { + $multiOptions = $this->getMultiOptions(); + $options = array(); + + foreach ($multiOptions as $opt_value => $opt_label) { + // optgroup instead of option label + if (is_array($opt_label)) { + $options = array_merge($options, array_keys($opt_label)); + } + else { + $options[] = $opt_value; + } + } + + $this->addValidator( + 'InArray', + true, + array($options) + ); + } + } + return parent::isValid($value, $context); + } + + /** + * Translate an option + * + * @param string $option + * @param string $value + * @return bool + */ + protected function _translateOption($option, $value) + { + if ($this->translatorIsDisabled()) { + return false; + } + + if (!isset($this->_translated[$option]) && !empty($value)) { + $this->options[$option] = $this->_translateValue($value); + if ($this->options[$option] === $value) { + return false; + } + $this->_translated[$option] = true; + return true; + } + + return false; + } + + /** + * Translate a multi option value + * + * @param string $value + * @return string + */ + protected function _translateValue($value) + { + if (is_array($value)) { + foreach ($value as $key => $val) { + $value[$key] = $this->_translateValue($val); + } + return $value; + } else { + if (null !== ($translator = $this->getTranslator())) { + return $translator->translate($value); + } + + return $value; + } + } +} diff --git a/libs/Zend/Form/Element/MultiCheckbox.php b/libs/Zend/Form/Element/MultiCheckbox.php new file mode 100644 index 0000000000000000000000000000000000000000..b5cec8a8bf7782df703870ac7a1255ab778fa81b --- /dev/null +++ b/libs/Zend/Form/Element/MultiCheckbox.php @@ -0,0 +1,52 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element_Multi */ +// require_once 'Zend/Form/Element/Multi.php'; + +/** + * MultiCheckbox form element + * + * Allows specifyinc a (multi-)dimensional associative array of values to use + * as labelled checkboxes; these will return an array of values for those + * checkboxes selected. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: MultiCheckbox.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Element_MultiCheckbox extends Zend_Form_Element_Multi +{ + /** + * Use formMultiCheckbox view helper by default + * @var string + */ + public $helper = 'formMultiCheckbox'; + + /** + * MultiCheckbox is an array of values by default + * @var bool + */ + protected $_isArray = true; +} diff --git a/libs/Zend/Form/Element/Multiselect.php b/libs/Zend/Form/Element/Multiselect.php new file mode 100644 index 0000000000000000000000000000000000000000..83c417ef2688291969de4b2a60aaafdefe675691 --- /dev/null +++ b/libs/Zend/Form/Element/Multiselect.php @@ -0,0 +1,54 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element_Select */ +// require_once 'Zend/Form/Element/Select.php'; + +/** + * Multiselect form element + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Multiselect.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Element_Multiselect extends Zend_Form_Element_Select +{ + /** + * 'multiple' attribute + * @var string + */ + public $multiple = 'multiple'; + + /** + * Use formSelect view helper by default + * @var string + */ + public $helper = 'formSelect'; + + /** + * Multiselect is an array of values by default + * @var bool + */ + protected $_isArray = true; +} diff --git a/libs/Zend/Form/Element/Password.php b/libs/Zend/Form/Element/Password.php new file mode 100644 index 0000000000000000000000000000000000000000..20b2c6af8056b03b85d4ec8e58e8d7af468e08f2 --- /dev/null +++ b/libs/Zend/Form/Element/Password.php @@ -0,0 +1,88 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element_Xhtml */ +// require_once 'Zend/Form/Element/Xhtml.php'; + +/** + * Password form element + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Password.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Element_Password extends Zend_Form_Element_Xhtml +{ + /** + * Use formPassword view helper by default + * @var string + */ + public $helper = 'formPassword'; + + /** + * Whether or not to render the password + * @var bool + */ + public $renderPassword = false; + + /** + * Set flag indicating whether or not to render the password + * @param bool $flag + * @return Zend_Form_Element_Password + */ + public function setRenderPassword($flag) + { + $this->renderPassword = (bool) $flag; + return $this; + } + + /** + * Get value of renderPassword flag + * + * @return bool + */ + public function renderPassword() + { + return $this->renderPassword; + } + + /** + * Override isValid() + * + * Ensure that validation error messages mask password value. + * + * @param string $value + * @param mixed $context + * @return bool + */ + public function isValid($value, $context = null) + { + foreach ($this->getValidators() as $validator) { + if ($validator instanceof Zend_Validate_Abstract) { + $validator->setObscureValue(true); + } + } + return parent::isValid($value, $context); + } +} diff --git a/libs/Zend/Form/Element/Radio.php b/libs/Zend/Form/Element/Radio.php new file mode 100644 index 0000000000000000000000000000000000000000..85f523a2faaf79ed0b02481dae55d3378a93dbec --- /dev/null +++ b/libs/Zend/Form/Element/Radio.php @@ -0,0 +1,60 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element_Multi */ +// require_once 'Zend/Form/Element/Multi.php'; + +/** + * Radio form element + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Radio.php 22329 2010-05-30 15:12:58Z bittarman $ + */ +class Zend_Form_Element_Radio extends Zend_Form_Element_Multi +{ + /** + * Use formRadio view helper by default + * @var string + */ + public $helper = 'formRadio'; + + /** + * Load default decorators + * + * Disables "for" attribute of label if label decorator enabled. + * + * @return void + */ + public function loadDefaultDecorators() + { + if ($this->loadDefaultDecoratorsIsDisabled()) { + return $this; + } + parent::loadDefaultDecorators(); + $this->addDecorator('Label', array('tag' => 'dt', + 'disableFor' => true)); + return $this; + } +} diff --git a/libs/Zend/Form/Element/Reset.php b/libs/Zend/Form/Element/Reset.php new file mode 100644 index 0000000000000000000000000000000000000000..6d4bec4c96d06f39439338888b5444a3c1019f0c --- /dev/null +++ b/libs/Zend/Form/Element/Reset.php @@ -0,0 +1,42 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element_Submit */ +// require_once 'Zend/Form/Element/Submit.php'; + +/** + * Reset form element + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Reset.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Element_Reset extends Zend_Form_Element_Submit +{ + /** + * Use formReset view helper by default + * @var string + */ + public $helper = 'formReset'; +} diff --git a/libs/Zend/Form/Element/Select.php b/libs/Zend/Form/Element/Select.php new file mode 100644 index 0000000000000000000000000000000000000000..132e033a7a8c3b84e8f9c96fb693bd8304038723 --- /dev/null +++ b/libs/Zend/Form/Element/Select.php @@ -0,0 +1,42 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element_Multi */ +// require_once 'Zend/Form/Element/Multi.php'; + +/** + * Select.php form element + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Select.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Element_Select extends Zend_Form_Element_Multi +{ + /** + * Use formSelect view helper by default + * @var string + */ + public $helper = 'formSelect'; +} diff --git a/libs/Zend/Form/Element/Submit.php b/libs/Zend/Form/Element/Submit.php new file mode 100644 index 0000000000000000000000000000000000000000..256b00ffb8cd0469aa805856d7eb1ae18127cf59 --- /dev/null +++ b/libs/Zend/Form/Element/Submit.php @@ -0,0 +1,127 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element_Xhtml */ +// require_once 'Zend/Form/Element/Xhtml.php'; + +/** + * Submit form element + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Submit.php 22329 2010-05-30 15:12:58Z bittarman $ + */ +class Zend_Form_Element_Submit extends Zend_Form_Element_Xhtml +{ + /** + * Default view helper to use + * @var string + */ + public $helper = 'formSubmit'; + + /** + * Constructor + * + * @param string|array|Zend_Config $spec Element name or configuration + * @param string|array|Zend_Config $options Element value or configuration + * @return void + */ + public function __construct($spec, $options = null) + { + if (is_string($spec) && ((null !== $options) && is_string($options))) { + $options = array('label' => $options); + } + + if (!isset($options['ignore'])) { + $options['ignore'] = true; + } + + parent::__construct($spec, $options); + } + + /** + * Return label + * + * If no label is present, returns the currently set name. + * + * If a translator is present, returns the translated label. + * + * @return string + */ + public function getLabel() + { + $value = parent::getLabel(); + + if (null === $value) { + $value = $this->getName(); + } + + if (null !== ($translator = $this->getTranslator())) { + return $translator->translate($value); + } + + return $value; + } + + /** + * Has this submit button been selected? + * + * @return bool + */ + public function isChecked() + { + $value = $this->getValue(); + + if (empty($value)) { + return false; + } + if ($value != $this->getLabel()) { + return false; + } + + return true; + } + + /** + * Default decorators + * + * Uses only 'Submit' and 'DtDdWrapper' decorators by default. + * + * @return void + */ + public function loadDefaultDecorators() + { + if ($this->loadDefaultDecoratorsIsDisabled()) { + return $this; + } + + $decorators = $this->getDecorators(); + if (empty($decorators)) { + $this->addDecorator('Tooltip') + ->addDecorator('ViewHelper') + ->addDecorator('DtDdWrapper'); + } + return $this; + } +} diff --git a/libs/Zend/Form/Element/Text.php b/libs/Zend/Form/Element/Text.php new file mode 100644 index 0000000000000000000000000000000000000000..fa30d44cf2ba81f89cc66ff971e5e912da001eaf --- /dev/null +++ b/libs/Zend/Form/Element/Text.php @@ -0,0 +1,42 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element_Xhtml */ +// require_once 'Zend/Form/Element/Xhtml.php'; + +/** + * Text form element + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Text.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Element_Text extends Zend_Form_Element_Xhtml +{ + /** + * Default form view helper to use for rendering + * @var string + */ + public $helper = 'formText'; +} diff --git a/libs/Zend/Form/Element/Textarea.php b/libs/Zend/Form/Element/Textarea.php new file mode 100644 index 0000000000000000000000000000000000000000..8dbce4553a328d941bdee1dfbac7139e3c1e52f2 --- /dev/null +++ b/libs/Zend/Form/Element/Textarea.php @@ -0,0 +1,42 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element_Xhtml */ +// require_once 'Zend/Form/Element/Xhtml.php'; + +/** + * Textarea form element + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Textarea.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +class Zend_Form_Element_Textarea extends Zend_Form_Element_Xhtml +{ + /** + * Use formTextarea view helper by default + * @var string + */ + public $helper = 'formTextarea'; +} diff --git a/libs/Zend/Form/Element/Xhtml.php b/libs/Zend/Form/Element/Xhtml.php new file mode 100644 index 0000000000000000000000000000000000000000..24cf69303670cefedbf365012fb8fe0b439e7744 --- /dev/null +++ b/libs/Zend/Form/Element/Xhtml.php @@ -0,0 +1,37 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form_Element */ +// require_once 'Zend/Form/Element.php'; + +/** + * Base element for XHTML elements + * + * @category Zend + * @package Zend_Form + * @subpackage Element + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Xhtml.php 20096 2010-01-06 02:05:09Z bkarwin $ + */ +abstract class Zend_Form_Element_Xhtml extends Zend_Form_Element +{ +} diff --git a/libs/Zend/Form/Exception.php b/libs/Zend/Form/Exception.php new file mode 100644 index 0000000000000000000000000000000000000000..8ec65aa9b9d705cc9c90d628910a804a9fd17fc6 --- /dev/null +++ b/libs/Zend/Form/Exception.php @@ -0,0 +1,35 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @version $Id: Exception.php 20096 2010-01-06 02:05:09Z bkarwin $ + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Exception */ +// require_once 'Zend/Exception.php'; + +/** + * Exception for Zend_Form component. + * + * @category Zend + * @package Zend_Form + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Form_Exception extends Zend_Exception +{ +} diff --git a/libs/Zend/Form/SubForm.php b/libs/Zend/Form/SubForm.php new file mode 100644 index 0000000000000000000000000000000000000000..dd20c0e06401f0a48946114eafbe31c5a5a92b3c --- /dev/null +++ b/libs/Zend/Form/SubForm.php @@ -0,0 +1,61 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Form + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** Zend_Form */ +// require_once 'Zend/Form.php'; + +/** + * Zend_Form_SubForm + * + * @category Zend + * @package Zend_Form + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: SubForm.php 22329 2010-05-30 15:12:58Z bittarman $ + */ +class Zend_Form_SubForm extends Zend_Form +{ + /** + * Whether or not form elements are members of an array + * @var bool + */ + protected $_isArray = true; + + /** + * Load the default decorators + * + * @return Zend_Form_SubForm + */ + public function loadDefaultDecorators() + { + if ($this->loadDefaultDecoratorsIsDisabled()) { + return $this; + } + + $decorators = $this->getDecorators(); + if (empty($decorators)) { + $this->addDecorator('FormElements') + ->addDecorator('HtmlTag', array('tag' => 'dl')) + ->addDecorator('Fieldset') + ->addDecorator('DtDdWrapper'); + } + return $this; + } +} diff --git a/libs/Zend/Http/Client/Adapter/Proxy.php b/libs/Zend/Http/Client/Adapter/Proxy.php index cda6d62dbb2f0487bf5d55387447204965ee340a..084fdae121e6b75036dfe9e4f37361cefefef330 100644 --- a/libs/Zend/Http/Client/Adapter/Proxy.php +++ b/libs/Zend/Http/Client/Adapter/Proxy.php @@ -16,7 +16,7 @@ * @category Zend * @package Zend_Http * @subpackage Client_Adapter - * @version $Id: Proxy.php 20947 2010-02-06 17:09:07Z padraic $ + * @version $Id: Proxy.php 22445 2010-06-16 09:09:12Z bate $ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -60,6 +60,7 @@ class Zend_Http_Client_Adapter_Proxy extends Zend_Http_Client_Adapter_Socket 'ssltransport' => 'ssl', 'sslcert' => null, 'sslpassphrase' => null, + 'sslusecontext' => false, 'proxy_host' => '', 'proxy_port' => 8080, 'proxy_user' => '', @@ -167,15 +168,26 @@ class Zend_Http_Client_Adapter_Proxy extends Zend_Http_Client_Adapter_Socket $request .= "$v\r\n"; } - // Add the request body - $request .= "\r\n" . $body; - + if(is_resource($body)) { + $request .= "\r\n"; + } else { + // Add the request body + $request .= "\r\n" . $body; + } + // Send the request if (! @fwrite($this->socket, $request)) { // require_once 'Zend/Http/Client/Adapter/Exception.php'; throw new Zend_Http_Client_Adapter_Exception("Error writing request to proxy server"); } - + + if(is_resource($body)) { + if(stream_copy_to_stream($body, $this->socket) == 0) { + // require_once 'Zend/Http/Client/Adapter/Exception.php'; + throw new Zend_Http_Client_Adapter_Exception('Error writing request to server'); + } + } + return $request; } diff --git a/libs/Zend/Loader/Autoloader.php b/libs/Zend/Loader/Autoloader.php index 11ddf6eca41d2036b4fbd753f683fe62e6f9bb5f..67be780d1a73f56f70b76a7f3a5e3cebd2782153 100644 --- a/libs/Zend/Loader/Autoloader.php +++ b/libs/Zend/Loader/Autoloader.php @@ -16,7 +16,7 @@ * @package Zend_Loader * @subpackage Autoloader * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) - * @version $Id: Autoloader.php 20096 2010-01-06 02:05:09Z bkarwin $ + * @version $Id: Autoloader.php 22480 2010-06-21 17:37:20Z matthew $ * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -120,14 +120,12 @@ class Zend_Loader_Autoloader if ($autoloader->autoload($class)) { return true; } - } elseif (is_string($autoloader)) { - if ($autoloader($class)) { + } elseif (is_array($autoloader)) { + if (call_user_func($autoloader, $class)) { return true; } - } elseif (is_array($autoloader)) { - $object = array_shift($autoloader); - $method = array_shift($autoloader); - if (call_user_func(array($object, $method), $class)) { + } elseif (is_string($autoloader) || is_callable($autoloader)) { + if ($autoloader($class)) { return true; } } diff --git a/libs/Zend/Log/Writer/Syslog.php b/libs/Zend/Log/Writer/Syslog.php index 310bcf54eca531f0a6e29aa8606511ec3cfe8e0b..d7827c9ce331c6f3943b0dab578af4d60cdf3b93 100644 --- a/libs/Zend/Log/Writer/Syslog.php +++ b/libs/Zend/Log/Writer/Syslog.php @@ -17,7 +17,7 @@ * @subpackage Writer * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id: Syslog.php 20096 2010-01-06 02:05:09Z bkarwin $ + * @version $Id: Syslog.php 22477 2010-06-21 12:15:35Z matthew $ */ /** Zend_Log_Writer_Abstract */ @@ -75,10 +75,17 @@ class Zend_Log_Writer_Syslog extends Zend_Log_Writer_Abstract /** * Facility used by this syslog-writer instance - * @var string + * @var int */ protected $_facility = LOG_USER; + /** + * _validFacilities + * + * @var array + */ + protected $_validFacilities = array(); + /** * Class constructor * @@ -90,15 +97,21 @@ class Zend_Log_Writer_Syslog extends Zend_Log_Writer_Abstract if (isset($params['application'])) { $this->_application = $params['application']; } + + $runInitializeSyslog = true; if (isset($params['facility'])) { - $this->_facility = $params['facility']; + $this->_facility = $this->setFacility($params['facility']); + $runInitializeSyslog = false; + } + + if ($runInitializeSyslog) { + $this->_initializeSyslog(); } - $this->_initializeSyslog(); } - + /** * Create a new instance of Zend_Log_Writer_Syslog - * + * * @param array|Zend_Config $config * @return Zend_Log_Writer_Syslog * @throws Zend_Log_Exception @@ -108,11 +121,45 @@ class Zend_Log_Writer_Syslog extends Zend_Log_Writer_Abstract return new self(self::_parseConfig($config)); } + /** + * Initialize values facilities + * + * @return void + */ + protected function _initializeValidFacilities() + { + $constants = array( + 'LOG_AUTH', + 'LOG_AUTHPRIV', + 'LOG_CRON', + 'LOG_DAEMON', + 'LOG_KERN', + 'LOG_LOCAL0', + 'LOG_LOCAL1', + 'LOG_LOCAL2', + 'LOG_LOCAL3', + 'LOG_LOCAL4', + 'LOG_LOCAL5', + 'LOG_LOCAL6', + 'LOG_LOCAL7', + 'LOG_LPR', + 'LOG_MAIL', + 'LOG_NEWS', + 'LOG_SYSLOG', + 'LOG_USER', + 'LOG_UUCP' + ); + + foreach ($constants as $constant) { + if (defined($constant)) { + $this->_validFacilities[] = constant($constant); + } + } + } + /** * Initialize syslog / set application name and facility * - * @param string $application Application name - * @param string $facility Syslog facility * @return void */ protected function _initializeSyslog() @@ -125,14 +172,32 @@ class Zend_Log_Writer_Syslog extends Zend_Log_Writer_Abstract /** * Set syslog facility * - * @param string $facility Syslog facility + * @param int $facility Syslog facility * @return void + * @throws Zend_Log_Exception for invalid log facility */ public function setFacility($facility) { if ($this->_facility === $facility) { return; } + + if (!count($this->_validFacilities)) { + $this->_initializeValidFacilities(); + } + + if (!in_array($facility, $this->_validFacilities)) { + // require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception('Invalid log facility provided; please see http://php.net/openlog for a list of valid facility values'); + } + + if (strstr(strtolower(PHP_OS), 'windows') + && ($facility !== LOG_USER) + ) { + // require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception('Only LOG_USER is a valid log facility on Windows'); + } + $this->_facility = $facility; $this->_initializeSyslog(); } diff --git a/libs/Zend/Validate/Abstract.php b/libs/Zend/Validate/Abstract.php index cbecc5eebff2072078381ea52e72aafe7b0b6295..a73185fd338a239774127464dfb61893bdb0697d 100644 --- a/libs/Zend/Validate/Abstract.php +++ b/libs/Zend/Validate/Abstract.php @@ -16,7 +16,7 @@ * @package Zend_Validate * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id: Abstract.php 21726 2010-03-31 19:57:27Z rob $ + * @version $Id: Abstract.php 22473 2010-06-20 08:30:04Z thomas $ */ /** @@ -216,10 +216,10 @@ abstract class Zend_Validate_Abstract implements Zend_Validate_Interface $message = $this->_messageTemplates[$messageKey]; if (null !== ($translator = $this->getTranslator())) { - if ($translator->isTranslated($message)) { - $message = $translator->translate($message); - } elseif ($translator->isTranslated($messageKey)) { + if ($translator->isTranslated($messageKey)) { $message = $translator->translate($messageKey); + } else { + $message = $translator->translate($message); } } @@ -354,14 +354,14 @@ abstract class Zend_Validate_Abstract implements Zend_Validate_Interface /** * Does this validator have its own specific translator? - * + * * @return bool */ public function hasTranslator() { return (bool)$this->_translator; - } - + } + /** * Set default translation object for all validate objects * @@ -404,14 +404,14 @@ abstract class Zend_Validate_Abstract implements Zend_Validate_Interface /** * Is there a default translation object set? - * + * * @return boolean */ public static function hasDefaultTranslator() - { + { return (bool)self::$_defaultTranslator; } - + /** * Indicate whether or not translation should be disabled * diff --git a/libs/Zend/Validate/File/Upload.php b/libs/Zend/Validate/File/Upload.php index 9a12f301d1cf9202b8982fbdae77bf57e7158ef0..92bbba59c32a9e25f72215f8f3d8646fc87ee983 100644 --- a/libs/Zend/Validate/File/Upload.php +++ b/libs/Zend/Validate/File/Upload.php @@ -16,7 +16,7 @@ * @package Zend_Validate * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id: Upload.php 20431 2010-01-19 21:36:05Z thomas $ + * @version $Id: Upload.php 22399 2010-06-09 19:08:28Z thomas $ */ /** @@ -156,6 +156,7 @@ class Zend_Validate_File_Upload extends Zend_Validate_Abstract */ public function isValid($value, $file = null) { + $this->_messages = null; if (array_key_exists($value, $this->_files)) { $files[$value] = $this->_files[$value]; } else { diff --git a/libs/Zend/Validate/Iban.php b/libs/Zend/Validate/Iban.php index 097cd32cbd45a309ad03137c6719be94da585d09..4b781a09c6970ddb884d8e2c74bf1c2711b32147 100644 --- a/libs/Zend/Validate/Iban.php +++ b/libs/Zend/Validate/Iban.php @@ -16,7 +16,7 @@ * @package Zend_Validate * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id: Iban.php 21563 2010-03-19 10:10:45Z thomas $ + * @version $Id: Iban.php 22401 2010-06-09 19:25:49Z thomas $ */ /** @@ -123,14 +123,14 @@ class Zend_Validate_Iban extends Zend_Validate_Abstract } } - if ($locale !== false) { + if (empty($locale)) { // require_once 'Zend/Registry.php'; if (Zend_Registry::isRegistered('Zend_Locale')) { $locale = Zend_Registry::get('Zend_Locale'); } } - if (!empty($locale)) { + if ($locale !== null) { $this->setLocale($locale); } } diff --git a/libs/Zend/Version.php b/libs/Zend/Version.php index 392ae3c7dfcd581d048709a8e75ad12787b4d019..96dbbf353cbb6f1751b8954d9fbcfed1a0da2129 100644 --- a/libs/Zend/Version.php +++ b/libs/Zend/Version.php @@ -16,7 +16,7 @@ * @package Zend_Version * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id: Version.php 22304 2010-05-26 14:25:33Z matthew $ + * @version $Id: Version.php 22482 2010-06-21 17:58:24Z matthew $ */ /** @@ -32,7 +32,7 @@ final class Zend_Version /** * Zend Framework version identification - see compareVersion() */ - const VERSION = '1.10.5'; + const VERSION = '1.10.6'; /** * Compare the specified Zend Framework version string $version