PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/File/Transfer/Adapter/Abstract.php

http://digitalus-site-manager.googlecode.com/
PHP | 980 lines | 551 code | 96 blank | 333 comment | 94 complexity | 36f55e88c9161ebe95c8ad2dcbfe72a5 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  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-2008 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. /** Zend_Validate_Interface */
  22. require_once 'Zend/Validate/Interface.php';
  23. /**
  24. * Abstract class for file transfers (Downloads and Uploads)
  25. *
  26. * @category Zend
  27. * @package Zend_File_Transfer
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. abstract class Zend_File_Transfer_Adapter_Abstract implements Zend_Validate_Interface
  32. {
  33. /**@+
  34. * @const string Plugin loader Constants
  35. */
  36. const FILTER = 'FILTER';
  37. const VALIDATE = 'VALIDATE';
  38. /**@-*/
  39. /**
  40. * Plugin loaders for filter and validation chains
  41. * @var array
  42. */
  43. protected $_loaders = array();
  44. /**
  45. * Internal list of validators
  46. * @var array
  47. */
  48. protected $_validators = array();
  49. /**
  50. * Internal list of messages
  51. * @var array
  52. */
  53. protected $_messages = array();
  54. /**
  55. * Internal list of filters
  56. * @var array
  57. */
  58. protected $_filters = array();
  59. /**
  60. * Internal list of files
  61. * This array looks like this:
  62. * array(form => array( - Form is the name within the form or, if not set the filename
  63. * name, - Original name of this file
  64. * type, - Mime type of this file
  65. * size, - Filesize in bytes
  66. * tmp_name, - Internalally temporary filename for uploaded files
  67. * error, - Error which has occured
  68. * destination, - New destination for this file
  69. * validators, - Set validator names for this file
  70. * files - Set file names for this file
  71. * ))
  72. *
  73. * @var array
  74. */
  75. protected $_files = array();
  76. /**
  77. * TMP directory
  78. * @var string
  79. */
  80. protected $_tmpDir;
  81. /**
  82. * Send file
  83. *
  84. * @param mixed $options
  85. * @return bool
  86. */
  87. abstract public function send($options = null);
  88. /**
  89. * Receive file
  90. *
  91. * @param mixed $options
  92. * @return bool
  93. */
  94. abstract public function receive($options = null);
  95. /**
  96. * Is file sent?
  97. *
  98. * @param array|string|null $file
  99. * @return bool
  100. */
  101. abstract public function isSent($file = null);
  102. /**
  103. * Is file received?
  104. *
  105. * @param array|string|null $file
  106. * @return bool
  107. */
  108. abstract public function isReceived($file = null);
  109. /**
  110. * Retrieve progress of transfer
  111. *
  112. * @return float
  113. */
  114. abstract public function getProgress();
  115. /**
  116. * Set plugin loader to use for validator or filter chain
  117. *
  118. * @param Zend_Loader_PluginLoader_Interface $loader
  119. * @param string $type 'filter', or 'validate'
  120. * @return Zend_File_Transfer_Adapter_Abstract
  121. * @throws Zend_File_Transfer_Exception on invalid type
  122. */
  123. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type)
  124. {
  125. $type = strtoupper($type);
  126. switch ($type) {
  127. case self::FILTER:
  128. case self::VALIDATE:
  129. $this->_loaders[$type] = $loader;
  130. return $this;
  131. default:
  132. require_once 'Zend/File/Transfer/Exception.php';
  133. throw new Zend_File_Transfer_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
  134. }
  135. }
  136. /**
  137. * Retrieve plugin loader for validator or filter chain
  138. *
  139. * Instantiates with default rules if none available for that type. Use
  140. * 'filter' or 'validate' for $type.
  141. *
  142. * @param string $type
  143. * @return Zend_Loader_PluginLoader
  144. * @throws Zend_File_Transfer_Exception on invalid type.
  145. */
  146. public function getPluginLoader($type)
  147. {
  148. $type = strtoupper($type);
  149. switch ($type) {
  150. case self::FILTER:
  151. case self::VALIDATE:
  152. $prefixSegment = ucfirst(strtolower($type));
  153. $pathSegment = $prefixSegment;
  154. if (!isset($this->_loaders[$type])) {
  155. $paths = array(
  156. 'Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/',
  157. 'Zend_' . $prefixSegment . '_File' => 'Zend/' . $pathSegment . '/File',
  158. );
  159. require_once 'Zend/Loader/PluginLoader.php';
  160. $this->_loaders[$type] = new Zend_Loader_PluginLoader($paths);
  161. }
  162. return $this->_loaders[$type];
  163. default:
  164. require_once 'Zend/File/Transfer/Exception.php';
  165. throw new Zend_File_Transfer_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  166. }
  167. }
  168. /**
  169. * Add prefix path for plugin loader
  170. *
  171. * If no $type specified, assumes it is a base path for both filters and
  172. * validators, and sets each according to the following rules:
  173. * - filters: $prefix = $prefix . '_Filter'
  174. * - validators: $prefix = $prefix . '_Validate'
  175. *
  176. * Otherwise, the path prefix is set on the appropriate plugin loader.
  177. *
  178. * @param string $path
  179. * @return Zend_File_Transfer_Adapter_Abstract
  180. * @throws Zend_File_Transfer_Exception for invalid type
  181. */
  182. public function addPrefixPath($prefix, $path, $type = null)
  183. {
  184. $type = strtoupper($type);
  185. switch ($type) {
  186. case self::FILTER:
  187. case self::VALIDATE:
  188. $loader = $this->getPluginLoader($type);
  189. $loader->addPrefixPath($prefix, $path);
  190. return $this;
  191. case null:
  192. $prefix = rtrim($prefix, '_');
  193. $path = rtrim($path, DIRECTORY_SEPARATOR);
  194. foreach (array(self::FILTER, self::VALIDATE) as $type) {
  195. $cType = ucfirst(strtolower($type));
  196. $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR;
  197. $pluginPrefix = $prefix . '_' . $cType;
  198. $loader = $this->getPluginLoader($type);
  199. $loader->addPrefixPath($pluginPrefix, $pluginPath);
  200. }
  201. return $this;
  202. default:
  203. require_once 'Zend/File/Transfer/Exception.php';
  204. throw new Zend_File_Transfer_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  205. }
  206. }
  207. /**
  208. * Add many prefix paths at once
  209. *
  210. * @param array $spec
  211. * @return Zend_File_Transfer_Exception
  212. */
  213. public function addPrefixPaths(array $spec)
  214. {
  215. if (isset($spec['prefix']) && isset($spec['path'])) {
  216. return $this->addPrefixPath($spec['prefix'], $spec['path']);
  217. }
  218. foreach ($spec as $type => $paths) {
  219. if (is_numeric($type) && is_array($paths)) {
  220. $type = null;
  221. if (isset($paths['prefix']) && isset($paths['path'])) {
  222. if (isset($paths['type'])) {
  223. $type = $paths['type'];
  224. }
  225. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  226. }
  227. } elseif (!is_numeric($type)) {
  228. if (!isset($paths['prefix']) || !isset($paths['path'])) {
  229. foreach ($paths as $prefix => $spec) {
  230. if (is_array($spec)) {
  231. foreach ($spec as $path) {
  232. if (!is_string($path)) {
  233. continue;
  234. }
  235. $this->addPrefixPath($prefix, $path, $type);
  236. }
  237. } elseif (is_string($spec)) {
  238. $this->addPrefixPath($prefix, $spec, $type);
  239. }
  240. }
  241. } else {
  242. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  243. }
  244. }
  245. }
  246. return $this;
  247. }
  248. /**
  249. * Adds a new validator for this class
  250. *
  251. * @param string|array $validator Type of validator to add
  252. * @param string|array $options Options to set for the validator
  253. * @param string|array $files Files to limit this validator to
  254. * @return Zend_File_Transfer_Adapter
  255. */
  256. public function addValidator($validator, $options = null, $files = null)
  257. {
  258. if ($validator instanceof Zend_Validate_Interface) {
  259. $class = get_class($validator);
  260. } elseif (is_string($validator)) {
  261. $class = $this->getPluginLoader(self::VALIDATE)->load($validator);
  262. $validator = new $class($options);
  263. } else {
  264. require_once 'Zend/File/Transfer/Exception.php';
  265. throw new Zend_File_Transfer_Exception('Invalid validator specified');
  266. }
  267. $this->_validators[$class] = $validator;
  268. if ($files === null) {
  269. $files = array_keys($this->_files);
  270. } else {
  271. if (!is_array($files)) {
  272. $files = array($files);
  273. }
  274. }
  275. foreach ($files as $key => $file) {
  276. if (!is_string($file)) {
  277. if (is_array($file) && !is_numeric($key)) {
  278. $file = $key;
  279. } else {
  280. continue;
  281. }
  282. }
  283. if (!array_key_exists($file, $this->_files)) {
  284. continue;
  285. }
  286. $this->_files[$file]['validators'][] = $class;
  287. }
  288. return $this;
  289. }
  290. /**
  291. * Add Multiple validators at once
  292. *
  293. * @param array $validators
  294. * @param string|array $files
  295. * @return Zend_File_Transfer_Adapter_Abstract
  296. */
  297. public function addValidators(array $validators, $files = null)
  298. {
  299. foreach ($validators as $key => $spec) {
  300. if ($spec instanceof Zend_Validate_Interface) {
  301. $this->addValidator($spec, null, $files);
  302. continue;
  303. }
  304. if (is_string($key)) {
  305. $this->addValidator($key, $spec, $files);
  306. continue;
  307. }
  308. if (is_int($key)) {
  309. if (is_string($spec)) {
  310. $this->addValidator($spec, null, $files);
  311. continue;
  312. }
  313. if (is_array($spec)) {
  314. if (!array_key_exists('validator', $spec)) {
  315. continue;
  316. }
  317. $validator = $spec['validator'];
  318. unset($spec['validator']);
  319. $this->addValidator($validator, $spec, $files);
  320. continue;
  321. }
  322. continue;
  323. }
  324. }
  325. return $this;
  326. }
  327. /**
  328. * Sets a validator for the class, erasing all previous set
  329. *
  330. * @param string|array $validator Validator to set
  331. * @param string|array $files Files to limit this validator to
  332. * @return Zend_File_Transfer_Adapter
  333. */
  334. public function setValidators(array $validators, $files = null)
  335. {
  336. $this->clearValidators();
  337. return $this->addValidators($validators, $files);
  338. }
  339. /**
  340. * Determine if a given validator has already been registered
  341. *
  342. * @param string $name
  343. * @return bool
  344. */
  345. public function hasValidator($name)
  346. {
  347. return (false !== $this->_getValidatorIdentifier($name));
  348. }
  349. /**
  350. * Retrieve individual validator
  351. *
  352. * @param string $name
  353. * @return Zend_Validate_Interface|null
  354. */
  355. public function getValidator($name)
  356. {
  357. if (false === ($identifier = $this->_getValidatorIdentifier($name))) {
  358. return null;
  359. }
  360. return $this->_validators[$identifier];
  361. }
  362. /**
  363. * Returns all set validators
  364. *
  365. * @param string|array $files (Optional) Returns the validator for this files
  366. * @return null|array List of set validators
  367. * @throws Zend_File_Transfer_Exception When file not found
  368. */
  369. public function getValidators($files = null)
  370. {
  371. if ($files === null) {
  372. return $this->_validators;
  373. }
  374. if (!is_array($files)) {
  375. $files = array($files);
  376. }
  377. $validators = array();
  378. foreach ($files as $file) {
  379. if (!isset($this->_files[$file])) {
  380. require_once 'Zend/File/Transfer/Exception.php';
  381. throw new Zend_File_Transfer_Exception('Unknown file');
  382. }
  383. $validators += $this->_files[$file]['validators'];
  384. }
  385. $validators = array_unique($validators);
  386. foreach ($validators as $validator) {
  387. $result[] = $this->_validators[$validator];
  388. }
  389. return $result;
  390. }
  391. /**
  392. * Remove an individual validator
  393. *
  394. * @param string $name
  395. * @return Zend_File_Transfer_Adapter_Abstract
  396. */
  397. public function removeValidator($name)
  398. {
  399. if (false === ($key = $this->_getValidatorIdentifier($name))) {
  400. return $this;
  401. }
  402. unset($this->_validators[$key]);
  403. foreach (array_keys($this->_files) as $file) {
  404. if (!$index = array_search($key, $this->_files[$file]['validators'])) {
  405. continue;
  406. }
  407. unset($this->_files[$file]['validators'][$index]);
  408. }
  409. return $this;
  410. }
  411. /**
  412. * Remove all validators
  413. *
  414. * @return Zend_File_Transfer_Adapter_Abstract
  415. */
  416. public function clearValidators()
  417. {
  418. $this->_validators = array();
  419. foreach (array_keys($this->_files) as $file) {
  420. $this->_files[$file]['validators'] = array();
  421. }
  422. return $this;
  423. }
  424. /**
  425. * Checks if the files are valid
  426. *
  427. * @param string|array $files Files to check
  428. * @return boolean True if all checks are valid
  429. */
  430. public function isValid($files)
  431. {
  432. $check = $this->_getFiles($files);
  433. $this->_messages = array();
  434. foreach ($check as $file => $content) {
  435. $uploaderror = false;
  436. if (array_key_exists('validators', $content)) {
  437. foreach ($content['validators'] as $class) {
  438. $validator = $this->_validators[$class];
  439. if (!$uploaderror and !$validator->isValid($content['tmp_name'], $content)) {
  440. $this->_messages += $validator->getMessages();
  441. }
  442. if (($class === 'Zend_Validate_File_Upload') and (count($this->_messages) > 0)) {
  443. $uploaderror = true;
  444. }
  445. }
  446. }
  447. }
  448. if (count($this->_messages) > 0) {
  449. return false;
  450. }
  451. return true;
  452. }
  453. /**
  454. * Returns found validation messages
  455. *
  456. * @return array
  457. */
  458. public function getMessages()
  459. {
  460. return $this->_messages;
  461. }
  462. /**
  463. * Retrieve error codes
  464. *
  465. * @return array
  466. */
  467. public function getErrors()
  468. {
  469. return array_keys($this->_messages);
  470. }
  471. /**
  472. * Adds a new filter for this class
  473. *
  474. * @param string|array $filter Type of filter to add
  475. * @param string|array $options Options to set for the filter
  476. * @param string|array $files Files to limit this filter to
  477. * @return Zend_File_Transfer_Adapter
  478. */
  479. public function addFilter($filter, $options = null, $files = null)
  480. {
  481. if ($filter instanceof Zend_Filter_Interface) {
  482. $class = get_class($filter);
  483. } elseif (is_string($filter)) {
  484. $class = $this->getPluginLoader(self::FILTER)->load($filter);
  485. $filter = new $class($options);
  486. } else {
  487. require_once 'Zend/File/Transfer/Exception.php';
  488. throw new Zend_File_Transfer_Exception('Invalid filter specified');
  489. }
  490. $this->_filters[$class] = $filter;
  491. if ($files === null) {
  492. $files = array_keys($this->_files);
  493. } else {
  494. if (!is_array($files)) {
  495. $files = array($files);
  496. }
  497. }
  498. foreach ($files as $key => $file) {
  499. if (!is_string($file)) {
  500. if (is_array($file) && !is_numeric($key)) {
  501. $file = $key;
  502. } else {
  503. continue;
  504. }
  505. }
  506. if (!array_key_exists($file, $this->_files)) {
  507. continue;
  508. }
  509. $this->_files[$file]['filters'][] = $class;
  510. }
  511. return $this;
  512. }
  513. /**
  514. * Add Multiple filters at once
  515. *
  516. * @param array $filters
  517. * @param string|array $files
  518. * @return Zend_File_Transfer_Adapter_Abstract
  519. */
  520. public function addFilters(array $filters, $files = null)
  521. {
  522. foreach ($filters as $key => $spec) {
  523. if ($spec instanceof Zend_Filter_Interface) {
  524. $this->addFilter($spec, null, $files);
  525. continue;
  526. }
  527. if (is_string($key)) {
  528. $this->addFilter($key, $spec, $files);
  529. continue;
  530. }
  531. if (is_int($key)) {
  532. if (is_string($spec)) {
  533. $this->addFilter($spec, null, $files);
  534. continue;
  535. }
  536. if (is_array($spec)) {
  537. if (!array_key_exists('filter', $spec)) {
  538. continue;
  539. }
  540. $filter = $spec['filter'];
  541. unset($spec['filter']);
  542. $this->addFilter($filter, $spec, $files);
  543. continue;
  544. }
  545. continue;
  546. }
  547. }
  548. return $this;
  549. }
  550. /**
  551. * Sets a filter for the class, erasing all previous set
  552. *
  553. * @param string|array $filter Filter to set
  554. * @param string|array $files Files to limit this filter to
  555. * @return Zend_File_Transfer_Adapter
  556. */
  557. public function setFilters(array $filters, $files = null)
  558. {
  559. $this->clearFilters();
  560. return $this->addFilters($filters, $files);
  561. }
  562. /**
  563. * Determine if a given filter has already been registered
  564. *
  565. * @param string $name
  566. * @return bool
  567. */
  568. public function hasFilter($name)
  569. {
  570. return (false !== $this->_getFilterIdentifier($name));
  571. }
  572. /**
  573. * Retrieve individual filter
  574. *
  575. * @param string $name
  576. * @return Zend_Filter_Interface|null
  577. */
  578. public function getFilter($name)
  579. {
  580. if (false === ($identifier = $this->_getFilterIdentifier($name))) {
  581. return null;
  582. }
  583. return $this->_filters[$identifier];
  584. }
  585. /**
  586. * Returns all set filters
  587. *
  588. * @param string|array $files (Optional) Returns the filter for this files
  589. * @return null|array List of set filters
  590. * @throws Zend_File_Transfer_Exception When file not found
  591. */
  592. public function getFilters($files = null)
  593. {
  594. if ($files === null) {
  595. return $this->_filters;
  596. }
  597. if (!is_array($files)) {
  598. $files = array($files);
  599. }
  600. $filters = array();
  601. foreach ($files as $file) {
  602. if (!isset($this->_files[$file])) {
  603. require_once 'Zend/File/Transfer/Exception.php';
  604. throw new Zend_File_Transfer_Exception('Unknown file');
  605. }
  606. $filters += $this->_files[$file]['filters'];
  607. }
  608. $filters = array_unique($filters);
  609. foreach ($filters as $filter) {
  610. $result[] = $this->_filters[$filter];
  611. }
  612. return $result;
  613. }
  614. /**
  615. * Remove an individual filter
  616. *
  617. * @param string $name
  618. * @return Zend_File_Transfer_Adapter_Abstract
  619. */
  620. public function removeFilter($name)
  621. {
  622. if (false === ($key = $this->_getFilterIdentifier($name))) {
  623. return $this;
  624. }
  625. unset($this->_filters[$key]);
  626. foreach (array_keys($this->_files) as $file) {
  627. if (!$index = array_search($key, $this->_files[$file]['filters'])) {
  628. continue;
  629. }
  630. unset($this->_files[$file]['filters'][$index]);
  631. }
  632. return $this;
  633. }
  634. /**
  635. * Remove all filters
  636. *
  637. * @return Zend_File_Transfer_Adapter_Abstract
  638. */
  639. public function clearFilters()
  640. {
  641. $this->_filters = array();
  642. foreach (array_keys($this->_files) as $file) {
  643. $this->_files[$file]['filters'] = array();
  644. }
  645. return $this;
  646. }
  647. /**
  648. * Returns all set files
  649. *
  650. * @return array List of set files
  651. * @throws Zend_File_Transfer_Exception Not implemented
  652. */
  653. public function getFile()
  654. {
  655. require_once 'Zend/File/Transfer/Exception.php';
  656. throw new Zend_File_Transfer_Exception('Method not implemented');
  657. }
  658. /**
  659. * Retrieve filename of transferred file
  660. *
  661. * Returns final target destnation of transferred file.
  662. *
  663. * @param string $file
  664. * @return string
  665. */
  666. public function getFileName($file)
  667. {
  668. $file = (string) $file;
  669. if (!array_key_exists($file, $this->_files)) {
  670. return null;
  671. }
  672. $directory = $this->getDestination($file);
  673. return $directory . DIRECTORY_SEPARATOR . $this->_files[$file]['name'];
  674. }
  675. /**
  676. * Adds one or more files
  677. *
  678. * @param string|array $file File to add
  679. * @param string|array $validator Validators to use for this file, must be set before
  680. * @param string|array $filter Filters to use for this file, must be set before
  681. * @return Zend_File_Transfer_Adapter_Abstract
  682. * @throws Zend_File_Transfer_Exception Not implemented
  683. */
  684. public function addFile($file, $validator = null, $filter = null)
  685. {
  686. require_once 'Zend/File/Transfer/Exception.php';
  687. throw new Zend_File_Transfer_Exception('Method not implemented');
  688. }
  689. /**
  690. * Returns all set types
  691. *
  692. * @return array List of set types
  693. * @throws Zend_File_Transfer_Exception Not implemented
  694. */
  695. public function getType()
  696. {
  697. require_once 'Zend/File/Transfer/Exception.php';
  698. throw new Zend_File_Transfer_Exception('Method not implemented');
  699. }
  700. /**
  701. * Adds one or more type of files
  702. *
  703. * @param string|array $type Type of files to add
  704. * @param string|array $validator Validators to use for this file, must be set before
  705. * @param string|array $filter Filters to use for this file, must be set before
  706. * @return Zend_File_Transfer_Adapter_Abstract
  707. * @throws Zend_File_Transfer_Exception Not implemented
  708. */
  709. public function addType($type, $validator = null, $filter = null)
  710. {
  711. require_once 'Zend/File/Transfer/Exception.php';
  712. throw new Zend_File_Transfer_Exception('Method not implemented');
  713. }
  714. /**
  715. * Sets a new destination for the given files
  716. *
  717. * @deprecated Will be changed to be a filter!!!
  718. * @param string $destination New destination directory
  719. * @param string|array $files Files to set the new destination for
  720. * @return Zend_File_Transfer_Abstract
  721. */
  722. public function setDestination($destination, $files = null)
  723. {
  724. if ($files === null) {
  725. foreach ($this->_files as $file => $content) {
  726. $this->_files[$file]['destination'] = $destination;
  727. }
  728. } else {
  729. if (!is_array($files)) {
  730. $files = array($files);
  731. }
  732. foreach ($files as $file) {
  733. $this->_files[$file]['destination'] = $destination;
  734. }
  735. }
  736. return $this;
  737. }
  738. /**
  739. * Retrieve destination directory value
  740. *
  741. * @param null|string|array $files
  742. * @return null|string|array
  743. */
  744. public function getDestination($files = null)
  745. {
  746. if ((null === $files) || is_array($files)) {
  747. $destinations = array();
  748. if (!is_array($files)) {
  749. $files = $this->_files;
  750. } else {
  751. $files = array_flip($files);
  752. $files = array_intersect_assoc($files, $this->_files);
  753. }
  754. foreach ($files as $file => $content) {
  755. if (array_key_exists('destination', $content)) {
  756. $destinations[$file] = $content['destination'];
  757. } else {
  758. $tmpdir = $this->_getTmpDir();
  759. $this->setDestination($tmpdir, $file);
  760. $destinations[$file] = $tmpdir;
  761. }
  762. }
  763. return $destinations;
  764. }
  765. if (!is_string($files)) {
  766. require_once 'Zend/File/Transfer/Exception.php';
  767. throw new Zend_File_Transfer_Exception('Invalid file value passed to getDestination()');
  768. }
  769. if (!array_key_exists($files, $this->_files)) {
  770. require_once 'Zend/File/Transfer/Exception.php';
  771. throw new Zend_File_Transfer_Exception(sprintf('Unknown file "%s" passed to getDestination()', $files));
  772. }
  773. if (!array_key_exists('destination', $this->_files[$files])) {
  774. return $this->_getTmpDir();
  775. }
  776. return $this->_files[$files]['destination'];
  777. }
  778. /**
  779. * Determine system TMP directory
  780. *
  781. * @return string
  782. * @throws Zend_File_Transfer_Exception if unable to determine directory
  783. */
  784. protected function _getTmpDir()
  785. {
  786. if (null === $this->_tmpDir) {
  787. if (function_exists('sys_get_temp_dir')) {
  788. $tmpdir = sys_get_temp_dir();
  789. } elseif (!empty($_ENV['TMP'])) {
  790. $tmpdir = realpath($_ENV['TMP']);
  791. } elseif (!empty($_ENV['TMPDIR'])) {
  792. $tmpdir = realpath($_ENV['TMPDIR']);
  793. } else if (!empty($_ENV['TEMP'])) {
  794. $tmpdir = realpath($_ENV['TEMP']);
  795. } else {
  796. // Attemp to detect by creating a temporary file
  797. $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
  798. if ($tempFile) {
  799. $tmpdir = realpath(dirname($tempFile));
  800. unlink($tempFile);
  801. } else {
  802. require_once 'Zend/File/Transfer/Exception.php';
  803. throw new Zend_File_Transfer_Exception('Could not determine temp directory');
  804. }
  805. }
  806. $this->_tmpDir = rtrim($tmpdir, "/\\");
  807. }
  808. return $this->_tmpDir;
  809. }
  810. /**
  811. * Returns found files based on internal file array and given files
  812. *
  813. * @param string|array $files (Optional) Files to return
  814. * @return array Found files
  815. * @throws Zend_File_Transfer_Exception On false filename
  816. */
  817. protected function _getFiles($files)
  818. {
  819. $check = null;
  820. if (is_string($files)) {
  821. $files = array($files);
  822. }
  823. if (is_array($files)) {
  824. foreach ($files as $find) {
  825. $found = null;
  826. foreach ($this->_files as $file => $content) {
  827. if ($content['name'] === $find) {
  828. $found = $file;
  829. break;
  830. }
  831. if ($file === $find) {
  832. $found = $file;
  833. break;
  834. }
  835. }
  836. if ($found === null) {
  837. require_once 'Zend/File/Transfer/Exception.php';
  838. throw new Zend_File_Transfer_Exception(sprintf('"%s" not found by file transfer adapter', $file));
  839. }
  840. $check[$found] = $this->_files[$found];
  841. }
  842. }
  843. if ($files === null) {
  844. $check = $this->_files;
  845. }
  846. return $check;
  847. }
  848. /**
  849. * Retrieve internal identifier for a named validator
  850. *
  851. * @param string $name
  852. * @return string
  853. */
  854. protected function _getValidatorIdentifier($name)
  855. {
  856. if (array_key_exists($name, $this->_validators)) {
  857. return $name;
  858. }
  859. foreach (array_keys($this->_validators) as $test) {
  860. if (preg_match('/' . preg_quote($name) . '$/i', $test)) {
  861. return $test;
  862. }
  863. }
  864. return false;
  865. }
  866. /**
  867. * Retrieve internal identifier for a named filter
  868. *
  869. * @param string $name
  870. * @return string
  871. */
  872. protected function _getFilterIdentifier($name)
  873. {
  874. if (array_key_exists($name, $this->_filters)) {
  875. return $name;
  876. }
  877. foreach (array_keys($this->_filters) as $test) {
  878. if (preg_match('/' . preg_quote($name) . '$/i', $test)) {
  879. return $test;
  880. }
  881. }
  882. return false;
  883. }
  884. }