1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class Framework {
24:
25:
26: private static $encodage = false;
27:
28:
29: private static $info = null;
30:
31:
32: private static $chemin = null;
33:
34:
35: private static $parametres_obligatoires = array('chemin_controleurs', 'chemin_modeles', 'chemin_bibliotheque',
36: 'url_arg_separateur_entree', 'url_arg_separateur_sortie',
37: 'encodage_sortie', 'encodage_appli');
38:
39: 40: 41: 42: 43:
44: private static function initialiserEnvironnement() {
45: self::enregistrerMethodesAutoload();
46: self::initialiserUrl();
47: }
48:
49: 50: 51: 52: 53:
54: private static function initialiserFramework() {
55: GestionnaireException::configurer();
56:
57: self::verifierEtReencoderTableauRequete();
58: }
59:
60: 61: 62:
63: private static function definirConstantesGlobales() {
64: if (!defined('DS')) {
65:
66: define('DS', DIRECTORY_SEPARATOR);
67: }
68: if (!defined('PS')) {
69:
70: define('PS', PATH_SEPARATOR);
71: }
72: }
73:
74: private static function definirCheminAppli($chemin) {
75: if (is_file($chemin)) {
76: self::$chemin = dirname($chemin).DS;
77: } else if (is_dir($chemin)) {
78: self::$chemin = $chemin;
79: } else {
80: throw new Exception("Le chemin indiqué '$chemin' n'est ni un fichier ni un dossier.");
81: }
82: }
83:
84: private static function enregistrerMethodesAutoload() {
85: spl_autoload_register(array(get_class(), 'autoloadFw'));
86:
87:
88: Config::verifierPresenceParametres(self::$parametres_obligatoires);
89:
90:
91: GestionnaireException::initialiser();
92:
93: spl_autoload_register(array(get_class(), 'autoloadAppliDefaut'));
94:
95:
96: if (function_exists('__autoload')) {
97: spl_autoload_register('__autoload');
98: }
99: }
100:
101: 102: 103:
104: private static function autoloadFw($nom_classe_fw) {
105: $racine_fw = dirname(__FILE__).DS;
106: $dossiers_classes = array( $racine_fw,
107: $racine_fw.'utilitaires'.DS,
108: $racine_fw.'brouillons'.DS);
109: foreach ($dossiers_classes as $chemin) {
110: $fichier_a_tester = $chemin.$nom_classe_fw.'.php';
111: if (file_exists($fichier_a_tester)) {
112: include_once $fichier_a_tester;
113: return null;
114: }
115: }
116: }
117:
118: 119: 120:
121: private static function autoloadAppliDefaut($nom_classe) {
122: $dossiers_classes = array( Config::get('chemin_controleurs'),
123: Config::get('chemin_modeles'),
124: Config::get('chemin_bibliotheque'));
125:
126: foreach ($dossiers_classes as $chemin) {
127: $fichier_a_tester = $chemin.$nom_classe.'.php';
128: if (file_exists($fichier_a_tester)) {
129: include_once $fichier_a_tester;
130: return null;
131: }
132: }
133: }
134:
135: 136: 137:
138: private static function initialiserUrl() {
139: ini_set('arg_separator.input', Config::get('furl_arg_separateur_entree'));
140: ini_set('arg_separator.output', Config::get('url_arg_separateur_sortie'));
141: }
142:
143: 144: 145: 146: 147:
148: public static function setCheminAppli($chemin_fichier_principal) {
149: if (self::$chemin === null) {
150: if (!file_exists($chemin_fichier_principal)) {
151: trigger_error("Le fichier indiqué n'existe pas. Utilisez __FILE__ dans la méthode setCheminAppli().", E_USER_ERROR);
152: } else {
153: self::definirConstantesGlobales();
154: self::definirCheminAppli($chemin_fichier_principal);
155: self::initialiserEnvironnement();
156: self::initialiserFramework();
157: }
158: } else {
159: trigger_error("Le chemin de l'application a déjà été enregistré auprès du Framework", E_USER_WARNING);
160: }
161: }
162:
163: 164: 165: 166:
167: public static function getCheminAppli() {
168: return self::$chemin;
169: }
170:
171: 172: 173: 174: 175: 176: 177: 178:
179: public static function setInfoAppli($info) {
180: if (self::$info === null) {
181: self::$info = $info;
182: } else {
183: trigger_error("Le informations de l'application ont déjà été enregistrées auprès du Framework", E_USER_WARNING);
184: }
185: }
186:
187: 188: 189: 190:
191: public static function getInfoAppli($cle = null) {
192: if ($cle !== null) {
193: if (isset(self::$info[$cle])) {
194: return self::$info[$cle];
195: }
196: } else {
197: return self::$info;
198: }
199: }
200:
201: 202: 203:
204: protected static function verifierEtReencoderTableauRequete() {
205: if (self::$encodage == false && Config::get('encodage_sortie') != Config::get('encodage_appli')) {
206: $_POST = self::encoderTableau($_POST, Config::get('encodage_appli'), Config::get('encodage_sortie'));
207: $_GET = self::encoderTableau($_GET, Config::get('encodage_appli'), Config::get('encodage_sortie'));
208:
209:
210: self::verifierEtTraiterSlashTableauRequete();
211:
212: self::$encodage = true;
213: }
214: }
215:
216: 217: 218:
219: private static function verifierEtTraiterSlashTableauRequete() {
220: if (get_magic_quotes_gpc()) {
221: if (!function_exists('nettoyerSlashProfond')) {
222: function nettoyerSlashProfond($valeur) {
223: return ( is_array($valeur) ) ? array_map('nettoyerSlashProfond', $valeur) : stripslashes($valeur);
224: }
225: }
226: $_GET = array_map('nettoyerSlashProfond', $_GET);
227: $_POST = array_map('nettoyerSlashProfond', $_POST);
228: $_COOKIE = array_map('nettoyerSlashProfond', $_COOKIE);
229: }
230: }
231:
232: 233: 234: 235: 236: 237: 238: 239:
240: final static protected function encoderTableau($tableau, $encodage_sortie, $encodage_entree = null) {
241: if (is_array($tableau)) {
242: foreach ($tableau as $cle => $valeur) {
243: if (is_array($valeur)) {
244: $tableau[$cle] = self::encoderTableau($valeur, $encodage_sortie, $encodage_entree);
245: } else {
246: $tableau[$cle] = mb_convert_encoding($valeur, $encodage_sortie, $encodage_entree);
247: }
248: }
249: }
250: return $tableau;
251: }
252: }
253: ?>