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

Documentation generated on Sat, 25 May 2013 04:21:44 +0200 by phpDocumentor 1.4.3