Overview

Packages

  • None
  • Sam

Classes

  • Sam
  • Sam_Driver_Amavisd_Sql
  • Sam_Driver_Base
  • Sam_Driver_Spamd_Base
  • Sam_Driver_Spamd_Ftp
  • Sam_Driver_Spamd_Ldap
  • Sam_Driver_Spamd_Sql
  • Sam_Exception
  • Sam_Factory_Driver
  • Sam_Form_Blacklist
  • Sam_Form_List
  • Sam_Form_Options
  • Sam_Form_Whitelist
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Sam SQL storage implementation using Horde_Db.
  4:  *
  5:  * Copyright 2003-2012 Horde LLC (http://www.horde.org/)
  6:  *
  7:  * See the enclosed file COPYING for license information (GPL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/gpl.
  9:  *
 10:  * @author  Chris Bowlby <cbowlby@tenthpowertech.com>
 11:  * @author  Max Kalika <max@horde.org>
 12:  * @author  Jan Schneider <jan@horde.org>
 13:  * @package Sam
 14:  */
 15: class Sam_Driver_Spamd_Sql extends Sam_Driver_Spamd_Base
 16: {
 17:     /**
 18:      * Handle for the current database connection.
 19:      *
 20:      * @var Horde_Db_Adapter
 21:      */
 22:     protected $_db;
 23: 
 24:     /**
 25:      * Constructor.
 26:      *
 27:      * @param string $user   A user name.
 28:      * @param array $params  Class parameters:
 29:      *                       - db:    (Horde_Db_Adapater) A database handle.
 30:      *                       - table: (string) The name of the preference table.
 31:      *                       - global_user: (string, optional) A user name to
 32:      *                         use when setting global preferences. Defaults to
 33:      *                         '@GLOBAL'.
 34:      */
 35:     public function __construct($user, $params = array())
 36:     {
 37:         foreach (array('db', 'table') as $param) {
 38:             if (!isset($params[$param])) {
 39:                 throw new InvalidArgumentException(
 40:                     sprintf('"%s" parameter is missing', $param));
 41:             }
 42:         }
 43: 
 44:         $this->_db = $params['db'];
 45:         unset($params['db']);
 46:         $params = array_merge(array('global_user' => '@GLOBAL'), $params);
 47:         $this->_capabilities[] = 'global_defaults';
 48: 
 49:         parent::__construct($user, $params);
 50:     }
 51: 
 52:     /**
 53:      * Retrieves user preferences and default values from the backend.
 54:      *
 55:      * @throws Sam_Exception
 56:      */
 57:     public function retrieve()
 58:     {
 59:         /* Load defaults for any options the user hasn't already overridden. */
 60:         $this->_defaults = $this->_retrieve(true);
 61: 
 62:         $this->_options = array_merge($this->_defaults, $this->_retrieve());
 63:     }
 64: 
 65:     /**
 66:      * Retrieve an option set from the storage backend.
 67:      *
 68:      * @param boolean $defaults  Whether to retrieve the global defaults
 69:      *                           instead of user options.
 70:      *
 71:      * @return array  Array of option-value pairs.
 72:      * @throws Sam_Exception
 73:      */
 74:     protected function _retrieve($defaults = false)
 75:     {
 76:         $user = $defaults ? $this->_params['global_user'] : $this->_user;
 77: 
 78:         try {
 79:             $result = $this->_db->select(
 80:                 'SELECT * FROM ' . $this->_params['table'] . ' WHERE username = ?',
 81:                 array($user));
 82:         } catch (Horde_Db_Exception $e) {
 83:             throw new Sam_Exception($e);
 84:         }
 85: 
 86:         /* Loop through rows, retrieving options. */
 87:         $return = array();
 88:         foreach ($result as $row) {
 89:             $attribute = $this->_mapOptionToAttribute($row['preference']);
 90: 
 91:             if (isset($return[$attribute])) {
 92:                 if (!is_array($return[$attribute])) {
 93:                     $return[$attribute] = array($return[$attribute]);
 94:                 }
 95:                 if (!in_array($row['value'], $return[$attribute])) {
 96:                     $return[$attribute][] = $row['value'];
 97:                 }
 98:             } else {
 99:                 $return[$attribute] = $row['value'];
100:             }
101:         }
102: 
103:         return $return;
104:     }
105: 
106:     /**
107:      * Stores user preferences and default values in the backend.
108:      *
109:      * @param boolean $defaults  Whether to store the global defaults instead
110:      *                           of user options.
111:      *
112:      * @throws Sam_Exception
113:      */
114:     public function store($defaults = false)
115:     {
116:         if ($defaults) {
117:             $store = $this->_defaults;
118:             $user = $this->_params['global_user'];
119:         } else {
120:             $store = $this->_options;
121:             $user = $this->_user;
122:         }
123: 
124:         foreach ($store as $attribute => $value) {
125:             $option = $this->_mapAttributeToOption($attribute);
126: 
127:             /* Delete the option if it is the same as the default */
128:             if (!$defaults && isset($this->_defaults[$attribute]) &&
129:                 $this->_defaults[$attribute] === $value) {
130:                 try {
131:                     $this->_db->delete(
132:                         'DELETE FROM ' . $this->_params['table']
133:                         . ' WHERE username = ? AND preference = ?',
134:                         array($user, $option));
135:                 } catch (Horde_Db_Exception $e) {
136:                     throw new Sam_Exception($e);
137:                 }
138:                 continue;
139:             }
140: 
141:             if (is_array($value)) {
142:                 try {
143:                     $this->_db->delete(
144:                         'DELETE FROM ' . $this->_params['table']
145:                         . ' WHERE username = ? AND preference = ?',
146:                         array($user, $option));
147:                 } catch (Horde_Db_Exception $e) {
148:                     throw new Sam_Exception($e);
149:                 }
150: 
151:                 foreach ($value as $address) {
152:                     /* Don't save email addresses already in defaults. */
153:                     if (!$defaults && isset($this->_defaults[$attribute]) &&
154:                         ((is_array($this->_defaults[$attribute]) &&
155:                           in_array($address, $this->_defaults[$attribute])) ||
156:                          $this->_defaults[$attribute] === $address)) {
157:                         continue;
158:                     }
159: 
160:                     try {
161:                         $this->_db->insert(
162:                             'INSERT INTO ' . $this->_params['table']
163:                             . ' (username, preference, value)'
164:                             . ' VALUES (?, ?, ?)',
165:                             array($user, $option, $address));
166:                     } catch (Horde_Db_Exception $e) {
167:                         throw new Sam_Exception($e);
168:                     }
169:                 }
170:             } else {
171:                 try {
172:                     $result = $this->_db->selectValue(
173:                         'SELECT 1 FROM ' . $this->_params['table']
174:                         . ' WHERE username = ? AND preference = ?',
175:                     array($user, $option));
176:                 } catch (Horde_Db_Exception $e) {
177:                     throw new Sam_Exception($e);
178:                 }
179: 
180:                 try {
181:                     if (!$result) {
182:                         $this->_db->insert(
183:                             'INSERT INTO ' . $this->_params['table']
184:                             . ' (username, preference, value)'
185:                             . ' VALUES (?, ?, ?)',
186:                         array($user, $option, $value));
187:                     } else {
188:                         $this->_db->insert(
189:                             'UPDATE ' . $this->_params['table']
190:                             . ' SET value = ?'
191:                             . ' WHERE username = ? AND preference = ?',
192:                         array($value, $user, $option));
193:                     }
194:                 } catch (Horde_Db_Exception $e) {
195:                     throw new Sam_Exception($e);
196:                 }
197:             }
198:         }
199:     }
200: }
201: 
API documentation generated by ApiGen