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

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

[tools] PHP Optimisateur :

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