Ajax.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Import;
  4. use PhpMyAdmin\Core;
  5. use function extension_loaded;
  6. use function function_exists;
  7. use function ini_get;
  8. use function json_encode;
  9. use function ucwords;
  10. use function uniqid;
  11. /**
  12. * Handles plugins that show the upload progress.
  13. */
  14. final class Ajax
  15. {
  16. /**
  17. * Sets up some variables for upload progress
  18. */
  19. public static function uploadProgressSetup(): array
  20. {
  21. /**
  22. * constant for differentiating array in $_SESSION variable
  23. */
  24. $SESSION_KEY = '__upload_status';
  25. /**
  26. * sets default plugin for handling the import process
  27. */
  28. $_SESSION[$SESSION_KEY]['handler'] = '';
  29. /**
  30. * unique ID for each upload
  31. */
  32. $upload_id = uniqid('');
  33. /**
  34. * list of available plugins
  35. */
  36. $plugins = [
  37. // in PHP 5.4 session-based upload progress was problematic, see closed bug 3964
  38. //"session",
  39. 'progress',
  40. 'apc',
  41. 'noplugin',
  42. ];
  43. // select available plugin
  44. foreach ($plugins as $plugin) {
  45. $check = $plugin . 'Check';
  46. if (self::$check()) {
  47. $upload_class = 'PhpMyAdmin\Plugins\Import\Upload\Upload' . ucwords(
  48. $plugin
  49. );
  50. $_SESSION[$SESSION_KEY]['handler'] = $upload_class;
  51. break;
  52. }
  53. }
  54. return [
  55. $SESSION_KEY,
  56. $upload_id,
  57. $plugins,
  58. ];
  59. }
  60. /**
  61. * Checks if APC bar extension is available and configured correctly.
  62. *
  63. * @return bool true if APC extension is available and if rfc1867 is enabled,
  64. * false if it is not
  65. */
  66. public static function apcCheck()
  67. {
  68. if (! extension_loaded('apc')
  69. || ! function_exists('apc_fetch')
  70. || ! function_exists('getallheaders')
  71. ) {
  72. return false;
  73. }
  74. return ini_get('apc.enabled') && ini_get('apc.rfc1867');
  75. }
  76. /**
  77. * Checks if PhpMyAdmin\Plugins\Import\Upload\UploadProgress bar extension is
  78. * available.
  79. *
  80. * @return bool true if PhpMyAdmin\Plugins\Import\Upload\UploadProgress
  81. * extension is available, false if it is not
  82. */
  83. public static function progressCheck(): bool
  84. {
  85. return function_exists('uploadprogress_get_info')
  86. && function_exists('getallheaders');
  87. }
  88. /**
  89. * Checks if PHP 5.4 session upload-progress feature is available.
  90. *
  91. * @return bool true if PHP 5.4 session upload-progress is available,
  92. * false if it is not
  93. */
  94. public static function sessionCheck(): bool
  95. {
  96. return ini_get('session.upload_progress.enabled') === '1';
  97. }
  98. /**
  99. * Default plugin for handling import.
  100. * If no other plugin is available, noplugin is used.
  101. *
  102. * @return true
  103. */
  104. public static function nopluginCheck(): bool
  105. {
  106. return true;
  107. }
  108. /**
  109. * The function outputs json encoded status of uploaded.
  110. * It uses PMA_getUploadStatus, which is defined in plugin's file.
  111. *
  112. * @param string $id ID of transfer, usually $upload_id
  113. */
  114. public static function status($id): void
  115. {
  116. Core::headerJSON();
  117. echo json_encode(
  118. $_SESSION[$GLOBALS['SESSION_KEY']]['handler']::getUploadStatus($id)
  119. );
  120. }
  121. }