2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * JavaScript management
8 namespace PMA\libraries
;
10 use PMA\libraries\URL
;
11 use PMA\libraries\Sanitize
;
14 * Collects information about which JavaScript
15 * files and objects are necessary to render
16 * the page and generates the relevant code.
23 * An array of SCRIPT tags
26 * @var array of strings
30 * An array of discrete javascript code snippets
33 * @var array of strings
38 * Returns HTML code to include javascript file.
40 * @param array $files The list of js file to include
42 * @return string HTML code for javascript inclusion.
44 private function _includeFiles($files)
46 $first_dynamic_scripts = "";
47 $dynamic_scripts = "";
49 $separator = URL
::getArgSeparator();
50 foreach ($files as $value) {
51 if (mb_strpos($value['filename'], "?") !== false) {
52 $file_name = $value['filename'] . $separator
53 . Header
::getVersionParameter();
54 if ($value['before_statics'] === true) {
55 $first_dynamic_scripts
56 .= "<script data-cfasync='false' type='text/javascript' "
57 . "src='js/" . $file_name . "'></script>";
59 $dynamic_scripts .= "<script data-cfasync='false' "
60 . "type='text/javascript' src='js/" . $file_name
67 $scripts[] = "scripts%5B%5D=" . $value['filename'];
70 $separator = URL
::getArgSeparator();
72 // Using chunks of 10 files to avoid too long URLs
73 // as some servers are set to 512 bytes URL limit
74 $script_chunks = array_chunk($scripts, 10);
75 foreach ($script_chunks as $script_chunk) {
76 $url = 'js/get_scripts.js.php?'
77 . implode($separator, $script_chunk)
78 . $separator . Header
::getVersionParameter();
80 $static_scripts .= sprintf(
81 '<script data-cfasync="false" type="text/javascript" src="%s">' .
83 htmlspecialchars($url)
86 return $first_dynamic_scripts . $static_scripts . $dynamic_scripts;
90 * Generates new Scripts objects
93 public function __construct()
95 $this->_files
= array();
101 * Adds a new file to the list of scripts
103 * @param string $filename The name of the file to include
104 * @param bool $before_statics Whether this dynamic script should be
105 * included before the static ones
109 public function addFile(
111 $before_statics = false
113 $hash = md5($filename);
114 if (!empty($this->_files
[$hash])) {
118 $has_onload = $this->_eventBlacklist($filename);
119 $this->_files
[$hash] = array(
120 'has_onload' => $has_onload,
121 'filename' => $filename,
122 'before_statics' => $before_statics
127 * Add new files to the list of scripts
129 * @param array $filelist The array of file names
133 public function addFiles($filelist)
135 foreach ($filelist as $filename) {
136 $this->addFile($filename);
141 * Determines whether to fire up an onload event for a file
143 * @param string $filename The name of the file to be checked
144 * against the blacklist
146 * @return int 1 to fire up the event, 0 not to
148 private function _eventBlacklist($filename)
150 if (strpos($filename, 'jquery') !== false
151 ||
strpos($filename, 'codemirror') !== false
152 ||
strpos($filename, 'messages.php') !== false
153 ||
strpos($filename, 'ajax.js') !== false
154 ||
strpos($filename, 'get_image.js.php') !== false
155 ||
strpos($filename, 'cross_framing_protection.js') !== false
164 * Adds a new code snippet to the code to be executed
166 * @param string $code The JS code to be added
170 public function addCode($code)
172 $this->_code
.= "$code\n";
176 * Returns a list with filenames and a flag to indicate
177 * whether to register onload events for this file
181 public function getFiles()
184 foreach ($this->_files
as $file) {
185 //If filename contains a "?", continue.
186 if (strpos($file['filename'], "?") !== false) {
190 'name' => $file['filename'],
191 'fire' => $file['has_onload']
199 * Renders all the JavaScript file inclusions, code and events
203 public function getDisplay()
207 if (count($this->_files
) > 0) {
208 $retval .= $this->_includeFiles(
213 $code = 'AJAX.scriptHandler';
214 foreach ($this->_files
as $file) {
217 Sanitize
::escapeJsString($file['filename']),
218 $file['has_onload'] ?
1 : 0
222 $this->addCode($code);
224 $code = '$(function() {';
225 foreach ($this->_files
as $file) {
226 if ($file['has_onload']) {
227 $code .= 'AJAX.fireOnload("';
228 $code .= Sanitize
::escapeJsString($file['filename']);
233 $this->addCode($code);
235 $retval .= '<script data-cfasync="false" type="text/javascript">';
236 $retval .= "// <![CDATA[\n";
237 $retval .= $this->_code
;
239 $retval .= '</script>';