UploadProgress.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Provides upload functionalities for the import plugins
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Import\Upload;
  7. use PhpMyAdmin\Import\Ajax;
  8. use PhpMyAdmin\Plugins\UploadInterface;
  9. use function array_key_exists;
  10. use function function_exists;
  11. use function trim;
  12. /**
  13. * Implementation for upload progress
  14. */
  15. class UploadProgress implements UploadInterface
  16. {
  17. /**
  18. * Gets the specific upload ID Key
  19. *
  20. * @return string ID Key
  21. */
  22. public static function getIdKey()
  23. {
  24. return 'UPLOAD_IDENTIFIER';
  25. }
  26. /**
  27. * Returns upload status.
  28. *
  29. * This is implementation for upload progress
  30. *
  31. * @param string $id upload id
  32. *
  33. * @return array|null
  34. */
  35. public static function getUploadStatus($id)
  36. {
  37. global $SESSION_KEY;
  38. if (trim($id) == '') {
  39. return null;
  40. }
  41. if (! array_key_exists($id, $_SESSION[$SESSION_KEY])) {
  42. $_SESSION[$SESSION_KEY][$id] = [
  43. 'id' => $id,
  44. 'finished' => false,
  45. 'percent' => 0,
  46. 'total' => 0,
  47. 'complete' => 0,
  48. 'plugin' => self::getIdKey(),
  49. ];
  50. }
  51. $ret = $_SESSION[$SESSION_KEY][$id];
  52. if (! Ajax::progressCheck() || $ret['finished']) {
  53. return $ret;
  54. }
  55. $status = null;
  56. // @see https://pecl.php.net/package/uploadprogress
  57. if (function_exists('uploadprogress_get_info')) {
  58. $status = uploadprogress_get_info($id);
  59. }
  60. if ($status) {
  61. if ($status['bytes_uploaded'] == $status['bytes_total']) {
  62. $ret['finished'] = true;
  63. } else {
  64. $ret['finished'] = false;
  65. }
  66. $ret['total'] = $status['bytes_total'];
  67. $ret['complete'] = $status['bytes_uploaded'];
  68. if ($ret['total'] > 0) {
  69. $ret['percent'] = $ret['complete'] / $ret['total'] * 100;
  70. }
  71. } else {
  72. $ret = [
  73. 'id' => $id,
  74. 'finished' => true,
  75. 'percent' => 100,
  76. 'total' => $ret['total'],
  77. 'complete' => $ret['total'],
  78. 'plugin' => self::getIdKey(),
  79. ];
  80. }
  81. $_SESSION[$SESSION_KEY][$id] = $ret;
  82. return $ret;
  83. }
  84. }