]> git.mxchange.org Git - shipsimu.git/commitdiff
Cookie-based login initially done
authorRoland Häder <roland@mxchange.org>
Sat, 14 Jun 2008 19:20:03 +0000 (19:20 +0000)
committerRoland Häder <roland@mxchange.org>
Sat, 14 Jun 2008 19:20:03 +0000 (19:20 +0000)
16 files changed:
.gitattributes
application/ship-simu/config.php
application/ship-simu/main/login/class_ShipSimuUserLogin.php
application/ship-simu/main/login/helper/class_ShipSimuLoginHelper.php
inc/classes/interfaces/helper/.htaccess [new file with mode: 0644]
inc/classes/interfaces/helper/class_HelpableLogin.php [new file with mode: 0644]
inc/classes/interfaces/login/class_LoginableUser.php
inc/classes/interfaces/response/class_Responseable.php
inc/classes/main/actions/post_registration/class_LoginAfterRegistrationAction.php
inc/classes/main/helper/web/class_WebFormHelper.php
inc/classes/main/login/.htaccess [new file with mode: 0644]
inc/classes/main/login/class_CookieLogin.php [new file with mode: 0644]
inc/classes/main/response/class_HttpResponse.php
inc/config.php
inc/config/class_FrameworkConfiguration.php
templates/de/code/emergency_exit.ctp

index e2ffacadf95cb017ec7642278bf56bfa26fd4325..81369b8ddcf52a4a673f02fad35965756c2a45c9 100644 (file)
@@ -259,6 +259,8 @@ inc/classes/interfaces/extended/.htaccess -text
 inc/classes/interfaces/extended/class_LimitableObject.php -text
 inc/classes/interfaces/filter/.htaccess -text
 inc/classes/interfaces/filter/class_Filterable.php -text
+inc/classes/interfaces/helper/.htaccess -text
+inc/classes/interfaces/helper/class_HelpableLogin.php -text
 inc/classes/interfaces/io/.htaccess -text
 inc/classes/interfaces/io/class_Streamable.php -text
 inc/classes/interfaces/io/file/.htaccess -text
@@ -376,6 +378,8 @@ inc/classes/main/io/class_FrameworkFileInputPointer.php -text
 inc/classes/main/io/class_FrameworkFileOutputPointer.php -text
 inc/classes/main/language/.htaccess -text
 inc/classes/main/language/class_LanguageSystem.php -text
+inc/classes/main/login/.htaccess -text
+inc/classes/main/login/class_CookieLogin.php -text
 inc/classes/main/output/.htaccess -text
 inc/classes/main/output/class_ConsoleOutput.php -text
 inc/classes/main/output/class_WebOutput.php -text
index 3d4b877bac31465621784ae47b344c1100a75236..a827e380cbe3c6169b3997e5d1226b3180dc23f1 100644 (file)
@@ -82,5 +82,11 @@ $cfg->setConfigEntry('user_status_register', "UNCONFIRMED");
 // CFG: LOGIN-HELPER
 $cfg->setConfigEntry('login_helper', "ShipSimuLoginHelper");
 
+// CFG: LOGIN-METHOD
+$cfg->setConfigEntry('login_method', "cookie");
+
+// CFG: APP-LOGIN-URL
+$cfg->setConfigEntry('app_login_url', "index.php?app=ship-simu&page=login_area");
+
 // [EOF]
 ?>
