Scripts.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * JavaScript management
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin;
  7. use function defined;
  8. use function md5;
  9. use function strpos;
  10. /**
  11. * Collects information about which JavaScript
  12. * files and objects are necessary to render
  13. * the page and generates the relevant code.
  14. */
  15. class Scripts
  16. {
  17. /**
  18. * An array of SCRIPT tags
  19. *
  20. * @access private
  21. * @var array of strings
  22. */
  23. private $files;
  24. /**
  25. * A string of discrete javascript code snippets
  26. *
  27. * @access private
  28. * @var string
  29. */
  30. private $code;
  31. /** @var Template */
  32. private $template;
  33. /**
  34. * Generates new Scripts objects
  35. */
  36. public function __construct()
  37. {
  38. $this->template = new Template();
  39. $this->files = [];
  40. $this->code = '';
  41. }
  42. /**
  43. * Adds a new file to the list of scripts
  44. *
  45. * @param string $filename The name of the file to include
  46. * @param array $params Additional parameters to pass to the file
  47. *
  48. * @return void
  49. */
  50. public function addFile(
  51. $filename,
  52. array $params = []
  53. ) {
  54. $hash = md5($filename);
  55. if (! empty($this->files[$hash])) {
  56. return;
  57. }
  58. $has_onload = $this->hasOnloadEvent($filename);
  59. $this->files[$hash] = [
  60. 'has_onload' => $has_onload,
  61. 'filename' => $filename,
  62. 'params' => $params,
  63. ];
  64. }
  65. /**
  66. * Add new files to the list of scripts
  67. *
  68. * @param array $filelist The array of file names
  69. *
  70. * @return void
  71. */
  72. public function addFiles(array $filelist)
  73. {
  74. foreach ($filelist as $filename) {
  75. $this->addFile($filename);
  76. }
  77. }
  78. /**
  79. * Determines whether to fire up an onload event for a file
  80. *
  81. * @param string $filename The name of the file to be checked
  82. * against the exclude list.
  83. *
  84. * @return int 1 to fire up the event, 0 not to
  85. */
  86. private function hasOnloadEvent($filename)
  87. {
  88. if (strpos($filename, 'jquery') !== false
  89. || strpos($filename, 'codemirror') !== false
  90. || strpos($filename, 'messages.php') !== false
  91. || strpos($filename, 'ajax.js') !== false
  92. || strpos($filename, 'cross_framing_protection.js') !== false
  93. ) {
  94. return 0;
  95. }
  96. return 1;
  97. }
  98. /**
  99. * Adds a new code snippet to the code to be executed
  100. *
  101. * @param string $code The JS code to be added
  102. *
  103. * @return void
  104. */
  105. public function addCode($code)
  106. {
  107. $this->code .= $code . "\n";
  108. }
  109. /**
  110. * Returns a list with filenames and a flag to indicate
  111. * whether to register onload events for this file
  112. *
  113. * @return array
  114. */
  115. public function getFiles()
  116. {
  117. $retval = [];
  118. foreach ($this->files as $file) {
  119. //If filename contains a "?", continue.
  120. if (strpos($file['filename'], '?') !== false) {
  121. continue;
  122. }
  123. $retval[] = [
  124. 'name' => $file['filename'],
  125. 'fire' => $file['has_onload'],
  126. ];
  127. }
  128. return $retval;
  129. }
  130. /**
  131. * Renders all the JavaScript file inclusions, code and events
  132. *
  133. * @return string
  134. */
  135. public function getDisplay()
  136. {
  137. $baseDir = defined('PMA_PATH_TO_BASEDIR') ? PMA_PATH_TO_BASEDIR : '';
  138. return $this->template->render('scripts', [
  139. 'base_dir' => $baseDir,
  140. 'files' => $this->files,
  141. 'version' => PMA_VERSION,
  142. 'code' => $this->code,
  143. ]);
  144. }
  145. }