Merge branch 'QA_3_3'
[phpmyadmin/dkf.git] / setup / lib / common.inc.php
blob2d1f1f69118ba00e355bd3fb65f39093cf028cec
1 <?php
2 /**
3 * Loads libraries/common.inc.php and preforms some additional actions
5 * @package phpMyAdmin-setup
6 * @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
7 * @version $Id$
8 */
10 /**
11 * Do not include full common.
12 * @ignore
14 define('PMA_MINIMUM_COMMON', TRUE);
15 define('PMA_SETUP', TRUE);
16 chdir('..');
18 if (!file_exists('./libraries/common.inc.php')) {
19 die('Bad invocation!');
22 require_once './libraries/common.inc.php';
23 require_once './libraries/url_generating.lib.php';
24 require_once './setup/lib/messages.inc.php';
25 require_once './setup/lib/ConfigFile.class.php';
27 // use default error handler
28 restore_error_handler();
30 // Save current language in a cookie, required since we use PMA_MINIMUM_COMMON
31 $GLOBALS['PMA_Config']->setCookie('pma_lang', $GLOBALS['lang']);
33 if (!isset($_SESSION['ConfigFile'])) {
34 $_SESSION['ConfigFile'] = array();
37 // allows for redirection even after sending some data
38 ob_start();
40 /**
41 * Returns value of an element in $array given by $path.
42 * $path is a string describing position of an element in an associative array,
43 * eg. Servers/1/host refers to $array[Servers][1][host]
45 * @param string $path
46 * @param array $array
47 * @param mixed $default
48 * @return mixed array element or $default
50 function array_read($path, $array, $default = null)
52 $keys = explode('/', $path);
53 $value =& $array;
54 foreach ($keys as $key) {
55 if (!isset($value[$key])) {
56 return $default;
58 $value =& $value[$key];
60 return $value;
63 /**
64 * Stores value in an array
66 * @param string $path
67 * @param array &$array
68 * @param mixed $value
70 function array_write($path, &$array, $value)
72 $keys = explode('/', $path);
73 $last_key = array_pop($keys);
74 $a =& $array;
75 foreach ($keys as $key) {
76 if (!isset($a[$key])) {
77 $a[$key] = array();
79 $a =& $a[$key];
81 $a[$last_key] = $value;
84 /**
85 * Removes value from an array
87 * @param string $path
88 * @param array &$array
89 * @param mixed $value
91 function array_remove($path, &$array)
93 $keys = explode('/', $path);
94 $keys_last = array_pop($keys);
95 $path = array();
96 $depth = 0;
98 $path[0] =& $array;
99 $found = true;
100 // go as deep as required or possible
101 foreach ($keys as $key) {
102 if (!isset($path[$depth][$key])) {
103 $found = false;
104 break;
106 $depth++;
107 $path[$depth] =& $path[$depth-1][$key];
109 // if element found, remove it
110 if ($found) {
111 unset($path[$depth][$keys_last]);
112 $depth--;
115 // remove empty nested arrays
116 for (; $depth >= 0; $depth--) {
117 if (!isset($path[$depth+1]) || count($path[$depth+1]) == 0) {
118 unset($path[$depth][$keys[$depth]]);
119 } else {
120 break;
126 * Returns sanitized language string, taking into account our special codes
127 * for formatting. Takes variable number of arguments.
128 * Based on PMA_sanitize from sanitize.lib.php.
130 * @param string $lang_key key in $GLOBALS WITHOUT 'strSetup' prefix
131 * @param mixed $args arguments for sprintf
132 * @return string
134 function PMA_lang($lang_key)
136 static $search, $replace;
138 // some quick cache'ing
139 if ($search === null) {
140 $replace_pairs = array(
141 '<' => '&lt;',
142 '>' => '&gt;',
143 '[em]' => '<em>',
144 '[/em]' => '</em>',
145 '[strong]' => '<strong>',
146 '[/strong]' => '</strong>',
147 '[code]' => '<code>',
148 '[/code]' => '</code>',
149 '[kbd]' => '<kbd>',
150 '[/kbd]' => '</kbd>',
151 '[br]' => '<br />',
152 '[sup]' => '<sup>',
153 '[/sup]' => '</sup>');
154 $search = array_keys($replace_pairs);
155 $replace = array_values($replace_pairs);
157 if (!isset($GLOBALS["strSetup$lang_key"])) {
158 return $lang_key;
160 $message = str_replace($search, $replace, $GLOBALS["strSetup$lang_key"]);
161 // replace [a@"$1"]$2[/a] with <a href="$1">$2</a>
162 $message = preg_replace('#\[a@("?)([^\]]+)\1\]([^\[]+)\[/a\]#e',
163 "PMA_lang_link_replace('$2', '$3')", $message);
165 if (func_num_args() == 1) {
166 return $message;
167 } else {
168 $args = func_get_args();
169 array_shift($args);
170 return vsprintf($message, $args);
175 * Returns translated field name
177 * @param string $canonical_path
178 * @return string
180 function PMA_lang_name($canonical_path)
182 $lang_key = str_replace(
183 array('Servers/1/', '/'),
184 array('Servers/', '_'),
185 $canonical_path) . '_name';
186 return isset($GLOBALS["strSetup$lang_key"])
187 ? $GLOBALS["strSetup$lang_key"]
188 : $lang_key;
192 * Returns translated field description
194 * @param string $canonical_path
195 * @return string
197 function PMA_lang_desc($canonical_path)
199 $lang_key = str_replace(
200 array('Servers/1/', '/'),
201 array('Servers/', '_'),
202 $canonical_path) . '_desc';
203 return isset($GLOBALS["strSetup$lang_key"])
204 ? PMA_lang($lang_key)
205 : '';
209 * Wraps link in &lt;a&gt; tags and replaces argument separator in internal links
210 * to the one returned by PMA_get_arg_separator()
212 * @param string $link
213 * @param string $text
214 * @return string
216 function PMA_lang_link_replace($link, $text)
218 static $separator;
220 if (!isset($separator)) {
221 $separator = PMA_get_arg_separator('html');
224 if (!preg_match('#^http://#', $link)) {
225 $link = str_replace('&amp;', $separator, $link);
228 return '<a href="' . $link . '">' . $text . '</a>';