~desarrollokumbia/kumbia/0.5

« back to all changes in this revision

Viewing changes to library/kumbia/router/router.php

  • Committer: Deivinson Tejeda
  • Date: 2009-01-01 20:30:45 UTC
  • Revision ID: deivinsontejeda@gmail.com-20090101203045-q9xgzpp0a6vaomdh
actualizando repositorio launchpad

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
abstract class Router extends Object {
39
39
 
40
40
        /**
41
 
         * Nombre de la aplicaci�n actual
 
41
         * Nombre de la aplicación actual
42
42
         *
43
43
         * @var string
44
44
         */
63
63
         *
64
64
         * @var string
65
65
         */
66
 
        static private $action;
 
66
        static private $action = 'index';
67
67
 
68
68
        /**
69
69
         * Nombre del primer parametro despues de action
77
77
         *
78
78
         * @var array
79
79
         */
80
 
        static private $all_parameters;
 
80
        static private $all_parameters = array();
81
81
 
82
82
        /**
83
83
         * Lista los parametros adicionales de la URL
84
84
         *
85
85
         * @var array
86
86
         */
87
 
        static private $parameters;
 
87
        static private $parameters = array();
88
88
 
89
89
        /**
90
90
         * Indica si se esta utilizando el modo de aplicacion por defecto
91
91
         *
92
92
         * @var boolean
93
93
         */
94
 
