PageRenderTime 74ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

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