|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZPreferences class 00004 // 00005 // Created on: <11-Aug-2003 13:23:55 bf> 00006 // 00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00008 // SOFTWARE NAME: eZ Publish 00009 // SOFTWARE RELEASE: 4.0.x 00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00011 // SOFTWARE LICENSE: GNU General Public License v2.0 00012 // NOTICE: > 00013 // This program is free software; you can redistribute it and/or 00014 // modify it under the terms of version 2.0 of the GNU General 00015 // Public License as published by the Free Software Foundation. 00016 // 00017 // This program is distributed in the hope that it will be useful, 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of version 2.0 of the GNU General 00023 // Public License along with this program; if not, write to the Free 00024 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00025 // MA 02110-1301, USA. 00026 // 00027 // 00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00029 // 00030 00031 /*! \file ezpreferences.php 00032 */ 00033 00034 /*! 00035 \class eZPreferences ezpreferences.php 00036 \brief Handles user/session preferences 00037 00038 Preferences can be either pr user or pr session. eZPreferences will automatically 00039 set a session preference if the user is not logged in, if not a user preference will be set. 00040 00041 */ 00042 00043 //include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' ); 00044 //include_once( "lib/ezdb/classes/ezdb.php" ); 00045 00046 00047 class eZPreferences 00048 { 00049 const SESSION_NAME = "eZPreferences"; 00050 00051 /*! 00052 \static 00053 Sets a preference value for a given user. If 00054 the user is anonymous the value is only stored in session. 00055 00056 \param $name The name of the preference to store 00057 \param $value The value of the preference to store 00058 \param $storeUserID The user which should get the preference, 00059 if \c false it will use the current user 00060 \return \c true if the preference was stored correctly or \c false if something went wrong 00061 \note Transaction unsafe. If you call several transaction unsafe methods you must enclose 00062 the calls within a db transaction; thus within db->begin and db->commit. 00063 */ 00064 static function setValue( $name, $value, $storeUserID = false ) 00065 { 00066 $db = eZDB::instance(); 00067 $name = $db->escapeString( $name ); 00068 $rawValue = $value; 00069 $value = $db->escapeString( $value ); 00070 00071 $isCurrentUser = true; 00072 if ( $storeUserID === false ) 00073 { 00074 $user = eZUser::currentUser(); 00075 } 00076 else 00077 { 00078 $currentID = eZUser::currentUserID(); 00079 if ( $currentID != $storeUserID ) 00080 $isCurrentUser = false; 00081 00082 $user = eZUser::fetch( $storeUserID ); 00083 if ( !is_object( $user ) ) 00084 { 00085 eZDebug::writeError( "Cannot set preference for user $storeUserID, the user does not exist" ); 00086 return false; 00087 } 00088 } 00089 00090 // We must store the database changes if: 00091 // a - The current user is logged in (ie. not anonymous) 00092 // b - We have specified a specific user (not the current). 00093 // in which case isLoggedIn() will fail. 00094 if ( $storeUserID !== false or $user->isLoggedIn() ) 00095 { 00096 // Only store in DB if user is logged in or we have 00097 // a specific user ID defined 00098 $userID = $user->attribute( 'contentobject_id' ); 00099 $existingRes = $db->arrayQuery( "SELECT * FROM ezpreferences WHERE user_id = $userID AND name='$name'" ); 00100 00101 if ( count( $existingRes ) > 0 ) 00102 { 00103 $prefID = $existingRes[0]['id']; 00104 $query = "UPDATE ezpreferences SET value='$value' WHERE id = $prefID AND name='$name'"; 00105 $db->query( $query ); 00106 } 00107 else 00108 { 00109 $query = "INSERT INTO ezpreferences ( user_id, name, value ) VALUES ( $userID, '$name', '$value' )"; 00110 $db->query( $query ); 00111 } 00112 } 00113 00114 // We also store in session if this is the current user (anonymous or normal user) 00115 // use $rawValue as value will be escaped by session code (see #014520) 00116 if ( $isCurrentUser ) 00117 { 00118 eZPreferences::storeInSession( $name, $rawValue ); 00119 } 00120 00121 return true; 00122 } 00123 00124 /*! 00125 \static 00126 \param $user The user object to read preferences for, if \c false it will read using the current user. 00127 \return The preference value for the specified user. 00128 If no variable is found \c false is returned. 00129 \note The preferences variable will be stored in session after fetching 00130 if the specified user is the current user. 00131 */ 00132 static function value( $name, $user = false ) 00133 { 00134 if ( !( $user instanceof eZUser ) ) 00135 $user = eZUser::currentUser(); 00136 00137 $value = false; 00138 // If the user object is not the currently logged in user we cannot use the session values 00139 $http = eZHTTPTool::instance(); 00140 $useCache = ( $user->ContentObjectID == $http->sessionVariable( 'eZUserLoggedInID' ) ); 00141 if ( $useCache and eZPreferences::isStoredInSession( $name ) ) 00142 return eZPreferences::storedSessionValue( $name ); 00143 00144 // If this the anonymous user we should return false, no need to check database. 00145 if ( $user->isAnonymous() ) 00146 return false; 00147 00148 $db = eZDB::instance(); 00149 $name = $db->escapeString( $name ); 00150 $userID = $user->attribute( 'contentobject_id' ); 00151 $existingRes = $db->arrayQuery( "SELECT value FROM ezpreferences WHERE user_id = $userID AND name = '$name'" ); 00152 00153 if ( count( $existingRes ) == 1 ) 00154 { 00155 $value = $existingRes[0]['value']; 00156 if ( $useCache ) 00157 eZPreferences::storeInSession( $name, $value ); 00158 } 00159 else 00160 { 00161 if ( $useCache ) 00162 eZPreferences::storeInSession( $name, false ); 00163 } 00164 return $value; 00165 } 00166 00167 /*! 00168 \static 00169 \param $user The user object to read preferences for, if \c false it will read using the current user. 00170 \return An array with all the preferences for the specified user. 00171 If the user is not logged in the empty array will be returned. 00172 */ 00173 static function values( $user = false ) 00174 { 00175 if ( !( $user instanceof eZUser ) ) 00176 $user = eZUser::currentUser(); 00177 00178 if ( !$user->isAnonymous() ) 00179 { 00180 // If the user object is not the currently logged in user we cannot use the session values 00181 $http = eZHTTPTool::instance(); 00182 $useCache = ( $user->ContentObjectID == $http->sessionVariable( 'eZUserLoggedInID' ) ); 00183 00184 $returnArray = array(); 00185 $userID = $user->attribute( 'contentobject_id' ); 00186 $db = eZDB::instance(); 00187 $values = $db->arrayQuery( "SELECT name,value FROM ezpreferences WHERE user_id=$userID ORDER BY id" ); 00188 foreach ( $values as $item ) 00189 { 00190 if ( $useCache ) 00191 eZPreferences::storeInSession( $item['name'], $item['value'] ); 00192 $returnArray[$item['name']] = $item['value']; 00193 } 00194 return $returnArray; 00195 } 00196 else 00197 { 00198 // For the anonymous user we just return all values 00199 $http = eZHTTPTool::instance(); 00200 if ( $http->hasSessionVariable( eZPreferences::SESSION_NAME ) ) 00201 return $http->sessionVariable( eZPreferences::SESSION_NAME ); 00202 return array(); 00203 } 00204 } 00205 00206 /*! 00207 \static 00208 Makes sure the stored session values are cleaned up. 00209 */ 00210 static function sessionCleanup() 00211 { 00212 $http = eZHTTPTool::instance(); 00213 $http->removeSessionVariable( eZPreferences::SESSION_NAME ); 00214 } 00215 00216 /*! 00217 \static 00218 Makes sure the preferences named \a $name is stored in the session with the value \a $value. 00219 */ 00220 static function storeInSession( $name, $value ) 00221 { 00222 $http = eZHTTPTool::instance(); 00223 $preferencesInSession = array(); 00224 if ( $http->hasSessionVariable( eZPreferences::SESSION_NAME ) ) 00225 $preferencesInSession = $http->sessionVariable( eZPreferences::SESSION_NAME ); 00226 $preferencesInSession[$name] = $value; 00227 $http->setSessionVariable( eZPreferences::SESSION_NAME, $preferencesInSession ); 00228 } 00229 00230 /*! 00231 \static 00232 \return \c true if the preference named \a $name is stored in session. 00233 */ 00234 static function isStoredInSession( $name ) 00235 { 00236 $http = eZHTTPTool::instance(); 00237 if ( !$http->hasSessionVariable( eZPreferences::SESSION_NAME ) ) 00238 return false; 00239 $preferencesInSession = $http->sessionVariable( eZPreferences::SESSION_NAME ); 00240 return array_key_exists( $name, $preferencesInSession ); 00241 } 00242 00243 /*! 00244 \static 00245 \return the stored preferenced value found in the session or \c null if none were found. 00246 */ 00247 static function storedSessionValue( $name ) 00248 { 00249 $http = eZHTTPTool::instance(); 00250 if ( !$http->hasSessionVariable( eZPreferences::SESSION_NAME ) ) 00251 return null; 00252 $preferencesInSession = $http->sessionVariable( eZPreferences::SESSION_NAME ); 00253 if ( !array_key_exists( $name, $preferencesInSession ) ) 00254 return null; 00255 return $preferencesInSession[$name]; 00256 } 00257 00258 /*! 00259 \static 00260 Removes all preferences for all users. 00261 \note Transaction unsafe. If you call several transaction unsafe methods you must enclose 00262 the calls within a db transaction; thus within db->begin and db->commit. 00263 */ 00264 static function cleanup() 00265 { 00266 $db = eZDB::instance(); 00267 $db->query( "DELETE FROM ezpreferences" ); 00268 } 00269 } 00270 00271 00272 ?>