UploadSession.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 session
  14. *
  15. * @package PhpMyAdmin
  16. */
  17. class UploadSession 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 ini_get('session.upload_progress.name');
  27. }
  28. /**
  29. * Returns upload status.
  30. *
  31. * This is implementation for session.upload_progress in PHP 5.4+.
  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' => UploadSession::getIdKey(),
  51. ];
  52. }
  53. $ret = $_SESSION[$SESSION_KEY][$id];
  54. if (! ImportAjax::sessionCheck() || $ret['finished']) {
  55. return $ret;
  56. }
  57. $status = false;
  58. $sessionkey = ini_get('session.upload_progress.prefix') . $id;
  59. if (isset($_SESSION[$sessionkey])) {
  60. $status = $_SESSION[$sessionkey];
  61. }
  62. if ($status) {
  63. $ret['finished'] = $status['done'];
  64. $ret['total'] = $status['content_length'];
  65. $ret['complete'] = $status['bytes_processed'];
  66. if ($ret['total'] > 0) {
  67. $ret['percent'] = $ret['complete'] / $ret['total'] * 100;
  68. }
  69. } else {
  70. $ret = [
  71. 'id' => $id,
  72. 'finished' => true,
  73. 'percent' => 100,
  74. 'total' => $ret['total'],
  75. 'complete' => $ret['total'],
  76. 'plugin' => UploadSession::getIdKey(),
  77. ];
  78. }
  79. $_SESSION[$SESSION_KEY][$id] = $ret;
  80. return $ret;
  81. }
  82. }