Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / Scripts.php
blob2c8e9ecae945350d160e2c1385dc16749d4b9cf6
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * JavaScript management
6 * @package PhpMyAdmin
7 */
8 namespace PMA\libraries;
10 use PMA\libraries\URL;
11 use PMA\libraries\Sanitize;
13 /**
14 * Collects information about which JavaScript
15 * files and objects are necessary to render
16 * the page and generates the relevant code.
18 * @package PhpMyAdmin
20 class Scripts
22 /**
23 * An array of SCRIPT tags
25 * @access private
26 * @var array of strings
28 private $_files;
29 /**
30 * An array of discrete javascript code snippets
32 * @access private
33 * @var array of strings
35 private $_code;
37 /**
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 = "";
48 $scripts = array();
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>";
58 } else {
59 $dynamic_scripts .= "<script data-cfasync='false' "
60 . "type='text/javascript' src='js/" . $file_name
61 . "'></script>";
63 continue;
65 $include = true;
66 if ($include) {
67 $scripts[] = "scripts%5B%5D=" . $value['filename'];
70 $separator = URL::getArgSeparator();
71 $static_scripts = '';
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">' .
82 '</script>',
83 htmlspecialchars($url)
86 return $first_dynamic_scripts . $static_scripts . $dynamic_scripts;
89 /**
90 * Generates new Scripts objects
93 public function __construct()
95 $this->_files = array();
96 $this->_code = '';
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
107 * @return void
109 public function addFile(
110 $filename,
111 $before_statics = false
113 $hash = md5($filename);
114 if (!empty($this->_files[$hash])) {
115 return;
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
131 * @return void
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
157 return 0;
160 return 1;
164 * Adds a new code snippet to the code to be executed
166 * @param string $code The JS code to be added
168 * @return void
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
179 * @return array
181 public function getFiles()
183 $retval = array();
184 foreach ($this->_files as $file) {
185 //If filename contains a "?", continue.
186 if (strpos($file['filename'], "?") !== false) {
187 continue;
189 $retval[] = array(
190 'name' => $file['filename'],
191 'fire' => $file['has_onload']
195 return $retval;
199 * Renders all the JavaScript file inclusions, code and events
201 * @return string
203 public function getDisplay()
205 $retval = '';
207 if (count($this->_files) > 0) {
208 $retval .= $this->_includeFiles(
209 $this->_files
213 $code = 'AJAX.scriptHandler';
214 foreach ($this->_files as $file) {
215 $code .= sprintf(
216 '.add("%s",%d)',
217 Sanitize::escapeJsString($file['filename']),
218 $file['has_onload'] ? 1 : 0
221 $code .= ';';
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']);
229 $code .= '");';
232 $code .= '});';
233 $this->addCode($code);
235 $retval .= '<script data-cfasync="false" type="text/javascript">';
236 $retval .= "// <![CDATA[\n";
237 $retval .= $this->_code;
238 $retval .= '// ]]>';
239 $retval .= '</script>';
241 return $retval;