Abstract.php 46 KB

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