Source for file db_prefs.php

Documentation is available at db_prefs.php

  1. <?php
  2.  
  3. /**
  4.  * db_prefs.php
  5.  *
  6.  * This contains functions for manipulating user preferences
  7.  * stored in a database, accessed though the Pear DB layer.
  8.  *
  9.  * Database:
  10.  *
  11.  * The preferences table should have three columns:
  12.  *    user       char  \  primary
  13.  *    prefkey    char  /  key
  14.  *    prefval    blob
  15.  *
  16.  *   CREATE TABLE userprefs (user CHAR(128) NOT NULL DEFAULT '',
  17.  *                           prefkey CHAR(64) NOT NULL DEFAULT '',
  18.  *                           prefval BLOB NOT NULL DEFAULT '',
  19.  *                           primary key (user,prefkey));
  20.  *
  21.  * Configuration of databasename, username and password is done
  22.  * by using conf.pl or the administrator plugin
  23.  *
  24.  * @copyright &copy; 1999-2006 The SquirrelMail Project Team
  25.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  26.  * @version $Id: db_prefs.php,v 1.31.2.9 2006/04/14 22:27:07 jervfors Exp $
  27.  * @package squirrelmail
  28.  * @subpackage prefs
  29.  * @since 1.1.3
  30.  */
  31.  
  32. /** Unknown database */
  33. define('SMDB_UNKNOWN'0);
  34. /** MySQL */
  35. define('SMDB_MYSQL'1);
  36. /** PostgreSQL */
  37. define('SMDB_PGSQL'2);
  38.  
  39. require_once(SM_PATH 'config/config.php');
  40. if (!include_once('DB.php')) {
  41.     // same error also in abook_database.php
  42.         require_once(SM_PATH 'functions/display_messages.php');
  43.     $error  _("Could not include PEAR database functions required for the database backend.""<br />\n";
  44.     $error .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
  45.                         '<tt>DB.php</tt>'"<br />\n";
  46.     $error .= _("Please contact your system administrator and report this error.");
  47.     error_box($error$color);
  48.     exit;
  49. }
  50.  
  51. global $prefs_are_cached$prefs_cache;
  52.  
  53. /**
  54.  * @ignore
  55.  */
  56. function cachePrefValues($username{
  57.     global $prefs_are_cached$prefs_cache;
  58.  
  59.     sqgetGlobalVar('prefs_are_cached'$prefs_are_cachedSQ_SESSION );
  60.     if ($prefs_are_cached{
  61.         sqgetGlobalVar('prefs_cache'$prefs_cacheSQ_SESSION );
  62.         return;
  63.     }
  64.  
  65.     sqsession_unregister('prefs_cache');
  66.     sqsession_unregister('prefs_are_cached');
  67.  
  68.     $db new dbPrefs;
  69.     if(isset($db->error)) {
  70.         printf_("Preference database error (%s). Exiting abnormally"),
  71.               $db->error);
  72.         exit;
  73.     }
  74.  
  75.     $db->fillPrefsCache($username);
  76.     if (isset($db->error)) {
  77.         printf_("Preference database error (%s). Exiting abnormally"),
  78.               $db->error);
  79.         exit;
  80.     }
  81.  
  82.     $prefs_are_cached true;
  83.  
  84.     sqsession_register($prefs_cache'prefs_cache');
  85.     sqsession_register($prefs_are_cached'prefs_are_cached');
  86. }
  87.  
  88. /**
  89.  * Completely undocumented class - someone document it!
  90.  * @package squirrelmail
  91.  */
  92. class dbPrefs {
  93.     var $table = 'userprefs';
  94.     var $user_field = 'user';
  95.     var $key_field = 'prefkey';
  96.     var $val_field = 'prefval';
  97.  
  98.     var $dbh   = NULL;
  99.     var $error = NULL;
  100.     var $db_type = SMDB_UNKNOWN;
  101.  
  102.     var $default = Array('theme_default' => 0,
  103.                          'show_html_default' => '0');
  104.  
  105.     function open({
  106.         global $prefs_dsn$prefs_table;
  107.         global $prefs_user_field$prefs_key_field$prefs_val_field;
  108.  
  109.         if(isset($this->dbh)) {
  110.             return true;
  111.         }
  112.  
  113.         if (preg_match('/^mysql/'$prefs_dsn)) {
  114.             $this->db_type = SMDB_MYSQL;
  115.         elseif (preg_match('/^pgsql/'$prefs_dsn)) {
  116.             $this->db_type = SMDB_PGSQL;
  117.         }
  118.  
  119.         if (!empty($prefs_table)) {
  120.             $this->table = $prefs_table;
  121.         }
  122.         if (!empty($prefs_user_field)) {
  123.             $this->user_field = $prefs_user_field;
  124.         }
  125.         if (!empty($prefs_key_field)) {
  126.             $this->key_field = $prefs_key_field;
  127.         }
  128.         if (!empty($prefs_val_field)) {
  129.             $this->val_field = $prefs_val_field;
  130.         }
  131.         $dbh DB::connect($prefs_dsntrue);
  132.  
  133.         if(DB::isError($dbh)) {
  134.             $this->error = DB::errorMessage($dbh);
  135.             return false;
  136.         }
  137.  
  138.         $this->dbh = $dbh;
  139.         return true;
  140.     }
  141.  
  142.     function failQuery($res NULL{
  143.         if($res == NULL{
  144.             printf(_("Preference database error (%s). Exiting abnormally"),
  145.                   $this->error);
  146.         else {
  147.             printf(_("Preference database error (%s). Exiting abnormally"),
  148.                   DB::errorMessage($res));
  149.         }
  150.         exit;
  151.     }
  152.  
  153.  
  154.     function getKey($user$key$default ''{
  155.         global $prefs_cache;
  156.  
  157.         cachePrefValues($user);
  158.  
  159.         if (isset($prefs_cache[$key])) {
  160.             return $prefs_cache[$key];
  161.         else {
  162.             if (isset($this->default[$key])) {
  163.                 return $this->default[$key];
  164.             else {
  165.                 return $default;
  166.             }
  167.         }
  168.     }
  169.  
  170.     function deleteKey($user$key{
  171.         global $prefs_cache;
  172.  
  173.         if (!$this->open()) {
  174.             return false;
  175.         }
  176.         $query sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
  177.                          $this->table,
  178.                          $this->user_field,
  179.                          $this->dbh->quoteString($user),
  180.                          $this->key_field,
  181.                          $this->dbh->quoteString($key));
  182.  
  183.         $res $this->dbh->simpleQuery($query);
  184.         if(DB::isError($res)) {
  185.             $this->failQuery($res);
  186.         }
  187.  
  188.         unset($prefs_cache[$key]);
  189.  
  190.         return true;
  191.     }
  192.  
  193.     function setKey($user$key$value{
  194.         if (!$this->open()) {
  195.             return false;
  196.         }
  197.         if ($this->db_type == SMDB_MYSQL{
  198.             $query sprintf("REPLACE INTO %s (%s, %s, %s) ".
  199.                              "VALUES('%s','%s','%s')",
  200.                              $this->table,
  201.                              $this->user_field,
  202.                              $this->key_field,
  203.                              $this->val_field,
  204.                              $this->dbh->quoteString($user),
  205.                              $this->dbh->quoteString($key),
  206.                              $this->dbh->quoteString($value));
  207.  
  208.             $res $this->dbh->simpleQuery($query);
  209.             if(DB::isError($res)) {
  210.                 $this->failQuery($res);
  211.             }
  212.         elseif ($this->db_type == SMDB_PGSQL{
  213.             $this->dbh->simpleQuery("BEGIN TRANSACTION");
  214.             $query sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
  215.                              $this->table,
  216.                              $this->user_field,
  217.                              $this->dbh->quoteString($user),
  218.                              $this->key_field,
  219.                              $this->dbh->quoteString($key));
  220.             $res $this->dbh->simpleQuery($query);
  221.             if (DB::isError($res)) {
  222.                 $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
  223.                 $this->failQuery($res);
  224.             }
  225.             $query sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
  226.                              $this->table,
  227.                              $this->user_field,
  228.                              $this->key_field,
  229.                              $this->val_field,
  230.                              $this->dbh->quoteString($user),
  231.                              $this->dbh->quoteString($key),
  232.                              $this->dbh->quoteString($value));
  233.             $res $this->dbh->simpleQuery($query);
  234.             if (DB::isError($res)) {
  235.                 $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
  236.                 $this->failQuery($res);
  237.             }
  238.             $this->dbh->simpleQuery("COMMIT TRANSACTION");
  239.         else {
  240.             $query sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
  241.                              $this->table,
  242.                              $this->user_field,
  243.                              $this->dbh->quoteString($user),
  244.                              $this->key_field,
  245.                              $this->dbh->quoteString($key));
  246.             $res $this->dbh->simpleQuery($query);
  247.             if (DB::isError($res)) {
  248.                 $this->failQuery($res);
  249.             }
  250.             $query sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
  251.                              $this->table,
  252.                              $this->user_field,
  253.                              $this->key_field,
  254.                              $this->val_field,
  255.                              $this->dbh->quoteString($user),
  256.                              $this->dbh->quoteString($key),
  257.                              $this->dbh->quoteString($value));
  258.             $res $this->dbh->simpleQuery($query);
  259.             if (DB::isError($res)) {
  260.                 $this->failQuery($res);
  261.             }
  262.         }
  263.  
  264.         return true;
  265.     }
  266.  
  267.     function fillPrefsCache($user{
  268.         global $prefs_cache;
  269.  
  270.         if (!$this->open()) {
  271.             return;
  272.         }
  273.  
  274.         $prefs_cache array();
  275.         $query sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
  276.                          "WHERE %s = '%s'",
  277.                          $this->key_field,
  278.                          $this->val_field,
  279.                          $this->table,
  280.                          $this->user_field,
  281.                          $this->dbh->quoteString($user));
  282.         $res $this->dbh->query($query);
  283.         if (DB::isError($res)) {
  284.             $this->failQuery($res);
  285.         }
  286.  
  287.         while ($row $res->fetchRow(DB_FETCHMODE_ASSOC)) {
  288.             $prefs_cache[$row['prefkey']] $row['prefval'];
  289.         }
  290.     }
  291.  
  292. /* end class dbPrefs */
  293.  
  294.  
  295. /**
  296.  * returns the value for the pref $string
  297.  * @ignore
  298.  */
  299. function getPref($data_dir$username$string$default ''{
  300.     $db new dbPrefs;
  301.     if(isset($db->error)) {
  302.         printf_("Preference database error (%s). Exiting abnormally"),
  303.               $db->error);
  304.         exit;
  305.     }
  306.  
  307.     return $db->getKey($username$string$default);
  308. }
  309.  
  310. /**
  311.  * Remove the pref $string
  312.  * @ignore
  313.  */
  314. function removePref($data_dir$username$string{
  315.     global $prefs_cache;
  316.     $db new dbPrefs;
  317.     if(isset($db->error)) {
  318.         $db->failQuery();
  319.     }
  320.  
  321.     $db->deleteKey($username$string);
  322.  
  323.     if (isset($prefs_cache[$string])) {
  324.         unset($prefs_cache[$string]);
  325.     }
  326.  
  327.     sqsession_register($prefs_cache 'prefs_cache');
  328.     return;
  329. }
  330.  
  331. /**
  332.  * sets the pref, $string, to $set_to
  333.  * @ignore
  334.  */
  335. function setPref($data_dir$username$string$set_to{
  336.     global $prefs_cache;
  337.  
  338.     if (isset($prefs_cache[$string]&& ($prefs_cache[$string== $set_to)) {
  339.         return;
  340.     }
  341.  
  342.     if ($set_to === ''{
  343.         removePref($data_dir$username$string);
  344.         return;
  345.     }
  346.  
  347.     $db new dbPrefs;
  348.     if(isset($db->error)) {
  349.         $db->failQuery();
  350.     }
  351.  
  352.     $db->setKey($username$string$set_to);
  353.     $prefs_cache[$string$set_to;
  354.     assert_options(ASSERT_ACTIVE1);
  355.     assert_options(ASSERT_BAIL1);
  356.     assert ('$set_to == $prefs_cache[$string]');
  357.     sqsession_register($prefs_cache 'prefs_cache');
  358.     return;
  359. }
  360.  
  361. /**
  362.  * This checks if the prefs are available
  363.  * @ignore
  364.  */
  365. function checkForPrefs($data_dir$username{
  366.     $db new dbPrefs;
  367.     if(isset($db->error)) {
  368.         $db->failQuery();
  369.     }
  370. }
  371.  
  372. /**
  373.  * Writes the Signature
  374.  * @ignore
  375.  */
  376. function setSig($data_dir$username$number$string{
  377.     if ($number == "g"{
  378.         $key '___signature___';
  379.     else {
  380.         $key sprintf('___sig%s___'$number);
  381.     }
  382.     setPref($data_dir$username$key$string);
  383.     return;
  384. }
  385.  
  386. /**
  387.  * Gets the signature
  388.  * @ignore
  389.  */
  390. function getSig($data_dir$username$number{
  391.     if ($number == "g"{
  392.         $key '___signature___';
  393.     else {
  394.         $key sprintf('___sig%d___'$number);
  395.     }
  396.     return getPref($data_dir$username$key);
  397. }
  398.  
  399. // vim: et ts=4
  400. ?>

Documentation generated on Sat, 07 Oct 2006 16:30:43 +0300 by phpDocumentor 1.3.0RC6