UploadSession.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 session
  13. *
  14. * @package PhpMyAdmin
  15. */
  16. class UploadSession 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 ini_get('session.upload_progress.name');
  26. }
  27. /**
  28. * Returns upload status.
  29. *
  30. * This is implementation for session.upload_progress in PHP 5.4+.
  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' => UploadSession::getIdKey(),
  50. );
  51. }
  52. $ret = $_SESSION[$SESSION_KEY][$id];
  53. if (!ImportAjax::sessionCheck() || $ret['finished']) {
  54. return $ret;
  55. }
  56. $status = false;
  57. $sessionkey = ini_get('session.upload_progress.prefix') . $id;
  58. if (isset($_SESSION[$sessionkey])) {
  59. $status = $_SESSION[$sessionkey];
  60. }
  61. if ($status) {
  62. $ret['finished'] = $status['done'];
  63. $ret['total'] = $status['content_length'];
  64. $ret['complete'] = $status['bytes_processed'];
  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' => UploadSession::getIdKey(),
  76. );
  77. }
  78. $_SESSION[$SESSION_KEY][$id] = $ret;
  79. return $ret;
  80. }
  81. }