3.3.5-rc1
[phpmyadmin/arisferyanto.git] / setup / lib / common.inc.php
blob25f4a9c3ef261050758e11670e1961cbf5a59f2c
1 <?php
2 /**
3 * Loads libraries/common.inc.php and preforms some additional actions
5 * @package phpMyAdmin-setup
6 * @author Piotr Przybylski <piotrprz@gmail.com>
7 * @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
8 * @version $Id$
9 */
11 /**
12 * Do not include full common.
13 * @ignore
15 define('PMA_MINIMUM_COMMON', TRUE);
16 define('PMA_SETUP', TRUE);
17 chdir('..');
19 require_once './libraries/common.inc.php';
20 require_once './libraries/url_generating.lib.php';
21 require_once './setup/lib/ConfigFile.class.php';
23 // use default error handler
24 restore_error_handler();
26 // Save current language in a cookie, required since we use PMA_MINIMUM_COMMON
27 PMA_setCookie('pma_lang', $GLOBALS['lang']);
29 if (!isset($_SESSION['ConfigFile'])) {
30 $_SESSION['ConfigFile'] = array();
33 // allows for redirection even after sending some data
34 ob_start();
36 /**
37 * Returns value of an element in $array given by $path.
38 * $path is a string describing position of an element in an associative array,
39 * eg. Servers/1/host refers to $array[Servers][1][host]
41 * @param string $path
42 * @param array $array
43 * @param mixed $default
44 * @return mixed array element or $default
46 function array_read($path, $array, $default = null)
48 $keys = explode('/', $path);
49 $value =& $array;
50 foreach ($keys as $key) {
51 if (!isset($value[$key])) {
52 return $default;
54 $value =& $value[$key];
56 return $value;
59 /**
60 * Stores value in an array
62 * @param string $path
63 * @param array &$array
64 * @param mixed $value
66 function array_write($path, &$array, $value)
68 $keys = explode('/', $path);
69 $last_key = array_pop($keys);
70 $a =& $array;
71 foreach ($keys as $key) {
72 if (!isset($a[$key])) {
73 $a[$key] = array();
75 $a =& $a[$key];
77 $a[$last_key] = $value;
80 /**
81 * Removes value from an array
83 * @param string $path
84 * @param array &$array
85 * @param mixed $value
87 function array_remove($path, &$array)
89 $keys = explode('/', $path);
90 $keys_last = array_pop($keys);
91 $path = array();
92 $depth = 0;
94 $path[0] =& $array;
95 $found = true;
96 // go as deep as required or possible
97 foreach ($keys as $key) {
98 if (!isset($path[$depth][$key])) {
99 $found = false;
100 break;
102 $depth++;
103 $path[$depth] =& $path[$depth-1][$key];
105 // if element found, remove it
106 if ($found) {
107 unset($path[$depth][$keys_last]);
108 $depth--;
111 // remove empty nested arrays
112 for (; $depth >= 0; $depth--) {
113 if (!isset($path[$depth+1]) || count($path[$depth+1]) == 0) {
114 unset($path[$depth][$keys[$depth]]);
115 } else {
116 break;
122 * Returns sanitized language string, taking into account our special codes
123 * for formatting. Takes variable number of arguments.
124 * Based on PMA_sanitize from sanitize.lib.php.
126 * @param string $lang_key key in $GLOBALS WITHOUT 'strSetup' prefix
127 * @param mixed $args arguments for sprintf
128 * @return string
130 function PMA_lang($lang_key)
132 static $search, $replace;
134 // some quick cache'ing
135 if ($search === null) {
136 $replace_pairs = array(
137 '<' => '&lt;',
138 '>' => '&gt;',
139 '[em]' => '<em>',
140 '[/em]' => '</em>',
141 '[strong]' => '<strong>',
142 '[/strong]' => '</strong>',
143 '[code]' => '<code>',
144 '[/code]' => '</code>',
145 '[kbd]' => '<kbd>',
146 '[/kbd]' => '</kbd>',
147 '[br]' => '<br />',
148 '[sup]' => '<sup>',
149 '[/sup]' => '</sup>');
150 $search = array_keys($replace_pairs);
151 $replace = array_values($replace_pairs);
153 if (!isset($GLOBALS["strSetup$lang_key"])) {
154 return $lang_key;
156 $message = str_replace($search, $replace, $GLOBALS["strSetup$lang_key"]);
157 // replace [a@"$1"]$2[/a] with <a href="$1">$2</a>
158 $message = preg_replace('#\[a@("?)([^\]]+)\1\]([^\[]+)\[/a\]#e',
159 "PMA_lang_link_replace('$2', '$3')", $message);
161 if (func_num_args() == 1) {
162 return $message;
163 } else {
164 $args = func_get_args();
165 array_shift($args);
166 return vsprintf($message, $args);
171 * Returns translated field name
173 * @param string $canonical_path
174 * @return string
176 function PMA_lang_name($canonical_path)
178 $lang_key = str_replace(
179 array('Servers/1/', '/'),
180 array('Servers/', '_'),
181 $canonical_path) . '_name';
182 return isset($GLOBALS["strSetup$lang_key"])
183 ? $GLOBALS["strSetup$lang_key"]
184 : $lang_key;
188 * Returns translated field description
190 * @param string $canonical_path
191 * @return string
193 function PMA_lang_desc($canonical_path)
195 $lang_key = str_replace(
196 array('Servers/1/', '/'),
197 array('Servers/', '_'),
198 $canonical_path) . '_desc';
199 return isset($GLOBALS["strSetup$lang_key"])
200 ? PMA_lang($lang_key)
201 : '';
205 * Wraps link in &lt;a&gt; tags and replaces argument separator in internal links
206 * to the one returned by PMA_get_arg_separator()
208 * @param string $link
209 * @param string $text
210 * @return string
212 function PMA_lang_link_replace($link, $text)
214 static $separator;
216 if (!isset($separator)) {
217 $separator = PMA_get_arg_separator('html');
220 if (!preg_match('#^http://#', $link)) {
221 $link = str_replace('&amp;', $separator, $link);
224 return '<a href="' . $link . '">' . $text . '</a>';