UploadSession.class.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /* Get the transformations interface */
  12. require_once 'libraries/plugins/UploadInterface.int.php';
  13. /**
  14. * Implementation for session
  15. *
  16. * @package PhpMyAdmin
  17. */
  18. class UploadSession implements UploadInterface
  19. {
  20. /**
  21. * Gets the specific upload ID Key
  22. *
  23. * @return string ID Key
  24. */
  25. public static function getIdKey()
  26. {
  27. return ini_get('session.upload_progress.name');
  28. }
  29. /**
  30. * Returns upload status.
  31. *
  32. * This is implementation for session.upload_progress in PHP 5.4+.
  33. *
  34. * @param string $id upload id
  35. *
  36. * @return array|null
  37. */
  38. public static function getUploadStatus($id)
  39. {
  40. global $SESSION_KEY;
  41. if (trim($id) == '') {
  42. return null;
  43. }
  44. if (! array_key_exists($id, $_SESSION[$SESSION_KEY])) {
  45. $_SESSION[$SESSION_KEY][$id] = array(
  46. 'id' => $id,
  47. 'finished' => false,
  48. 'percent' => 0,
  49. 'total' => 0,
  50. 'complete' => 0,
  51. 'plugin' => UploadSession::getIdKey()
  52. );
  53. }
  54. $ret = $_SESSION[$SESSION_KEY][$id];
  55. if (! PMA_import_sessionCheck() || $ret['finished']) {
  56. return $ret;
  57. }
  58. $status = false;
  59. $sessionkey = ini_get('session.upload_progress.prefix') . $id;
  60. if (isset($_SESSION[$sessionkey])) {
  61. $status = $_SESSION[$sessionkey];
  62. }
  63. if ($status) {
  64. $ret['finished'] = $status['done'];
  65. $ret['total'] = $status['content_length'];
  66. $ret['complete'] = $status['bytes_processed'];
  67. if ($ret['total'] > 0) {
  68. $ret['percent'] = $ret['complete'] / $ret['total'] * 100;
  69. }
  70. } else {
  71. $ret = array(
  72. 'id' => $id,
  73. 'finished' => true,
  74. 'percent' => 100,
  75. 'total' => $ret['total'],
  76. 'complete' => $ret['total'],
  77. 'plugin' => UploadSession::getIdKey()
  78. );
  79. }
  80. $_SESSION[$SESSION_KEY][$id] = $ret;
  81. return $ret;
  82. }
  83. }
  84. ?>