Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Abstract.php
Go to the documentation of this file.
1 <?php
31 {
35  const FILTER = 'FILTER';
36  const VALIDATE = 'VALIDATE';
44  protected $_break = array();
45 
51  protected $_filters = array();
52 
58  protected $_loaders = array();
59 
65  protected $_messages = array();
66 
70  protected $_translator;
71 
77  protected $_translatorDisabled = false;
78 
83  protected $_validators = array();
84 
101  protected $_files = array();
102 
107  protected $_tmpDir;
108 
112  protected $_options = array(
113  'ignoreNoFile' => false,
114  'useByteString' => true,
115  'magicFile' => null,
116  'detectInfos' => true,
117  );
118 
125  abstract public function send($options = null);
126 
133  abstract public function receive($options = null);
134 
141  abstract public function isSent($files = null);
142 
149  abstract public function isReceived($files = null);
150 
157  abstract public function isUploaded($files = null);
158 
165  abstract public function isFiltered($files = null);
166 
172  public static function getProgress()
173  {
174  #require_once 'Zend/File/Transfer/Exception.php';
175  throw new Zend_File_Transfer_Exception('Method must be implemented within the adapter');
176  }
177 
187  {
188  $type = strtoupper($type);
189  switch ($type) {
190  case self::FILTER:
191  case self::VALIDATE:
192  $this->_loaders[$type] = $loader;
193  return $this;
194  default:
195  #require_once 'Zend/File/Transfer/Exception.php';
196  throw new Zend_File_Transfer_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
197  }
198  }
199 
210  public function getPluginLoader($type)
211  {
212  $type = strtoupper($type);
213  switch ($type) {
214  case self::FILTER:
215  case self::VALIDATE:
216  $prefixSegment = ucfirst(strtolower($type));
217  $pathSegment = $prefixSegment;
218  if (!isset($this->_loaders[$type])) {
219  $paths = array(
220  'Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/',
221  'Zend_' . $prefixSegment . '_File' => 'Zend/' . $pathSegment . '/File',
222  );
223 
224  #require_once 'Zend/Loader/PluginLoader.php';
225  $this->_loaders[$type] = new Zend_Loader_PluginLoader($paths);
226  } else {
227  $loader = $this->_loaders[$type];
228  $prefix = 'Zend_' . $prefixSegment . '_File_';
229  if (!$loader->getPaths($prefix)) {
230  $loader->addPrefixPath($prefix, str_replace('_', '/', $prefix));
231  }
232  }
233  return $this->_loaders[$type];
234  default:
235  #require_once 'Zend/File/Transfer/Exception.php';
236  throw new Zend_File_Transfer_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
237  }
238  }
239 
256  public function addPrefixPath($prefix, $path, $type = null)
257  {
258  $type = strtoupper($type);
259  switch ($type) {
260  case self::FILTER:
261  case self::VALIDATE:
262  $loader = $this->getPluginLoader($type);
263  $loader->addPrefixPath($prefix, $path);
264  return $this;
265  case null:
266  $prefix = rtrim($prefix, '_');
267  $path = rtrim($path, DIRECTORY_SEPARATOR);
268  foreach (array(self::FILTER, self::VALIDATE) as $type) {
269  $cType = ucfirst(strtolower($type));
270  $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR;
271  $pluginPrefix = $prefix . '_' . $cType;
272  $loader = $this->getPluginLoader($type);
273  $loader->addPrefixPath($pluginPrefix, $pluginPath);
274  }
275  return $this;
276  default:
277  #require_once 'Zend/File/Transfer/Exception.php';
278  throw new Zend_File_Transfer_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
279  }
280  }
281 
288  public function addPrefixPaths(array $spec)
289  {
290  if (isset($spec['prefix']) && isset($spec['path'])) {
291  return $this->addPrefixPath($spec['prefix'], $spec['path']);
292  }
293  foreach ($spec as $type => $paths) {
294  if (is_numeric($type) && is_array($paths)) {
295  $type = null;
296  if (isset($paths['prefix']) && isset($paths['path'])) {
297  if (isset($paths['type'])) {
298  $type = $paths['type'];
299  }
300  $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
301  }
302  } elseif (!is_numeric($type)) {
303  if (!isset($paths['prefix']) || !isset($paths['path'])) {
304  foreach ($paths as $prefix => $spec) {
305  if (is_array($spec)) {
306  foreach ($spec as $path) {
307  if (!is_string($path)) {
308  continue;
309  }
310  $this->addPrefixPath($prefix, $path, $type);
311  }
312  } elseif (is_string($spec)) {
313  $this->addPrefixPath($prefix, $spec, $type);
314  }
315  }
316  } else {
317  $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
318  }
319  }
320  }
321  return $this;
322  }
323 
333  public function addValidator($validator, $breakChainOnFailure = false, $options = null, $files = null)
334  {
335  if ($validator instanceof Zend_Validate_Interface) {
336  $name = get_class($validator);
337  } elseif (is_string($validator)) {
338  $name = $this->getPluginLoader(self::VALIDATE)->load($validator);
339  $validator = new $name($options);
340  if (is_array($options) && isset($options['messages'])) {
341  if (is_array($options['messages'])) {
342  $validator->setMessages($options['messages']);
343  } elseif (is_string($options['messages'])) {
344  $validator->setMessage($options['messages']);
345  }
346 
347  unset($options['messages']);
348  }
349  } else {
350  #require_once 'Zend/File/Transfer/Exception.php';
351  throw new Zend_File_Transfer_Exception('Invalid validator provided to addValidator; must be string or Zend_Validate_Interface');
352  }
353 
354  $this->_validators[$name] = $validator;
355  $this->_break[$name] = $breakChainOnFailure;
356  $files = $this->_getFiles($files, true, true);
357  foreach ($files as $file) {
358  if ($name == 'NotEmpty') {
359  $temp = $this->_files[$file]['validators'];
360  $this->_files[$file]['validators'] = array($name);
361  $this->_files[$file]['validators'] += $temp;
362  } else {
363  $this->_files[$file]['validators'][] = $name;
364  }
365 
366  $this->_files[$file]['validated'] = false;
367  }
368 
369  return $this;
370  }
371 
379  public function addValidators(array $validators, $files = null)
380  {
381  foreach ($validators as $name => $validatorInfo) {
382  if ($validatorInfo instanceof Zend_Validate_Interface) {
383  $this->addValidator($validatorInfo, null, null, $files);
384  } else if (is_string($validatorInfo)) {
385  if (!is_int($name)) {
386  $this->addValidator($name, null, $validatorInfo, $files);
387  } else {
388  $this->addValidator($validatorInfo, null, null, $files);
389  }
390  } else if (is_array($validatorInfo)) {
391  $argc = count($validatorInfo);
392  $breakChainOnFailure = false;
393  $options = array();
394  if (isset($validatorInfo['validator'])) {
395  $validator = $validatorInfo['validator'];
396  if (isset($validatorInfo['breakChainOnFailure'])) {
397  $breakChainOnFailure = $validatorInfo['breakChainOnFailure'];
398  }
399 
400  if (isset($validatorInfo['options'])) {
401  $options = $validatorInfo['options'];
402  }
403 
404  $this->addValidator($validator, $breakChainOnFailure, $options, $files);
405  } else {
406  if (is_string($name)) {
407  $validator = $name;
408  $options = $validatorInfo;
409  $this->addValidator($validator, $breakChainOnFailure, $options, $files);
410  } else {
411  $file = $files;
412  switch (true) {
413  case (0 == $argc):
414  break;
415  case (1 <= $argc):
416  $validator = array_shift($validatorInfo);
417  case (2 <= $argc):
418  $breakChainOnFailure = array_shift($validatorInfo);
419  case (3 <= $argc):
420  $options = array_shift($validatorInfo);
421  case (4 <= $argc):
422  if (!empty($validatorInfo)) {
423  $file = array_shift($validatorInfo);
424  }
425  default:
426  $this->addValidator($validator, $breakChainOnFailure, $options, $file);
427  break;
428  }
429  }
430  }
431  } else {
432  #require_once 'Zend/File/Transfer/Exception.php';
433  throw new Zend_File_Transfer_Exception('Invalid validator passed to addValidators()');
434  }
435  }
436 
437  return $this;
438  }
439 
447  public function setValidators(array $validators, $files = null)
448  {
449  $this->clearValidators();
450  return $this->addValidators($validators, $files);
451  }
452 
459  public function hasValidator($name)
460  {
461  return (false !== $this->_getValidatorIdentifier($name));
462  }
463 
470  public function getValidator($name)
471  {
472  if (false === ($identifier = $this->_getValidatorIdentifier($name))) {
473  return null;
474  }
475  return $this->_validators[$identifier];
476  }
477 
484  public function getValidators($files = null)
485  {
486  if ($files == null) {
487  return $this->_validators;
488  }
489 
490  $files = $this->_getFiles($files, true, true);
491  $validators = array();
492  foreach ($files as $file) {
493  if (!empty($this->_files[$file]['validators'])) {
494  $validators += $this->_files[$file]['validators'];
495  }
496  }
497 
498  $validators = array_unique($validators);
499  $result = array();
500  foreach ($validators as $validator) {
501  $result[$validator] = $this->_validators[$validator];
502  }
503 
504  return $result;
505  }
506 
513  public function removeValidator($name)
514  {
515  if (false === ($key = $this->_getValidatorIdentifier($name))) {
516  return $this;
517  }
518 
519  unset($this->_validators[$key]);
520  foreach (array_keys($this->_files) as $file) {
521  if (empty($this->_files[$file]['validators'])) {
522  continue;
523  }
524 
525  $index = array_search($key, $this->_files[$file]['validators']);
526  if ($index === false) {
527  continue;
528  }
529 
530  unset($this->_files[$file]['validators'][$index]);
531  $this->_files[$file]['validated'] = false;
532  }
533 
534  return $this;
535  }
536 
542  public function clearValidators()
543  {
544  $this->_validators = array();
545  foreach (array_keys($this->_files) as $file) {
546  $this->_files[$file]['validators'] = array();
547  $this->_files[$file]['validated'] = false;
548  }
549 
550  return $this;
551  }
552 
560  public function setOptions($options = array(), $files = null) {
561  $file = $this->_getFiles($files, false, true);
562 
563  if (is_array($options)) {
564  if (empty($file)) {
565  $this->_options = array_merge($this->_options, $options);
566  }
567 
568  foreach ($options as $name => $value) {
569  foreach ($file as $key => $content) {
570  switch ($name) {
571  case 'magicFile' :
572  $this->_files[$key]['options'][$name] = (string) $value;
573  break;
574 
575  case 'ignoreNoFile' :
576  case 'useByteString' :
577  case 'detectInfos' :
578  $this->_files[$key]['options'][$name] = (boolean) $value;
579  break;
580 
581  default:
582  #require_once 'Zend/File/Transfer/Exception.php';
583  throw new Zend_File_Transfer_Exception("Unknown option: $name = $value");
584  }
585  }
586  }
587  }
588 
589  return $this;
590  }
591 
598  public function getOptions($files = null) {
599  $file = $this->_getFiles($files, false, true);
600 
601  foreach ($file as $key => $content) {
602  if (isset($this->_files[$key]['options'])) {
603  $options[$key] = $this->_files[$key]['options'];
604  } else {
605  $options[$key] = array();
606  }
607  }
608 
609  return $options;
610  }
611 
618  public function isValid($files = null)
619  {
620  $check = $this->_getFiles($files, false, true);
621  if (empty($check)) {
622  return false;
623  }
624 
625  $translator = $this->getTranslator();
626  $this->_messages = array();
627  $break = false;
628  foreach($check as $key => $content) {
629  if (array_key_exists('validators', $content) &&
630  in_array('Zend_Validate_File_Count', $content['validators'])) {
631  $validator = $this->_validators['Zend_Validate_File_Count'];
632  $count = $content;
633  if (empty($content['tmp_name'])) {
634  continue;
635  }
636 
637  if (array_key_exists('destination', $content)) {
638  $checkit = $content['destination'];
639  } else {
640  $checkit = dirname($content['tmp_name']);
641  }
642 
643  $checkit .= DIRECTORY_SEPARATOR . $content['name'];
644  $validator->addFile($checkit);
645  }
646  }
647 
648  if (isset($count)) {
649  if (!$validator->isValid($count['tmp_name'], $count)) {
650  $this->_messages += $validator->getMessages();
651  }
652  }
653 
654  foreach ($check as $key => $content) {
655  $fileerrors = array();
656  if (array_key_exists('validators', $content) && $content['validated']) {
657  continue;
658  }
659 
660  if (array_key_exists('validators', $content)) {
661  foreach ($content['validators'] as $class) {
662  $validator = $this->_validators[$class];
663  if (method_exists($validator, 'setTranslator')) {
664  $validator->setTranslator($translator);
665  }
666 
667  if (($class === 'Zend_Validate_File_Upload') and (empty($content['tmp_name']))) {
668  $tocheck = $key;
669  } else {
670  $tocheck = $content['tmp_name'];
671  }
672 
673  if (!$validator->isValid($tocheck, $content)) {
674  $fileerrors += $validator->getMessages();
675  }
676 
677  if (!empty($content['options']['ignoreNoFile']) and (isset($fileerrors['fileUploadErrorNoFile']))) {
678  unset($fileerrors['fileUploadErrorNoFile']);
679  break;
680  }
681 
682  if (($class === 'Zend_Validate_File_Upload') and (count($fileerrors) > 0)) {
683  break;
684  }
685 
686  if (($this->_break[$class]) and (count($fileerrors) > 0)) {
687  $break = true;
688  break;
689  }
690  }
691  }
692 
693  if (count($fileerrors) > 0) {
694  $this->_files[$key]['validated'] = false;
695  } else {
696  $this->_files[$key]['validated'] = true;
697  }
698 
699  $this->_messages += $fileerrors;
700  if ($break) {
701  break;
702  }
703  }
704 
705  if (count($this->_messages) > 0) {
706  return false;
707  }
708 
709  return true;
710  }
711 
717  public function getMessages()
718  {
719  return $this->_messages;
720  }
721 
727  public function getErrors()
728  {
729  return array_keys($this->_messages);
730  }
731 
737  public function hasErrors()
738  {
739  return (!empty($this->_messages));
740  }
741 
750  public function addFilter($filter, $options = null, $files = null)
751  {
752  if ($filter instanceof Zend_Filter_Interface) {
753  $class = get_class($filter);
754  } elseif (is_string($filter)) {
755  $class = $this->getPluginLoader(self::FILTER)->load($filter);
756  $filter = new $class($options);
757  } else {
758  #require_once 'Zend/File/Transfer/Exception.php';
759  throw new Zend_File_Transfer_Exception('Invalid filter specified');
760  }
761 
762  $this->_filters[$class] = $filter;
763  $files = $this->_getFiles($files, true, true);
764  foreach ($files as $file) {
765  $this->_files[$file]['filters'][] = $class;
766  }
767 
768  return $this;
769  }
770 
778  public function addFilters(array $filters, $files = null)
779  {
780  foreach ($filters as $key => $spec) {
781  if ($spec instanceof Zend_Filter_Interface) {
782  $this->addFilter($spec, null, $files);
783  continue;
784  }
785 
786  if (is_string($key)) {
787  $this->addFilter($key, $spec, $files);
788  continue;
789  }
790 
791  if (is_int($key)) {
792  if (is_string($spec)) {
793  $this->addFilter($spec, null, $files);
794  continue;
795  }
796 
797  if (is_array($spec)) {
798  if (!array_key_exists('filter', $spec)) {
799  continue;
800  }
801 
802  $filter = $spec['filter'];
803  unset($spec['filter']);
804  $this->addFilter($filter, $spec, $files);
805  continue;
806  }
807 
808  continue;
809  }
810  }
811 
812  return $this;
813  }
814 
822  public function setFilters(array $filters, $files = null)
823  {
824  $this->clearFilters();
825  return $this->addFilters($filters, $files);
826  }
827 
834  public function hasFilter($name)
835  {
836  return (false !== $this->_getFilterIdentifier($name));
837  }
838 
845  public function getFilter($name)
846  {
847  if (false === ($identifier = $this->_getFilterIdentifier($name))) {
848  return null;
849  }
850  return $this->_filters[$identifier];
851  }
852 
860  public function getFilters($files = null)
861  {
862  if ($files === null) {
863  return $this->_filters;
864  }
865 
866  $files = $this->_getFiles($files, true, true);
867  $filters = array();
868  foreach ($files as $file) {
869  if (!empty($this->_files[$file]['filters'])) {
870  $filters += $this->_files[$file]['filters'];
871  }
872  }
873 
874  $filters = array_unique($filters);
875  $result = array();
876  foreach ($filters as $filter) {
877  $result[] = $this->_filters[$filter];
878  }
879 
880  return $result;
881  }
882 
889  public function removeFilter($name)
890  {
891  if (false === ($key = $this->_getFilterIdentifier($name))) {
892  return $this;
893  }
894 
895  unset($this->_filters[$key]);
896  foreach (array_keys($this->_files) as $file) {
897  if (empty($this->_files[$file]['filters'])) {
898  continue;
899  }
900 
901  $index = array_search($key, $this->_files[$file]['filters']);
902  if ($index === false) {
903  continue;
904  }
905 
906  unset($this->_files[$file]['filters'][$index]);
907  }
908  return $this;
909  }
910 
916  public function clearFilters()
917  {
918  $this->_filters = array();
919  foreach (array_keys($this->_files) as $file) {
920  $this->_files[$file]['filters'] = array();
921  }
922  return $this;
923  }
924 
931  public function getFile()
932  {
933  #require_once 'Zend/File/Transfer/Exception.php';
934  throw new Zend_File_Transfer_Exception('Method not implemented');
935  }
936 
944  public function getFileName($file = null, $path = true)
945  {
946  $files = $this->_getFiles($file, true, true);
947  $result = array();
948  $directory = "";
949  foreach($files as $file) {
950  if (empty($this->_files[$file]['name'])) {
951  continue;
952  }
953 
954  if ($path === true) {
955  $directory = $this->getDestination($file) . DIRECTORY_SEPARATOR;
956  }
957 
958  $result[$file] = $directory . $this->_files[$file]['name'];
959  }
960 
961  if (count($result) == 1) {
962  return current($result);
963  }
964 
965  return $result;
966  }
967 
974  public function getFileInfo($file = null)
975  {
976  return $this->_getFiles($file);
977  }
978 
988  public function addFile($file, $validator = null, $filter = null)
989  {
990  #require_once 'Zend/File/Transfer/Exception.php';
991  throw new Zend_File_Transfer_Exception('Method not implemented');
992  }
993 
1000  public function getType()
1001  {
1002  #require_once 'Zend/File/Transfer/Exception.php';
1003  throw new Zend_File_Transfer_Exception('Method not implemented');
1004  }
1005 
1015  public function addType($type, $validator = null, $filter = null)
1016  {
1017  #require_once 'Zend/File/Transfer/Exception.php';
1018  throw new Zend_File_Transfer_Exception('Method not implemented');
1019  }
1020 
1030  public function setDestination($destination, $files = null)
1031  {
1032  $orig = $files;
1033  $destination = rtrim($destination, "/\\");
1034  if (!is_dir($destination)) {
1035  #require_once 'Zend/File/Transfer/Exception.php';
1036  throw new Zend_File_Transfer_Exception(
1037  'The given destination is not a directory or does not exist'
1038  );
1039  }
1040 
1041  if (!$this->_isPathWriteable($destination)) {
1042  #require_once 'Zend/File/Transfer/Exception.php';
1043  throw new Zend_File_Transfer_Exception(
1044  'The given destination is not writable'
1045  );
1046  }
1047 
1048  if ($files === null) {
1049  foreach ($this->_files as $file => $content) {
1050  $this->_files[$file]['destination'] = $destination;
1051  }
1052  } else {
1053  $files = $this->_getFiles($files, true, true);
1054  if (empty($files) and is_string($orig)) {
1055  $this->_files[$orig]['destination'] = $destination;
1056  }
1057 
1058  foreach ($files as $file) {
1059  $this->_files[$file]['destination'] = $destination;
1060  }
1061  }
1062 
1063  return $this;
1064  }
1065 
1073  public function getDestination($files = null)
1074  {
1075  $orig = $files;
1076  $files = $this->_getFiles($files, false, true);
1077  $destinations = array();
1078  if (empty($files) and is_string($orig)) {
1079  if (isset($this->_files[$orig]['destination'])) {
1080  $destinations[$orig] = $this->_files[$orig]['destination'];
1081  } else {
1082  #require_once 'Zend/File/Transfer/Exception.php';
1083  throw new Zend_File_Transfer_Exception(sprintf('The file transfer adapter can not find "%s"', $orig));
1084  }
1085  }
1086 
1087  foreach ($files as $key => $content) {
1088  if (isset($this->_files[$key]['destination'])) {
1089  $destinations[$key] = $this->_files[$key]['destination'];
1090  } else {
1091  $tmpdir = $this->_getTmpDir();
1092  $this->setDestination($tmpdir, $key);
1093  $destinations[$key] = $tmpdir;
1094  }
1095  }
1096 
1097  if (empty($destinations)) {
1098  $destinations = $this->_getTmpDir();
1099  } else if (count($destinations) == 1) {
1100  $destinations = current($destinations);
1101  }
1102 
1103  return $destinations;
1104  }
1105 
1113  public function setTranslator($translator = null)
1114  {
1115  if (null === $translator) {
1116  $this->_translator = null;
1117  } elseif ($translator instanceof Zend_Translate_Adapter) {
1118  $this->_translator = $translator;
1119  } elseif ($translator instanceof Zend_Translate) {
1120  $this->_translator = $translator->getAdapter();
1121  } else {
1122  #require_once 'Zend/File/Transfer/Exception.php';
1123  throw new Zend_File_Transfer_Exception('Invalid translator specified');
1124  }
1125 
1126  return $this;
1127  }
1128 
1134  public function getTranslator()
1135  {
1136  if ($this->translatorIsDisabled()) {
1137  return null;
1138  }
1139 
1140  return $this->_translator;
1141  }
1142 
1149  public function setDisableTranslator($flag)
1150  {
1151  $this->_translatorDisabled = (bool) $flag;
1152  return $this;
1153  }
1154 
1160  public function translatorIsDisabled()
1161  {
1163  }
1164 
1173  public function getHash($hash = 'crc32', $files = null)
1174  {
1175  if (!in_array($hash, hash_algos())) {
1176  #require_once 'Zend/File/Transfer/Exception.php';
1177  throw new Zend_File_Transfer_Exception('Unknown hash algorithm');
1178  }
1179 
1180  $files = $this->_getFiles($files);
1181  $result = array();
1182  foreach($files as $key => $value) {
1183  if (file_exists($value['name'])) {
1184  $result[$key] = hash_file($hash, $value['name']);
1185  } else if (file_exists($value['tmp_name'])) {
1186  $result[$key] = hash_file($hash, $value['tmp_name']);
1187  } else if (empty($value['options']['ignoreNoFile'])) {
1188  #require_once 'Zend/File/Transfer/Exception.php';
1189  throw new Zend_File_Transfer_Exception("The file '{$value['name']}' does not exist");
1190  }
1191  }
1192 
1193  if (count($result) == 1) {
1194  return current($result);
1195  }
1196 
1197  return $result;
1198  }
1199 
1207  public function getFileSize($files = null)
1208  {
1209  $files = $this->_getFiles($files);
1210  $result = array();
1211  foreach($files as $key => $value) {
1212  if (file_exists($value['name']) || file_exists($value['tmp_name'])) {
1213  if ($value['options']['useByteString']) {
1214  $result[$key] = self::_toByteString($value['size']);
1215  } else {
1216  $result[$key] = $value['size'];
1217  }
1218  } else if (empty($value['options']['ignoreNoFile'])) {
1219  #require_once 'Zend/File/Transfer/Exception.php';
1220  throw new Zend_File_Transfer_Exception("The file '{$value['name']}' does not exist");
1221  } else {
1222  continue;
1223  }
1224  }
1225 
1226  if (count($result) == 1) {
1227  return current($result);
1228  }
1229 
1230  return $result;
1231  }
1232 
1239  protected function _detectFileSize($value)
1240  {
1241  if (file_exists($value['name'])) {
1242  $result = sprintf("%u", @filesize($value['name']));
1243  } else if (file_exists($value['tmp_name'])) {
1244  $result = sprintf("%u", @filesize($value['tmp_name']));
1245  } else {
1246  return null;
1247  }
1248 
1249  return $result;
1250  }
1251 
1260  public function getMimeType($files = null)
1261  {
1262  $files = $this->_getFiles($files);
1263  $result = array();
1264  foreach($files as $key => $value) {
1265  if (file_exists($value['name']) || file_exists($value['tmp_name'])) {
1266  $result[$key] = $value['type'];
1267  } else if (empty($value['options']['ignoreNoFile'])) {
1268  #require_once 'Zend/File/Transfer/Exception.php';
1269  throw new Zend_File_Transfer_Exception("The file '{$value['name']}' does not exist");
1270  } else {
1271  continue;
1272  }
1273  }
1274 
1275  if (count($result) == 1) {
1276  return current($result);
1277  }
1278 
1279  return $result;
1280  }
1281 
1288  protected function _detectMimeType($value)
1289  {
1290  if (file_exists($value['name'])) {
1291  $file = $value['name'];
1292  } else if (file_exists($value['tmp_name'])) {
1293  $file = $value['tmp_name'];
1294  } else {
1295  return null;
1296  }
1297 
1298  if (class_exists('finfo', false)) {
1299  $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
1300  if (!empty($value['options']['magicFile'])) {
1301  $mime = @finfo_open($const, $value['options']['magicFile']);
1302  }
1303 
1304  if (empty($mime)) {
1305  $mime = @finfo_open($const);
1306  }
1307 
1308  if (!empty($mime)) {
1309  $result = finfo_file($mime, $file);
1310  }
1311 
1312  unset($mime);
1313  }
1314 
1315  if (empty($result) && (function_exists('mime_content_type')
1316  && ini_get('mime_magic.magicfile'))) {
1317  $result = mime_content_type($file);
1318  }
1319 
1320  if (empty($result)) {
1321  $result = 'application/octet-stream';
1322  }
1323 
1324  return $result;
1325  }
1326 
1333  protected static function _toByteString($size)
1334  {
1335  $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
1336  for ($i=0; $size >= 1024 && $i < 9; $i++) {
1337  $size /= 1024;
1338  }
1339 
1340  return round($size, 2) . $sizes[$i];
1341  }
1342 
1349  protected function _filter($files = null)
1350  {
1351  $check = $this->_getFiles($files);
1352  foreach ($check as $name => $content) {
1353  if (array_key_exists('filters', $content)) {
1354  foreach ($content['filters'] as $class) {
1355  $filter = $this->_filters[$class];
1356  try {
1357  $result = $filter->filter($this->getFileName($name));
1358 
1359  $this->_files[$name]['destination'] = dirname($result);
1360  $this->_files[$name]['name'] = basename($result);
1361  } catch (Zend_Filter_Exception $e) {
1362  $this->_messages += array($e->getMessage());
1363  }
1364  }
1365  }
1366  }
1367 
1368  if (count($this->_messages) > 0) {
1369  return false;
1370  }
1371 
1372  return true;
1373  }
1374 
1381  protected function _getTmpDir()
1382  {
1383  if (null === $this->_tmpDir) {
1384  $tmpdir = array();
1385  if (function_exists('sys_get_temp_dir')) {
1386  $tmpdir[] = sys_get_temp_dir();
1387  }
1388 
1389  if (!empty($_ENV['TMP'])) {
1390  $tmpdir[] = realpath($_ENV['TMP']);
1391  }
1392 
1393  if (!empty($_ENV['TMPDIR'])) {
1394  $tmpdir[] = realpath($_ENV['TMPDIR']);
1395  }
1396 
1397  if (!empty($_ENV['TEMP'])) {
1398  $tmpdir[] = realpath($_ENV['TEMP']);
1399  }
1400 
1401  $upload = ini_get('upload_tmp_dir');
1402  if ($upload) {
1403  $tmpdir[] = realpath($upload);
1404  }
1405 
1406  foreach($tmpdir as $directory) {
1407  if ($this->_isPathWriteable($directory)) {
1408  $this->_tmpDir = $directory;
1409  }
1410  }
1411 
1412  if (empty($this->_tmpDir)) {
1413  // Attemp to detect by creating a temporary file
1414  $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
1415  if ($tempFile) {
1416  $this->_tmpDir = realpath(dirname($tempFile));
1417  unlink($tempFile);
1418  } else {
1419  #require_once 'Zend/File/Transfer/Exception.php';
1420  throw new Zend_File_Transfer_Exception('Could not determine a temporary directory');
1421  }
1422  }
1423 
1424  $this->_tmpDir = rtrim($this->_tmpDir, "/\\");
1425  }
1426  return $this->_tmpDir;
1427  }
1428 
1435  protected function _isPathWriteable($path)
1436  {
1437  $tempFile = rtrim($path, "/\\");
1438  $tempFile .= '/' . 'test.1';
1439 
1440  $result = @file_put_contents($tempFile, 'TEST');
1441 
1442  if ($result == false) {
1443  return false;
1444  }
1445 
1446  $result = @unlink($tempFile);
1447 
1448  if ($result == false) {
1449  return false;
1450  }
1451 
1452  return true;
1453  }
1454 
1464  protected function _getFiles($files, $names = false, $noexception = false)
1465  {
1466  $check = array();
1467 
1468  if (is_string($files)) {
1469  $files = array($files);
1470  }
1471 
1472  if (is_array($files)) {
1473  foreach ($files as $find) {
1474  $found = array();
1475  foreach ($this->_files as $file => $content) {
1476  if (!isset($content['name'])) {
1477  continue;
1478  }
1479 
1480  if (($content['name'] === $find) && isset($content['multifiles'])) {
1481  foreach ($content['multifiles'] as $multifile) {
1482  $found[] = $multifile;
1483  }
1484  break;
1485  }
1486 
1487  if ($file === $find) {
1488  $found[] = $file;
1489  break;
1490  }
1491 
1492  if ($content['name'] === $find) {
1493  $found[] = $file;
1494  break;
1495  }
1496  }
1497 
1498  if (empty($found)) {
1499  if ($noexception !== false) {
1500  return array();
1501  }
1502 
1503  #require_once 'Zend/File/Transfer/Exception.php';
1504  throw new Zend_File_Transfer_Exception(sprintf('The file transfer adapter can not find "%s"', $find));
1505  }
1506 
1507  foreach ($found as $checked) {
1508  $check[$checked] = $this->_files[$checked];
1509  }
1510  }
1511  }
1512 
1513  if ($files === null) {
1514  $check = $this->_files;
1515  $keys = array_keys($check);
1516  foreach ($keys as $key) {
1517  if (isset($check[$key]['multifiles'])) {
1518  unset($check[$key]);
1519  }
1520  }
1521  }
1522 
1523  if ($names) {
1524  $check = array_keys($check);
1525  }
1526 
1527  return $check;
1528  }
1529 
1536  protected function _getValidatorIdentifier($name)
1537  {
1538  if (array_key_exists($name, $this->_validators)) {
1539  return $name;
1540  }
1541 
1542  foreach (array_keys($this->_validators) as $test) {
1543  if (preg_match('/' . preg_quote($name) . '$/i', $test)) {
1544  return $test;
1545  }
1546  }
1547 
1548  return false;
1549  }
1550 
1557  protected function _getFilterIdentifier($name)
1558  {
1559  if (array_key_exists($name, $this->_filters)) {
1560  return $name;
1561  }
1562 
1563  foreach (array_keys($this->_filters) as $test) {
1564  if (preg_match('/' . preg_quote($name) . '$/i', $test)) {
1565  return $test;
1566  }
1567  }
1568 
1569  return false;
1570  }
1571 }
addValidators(array $validators, $files=null)
Definition: Abstract.php:379
addType($type, $validator=null, $filter=null)
Definition: Abstract.php:1015
addFilter($filter, $options=null, $files=null)
Definition: Abstract.php:750
addPrefixPath($prefix, $path, $type=null)
Definition: Abstract.php:256
addFile($file, $validator=null, $filter=null)
Definition: Abstract.php:988
setValidators(array $validators, $files=null)
Definition: Abstract.php:447
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
setDestination($destination, $files=null)
Definition: Abstract.php:1030
$count
Definition: recent.phtml:13
$loader
Definition: autoload.php:8
getHash($hash='crc32', $files=null)
Definition: Abstract.php:1173
addFilters(array $filters, $files=null)
Definition: Abstract.php:778
getFileName($file=null, $path=true)
Definition: Abstract.php:944
$type
Definition: item.phtml:13
$prefix
Definition: name.phtml:25
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
addValidator($validator, $breakChainOnFailure=false, $options=null, $files=null)
Definition: Abstract.php:333
$filters
Definition: uploader.phtml:11
setFilters(array $filters, $files=null)
Definition: Abstract.php:822
_getFiles($files, $names=false, $noexception=false)
Definition: Abstract.php:1464
setOptions($options=array(), $files=null)
Definition: Abstract.php:560
$paths
Definition: _bootstrap.php:83
$i
Definition: gallery.phtml:31
setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type)
Definition: Abstract.php:186
$index
Definition: list.phtml:44
foreach($appDirs as $dir) $files
$checked
Definition: billing.phtml:77
if(!isset($_GET['name'])) $name
Definition: log.php:14