2
0

Abstract.php 46 KB

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