Browse Source

Adding new approach that redirects the main/* files into the web/main/* in order to load correctly session and middlewares (course permissions, user settings, platform settings, etc)
The benefits of this changes are that we can add real hooks before some actions (using middlewares) as well as improve security using an upcoming role permission feature.

Julio Montoya 11 years ago
parent
commit
60dfd7175d

+ 3 - 0
.htaccess

@@ -15,6 +15,9 @@
     RewriteRule ^courses/(.*)/index.php$ web/courses/$1? [R,L]
     RewriteRule ^courses/(.*)/$ web/courses/$1? [R,L]
 
+    # PHP Main files are redirected to the "web/main" zone
+    RewriteRule ^main/(.*)\.php web/main/$1.php [R,L]
+
     # Courses documents
     # courses/MATHS/document/folder1/picture.jpg --> web/data/courses/MATHS/document/folder1/picture.jpg
     RewriteRule ^courses/(.*)/document/(.*)$ web/data/courses/$1/document/$2 [R,L]

+ 2 - 0
inc/global.inc.php

@@ -0,0 +1,2 @@
+<?php
+/*  File needed because all files include the "require_once '../inc/global.inc.php'" */

+ 2 - 0
main/inc/global.inc.php

@@ -84,6 +84,7 @@ if (file_exists($configurationYMLFile)) {
 /** Setting Chamilo paths */
 
 $app['root_sys'] = isset($_configuration['root_sys']) ? $_configuration['root_sys'] : dirname(dirname(__DIR__)).'/';
+$app['sys_root'] = $app['root_sys'];
 $app['sys_data_path'] = isset($_configuration['sys_data_path']) ? $_configuration['sys_data_path'] : $app['root_sys'].'data/';
 $app['sys_config_path'] = isset($_configuration['sys_config_path']) ? $_configuration['sys_config_path'] : $app['root_sys'].'config/';
 $app['sys_temp_path'] = isset($_configuration['sys_temp_path']) ? $_configuration['sys_temp_path'] : $app['root_sys'].'temp/';
@@ -446,6 +447,7 @@ $language_files[] = 'accessibility';
 $language_files[] = 'index';
 $language_files[] = 'courses';
 $language_files[] = 'course_home';
+$language_files[] = 'exercice';
 
 if (isset($language_file)) {
     if (!is_array($language_file)) {

+ 8 - 8
main/inc/lib/display.lib.php

@@ -38,10 +38,10 @@ class Display
         $app['title'] = $tool_name;
 
         if ($app['allowed'] == true) {
-            ob_start(array($app['template'], 'manageDisplay'));
+            //ob_start(array($app['template'], 'manageDisplay'));
         } else {
-            $app->run();
-            exit;
+            //$app->run();
+            //exit;
         }
     }
 
@@ -50,11 +50,11 @@ class Display
      */
     public static function display_footer()
     {
-        global $app;
-        $out = ob_get_contents();
-        ob_end_clean();
-        $app['template']->assign('content', $out);
-        $app->run();
+        //global $app;
+        //$out = ob_get_contents();
+        //ob_end_clean();
+        //$app['template']->assign('content', $out);
+        //$app->run();
     }
 
     /**

+ 6 - 3
main/inc/lib/main_api.lib.php

@@ -689,7 +689,8 @@ function api_get_path($path_type, $path = null) {
         $paths[WEB_CSS_PATH]            = $paths[WEB_CODE_PATH].$paths[WEB_CSS_PATH];
         $paths[WEB_IMG_PATH]            = $paths[WEB_CODE_PATH].$paths[WEB_IMG_PATH];
         $paths[WEB_LIBRARY_PATH]        = $paths[WEB_CODE_PATH].$paths[WEB_LIBRARY_PATH];
-        $paths[WEB_AJAX_PATH]           = $paths[WEB_CODE_PATH].$paths[WEB_AJAX_PATH];
+        //$paths[WEB_AJAX_PATH]           = $paths[WEB_CODE_PATH].$paths[WEB_AJAX_PATH];
+        $paths[WEB_AJAX_PATH]           = $paths[WEB_PUBLIC_PATH].'main/'.$paths[WEB_AJAX_PATH];
 
         $paths[WEB_PLUGIN_PATH]         = $paths[WEB_PATH].$paths[WEB_PLUGIN_PATH];
         $paths[WEB_ARCHIVE_PATH]        = $paths[WEB_PATH].$paths[WEB_ARCHIVE_PATH];
@@ -2229,10 +2230,12 @@ function api_delete_settings_params($params) {
  * @return string   Escaped version of $_SERVER['PHP_SELF']
  */
 function api_get_self() {
-    return htmlentities($_SERVER['PHP_SELF']);
+    $urlInfo = parse_url($_SERVER['REQUEST_URI']);
+    return $urlInfo['path'];
+    //return $_SERVER['REQUEST_URI'];
+    //return htmlentities($_SERVER['PHP_SELF']);
 }
 
-
 /* USER PERMISSIONS */
 
 /**

+ 18 - 5
main/inc/routes.php

@@ -399,15 +399,28 @@ $afterLogin = function (Request $request) use ($app) {
 };
 
 /** Legacy controller */
-$app->get('/', 'legacy.controller:classicAction')
+$app->match('/', 'legacy.controller:classicAction', 'GET|POST')
     ->before($userAccessConditions)
     ->before($settingCourseConditions)
     ->before($userPermissionsInsideACourse);
 
-$app->post('/', 'legacy.controller:classicAction')
-    ->before($userAccessConditions)
-    ->before($settingCourseConditions)
-    ->before($userPermissionsInsideACourse);
+$checkLangs = function (Request $request) use ($app) {
+    /*$file = $request->get('file');
+    $info = pathinfo($file);
+    $section = $info['dirname'];*/
+};
+
+/** main files */
+$app->match('/main/{file}', 'legacy.controller:includeAction', 'GET|POST')
+    ->before($checkLangs)
+    ->before(
+        function() use ($app) {
+            // Do not load breadcrumbs
+            $app['template']->loadBreadcrumb = false;
+        })
+    ->assert('file', '.+')
+    ->assert('type', '.+');
+
 
 /** web/index */
 $app->match('/index', 'index.controller:indexAction', 'GET|POST')

+ 71 - 1
src/ChamiloLMS/Controller/LegacyController.php

@@ -5,6 +5,8 @@ namespace ChamiloLMS\Controller;
 
 use Silex\Application;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\Request;
+use \ChamiloSession as Session;
 
 /**
  * Class LegacyController
@@ -12,7 +14,7 @@ use Symfony\Component\HttpFoundation\Response;
  * @package ChamiloLMS\Controller
  * @author Julio Montoya <gugli100@gmail.com>
  */
-class LegacyController// extends Controller
+class LegacyController extends CommonController
 {
     public $section;
     public $language_files = array('courses', 'index', 'admin');
@@ -41,6 +43,74 @@ class LegacyController// extends Controller
             return $app->redirect('index');
         }
 
+        return new Response($response, 200, array());
+    }
+
+     /**
+     * Handles default Chamilo scripts handled by Display::display_header() and display_footer()
+     *
+     * @param \Silex\Application $app
+     *
+     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response|void
+     */
+    public function includeAction(Application $app, $file)
+    {
+        /** @var  Request $request */
+        $request = $app['request'];
+
+        // get.
+        $_GET = $request->query->all();
+        // post.
+        $_POST = $request->request->all();
+        // echo $request->getMethod();
+
+        //$_REQUEST = $request->request->all();
+
+        // Getting language section
+        $info = pathinfo($file);
+        $section = $info['dirname'];
+
+        if ($section == 'admin') {
+            $this->cidReset();
+        }
+
+        $mainPath = $app['paths']['sys_root'].'main/';
+        if (is_file($mainPath.$file)) {
+
+            // Default values
+            $_course = api_get_course_info();
+            $_user = api_get_user_info();
+            $charset = 'UTF-8';
+            $debug = $app['debug'];
+            $text_dir = api_get_text_direction();
+
+            // Loading file
+            ob_start();
+            require_once '../inc/global.inc.php';
+            require_once $mainPath.$file;
+            $out = ob_get_contents();
+            ob_end_clean();
+
+            //var_dump($htmlHeadXtra);
+            if (isset($htmlHeadXtra)) {
+                $app['template']->addJsFiles($htmlHeadXtra);
+            }
+
+            if (isset($interbreadcrumb)) {
+                $app['template']->setBreadcrumb($interbreadcrumb);
+                $app['template']->loadBreadcrumbToTemplate();
+            }
+
+            if (isset($tpl)) {
+                $response = $app['twig']->render($app['default_layout']);
+            } else {
+                $app['template']->assign('content', $out);
+                $response = $app['twig']->render($app['default_layout']);
+            }
+        } else {
+            return $app->abort(404, 'File not found');
+        }
+
         return new Response($response, 200, array());
     }
 }