UploadSession.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 ini_get;
  11. use function trim;
  12. /**
  13. * Implementation for session
  14. */
  15. class UploadSession 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 ini_get('session.upload_progress.name');
  25. }
  26. /**
  27. * Returns upload status.
  28. *
  29. * This is implementation for session.upload_progress in PHP 5.4+.
  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::sessionCheck() || $ret['finished']) {
  53. return $ret;
  54. }
  55. $status = false;
  56. $sessionkey = ini_get('session.upload_progress.prefix') . $id;
  57. if (isset($_SESSION[$sessionkey])) {
  58. $status = $_SESSION[$sessionkey];
  59. }
  60. if ($status) {
  61. $ret['finished'] = $status['done'];
  62. $ret['total'] = $status['content_length'];
  63. $ret['complete'] = $status['bytes_processed'];
  64. if ($ret['total'] > 0) {
  65. $ret['percent'] = $ret['complete'] / $ret['total'] * 100;
  66. }
  67. } else {
  68. $ret = [
  69. 'id' => $id,
  70. 'finished' => true,
  71. 'percent' => 100,
  72. 'total' => $ret['total'],
  73. 'complete' => $ret['total'],
  74. 'plugin' => self::getIdKey(),
  75. ];
  76. }
  77. $_SESSION[$SESSION_KEY][$id] = $ret;
  78. return $ret;
  79. }
  80. }