UploadProgress.php 2.2 KB

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