2
0

Abstract.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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. $validator->addFile($content['tmp_name']);
  561. $count = $content;
  562. }
  563. }
  564. if (isset($count)) {
  565. if (!$validator->isValid($count['tmp_name'], $count)) {
  566. $this->_messages += $validator->getMessages();
  567. }
  568. }
  569. foreach ($check as $key => $content) {
  570. $fileerrors = array();
  571. if (array_key_exists('validators', $content) && $content['validated']) {
  572. continue;
  573. }
  574. if (array_key_exists('validators', $content)) {
  575. foreach ($content['validators'] as $class) {
  576. $validator = $this->_validators[$class];
  577. if (method_exists($validator, 'setTranslator')) {
  578. $validator->setTranslator($translator);
  579. }
  580. if (($class === 'Zend_Validate_File_Upload') and (empty($content['tmp_name']))) {
  581. $tocheck = $key;
  582. } else {
  583. $tocheck = $content['tmp_name'];
  584. }
  585. if (!$validator->isValid($tocheck, $content)) {
  586. $fileerrors += $validator->getMessages();
  587. }
  588. if (!empty($content['options']['ignoreNoFile']) and (isset($fileerrors['fileUploadErrorNoFile']))) {
  589. unset($fileerrors['fileUploadErrorNoFile']);
  590. break;
  591. }
  592. if (($class === 'Zend_Validate_File_Upload') and (count($fileerrors) > 0)) {
  593. break;
  594. }
  595. if (($this->_break[$class]) and (count($fileerrors) > 0)) {
  596. $break = true;
  597. break;
  598. }
  599. }
  600. }
  601. if (count($fileerrors) > 0) {
  602. $this->_files[$key]['validated'] = false;
  603. } else {
  604. $this->_files[$key]['validated'] = true;
  605. }
  606. $this->_messages += $fileerrors;
  607. if ($break) {
  608. break;
  609. }
  610. }
  611. if (count($this->_messages) > 0) {
  612. return false;
  613. }
  614. return true;
  615. }
  616. /**
  617. * Returns found validation messages
  618. *
  619. * @return array
  620. */
  621. public function getMessages()
  622. {
  623. return $this->_messages;
  624. }
  625. /**
  626. * Retrieve error codes
  627. *
  628. * @return array
  629. */
  630. public function getErrors()
  631. {
  632. return array_keys($this->_messages);
  633. }
  634. /**
  635. * Are there errors registered?
  636. *
  637. * @return boolean
  638. */
  639. public function hasErrors()
  640. {
  641. return (!empty($this->_messages));
  642. }
  643. /**
  644. * Adds a new filter for this class
  645. *
  646. * @param string|array $filter Type of filter to add
  647. * @param string|array $options Options to set for the filter
  648. * @param string|array $files Files to limit this filter to
  649. * @return Zend_File_Transfer_Adapter
  650. */
  651. public function addFilter($filter, $options = null, $files = null)
  652. {
  653. if ($filter instanceof Zend_Filter_Interface) {
  654. $class = get_class($filter);
  655. } elseif (is_string($filter)) {
  656. $class = $this->getPluginLoader(self::FILTER)->load($filter);
  657. $filter = new $class($options);
  658. } else {
  659. require_once 'Zend/File/Transfer/Exception.php';
  660. throw new Zend_File_Transfer_Exception('Invalid filter specified');
  661. }
  662. $this->_filters[$class] = $filter;
  663. $files = $this->_getFiles($files, true, true);
  664. foreach ($files as $file) {
  665. $this->_files[$file]['filters'][] = $class;
  666. }
  667. return $this;
  668. }
  669. /**
  670. * Add Multiple filters at once
  671. *
  672. * @param array $filters
  673. * @param string|array $files
  674. * @return Zend_File_Transfer_Adapter_Abstract
  675. */
  676. public function addFilters(array $filters, $files = null)
  677. {
  678. foreach ($filters as $key => $spec) {
  679. if ($spec instanceof Zend_Filter_Interface) {
  680. $this->addFilter($spec, null, $files);
  681. continue;
  682. }
  683. if (is_string($key)) {
  684. $this->addFilter($key, $spec, $files);
  685. continue;
  686. }
  687. if (is_int($key)) {
  688. if (is_string($spec)) {
  689. $this->addFilter($spec, null, $files);
  690. continue;
  691. }
  692. if (is_array($spec)) {
  693. if (!array_key_exists('filter', $spec)) {
  694. continue;
  695. }
  696. $filter = $spec['filter'];
  697. unset($spec['filter']);
  698. $this->addFilter($filter, $spec, $files);
  699. continue;
  700. }
  701. continue;
  702. }
  703. }
  704. return $this;
  705. }
  706. /**
  707. * Sets a filter for the class, erasing all previous set
  708. *
  709. * @param string|array $filter Filter to set
  710. * @param string|array $files Files to limit this filter to
  711. * @return Zend_File_Transfer_Adapter
  712. */
  713. public function setFilters(array $filters, $files = null)
  714. {
  715. $this->clearFilters();
  716. return $this->addFilters($filters, $files);
  717. }
  718. /**
  719. * Determine if a given filter has already been registered
  720. *
  721. * @param string $name
  722. * @return bool
  723. */
  724. public function hasFilter($name)
  725. {
  726. return (false !== $this->_getFilterIdentifier($name));
  727. }
  728. /**
  729. * Retrieve individual filter
  730. *
  731. * @param string $name
  732. * @return Zend_Filter_Interface|null
  733. */
  734. public function getFilter($name)
  735. {
  736. if (false === ($identifier = $this->_getFilterIdentifier($name))) {
  737. return null;
  738. }
  739. return $this->_filters[$identifier];
  740. }
  741. /**
  742. * Returns all set filters
  743. *
  744. * @param string|array $files (Optional) Returns the filter for this files
  745. * @return array List of set filters
  746. * @throws Zend_File_Transfer_Exception When file not found
  747. */
  748. public function getFilters($files = null)
  749. {
  750. if ($files === null) {
  751. return $this->_filters;
  752. }
  753. $files = $this->_getFiles($files, true, true);
  754. $filters = array();
  755. foreach ($files as $file) {
  756. if (!empty($this->_files[$file]['filters'])) {
  757. $filters += $this->_files[$file]['filters'];
  758. }
  759. }
  760. $filters = array_unique($filters);
  761. $result = array();
  762. foreach ($filters as $filter) {
  763. $result[] = $this->_filters[$filter];
  764. }
  765. return $result;
  766. }
  767. /**
  768. * Remove an individual filter
  769. *
  770. * @param string $name
  771. * @return Zend_File_Transfer_Adapter_Abstract
  772. */
  773. public function removeFilter($name)
  774. {
  775. if (false === ($key = $this->_getFilterIdentifier($name))) {
  776. return $this;
  777. }
  778. unset($this->_filters[$key]);
  779. foreach (array_keys($this->_files) as $file) {
  780. if (empty($this->_files[$file]['filters'])) {
  781. continue;
  782. }
  783. $index = array_search($key, $this->_files[$file]['filters']);
  784. if ($index === false) {
  785. continue;
  786. }
  787. unset($this->_files[$file]['filters'][$index]);
  788. }
  789. return $this;
  790. }
  791. /**
  792. * Remove all filters
  793. *
  794. * @return Zend_File_Transfer_Adapter_Abstract
  795. */
  796. public function clearFilters()
  797. {
  798. $this->_filters = array();
  799. foreach (array_keys($this->_files) as $file) {
  800. $this->_files[$file]['filters'] = array();
  801. }
  802. return $this;
  803. }
  804. /**
  805. * Returns all set files
  806. *
  807. * @return array List of set files
  808. * @throws Zend_File_Transfer_Exception Not implemented
  809. */
  810. public function getFile()
  811. {
  812. require_once 'Zend/File/Transfer/Exception.php';
  813. throw new Zend_File_Transfer_Exception('Method not implemented');
  814. }
  815. /**
  816. * Retrieves the filename of transferred files.
  817. *
  818. * @param string $fileelement (Optional) Element to return the filename for
  819. * @param boolean $path (Optional) Should the path also be returned ?
  820. * @return string|array
  821. */
  822. public function getFileName($file = null, $path = true)
  823. {
  824. $files = $this->_getFiles($file, true, true);
  825. $result = array();
  826. $directory = "";
  827. foreach($files as $file) {
  828. if (empty($this->_files[$file]['name'])) {
  829. continue;
  830. }
  831. if ($path === true) {
  832. $directory = $this->getDestination($file) . DIRECTORY_SEPARATOR;
  833. }
  834. $result[$file] = $directory . $this->_files[$file]['name'];
  835. }
  836. if (count($result) == 1) {
  837. return current($result);
  838. }
  839. return $result;
  840. }
  841. /**
  842. * Retrieve additional internal file informations for files
  843. *
  844. * @param string $file (Optional) File to get informations for
  845. * @return array
  846. */
  847. public function getFileInfo($file = null)
  848. {
  849. return $this->_getFiles($file);
  850. }
  851. /**
  852. * Adds one or more files
  853. *
  854. * @param string|array $file File to add
  855. * @param string|array $validator Validators to use for this file, must be set before
  856. * @param string|array $filter Filters to use for this file, must be set before
  857. * @return Zend_File_Transfer_Adapter_Abstract
  858. * @throws Zend_File_Transfer_Exception Not implemented
  859. */
  860. public function addFile($file, $validator = null, $filter = null)
  861. {
  862. require_once 'Zend/File/Transfer/Exception.php';
  863. throw new Zend_File_Transfer_Exception('Method not implemented');
  864. }
  865. /**
  866. * Returns all set types
  867. *
  868. * @return array List of set types
  869. * @throws Zend_File_Transfer_Exception Not implemented
  870. */
  871. public function getType()
  872. {
  873. require_once 'Zend/File/Transfer/Exception.php';
  874. throw new Zend_File_Transfer_Exception('Method not implemented');
  875. }
  876. /**
  877. * Adds one or more type of files
  878. *
  879. * @param string|array $type Type of files to add
  880. * @param string|array $validator Validators to use for this file, must be set before
  881. * @param string|array $filter Filters to use for this file, must be set before
  882. * @return Zend_File_Transfer_Adapter_Abstract
  883. * @throws Zend_File_Transfer_Exception Not implemented
  884. */
  885. public function addType($type, $validator = null, $filter = null)
  886. {
  887. require_once 'Zend/File/Transfer/Exception.php';
  888. throw new Zend_File_Transfer_Exception('Method not implemented');
  889. }
  890. /**
  891. * Sets a new destination for the given files
  892. *
  893. * @deprecated Will be changed to be a filter!!!
  894. * @param string $destination New destination directory
  895. * @param string|array $files Files to set the new destination for
  896. * @return Zend_File_Transfer_Abstract
  897. * @throws Zend_File_Transfer_Exception when the given destination is not a directory or does not exist
  898. */
  899. public function setDestination($destination, $files = null)
  900. {
  901. $orig = $files;
  902. $destination = rtrim($destination, "/\\");
  903. if (!is_dir($destination)) {
  904. require_once 'Zend/File/Transfer/Exception.php';
  905. throw new Zend_File_Transfer_Exception('The given destination is no directory or does not exist');
  906. }
  907. if (!is_writable($destination)) {
  908. require_once 'Zend/File/Transfer/Exception.php';
  909. throw new Zend_File_Transfer_Exception('The given destination is not writeable');
  910. }
  911. if ($files === null) {
  912. foreach ($this->_files as $file => $content) {
  913. $this->_files[$file]['destination'] = $destination;
  914. }
  915. } else {
  916. $files = $this->_getFiles($files, true, true);
  917. if (empty($files) and is_string($orig)) {
  918. $this->_files[$orig]['destination'] = $destination;
  919. }
  920. foreach ($files as $file) {
  921. $this->_files[$file]['destination'] = $destination;
  922. }
  923. }
  924. return $this;
  925. }
  926. /**
  927. * Retrieve destination directory value
  928. *
  929. * @param null|string|array $files
  930. * @return null|string|array
  931. */
  932. public function getDestination($files = null)
  933. {
  934. $orig = $files;
  935. $files = $this->_getFiles($files, false, true);
  936. $destinations = array();
  937. if (empty($files) and is_string($orig)) {
  938. if (isset($this->_files[$orig]['destination'])) {
  939. $destinations[$orig] = $this->_files[$orig]['destination'];
  940. } else {
  941. require_once 'Zend/File/Transfer/Exception.php';
  942. throw new Zend_File_Transfer_Exception(sprintf('"%s" not found by file transfer adapter', $orig));
  943. }
  944. }
  945. foreach ($files as $key => $content) {
  946. if (isset($this->_files[$key]['destination'])) {
  947. $destinations[$key] = $this->_files[$key]['destination'];
  948. } else {
  949. $tmpdir = $this->_getTmpDir();
  950. $this->setDestination($tmpdir, $key);
  951. $destinations[$key] = $tmpdir;
  952. }
  953. }
  954. if (empty($destinations)) {
  955. $destinations = $this->_getTmpDir();
  956. } else if (count($destinations) == 1) {
  957. $destinations = current($destinations);
  958. }
  959. return $destinations;
  960. }
  961. /**
  962. * Set translator object for localization
  963. *
  964. * @param Zend_Translate|null $translator
  965. * @return Zend_File_Transfer_Abstract
  966. */
  967. public function setTranslator($translator = null)
  968. {
  969. if (null === $translator) {
  970. $this->_translator = null;
  971. } elseif ($translator instanceof Zend_Translate_Adapter) {
  972. $this->_translator = $translator;
  973. } elseif ($translator instanceof Zend_Translate) {
  974. $this->_translator = $translator->getAdapter();
  975. } else {
  976. require_once 'Zend/File/Transfer/Exception.php';
  977. throw new Zend_File_Transfer_Exception('Invalid translator specified');
  978. }
  979. return $this;
  980. }
  981. /**
  982. * Retrieve localization translator object
  983. *
  984. * @return Zend_Translate_Adapter|null
  985. */
  986. public function getTranslator()
  987. {
  988. if ($this->translatorIsDisabled()) {
  989. return null;
  990. }
  991. return $this->_translator;
  992. }
  993. /**
  994. * Indicate whether or not translation should be disabled
  995. *
  996. * @param bool $flag
  997. * @return Zend_File_Transfer_Abstract
  998. */
  999. public function setDisableTranslator($flag)
  1000. {
  1001. $this->_translatorDisabled = (bool) $flag;
  1002. return $this;
  1003. }
  1004. /**
  1005. * Is translation disabled?
  1006. *
  1007. * @return bool
  1008. */
  1009. public function translatorIsDisabled()
  1010. {
  1011. return $this->_translatorDisabled;
  1012. }
  1013. /**
  1014. * Returns the hash for a given file
  1015. *
  1016. * @param string $hash Hash algorithm to use
  1017. * @param string|array $files Files to return the hash for
  1018. * @return string|array Hashstring
  1019. * @throws Zend_File_Transfer_Exception On unknown hash algorithm
  1020. */
  1021. public function getHash($hash = 'crc32', $files = null)
  1022. {
  1023. if (!in_array($hash, hash_algos())) {
  1024. require_once 'Zend/File/Transfer/Exception.php';
  1025. throw new Zend_File_Transfer_Exception('Unknown hash algorithm');
  1026. }
  1027. $files = $this->_getFiles($files);
  1028. $result = array();
  1029. foreach($files as $key => $value) {
  1030. if (file_exists($value['name'])) {
  1031. $result[$key] = hash_file($hash, $value['name']);
  1032. } else if (file_exists($value['tmp_name'])) {
  1033. $result[$key] = hash_file($hash, $value['tmp_name']);
  1034. } else {
  1035. require_once 'Zend/File/Transfer/Exception.php';
  1036. throw new Zend_File_Transfer_Exception("File '{$value['name']}' does not exist");
  1037. }
  1038. }
  1039. if (count($result) == 1) {
  1040. return current($result);
  1041. }
  1042. return $result;
  1043. }
  1044. /**
  1045. * Returns the real filesize of the file
  1046. *
  1047. * @param string|array $files Files to get the filesize from
  1048. * @throws Zend_File_Transfer_Exception When the file does not exist
  1049. * @return string|array Filesize
  1050. */
  1051. public function getFileSize($files = null)
  1052. {
  1053. $files = $this->_getFiles($files);
  1054. $result = array();
  1055. foreach($files as $key => $value) {
  1056. if (file_exists($value['name'])) {
  1057. $size = sprintf("%u", @filesize($value['name']));
  1058. } else if (file_exists($value['tmp_name'])) {
  1059. $size = sprintf("%u", @filesize($value['tmp_name']));
  1060. } else {
  1061. require_once 'Zend/File/Transfer/Exception.php';
  1062. throw new Zend_File_Transfer_Exception("File '{$value['name']}' does not exist");
  1063. }
  1064. if ($value['options']['useByteString']) {
  1065. $result[$key] = self::_toByteString($size);
  1066. } else {
  1067. $result[$key] = $size;
  1068. }
  1069. }
  1070. if (count($result) == 1) {
  1071. return current($result);
  1072. }
  1073. return $result;
  1074. }
  1075. /**
  1076. * Returns the real mimetype of the file
  1077. * Uses fileinfo, when not available mime_magic and as last fallback a manual given mimetype
  1078. *
  1079. * @param string|array $files Files to get the mimetype from
  1080. * @throws Zend_File_Transfer_Exception When the file does not exist
  1081. * @return string|array MimeType
  1082. */
  1083. public function getMimeType($files = null)
  1084. {
  1085. $files = $this->_getFiles($files);
  1086. $result = array();
  1087. foreach($files as $key => $value) {
  1088. if (file_exists($value['name'])) {
  1089. $file = $value['name'];
  1090. } else if (file_exists($value['tmp_name'])) {
  1091. $file = $value['tmp_name'];
  1092. } else {
  1093. require_once 'Zend/File/Transfer/Exception.php';
  1094. throw new Zend_File_Transfer_Exception("File '{$value['name']}' does not exist");
  1095. }
  1096. if (class_exists('finfo', false)) {
  1097. $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
  1098. if (!empty($value['options']['magicFile'])) {
  1099. $mime = new finfo($const, $value['options']['magicFile']);
  1100. } else {
  1101. $mime = new finfo($const);
  1102. }
  1103. if ($mime !== false) {
  1104. $result[$key] = $mime->file($file);
  1105. }
  1106. unset($mime);
  1107. }
  1108. if (empty($result[$key])) {
  1109. if (function_exists('mime_content_type') && ini_get('mime_magic.magicfile')) {
  1110. $result[$key] = mime_content_type($file);
  1111. } else {
  1112. $result[$key] = $value['type'];
  1113. }
  1114. }
  1115. if (empty($result[$key])) {
  1116. require_once 'Zend/File/Transfer/Exception.php';
  1117. throw new Zend_File_Transfer_Exception("The mimetype of file '{$value['name']}' could not been detected");
  1118. }
  1119. }
  1120. if (count($result) == 1) {
  1121. return current($result);
  1122. }
  1123. return $result;
  1124. }
  1125. /**
  1126. * Returns the formatted size
  1127. *
  1128. * @param integer $size
  1129. * @return string
  1130. */
  1131. protected static function _toByteString($size)
  1132. {
  1133. $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  1134. for ($i=0; $size >= 1024 && $i < 9; $i++) {
  1135. $size /= 1024;
  1136. }
  1137. return round($size, 2) . $sizes[$i];
  1138. }
  1139. /**
  1140. * Internal function to filter all given files
  1141. *
  1142. * @param string|array $files (Optional) Files to check
  1143. * @return boolean False on error
  1144. */
  1145. protected function _filter($files = null)
  1146. {
  1147. $check = $this->_getFiles($files);
  1148. foreach ($check as $name => $content) {
  1149. if (array_key_exists('filters', $content)) {
  1150. foreach ($content['filters'] as $class) {
  1151. $filter = $this->_filters[$class];
  1152. try {
  1153. $result = $filter->filter($this->getFileName($name));
  1154. $this->_files[$name]['destination'] = dirname($result);
  1155. $this->_files[$name]['name'] = basename($result);
  1156. } catch (Zend_Filter_Exception $e) {
  1157. $this->_messages += array($e->getMessage());
  1158. }
  1159. }
  1160. }
  1161. }
  1162. if (count($this->_messages) > 0) {
  1163. return false;
  1164. }
  1165. return true;
  1166. }
  1167. /**
  1168. * Determine system TMP directory and detect if we have read access
  1169. *
  1170. * @return string
  1171. * @throws Zend_File_Transfer_Exception if unable to determine directory
  1172. */
  1173. protected function _getTmpDir()
  1174. {
  1175. if (null === $this->_tmpDir) {
  1176. $tmpdir = array();
  1177. if (function_exists('sys_get_temp_dir')) {
  1178. $tmpdir[] = sys_get_temp_dir();
  1179. }
  1180. if (!empty($_ENV['TMP'])) {
  1181. $tmpdir[] = realpath($_ENV['TMP']);
  1182. }
  1183. if (!empty($_ENV['TMPDIR'])) {
  1184. $tmpdir[] = realpath($_ENV['TMPDIR']);
  1185. }
  1186. if (!empty($_ENV['TEMP'])) {
  1187. $tmpdir[] = realpath($_ENV['TEMP']);
  1188. }
  1189. $upload = ini_get('upload_tmp_dir');
  1190. if ($upload) {
  1191. $tmpdir[] = realpath($upload);
  1192. }
  1193. foreach($tmpdir as $directory) {
  1194. if ($this->_isPathWriteable($directory)) {
  1195. $this->_tmpDir = $directory;
  1196. }
  1197. }
  1198. if (empty($this->_tmpDir)) {
  1199. // Attemp to detect by creating a temporary file
  1200. $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
  1201. if ($tempFile) {
  1202. $this->_tmpDir = realpath(dirname($tempFile));
  1203. unlink($tempFile);
  1204. } else {
  1205. require_once 'Zend/File/Transfer/Exception.php';
  1206. throw new Zend_File_Transfer_Exception('Could not determine temp directory');
  1207. }
  1208. }
  1209. $this->_tmpDir = rtrim($this->_tmpDir, "/\\");
  1210. }
  1211. return $this->_tmpDir;
  1212. }
  1213. /**
  1214. * Tries to detect if we can read and write to the given path
  1215. *
  1216. * @param string $path
  1217. */
  1218. protected function _isPathWriteable($path)
  1219. {
  1220. $tempFile = rtrim($path, "/\\");
  1221. $tempFile .= '/' . 'test.1';
  1222. $result = @file_put_contents($tempFile, 'TEST');
  1223. if ($result == false) {
  1224. return false;
  1225. }
  1226. $result = @unlink($tempFile);
  1227. if ($result == false) {
  1228. return false;
  1229. }
  1230. return true;
  1231. }
  1232. /**
  1233. * Returns found files based on internal file array and given files
  1234. *
  1235. * @param string|array $files (Optional) Files to return
  1236. * @param boolean $names (Optional) Returns only names on true, else complete info
  1237. * @param boolean $noexception (Optional) Allows throwing an exception, otherwise returns an empty array
  1238. * @return array Found files
  1239. * @throws Zend_File_Transfer_Exception On false filename
  1240. */
  1241. protected function _getFiles($files, $names = false, $noexception = false)
  1242. {
  1243. $check = array();
  1244. if (is_string($files)) {
  1245. $files = array($files);
  1246. }
  1247. if (is_array($files)) {
  1248. foreach ($files as $find) {
  1249. $found = array();
  1250. foreach ($this->_files as $file => $content) {
  1251. if (!isset($content['name'])) {
  1252. continue;
  1253. }
  1254. if (($content['name'] === $find) && isset($content['multifiles'])) {
  1255. foreach ($content['multifiles'] as $multifile) {
  1256. $found[] = $multifile;
  1257. }
  1258. break;
  1259. }
  1260. if ($file === $find) {
  1261. $found[] = $file;
  1262. break;
  1263. }
  1264. if ($content['name'] === $find) {
  1265. $found[] = $file;
  1266. break;
  1267. }
  1268. }
  1269. if (empty($found)) {
  1270. if ($noexception !== false) {
  1271. return array();
  1272. }
  1273. require_once 'Zend/File/Transfer/Exception.php';
  1274. throw new Zend_File_Transfer_Exception(sprintf('"%s" not found by file transfer adapter', $find));
  1275. }
  1276. foreach ($found as $checked) {
  1277. $check[$checked] = $this->_files[$checked];
  1278. }
  1279. }
  1280. }
  1281. if ($files === null) {
  1282. $check = $this->_files;
  1283. $keys = array_keys($check);
  1284. foreach ($keys as $key) {
  1285. if (isset($check[$key]['multifiles'])) {
  1286. unset($check[$key]);
  1287. }
  1288. }
  1289. }
  1290. if ($names) {
  1291. $check = array_keys($check);
  1292. }
  1293. return $check;
  1294. }
  1295. /**
  1296. * Retrieve internal identifier for a named validator
  1297. *
  1298. * @param string $name
  1299. * @return string
  1300. */
  1301. protected function _getValidatorIdentifier($name)
  1302. {
  1303. if (array_key_exists($name, $this->_validators)) {
  1304. return $name;
  1305. }
  1306. foreach (array_keys($this->_validators) as $test) {
  1307. if (preg_match('/' . preg_quote($name) . '$/i', $test)) {
  1308. return $test;
  1309. }
  1310. }
  1311. return false;
  1312. }
  1313. /**
  1314. * Retrieve internal identifier for a named filter
  1315. *
  1316. * @param string $name
  1317. * @return string
  1318. */
  1319. protected function _getFilterIdentifier($name)
  1320. {
  1321. if (array_key_exists($name, $this->_filters)) {
  1322. return $name;
  1323. }
  1324. foreach (array_keys($this->_filters) as $test) {
  1325. if (preg_match('/' . preg_quote($name) . '$/i', $test)) {
  1326. return $test;
  1327. }
  1328. }
  1329. return false;
  1330. }
  1331. }