source: extensions/PHP_Optimisateur/include/functions.php @ 9265

Last change on this file since 9265 was 9265, checked in by mistic100, 13 years ago

[extensions] PHP Optimisateur

  • change language management
  • correct english and letton language file
File size: 7.6 KB
Line 
1<?php
2// Affiche les textes localisés
3function l10n($code) {
4        global $Lang;
5        if (isset($Lang[$code])) {
6                $args = func_get_args();
7                if (count($args) > 1) {
8                        array_shift($args);
9                        return vsprintf($Lang[$code], $args);
10                } else {
11                        return $Lang[$code];
12                }
13        } else {
14                return $code;
15        }
16}
17
18// Retourne la langue de l'utilisateur et les langues installées
19function GetLanguage() {
20        // tableau avec les langues installées
21        $dh = opendir('language');
22        while ($file = readdir($dh)) {
23                if ($file !== '.' && $file !== '..' && is_dir('language/'.$file)) {
24                        $name = (file_exists('language/'.$file.'/'.$file.'.txt')) ? file_get_contents('language/'.$file.'/'.$file.'.txt') : $file;
25                        $languages[$file] = $name;
26                }
27        }
28        closedir($dh);
29       
30        // recherche du paramètre get ou session, ou langue du navigateur
31        if (isset($_GET['Lang'])) {
32                $user_lang = $_GET['Lang'];
33        } else if (isset($_SESSION['Lang']) AND $_SESSION['Lang'] != NULL) {
34                $user_lang = $_SESSION['Lang'];
35        } else {
36                $user_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2);
37        }
38       
39        // si langue navigateur cherche une correspondance dans la tableau
40        if (strlen($user_lang) == 2) {
41                foreach (array_keys($languages) as $lang) {
42                        if ($user_lang == substr($lang,0,2)) {
43                                $user_lang = $lang;
44                                break;
45                        }
46                }
47        }
48       
49        // si pas de correspondance OU get faux, langue par défaut
50        if (!in_array($user_lang, array_keys($languages))) {
51                $user_lang = 'en_UK';
52        }
53       
54        $_SESSION['Lang'] = $user_lang;
55        return array('user_lang' => $_SESSION['Lang'], 'languages' => $languages);
56}
57
58// Supprime tous les caractères speciaux
59function delete_special_car($string) {
60        global $CONF;
61       
62        $search = array ('#[éèêë]#','#[ÊËÈÉ]#','#[àâäã]#','#[ÂÄÁÀ]#','#[îïíì]#','#[ÎÏÍÌ]#','#[ûùüúµ]#','#[ÛÙÚÜ]#','#[ôóòõö]#','#[ÓÒÖÔÕ]#','#[ç]#','#[Ç]#','#[ñ]#','#[Ñ]#','#[ýÿ]#','#[Ý]#','#[ \&\!\?\#\"\(\)\{\}\[\]\=\+\~\%]#','#[^a-zA-Z0-9_\.\-]#');
63        $replace = array ('e','E','a','A','i','I','u','U','o','O','c','C',',n','N','y','Y','_','');
64       
65        if ($CONF['renameORNOT']) {
66                return preg_replace($search, $replace, $string);
67        } else {
68                return str_replace(array('%','$'), '_', $string);
69        }
70}
71
72// Teste si un dossier est vide
73function is_dir_empty($dir) {
74        $array = scandir($dir);
75        if (count($array) > 2) {
76                return false;
77        } else {
78                return true;
79        }
80}
81
82// Supprime un repertoire non-vide
83function rrmdir($dir) {
84        $dir = rtrim($dir, '/');
85        $objects = scandir($dir);
86       
87        foreach ($objects as $object) {
88                if ($object !== '.' && $object !== '..') {
89                        $path = $dir.'/'.$object;
90                        if (filetype($path) == 'dir') rrmdir($path); 
91                        else unlink($path);
92                }
93        }
94
95        rmdir($dir);
96} 
97
98// Parcours récursivement un dossier
99function recursive_readdir($dir, $array, $dirs=false) {
100        # si $dirs=true liste les dossiers plutot que les fichiers
101        global ${$array}; // Charge la tableau pour la sortie, déclaré à l'exterieur à cause de l'appel recursif de la fonction
102        $dir = rtrim($dir, '/');
103        $dh = opendir($dir);
104       
105        while (($file = readdir($dh)) !== false ) {
106                if ($file !== '.' && $file !== '..') {
107                        $path = $dir.'/'.$file;
108                        if (is_dir($path)) {
109                                if($dirs) ${$array}[] = $path;
110                                recursive_readdir($path, $array, $dirs);
111                        } else {
112                                if(!$dirs) ${$array}[] = $path;
113                        }
114                }
115        }
116        closedir($dh);
117}
118
119// Parse le fichier de configuration
120function XMLParse($xml) {
121        $content = array();
122        foreach ($xml as $nom => $elem) { 
123                if (trim($elem) == '') { 
124                        $content[$nom] = XMLParse($elem->children()); 
125                } else { 
126                        $content[$nom] = utf8_encode(utf8_decode($elem));
127                }
128        }
129        return $content;
130}
131
132// Crée le fichier de configuration
133function XMLCreate($array, $level=0) {
134        $content = null;
135        foreach ($array as $nom => $elem) {
136                if (is_array($elem)) {
137                        for($i=0;$i<=$level;$i++) $content .= "\t";
138                        $content .= '<'.$nom.'>'."\n";
139                        $content .= XMLCreate($elem, $level+1);
140                        for($i=0;$i<=$level;$i++) $content .= "\t";
141                        $content .= '</'.$nom.'>'."\n";
142                } else {
143                        for($i=0;$i<=$level;$i++) $content .= "\t";
144                        $content .= '<'.$nom.'>'.$elem.'</'.$nom.'>'."\n";
145                }
146        }
147        return $content;
148}
149
150// Charge la configuration
151function load_config($file) {
152        $config = simplexml_load_file($file);
153        $config = XMLParse($config);
154        $config = array_settype($config);
155        return $config;
156}
157
158// Convertit les booléens, entiers et flotants d'un tableau de 'string'
159function array_settype($array) {
160        foreach ($array as $key => $value) {
161                if (is_array($value)) {
162                        $array[$key] = array_settype($value);
163                } else {
164                        if ($value === 'true') {
165                                $array[$key] = true;           
166                        } else if ($value === 'false') {
167                                $array[$key] = false;           
168                        } else if (preg_match('#^([0-9]*)$#', $value)) {
169                                settype($array[$key], 'int');           
170                        } else if (preg_match('#^([0-9]*)(\.)([0-9]*)$#', $value)) {
171                                settype($array[$key], 'float');         
172                        }
173                }
174        }
175               
176        return $array;
177}
178
179// Booléen vers français ou texte
180function bool_to_string($string, $just_echo=0) {
181        global $Lang;
182        # $just_echo pour pouvoir afficher un booléen tel quel (echo true; n'affiche rien)
183        if (is_bool($string)) {
184                if ($string) {
185                        if ($just_echo) return 'true';
186                        else return l10n('yes');
187                } else {
188                        if ($just_echo) return 'false';
189                        else return l10n('no');
190                }
191        } else {
192                return $string;
193        }
194}
195
196// Converti une chaine de config (param:value;param:value[;]) en tableau associatif
197function ExplodeString($string, $sep1=';', $sep2=':') {
198        $string = preg_replace('#[;]$#', '', $string);
199        $result = array();
200        $a = explode($sep1, $string);
201       
202        foreach ($a as $s) {
203           $v = explode($sep2, $s);
204           $result[$v[0]] = $v[1];
205        }
206        return $result;
207}
208
209// Converti un tableau associatif en chaine de config (param:value;param:value;)
210function ImplodeArray($array, $sep1=';', $sep2=':') {
211        $result = null;
212        foreach ($array as $key => $value) {
213                $result .= $key.$sep2.$value.$sep1;
214        }
215        return $result;
216}
217
218// Convertit les couleurs 'numériques' et gère les 0 manquants
219function nice_hex_color($hex) {
220        $hex = strval($hex);
221        for ($i=strlen($hex); $i<6; $i++) {
222                $hex = '0'.$hex;
223        }
224        return $hex;
225}
226
227// Converti une couleur HEX et RGB
228function hex2RGB($hexStr, $returnAsString = false, $seperator = ',') {
229        $hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string
230       
231        if (strlen($hexStr) == 6) { //If a proper hex code, convert using bitwise operation. No overhead... faster
232                $colorVal = hexdec($hexStr);
233                $rgbArray['r'] = 0xFF & ($colorVal >> 0x10);
234                $rgbArray['g'] = 0xFF & ($colorVal >> 0x8);
235                $rgbArray['b'] = 0xFF & $colorVal;
236        } elseif (strlen($hexStr) == 3) { //if shorthand notation, need some string manipulations
237                $rgbArray['r'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
238                $rgbArray['g'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
239                $rgbArray['b'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
240        } else {
241                return false; //Invalid hex color code
242        }
243       
244        return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray; // returns the rgb string or the associative array
245}
246
247// Verifie si une chaine est un nombre décimal (plus strict que is_numeric qui fonctionne avec les hexadecimaux)
248function is_decimal($num) {
249        if(is_numeric($num) === true AND (float)$num == $num) {
250                return true;
251        } else {
252                return false;
253        }
254}
255
256// Charge le fichier de langue d'un plugin
257function load_plugin_lang($plugin_id) {
258        global $CONF, $Lang;
259        if (file_exists('plugins/'.$plugin_id.'/lang/'.$CONF['user_lang'].'.php')) {
260                include('plugins/'.$plugin_id.'/lang/'.$CONF['user_lang'].'.php');
261        } else {
262                include('plugins/'.$plugin_id.'/lang/en_UK.php');
263        }
264}
265?>
Note: See TracBrowser for help on using the repository browser.