prefmanager.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 427 行 · 第 1/2 页

PHP
427
字号
                $this->_lastError = "DB Error: ".$result->getMessage();                return NULL;            } else if ($result->numRows()) {                // The query found a value, so we can cache that, and then return it.                $row = $result->fetchRow(DB_FETCHMODE_ASSOC);                $_SESSION[$this->_cacheName][$user_id][$pref_id] = $this->_unpack($row[$this->_valueColumn]);                return $_SESSION[$this->_cacheName][$user_id][$pref_id];            } else if ($this->_returnDefaults && $showDefaults) {                // I was doing this with a call to getPref again, but it threw things into an                // infinite loop if the default value didn't exist. If you can fix that, it would                // be great ;)                if (isset($_SESSION[$this->_cacheName][$this->_defaultUser][$pref_id]) && $this->_useCache) {                    $_SESSION[$this->_cacheName][$user_id][$pref_id] = $_SESSION[$this->_cacheName][$this->_defaultUser][$pref_id];                    return $_SESSION[$this->_cacheName][$this->_defaultUser][$pref_id];                } else {                    $query = sprintf("SELECT * FROM %s WHERE %s=%s AND %s=%s", $this->_table,                                                                               $this->_userColumn,                                                                               $this->_db->quote($this->_defaultUser),                                                                               $this->_nameColumn,                                                                               $this->_db->quote($pref_id));                    $result = $this->_db->query($query);                    if (DB::isError($result)) {                        $this->_lastError = "DB Error: ".$result->getMessage();                        return NULL;                    } else {                        if ($result->numRows()) {                            $row = $result->fetchRow(DB_FETCHMODE_ASSOC);                            $_SESSION[$this->_cacheName][$this->_defaultUser][$pref_id] = $this->_unpack($row[$this->_valueColumn]);                            $_SESSION[$this->_cacheName][$user_id][$pref_id] = $_SESSION[$this->_cacheName][$this->_defaultUser][$pref_id];                            return $_SESSION[$this->_cacheName][$user_id][$pref_id];                        } else {                            return NULL;                        }                    }                }            } else {                // We've used up all the resources we're allowed to search, so return a NULL.                return NULL;            }        }    }    /**    * A shortcut function for getPref($this->_defaultUser, $pref_id, $value),    * useful if you have a logged in user, but want to get defaults anyway.    *    * @param string $pref_id The name of the preference to get.    * @return mixed The value if it's found, or NULL if it isn't.    * @access public    */    function getDefaultPref($pref_id)    {        return $this->getPref($this->_defaultUser, $pref_id);    }    /**     * Set a preference for the specified user.     *      * @param string $user_id The user to set for.     * @param string $pref_id The preference to set.     * @param mixed $value The value it should be set to.     * @return bool Sucess or failure.     * @access public     */    function setPref($user_id, $pref_id, $value)    {        // Start off by checking if the preference is already set (if it is we need to do        // an UPDATE, if not, it's an INSERT.        if ($this->_exists($user_id, $pref_id, false)) {            $query = sprintf("UPDATE %s SET %s=%s WHERE %s=%s AND %s=%s", $this->_table,                                                                          $this->_valueColumnQuoted,                                                                          $this->_db->quote($this->_pack($value)),                                                                          $this->_userColumn,                                                                          $this->_db->quote($user_id),                                                                          $this->_nameColumn,                                                                          $this->_db->quote($pref_id));        } else {            $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES(%s, %s, %s)", $this->_table,                                                                               $this->_userColumn,                                                                               $this->_nameColumn,                                                                               $this->_valueColumnQuoted,                                                                               $this->_db->quote($user_id),                                                                               $this->_db->quote($pref_id),                                                                               $this->_db->quote($this->_pack($value)));        }        $result = $this->_db->query($query);        if (DB::isError($result)) {            $this->_lastError = "DB Error: ".$result->getMessage();            return false;        } else {	    if ($this->_useCache) {	        $_SESSION[$this->_cacheName][$user_id][$pref_id] = $value;	    }            return true;        }    }    /**    * A shortcut function for setPref($this->_defaultUser, $pref_id, $value)    *    * @param string $pref_id The name of the preference to set.    * @param mixed $value The value to set it to.    * @return bool Sucess or failure.    * @access public    */    function setDefaultPref($pref_id, $value)    {        return $this->setPref($this->_defaultUser, $pref_id, $value);    }    /**    * Deletes a preference for the specified user.    *     * @param string $user_id The userid of the user to delete from.    * @param string $pref_id The preference to delete.    * @return bool Success/Failure    * @access public    */    function deletePref($user_id, $pref_id)    {        if ($this->getPref($user_id, $pref_id) == NULL) {            // The user doesn't have this variable anyway ;)            return true;        } else {            $query = sprintf("DELETE 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)) {                $this->_lastError = "DB Error: ".$result->getMessage();                return false;            } else {				if ($this->_useCache) {				    unset($_SESSION[$this->_cacheName][$user_id][$pref_id]);				}                return true;            }        }    }    /**    * Deletes a preference for the default user.    *     * @param string $pref_id The preference to delete.    * @return bool Success/Failure    * @access public    */    function deleteDefaultPref($pref_id)    {        return $this->deletePref($this->_defaultUser, $pref_id);    }	    /**     * Checks if a preference exists in the database.       *     * @param string $user_id The userid of the preference owner.     * @param string $pref_id The preference to check for.     * @return bool True if the preference exists.     * @access private     */    function _exists($user_id, $pref_id)    {        $query = sprintf("SELECT COUNT(%s) FROM %s WHERE %s=%s AND %s=%s", $this->_nameColumn,                                                                           $this->_table,                                                                           $this->_userColumn,                                                                           $this->_db->quoteSmart($user_id),                                                                           $this->_nameColumn,                                                                           $this->_db->quote($pref_id));        $result = $this->_db->getOne($query);        if (DB::isError($result)) {            $this->_lastError = "DB Error: ".$result->getMessage();            return false;        } else {            return (bool)$result;        }    }    /**     * Does anything needed to prepare a value for saving in the database.     *     * @param mixed $value The value to be saved.     * @return string The value in a format valid for saving to the database.     * @access private     */    function _pack($value)    {        if ($this->_serialize) {            return serialize($value);        } else {            return $value;        }    }        /**     * Does anything needed to create a value of the preference, such as unserializing.     *     * @param string $value The value of the preference.     * @return mixed The unpacked version of the preference.     * @access private     */    function _unpack($value)    {        if ($this->_serialize) {            return unserialize($value);        } else {            return $value;        }    }}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?