UploadProgress.php 2.4 KB

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