source: extensions/PHP_Optimisateur/files/functions.php @ 8226

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