Scripts.php 3.6 KB

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