Auth_PrefManager
[ class tree: Auth_PrefManager ] [ index: Auth_PrefManager ] [ all elements ]

Source for file PrefManager.php

Documentation is available at PrefManager.php

  1. <?php
  2. require_once("DB.php");
  3.  
  4. /**
  5.  * A simple preference manager, takes userid, preference name pairs and returns the value
  6.  * of that preference.
  7.  *  
  8.  * CREATE TABLE `preferences` (
  9.  * `user_id` varchar( 255 ) NOT NULL default '',
  10.  * `pref_id` varchar( 32 ) NOT NULL default '',
  11.  * `pref_value` longtext NOT NULL ,
  12.  *     PRIMARY KEY ( `user_id` , `pref_id` )
  13.  * )
  14.  * 
  15.  * @author Jon Wood <jon@jellybob.co.uk>
  16.  * @package Auth_PrefManager
  17.  * @category Authentication
  18.  */
  19. {
  20.     /**
  21.      * The database object.
  22.      * @var object 
  23.      * @access private
  24.      */
  25.     var $_db;
  26.  
  27.     /**
  28.      * The user name to get preferences from if the user specified doesn't
  29.      * have that preference set.
  30.      * @var string 
  31.      * @access private
  32.      */
  33.     var $_defaultUser "__default__";
  34.  
  35.     /**
  36.      * Should we search for default values, or just fail when we find out that
  37.      * the specified user didn't have it set.
  38.      * 
  39.      * @var bool 
  40.      * @access private
  41.      */
  42.     var $_returnDefaults = true;
  43.  
  44.     /**
  45.      * The table containing the preferences.
  46.      * @var string 
  47.      * @access private
  48.      */
  49.     var $_table "preferences";
  50.  
  51.     /**
  52.      * The column containing user ids.
  53.      * @var string 
  54.      * @access private
  55.      */
  56.     var $_userColumn "user_id";
  57.  
  58.     /**
  59.      * The column containing preference names.
  60.      * @var string 
  61.      * @access private
  62.      */
  63.     var $_nameColumn "pref_id";
  64.  
  65.     /**
  66.      * The column containing preference values.
  67.      * @var string 
  68.      * @access private
  69.      */
  70.     var $_valueColumn "pref_value";
  71.  
  72.     /**
  73.      * The quoted value column.
  74.      * @var string 
  75.      * @access private
  76.      */
  77.     var $_valueColumnQuoted "pref_value";
  78.     
  79.     /**
  80.      * The session variable that the cache array is stored in.
  81.      * @var string 
  82.      * @access private
  83.      */
  84.      var $_cacheName "prefCache";
  85.  
  86.     /**
  87.      * The last error given.
  88.      * @var string 
  89.      * @access private
  90.      */
  91.     var $_lastError;
  92.  
  93.     /**
  94.      * Defines whether the cache should be used or not.
  95.      * @var bool 
  96.      * @access private
  97.      */
  98.     var $_useCache = true;
  99.     
  100.     /**
  101.      * Defines whether values should be serialized before saving.
  102.      * @var bool 
  103.      * @access private
  104.      */
  105.     var $_serialize = false;
  106.     
  107.     /**
  108.      * Constructor
  109.      * 
  110.      * Options:
  111.      *  table: The table to get prefs from. [preferences]
  112.      *  userColumn: The field name to search for userid's [user_id]
  113.      *  nameColumn: The field name to search for preference names [pref_name]
  114.      *  valueColumn: The field name to search for preference values [pref_value]
  115.      *  defaultUser: The userid assigned to default values [__default__]
  116.      *  cacheName: The name of cache in the session variable ($_SESSION[cacheName]) [prefsCache]
  117.      *  useCache: Whether or not values should be cached.
  118.      *  serialize: Should preference values be serialzed before saving?
  119.      *
  120.      * @param string $dsn The DSN of the database connection to make, or a DB object.
  121.      * @param array $properties An array of properties to set.
  122.      * @param string $defaultUser The default user to manage for.
  123.      * @return bool Success or failure.
  124.      * @access public
  125.      */
  126.     function Auth_PrefManager($dsn$properties = NULL)
  127.     {
  128.         // Connect to the database.
  129.         if (isset($dsn)) {
  130.             if (is_string($dsn)) {
  131.                 $this->_db = DB::Connect($dsn);
  132.                 if (DB::isError($this->_db)) {
  133.                     $this->_lastError "DB Error: ".$this->_db->getMessage();
  134.                 }
  135.             else if (is_subclass_of($dsn'db_common')) {
  136.                 $this->_db &$dsn;
  137.             else {
  138.                 $this->_lastError "Invalid DSN specified.";
  139.                 return false;
  140.             }
  141.         else {
  142.             $this->_lastError "No DSN specified.";
  143.             return false;
  144.         }
  145.  
  146.         if (is_array($properties)) {
  147.             if (isset($properties["table"]))        $this->_table $this->_db->quoteIdentifier($properties["table"])}
  148.             if (isset($properties["userColumn"]))   $this->_userColumn $this->_db->quoteIdentifier($properties["userColumn"])}
  149.             if (isset($properties["nameColumn"]))   $this->_nameColumn $this->_db->quoteIdentifier($properties["nameColumn"])}
  150.             if (isset($properties["valueColumn"]))  $this->_valueColumn $properties["valueColumn"]}
  151.             if (isset($properties["valueColumn"]))  $this->_valueColumnQuoted $this->_db->quoteIdentifier($properties["valueColumn"])}
  152.             if (isset($properties["defaultUser"]))  $this->_defaultUser $properties["defaultUser"]}
  153.             if (isset($properties["cacheName"]))    $this->_cacheName $properties["cacheName"]}
  154.             if (isset($properties["useCache"]))     $this->_useCache $properties["useCache"]}
  155.             if (isset($properties["serialize"]))    $this->_serialize $properties["serialize"]}
  156.         }
  157.  
  158.         return true;
  159.     }
  160.  
  161.     function setReturnDefaults($returnDefaults = true)
  162.     {
  163.         if (is_bool($returnDefaults)) {
  164.             $this->_returnDefaults $returnDefaults;
  165.         }
  166.     }
  167.  
  168.     /**
  169.      * Sets whether the cache should be used.
  170.      * 
  171.      * @param bool $use Should the cache be used.
  172.      * @access public
  173.      */
  174.     function useCache($use = true)
  175.     {
  176.         $this->_useCache $use;
  177.     }
  178.     
  179.     /**
  180.      * Cleans out the cache.
  181.      * 
  182.      * @access public
  183.      */
  184.     function clearCache()
  185.     {
  186.         unset($_SESSION[$this->_cacheName]);
  187.     }
  188.  
  189.     /**
  190.      * Get a preference for the specified user, or, if returning default values
  191.      * is enabled, the default.
  192.      * 
  193.      * @param string $user_id The user to get the preference for.
  194.      * @param string $pref_id The preference to get.
  195.      * @param bool $showDefaults Should default values be searched (overrides the global setting).
  196.      * @return mixed The value if it's found, or NULL if it isn't.
  197.      * @access public
  198.      */
  199.     function getPref($user_id$pref_id$showDefaults = true)
  200.     {
  201.         if (isset($_SESSION[$this->_cacheName][$user_id][$pref_id]&& $this->_useCache{
  202.             // Value is cached for the specified user, so give them the cached copy.
  203.             return $_SESSION[$this->_cacheName][$user_id][$pref_id];
  204.         else {
  205.             // Not cached, search the database for this user's preference.
  206.             $query sprintf("SELECT * FROM %s WHERE %s=%s AND %s=%s"$this->_table,
  207.                                                                    $this->_userColumn,
  208.                                                                        $this->_db->quote($user_id),
  209.                                                                        $this->_nameColumn,
  210.                                                                        $this->_db->quote($pref_id));
  211.             $result $this->_db->query($query);
  212.             if (DB::isError($result)) {
  213.                 // Ouch! The query failed!
  214.                 $this->_lastError "DB Error: ".$result->getMessage();
  215.                 return NULL;
  216.             else if ($result->numRows()) {
  217.                 // The query found a value, so we can cache that, and then return it.
  218.                 $row $result->fetchRow(DB_FETCHMODE_ASSOC);
  219.                 $_SESSION[$this->_cacheName][$user_id][$pref_id$this->_unpack($row[$this->_valueColumn]);
  220.                 return $_SESSION[$this->_cacheName][$user_id][$pref_id];
  221.             else if ($this->_returnDefaults && $showDefaults{
  222.                 // I was doing this with a call to getPref again, but it threw things into an
  223.                 // infinite loop if the default value didn't exist. If you can fix that, it would
  224.                 // be great ;)
  225.                 if (isset($_SESSION[$this->_cacheName][$this->_defaultUser][$pref_id]&& $this->_useCache{
  226.                     $_SESSION[$this->_cacheName][$user_id][$pref_id$_SESSION[$this->_cacheName][$this->_defaultUser][$pref_id];
  227.                     return $_SESSION[$this->_cacheName][$this->_defaultUser][$pref_id];
  228.                 else {
  229.                     $query sprintf("SELECT * FROM %s WHERE %s=%s AND %s=%s"$this->_table,
  230.                                                                                $this->_userColumn,
  231.                                                                                $this->_db->quote($this->_defaultUser),
  232.                                                                                $this->_nameColumn,
  233.                                                                                $this->_db->quote($pref_id));
  234.                     $result $this->_db->query($query);
  235.                     if (DB::isError($result)) {
  236.                         $this->_lastError "DB Error: ".$result->getMessage();
  237.                         return NULL;
  238.                     else {
  239.                         if ($result->numRows()) {
  240.                             $row $result->fetchRow(DB_FETCHMODE_ASSOC);
  241.                             $_SESSION[$this->_cacheName][$this->_defaultUser][$pref_id$this->_unpack($row[$this->_valueColumn]);
  242.                             $_SESSION[$this->_cacheName][$user_id][$pref_id$_SESSION[$this->_cacheName][$this->_defaultUser][$pref_id];
  243.                             return $_SESSION[$this->_cacheName][$user_id][$pref_id];
  244.                         else {
  245.                             return NULL;
  246.                         }
  247.                     }
  248.                 }
  249.             else {
  250.                 // We've used up all the resources we're allowed to search, so return a NULL.
  251.                 return NULL;
  252.             }
  253.         }
  254.     }
  255.  
  256.     /**
  257.     * A shortcut function for getPref($this->_defaultUser, $pref_id, $value),
  258.     * useful if you have a logged in user, but want to get defaults anyway.
  259.     *
  260.     * @param string $pref_id The name of the preference to get.
  261.     * @return mixed The value if it's found, or NULL if it isn't.
  262.     * @access public
  263.     */
  264.     function getDefaultPref($pref_id)
  265.     {
  266.         return $this->getPref($this->_defaultUser$pref_id);
  267.     }
  268.  
  269.     /**
  270.      * Set a preference for the specified user.
  271.      * 
  272.      * @param string $user_id The user to set for.
  273.      * @param string $pref_id The preference to set.
  274.      * @param mixed $value The value it should be set to.
  275.      * @return bool Sucess or failure.
  276.      * @access public
  277.      */
  278.     function setPref($user_id$pref_id$value)
  279.     {
  280.         // Start off by checking if the preference is already set (if it is we need to do
  281.         // an UPDATE, if not, it's an INSERT.
  282.         if ($this->_exists($user_id$pref_idfalse)) {
  283.             $query sprintf("UPDATE %s SET %s=%s WHERE %s=%s AND %s=%s"$this->_table,
  284.                                                                           $this->_valueColumnQuoted,
  285.                                                                           $this->_db->quote($this->_pack($value)),
  286.                                                                           $this->_userColumn,
  287.                                                                           $this->_db->quote($user_id),
  288.                                                                           $this->_nameColumn,
  289.                                                                           $this->_db->quote($pref_id));
  290.         else {
  291.             $query sprintf("INSERT INTO %s (%s, %s, %s) VALUES(%s, %s, %s)"$this->_table,
  292.                                                                                $this->_userColumn,
  293.                                                                                $this->_nameColumn,
  294.                                                                                $this->_valueColumnQuoted,
  295.                                                                                $this->_db->quote($user_id),
  296.                                                                                $this->_db->quote($pref_id),
  297.                                                                                $this->_db->quote($this->_pack($value)));
  298.         }
  299.         $result $this->_db->query($query);
  300.         if (DB::isError($result)) {
  301.             $this->_lastError "DB Error: ".$result->getMessage();
  302.             return false;
  303.         else {
  304.         if ($this->_useCache{
  305.             $_SESSION[$this->_cacheName][$user_id][$pref_id$value;
  306.         }
  307.             return true;
  308.         }
  309.     }
  310.  
  311.     /**
  312.     * A shortcut function for setPref($this->_defaultUser, $pref_id, $value)
  313.     *
  314.     * @param string $pref_id The name of the preference to set.
  315.     * @param mixed $value The value to set it to.
  316.     * @return bool Sucess or failure.
  317.     * @access public
  318.     */
  319.     function setDefaultPref($pref_id$value)
  320.     {
  321.         return $this->setPref($this->_defaultUser$pref_id$value);
  322.     }
  323.  
  324.     /**
  325.     * Deletes a preference for the specified user.
  326.     * 
  327.     * @param string $user_id The userid of the user to delete from.
  328.     * @param string $pref_id The preference to delete.
  329.     * @return bool Success/Failure
  330.     * @access public
  331.     */
  332.     function deletePref($user_id$pref_id)
  333.     {
  334.         if ($this->getPref($user_id$pref_id== NULL{
  335.             // The user doesn't have this variable anyway ;)
  336.             return true;
  337.         else {
  338.             $query sprintf("DELETE FROM %s WHERE %s=%s AND %s=%s"$this->_table,
  339.                                                                      $this->_userColumn,
  340.                                                                      $this->_db->quote($user_id),
  341.                                                                      $this->_nameColumn,
  342.                                                                      $this->_db->quote($pref_id));
  343.             $result $this->_db->query($query);
  344.             if (DB::isError($result)) {
  345.                 $this->_lastError "DB Error: ".$result->getMessage();
  346.                 return false;
  347.             else {
  348.                 if ($this->_useCache{
  349.                     unset($_SESSION[$this->_cacheName][$user_id][$pref_id]);
  350.                 }
  351.                 return true;
  352.             }
  353.         }
  354.     }
  355.  
  356.     /**
  357.     * Deletes a preference for the default user.
  358.     * 
  359.     * @param string $pref_id The preference to delete.
  360.     * @return bool Success/Failure
  361.     * @access public
  362.     */
  363.     function deleteDefaultPref($pref_id)
  364.     {
  365.         return $this->deletePref($this->_defaultUser$pref_id);
  366.     }
  367.     
  368.     /**
  369.      * Checks if a preference exists in the database.
  370.      *
  371.      * @param string $user_id The userid of the preference owner.
  372.      * @param string $pref_id The preference to check for.
  373.      * @return bool True if the preference exists.
  374.      * @access private
  375.      */
  376.     function _exists($user_id$pref_id)
  377.     {
  378.         $query sprintf("SELECT COUNT(%s) FROM %s WHERE %s=%s AND %s=%s"$this->_nameColumn,
  379.                                                                            $this->_table,
  380.                                                                            $this->_userColumn,
  381.                                                                            $this->_db->quoteSmart($user_id),
  382.                                                                            $this->_nameColumn,
  383.                                                                            $this->_db->quote($pref_id));
  384.         $result $this->_db->getOne($query);
  385.         if (DB::isError($result)) {
  386.             $this->_lastError "DB Error: ".$result->getMessage();
  387.             return false;
  388.         else {
  389.             return (bool)$result;
  390.         }
  391.     }
  392.  
  393.     /**
  394.      * Does anything needed to prepare a value for saving in the database.
  395.      *
  396.      * @param mixed $value The value to be saved.
  397.      * @return string The value in a format valid for saving to the database.
  398.      * @access private
  399.      */
  400.     function _pack($value)
  401.     {
  402.         if ($this->_serialize{
  403.             return serialize($value);
  404.         else {
  405.             return $value;
  406.         }
  407.     }
  408.     
  409.     /**
  410.      * Does anything needed to create a value of the preference, such as unserializing.
  411.      *
  412.      * @param string $value The value of the preference.
  413.      * @return mixed The unpacked version of the preference.
  414.      * @access private
  415.      */
  416.     function _unpack($value)
  417.     {
  418.         if ($this->_serialize{
  419.             return unserialize($value);
  420.         else {
  421.             return $value;
  422.         }
  423.     }
  424. }
  425. ?>

Documentation generated on Mon, 11 Mar 2019 14:31:10 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.