Scripts.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * JavaScript management
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin;
  9. use PhpMyAdmin\Header;
  10. use PhpMyAdmin\Sanitize;
  11. use PhpMyAdmin\Url;
  12. /**
  13. * Collects information about which JavaScript
  14. * files and objects are necessary to render
  15. * the page and generates the relevant code.
  16. *
  17. * @package PhpMyAdmin
  18. */
  19. class Scripts
  20. {
  21. /**
  22. * An array of SCRIPT tags
  23. *
  24. * @access private
  25. * @var array of strings
  26. */
  27. private $_files;
  28. /**
  29. * An array of discrete javascript code snippets
  30. *
  31. * @access private
  32. * @var array of strings
  33. */
  34. private $_code;
  35. /**
  36. * Returns HTML code to include javascript file.
  37. *
  38. * @param array $files The list of js file to include
  39. *
  40. * @return string HTML code for javascript inclusion.
  41. */
  42. private function _includeFiles(array $files)
  43. {
  44. $result = '';
  45. foreach ($files as $value) {
  46. if (strpos($value['filename'], ".php") !== false) {
  47. $file_name = $value['filename'] . Url::getCommon($value['params'] + array('v' => PMA_VERSION));
  48. $result .= "<script data-cfasync='false' "
  49. . "type='text/javascript' src='js/" . $file_name
  50. . "'></script>\n";
  51. } else {
  52. $result .= '<script data-cfasync="false" type="text/javascript" src="js/'
  53. . $value['filename'] . '?' . Header::getVersionParameter() . '"></script>' . "\n";
  54. }
  55. }
  56. return $result;
  57. }
  58. /**
  59. * Generates new Scripts objects
  60. *
  61. */
  62. public function __construct()
  63. {
  64. $this->_files = array();
  65. $this->_code = '';
  66. }
  67. /**
  68. * Adds a new file to the list of scripts
  69. *
  70. * @param string $filename The name of the file to include
  71. * @param array $params Additional parameters to pass to the file
  72. *
  73. * @return void
  74. */
  75. public function addFile(
  76. $filename,
  77. array $params = array()
  78. ) {
  79. $hash = md5($filename);
  80. if (!empty($this->_files[$hash])) {
  81. return;
  82. }
  83. $has_onload = $this->_eventBlacklist($filename);
  84. $this->_files[$hash] = array(
  85. 'has_onload' => $has_onload,
  86. 'filename' => $filename,
  87. 'params' => $params,
  88. );
  89. }
  90. /**
  91. * Add new files to the list of scripts
  92. *
  93. * @param array $filelist The array of file names
  94. *
  95. * @return void
  96. */
  97. public function addFiles(array $filelist)
  98. {
  99. foreach ($filelist as $filename) {
  100. $this->addFile($filename);
  101. }
  102. }
  103. /**
  104. * Determines whether to fire up an onload event for a file
  105. *
  106. * @param string $filename The name of the file to be checked
  107. * against the blacklist
  108. *
  109. * @return int 1 to fire up the event, 0 not to
  110. */
  111. private function _eventBlacklist($filename)
  112. {
  113. if (strpos($filename, 'jquery') !== false
  114. || strpos($filename, 'codemirror') !== false
  115. || strpos($filename, 'messages.php') !== false
  116. || strpos($filename, 'ajax.js') !== false
  117. || strpos($filename, 'cross_framing_protection.js') !== false
  118. ) {
  119. return 0;
  120. }
  121. return 1;
  122. }
  123. /**
  124. * Adds a new code snippet to the code to be executed
  125. *
  126. * @param string $code The JS code to be added
  127. *
  128. * @return void
  129. */
  130. public function addCode($code)
  131. {
  132. $this->_code .= "$code\n";
  133. }
  134. /**
  135. * Returns a list with filenames and a flag to indicate
  136. * whether to register onload events for this file
  137. *
  138. * @return array
  139. */
  140. public function getFiles()
  141. {
  142. $retval = array();
  143. foreach ($this->_files as $file) {
  144. //If filename contains a "?", continue.
  145. if (strpos($file['filename'], "?") !== false) {
  146. continue;
  147. }
  148. $retval[] = array(
  149. 'name' => $file['filename'],
  150. 'fire' => $file['has_onload']
  151. );
  152. }
  153. return $retval;
  154. }
  155. /**
  156. * Renders all the JavaScript file inclusions, code and events
  157. *
  158. * @return string
  159. */
  160. public function getDisplay()
  161. {
  162. $retval = '';
  163. if (count($this->_files) > 0) {
  164. $retval .= $this->_includeFiles(
  165. $this->_files
  166. );
  167. }
  168. $code = 'AJAX.scriptHandler';
  169. foreach ($this->_files as $file) {
  170. $code .= sprintf(
  171. '.add("%s",%d)',
  172. Sanitize::escapeJsString($file['filename']),
  173. $file['has_onload'] ? 1 : 0
  174. );
  175. }
  176. $code .= ';';
  177. $this->addCode($code);
  178. $code = '$(function() {';
  179. foreach ($this->_files as $file) {
  180. if ($file['has_onload']) {
  181. $code .= 'AJAX.fireOnload("';
  182. $code .= Sanitize::escapeJsString($file['filename']);
  183. $code .= '");';
  184. }
  185. }
  186. $code .= '});';
  187. $this->addCode($code);
  188. $retval .= '<script data-cfasync="false" type="text/javascript">';
  189. $retval .= "// <![CDATA[\n";
  190. $retval .= $this->_code;
  191. $retval .= '// ]]>';
  192. $retval .= '</script>';
  193. return $retval;
  194. }
  195. }