prefmanager.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 427 行 · 第 1/2 页
PHP
427 行
<?phprequire_once("DB.php");/** * A simple preference manager, takes userid, preference name pairs and returns the value * of that preference. * * CREATE TABLE `preferences` ( * `user_id` varchar( 255 ) NOT NULL default '', * `pref_id` varchar( 32 ) NOT NULL default '', * `pref_value` longtext NOT NULL , * PRIMARY KEY ( `user_id` , `pref_id` ) * ) * * @author Jon Wood <jon@jellybob.co.uk> * @package Auth_PrefManager * @category Authentication */class Auth_PrefManager{ /** * The database object. * @var object * @access private */ var $_db; /** * The user name to get preferences from if the user specified doesn't * have that preference set. * @var string * @access private */ var $_defaultUser = "__default__"; /** * Should we search for default values, or just fail when we find out that * the specified user didn't have it set. * * @var bool * @access private */ var $_returnDefaults = true; /** * The table containing the preferences. * @var string * @access private */ var $_table = "preferences"; /** * The column containing user ids. * @var string * @access private */ var $_userColumn = "user_id"; /** * The column containing preference names. * @var string * @access private */ var $_nameColumn = "pref_id"; /** * The column containing preference values. * @var string * @access private */ var $_valueColumn = "pref_value"; /** * The quoted value column. * @var string * @access private */ var $_valueColumnQuoted = "pref_value"; /** * The session variable that the cache array is stored in. * @var string * @access private */ var $_cacheName = "prefCache"; /** * The last error given. * @var string * @access private */ var $_lastError; /** * Defines whether the cache should be used or not. * @var bool * @access private */ var $_useCache = true; /** * Defines whether values should be serialized before saving. * @var bool * @access private */ var $_serialize = false; /** * Constructor * * Options: * table: The table to get prefs from. [preferences] * userColumn: The field name to search for userid's [user_id] * nameColumn: The field name to search for preference names [pref_name] * valueColumn: The field name to search for preference values [pref_value] * defaultUser: The userid assigned to default values [__default__] * cacheName: The name of cache in the session variable ($_SESSION[cacheName]) [prefsCache] * useCache: Whether or not values should be cached. * serialize: Should preference values be serialzed before saving? * * @param string $dsn The DSN of the database connection to make, or a DB object. * @param array $properties An array of properties to set. * @param string $defaultUser The default user to manage for. * @return bool Success or failure. * @access public */ function Auth_PrefManager($dsn, $properties = NULL) { // Connect to the database. if (isset($dsn)) { if (is_string($dsn)) { $this->_db = DB::Connect($dsn); if (DB::isError($this->_db)) { $this->_lastError = "DB Error: ".$this->_db->getMessage(); } } else if (is_subclass_of($dsn, 'db_common')) { $this->_db = &$dsn; } else { $this->_lastError = "Invalid DSN specified."; return false; } } else { $this->_lastError = "No DSN specified."; return false; } if (is_array($properties)) { if (isset($properties["table"])) { $this->_table = $this->_db->quoteIdentifier($properties["table"]); } if (isset($properties["userColumn"])) { $this->_userColumn = $this->_db->quoteIdentifier($properties["userColumn"]); } if (isset($properties["nameColumn"])) { $this->_nameColumn = $this->_db->quoteIdentifier($properties["nameColumn"]); } if (isset($properties["valueColumn"])) { $this->_valueColumn = $properties["valueColumn"]; } if (isset($properties["valueColumn"])) { $this->_valueColumnQuoted = $this->_db->quoteIdentifier($properties["valueColumn"]); } if (isset($properties["defaultUser"])) { $this->_defaultUser = $properties["defaultUser"]; } if (isset($properties["cacheName"])) { $this->_cacheName = $properties["cacheName"]; } if (isset($properties["useCache"])) { $this->_useCache = $properties["useCache"]; } if (isset($properties["serialize"])) { $this->_serialize = $properties["serialize"]; } } return true; } function setReturnDefaults($returnDefaults = true) { if (is_bool($returnDefaults)) { $this->_returnDefaults = $returnDefaults; } } /** * Sets whether the cache should be used. * * @param bool $use Should the cache be used. * @access public */ function useCache($use = true) { $this->_useCache = $use; } /** * Cleans out the cache. * * @access public */ function clearCache() { unset($_SESSION[$this->_cacheName]); } /** * Get a preference for the specified user, or, if returning default values * is enabled, the default. * * @param string $user_id The user to get the preference for. * @param string $pref_id The preference to get. * @param bool $showDefaults Should default values be searched (overrides the global setting). * @return mixed The value if it's found, or NULL if it isn't. * @access public */ function getPref($user_id, $pref_id, $showDefaults = true) { if (isset($_SESSION[$this->_cacheName][$user_id][$pref_id]) && $this->_useCache) { // Value is cached for the specified user, so give them the cached copy. return $_SESSION[$this->_cacheName][$user_id][$pref_id]; } else { // Not cached, search the database for this user's preference. $query = sprintf("SELECT * FROM %s WHERE %s=%s AND %s=%s", $this->_table, $this->_userColumn, $this->_db->quote($user_id), $this->_nameColumn, $this->_db->quote($pref_id)); $result = $this->_db->query($query); if (DB::isError($result)) { // Ouch! The query failed!
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?