        static private $default_app_mode;
 
94
        static private $default_app_mode = true;
95
95
 
96
96
        /**
97
 
         * Indica si esta pendiente la ejecuci�n de una ruta por
 
97
         * Indica si esta pendiente la ejecución de una ruta por
98
98
         * parte del dispatcher
99
99
         *
100
100
         * @var boolean
112
112
         * @param string $url
113
113
         */
114
114
        static function rewrite($url){
115
 
                /**
116
 
                 * Obtengo y asigno todos los parametros de la url
117
 
                 **/
118
 
                self::$all_parameters = $url_items = explode("/", $url);
119
 
 
120
 
                /**
121
 
                 * Valores por defecto
122
 
                 */
123
 
                self::$module = '';
124
 
                self::$controller = '';
125
 
                self::$action = 'index';
126
 
                self::$id = null;
127
115
 
128
116
                /**
129
117
                 * Cargo la configuracion
130
118
                 */
131
119
                $config = Config::read("config.ini");
 
120
                
 
121
                /**
 
122
                 * Valor por defecto
 
123
                 */
 
124
                
 
125
                self::$application = $config->kumbia->default_app;
 
126
                
 
127
                // Si esta vacio (es root) volver con los parametros por defecto
 
128
                if (!$url) {
 
129
                        return;
 
130
                }
 
131
        
 
132
                /**
 
133
                 * Limpio la url en caso de que la hallan escrito con el 
 
134
                 * ultimo parametro sin valor es decir controller/action/
 
135
                 **/
 
136
                $url = trim($url,'/');
132
137
 
133
138
                /**
 
139
                 * Obtengo y asigno todos los parametros de la url
 
140
                 **/
 
141
                $url_items = explode ('/', $url);
 
142
                
 
143
                // Se limpian los parametros por seguridad
 
144
                $clean = array( '\\', '..');
 
145
                $url_items = str_replace($clean,  '', $url_items, $errors);
 
146
                
 
147
                // Si hay intento de hack TODO: añadir la ip y referer en el log
 
148
                if($errors) throw new RouterException("Posible intento de hack en URL: '$url'");
 
149
                
 
150
                /**
 
151
                * Asigna todos los parametros
 
152
                **/
 
153
                self::$all_parameters = $url_items;
 
154
               
 
155
                /**
134
156
                 * Si el primer parametro de la url corresponde a una aplicacion
135
 
                 */
136
 
                if(isset($config->$url_items[0])){
 
157
                 */  
 
158
                if (isset($config->$url_items[0])) {
137
159
                        /**
138
160
                         * Indico la aplicacion que se ejecutara
139
161
                         **/
140
162
                        self::$application = $url_items[0];
141
 
                        unset($url_items[0]);
142
 
                        
143
163
                        /**
144
164
                         * Indico que no se utiliza el modo de aplicacion por defecto
145
165
                         **/
146
166
                        self::$default_app_mode = false;
147
 
 
148
 
                        /**
149
 
                         * Si existe un segundo parametro en la url
150
 
                         **/
151
 
                        if(isset($url_items[1]) && $url_items[1]) {
152
 
                                /**
153
 
                                 * Obtengo el directorio de controladores
154
 
                                 **/
155
 
                                $controllers_dir = $config->{self::$application}->controllers_dir;
156
 
                                
157
 
                                /**
158
 
                                 * Se limpia el segundo parametro de la url (puede ser un modulo o un controlador)
159
 
                                 **/
160
 
                                $url_items[1] = str_replace("\\", '', $url_items[1]);
161
 
                                $url_items[1] = str_replace('..', '', $url_items[1]);                   
162
 
                                
163
 
                                /**
164
 
                                 * El segundo parametro de la url es un modulo?
165
 
                                 **/
166
 
                                if(is_dir("apps/$controllers_dir/$url_items[1]")) {
167
 
                                        self::$module = $url_items[1];
168
 
                                        unset($url_items[1]);
169
 
                                        
170
 
                                        /**
171
 
                                         * Hay algun controlador?
172
 
                                         */
173
 
                                        if(isset($url_items[2]) && $url_items[2]){
174
 
                                                self::$controller = $url_items[2];
175
 
                                                unset($url_items[2]);
176
 
                                                
177
 
                                                /**
178
 
                                                 * Se limpia el nombre del controlador
179
 
                                                 **/
180
 
                                                
181
 
                                                self::$controller = str_replace("\\", '', self::$controller);
182
 
                                                self::$controller = str_replace('..', '', self::$controller);
183
 
                                        } else {
184
 
                                                self::$controller = 'index';
185
 
                                        }
186
 
                        
187
 
                                        /**
188
 
                                         * Hay alguna accion?
189
 
                                         */
190
 
                                        if(isset($url_items[3]) && $url_items[3]){
191
 
                                                self::$action = $url_items[3];
192
 
                                                unset($url_items[3]);
193
 
                                        }
194
 
 
195
 
                                        /**
196
 
                                         * Hay algun id?
197
 
                                         */
198
 
                                        if(isset($url_items[4]) && $url_items[4]){
199
 
                                                self::$id = $url_items[4];
200
 
                                        }
201
 
                                        
202
 
                                } else {
203
 
                                        /**
204
 
                                         * No es un modulo, entonces es un controlador
205
 
                                         */
206
 
                                        self::$controller = $url_items[1];
207
 
                                        unset($url_items[1]);
208
 
                        
209
 
                                        /**
210
 
                                         * Hay alguna accion?
211
 
                                         */
212
 
                                        if(isset($url_items[2]) && $url_items[2]){
213
 
                                                self::$action = $url_items[2];
214
 
                                                unset($url_items[2]);
215
 
                                        }
216
 
 
217
 
                                        /**
218
 
                                         * Hay algun id?
219
 
                                         */
220
 
                                        if(isset($url_items[3]) && $url_items[3]){
221
 
                                                self::$id = $url_items[3];
222
 
                                        }
223
 
                                        
224
 
                                        
225
 
                                }
226
 
                        }
227
 
                        
228
 
                } else {
229
 
                        /**
230
 
                         * Indico la aplicacion que se ejecutara
231
 
                         **/
232
 
                        self::$application = $config->kumbia->default_app;
233
 
                        
234
 
                        /**
235
 
                         * Indico que se utiliza el modo de aplicacion por defecto
236
 
                         **/
237
 
                        self::$default_app_mode = true;
238
 
                        
239
 
                        /**
240
 
                         * Si existe un segundo parametro en la url
241
 
                         **/
242
 
                        if(isset($url_items[0]) && $url_items[0]) {
243
 
                                /**
244
 
                                 * Obtengo el directorio de controladores
245
 
                                 **/
246
 
                                $controllers_dir = $config->{self::$application}->controllers_dir;
247
 
                                
248
 
                                /**
249
 
                                 * Se limpia el primer parametro de la url (puede ser un modulo o un controlador)
250
 
                                 **/
251
 
                                $url_items[0] = str_replace("\\", '', $url_items[0]);
252
 
                                $url_items[0] = str_replace('..', '', $url_items[0]);
253
 
                                
254
 
                                /**
255
 
                                 * El primer parametro de la url es un modulo?
256
 
                                 **/
257
 
                                if(is_dir("apps/$controllers_dir/$url_items[0]")) {
258
 
                                        self::$module = $url_items[0];
259
 
                                        unset($url_items[0]);
260
 
 
261
 
                                        /**
262
 
                                         * Hay algun controlador?
263
 
                                         */
264
 
                                        if(isset($url_items[1]) && $url_items[1]){
265
 
                                                self::$controller = $url_items[1];
266
 
                                                unset($url_items[1]);
267
 
                                                
268
 
                                                /**
269
 
                                                 * Se limpia el nombre del controlador
270
 
                                                 **/
271
 
                                                
272
 
                                                self::$controller = str_replace("\\", '', self::$controller);
273
 
                                                self::$controller = str_replace('..', '', self::$controller);
274
 
                                        } else {
275
 
                                                self::$controller = 'index';
276
 
                                        }
277
 
                        
278
 
                                        /**
279
 
                                         * Hay alguna accion?
280
 
                                         */
281
 
                                        if(isset($url_items[2]) && $url_items[2]){
282
 
                                                self::$action = $url_items[2];
283
 
                                                unset($url_items[2]);
284
 
                                        }
285
 
 
286
 
                                        /**
287
 
                                         * Hay algun id?
288
 
                                         */
289
 
                                        if(isset($url_items[3]) && $url_items[3]){
290
 
                                                self::$id = $url_items[3];
291
 
                                        }
292
 
                                } else {
293
 
                                        /**
294
 
                                         * No es un modulo, entonces es un controlador
295
 
                                         */
296
 
                                        self::$controller = $url_items[0];
297
 
                                        unset($url_items[0]);
298
 
                                        
299
 
                                        /**
300
 
                                         * Hay alguna accion?
301
 
                                         */
302
 
                                        if(isset($url_items[1]) && $url_items[1]){
303
 
                                                self::$action = $url_items[1];
304
 
                                                unset($url_items[1]);
305
 
                                        }
306
 
                                        
307
 
                                        /**
308
 
                                         * Hay algun id?
309
 
                                         */
310
 
                                        if(isset($url_items[2]) && $url_items[2]){
311
 
                                                self::$id = $url_items[2];
312
 
                                        }
313
 
                                }
314
 
                        }
315
 
                        
 
167
                        
 
168
                        // Si no hay mas parametros sale
 
169
                        if (next($url_items) === FALSE) {
 
170
                                return;
 
171
                        }
316
172
                }
317
173
                
318
 
                self::$parameters = array_values($url_items);
 
174
                /**
 
175
                * Obtengo el directorio de controladores
 
176
                **/
 
177
                $controllers_dir = $config->{self::$application}->controllers_dir;
 
178
                
 
179
                /**
 
180
                * El siguiente parametro de la url es un modulo?
 
181
                **/
 
182
                $item = current($url_items);
 
183
                if(is_dir("apps/$controllers_dir/$item")) {
 
184
                       self::$module = current($url_items);
 
185
                       
 
186
                       // Si no hay mas parametros sale y pone index como controlador
 
187
                        if (next($url_items) === FALSE) {
 
188
                                self::$controller = 'index';
 
189
                                return;
 
190
                        }       
 
191
                }       
 
192
                       
 
193
                /**
 
194
                 * Controlador
 
195
                 */
 
196
                self::$controller = current($url_items);
 
197
                        
 
198
                        // Si no hay mas parametros sale
 
199
                        if (next($url_items) === FALSE) {
 
200
                                return;
 
201
                        }       
 
202
                        
 
203
                /**
 
204
                * Accion
 
205
                */
 
206
                self::$action = current($url_items);
 
207
                
 
208
                        // Si no hay mas parametros sale
 
209
                        if (next($url_items) === FALSE) {
 
210
                                return;
 
211
                        }
 
212
                
 
213
                /**
 
214
                *  id
 
215
                */
 
216
                self::$id = current($url_items);
 
217
                
 
218
                /**
 
219
                *  Creo los parametros y los paso, depues elimino el $url_items
 
220
                */
 
221
                $key = key($url_items);
 
222
                $rest = count($url_items) - $key;
 
223
                $parameters = array_slice($url_items, $key, $rest);
 
224
                
 
225
                self::$parameters = $parameters;
 
226
                unset ($url_items);
319
227
        }
320
228
 
 
229
        
 
230
        static function routed(){
 
231
                $routes = Config::read('routes.ini');
 
232
                
 
233
                /**
 
234
                                Leo la politica de enrutamiento para la aplicacion
 
235
                        **/
 
236
                        if(isset($routes->{self::$application})){
 
237
                                foreach($routes->{self::$application} as $source => $destination){
 
238
                                        
 
239
                                        /**
 
240
                                                Extraigo la regla de enrutamiento para el origen
 
241
                                        **/
 
242
                                        // Limpio la URL
 
243
                                        $source = trim($source,'/');
 
244
                                        
 
245
                                        $source_items = explode ('/', $source);
 
246
                                        
 
247
                                        $num_sep = substr_count($source, '/');
 
248
                                        if ($source === '/'){
 
249
                                                $module_source = ''; 
 
250
                                                $action_source = 'index';
 
251
                                                $controller_source = '';
 
252
                                                $id_source =  null;
 
253
                                        } elseif ($num_sep==1 && $source != '/') {
 
254
                                                $module_source = ''; 
 
255
                                                $action_source = 'index';
 
256
                                                $controller_source = 'ss';
 
257
                                                $id_source =  null;
 
258
                                        } elseif($num_sep==2) {
 
259
                                                list($controller_source, $action_source, $id_source) = explode("/", $source);
 
260
                                                $module_source = ''; 
 
261
                                        } elseif($num_sep==3) {
 
262
                                                list($module_source, $controller_source, $action_source, $id_source) = explode("/", $source);
 
263
                                        } else {
 
264
                                                throw new RouterException("Política de enrutamiento invalida '$source' a '$destination' en config/routes.ini");
 
265
                                        }
 
266
                                        $application_source = self::$application;
 
267
                                }
 
268
                                
 
269
                        }
 
270
                
 
271
        }
 
272
        
321
273
        /**
322
274
         * Busca en la tabla de entutamiento si hay una ruta en config/routes.ini
323
275
         * para el controlador, accion, id actual
332
284
                        /**
333
285
                                Leo la politica de enrutamiento para la aplicacion
334
286
                        **/
335
 
                        if(isset($routes->{self::get_application()})){
336
 
                                foreach($routes->{self::get_application()} as $source => $destination){
 
287
                        if(isset($routes->{self::$application})){
 
288
                                //echo "aplicacion";
 
289
                                foreach($routes->{self::$application} as $source => $destination){
337
290
                                
338
291
                                        /**
339
292
                                                Extraigo la regla de enrutamiento para el origen
340
293
                                        **/
341
294
                                        $num_sep = substr_count($source, '/');
342
 
                                        if($num_sep==1) {
 
295
                                        if ($source === '/'){
343
296
                                                $module_source = ''; 
344
297
                                                $action_source = 'index';
345
298
                                                $controller_source = '';
346
299
                                                $id_source =  null;
 
300
                                        } elseif ($num_sep==1 && $source != '/') {
 
301
                                                $module_source = ''; 
 
302
                                                $action_source = 'index';
 
303
                                                $controller_source = 'ss';
 
304
                                                $id_source =  null;
347
305
                                        } elseif($num_sep==2) {
348
306
                                                list($controller_source, $action_source, $id_source) = explode("/", $source);
349
307
                                                $module_source = ''; 
352
310
                                        } else {
353
311
                                                throw new RouterException("Política de enrutamiento invalida '$source' a '$destination' en config/routes.ini");
354
312
                                        }
355
 
                                        $application_source = self::get_application();
 
313
                                        $application_source = self::$application;
356
314
 
357
315
                                        /**
358
316
                                         * Evaluo la regla de enrutamiento
363
321
                                                $action_destination = 'index';
364
322
                                                $controller_destination = '';
365
323
                                                $id_destination = null;
 
324
                                                //echo "--destino sep1";
366
325
                                        } elseif($num_sep==2) {
367
326
                                                list($controller_destination, $action_destination, $id_destination) = explode("/", $destination);
368
327
                                                $module_destination = '';
 
328
                                                //echo "--destino sep2";
369
329
                                        } elseif($num_sep==3) {
370
330
                                                list($module_destination, $controller_destination, $action_destination, $id_destination) = explode("/", $destination);
371
331
                                        } else {
372
332
                                                throw new RouterException("Política de enrutamiento invalida '$source' a '$destination' en config/routes.ini");
373
333
                                        }
374
 
                                        $application_destination = Router::get_application();
375
 
 
 
334
                                        $application_destination = self::$application;
 
335
                                        //echo "[destino contr:".$controller_destination." source contr:".$controller_source."]";
376
336
                                        /**
377
337
                                                Si la politica de enrutamiento no es ciclica, entonces la cargo en la variable de sesion
378
338
                                        **/
391
351
                        }
392
352
                }
393
353
 
394
 
                $application = self::get_application();
395
 
                $module = self::get_module();
 
354
                $application = self::$application;
 
355
                $module = self::$module;
396
356
                $controller = self::$controller;
397
357
                $action = self::$action;
398
358
                $id = self::$id;
471
431
        }
472
432
 
473
433
        /**
474
 
         * Devuelve el nombre de la aplicaci�n actual
 
434
         * Devuelve el nombre de la aplicación actual
475
435
         *
476
436
         * @return string
477
437
         */
544
504
        }
545
505
 
546
506
        /**
547
 
         * Enruta el controlador actual a otro controlador, ó a otra acci�n
 
507
         * Enruta el controlador actual a otro controlador, ó a otra acción
548
508
         * Ej:
549
509
         * <code>
550
 
         * kumbia::route_to("controller: nombre", ["action: accion"], ["id: id"])
 
510
         * kumbia::route_to(["module: modulo"], "controller: nombre", ["action: accion"], ["id: id"])
551
511
         * </code>
552
512
         *
553
513
         * @return null
663
623
 
664
624
        /**
665
625
         * Nombre de la aplicaci&oacute;n activa actual devuelve "" en caso de
666
 
         * que la aplicaci�n sea default
 
626
         * que la aplicación sea default
667
627
         *
668
628
         * @return string
669
629
         */
670
630
        static public function get_active_app(){
671
 
                return self::$application != "default" ? self::$application : "";
 
631
                return self::$default_app_mode ? "" : self::$application;
672
632
        }
673
633
        
674
634
        /**
681
641
                return self::$default_app_mode;
682
642
        }
683
643
}
684
 
 
685
 
?>
 
 
b'\\ No newline at end of file'