|
eZ Publish
[trunk]
|
00001 <?php 00002 /** 00003 * File containing the eZPreferences class. 00004 * 00005 * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved. 00006 * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 00007 * @version //autogentag// 00008 * @package kernel 00009 */ 00010 00011 /*! 00012 \class eZPreferences ezpreferences.php 00013 \brief Handles user/session preferences 00014 00015 Preferences can be either pr user or pr session. eZPreferences will automatically 00016 set a session preference if the user is not logged in, if not a user preference will be set. 00017 00018 */ 00019 00020 00021 class eZPreferences 00022 { 00023 const SESSION_NAME = "eZPreferences"; 00024 00025 /*! 00026 \static 00027 Sets a preference value for a given user. If 00028 the user is anonymous the value is only stored in session. 00029 00030 \param $name The name of the preference to store 00031 \param $value The value of the preference to store 00032 \param $storeUserID The user which should get the preference, 00033 if \c false it will use the current user 00034 \return \c true if the preference was stored correctly or \c false if something went wrong 00035 \note Transaction unsafe. If you call several transaction unsafe methods you must enclose 00036 the calls within a db transaction; thus within db->begin and db->commit. 00037 */ 00038 static function setValue( $name, $value, $storeUserID = false ) 00039 { 00040 $db = eZDB::instance(); 00041 $name = $db->escapeString( $name ); 00042 $rawValue = $value; 00043 $value = $db->escapeString( $value ); 00044 00045 $isCurrentUser = true; 00046 if ( $storeUserID === false ) 00047 { 00048 $user = eZUser::currentUser(); 00049 } 00050 else 00051 { 00052 $currentID = eZUser::currentUserID(); 00053 if ( $currentID != $storeUserID ) 00054 $isCurrentUser = false; 00055 00056 $user = eZUser::fetch( $storeUserID ); 00057 if ( !is_object( $user ) ) 00058 { 00059 eZDebug::writeError( "Cannot set preference for user $storeUserID, the user does not exist" ); 00060 return false; 00061 } 00062 } 00063 00064 // We must store the database changes if: 00065 // a - The current user is logged in (ie. not anonymous) 00066 // b - We have specified a specific user (not the current). 00067 // in which case isLoggedIn() will fail. 00068 if ( $storeUserID !== false or $user->isLoggedIn() ) 00069 { 00070 // Only store in DB if user is logged in or we have 00071 // a specific user ID defined 00072 $userID = $user->attribute( 'contentobject_id' ); 00073 $existingRes = $db->arrayQuery( "SELECT * FROM ezpreferences WHERE user_id = $userID AND name='$name'" ); 00074 00075 if ( count( $existingRes ) > 0 ) 00076 { 00077 $prefID = $existingRes[0]['id']; 00078 $query = "UPDATE ezpreferences SET value='$value' WHERE id = $prefID AND name='$name'"; 00079 $db->query( $query ); 00080 } 00081 else 00082 { 00083 $query = "INSERT INTO ezpreferences ( user_id, name, value ) VALUES ( $userID, '$name', '$value' )"; 00084 $db->query( $query ); 00085 } 00086 } 00087 00088 // We also store in session if this is the current user (anonymous or normal user) 00089 // use $rawValue as value will be escaped by session code (see #014520) 00090 if ( $isCurrentUser ) 00091 { 00092 eZPreferences::storeInSession( $name, $rawValue ); 00093 } 00094 00095 return true; 00096 } 00097 00098 /*! 00099 \static 00100 \param $user The user object to read preferences for, if \c false it will read using the current user. 00101 \return The preference value for the specified user. 00102 If no variable is found \c false is returned. 00103 \note The preferences variable will be stored in session after fetching 00104 if the specified user is the current user. 00105 */ 00106 static function value( $name, $user = false ) 00107 { 00108 if ( !( $user instanceof eZUser ) ) 00109 $user = eZUser::currentUser(); 00110 00111 $value = false; 00112 // If the user object is not the currently logged in user we cannot use the session values 00113 $http = eZHTTPTool::instance(); 00114 $useCache = ( $user->ContentObjectID == $http->sessionVariable( 'eZUserLoggedInID', false ) ); 00115 if ( $useCache and eZPreferences::isStoredInSession( $name ) ) 00116 return eZPreferences::storedSessionValue( $name ); 00117 00118 // If this the anonymous user we should return false, no need to check database. 00119 if ( $user->isAnonymous() ) 00120 return false; 00121 00122 $db = eZDB::instance(); 00123 $name = $db->escapeString( $name ); 00124 $userID = $user->attribute( 'contentobject_id' ); 00125 $existingRes = $db->arrayQuery( "SELECT value FROM ezpreferences WHERE user_id = $userID AND name = '$name'" ); 00126 00127 if ( count( $existingRes ) == 1 ) 00128 { 00129 $value = $existingRes[0]['value']; 00130 if ( $useCache ) 00131 eZPreferences::storeInSession( $name, $value ); 00132 } 00133 else 00134 { 00135 if ( $useCache ) 00136 eZPreferences::storeInSession( $name, false ); 00137 } 00138 return $value; 00139 } 00140 00141 /*! 00142 \static 00143 \param $user The user object to read preferences for, if \c false it will read using the current user. 00144 \return An array with all the preferences for the specified user. 00145 If the user is not logged in the empty array will be returned. 00146 */ 00147 static function values( $user = false ) 00148 { 00149 if ( !( $user instanceof eZUser ) ) 00150 $user = eZUser::currentUser(); 00151 00152 if ( !$user->isAnonymous() ) 00153 { 00154 // If the user object is not the currently logged in user we cannot use the session values 00155 $http = eZHTTPTool::instance(); 00156 $useCache = ( $user->ContentObjectID == $http->sessionVariable( 'eZUserLoggedInID', false ) ); 00157 00158 $returnArray = array(); 00159 $userID = $user->attribute( 'contentobject_id' ); 00160 $db = eZDB::instance(); 00161 $values = $db->arrayQuery( "SELECT name,value FROM ezpreferences WHERE user_id=$userID ORDER BY id" ); 00162 foreach ( $values as $item ) 00163 { 00164 if ( $useCache ) 00165 eZPreferences::storeInSession( $item['name'], $item['value'] ); 00166 $returnArray[$item['name']] = $item['value']; 00167 } 00168 return $returnArray; 00169 } 00170 else 00171 { 00172 // For the anonymous user we just return all values, or empty array if session is un-started / value undefined 00173 $http = eZHTTPTool::instance(); 00174 return $http->sessionVariable( eZPreferences::SESSION_NAME, array() ); 00175 } 00176 } 00177 00178 /*! 00179 \static 00180 Makes sure the stored session values are cleaned up. 00181 */ 00182 static function sessionCleanup() 00183 { 00184 $http = eZHTTPTool::instance(); 00185 $http->removeSessionVariable( eZPreferences::SESSION_NAME ); 00186 } 00187 00188 /*! 00189 \static 00190 Makes sure the preferences named \a $name is stored in the session with the value \a $value. 00191 */ 00192 static function storeInSession( $name, $value ) 00193 { 00194 $http = eZHTTPTool::instance(); 00195 $preferencesInSession = array(); 00196 if ( $http->hasSessionVariable( eZPreferences::SESSION_NAME ) ) 00197 $preferencesInSession = $http->sessionVariable( eZPreferences::SESSION_NAME ); 00198 $preferencesInSession[$name] = $value; 00199 $http->setSessionVariable( eZPreferences::SESSION_NAME, $preferencesInSession ); 00200 } 00201 00202 /*! 00203 \static 00204 \return \c true if the preference named \a $name is stored in session. 00205 */ 00206 static function isStoredInSession( $name ) 00207 { 00208 $http = eZHTTPTool::instance(); 00209 if ( !$http->hasSessionVariable( eZPreferences::SESSION_NAME, false ) ) 00210 return false; 00211 $preferencesInSession = $http->sessionVariable( eZPreferences::SESSION_NAME ); 00212 return array_key_exists( $name, $preferencesInSession ); 00213 } 00214 00215 /*! 00216 \static 00217 \return the stored preferenced value found in the session or \c null if none were found. 00218 */ 00219 static function storedSessionValue( $name ) 00220 { 00221 $http = eZHTTPTool::instance(); 00222 if ( !$http->hasSessionVariable( eZPreferences::SESSION_NAME ) ) 00223 return null; 00224 $preferencesInSession = $http->sessionVariable( eZPreferences::SESSION_NAME ); 00225 if ( !array_key_exists( $name, $preferencesInSession ) ) 00226 return null; 00227 return $preferencesInSession[$name]; 00228 } 00229 00230 /*! 00231 \static 00232 Removes all preferences for all users. 00233 \note Transaction unsafe. If you call several transaction unsafe methods you must enclose 00234 the calls within a db transaction; thus within db->begin and db->commit. 00235 */ 00236 static function cleanup() 00237 { 00238 $db = eZDB::instance(); 00239 $db->query( "DELETE FROM ezpreferences" ); 00240 } 00241 } 00242 00243 00244 ?>