UploadApc.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 the APC extension
  14. *
  15. * @package PhpMyAdmin
  16. */
  17. class UploadApc 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 'APC_UPLOAD_PROGRESS';
  27. }
  28. /**
  29. * Returns upload status.
  30. *
  31. * This is implementation for APC extension.
  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' => UploadApc::getIdKey(),
  51. ];
  52. }
  53. $ret = $_SESSION[$SESSION_KEY][$id];
  54. if (! ImportAjax::apcCheck() || $ret['finished']) {
  55. return $ret;
  56. }
  57. $status = apc_fetch('upload_' . $id);
  58. if ($status) {
  59. $ret['finished'] = (bool) $status['done'];
  60. $ret['total'] = $status['total'];
  61. $ret['complete'] = $status['current'];
  62. if ($ret['total'] > 0) {
  63. $ret['percent'] = $ret['complete'] / $ret['total'] * 100;
  64. }
  65. if ($ret['percent'] == 100) {
  66. $ret['finished'] = (bool) true;
  67. }
  68. $_SESSION[$SESSION_KEY][$id] = $ret;
  69. }
  70. return $ret;
  71. }
  72. }