Scripts.class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. $dynamic_scripts = "";
  52. $scripts = array();
  53. foreach ($files as $value) {
  54. if (strpos($value['filename'], "?") !== false) {
  55. $dynamic_scripts .= "<script type='text/javascript' src='js/"
  56. . $value['filename'] . "'></script>";
  57. continue;
  58. }
  59. $include = true;
  60. if ($value['conditional_ie'] !== false
  61. && PMA_USR_BROWSER_AGENT === 'IE'
  62. ) {
  63. if ($value['conditional_ie'] === true) {
  64. $include = true;
  65. } else if ($value['conditional_ie'] == PMA_USR_BROWSER_VER) {
  66. $include = true;
  67. } else {
  68. $include = false;
  69. }
  70. }
  71. if ($include) {
  72. $scripts[] = "scripts[]=" . $value['filename'];
  73. }
  74. }
  75. $separator = PMA_URL_getArgSeparator();
  76. $url = 'js/get_scripts.js.php'
  77. . PMA_URL_getCommon(array(), 'none')
  78. . $separator . implode($separator, $scripts);
  79. $static_scripts = sprintf(
  80. '<script type="text/javascript" src="%s"></script>',
  81. htmlspecialchars($url)
  82. );
  83. return $static_scripts . $dynamic_scripts;
  84. }
  85. /**
  86. * Generates new PMA_Scripts objects
  87. *
  88. * @return PMA_Scripts object
  89. */
  90. public function __construct()
  91. {
  92. $this->_files = array();
  93. $this->_code = '';
  94. $this->_events = array();
  95. }
  96. /**
  97. * Adds a new file to the list of scripts
  98. *
  99. * @param string $filename The name of the file to include
  100. * @param bool $conditional_ie Whether to wrap the script tag in
  101. * conditional comments for IE
  102. *
  103. * @return void
  104. */
  105. public function addFile($filename, $conditional_ie = false)
  106. {
  107. $hash = md5($filename);
  108. if (!empty($this->_files[$hash])) {
  109. return;
  110. }
  111. $has_onload = $this->_eventBlacklist($filename);
  112. $this->_files[$hash] = array(
  113. 'has_onload' => $has_onload,
  114. 'filename' => $filename,
  115. 'conditional_ie' => $conditional_ie
  116. );
  117. }
  118. /**
  119. * Determines whether to fire up an onload event for a file
  120. *
  121. * @param string $filename The name of the file to be checked
  122. * against the blacklist
  123. *
  124. * @return int 1 to fire up the event, 0 not to
  125. */
  126. private function _eventBlacklist($filename)
  127. {
  128. if ( strpos($filename, 'jquery') !== false
  129. || strpos($filename, 'codemirror') !== false
  130. || strpos($filename, 'messages.php') !== false
  131. || strpos($filename, 'ajax.js') !== false
  132. || strpos($filename, 'navigation.js') !== false
  133. || strpos($filename, 'get_image.js.php') !== false
  134. || strpos($filename, 'cross_framing_protection.js') !== false
  135. ) {
  136. return 0;
  137. }
  138. return 1;
  139. }
  140. /**
  141. * Adds a new code snippet to the code to be executed
  142. *
  143. * @param string $code The JS code to be added
  144. *
  145. * @return void
  146. */
  147. public function addCode($code)
  148. {
  149. $this->_code .= "$code\n";
  150. }
  151. /**
  152. * Adds a new event to the list of events
  153. *
  154. * @param string $event The name of the event to register
  155. * @param string $function The code to execute when the event fires
  156. * E.g: 'function () { doSomething(); }'
  157. * or 'doSomething'
  158. *
  159. * @return void
  160. */
  161. public function addEvent($event, $function)
  162. {
  163. $this->_events[] = array(
  164. 'event' => $event,
  165. 'function' => $function
  166. );
  167. }
  168. /**
  169. * Returns a list with filenames and a flag to indicate
  170. * whether to register onload events for this file
  171. *
  172. * @return array
  173. */
  174. public function getFiles()
  175. {
  176. $retval = array();
  177. foreach ($this->_files as $file) {
  178. //If filename contains a "?", continue.
  179. if (strpos($file['filename'], "?") !== false) {
  180. continue;
  181. }
  182. if (! $file['conditional_ie'] || PMA_USR_BROWSER_AGENT == 'IE') {
  183. $retval[] = array(
  184. 'name' => $file['filename'],
  185. 'fire' => $file['has_onload']
  186. );
  187. }
  188. }
  189. return $retval;
  190. }
  191. /**
  192. * Renders all the JavaScript file inclusions, code and events
  193. *
  194. * @return string
  195. */
  196. public function getDisplay()
  197. {
  198. $retval = '';
  199. if (count($this->_files) > 0) {
  200. $retval .= $this->_includeFiles(
  201. $this->_files
  202. );
  203. }
  204. $code = 'AJAX.scriptHandler';
  205. foreach ($this->_files as $file) {
  206. $code .= sprintf(
  207. '.add("%s",%d)',
  208. PMA_escapeJsString($file['filename']),
  209. $file['has_onload'] ? 1 : 0
  210. );
  211. }
  212. $code .= ';';
  213. $this->addCode($code);
  214. $code = '$(function() {';
  215. foreach ($this->_files as $file) {
  216. if ($file['has_onload']) {
  217. $code .= 'AJAX.fireOnload("';
  218. $code .= PMA_escapeJsString($file['filename']);
  219. $code .= '");';
  220. }
  221. }
  222. $code .= '});';
  223. $this->addCode($code);
  224. $retval .= '<script type="text/javascript">';
  225. $retval .= "// <![CDATA[\n";
  226. $retval .= $this->_code;
  227. foreach ($this->_events as $js_event) {
  228. $retval .= sprintf(
  229. "$(window).bind('%s', %s);\n",
  230. $js_event['event'],
  231. $js_event['function']
  232. );
  233. }
  234. $retval .= '// ]]>';
  235. $retval .= '</script>';
  236. return $retval;
  237. }
  238. }