Sfoglia il codice sorgente

autoload and utility classes

Laurent Opprecht 13 anni fa
parent
commit
22410183b1
3 ha cambiato i file con 140 aggiunte e 1 eliminazioni
  1. 3 1
      main/inc/global.inc.php
  2. 60 0
      main/inc/lib/autoload.class.php
  3. 77 0
      main/inc/lib/redirect.class.php

+ 3 - 1
main/inc/global.inc.php

@@ -92,7 +92,9 @@ ini_set('include_path', api_create_include_path_setting());
 ini_set('auto_detect_line_endings', '1');
 
 // Include the libraries that are necessary everywhere
-require_once $lib_path.'request.class.php';
+require_once $lib_path.'autoload.class.php';
+Autoload::register();
+
 require_once $lib_path.'database.lib.php';
 require_once $lib_path.'template.lib.php';
 require_once $lib_path.'display.lib.php';

+ 60 - 0
main/inc/lib/autoload.class.php

@@ -0,0 +1,60 @@
+<?php
+
+/**
+ * Autoload Chamilo classes
+ * 
+ * @license see /license.txt
+ * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
+ */
+class Autoload
+{
+
+    /**
+     * Register the Chamilo autoloader on the stack. 
+     */
+    static public function register()
+    {
+        $f = array(new self, 'load');
+        spl_autoload_register($f);
+    }
+
+    static public function map()
+    {
+        static $result = false;
+
+        if ($result)
+        {
+            return $result;
+        }
+
+        $dir = dirname(__FILE__);
+
+        $result = array();
+        $result['Redirect'] = $dir . '/redirect.class.php';
+        $result['Request'] = $dir . '/request.class.php';
+        return $result;
+    }
+
+    /**
+     * Handles autoloading of classes.
+     *
+     * @param  string  $class_name  A class name.
+     *
+     * @return boolean returns true if the class has been loaded
+     */
+    public function load($class_name)
+    {
+        $map = self::map();
+        if (isset($map[$class_name]))
+        {
+            $path = $map[$class_name];
+            require_once $path;
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+}

+ 77 - 0
main/inc/lib/redirect.class.php

@@ -0,0 +1,77 @@
+<?php
+
+/**
+ * Send a redirect to the user agent and exist
+ * 
+ * @license see /license.txt
+ * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
+ */
+class Redirect
+{
+
+    static function www()
+    {
+        static $result = false;
+        if (empty($result))
+        {
+            $result = api_get_path('WEB_PATH');
+        }
+        return $result;
+    }
+
+    static function go($url = '')
+    {
+        if (empty($url))
+        {
+            Redirect::session_request_uri();
+            $www = self::www();
+            self::navigate($www);
+        }
+
+        $is_full_uri = (strpos($url, 'http') === 0);
+        if ($is_full_uri)
+        {
+            self::navigate($url);
+        }
+
+        $url = self::www() . $url;
+        self::navigate($url);
+    }
+
+    /**
+     * Redirect to the session "request uri" if it exists. 
+     */
+    static function session_request_uri()
+    {
+//        if (api_is_anonymous())
+//        {
+//            return;
+//        }
+        $url = isset($_SESSION['request_uri']) ? $_SESSION['request_uri'] : '';
+        unset($_SESSION['request_uri']);
+        if ($url)
+        {
+            self::navigate($url);
+        }
+    }
+
+    static function home()
+    {
+        $www = self::www();
+        self::navigate($www);
+    }
+
+    static function user_home()
+    {
+        $www = self::www();
+        self::navigate("$www/user_portal.php");
+    }
+
+    protected static function navigate($url)
+    {
+        session_write_close(); //should not be neeeded 
+        header("Location: $url");
+        exit;
+    }
+
+}