Scripts.class.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * JavaScript management
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Collects information about which JavaScript
  13. * files and objects are necessary to render
  14. * the page and generates the relevant code.
  15. *
  16. * @package PhpMyAdmin
  17. */
  18. class PMA_Scripts
  19. {
  20. /**
  21. * An array of SCRIPT tags
  22. *
  23. * @access private
  24. * @var array of strings
  25. */
  26. private $_files;
  27. /**
  28. * An array of discrete javascript code snippets
  29. *
  30. * @access private
  31. * @var array of strings
  32. */
  33. private $_code;
  34. /**
  35. * An array of event names to bind and javascript code
  36. * snippets to fire for the corresponding events
  37. *
  38. * @access private
  39. * @var array
  40. */
  41. private $_events;
  42. /**
  43. * Returns HTML code to include javascript file.
  44. *
  45. * @param array $files The list of js file to include
  46. *
  47. * @return string HTML code for javascript inclusion.
  48. */
  49. private function _includeFiles($files)
  50. {
  51. $first_dynamic_scripts = "";
  52. $dynamic_scripts = "";
  53. $params = array();
  54. foreach ($files as $value) {
  55. if (strpos($value['filename'], "?") === false) {
  56. $include = true;
  57. if ($value['conditional_ie'] !== false && PMA_USR_BROWSER_AGENT === 'IE') {
  58. if ($value['conditional_ie'] === true) {
  59. $include = true;
  60. } else if ($value['conditional_ie'] == PMA_USR_BROWSER_VER) {
  61. $include = true;
  62. } else {
  63. $include = false;
  64. }
  65. }
  66. if ($include) {
  67. $params[] = "scripts[]=" . $value['filename'];
  68. }
  69. } else {
  70. if ($value['before_statics'] === true) {
  71. $first_dynamic_scripts .= "<script type='text/javascript' src='js/" . $value['filename'] . "'></script>";
  72. } else {
  73. $dynamic_scripts .= "<script type='text/javascript' src='js/" . $value['filename'] . "'></script>";
  74. }
  75. }
  76. }
  77. $static_scripts = '';
  78. // Using chunks of 10 files to avoid too long URLs
  79. $script_chunks = array_chunk($params, 10);
  80. foreach ($script_chunks as $script_chunk) {
  81. $url = 'js/get_scripts.js.php?' . implode('&', $script_chunk);
  82. $static_scripts .= sprintf(
  83. '<script type="text/javascript" src="%s"></script>',
  84. htmlspecialchars($url)
  85. );
  86. }
  87. return $first_dynamic_scripts . $static_scripts . $dynamic_scripts;
  88. }
  89. /**
  90. * Generates new PMA_Scripts objects
  91. *
  92. * @return PMA_Scripts object
  93. */
  94. public function __construct()
  95. {
  96. $this->_files = array();
  97. $this->_code = '';
  98. $this->_events = array();
  99. }
  100. /**
  101. * Adds a new file to the list of scripts
  102. *
  103. * @param string $filename The name of the file to include
  104. * @param bool $conditional_ie Whether to wrap the script tag in
  105. * conditional comments for IE
  106. * @param bool $before_statics Whether this dynamic script should be
  107. * include before the static ones
  108. *
  109. * @return void
  110. */
  111. public function addFile($filename, $conditional_ie = false, $before_statics = false)
  112. {
  113. $hash = md5($filename);
  114. if (empty($this->_files[$hash])) {
  115. $has_onload = $this->_eventBlacklist($filename);
  116. $this->_files[$hash] = array(
  117. 'has_onload' => $has_onload,
  118. 'filename' => $filename,
  119. 'conditional_ie' => $conditional_ie,
  120. 'before_statics' => $before_statics
  121. );
  122. }
  123. }
  124. /**
  125. * Determines whether to fire up an onload event for a file
  126. *
  127. * @param string $filename The name of the file to be checked
  128. * against the blacklist
  129. *
  130. * @return int 1 to fire up the event, 0 not to
  131. */
  132. private function _eventBlacklist($filename)
  133. {
  134. if ( strpos($filename, 'jquery') !== false
  135. || strpos($filename, 'codemirror') !== false
  136. || strpos($filename, 'messages.php') !== false
  137. || strpos($filename, 'ajax.js') !== false
  138. || strpos($filename, 'navigation.js') !== false
  139. || strpos($filename, 'get_image.js.php') !== false
  140. || strpos($filename, 'cross_framing_protection.js') !== false
  141. ) {
  142. return 0;
  143. } else {
  144. return 1;
  145. }
  146. }
  147. /**
  148. * Adds a new code snippet to the code to be executed
  149. *
  150. * @param string $code The JS code to be added
  151. *
  152. * @return void
  153. */
  154. public function addCode($code)
  155. {
  156. $this->_code .= "$code\n";
  157. }
  158. /**
  159. * Adds a new event to the list of events
  160. *
  161. * @param string $event The name of the event to register
  162. * @param string $function The code to execute when the event fires
  163. * E.g: 'function () { doSomething(); }'
  164. * or 'doSomething'
  165. *
  166. * @return void
  167. */
  168. public function addEvent($event, $function)
  169. {
  170. $this->_events[] = array(
  171. 'event' => $event,
  172. 'function' => $function
  173. );
  174. }
  175. /**
  176. * Returns a list with filenames and a flag to indicate
  177. * whether to register onload events for this file
  178. *
  179. * @return array
  180. */
  181. public function getFiles()
  182. {
  183. $retval = array();
  184. foreach ($this->_files as $file) {
  185. if (strpos($file['filename'], "?") === false) {
  186. if (! $file['conditional_ie'] || PMA_USR_BROWSER_AGENT == 'IE') {
  187. $retval[] = array(
  188. 'name' => $file['filename'],
  189. 'fire' => $file['has_onload']
  190. );
  191. }
  192. }
  193. }
  194. return $retval;
  195. }
  196. /**
  197. * Renders all the JavaScript file inclusions, code and events
  198. *
  199. * @return string
  200. */
  201. public function getDisplay()
  202. {
  203. $retval = '';
  204. if (count($this->_files) > 0) {
  205. $retval .= $this->_includeFiles(
  206. $this->_files
  207. );
  208. }
  209. $code = 'AJAX.scriptHandler';
  210. foreach ($this->_files as $file) {
  211. $code .= sprintf(
  212. '.add("%s",%d)',
  213. PMA_escapeJsString($file['filename']),
  214. $file['has_onload'] ? 1 : 0
  215. );
  216. }
  217. $code .= ';';
  218. $this->addCode($code);
  219. $code = '$(function() {';
  220. foreach ($this->_files as $file) {
  221. if ($file['has_onload']) {
  222. $code .= 'AJAX.fireOnload("';
  223. $code .= PMA_escapeJsString($file['filename']);
  224. $code .= '");';
  225. }
  226. }
  227. $code .= '});';
  228. $this->addCode($code);
  229. $retval .= '<script type="text/javascript">';
  230. $retval .= "// <![CDATA[\n";
  231. $retval .= $this->_code;
  232. foreach ($this->_events as $js_event) {
  233. $retval .= sprintf(
  234. "$(window).bind('%s', %s);\n",
  235. $js_event['event'],
  236. $js_event['function']
  237. );
  238. }
  239. $retval .= '// ]]>';
  240. $retval .= '</script>';
  241. return $retval;
  242. }
  243. }