index a043de8ba74c0ed28cedc5ab4621ebdcd608ab6c..59ddcdcb6b9f8619191dbabb3aeb1732281d0aaa 100644 (file)
@@ -61,6 +61,7 @@ class ShipSimuUserLogin extends BaseFrameworkSystem implements LoginableUser {
         * in a boolean attribute which is then readable by a matching getter.
         *
         * @param       $requestInstance        An instance of a Requestable class
+        * @param       $responseInstance       An instance of a Responseable class
         * @return      void
         * @throws      UserLoginMethodException        If wether username nor email login
         *                                                                              was detected
@@ -69,7 +70,7 @@ class ShipSimuUserLogin extends BaseFrameworkSystem implements LoginableUser {
         * @throws      UserPasswordMismatchException   If the supplied password did not
         *                                                                              match with the stored password
         */
-       public function doLogin (Requestable $requestInstance) {
+       public function doLogin (Requestable $requestInstance, Responseable $responseInstance) {
                // By default no method is selected
                $method = null;
                $data = "";
@@ -118,7 +119,20 @@ class ShipSimuUserLogin extends BaseFrameworkSystem implements LoginableUser {
                $helperInstance = ObjectFactory::createObjectByConfiguredName('login_helper', array($requestInstance));
 
                // 2) Execute the login. This will now login...
-               $helperInstance->executeLogin();
+               $helperInstance->executeLogin($responseInstance);
+       }
+
+       /**
+        * Determines wether the login was fine. This is done by checking if the 'login' instance is in registry
+        *
+        * @return      $loginDone      Wether the login was fine or not
+        */
+       public function ifLoginWasSuccessfull () {
+               // Is the registry key there?
+               $loginDone = (Registry::getRegistry()->getInstance('login') instanceof Registerable);
+
+               // Return the result
+               return $loginDone;
        }
 }
 
index 13bed29d1e8ee961b2e95fd131be8f0f2dcc7e93..9dd7f96fb34a815a11e1b2e6c13a9b8a29af9347 100644 (file)
@@ -15,7 +15,6 @@
  * @copyright  Copyright(c) 2007, 2008 Roland Haeder, this is free software
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
- * @todo               Find an interface name for login helper
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-class ShipSimuLoginHelper extends BaseLoginHelper {
+class ShipSimuLoginHelper extends BaseLoginHelper implements HelpableLogin {
+       /**
+        * The login method we shall choose
+        */
+       private $loginMethod = "";
+
+       /**
+        * Instance for a request class
+        */
+       private $requestInstance = null;
+
        // Exception constants
        const EXCEPTION_INVALID_USER_INSTANCE = 0xf00;
 
@@ -88,11 +97,67 @@ class ShipSimuLoginHelper extends BaseLoginHelper {
                        // Set default login method from config
                        $helperInstance->setDefaultLoginMethod();
                }
-               
+
+               // Set request instance
+               $helperInstance->setRequestInstance($requestInstance);
+
                // Return the prepared instance
                return $helperInstance;
        }
+
+       /**
+        * Setter for default login method from config
+        *
+        * @return      void
+        */
+       protected function setDefaultLoginMethod () {
+               $this->loginMethod = $this->getConfigInstance()->readConfig('login_method');
+       }
+
+       /**
+        * Setter for request instance
+        *
+        * @param       $requestInstance        A Requestable class instance
+        * @return      void
+        */
+       public final function setRequestInstance (Requestable $requestInstance) {
+               $this->requestInstance = $requestInstance;
+       }
+
+       /**
+        * Getter for request instance
+        *
+        * @param       
+        * @return      $requestInstance        A Requestable class instance
+        */
+       public final function getRequestInstance () {
+               return $this->requestInstance;
+       }
+
+       /**
+        * Execute the login request by given response instance. This instance can
+        * be used for sending cookies or at least the session id out.
+        *
+        * @param       $responseInstance       An instance of a Responseable class
+        * @return      void
+        */
+       public function executeLogin (Responseable $responseInstance) {
+               // First create the requested login method name
+               $loginMethodClass = ucfirst(strtolower($this->loginMethod)) . "Login";
+
+               // Then try to get an instance from it
+               $loginInstance = ObjectFactory::createObjectByName($loginMethodClass, array($responseInstance));
+
+               // Set user cookie
+               $loginInstance->setUserAuth($this->requestInstance->getRequestElement('username'));
+
+               // Set password cookie
+               $loginInstance->setPasswordAuth($this->requestInstance->getRequestElement('pass_hash'));
+
+               // Remember this login instance for later usage
+               Registry::getRegistry()->addInstance('login', $loginInstance);
+       }
 }
 
-// [EOF]
+//
 ?>
diff --git a/inc/classes/interfaces/helper/.htaccess b/inc/classes/interfaces/helper/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/inc/classes/interfaces/helper/class_HelpableLogin.php b/inc/classes/interfaces/helper/class_HelpableLogin.php
new file mode 100644 (file)
index 0000000..cf5f84f
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+/**
+ * A helper interface for logins
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright(c) 2007, 2008 Roland Haeder, this is free software
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+interface HelpableLogin extends FrameworkInterface {
+       /**
+        * Execute the login request by given response instance. This instance can
+        * be used for sending cookies or at least the session id out.
+        *
+        * @param       $responseInstance       An instance of a Responseable class
+        * @return      void
+        */
+       function executeLogin (Responseable $responseInstance);
+}
+
+//
+?>
index 962cba8718086fa7c2494bfd03b5d132f152600c..107b13387ce19f8f730b80f5a4fa7fa68575af63 100644 (file)
@@ -28,9 +28,10 @@ interface LoginableUser extends FrameworkInterface {
         * in a boolean attribute which is then readable by a matching getter.
         *
         * @param       $requestInstance        An instance of a Requestable class
+        * @param       $responseInstance       An instance of a Responseable class
         * @return      void
         */
-       function doLogin (Requestable $requestInstance);
+       function doLogin (Requestable $requestInstance, Responseable $responseInstance);
 }
 
 //
index c7d21032d2223c6be2d3907e0be498e011c01c55..5fd6b50cc4a613fc6f9c7b75fefbe8065fabde7d 100644 (file)
@@ -56,7 +56,7 @@ interface Responseable extends FrameworkInterface {
         * @throws      ResponseHeadersAlreadySentException             Thrown if headers are
         *                                                                                                      already sent
         */
-       function flushBuffer($force=false);
+       function flushBuffer ($force = false);
 
        /**
         * Adds a fatal message id to the response. The added messages can then be
@@ -66,6 +66,27 @@ interface Responseable extends FrameworkInterface {
         * @return      void
         */
        function addFatalMessage ($messageId);
+
+       /**
+        * Adds a cookie to the response
+        *
+        * @param       $cookieName             Cookie's name
+        * @param       $cookieValue    Value to store in the cookie
+        * @param       $encrypted              Do some extra encryption on the value
+        * @return      void
+        * @throws      ResponseHeadersAlreadySentException             If headers are already sent
+        */
+       function addCookie ($cookieName, $cookieValue, $encrypted = false);
+
+       /**
+        * Redirect to a configured URL. The URL can be absolute or relative. In
+        * case of relative URL it will be extended automatically.
+        *
+        * @param       $configEntry    The configuration entry which holds our URL
+        * @return      void
+        * @throws      ResponseHeadersAlreadySentException             If headers are already sent
+        */
+       function redirectToConfiguredUrl ($configEntry);
 }
 
 //
index aae33eb5c2c8f0d67d7c97ba8b20bfc0218aa754..63c4450fa817a693dd960ca60e47aa0af3ed3d8f 100644 (file)
@@ -63,13 +63,13 @@ class LoginAfterRegistrationAction extends BaseAction implements Commandable {
                $loginInstance = ObjectFactory::createObjectByConfiguredName('login_user');
 
                // Login the user by the request instance
-               $loginInstance->doLogin($requestInstance);
+               $loginInstance->doLogin($requestInstance, $responseInstance);
 
                // Was the login fine? Then redirect here
                if ($loginInstance->ifLoginWasSuccessfull()) {
                        // Try to redirect here
                        try {
-                               $responseInstance->redirectConfiguredUrl('app_login');
+                               $responseInstance->redirectToConfiguredUrl('app_login_url');
                        } catch (FrameworkException $e) {
                                // Something went wrong here!
                                $responseInstance->addFatalMessage($e->getMessage());
index e71f15137d7a414d7a1ac1f28c38262585ca90d2..718ed77871ff0d8ee98c0adcd374692554be56f5 100644 (file)
@@ -119,8 +119,9 @@ class WebFormHelper extends BaseHelper {
                // Check wether we shall open or close the form
                if ($this->formOpened === false) {
                        // Add HTML code
-                       $formContent = sprintf("<form name=\"%s\" class=\"forms\" action=\"%s\" method=\"%s\" target=\"%s\"",
+                       $formContent = sprintf("<form name=\"%s\" class=\"forms\" action=\"%s/%s\" method=\"%s\" target=\"%s\"",
                                $formName,
+                               $this->getConfigInstance()->readConfig('base_url'),
                                $this->getConfigInstance()->readConfig('form_action'),
                                $this->getConfigInstance()->readConfig('form_method'),
                                $this->getConfigInstance()->readConfig('form_target')
diff --git a/inc/classes/main/login/.htaccess b/inc/classes/main/login/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/inc/classes/main/login/class_CookieLogin.php b/inc/classes/main/login/class_CookieLogin.php
new file mode 100644 (file)
index 0000000..cbc9203
--- /dev/null
@@ -0,0 +1,99 @@
+<?php
+/**
+ * A cookie-bases login class
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright(c) 2007, 2008 Roland Haeder, this is free software
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+class CookieLogin extends BaseFrameworkSystem implements Registerable {
+       /**
+        * Response instance
+        */
+       private $responseInstance = null;
+
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+
+               // Set part description
+               $this->setObjectDescription("Cookie-based login");
+
+               // Create unique ID number
+               $this->generateUniqueId();
+
+               // Clean up a little
+               $this->removeNumberFormaters();
+               $this->removeSystemArray();
+       }
+
+       /**
+        * Creates an instance of this class by the given response instance
+        *
+        * @param       $responseInstance       An instance of a Responseable class
+        * @return      $loginInstance          An instance of this login class
+        */
+       public final static function createCookieLogin (Responseable $responseInstance) {
+               // Get a new instance
+               $loginInstance = new CookieLogin();
+
+               // Set the response instance
+               $loginInstance->setResponseInstance($responseInstance);
+
+               // Return the prepared instance
+               return $loginInstance;
+       }
+
+       /**
+        * Setter for login instance
+        *
+        * @param       $responseInstance       An instance of a Responseable class
+        * @return      void
+        */
+       protected final function setResponseInstance (Responseable $responseInstance) {
+               $this->responseInstance = $responseInstance;
+       }
+
+       /**
+        * "Setter" for username auth data
+        *
+        * @param       $userName       The username from request we shall set
+        * @return      void
+        */
+       public function setUserAuth ($userName) {
+               $this->responseInstance->addCookie('username', $userName);
+       }
+
+       /**
+        * "Setter" for password hash auth data
+        *
+        * @param       $passHash       The hashed password from request we shall set
+        * @return      void
+        */
+       public function setPasswordAuth ($passHash) {
+               $this->responseInstance->addCookie('u_hash', $passHash, true);
+       }
+}
+
+// [EOF]
+?>
index 074373d1ea00655dec310069f75ebe6e9ef26463..bc7693335eb858e09321834c7a58b9a06604a183 100644 (file)
@@ -136,7 +136,7 @@ class HttpResponse extends BaseFrameworkSystem implements Responseable {
         * @param       $output         Output we shall sent in the HTTP response
         * @return      void
         */
-       public function setReponseBody ($output) {
+       public function setResponseBody ($output) {
                $this->responseBody = $output;
        }
 
@@ -187,7 +187,7 @@ class HttpResponse extends BaseFrameworkSystem implements Responseable {
                }
 
                // Clear response header and body
-               $this->setReponseBody("");
+               $this->setResponseBody("");
                $this->resetResponseHeaders();
        }
 
@@ -221,6 +221,84 @@ class HttpResponse extends BaseFrameworkSystem implements Responseable {
                // Adds the resolved message id to the fatal message list
                $this->fatalMessages[] = $this->getApplicationInstance()->getLanguageInstance()->getMessage($messageId);
        }
+
+       /**
+        * Adds a cookie to the response
+        *
+        * @param       $cookieName             Cookie's name
+        * @param       $cookieValue    Value to store in the cookie
+        * @param       $encrypted              Do some extra encryption on the value
+        * @return      void
+        * @throws      ResponseHeadersAlreadySentException             If headers are already sent
+        */
+       public function addCookie ($cookieName, $cookieValue, $encrypted = false) {
+               // Are headers already sent?
+               if (headers_sent()) {
+                       // Throw an exception here
+                       throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
+               } // END - if
+
+               // Shall we encrypt the cookie?
+               if ($encrypted === true) {
+                       // @TODO Encryption of cookie data not yet supported
+               } // END - if
+
+               // Set the cookie
+               setcookie(
+                       $cookieName,
+                       $cookieValue,
+                       $this->getConfigInstance()->readConfig('cookie_expire'),
+                       $this->getConfigInstance()->readConfig('cookie_path'),
+                       $this->getConfigInstance()->readConfig('cookie_domain'),
+                       $this->getConfigInstance()->readConfig('cookie_ssl')
+               );
+       }
+
+       /**
+        * Redirect to a configured URL. The URL can be absolute or relative. In
+        * case of relative URL it will be extended automatically.
+        *
+        * @param       $configEntry    The configuration entry which holds our URL
+        * @return      void
+        * @throws      ResponseHeadersAlreadySentException             If headers are already sent
+        */
+       public function redirectToConfiguredUrl ($configEntry) {
+               // Is the header not yet sent?
+               if (headers_sent()) {
+                       // Throw an exception here
+                       throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
+               } // END - if
+
+               // Get the url from config
+               $url = $this->getConfigInstance()->readConfig($configEntry);
+
+               // Do we have a "http" in front of the URL?
+               if (substr(strtolower($url), 0, 4) != "http") {
+                       // Is there a / in front of the relative URL?
+                       if (substr($url, 0, 1) == "/") $url = substr($url, 1);
+
+                       // No, then extend it with our base URL
+                       $url = $this->getConfigInstance()->readConfig('base_url') . "/" . $url;
+               } // END - if
+
+               // Clean response headers
+               $this->resetResponseHeaders();
+
+               // Add redirect header
+               $this->addHeader("Location", $url);
+
+               // Set correct response status
+               $this->setResponseStatus("301 Moved Permanently");
+
+               // Clear the body
+               $this->setResponseBody("");
+
+               // Flush the result
+               $this->flushBuffer();
+
+               // All done here...
+               exit();
+       }
 }
 
 // [EOF]
index 0b450d8c730a9c52c7275d5faa2e6c694bb89aec..817609c98c8e3d47d6e6841c7712873dce679991 100644 (file)
@@ -35,6 +35,9 @@ $cfg = FrameworkConfiguration::createFrameworkConfiguration();
 // CFG: SERVER-PATH
 $cfg->definePath(dirname(dirname(__FILE__)) . '/'); // DON'T MISS THE TRAILING SLASH!!!
 
+// CFG: BASE-URL
+$cfg->setConfigEntry('base_url', $cfg->detectBaseUrl());
+
 // CFG: DATABASE-TYPE
 $cfg->defineDatabaseType('local');
 
@@ -218,5 +221,17 @@ $cfg->setConfigEntry('post_registration_action', "LoginAfterRegistrationAction")
 // CFG: USER-CLASS
 $cfg->setConfigEntry('user_class', "User");
 
+// CFG: COOKIE-EXPIRE
+$cfg->setConfigEntry('cookie_expire', 60*60*24*2); // Two hours!
+
+// CFG: COOKIE-PATH
+$cfg->setConfigEntry('cookie_path', dirname($_SERVER['SCRIPT_NAME']) . "/");
+
+// CFG: COOKIE-DOMAIN
+$cfg->setConfigEntry('cookie_domain', $cfg->readConfig('base_url')); // Is mostly the same...
+
+// CFG: COOKIE-SSL
+$cfg->setConfigEntry('cookie_ssl', (isset($_SERVER['HTTPS'])));
+
 // [EOF]
 ?>
index 6bfc3a26ce0fbfd1fbec6d0396a529c5b3da6bf2..5e9b87ef93ff72aa00d7a03d858522c1f07183b4 100644 (file)
@@ -169,7 +169,7 @@ class FrameworkConfiguration implements Registerable {
        /**
         * Define the local file path
         *
-        * @param               $path   The database type. See path inc/database/.
+        * @param               $path   Local file path for include files.
         * @return      void
         */
        public function definePath ($path) {
@@ -261,6 +261,28 @@ class FrameworkConfiguration implements Registerable {
        public function __toString () {
                return get_class($this);
        }
+
+       /**
+        * Dectect and return the base URL for all URLs and forms
+        *
+        * @return      $baseUrl        Detected base URL
+        */
+       public function detectBaseUrl() {
+               // Initialize the URL
+               $baseUrl = "http";
+
+               // Do we have HTTPS?
+               if (isset($_SERVER['HTTPS'])) {
+                       // Add the >s< for HTTPS
+                       $baseUrl .= "s";
+               } // END - if
+
+               // Construct the full URL now and secure it against CSRF attacks
+               $baseUrl = $baseUrl . "://" . htmlentities(strip_tags($_SERVER['SERVER_NAME']), ENT_QUOTES) . dirname($_SERVER['SCRIPT_NAME']);
+
+               // Return the URL
+               return $baseUrl;
+       }
 } // END - class
 
 // [EOF]
index d5c6f906a96fcd7e92a9ea45637d7517b0fca8d1..29725af3dc9c8a33f664f09fbf2ef00bffd8e04d 100644 (file)
                </div>
        </div>
 
+       <div id="stats_box">
+               <div id="stats_header">
+                       Statistics
+               </div>
+               <div id="stats_objects">
+                       Total objects: $content[total_objects]
+               </div>
+       </div>
+
 {?footer_msg:footer_msg="Please contact the support and supply the full above message, if you think you are not qualified to fix this problem."?}