UploadApc.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 trim;
  11. /**
  12. * Implementation for the APC extension
  13. */
  14. class UploadApc implements UploadInterface
  15. {
  16. /**
  17. * Gets the specific upload ID Key
  18. *
  19. * @return string ID Key
  20. */
  21. public static function getIdKey()
  22. {
  23. return 'APC_UPLOAD_PROGRESS';
  24. }
  25. /**
  26. * Returns upload status.
  27. *
  28. * This is implementation for APC extension.
  29. *
  30. * @param string $id upload id
  31. *
  32. * @return array|null
  33. */
  34. public static function getUploadStatus($id)
  35. {
  36. global $SESSION_KEY;
  37. if (trim($id) == '') {
  38. return null;
  39. }
  40. if (! array_key_exists($id, $_SESSION[$SESSION_KEY])) {
  41. $_SESSION[$SESSION_KEY][$id] = [
  42. 'id' => $id,
  43. 'finished' => false,
  44. 'percent' => 0,
  45. 'total' => 0,
  46. 'complete' => 0,
  47. 'plugin' => self::getIdKey(),
  48. ];
  49. }
  50. $ret = $_SESSION[$SESSION_KEY][$id];
  51. if (! Ajax::apcCheck() || $ret['finished']) {
  52. return $ret;
  53. }
  54. $status = apc_fetch('upload_' . $id);
  55. if ($status) {
  56. $ret['finished'] = (bool) $status['done'];
  57. $ret['total'] = $status['total'];
  58. $ret['complete'] = $status['current'];
  59. if ($ret['total'] > 0) {
  60. $ret['percent'] = $ret['complete'] / $ret['total'] * 100;
  61. }
  62. if ($ret['percent'] == 100) {
  63. $ret['finished'] = (bool) true;
  64. }
  65. $_SESSION[$SESSION_KEY][$id] = $ret;
  66. }
  67. return $ret;
  68. }
  69. }