PageRenderTime 87ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

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

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