Abstract.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  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 no 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('"%s" not found by file transfer adapter', $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("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("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("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 = new finfo($const, $value['options']['magicFile']);
  1154. } else {
  1155. $mime = new finfo($const);
  1156. }
  1157. if ($mime !== false) {
  1158. $result = $mime->file($file);
  1159. }
  1160. unset($mime);
  1161. }
  1162. if (empty($result) && (function_exists('mime_content_type')
  1163. && ini_get('mime_magic.magicfile'))) {
  1164. $result = mime_content_type($file);
  1165. }
  1166. if (empty($result)) {
  1167. $result = 'application/octet-stream';
  1168. }
  1169. return $result;
  1170. }
  1171. /**
  1172. * Returns the formatted size
  1173. *
  1174. * @param integer $size
  1175. * @return string
  1176. */
  1177. protected static function _toByteString($size)
  1178. {
  1179. $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  1180. for ($i=0; $size >= 1024 && $i < 9; $i++) {
  1181. $size /= 1024;
  1182. }
  1183. return round($size, 2) . $sizes[$i];
  1184. }
  1185. /**
  1186. * Internal function to filter all given files
  1187. *
  1188. * @param string|array $files (Optional) Files to check
  1189. * @return boolean False on error
  1190. */
  1191. protected function _filter($files = null)
  1192. {
  1193. $check = $this->_getFiles($files);
  1194. foreach ($check as $name => $content) {
  1195. if (array_key_exists('filters', $content)) {
  1196. foreach ($content['filters'] as $class) {
  1197. $filter = $this->_filters[$class];
  1198. try {
  1199. $result = $filter->filter($this->getFileName($name));
  1200. $this->_files[$name]['destination'] = dirname($result);
  1201. $this->_files[$name]['name'] = basename($result);
  1202. } catch (Zend_Filter_Exception $e) {
  1203. $this->_messages += array($e->getMessage());
  1204. }
  1205. }
  1206. }
  1207. }
  1208. if (count($this->_messages) > 0) {
  1209. return false;
  1210. }
  1211. return true;
  1212. }
  1213. /**
  1214. * Determine system TMP directory and detect if we have read access
  1215. *
  1216. * @return string
  1217. * @throws Zend_File_Transfer_Exception if unable to determine directory
  1218. */
  1219. protected function _getTmpDir()
  1220. {
  1221. if (null === $this->_tmpDir) {
  1222. $tmpdir = array();
  1223. if (function_exists('sys_get_temp_dir')) {
  1224. $tmpdir[] = sys_get_temp_dir();
  1225. }
  1226. if (!empty($_ENV['TMP'])) {
  1227. $tmpdir[] = realpath($_ENV['TMP']);
  1228. }
  1229. if (!empty($_ENV['TMPDIR'])) {
  1230. $tmpdir[] = realpath($_ENV['TMPDIR']);
  1231. }
  1232. if (!empty($_ENV['TEMP'])) {
  1233. $tmpdir[] = realpath($_ENV['TEMP']);
  1234. }
  1235. $upload = ini_get('upload_tmp_dir');
  1236. if ($upload) {
  1237. $tmpdir[] = realpath($upload);
  1238. }
  1239. foreach($tmpdir as $directory) {
  1240. if ($this->_isPathWriteable($directory)) {
  1241. $this->_tmpDir = $directory;
  1242. }
  1243. }
  1244. if (empty($this->_tmpDir)) {
  1245. // Attemp to detect by creating a temporary file
  1246. $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
  1247. if ($tempFile) {
  1248. $this->_tmpDir = realpath(dirname($tempFile));
  1249. unlink($tempFile);
  1250. } else {
  1251. require_once 'Zend/File/Transfer/Exception.php';
  1252. throw new Zend_File_Transfer_Exception('Could not determine temp directory');
  1253. }
  1254. }
  1255. $this->_tmpDir = rtrim($this->_tmpDir, "/\\");
  1256. }
  1257. return $this->_tmpDir;
  1258. }
  1259. /**
  1260. * Tries to detect if we can read and write to the given path
  1261. *
  1262. * @param string $path
  1263. */
  1264. protected function _isPathWriteable($path)
  1265. {
  1266. $tempFile = rtrim($path, "/\\");
  1267. $tempFile .= '/' . 'test.1';
  1268. $result = @file_put_contents($tempFile, 'TEST');
  1269. if ($result == false) {
  1270. return false;
  1271. }
  1272. $result = @unlink($tempFile);
  1273. if ($result == false) {
  1274. return false;
  1275. }
  1276. return true;
  1277. }
  1278. /**
  1279. * Returns found files based on internal file array and given files
  1280. *
  1281. * @param string|array $files (Optional) Files to return
  1282. * @param boolean $names (Optional) Returns only names on true, else complete info
  1283. * @param boolean $noexception (Optional) Allows throwing an exception, otherwise returns an empty array
  1284. * @return array Found files
  1285. * @throws Zend_File_Transfer_Exception On false filename
  1286. */
  1287. protected function _getFiles($files, $names = false, $noexception = false)
  1288. {
  1289. $check = array();
  1290. if (is_string($files)) {
  1291. $files = array($files);
  1292. }
  1293. if (is_array($files)) {
  1294. foreach ($files as $find) {
  1295. $found = array();
  1296. foreach ($this->_files as $file => $content) {
  1297. if (!isset($content['name'])) {
  1298. continue;
  1299. }
  1300. if (($content['name'] === $find) && isset($content['multifiles'])) {
  1301. foreach ($content['multifiles'] as $multifile) {
  1302. $found[] = $multifile;
  1303. }
  1304. break;
  1305. }
  1306. if ($file === $find) {
  1307. $found[] = $file;
  1308. break;
  1309. }
  1310. if ($content['name'] === $find) {
  1311. $found[] = $file;
  1312. break;
  1313. }
  1314. }
  1315. if (empty($found)) {
  1316. if ($noexception !== false) {
  1317. return array();
  1318. }
  1319. require_once 'Zend/File/Transfer/Exception.php';
  1320. throw new Zend_File_Transfer_Exception(sprintf('"%s" not found by file transfer adapter', $find));
  1321. }
  1322. foreach ($found as $checked) {
  1323. $check[$checked] = $this->_files[$checked];
  1324. }
  1325. }
  1326. }
  1327. if ($files === null) {
  1328. $check = $this->_files;
  1329. $keys = array_keys($check);
  1330. foreach ($keys as $key) {
  1331. if (isset($check[$key]['multifiles'])) {
  1332. unset($check[$key]);
  1333. }
  1334. }
  1335. }
  1336. if ($names) {
  1337. $check = array_keys($check);
  1338. }
  1339. return $check;
  1340. }
  1341. /**
  1342. * Retrieve internal identifier for a named validator
  1343. *
  1344. * @param string $name
  1345. * @return string
  1346. */
  1347. protected function _getValidatorIdentifier($name)
  1348. {
  1349. if (array_key_exists($name, $this->_validators)) {
  1350. return $name;
  1351. }
  1352. foreach (array_keys($this->_validators) as $test) {
  1353. if (preg_match('/' . preg_quote($name) . '$/i', $test)) {
  1354. return $test;
  1355. }
  1356. }
  1357. return false;
  1358. }
  1359. /**
  1360. * Retrieve internal identifier for a named filter
  1361. *
  1362. * @param string $name
  1363. * @return string
  1364. */
  1365. protected function _getFilterIdentifier($name)
  1366. {
  1367. if (array_key_exists($name, $this->_filters)) {
  1368. return $name;
  1369. }
  1370. foreach (array_keys($this->_filters) as $test) {
  1371. if (preg_match('/' . preg_quote($name) . '$/i', $test)) {
  1372. return $test;
  1373. }
  1374. }
  1375. return false;
  1376. }
  1377. }