Abstract.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_File_Transfer
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * Abstract class for file transfers (Downloads and Uploads)
  23. *
  24. * @category Zend
  25. * @package Zend_File_Transfer
  26. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. abstract class Zend_File_Transfer_Adapter_Abstract
  30. {
  31. /**@+
  32. * Plugin loader Constants
  33. */
  34. const FILTER = 'FILTER';
  35. const VALIDATE = 'VALIDATE';
  36. /**@-*/
  37. /**
  38. * Internal list of breaks
  39. *
  40. * @var array
  41. */
  42. protected $_break = array();
  43. /**
  44. * Internal list of filters
  45. *
  46. * @var array
  47. */
  48. protected $_filters = array();
  49. /**
  50. * Plugin loaders for filter and validation chains
  51. *
  52. * @var array
  53. */
  54. protected $_loaders = array();
  55. /**
  56. * Internal list of messages
  57. *
  58. * @var array
  59. */
  60. protected $_messages = array();
  61. /**
  62. * @var Zend_Translate
  63. */
  64. protected $_translator;
  65. /**
  66. * Is translation disabled?
  67. *
  68. * @var bool
  69. */
  70. protected $_translatorDisabled = false;
  71. /**
  72. * Internal list of validators
  73. * @var array
  74. */
  75. protected $_validators = array();
  76. /**
  77. * Internal list of files
  78. * This array looks like this:
  79. * array(form => array( - Form is the name within the form or, if not set the filename
  80. * name, - Original name of this file
  81. * type, - Mime type of this file
  82. * size, - Filesize in bytes
  83. * tmp_name, - Internalally temporary filename for uploaded files
  84. * error, - Error which has occured
  85. * destination, - New destination for this file
  86. * validators, - Set validator names for this file
  87. * files - Set file names for this file
  88. * ))
  89. *
  90. * @var array
  91. */
  92. protected $_files = array();
  93. /**
  94. * TMP directory
  95. * @var string
  96. */
  97. protected $_tmpDir;
  98. /**
  99. * Available options for file transfers
  100. */
  101. protected $_options = array(
  102. 'ignoreNoFile' => false,
  103. 'useByteString' => true,
  104. 'magicFile' => null,
  105. 'detectInfos' => true,
  106. );
  107. /**
  108. * Send file
  109. *
  110. * @param mixed $options
  111. * @return bool
  112. */
  113. abstract public function send($options = null);
  114. /**
  115. * Receive file
  116. *
  117. * @param mixed $options
  118. * @return bool
  119. */
  120. abstract public function receive($options = null);
  121. /**
  122. * Is file sent?
  123. *
  124. * @param array|string|null $files
  125. * @return bool
  126. */
  127. abstract public function isSent($files = null);
  128. /**
  129. * Is file received?
  130. *
  131. * @param array|string|null $files
  132. * @return bool
  133. */
  134. abstract public function isReceived($files = null);
  135. /**
  136. * Has a file been uploaded ?
  137. *
  138. * @param array|string|null $files
  139. * @return bool
  140. */
  141. abstract public function isUploaded($files = null);
  142. /**
  143. * Has the file been filtered ?
  144. *
  145. * @param array|string|null $files
  146. * @return bool
  147. */
  148. abstract public function isFiltered($files = null);
  149. /**
  150. * Retrieve progress of transfer
  151. *
  152. * @return float
  153. */
  154. public static function getProgress()
  155. {
  156. require_once 'Zend/File/Transfer/Exception.php';
  157. throw new Zend_File_Transfer_Exception('Method must be implemented within the adapter');
  158. }
  159. /**
  160. * Set plugin loader to use for validator or filter chain
  161. *
  162. * @param Zend_Loader_PluginLoader_Interface $loader
  163. * @param string $type 'filter', or 'validate'
  164. * @return Zend_File_Transfer_Adapter_Abstract
  165. * @throws Zend_File_Transfer_Exception on invalid type
  166. */
  167. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type)
  168. {
  169. $type = strtoupper($type);
  170. switch ($type) {
  171. case self::FILTER:
  172. case self::VALIDATE:
  173. $this->_loaders[$type] = $loader;
  174. return $this;
  175. default:
  176. require_once 'Zend/File/Transfer/Exception.php';
  177. throw new Zend_File_Transfer_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
  178. }
  179. }
  180. /**
  181. * Retrieve plugin loader for validator or filter chain
  182. *
  183. * Instantiates with default rules if none available for that type. Use
  184. * 'filter' or 'validate' for $type.
  185. *
  186. * @param string $type
  187. * @return Zend_Loader_PluginLoader
  188. * @throws Zend_File_Transfer_Exception on invalid type.
  189. */
  190. public function getPluginLoader($type)
  191. {
  192. $type = strtoupper($type);
  193. switch ($type) {
  194. case self::FILTER:
  195. case self::VALIDATE:
  196. $prefixSegment = ucfirst(strtolower($type));
  197. $pathSegment = $prefixSegment;
  198. if (!isset($this->_loaders[$type])) {
  199. $paths = array(
  200. 'Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/',
  201. 'Zend_' . $prefixSegment . '_File' => 'Zend/' . $pathSegment . '/File',
  202. );
  203. require_once 'Zend/Loader/PluginLoader.php';
  204. $this->_loaders[$type] = new Zend_Loader_PluginLoader($paths);
  205. } else {
  206. $loader = $this->_loaders[$type];
  207. $prefix = 'Zend_' . $prefixSegment . '_File_';
  208. if (!$loader->getPaths($prefix)) {
  209. $loader->addPrefixPath($prefix, str_replace('_', '/', $prefix));
  210. }
  211. }
  212. return $this->_loaders[$type];
  213. default:
  214. require_once 'Zend/File/Transfer/Exception.php';
  215. throw new Zend_File_Transfer_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  216. }
  217. }
  218. /**
  219. * Add prefix path for plugin loader
  220. *
  221. * If no $type specified, assumes it is a base path for both filters and
  222. * validators, and sets each according to the following rules:
  223. * - filters: $prefix = $prefix . '_Filter'
  224. * - validators: $prefix = $prefix . '_Validate'
  225. *
  226. * Otherwise, the path prefix is set on the appropriate plugin loader.
  227. *
  228. * @param string $prefix
  229. * @param string $path
  230. * @param string $type
  231. * @return Zend_File_Transfer_Adapter_Abstract
  232. * @throws Zend_File_Transfer_Exception for invalid type
  233. */
  234. public function addPrefixPath($prefix, $path, $type = null)
  235. {
  236. $type = strtoupper($type);
  237. switch ($type) {
  238. case self::FILTER:
  239. case self::VALIDATE:
  240. $loader = $this->getPluginLoader($type);
  241. $loader->addPrefixPath($prefix, $path);
  242. return $this;
  243. case null:
  244. $prefix = rtrim($prefix, '_');
  245. $path = rtrim($path, DIRECTORY_SEPARATOR);
  246. foreach (array(self::FILTER, self::VALIDATE) as $type) {
  247. $cType = ucfirst(strtolower($type));
  248. $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR;
  249. $pluginPrefix = $prefix . '_' . $cType;
  250. $loader = $this->getPluginLoader($type);
  251. $loader->addPrefixPath($pluginPrefix, $pluginPath);
  252. }
  253. return $this;
  254. default:
  255. require_once 'Zend/File/Transfer/Exception.php';
  256. throw new Zend_File_Transfer_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  257. }
  258. }
  259. /**
  260. * Add many prefix paths at once
  261. *
  262. * @param array $spec
  263. * @return Zend_File_Transfer_Exception
  264. */
  265. public function addPrefixPaths(array $spec)
  266. {
  267. if (isset($spec['prefix']) && isset($spec['path'])) {
  268. return $this->addPrefixPath($spec['prefix'], $spec['path']);
  269. }
  270. foreach ($spec as $type => $paths) {
  271. if (is_numeric($type) && is_array($paths)) {
  272. $type = null;
  273. if (isset($paths['prefix']) && isset($paths['path'])) {
  274. if (isset($paths['type'])) {
  275. $type = $paths['type'];
  276. }
  277. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  278. }
  279. } elseif (!is_numeric($type)) {
  280. if (!isset($paths['prefix']) || !isset($paths['path'])) {
  281. foreach ($paths as $prefix => $spec) {
  282. if (is_array($spec)) {
  283. foreach ($spec as $path) {
  284. if (!is_string($path)) {
  285. continue;
  286. }
  287. $this->addPrefixPath($prefix, $path, $type);
  288. }
  289. } elseif (is_string($spec)) {
  290. $this->addPrefixPath($prefix, $spec, $type);
  291. }
  292. }
  293. } else {
  294. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  295. }
  296. }
  297. }
  298. return $this;
  299. }
  300. /**
  301. * Adds a new validator for this class
  302. *
  303. * @param string|array $validator Type of validator to add
  304. * @param boolean $breakChainOnFailure If the validation chain should stop an failure
  305. * @param string|array $options Options to set for the validator
  306. * @param string|array $files Files to limit this validator to
  307. * @return Zend_File_Transfer_Adapter
  308. */
  309. public function addValidator($validator, $breakChainOnFailure = false, $options = null, $files = null)
  310. {
  311. if ($validator instanceof Zend_Validate_Interface) {
  312. $name = get_class($validator);
  313. } elseif (is_string($validator)) {
  314. $name = $this->getPluginLoader(self::VALIDATE)->load($validator);
  315. $validator = new $name($options);
  316. if (is_array($options) && isset($options['messages'])) {
  317. if (is_array($options['messages'])) {
  318. $validator->setMessages($options['messages']);
  319. } elseif (is_string($options['messages'])) {
  320. $validator->setMessage($options['messages']);
  321. }
  322. unset($options['messages']);
  323. }
  324. } else {
  325. require_once 'Zend/File/Transfer/Exception.php';
  326. throw new Zend_File_Transfer_Exception('Invalid validator provided to addValidator; must be string or Zend_Validate_Interface');
  327. }
  328. $this->_validators[$name] = $validator;
  329. $this->_break[$name] = $breakChainOnFailure;
  330. $files = $this->_getFiles($files, true, true);
  331. foreach ($files as $file) {
  332. $this->_files[$file]['validators'][] = $name;
  333. $this->_files[$file]['validated'] = false;
  334. }
  335. return $this;
  336. }
  337. /**
  338. * Add Multiple validators at once
  339. *
  340. * @param array $validators
  341. * @param string|array $files
  342. * @return Zend_File_Transfer_Adapter_Abstract
  343. */
  344. public function addValidators(array $validators, $files = null)
  345. {
  346. foreach ($validators as $name => $validatorInfo) {
  347. if ($validatorInfo instanceof Zend_Validate_Interface) {
  348. $this->addValidator($validatorInfo, null, null, $files);
  349. } else if (is_string($validatorInfo)) {
  350. if (!is_int($name)) {
  351. $this->addValidator($name, null, $validatorInfo, $files);
  352. } else {
  353. $this->addValidator($validatorInfo, null, null, $files);
  354. }
  355. } else if (is_array($validatorInfo)) {
  356. $argc = count($validatorInfo);
  357. $breakChainOnFailure = false;
  358. $options = array();
  359. if (isset($validatorInfo['validator'])) {
  360. $validator = $validatorInfo['validator'];
  361. if (isset($validatorInfo['breakChainOnFailure'])) {
  362. $breakChainOnFailure = $validatorInfo['breakChainOnFailure'];
  363. }
  364. if (isset($validatorInfo['options'])) {
  365. $options = $validatorInfo['options'];
  366. }
  367. $this->addValidator($validator, $breakChainOnFailure, $options, $files);
  368. } else {
  369. if (is_string($name)) {
  370. $validator = $name;
  371. $options = $validatorInfo;
  372. $this->addValidator($validator, $breakChainOnFailure, $options, $files);
  373. } else {
  374. $file = $files;
  375. switch (true) {
  376. case (0 == $argc):
  377. break;
  378. case (1 <= $argc):
  379. $validator = array_shift($validatorInfo);
  380. case (2 <= $argc):
  381. $breakChainOnFailure = array_shift($validatorInfo);
  382. case (3 <= $argc):
  383. $options = array_shift($validatorInfo);
  384. case (4 <= $argc):
  385. if (!empty($validatorInfo)) {
  386. $file = array_shift($validatorInfo);
  387. }
  388. default:
  389. $this->addValidator($validator, $breakChainOnFailure, $options, $file);
  390. break;
  391. }
  392. }
  393. }
  394. } else {
  395. require_once 'Zend/File/Transfer/Exception.php';
  396. throw new Zend_File_Transfer_Exception('Invalid validator passed to addValidators()');
  397. }
  398. }
  399. return $this;
  400. }
  401. /**
  402. * Sets a validator for the class, erasing all previous set
  403. *
  404. * @param string|array $validator Validator to set
  405. * @param string|array $files Files to limit this validator to
  406. * @return Zend_File_Transfer_Adapter
  407. */
  408. public function setValidators(array $validators, $files = null)
  409. {
  410. $this->clearValidators();
  411. return $this->addValidators($validators, $files);
  412. }
  413. /**
  414. * Determine if a given validator has already been registered
  415. *
  416. * @param string $name
  417. * @return bool
  418. */
  419. public function hasValidator($name)
  420. {
  421. return (false !== $this->_getValidatorIdentifier($name));
  422. }
  423. /**
  424. * Retrieve individual validator
  425. *
  426. * @param string $name
  427. * @return Zend_Validate_Interface|null
  428. */
  429. public function getValidator($name)
  430. {
  431. if (false === ($identifier = $this->_getValidatorIdentifier($name))) {
  432. return null;
  433. }
  434. return $this->_validators[$identifier];
  435. }
  436. /**
  437. * Returns all set validators
  438. *
  439. * @param string|array $files (Optional) Returns the validator for this files
  440. * @return null|array List of set validators
  441. */
  442. public function getValidators($files = null)
  443. {
  444. if ($files == null) {
  445. return $this->_validators;
  446. }
  447. $files = $this->_getFiles($files, true, true);
  448. $validators = array();
  449. foreach ($files as $file) {
  450. if (!empty($this->_files[$file]['validators'])) {
  451. $validators += $this->_files[$file]['validators'];
  452. }
  453. }
  454. $validators = array_unique($validators);
  455. $result = array();
  456. foreach ($validators as $validator) {
  457. $result[$validator] = $this->_validators[$validator];
  458. }
  459. return $result;
  460. }
  461. /**
  462. * Remove an individual validator
  463. *
  464. * @param string $name
  465. * @return Zend_File_Transfer_Adapter_Abstract
  466. */
  467. public function removeValidator($name)
  468. {
  469. if (false === ($key = $this->_getValidatorIdentifier($name))) {
  470. return $this;
  471. }
  472. unset($this->_validators[$key]);
  473. foreach (array_keys($this->_files) as $file) {
  474. if (empty($this->_files[$file]['validators'])) {
  475. continue;
  476. }
  477. $index = array_search($key, $this->_files[$file]['validators']);
  478. if ($index === false) {
  479. continue;
  480. }
  481. unset($this->_files[$file]['validators'][$index]);
  482. $this->_files[$file]['validated'] = false;
  483. }
  484. return $this;
  485. }
  486. /**
  487. * Remove all validators
  488. *
  489. * @return Zend_File_Transfer_Adapter_Abstract
  490. */
  491. public function clearValidators()
  492. {
  493. $this->_validators = array();
  494. foreach (array_keys($this->_files) as $file) {
  495. $this->_files[$file]['validators'] = array();
  496. $this->_files[$file]['validated'] = false;
  497. }
  498. return $this;
  499. }
  500. /**
  501. * Sets Options for adapters
  502. *
  503. * @param array $options Options to set
  504. * @param array $files (Optional) Files to set the options for
  505. */
  506. public function setOptions($options = array(), $files = null) {
  507. $file = $this->_getFiles($files, false, true);
  508. if (is_array($options)) {
  509. if (empty($file)) {
  510. $this->_options = array_merge($this->_options, $options);
  511. }
  512. foreach ($options as $name => $value) {
  513. foreach ($file as $key => $content) {
  514. switch ($name) {
  515. case 'magicFile' :
  516. $this->_files[$key]['options'][$name] = (string) $value;
  517. break;
  518. case 'ignoreNoFile' :
  519. case 'useByteString' :
  520. case 'detectInfos' :
  521. $this->_files[$key]['options'][$name] = (boolean) $value;
  522. break;
  523. default:
  524. require_once 'Zend/File/Transfer/Exception.php';
  525. throw new Zend_File_Transfer_Exception("Unknown option: $name = $value");
  526. }
  527. }
  528. }
  529. }
  530. return $this;
  531. }
  532. /**
  533. * Returns set options for adapters or files
  534. *
  535. * @param array $files (Optional) Files to return the options for
  536. * @return array Options for given files
  537. */
  538. public function getOptions($files = null) {
  539. $file = $this->_getFiles($files, false, true);
  540. foreach ($file as $key => $content) {
  541. if (isset($this->_files[$key]['options'])) {
  542. $options[$key] = $this->_files[$key]['options'];
  543. } else {
  544. $options[$key] = array();
  545. }
  546. }
  547. return $options;
  548. }
  549. /**
  550. * Checks if the files are valid
  551. *
  552. * @param string|array $files (Optional) Files to check
  553. * @return boolean True if all checks are valid
  554. */
  555. public function isValid($files = null)
  556. {
  557. $check = $this->_getFiles($files, false, true);
  558. if (empty($check)) {
  559. return false;
  560. }
  561. $translator = $this->getTranslator();
  562. $this->_messages = array();
  563. $break = false;
  564. foreach($check as $key => $content) {
  565. if (array_key_exists('validators', $content) &&
  566. in_array('Zend_Validate_File_Count', $content['validators'])) {
  567. $validator = $this->_validators['Zend_Validate_File_Count'];
  568. $count = $content;
  569. if (empty($content['tmp_name'])) {
  570. continue;
  571. }
  572. if (array_key_exists('destination', $content)) {
  573. $checkit = $content['destination'];
  574. } else {
  575. $checkit = dirname($content['tmp_name']);
  576. }
  577. $checkit .= DIRECTORY_SEPARATOR . $content['name'];
  578. $validator->addFile($checkit);
  579. }
  580. }
  581. if (isset($count)) {
  582. if (!$validator->isValid($count['tmp_name'], $count)) {
  583. $this->_messages += $validator->getMessages();
  584. }
  585. }
  586. foreach ($check as $key => $content) {
  587. $fileerrors = array();
  588. if (array_key_exists('validators', $content) && $content['validated']) {
  589. continue;
  590. }
  591. if (array_key_exists('validators', $content)) {
  592. foreach ($content['validators'] as $class) {
  593. $validator = $this->_validators[$class];
  594. if (method_exists($validator, 'setTranslator')) {
  595. $validator->setTranslator($translator);
  596. }
  597. if (($class === 'Zend_Validate_File_Upload') and (empty($content['tmp_name']))) {
  598. $tocheck = $key;
  599. } else {
  600. $tocheck = $content['tmp_name'];
  601. }
  602. if (!$validator->isValid($tocheck, $content)) {
  603. $fileerrors += $validator->getMessages();
  604. }
  605. if (!empty($content['options']['ignoreNoFile']) and (isset($fileerrors['fileUploadErrorNoFile']))) {
  606. unset($fileerrors['fileUploadErrorNoFile']);
  607. break;
  608. }
  609. if (($class === 'Zend_Validate_File_Upload') and (count($fileerrors) > 0)) {
  610. break;
  611. }
  612. if (($this->_break[$class]) and (count($fileerrors) > 0)) {
  613. $break = true;
  614. break;
  615. }
  616. }
  617. }
  618. if (count($fileerrors) > 0) {
  619. $this->_files[$key]['validated'] = false;
  620. } else {
  621. $this->_files[$key]['validated'] = true;
  622. }
  623. $this->_messages += $fileerrors;
  624. if ($break) {
  625. break;
  626. }
  627. }
  628. if (count($this->_messages) > 0) {
  629. return false;
  630. }
  631. return true;
  632. }
  633. /**
  634. * Returns found validation messages
  635. *
  636. * @return array
  637. */
  638. public function getMessages()
  639. {
  640. return $this->_messages;
  641. }
  642. /**
  643. * Retrieve error codes
  644. *
  645. * @return array
  646. */
  647. public function getErrors()
  648. {
  649. return array_keys($this->_messages);
  650. }
  651. /**
  652. * Are there errors registered?
  653. *
  654. * @return boolean
  655. */
  656. public function hasErrors()
  657. {
  658. return (!empty($this->_messages));
  659. }
  660. /**
  661. * Adds a new filter for this class
  662. *
  663. * @param string|array $filter Type of filter to add
  664. * @param string|array $options Options to set for the filter
  665. * @param string|array $files Files to limit this filter to
  666. * @return Zend_File_Transfer_Adapter
  667. */
  668. public function addFilter($filter, $options = null, $files = null)
  669. {
  670. if ($filter instanceof Zend_Filter_Interface) {
  671. $class = get_class($filter);
  672. } elseif (is_string($filter)) {
  673. $class = $this->getPluginLoader(self::FILTER)->load($filter);
  674. $filter = new $class($options);
  675. } else {
  676. require_once 'Zend/File/Transfer/Exception.php';
  677. throw new Zend_File_Transfer_Exception('Invalid filter specified');
  678. }
  679. $this->_filters[$class] = $filter;
  680. $files = $this->_getFiles($files, true, true);
  681. foreach ($files as $file) {
  682. $this->_files[$file]['filters'][] = $class;
  683. }
  684. return $this;
  685. }
  686. /**
  687. * Add Multiple filters at once
  688. *
  689. * @param array $filters
  690. * @param string|array $files
  691. * @return Zend_File_Transfer_Adapter_Abstract
  692. */
  693. public function addFilters(array $filters, $files = null)
  694. {
  695. foreach ($filters as $key => $spec) {
  696. if ($spec instanceof Zend_Filter_Interface) {
  697. $this->addFilter($spec, null, $files);
  698. continue;
  699. }
  700. if (is_string($key)) {
  701. $this->addFilter($key, $spec, $files);
  702. continue;
  703. }
  704. if (is_int($key)) {
  705. if (is_string($spec)) {
  706. $this->addFilter($spec, null, $files);
  707. continue;
  708. }
  709. if (is_array($spec)) {
  710. if (!array_key_exists('filter', $spec)) {
  711. continue;
  712. }
  713. $filter = $spec['filter'];
  714. unset($spec['filter']);
  715. $this->addFilter($filter, $spec, $files);
  716. continue;
  717. }
  718. continue;
  719. }
  720. }
  721. return $this;
  722. }
  723. /**
  724. * Sets a filter for the class, erasing all previous set
  725. *
  726. * @param string|array $filter Filter to set
  727. * @param string|array $files Files to limit this filter to
  728. * @return Zend_File_Transfer_Adapter
  729. */
  730. public function setFilters(array $filters, $files = null)
  731. {
  732. $this->clearFilters();
  733. return $this->addFilters($filters, $files);
  734. }
  735. /**
  736. * Determine if a given filter has already been registered
  737. *
  738. * @param string $name
  739. * @return bool
  740. */
  741. public function hasFilter($name)
  742. {
  743. return (false !== $this->_getFilterIdentifier($name));
  744. }
  745. /**
  746. * Retrieve individual filter
  747. *
  748. * @param string $name
  749. * @return Zend_Filter_Interface|null
  750. */
  751. public function getFilter($name)
  752. {
  753. if (false === ($identifier = $this->_getFilterIdentifier($name))) {
  754. return null;
  755. }
  756. return $this->_filters[$identifier];
  757. }
  758. /**
  759. * Returns all set filters
  760. *
  761. * @param string|array $files (Optional) Returns the filter for this files
  762. * @return array List of set filters
  763. * @throws Zend_File_Transfer_Exception When file not found
  764. */
  765. public function getFilters($files = null)
  766. {
  767. if ($files === null) {
  768. return $this->_filters;
  769. }
  770. $files = $this->_getFiles($files, true, true);
  771. $filters = array();
  772. foreach ($files as $file) {
  773. if (!empty($this->_files[$file]['filters'])) {
  774. $filters += $this->_files[$file]['filters'];
  775. }
  776. }
  777. $filters = array_unique($filters);
  778. $result = array();
  779. foreach ($filters as $filter) {
  780. $result[] = $this->_filters[$filter];
  781. }
  782. return $result;
  783. }
  784. /**
  785. * Remove an individual filter
  786. *
  787. * @param string $name
  788. * @return Zend_File_Transfer_Adapter_Abstract
  789. */
  790. public function removeFilter($name)
  791. {
  792. if (false === ($key = $this->_getFilterIdentifier($name))) {
  793. return $this;
  794. }
  795. unset($this->_filters[$key]);
  796. foreach (array_keys($this->_files) as $file) {
  797. if (empty($this->_files[$file]['filters'])) {
  798. continue;
  799. }
  800. $index = array_search($key, $this->_files[$file]['filters']);
  801. if ($index === false) {
  802. continue;
  803. }
  804. unset($this->_files[$file]['filters'][$index]);
  805. }
  806. return $this;
  807. }
  808. /**
  809. * Remove all filters
  810. *
  811. * @return Zend_File_Transfer_Adapter_Abstract
  812. */
  813. public function clearFilters()
  814. {
  815. $this->_filters = array();
  816. foreach (array_keys($this->_files) as $file) {
  817. $this->_files[$file]['filters'] = array();
  818. }
  819. return $this;
  820. }
  821. /**
  822. * Returns all set files
  823. *
  824. * @return array List of set files
  825. * @throws Zend_File_Transfer_Exception Not implemented
  826. */
  827. public function getFile()
  828. {
  829. require_once 'Zend/File/Transfer/Exception.php';
  830. throw new Zend_File_Transfer_Exception('Method not implemented');
  831. }
  832. /**
  833. * Retrieves the filename of transferred files.
  834. *
  835. * @param string $fileelement (Optional) Element to return the filename for
  836. * @param boolean $path (Optional) Should the path also be returned ?
  837. * @return string|array
  838. */
  839. public function getFileName($file = null, $path = true)
  840. {
  841. $files = $this->_getFiles($file, true, true);
  842. $result = array();
  843. $directory = "";
  844. foreach($files as $file) {
  845. if (empty($this->_files[$file]['name'])) {
  846. continue;
  847. }
  848. if ($path === true) {
  849. $directory = $this->getDestination($file) . DIRECTORY_SEPARATOR;
  850. }
  851. $result[$file] = $directory . $this->_files[$file]['name'];
  852. }
  853. if (count($result) == 1) {
  854. return current($result);
  855. }
  856. return $result;
  857. }
  858. /**
  859. * Retrieve additional internal file informations for files
  860. *
  861. * @param string $file (Optional) File to get informations for
  862. * @return array
  863. */
  864. public function getFileInfo($file = null)
  865. {
  866. return $this->_getFiles($file);
  867. }
  868. /**
  869. * Adds one or more files
  870. *
  871. * @param string|array $file File to add
  872. * @param string|array $validator Validators to use for this file, must be set before
  873. * @param string|array $filter Filters to use for this file, must be set before
  874. * @return Zend_File_Transfer_Adapter_Abstract
  875. * @throws Zend_File_Transfer_Exception Not implemented
  876. */
  877. public function addFile($file, $validator = null, $filter = null)
  878. {
  879. require_once 'Zend/File/Transfer/Exception.php';
  880. throw new Zend_File_Transfer_Exception('Method not implemented');
  881. }
  882. /**
  883. * Returns all set types
  884. *
  885. * @return array List of set types
  886. * @throws Zend_File_Transfer_Exception Not implemented
  887. */
  888. public function getType()
  889. {
  890. require_once 'Zend/File/Transfer/Exception.php';
  891. throw new Zend_File_Transfer_Exception('Method not implemented');
  892. }
  893. /**
  894. * Adds one or more type of files
  895. *
  896. * @param string|array $type Type of files to add
  897. * @param string|array $validator Validators to use for this file, must be set before
  898. * @param string|array $filter Filters to use for this file, must be set before
  899. * @return Zend_File_Transfer_Adapter_Abstract
  900. * @throws Zend_File_Transfer_Exception Not implemented
  901. */
  902. public function addType($type, $validator = null, $filter = null)
  903. {
  904. require_once 'Zend/File/Transfer/Exception.php';
  905. throw new Zend_File_Transfer_Exception('Method not implemented');
  906. }
  907. /**
  908. * Sets a new destination for the given files
  909. *
  910. * @deprecated Will be changed to be a filter!!!
  911. * @param string $destination New destination directory
  912. * @param string|array $files Files to set the new destination for
  913. * @return Zend_File_Transfer_Abstract
  914. * @throws Zend_File_Transfer_Exception when the given destination is not a directory or does not exist
  915. */
  916. public function setDestination($destination, $files = null)
  917. {
  918. $orig = $files;
  919. $destination = rtrim($destination, "/\\");
  920. if (!is_dir($destination)) {
  921. require_once 'Zend/File/Transfer/Exception.php';
  922. throw new Zend_File_Transfer_Exception('The given destination is not a directory or does not exist');
  923. }
  924. if (!is_writable($destination)) {
  925. require_once 'Zend/File/Transfer/Exception.php';
  926. throw new Zend_File_Transfer_Exception('The given destination is not writeable');
  927. }
  928. if ($files === null) {
  929. foreach ($this->_files as $file => $content) {
  930. $this->_files[$file]['destination'] = $destination;
  931. }
  932. } else {
  933. $files = $this->_getFiles($files, true, true);
  934. if (empty($files) and is_string($orig)) {
  935. $this->_files[$orig]['destination'] = $destination;
  936. }
  937. foreach ($files as $file) {
  938. $this->_files[$file]['destination'] = $destination;
  939. }
  940. }
  941. return $this;
  942. }
  943. /**
  944. * Retrieve destination directory value
  945. *
  946. * @param null|string|array $files
  947. * @return null|string|array
  948. */
  949. public function getDestination($files = null)
  950. {
  951. $orig = $files;
  952. $files = $this->_getFiles($files, false, true);
  953. $destinations = array();
  954. if (empty($files) and is_string($orig)) {
  955. if (isset($this->_files[$orig]['destination'])) {
  956. $destinations[$orig] = $this->_files[$orig]['destination'];
  957. } else {
  958. require_once 'Zend/File/Transfer/Exception.php';
  959. throw new Zend_File_Transfer_Exception(sprintf('The file transfer adapter can not find "%s"', $orig));
  960. }
  961. }
  962. foreach ($files as $key => $content) {
  963. if (isset($this->_files[$key]['destination'])) {
  964. $destinations[$key] = $this->_files[$key]['destination'];
  965. } else {
  966. $tmpdir = $this->_getTmpDir();
  967. $this->setDestination($tmpdir, $key);
  968. $destinations[$key] = $tmpdir;
  969. }
  970. }
  971. if (empty($destinations)) {
  972. $destinations = $this->_getTmpDir();
  973. } else if (count($destinations) == 1) {
  974. $destinations = current($destinations);
  975. }
  976. return $destinations;
  977. }
  978. /**
  979. * Set translator object for localization
  980. *
  981. * @param Zend_Translate|null $translator
  982. * @return Zend_File_Transfer_Abstract
  983. */
  984. public function setTranslator($translator = null)
  985. {
  986. if (null === $translator) {
  987. $this->_translator = null;
  988. } elseif ($translator instanceof Zend_Translate_Adapter) {
  989. $this->_translator = $translator;
  990. } elseif ($translator instanceof Zend_Translate) {
  991. $this->_translator = $translator->getAdapter();
  992. } else {
  993. require_once 'Zend/File/Transfer/Exception.php';
  994. throw new Zend_File_Transfer_Exception('Invalid translator specified');
  995. }
  996. return $this;
  997. }
  998. /**
  999. * Retrieve localization translator object
  1000. *
  1001. * @return Zend_Translate_Adapter|null
  1002. */
  1003. public function getTranslator()
  1004. {
  1005. if ($this->translatorIsDisabled()) {
  1006. return null;
  1007. }
  1008. return $this->_translator;
  1009. }
  1010. /**
  1011. * Indicate whether or not translation should be disabled
  1012. *
  1013. * @param bool $flag
  1014. * @return Zend_File_Transfer_Abstract
  1015. */
  1016. public function setDisableTranslator($flag)
  1017. {
  1018. $this->_translatorDisabled = (bool) $flag;
  1019. return $this;
  1020. }
  1021. /**
  1022. * Is translation disabled?
  1023. *
  1024. * @return bool
  1025. */
  1026. public function translatorIsDisabled()
  1027. {
  1028. return $this->_translatorDisabled;
  1029. }
  1030. /**
  1031. * Returns the hash for a given file
  1032. *
  1033. * @param string $hash Hash algorithm to use
  1034. * @param string|array $files Files to return the hash for
  1035. * @return string|array Hashstring
  1036. * @throws Zend_File_Transfer_Exception On unknown hash algorithm
  1037. */
  1038. public function getHash($hash = 'crc32', $files = null)
  1039. {
  1040. if (!in_array($hash, hash_algos())) {
  1041. require_once 'Zend/File/Transfer/Exception.php';
  1042. throw new Zend_File_Transfer_Exception('Unknown hash algorithm');
  1043. }
  1044. $files = $this->_getFiles($files);
  1045. $result = array();
  1046. foreach($files as $key => $value) {
  1047. if (file_exists($value['name'])) {
  1048. $result[$key] = hash_file($hash, $value['name']);
  1049. } else if (file_exists($value['tmp_name'])) {
  1050. $result[$key] = hash_file($hash, $value['tmp_name']);
  1051. } else if (empty($value['options']['ignoreNoFile'])) {
  1052. require_once 'Zend/File/Transfer/Exception.php';
  1053. throw new Zend_File_Transfer_Exception("The file '{$value['name']}' does not exist");
  1054. }
  1055. }
  1056. if (count($result) == 1) {
  1057. return current($result);
  1058. }
  1059. return $result;
  1060. }
  1061. /**
  1062. * Returns the real filesize of the file
  1063. *
  1064. * @param string|array $files Files to get the filesize from
  1065. * @throws Zend_File_Transfer_Exception When the file does not exist
  1066. * @return string|array Filesize
  1067. */
  1068. public function getFileSize($files = null)
  1069. {
  1070. $files = $this->_getFiles($files);
  1071. $result = array();
  1072. foreach($files as $key => $value) {
  1073. if (file_exists($value['name']) || file_exists($value['tmp_name'])) {
  1074. if ($value['options']['useByteString']) {
  1075. $result[$key] = self::_toByteString($value['size']);
  1076. } else {
  1077. $result[$key] = $value['size'];
  1078. }
  1079. } else if (empty($value['options']['ignoreNoFile'])) {
  1080. require_once 'Zend/File/Transfer/Exception.php';
  1081. throw new Zend_File_Transfer_Exception("The file '{$value['name']}' does not exist");
  1082. } else {
  1083. continue;
  1084. }
  1085. }
  1086. if (count($result) == 1) {
  1087. return current($result);
  1088. }
  1089. return $result;
  1090. }
  1091. /**
  1092. * Internal method to detect the size of a file
  1093. *
  1094. * @param array $value File infos
  1095. * @return string Filesize of given file
  1096. */
  1097. protected function _detectFileSize($value)
  1098. {
  1099. if (file_exists($value['name'])) {
  1100. $result = sprintf("%u", @filesize($value['name']));
  1101. } else if (file_exists($value['tmp_name'])) {
  1102. $result = sprintf("%u", @filesize($value['tmp_name']));
  1103. } else {
  1104. return null;
  1105. }
  1106. return $result;
  1107. }
  1108. /**
  1109. * Returns the real mimetype of the file
  1110. * Uses fileinfo, when not available mime_magic and as last fallback a manual given mimetype
  1111. *
  1112. * @param string|array $files Files to get the mimetype from
  1113. * @throws Zend_File_Transfer_Exception When the file does not exist
  1114. * @return string|array MimeType
  1115. */
  1116. public function getMimeType($files = null)
  1117. {
  1118. $files = $this->_getFiles($files);
  1119. $result = array();
  1120. foreach($files as $key => $value) {
  1121. if (file_exists($value['name']) || file_exists($value['tmp_name'])) {
  1122. $result[$key] = $value['type'];
  1123. } else if (empty($value['options']['ignoreNoFile'])) {
  1124. require_once 'Zend/File/Transfer/Exception.php';
  1125. throw new Zend_File_Transfer_Exception("The file '{$value['name']}' does not exist");
  1126. } else {
  1127. continue;
  1128. }
  1129. }
  1130. if (count($result) == 1) {
  1131. return current($result);
  1132. }
  1133. return $result;
  1134. }
  1135. /**
  1136. * Internal method to detect the mime type of a file
  1137. *
  1138. * @param array $value File infos
  1139. * @return string Mimetype of given file
  1140. */
  1141. protected function _detectMimeType($value)
  1142. {
  1143. if (file_exists($value['name'])) {
  1144. $file = $value['name'];
  1145. } else if (file_exists($value['tmp_name'])) {
  1146. $file = $value['tmp_name'];
  1147. } else {
  1148. return null;
  1149. }
  1150. if (class_exists('finfo', false)) {
  1151. $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
  1152. if (!empty($value['options']['magicFile'])) {
  1153. $mime = @finfo_open($const, $value['options']['magicFile']);
  1154. }
  1155. if (empty($mime)) {
  1156. $mime = @finfo_open($const);
  1157. }
  1158. if (!empty($mime)) {
  1159. $result = finfo_file($mime, $file);
  1160. }
  1161. unset($mime);
  1162. }
  1163. if (empty($result) && (function_exists('mime_content_type')
  1164. && ini_get('mime_magic.magicfile'))) {
  1165. $result = mime_content_type($file);
  1166. }
  1167. if (empty($result)) {
  1168. $result = 'application/octet-stream';
  1169. }
  1170. return $result;
  1171. }
  1172. /**
  1173. * Returns the formatted size
  1174. *
  1175. * @param integer $size
  1176. * @return string
  1177. */
  1178. protected static function _toByteString($size)
  1179. {
  1180. $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  1181. for ($i=0; $size >= 1024 && $i < 9; $i++) {
  1182. $size /= 1024;
  1183. }
  1184. return round($size, 2) . $sizes[$i];
  1185. }
  1186. /**
  1187. * Internal function to filter all given files
  1188. *
  1189. * @param string|array $files (Optional) Files to check
  1190. * @return boolean False on error
  1191. */
  1192. protected function _filter($files = null)
  1193. {
  1194. $check = $this->_getFiles($files);
  1195. foreach ($check as $name => $content) {
  1196. if (array_key_exists('filters', $content)) {
  1197. foreach ($content['filters'] as $class) {
  1198. $filter = $this->_filters[$class];
  1199. try {
  1200. $result = $filter->filter($this->getFileName($name));
  1201. $this->_files[$name]['destination'] = dirname($result);
  1202. $this->_files[$name]['name'] = basename($result);
  1203. } catch (Zend_Filter_Exception $e) {
  1204. $this->_messages += array($e->getMessage());
  1205. }
  1206. }
  1207. }
  1208. }
  1209. if (count($this->_messages) > 0) {
  1210. return false;
  1211. }
  1212. return true;
  1213. }
  1214. /**
  1215. * Determine system TMP directory and detect if we have read access
  1216. *
  1217. * @return string
  1218. * @throws Zend_File_Transfer_Exception if unable to determine directory
  1219. */
  1220. protected function _getTmpDir()
  1221. {
  1222. if (null === $this->_tmpDir) {
  1223. $tmpdir = array();
  1224. if (function_exists('sys_get_temp_dir')) {
  1225. $tmpdir[] = sys_get_temp_dir();
  1226. }
  1227. if (!empty($_ENV['TMP'])) {
  1228. $tmpdir[] = realpath($_ENV['TMP']);
  1229. }
  1230. if (!empty($_ENV['TMPDIR'])) {
  1231. $tmpdir[] = realpath($_ENV['TMPDIR']);
  1232. }
  1233. if (!empty($_ENV['TEMP'])) {
  1234. $tmpdir[] = realpath($_ENV['TEMP']);
  1235. }
  1236. $upload = ini_get('upload_tmp_dir');
  1237. if ($upload) {
  1238. $tmpdir[] = realpath($upload);
  1239. }
  1240. foreach($tmpdir as $directory) {
  1241. if ($this->_isPathWriteable($directory)) {
  1242. $this->_tmpDir = $directory;
  1243. }
  1244. }
  1245. if (empty($this->_tmpDir)) {
  1246. // Attemp to detect by creating a temporary file
  1247. $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
  1248. if ($tempFile) {
  1249. $this->_tmpDir = realpath(dirname($tempFile));
  1250. unlink($tempFile);
  1251. } else {
  1252. require_once 'Zend/File/Transfer/Exception.php';
  1253. throw new Zend_File_Transfer_Exception('Could not determine a temporary directory');
  1254. }
  1255. }
  1256. $this->_tmpDir = rtrim($this->_tmpDir, "/\\");
  1257. }
  1258. return $this->_tmpDir;
  1259. }
  1260. /**
  1261. * Tries to detect if we can read and write to the given path
  1262. *
  1263. * @param string $path
  1264. */
  1265. protected function _isPathWriteable($path)
  1266. {
  1267. $tempFile = rtrim($path, "/\\");
  1268. $tempFile .= '/' . 'test.1';
  1269. $result = @file_put_contents($tempFile, 'TEST');
  1270. if ($result == false) {
  1271. return false;
  1272. }
  1273. $result = @unlink($tempFile);
  1274. if ($result == false) {
  1275. return false;
  1276. }
  1277. return true;
  1278. }
  1279. /**
  1280. * Returns found files based on internal file array and given files
  1281. *
  1282. * @param string|array $files (Optional) Files to return
  1283. * @param boolean $names (Optional) Returns only names on true, else complete info
  1284. * @param boolean $noexception (Optional) Allows throwing an exception, otherwise returns an empty array
  1285. * @return array Found files
  1286. * @throws Zend_File_Transfer_Exception On false filename
  1287. */
  1288. protected function _getFiles($files, $names = false, $noexception = false)
  1289. {
  1290. $check = array();
  1291. if (is_string($files)) {
  1292. $files = array($files);
  1293. }
  1294. if (is_array($files)) {
  1295. foreach ($files as $find) {
  1296. $found = array();
  1297. foreach ($this->_files as $file => $content) {
  1298. if (!isset($content['name'])) {
  1299. continue;
  1300. }
  1301. if (($content['name'] === $find) && isset($content['multifiles'])) {
  1302. foreach ($content['multifiles'] as $multifile) {
  1303. $found[] = $multifile;
  1304. }
  1305. break;
  1306. }
  1307. if ($file === $find) {
  1308. $found[] = $file;
  1309. break;
  1310. }
  1311. if ($content['name'] === $find) {
  1312. $found[] = $file;
  1313. break;
  1314. }
  1315. }
  1316. if (empty($found)) {
  1317. if ($noexception !== false) {
  1318. return array();
  1319. }
  1320. require_once 'Zend/File/Transfer/Exception.php';
  1321. throw new Zend_File_Transfer_Exception(sprintf('The file transfer adapter can not find "%s"', $find));
  1322. }
  1323. foreach ($found as $checked) {
  1324. $check[$checked] = $this->_files[$checked];
  1325. }
  1326. }
  1327. }
  1328. if ($files === null) {
  1329. $check = $this->_files;
  1330. $keys = array_keys($check);
  1331. foreach ($keys as $key) {
  1332. if (isset($check[$key]['multifiles'])) {
  1333. unset($check[$key]);
  1334. }
  1335. }
  1336. }
  1337. if ($names) {
  1338. $check = array_keys($check);
  1339. }
  1340. return $check;
  1341. }
  1342. /**
  1343. * Retrieve internal identifier for a named validator
  1344. *
  1345. * @param string $name
  1346. * @return string
  1347. */
  1348. protected function _getValidatorIdentifier($name)
  1349. {
  1350. if (array_key_exists($name, $this->_validators)) {
  1351. return $name;
  1352. }
  1353. foreach (array_keys($this->_validators) as $test) {
  1354. if (preg_match('/' . preg_quote($name) . '$/i', $test)) {
  1355. return $test;
  1356. }
  1357. }
  1358. return false;
  1359. }
  1360. /**
  1361. * Retrieve internal identifier for a named filter
  1362. *
  1363. * @param string $name
  1364. * @return string
  1365. */
  1366. protected function _getFilterIdentifier($name)
  1367. {
  1368. if (array_key_exists($name, $this->_filters)) {
  1369. return $name;
  1370. }
  1371. foreach (array_keys($this->_filters) as $test) {
  1372. if (preg_match('/' . preg_quote($name) . '$/i', $test)) {
  1373. return $test;
  1374. }
  1375. }
  1376. return false;
  1377. }
  1378. }