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: $dossiers_classes = array( dirname(__FILE__).DS,
106: dirname(__FILE__).DS.'utilitaires'.DS);
107: foreach ($dossiers_classes as $chemin) {
108: $fichier_a_tester = $chemin.$nom_classe_fw.'.php';
109: if (file_exists($fichier_a_tester)) {
110: include_once $fichier_a_tester;
111: return null;
112: }
113: }
114: }
115:
116: 117: 118:
119: private static function autoloadAppliDefaut($nom_classe) {
120: $dossiers_classes = array( Config::get('chemin_controleurs'),
121: Config::get('chemin_modeles'),
122: Config::get('chemin_bibliotheque'));
123:
124: foreach ($dossiers_classes as $chemin) {
125: $fichier_a_tester = $chemin.$nom_classe.'.php';
126: if (file_exists($fichier_a_tester)) {
127: include_once $fichier_a_tester;
128: return null;
129: }
130: }
131: }
132:
133: 134: 135:
136: private static function initialiserUrl() {
137: ini_set('arg_separator.input', Config::get('furl_arg_separateur_entree'));
138: ini_set('arg_separator.output', Config::get('url_arg_separateur_sortie'));
139: }
140:
141: 142: 143: 144: 145:
146: public static function setCheminAppli($chemin_fichier_principal) {
147: if (self::$chemin === null) {
148: if (!file_exists($chemin_fichier_principal)) {
149: trigger_error("Le fichier indiqué n'existe pas. Utilisez __FILE__ dans la méthode setCheminAppli().", E_USER_ERROR);
150: } else {
151: self::definirConstantesGlobales();
152: self::definirCheminAppli($chemin_fichier_principal);
153: self::initialiserEnvironnement();
154: self::initialiserFramework();
155: }
156: } else {
157: trigger_error("Le chemin de l'application a déjà été enregistré auprès du Framework", E_USER_WARNING);
158: }
159: }
160:
161: 162: 163: 164:
165: public static function getCheminAppli() {
166: return self::$chemin;
167: }
168:
169: 170: 171: 172: 173: 174: 175: 176:
177: public static function setInfoAppli($info) {
178: if (self::$info === null) {
179: self::$info = $info;
180: } else {
181: trigger_error("Le informations de l'application ont déjà été enregistrées auprès du Framework", E_USER_WARNING);
182: }
183: }
184:
185: 186: 187: 188:
189: public static function getInfoAppli($cle = null) {
190: if ($cle !== null) {
191: if (isset(self::$info[$cle])) {
192: return self::$info[$cle];
193: }
194: } else {
195: return self::$info;
196: }
197: }
198:
199: 200: 201:
202: protected static function verifierEtReencoderTableauRequete() {
203: if (self::$encodage == false && Config::get('encodage_sortie') != Config::get('encodage_appli')) {
204: $_POST = self::encoderTableau($_POST, Config::get('encodage_appli'), Config::get('encodage_sortie'));
205: $_GET = self::encoderTableau($_GET, Config::get('encodage_appli'), Config::get('encodage_sortie'));
206:
207:
208: self::verifierEtTraiterSlashTableauRequete();
209:
210: self::$encodage = true;
211: }
212: }
213:
214: 215: 216:
217: private static function verifierEtTraiterSlashTableauRequete() {
218: if (get_magic_quotes_gpc()) {
219: if (!function_exists('nettoyerSlashProfond')) {
220: function nettoyerSlashProfond($valeur) {
221: return ( is_array($valeur) ) ? array_map('nettoyerSlashProfond', $valeur) : stripslashes($valeur);
222: }
223: }
224: $_GET = array_map('nettoyerSlashProfond', $_GET);
225: $_POST = array_map('nettoyerSlashProfond', $_POST);
226: $_COOKIE = array_map('nettoyerSlashProfond', $_COOKIE);
227: }
228: }
229:
230: 231: 232: 233: 234: 235: 236: 237:
238: final static protected function encoderTableau($tableau, $encodage_sortie, $encodage_entree = null) {
239: if (is_array($tableau)) {
240: foreach ($tableau as $cle => $valeur) {
241: if (is_array($valeur)) {
242: $tableau[$cle] = self::encoderTableau($valeur, $encodage_sortie, $encodage_entree);
243: } else {
244: $tableau[$cle] = mb_convert_encoding($valeur, $encodage_sortie, $encodage_entree);
245: }
246: }
247: }
248: return $tableau;
249: }
250: }
251: ?>