PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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