Root/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | <?php /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* # ***** BEGIN LICENSE BLOCK ***** # This file is part of Plume Framework, a simple PHP Application Framework. # Copyright (C) 2001-2007 Loic d'Anterroches and contributors. # # Plume Framework is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # # Plume Framework 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 Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # ***** END LICENSE BLOCK ***** */ /** * Session middleware. * * Allow a session object in the request and the automatic * login/logout of a user if a standard authentication against the * Pluf_User model is performed. */ class Pluf_Middleware_Session { /** * Process the request. * * When processing the request, if a session is found with * Pluf_User creditentials the corresponding user is loaded into * $request->user. * * FIXME: We should logout everybody when the session table is emptied. * * @param Pluf_HTTP_Request The request * @return bool false */ function process_request(& $request ) { $session = new Pluf_Session(); $user_model = Pluf::f( 'pluf_custom_user' , 'Pluf_User' ); $user = new $user_model (); if (!isset( $request ->COOKIE[ $session ->cookie_name])) { // No session is defined. We set an empty user and empty // session. $request ->user = $user ; $request ->session = $session ; if (isset( $request ->COOKIE[ $request ->session->test_cookie_name])) { $request ->session->test_cookie = $request ->COOKIE[ $request ->session->test_cookie_name]; } return false; } try { $data = self::_decodeData( $request ->COOKIE[ $session ->cookie_name]); } catch (Exception $e ) { $request ->user = $user ; $request ->session = $session ; if (isset( $request ->COOKIE[ $request ->session->test_cookie_name])) { $request ->session->test_cookie = $request ->COOKIE[ $request ->session->test_cookie_name]; } return false; } $set_lang = false; if (isset( $data [ $user ->session_key])) { // We can get the corresponding user $found_user = new $user_model ( $data [ $user ->session_key]); if ( $found_user ->id == $data [ $user ->session_key]) { // User found! $request ->user = $found_user ; // If the last login is from 12h or more, set it to // now. Pluf::loadFunction( 'Pluf_Date_Compare' ); if (43200 < Pluf_Date_Compare( $request ->user->last_login)) { $request ->user->last_login = gmdate ( 'Y-m-d H:i:s' ); $request ->user->update(); } $set_lang = $found_user ->language; } else { $request ->user = $user ; } } else { $request ->user = $user ; } if (isset( $data [ 'Pluf_Session_key' ])) { $sql = new Pluf_SQL( 'session_key=%s' , $data [ 'Pluf_Session_key' ]); $found_session = Pluf::factory( 'Pluf_Session' )->getList( array ( 'filter' => $sql ->gen())); if (isset( $found_session [0])) { $request ->session = $found_session [0]; } else { $request ->session = $session ; } } else { $request ->session = $session ; } if ( $set_lang and false == $request ->session->getData( 'pluf_language' , false)) { $request ->session->setData( 'pluf_language' , $set_lang ); } if (isset( $request ->COOKIE[ $request ->session->test_cookie_name])) { $request ->session->test_cookie = $request ->COOKIE[ $request ->session->test_cookie_name]; } return false; } /** * Process the response of a view. * * If the session has been modified save it into the database. * Add the session cookie to the response. * * @param Pluf_HTTP_Request The request * @param Pluf_HTTP_Response The response * @return Pluf_HTTP_Response The response */ function process_response( $request , $response ) { if ( $request ->session->touched) { if ( $request ->session->id > 0) { $request ->session->update(); } else { $request ->session->create(); } $data = array (); if ( $request ->user->id > 0) { $data [ $request ->user->session_key] = $request ->user->id; } $data [ 'Pluf_Session_key' ] = $request ->session->session_key; $response ->cookies[ $request ->session->cookie_name] = self::_encodeData( $data ); } if ( $request ->session->set_test_cookie != false) { $response ->cookies[ $request ->session->test_cookie_name] = $request ->session->test_cookie_value; } return $response ; } /** * Encode the cookie data and create a check with the secret key. * * @param mixed Data to encode * @return string Encoded data ready for the cookie */ public static function _encodeData( $data ) { if ( '' == ( $key = Pluf::f( 'secret_key' ))) { throw new Exception( 'Security error: "secret_key" is not set in the configuration file.' ); } $data = serialize( $data ); return base64_encode ( $data ).md5( base64_encode ( $data ). $key ); } /** * Decode the data and check that the data have not been tampered. * * If the data have been tampered an exception is raised. * * @param string Encoded data * @return mixed Decoded data */ public static function _decodeData( $encoded_data ) { $check = substr ( $encoded_data , -32); $base64_data = substr ( $encoded_data , 0, strlen ( $encoded_data )-32); if (md5( $base64_data .Pluf::f( 'secret_key' )) != $check ) { throw new Exception( 'The session data may have been tampered.' ); } return unserialize( base64_decode ( $base64_data )); } public static function processContext( $signal , & $params ) { $params [ 'context' ] = array_merge ( $params [ 'context' ], Pluf_Middleware_Session_ContextPreProcessor( $params [ 'request' ])); } } /** * Context preprocessor. * * Set the $user key. * * @param Pluf_HTTP_Request Request object * @return array Array to merge with the context */ function Pluf_Middleware_Session_ContextPreProcessor( $request ) { return array ( 'user' => $request ->user); } Pluf_Signal::connect( 'Pluf_Template_Context_Request::construct' , array ( 'Pluf_Middleware_Session' , 'processContext' )); |