Changeset 12819 for extensions/PHP_Optimisateur/include/functions.php
- Timestamp:
- Jan 1, 2012, 10:09:17 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/PHP_Optimisateur/include/functions.php
r10337 r12819 3 3 // Affiche les textes localisés 4 4 function l10n($code) { 5 6 7 8 9 10 11 12 13 14 15 16 5 global $Lang; 6 if (isset($Lang[$code])) { 7 $args = func_get_args(); 8 if (count($args) > 1) { 9 array_shift($args); 10 return vsprintf($Lang[$code], $args); 11 } else { 12 return $Lang[$code]; 13 } 14 } else { 15 return $code; 16 } 17 17 } 18 18 19 19 // Retourne la langue de l'utilisateur et les langues installées 20 20 function GetLanguage() { 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 21 // tableau avec les langues installées 22 $dh = opendir('language'); 23 while ($file = readdir($dh)) { 24 if ($file !== '.' && $file !== '..' && is_dir('language/'.$file)) { 25 $name = (file_exists('language/'.$file.'/iso.txt')) ? file_get_contents('language/'.$file.'/iso.txt') : $file; 26 $languages[$file] = $name; 27 } 28 } 29 closedir($dh); 30 31 // recherche du paramètre get ou session, ou langue du navigateur 32 if (isset($_GET['Lang'])) { 33 $user_lang = $_GET['Lang']; 34 } else if (isset($_SESSION['Lang']) AND $_SESSION['Lang'] != NULL) { 35 $user_lang = $_SESSION['Lang']; 36 } else { 37 $user_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2); 38 } 39 40 // si langue navigateur cherche une correspondance dans la tableau 41 if (strlen($user_lang) == 2) { 42 foreach (array_keys($languages) as $lang) { 43 if ($user_lang == substr($lang,0,2)) { 44 $user_lang = $lang; 45 break; 46 } 47 } 48 } 49 50 // si pas de correspondance OU get faux, langue par défaut 51 if (!in_array($user_lang, array_keys($languages))) { 52 $user_lang = 'en_UK'; 53 } 54 55 $_SESSION['Lang'] = $user_lang; 56 return array('user_lang' => $_SESSION['Lang'], 'languages' => $languages); 57 57 } 58 58 59 59 // Supprime tous les caractères speciaux 60 60 function delete_special_car($string) { 61 62 63 64 65 66 67 68 69 70 61 global $CONF; 62 63 $search = array ('#[éèêë]#','#[ÊËÈÉ]#','#[àâäã]#','#[ÂÄÁÀ]#','#[îïíì]#','#[ÎÏÍÌ]#','#[ûùüúµ]#','#[ÛÙÚÜ]#','#[ôóòõö]#','#[ÓÒÖÔÕ]#','#[ç]#','#[Ç]#','#[ñ]#','#[Ñ]#','#[ýÿ]#','#[Ý]#','#[ \&\!\?\#\"\(\)\{\}\[\]\=\+\~\%]#','#[^a-zA-Z0-9_\.\-]#'); 64 $replace = array ('e','E','a','A','i','I','u','U','o','O','c','C',',n','N','y','Y','_',''); 65 66 if ($CONF['renameORNOT']) { 67 return preg_replace($search, $replace, $string); 68 } else { 69 return str_replace(array('%','$',' '), '_', $string); 70 } 71 71 } 72 72 73 73 // Teste si un dossier est vide 74 74 function is_dir_empty($dir) { 75 76 77 78 79 80 75 $array = scandir($dir); 76 if (count($array) > 2) { 77 return false; 78 } else { 79 return true; 80 } 81 81 } 82 82 83 83 // Supprime un repertoire non-vide 84 84 function rrmdir($dir) { 85 86 87 88 89 90 91 92 93 94 95 96 85 $dir = rtrim($dir, '/'); 86 $objects = scandir($dir); 87 88 foreach ($objects as $object) { 89 if ($object !== '.' && $object !== '..') { 90 $path = $dir.'/'.$object; 91 if (filetype($path) == 'dir') rrmdir($path); 92 else unlink($path); 93 } 94 } 95 96 rmdir($dir); 97 97 } 98 98 99 99 // Parcours récursivement un dossier 100 100 function recursive_readdir($dir, $array, $dirs=false) { 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 101 # si $dirs=true liste les dossiers plutot que les fichiers 102 global ${$array}; // Charge la tableau pour la sortie, déclaré à l'exterieur à cause de l'appel recursif de la fonction 103 $dir = rtrim($dir, '/'); 104 $dh = opendir($dir); 105 106 while (($file = readdir($dh)) !== false ) { 107 if ($file !== '.' && $file !== '..') { 108 $path = $dir.'/'.$file; 109 if (is_dir($path)) { 110 if($dirs) ${$array}[] = $path; 111 recursive_readdir($path, $array, $dirs); 112 } else { 113 if(!$dirs) ${$array}[] = $path; 114 } 115 } 116 } 117 closedir($dh); 118 118 } 119 119 120 120 // Parse le fichier de configuration 121 121 function XMLParse($xml) { 122 123 124 125 126 127 128 129 130 122 $content = array(); 123 foreach ($xml as $nom => $elem) { 124 if (trim($elem) == '' AND $elem != '') { 125 $content[$nom] = XMLParse($elem->children()); 126 } else { 127 $content[$nom] = utf8_encode(utf8_decode($elem)); 128 } 129 } 130 return $content; 131 131 } 132 132 133 133 // Crée le fichier de configuration 134 134 function XMLCreate($array, $level=0) { 135 136 137 138 for($i=0;$i<=$level;$i++) $content .= "\t";139 140 141 for($i=0;$i<=$level;$i++) $content .= "\t";142 143 144 for($i=0;$i<=$level;$i++) $content .= "\t";145 146 147 148 135 $content = null; 136 foreach ($array as $nom => $elem) { 137 if (is_array($elem)) { 138 for($i=0;$i<=$level;$i++) $content .= " "; 139 $content .= '<'.$nom.'>'."\n"; 140 $content .= XMLCreate($elem, $level+1); 141 for($i=0;$i<=$level;$i++) $content .= " "; 142 $content .= '</'.$nom.'>'."\n"; 143 } else { 144 for($i=0;$i<=$level;$i++) $content .= " "; 145 $content .= '<'.$nom.'>'.$elem.'</'.$nom.'>'."\n"; 146 } 147 } 148 return $content; 149 149 } 150 150 151 151 // Charge la configuration 152 152 function load_config($file) { 153 154 155 156 153 $config = simplexml_load_file($file); 154 $config = XMLParse($config); 155 $config = array_settype($config); 156 return $config; 157 157 } 158 158 159 159 // Convertit les booléens, entiers et flotants d'un tableau de 'string' 160 160 function array_settype($array) { 161 162 163 164 165 166 $array[$key] = true; 167 168 $array[$key] = false; 169 170 171 172 settype($array[$key], 'int'); 173 174 settype($array[$key], 'float'); 175 176 177 178 179 161 foreach ($array as $key => $value) { 162 if (is_array($value)) { 163 $array[$key] = array_settype($value); 164 } else { 165 if ($value === 'true') { 166 $array[$key] = true; 167 } else if ($value === 'false') { 168 $array[$key] = false; 169 } else if ($value == null) { 170 // rien à faire ^^ 171 } else if (preg_match('#^([0-9]*)$#', $value)) { 172 settype($array[$key], 'int'); 173 } else if (preg_match('#^([0-9]*)(\.)([0-9]*)$#', $value)) { 174 settype($array[$key], 'float'); 175 } 176 } 177 } 178 179 return $array; 180 180 } 181 181 182 182 // Booléen vers français ou texte 183 183 function bool_to_string($string, $just_echo=0) { 184 185 186 187 188 189 190 191 192 193 194 195 196 184 global $Lang; 185 # $just_echo pour pouvoir afficher un booléen tel quel (echo true; n'affiche rien) 186 if (is_bool($string)) { 187 if ($string) { 188 if ($just_echo) return 'true'; 189 else return l10n('yes'); 190 } else { 191 if ($just_echo) return 'false'; 192 else return l10n('no'); 193 } 194 } else { 195 return $string; 196 } 197 197 } 198 198 199 199 // Converti une chaine de config (param:value;param:value[;]) en tableau associatif 200 200 function ExplodeString($string, $sep1=';', $sep2=':') { 201 202 203 204 205 206 207 208 209 201 $string = preg_replace('#[;]$#', '', $string); 202 $result = array(); 203 $a = explode($sep1, $string); 204 205 foreach ($a as $s) { 206 $v = explode($sep2, $s); 207 $result[$v[0]] = $v[1]; 208 } 209 return $result; 210 210 } 211 211 212 212 // Converti un tableau associatif en chaine de config (param:value;param:value;) 213 213 function ImplodeArray($array, $sep1=';', $sep2=':') { 214 215 216 217 218 214 $result = null; 215 foreach ($array as $key => $value) { 216 $result .= $key.$sep2.$value.$sep1; 217 } 218 return $result; 219 219 } 220 220 221 221 // Convertit les couleurs 'numériques' et gère les 0 manquants 222 222 function nice_hex_color($hex) { 223 224 225 226 227 223 $hex = strval($hex); 224 for ($i=strlen($hex); $i<6; $i++) { 225 $hex = '0'.$hex; 226 } 227 return $hex; 228 228 } 229 229 230 230 // Converti une couleur HEX et RGB 231 231 function hex2RGB($hexStr, $returnAsString = false, $seperator = ',') { 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 232 $hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string 233 234 if (strlen($hexStr) == 6) { //If a proper hex code, convert using bitwise operation. No overhead... faster 235 $colorVal = hexdec($hexStr); 236 $rgbArray['r'] = 0xFF & ($colorVal >> 0x10); 237 $rgbArray['g'] = 0xFF & ($colorVal >> 0x8); 238 $rgbArray['b'] = 0xFF & $colorVal; 239 } elseif (strlen($hexStr) == 3) { //if shorthand notation, need some string manipulations 240 $rgbArray['r'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2)); 241 $rgbArray['g'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2)); 242 $rgbArray['b'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2)); 243 } else { 244 return false; //Invalid hex color code 245 } 246 247 return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray; // returns the rgb string or the associative array 248 248 } 249 249 250 250 // Verifie si une chaine est un nombre décimal (plus strict que is_numeric qui fonctionne avec les hexadecimaux) 251 251 function is_decimal($num) { 252 253 254 255 256 252 if((is_numeric($num) === true AND (float)$num == $num) OR $num === '0') { 253 return true; 254 } else { 255 return false; 256 } 257 257 } 258 258 259 259 // Charge le fichier de langue d'un plugin 260 260 function load_plugin_lang($plugin_id) { 261 262 263 264 265 266 261 global $CONF, $Lang; 262 if (file_exists('plugins/'.$plugin_id.'/language/'.$CONF['user_lang'].'.php')) { 263 include('plugins/'.$plugin_id.'/language/'.$CONF['user_lang'].'.php'); 264 } else { 265 include('plugins/'.$plugin_id.'/language/en_UK.php'); 266 } 267 267 } 268 268 269 269 // fonction nconvert avec gestion du log 270 270 function nconvert($param, $log=true) { 271 272 273 274 275 276 277 278 279 //$LOG[] = "\t" . $param;280 281 282 271 global $LOG; 272 273 exec('include\nconvert.exe '. $param .' 2>&1', $out); 274 275 if ($log) { 276 for ($i=0; $i<=3; $i++) 277 unset($out[$i]); 278 if (count($out)) $LOG[] = array_values($out); 279 //$LOG[] = " " . $param; 280 } else { 281 return $out; 282 } 283 283 } 284 284 285 285 // fonction mkdir avec gestion du log 286 286 function mkdir_log($dir) { 287 288 289 287 global $LOG; 288 mkdir($dir, 0777, true); 289 $LOG[] = 'Dir '.$dir.' created'; 290 290 } 291 291 292 292 // fonction copy avec gestion du log 293 293 function copy_log($src, $dest) { 294 295 296 294 global $LOG; 295 copy($src, $dest); 296 $LOG[] = 'Copy '.$src.' to '.$dest; 297 297 } 298 298 299 299 // fonction rename avec gestion du log 300 300 function rename_log($path, $new_path, $move=false) { 301 302 303 304 305 306 301 global $LOG; 302 rename($path, $new_path); 303 if ($move) 304 $LOG[] = 'Move '.$path.' to '.$new_path; 305 else 306 $LOG[] = 'Rename '.$path.' into '.$new_path; 307 307 } 308 308 309 309 // enregistre le log dans un fichier 310 310 function print_log($nb, $s) { 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 311 global $APPversion, $LOG; 312 313 $config = file_get_contents('config.xml'); 314 315 $string = 316 "PHP Optimisateur " . $APPversion . "\r\n". 317 date('r') . "\r\n". 318 $nb . " files in " . $s . " seconds" . "\r\n\r\n". 319 320 $config . "\r\n\r\n"; 321 322 foreach ($LOG as $event) { 323 if (is_array($event)) { 324 $string .= implode("\r\n", $event) . "\r\n"; 325 } else { 326 $string .= $event . "\r\n"; 327 } 328 } 329 330 if (!file_exists('logs')) { 331 mkdir('logs', 0777, true); 332 } 333 334 $filename = 'log_' . date('Y-m-d_H-i-s') . '.txt'; 335 file_put_contents('logs/'.$filename, $string); 336 return $filename; 337 337 } 338 338 339 339 // case insentive version of in_array 340 340 function in_arrayi($needle, $haystack) { 341 342 343 341 $needle = strtolower($needle); 342 $haystack = array_map('strtolower', $haystack); 343 return in_array($needle, $haystack); 344 344 } 345 345
Note: See TracChangeset
for help on using the changeset viewer.