1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class Sam_Driver_Spamd_Sql extends Sam_Driver_Spamd_Base
16: {
17: 18: 19: 20: 21:
22: protected $_db;
23:
24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 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: 54: 55: 56:
57: public function retrieve()
58: {
59:
60: $this->_defaults = $this->_retrieve(true);
61:
62: $this->_options = array_merge($this->_defaults, $this->_retrieve());
63: }
64:
65: 66: 67: 68: 69: 70: 71: 72: 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:
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: 108: 109: 110: 111: 112: 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:
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:
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: