Abstract.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  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. foreach ($options as $name => $value) {
  510. foreach ($file as $key => $content) {
  511. switch ($name) {
  512. case 'magicFile' :
  513. $this->_files[$key]['options'][$name] = (string) $value;
  514. break;
  515. case 'ignoreNoFile' :
  516. case 'useByteString' :
  517. $this->_files[$key]['options'][$name] = (boolean) $value;
  518. break;
  519. default:
  520. require_once 'Zend/File/Transfer/Exception.php';
  521. throw new Zend_File_Transfer_Exception("Unknown option: $name = $value");
  522. }
  523. }
  524. }
  525. }
  526. return $this;
  527. }
  528. /**
  529. * Returns set options for adapters or files
  530. *
  531. * @param array $files (Optional) Files to return the options for
  532. * @return array Options for given files
  533. */
  534. public function getOptions($files = null) {
  535. $file = $this->_getFiles($files, false, true);
  536. foreach ($file as $key => $content) {
  537. if (isset($this->_files[$key]['options'])) {
  538. $options[$key] = $this->_files[$key]['options'];
  539. } else {
  540. $options[$key] = array();
  541. }
  542. }
  543. return $options;
  544. }
  545. /**
  546. * Checks if the files are valid
  547. *
  548. * @param string|array $files (Optional) Files to check
  549. * @return boolean True if all checks are valid
  550. */
  551. public function isValid($files = null)
  552. {
  553. $check = $this->_getFiles($files, false, true);
  554. if (empty($check)) {
  555. return false;
  556. }
  557. $translator = $this->getTranslator();
  558. $this->_messages = array();
  559. $break = false;
  560. foreach($check as $key => $content) {
  561. if (array_key_exists('validators', $content) &&
  562. in_array('Zend_Validate_File_Count', $content['validators'])) {
  563. $validator = $this->_validators['Zend_Validate_File_Count'];
  564. if (array_key_exists('destination', $content)) {
  565. $checkit = $content['destination'];
  566. } else {
  567. $checkit = dirname($content['tmp_name']);
  568. }
  569. $checkit .= DIRECTORY_SEPARATOR . $content['name'];
  570. $validator->addFile($checkit);
  571. $count = $content;
  572. }
  573. }
  574. if (isset($count)) {
  575. if (!$validator->isValid($count['tmp_name'], $count)) {
  576. $this->_messages += $validator->getMessages();
  577. }
  578. }
  579. foreach ($check as $key => $content) {
  580. $fileerrors = array();
  581. if (array_key_exists('validators', $content) && $content['validated']) {
  582. continue;
  583. }
  584. if (array_key_exists('validators', $content)) {
  585. foreach ($content['validators'] as $class) {
  586. $validator = $this->_validators[$class];
  587. if (method_exists($validator, 'setTranslator')) {
  588. $validator->setTranslator($translator);
  589. }
  590. if (($class === 'Zend_Validate_File_Upload') and (empty($content['tmp_name']))) {
  591. $tocheck = $key;
  592. } else {
  593. $tocheck = $content['tmp_name'];
  594. }
  595. if (!$validator->isValid($tocheck, $content)) {
  596. $fileerrors += $validator->getMessages();
  597. }
  598. if (!empty($content['options']['ignoreNoFile']) and (isset($fileerrors['fileUploadErrorNoFile']))) {
  599. unset($fileerrors['fileUploadErrorNoFile']);
  600. break;
  601. }
  602. if (($class === 'Zend_Validate_File_Upload') and (count($fileerrors) > 0)) {
  603. break;
  604. }
  605. if (($this->_break[$class]) and (count($fileerrors) > 0)) {
  606. $break = true;
  607. break;
  608. }
  609. }
  610. }
  611. if (count($fileerrors) > 0) {
  612. $this->_files[$key]['validated'] = false;
  613. } else {
  614. $this->_files[$key]['validated'] = true;
  615. }
  616. $this->_messages += $fileerrors;
  617. if ($break) {
  618. break;
  619. }
  620. }
  621. if (count($this->_messages) > 0) {
  622. return false;
  623. }
  624. return true;
  625. }
  626. /**
  627. * Returns found validation messages
  628. *
  629. * @return array
  630. */
  631. public function getMessages()
  632. {
  633. return $this->_messages;
  634. }
  635. /**
  636. * Retrieve error codes
  637. *
  638. * @return array
  639. */
  640. public function getErrors()
  641. {
  642. return array_keys($this->_messages);
  643. }
  644. /**
  645. * Are there errors registered?
  646. *
  647. * @return boolean
  648. */
  649. public function hasErrors()
  650. {
  651. return (!empty($this->_messages));
  652. }
  653. /**
  654. * Adds a new filter for this class
  655. *
  656. * @param string|array $filter Type of filter to add
  657. * @param string|array $options Options to set for the filter
  658. * @param string|array $files Files to limit this filter to
  659. * @return Zend_File_Transfer_Adapter
  660. */
  661. public function addFilter($filter, $options = null, $files = null)
  662. {
  663. if ($filter instanceof Zend_Filter_Interface) {
  664. $class = get_class($filter);
  665. } elseif (is_string($filter)) {
  666. $class = $this->getPluginLoader(self::FILTER)->load($filter);
  667. $filter = new $class($options);
  668. } else {
  669. require_once 'Zend/File/Transfer/Exception.php';
  670. throw new Zend_File_Transfer_Exception('Invalid filter specified');
  671. }
  672. $this->_filters[$class] = $filter;
  673. $files = $this->_getFiles($files, true, true);
  674. foreach ($files as $file) {
  675. $this->_files[$file]['filters'][] = $class;
  676. }
  677. return $this;
  678. }
  679. /**
  680. * Add Multiple filters at once
  681. *
  682. * @param array $filters
  683. * @param string|array $files
  684. * @return Zend_File_Transfer_Adapter_Abstract
  685. */
  686. public function addFilters(array $filters, $files = null)
  687. {
  688. foreach ($filters as $key => $spec) {
  689. if ($spec instanceof Zend_Filter_Interface) {
  690. $this->addFilter($spec, null, $files);
  691. continue;
  692. }
  693. if (is_string($key)) {
  694. $this->addFilter($key, $spec, $files);
  695. continue;
  696. }
  697. if (is_int($key)) {
  698. if (is_string($spec)) {
  699. $this->addFilter($spec, null, $files);
  700. continue;
  701. }
  702. if (is_array($spec)) {
  703. if (!array_key_exists('filter', $spec)) {
  704. continue;
  705. }
  706. $filter = $spec['filter'];
  707. unset($spec['filter']);
  708. $this->addFilter($filter, $spec, $files);
  709. continue;
  710. }
  711. continue;
  712. }
  713. }
  714. return $this;
  715. }
  716. /**
  717. * Sets a filter for the class, erasing all previous set
  718. *
  719. * @param string|array $filter Filter to set
  720. * @param string|array $files Files to limit this filter to
  721. * @return Zend_File_Transfer_Adapter
  722. */
  723. public function setFilters(array $filters, $files = null)
  724. {
  725. $this->clearFilters();
  726. return $this->addFilters($filters, $files);
  727. }
  728. /**
  729. * Determine if a given filter has already been registered
  730. *
  731. * @param string $name
  732. * @return bool
  733. */
  734. public function hasFilter($name)
  735. {
  736. return (false !== $this->_getFilterIdentifier($name));
  737. }
  738. /**
  739. * Retrieve individual filter
  740. *
  741. * @param string $name
  742. * @return Zend_Filter_Interface|null
  743. */
  744. public function getFilter($name)
  745. {
  746. if (false === ($identifier = $this->_getFilterIdentifier($name))) {
  747. return null;
  748. }
  749. return $this->_filters[$identifier];
  750. }
  751. /**
  752. * Returns all set filters
  753. *
  754. * @param string|array $files (Optional) Returns the filter for this files
  755. * @return array List of set filters
  756. * @throws Zend_File_Transfer_Exception When file not found
  757. */
  758. public function getFilters($files = null)
  759. {
  760. if ($files === null) {
  761. return $this->_filters;
  762. }
  763. $files = $this->_getFiles($files, true, true);
  764. $filters = array();
  765. foreach ($files as $file) {
  766. if (!empty($this->_files[$file]['filters'])) {
  767. $filters += $this->_files[$file]['filters'];
  768. }
  769. }
  770. $filters = array_unique($filters);
  771. $result = array();
  772. foreach ($filters as $filter) {
  773. $result[] = $this->_filters[$filter];
  774. }
  775. return $result;
  776. }
  777. /**
  778. * Remove an individual filter
  779. *
  780. * @param string $name
  781. * @return Zend_File_Transfer_Adapter_Abstract
  782. */
  783. public function removeFilter($name)
  784. {
  785. if (false === ($key = $this->_getFilterIdentifier($name))) {
  786. return $this;
  787. }
  788. unset($this->_filters[$key]);
  789. foreach (array_keys($this->_files) as $file) {
  790. if (empty($this->_files[$file]['filters'])) {
  791. continue;
  792. }
  793. $index = array_search($key, $this->_files[$file]['filters']);
  794. if ($index === false) {
  795. continue;
  796. }
  797. unset($this->_files[$file]['filters'][$index]);
  798. }
  799. return $this;
  800. }
  801. /**
  802. * Remove all filters
  803. *
  804. * @return Zend_File_Transfer_Adapter_Abstract
  805. */
  806. public function clearFilters()
  807. {
  808. $this->_filters = array();
  809. foreach (array_keys($this->_files) as $file) {
  810. $this->_files[$file]['filters'] = array();
  811. }
  812. return $this;
  813. }
  814. /**
  815. * Returns all set files
  816. *
  817. * @return array List of set files
  818. * @throws Zend_File_Transfer_Exception Not implemented
  819. */
  820. public function getFile()
  821. {
  822. require_once 'Zend/File/Transfer/Exception.php';
  823. throw new Zend_File_Transfer_Exception('Method not implemented');
  824. }
  825. /**
  826. * Retrieves the filename of transferred files.
  827. *
  828. * @param string $fileelement (Optional) Element to return the filename for
  829. * @param boolean $path (Optional) Should the path also be returned ?
  830. * @return string|array
  831. */
  832. public function getFileName($file = null, $path = true)
  833. {
  834. $files = $this->_getFiles($file, true, true);
  835. $result = array();
  836. $directory = "";
  837. foreach($files as $file) {
  838. if (empty($this->_files[$file]['name'])) {
  839. continue;
  840. }
  841. if ($path === true) {
  842. $directory = $this->getDestination($file) . DIRECTORY_SEPARATOR;
  843. }
  844. $result[$file] = $directory . $this->_files[$file]['name'];
  845. }
  846. if (count($result) == 1) {
  847. return current($result);
  848. }
  849. return $result;
  850. }
  851. /**
  852. * Retrieve additional internal file informations for files
  853. *
  854. * @param string $file (Optional) File to get informations for
  855. * @return array
  856. */
  857. public function getFileInfo($file = null)
  858. {
  859. return $this->_getFiles($file);
  860. }
  861. /**
  862. * Adds one or more files
  863. *
  864. * @param string|array $file File to add
  865. * @param string|array $validator Validators to use for this file, must be set before
  866. * @param string|array $filter Filters to use for this file, must be set before
  867. * @return Zend_File_Transfer_Adapter_Abstract
  868. * @throws Zend_File_Transfer_Exception Not implemented
  869. */
  870. public function addFile($file, $validator = null, $filter = null)
  871. {
  872. require_once 'Zend/File/Transfer/Exception.php';
  873. throw new Zend_File_Transfer_Exception('Method not implemented');
  874. }
  875. /**
  876. * Returns all set types
  877. *
  878. * @return array List of set types
  879. * @throws Zend_File_Transfer_Exception Not implemented
  880. */
  881. public function getType()
  882. {
  883. require_once 'Zend/File/Transfer/Exception.php';
  884. throw new Zend_File_Transfer_Exception('Method not implemented');
  885. }
  886. /**
  887. * Adds one or more type of files
  888. *
  889. * @param string|array $type Type of files to add
  890. * @param string|array $validator Validators to use for this file, must be set before
  891. * @param string|array $filter Filters to use for this file, must be set before
  892. * @return Zend_File_Transfer_Adapter_Abstract
  893. * @throws Zend_File_Transfer_Exception Not implemented
  894. */
  895. public function addType($type, $validator = null, $filter = null)
  896. {
  897. require_once 'Zend/File/Transfer/Exception.php';
  898. throw new Zend_File_Transfer_Exception('Method not implemented');
  899. }
  900. /**
  901. * Sets a new destination for the given files
  902. *
  903. * @deprecated Will be changed to be a filter!!!
  904. * @param string $destination New destination directory
  905. * @param string|array $files Files to set the new destination for
  906. * @return Zend_File_Transfer_Abstract
  907. * @throws Zend_File_Transfer_Exception when the given destination is not a directory or does not exist
  908. */
  909. public function setDestination($destination, $files = null)
  910. {
  911. $orig = $files;
  912. $destination = rtrim($destination, "/\\");
  913. if (!is_dir($destination)) {
  914. require_once 'Zend/File/Transfer/Exception.php';
  915. throw new Zend_File_Transfer_Exception('The given destination is no directory or does not exist');
  916. }
  917. if (!is_writable($destination)) {
  918. require_once 'Zend/File/Transfer/Exception.php';
  919. throw new Zend_File_Transfer_Exception('The given destination is not writeable');
  920. }
  921. if ($files === null) {
  922. foreach ($this->_files as $file => $content) {
  923. $this->_files[$file]['destination'] = $destination;
  924. }
  925. } else {
  926. $files = $this->_getFiles($files, true, true);
  927. if (empty($files) and is_string($orig)) {
  928. $this->_files[$orig]['destination'] = $destination;
  929. }
  930. foreach ($files as $file) {
  931. $this->_files[$file]['destination'] = $destination;
  932. }
  933. }
  934. return $this;
  935. }
  936. /**
  937. * Retrieve destination directory value
  938. *
  939. * @param null|string|array $files
  940. * @return null|string|array
  941. */
  942. public function getDestination($files = null)
  943. {
  944. $orig = $files;
  945. $files = $this->_getFiles($files, false, true);
  946. $destinations = array();
  947. if (empty($files) and is_string($orig)) {
  948. if (isset($this->_files[$orig]['destination'])) {
  949. $destinations[$orig] = $this->_files[$orig]['destination'];
  950. } else {
  951. require_once 'Zend/File/Transfer/Exception.php';
  952. throw new Zend_File_Transfer_Exception(sprintf('"%s" not found by file transfer adapter', $orig));
  953. }
  954. }
  955. foreach ($files as $key => $content) {
  956. if (isset($this->_files[$key]['destination'])) {
  957. $destinations[$key] = $this->_files[$key]['destination'];
  958. } else {
  959. $tmpdir = $this->_getTmpDir();
  960. $this->setDestination($tmpdir, $key);
  961. $destinations[$key] = $tmpdir;
  962. }
  963. }
  964. if (empty($destinations)) {
  965. $destinations = $this->_getTmpDir();
  966. } else if (count($destinations) == 1) {
  967. $destinations = current($destinations);
  968. }
  969. return $destinations;
  970. }
  971. /**
  972. * Set translator object for localization
  973. *
  974. * @param Zend_Translate|null $translator
  975. * @return Zend_File_Transfer_Abstract
  976. */
  977. public function setTranslator($translator = null)
  978. {
  979. if (null === $translator) {
  980. $this->_translator = null;
  981. } elseif ($translator instanceof Zend_Translate_Adapter) {
  982. $this->_translator = $translator;
  983. } elseif ($translator instanceof Zend_Translate) {
  984. $this->_translator = $translator->getAdapter();
  985. } else {
  986. require_once 'Zend/File/Transfer/Exception.php';
  987. throw new Zend_File_Transfer_Exception('Invalid translator specified');
  988. }
  989. return $this;
  990. }
  991. /**
  992. * Retrieve localization translator object
  993. *
  994. * @return Zend_Translate_Adapter|null
  995. */
  996. public function getTranslator()
  997. {
  998. if ($this->translatorIsDisabled()) {
  999. return null;
  1000. }
  1001. return $this->_translator;
  1002. }
  1003. /**
  1004. * Indicate whether or not translation should be disabled
  1005. *
  1006. * @param bool $flag
  1007. * @return Zend_File_Transfer_Abstract
  1008. */
  1009. public function setDisableTranslator($flag)
  1010. {
  1011. $this->_translatorDisabled = (bool) $flag;
  1012. return $this;
  1013. }
  1014. /**
  1015. * Is translation disabled?
  1016. *
  1017. * @return bool
  1018. */
  1019. public function translatorIsDisabled()
  1020. {
  1021. return $this->_translatorDisabled;
  1022. }
  1023. /**
  1024. * Returns the hash for a given file
  1025. *
  1026. * @param string $hash Hash algorithm to use
  1027. * @param string|array $files Files to return the hash for
  1028. * @return string|array Hashstring
  1029. * @throws Zend_File_Transfer_Exception On unknown hash algorithm
  1030. */
  1031. public function getHash($hash = 'crc32', $files = null)
  1032. {
  1033. if (!in_array($hash, hash_algos())) {
  1034. require_once 'Zend/File/Transfer/Exception.php';
  1035. throw new Zend_File_Transfer_Exception('Unknown hash algorithm');
  1036. }
  1037. $files = $this->_getFiles($files);
  1038. $result = array();
  1039. foreach($files as $key => $value) {
  1040. if (file_exists($value['name'])) {
  1041. $result[$key] = hash_file($hash, $value['name']);
  1042. } else if (file_exists($value['tmp_name'])) {
  1043. $result[$key] = hash_file($hash, $value['tmp_name']);
  1044. } else if (empty($value['options']['ignoreNoFile'])) {
  1045. require_once 'Zend/File/Transfer/Exception.php';
  1046. throw new Zend_File_Transfer_Exception("File '{$value['name']}' does not exist");
  1047. }
  1048. }
  1049. if (count($result) == 1) {
  1050. return current($result);
  1051. }
  1052. return $result;
  1053. }
  1054. /**
  1055. * Returns the real filesize of the file
  1056. *
  1057. * @param string|array $files Files to get the filesize from
  1058. * @throws Zend_File_Transfer_Exception When the file does not exist
  1059. * @return string|array Filesize
  1060. */
  1061. public function getFileSize($files = null)
  1062. {
  1063. $files = $this->_getFiles($files);
  1064. $result = array();
  1065. foreach($files as $key => $value) {
  1066. if (file_exists($value['name']) || file_exists($value['tmp_name'])) {
  1067. if ($value['options']['useByteString']) {
  1068. $result[$key] = self::_toByteString($value['size']);
  1069. } else {
  1070. $result[$key] = $value['size'];
  1071. }
  1072. } else if (empty($value['options']['ignoreNoFile'])) {
  1073. require_once 'Zend/File/Transfer/Exception.php';
  1074. throw new Zend_File_Transfer_Exception("File '{$value['name']}' does not exist");
  1075. } else {
  1076. continue;
  1077. }
  1078. }
  1079. if (count($result) == 1) {
  1080. return current($result);
  1081. }
  1082. return $result;
  1083. }
  1084. /**
  1085. * Internal method to detect the size of a file
  1086. *
  1087. * @param array $value File infos
  1088. * @return string Filesize of given file
  1089. */
  1090. protected function _detectFileSize($value)
  1091. {
  1092. if (file_exists($value['name'])) {
  1093. $result = sprintf("%u", @filesize($value['name']));
  1094. } else if (file_exists($value['tmp_name'])) {
  1095. $result = sprintf("%u", @filesize($value['tmp_name']));
  1096. } else {
  1097. return null;
  1098. }
  1099. return $result;
  1100. }
  1101. /**
  1102. * Returns the real mimetype of the file
  1103. * Uses fileinfo, when not available mime_magic and as last fallback a manual given mimetype
  1104. *
  1105. * @param string|array $files Files to get the mimetype from
  1106. * @throws Zend_File_Transfer_Exception When the file does not exist
  1107. * @return string|array MimeType
  1108. */
  1109. public function getMimeType($files = null)
  1110. {
  1111. $files = $this->_getFiles($files);
  1112. $result = array();
  1113. foreach($files as $key => $value) {
  1114. if (file_exists($value['name']) || file_exists($value['tmp_name'])) {
  1115. $result[$key] = $value['type'];
  1116. } else if (empty($value['options']['ignoreNoFile'])) {
  1117. require_once 'Zend/File/Transfer/Exception.php';
  1118. throw new Zend_File_Transfer_Exception("File '{$value['name']}' does not exist");
  1119. } else {
  1120. continue;
  1121. }
  1122. }
  1123. if (count($result) == 1) {
  1124. return current($result);
  1125. }
  1126. return $result;
  1127. }
  1128. /**
  1129. * Internal method to detect the mime type of a file
  1130. *
  1131. * @param array $value File infos
  1132. * @return string Mimetype of given file
  1133. */
  1134. protected function _detectMimeType($value)
  1135. {
  1136. if (file_exists($value['name'])) {
  1137. $file = $value['name'];
  1138. } else if (file_exists($value['tmp_name'])) {
  1139. $file = $value['tmp_name'];
  1140. } else {
  1141. return null;
  1142. }
  1143. if (class_exists('finfo', false)) {
  1144. $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
  1145. if (!empty($magicFile)) {
  1146. $mime = new finfo($const, $magicFile);
  1147. } else {
  1148. $mime = new finfo($const);
  1149. }
  1150. if ($mime !== false) {
  1151. $result = $mime->file($file);
  1152. }
  1153. unset($mime);
  1154. }
  1155. if (empty($result) && (function_exists('mime_content_type')
  1156. && ini_get('mime_magic.magicfile'))) {
  1157. $result = mime_content_type($file);
  1158. }
  1159. if (empty($result)) {
  1160. $result = 'application/octet-stream';
  1161. }
  1162. return $result;
  1163. }
  1164. /**
  1165. * Returns the formatted size
  1166. *
  1167. * @param integer $size
  1168. * @return string
  1169. */
  1170. protected static function _toByteString($size)
  1171. {
  1172. $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  1173. for ($i=0; $size >= 1024 && $i < 9; $i++) {
  1174. $size /= 1024;
  1175. }
  1176. return round($size, 2) . $sizes[$i];
  1177. }
  1178. /**
  1179. * Internal function to filter all given files
  1180. *
  1181. * @param string|array $files (Optional) Files to check
  1182. * @return boolean False on error
  1183. */
  1184. protected function _filter($files = null)
  1185. {
  1186. $check = $this->_getFiles($files);
  1187. foreach ($check as $name => $content) {
  1188. if (array_key_exists('filters', $content)) {
  1189. foreach ($content['filters'] as $class) {
  1190. $filter = $this->_filters[$class];
  1191. try {
  1192. $result = $filter->filter($this->getFileName($name));
  1193. $this->_files[$name]['destination'] = dirname($result);
  1194. $this->_files[$name]['name'] = basename($result);
  1195. } catch (Zend_Filter_Exception $e) {
  1196. $this->_messages += array($e->getMessage());
  1197. }
  1198. }
  1199. }
  1200. }
  1201. if (count($this->_messages) > 0) {
  1202. return false;
  1203. }
  1204. return true;
  1205. }
  1206. /**
  1207. * Determine system TMP directory and detect if we have read access
  1208. *
  1209. * @return string
  1210. * @throws Zend_File_Transfer_Exception if unable to determine directory
  1211. */
  1212. protected function _getTmpDir()
  1213. {
  1214. if (null === $this->_tmpDir) {
  1215. $tmpdir = array();
  1216. if (function_exists('sys_get_temp_dir')) {
  1217. $tmpdir[] = sys_get_temp_dir();
  1218. }
  1219. if (!empty($_ENV['TMP'])) {
  1220. $tmpdir[] = realpath($_ENV['TMP']);
  1221. }
  1222. if (!empty($_ENV['TMPDIR'])) {
  1223. $tmpdir[] = realpath($_ENV['TMPDIR']);
  1224. }
  1225. if (!empty($_ENV['TEMP'])) {
  1226. $tmpdir[] = realpath($_ENV['TEMP']);
  1227. }
  1228. $upload = ini_get('upload_tmp_dir');
  1229. if ($upload) {
  1230. $tmpdir[] = realpath($upload);
  1231. }
  1232. foreach($tmpdir as $directory) {
  1233. if ($this->_isPathWriteable($directory)) {
  1234. $this->_tmpDir = $directory;
  1235. }
  1236. }
  1237. if (empty($this->_tmpDir)) {
  1238. // Attemp to detect by creating a temporary file
  1239. $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
  1240. if ($tempFile) {
  1241. $this->_tmpDir = realpath(dirname($tempFile));
  1242. unlink($tempFile);
  1243. } else {
  1244. require_once 'Zend/File/Transfer/Exception.php';
  1245. throw new Zend_File_Transfer_Exception('Could not determine temp directory');
  1246. }
  1247. }
  1248. $this->_tmpDir = rtrim($this->_tmpDir, "/\\");
  1249. }
  1250. return $this->_tmpDir;
  1251. }
  1252. /**
  1253. * Tries to detect if we can read and write to the given path
  1254. *
  1255. * @param string $path
  1256. */
  1257. protected function _isPathWriteable($path)
  1258. {
  1259. $tempFile = rtrim($path, "/\\");
  1260. $tempFile .= '/' . 'test.1';
  1261. $result = @file_put_contents($tempFile, 'TEST');
  1262. if ($result == false) {
  1263. return false;
  1264. }
  1265. $result = @unlink($tempFile);
  1266. if ($result == false) {
  1267. return false;
  1268. }
  1269. return true;
  1270. }
  1271. /**
  1272. * Returns found files based on internal file array and given files
  1273. *
  1274. * @param string|array $files (Optional) Files to return
  1275. * @param boolean $names (Optional) Returns only names on true, else complete info
  1276. * @param boolean $noexception (Optional) Allows throwing an exception, otherwise returns an empty array
  1277. * @return array Found files
  1278. * @throws Zend_File_Transfer_Exception On false filename
  1279. */
  1280. protected function _getFiles($files, $names = false, $noexception = false)
  1281. {
  1282. $check = array();
  1283. if (is_string($files)) {
  1284. $files = array($files);
  1285. }
  1286. if (is_array($files)) {
  1287. foreach ($files as $find) {
  1288. $found = array();
  1289. foreach ($this->_files as $file => $content) {
  1290. if (!isset($content['name'])) {
  1291. continue;
  1292. }
  1293. if (($content['name'] === $find) && isset($content['multifiles'])) {
  1294. foreach ($content['multifiles'] as $multifile) {
  1295. $found[] = $multifile;
  1296. }
  1297. break;
  1298. }
  1299. if ($file === $find) {
  1300. $found[] = $file;
  1301. break;
  1302. }
  1303. if ($content['name'] === $find) {
  1304. $found[] = $file;
  1305. break;
  1306. }
  1307. }
  1308. if (empty($found)) {
  1309. if ($noexception !== false) {
  1310. return array();
  1311. }
  1312. require_once 'Zend/File/Transfer/Exception.php';
  1313. throw new Zend_File_Transfer_Exception(sprintf('"%s" not found by file transfer adapter', $find));
  1314. }
  1315. foreach ($found as $checked) {
  1316. $check[$checked] = $this->_files[$checked];
  1317. }
  1318. }
  1319. }
  1320. if ($files === null) {
  1321. $check = $this->_files;
  1322. $keys = array_keys($check);
  1323. foreach ($keys as $key) {
  1324. if (isset($check[$key]['multifiles'])) {
  1325. unset($check[$key]);
  1326. }
  1327. }
  1328. }
  1329. if ($names) {
  1330. $check = array_keys($check);
  1331. }
  1332. return $check;
  1333. }
  1334. /**
  1335. * Retrieve internal identifier for a named validator
  1336. *
  1337. * @param string $name
  1338. * @return string
  1339. */
  1340. protected function _getValidatorIdentifier($name)
  1341. {
  1342. if (array_key_exists($name, $this->_validators)) {
  1343. return $name;
  1344. }
  1345. foreach (array_keys($this->_validators) as $test) {
  1346. if (preg_match('/' . preg_quote($name) . '$/i', $test)) {
  1347. return $test;
  1348. }
  1349. }
  1350. return false;
  1351. }
  1352. /**
  1353. * Retrieve internal identifier for a named filter
  1354. *
  1355. * @param string $name
  1356. * @return string
  1357. */
  1358. protected function _getFilterIdentifier($name)
  1359. {
  1360. if (array_key_exists($name, $this->_filters)) {
  1361. return $name;
  1362. }
  1363. foreach (array_keys($this->_filters) as $test) {
  1364. if (preg_match('/' . preg_quote($name) . '$/i', $test)) {
  1365. return $test;
  1366. }
  1367. }
  1368. return false;
  1369. }
  1370. }