Overview

Packages

  • Horde
    • Icalendar
      • UnitTests
  • Ingo
    • UnitTests
  • None

Classes

  • Horde_Core_Ui_VarRenderer_Ingo
  • Ingo
  • Ingo_Api
  • Ingo_Exception
  • Ingo_Exception_Pear
  • Ingo_LoginTasks_SystemTask_Upgrade
  • Ingo_Script
  • Ingo_Script_Imap
  • Ingo_Script_Imap_Api
  • Ingo_Script_Imap_Live
  • Ingo_Script_Maildrop
  • Ingo_Script_Maildrop_Comment
  • Ingo_Script_Maildrop_Recipe
  • Ingo_Script_Maildrop_Variable
  • Ingo_Script_Procmail
  • Ingo_Script_Procmail_Comment
  • Ingo_Script_Procmail_Recipe
  • Ingo_Script_Procmail_Variable
  • Ingo_Script_Sieve
  • Ingo_Script_Sieve_Action
  • Ingo_Script_Sieve_Action_Addflag
  • Ingo_Script_Sieve_Action_Discard
  • Ingo_Script_Sieve_Action_Fileinto
  • Ingo_Script_Sieve_Action_Flag
  • Ingo_Script_Sieve_Action_Keep
  • Ingo_Script_Sieve_Action_Notify
  • Ingo_Script_Sieve_Action_Redirect
  • Ingo_Script_Sieve_Action_Reject
  • Ingo_Script_Sieve_Action_Removeflag
  • Ingo_Script_Sieve_Action_Stop
  • Ingo_Script_Sieve_Action_Vacation
  • Ingo_Script_Sieve_Comment
  • Ingo_Script_Sieve_Else
  • Ingo_Script_Sieve_Elsif
  • Ingo_Script_Sieve_If
  • Ingo_Script_Sieve_Test
  • Ingo_Script_Sieve_Test_Address
  • Ingo_Script_Sieve_Test_Allof
  • Ingo_Script_Sieve_Test_Anyof
  • Ingo_Script_Sieve_Test_Body
  • Ingo_Script_Sieve_Test_Exists
  • Ingo_Script_Sieve_Test_False
  • Ingo_Script_Sieve_Test_Header
  • Ingo_Script_Sieve_Test_Not
  • Ingo_Script_Sieve_Test_Relational
  • Ingo_Script_Sieve_Test_Size
  • Ingo_Script_Sieve_Test_True
  • Ingo_Storage
  • Ingo_Storage_Blacklist
  • Ingo_Storage_Filters
  • Ingo_Storage_Filters_Sql
  • Ingo_Storage_Forward
  • Ingo_Storage_Mock
  • Ingo_Storage_Prefs
  • Ingo_Storage_Rule
  • Ingo_Storage_Spam
  • Ingo_Storage_Sql
  • Ingo_Storage_Vacation
  • Ingo_Storage_VacationTest
  • Ingo_Storage_Whitelist
  • Ingo_Test
  • Ingo_Transport
  • Ingo_Transport_Ldap
  • Ingo_Transport_Null
  • Ingo_Transport_Sivtest
  • Ingo_Transport_Timsieved
  • Ingo_Transport_Vfs
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Ingo_Storage_Sql implements the Ingo_Storage API to save Ingo data via
  4:  * Horde's Horde_Db database abstraction layer.
  5:  *
  6:  * See the enclosed file LICENSE for license information (ASL).  If you
  7:  * did not receive this file, see http://www.horde.org/licenses/apache.
  8:  *
  9:  * @author  Jan Schneider <jan@horde.org>
 10:  * @package Ingo
 11:  */
 12: class Ingo_Storage_Sql extends Ingo_Storage
 13: {
 14:     /**
 15:      * Handle for the current database connection.
 16:      *
 17:      * @var Horde_Db_Adapter
 18:      */
 19:     protected $_db;
 20: 
 21:     /**
 22:      * Constructor.
 23:      *
 24:      * @param array $params  Additional parameters for the subclass.
 25:      *
 26:      * @throws Horde_Exception
 27:      */
 28:     public function __construct($params = array())
 29:     {
 30:         // @TODO: Inject
 31:         $this->_db = $GLOBALS['injector']->getInstance('Horde_Db_Adapter');;
 32:         $this->_params = array_merge($params, array(
 33:             'table_rules' => 'ingo_rules',
 34:             'table_lists' => 'ingo_lists',
 35:             'table_vacations' => 'ingo_vacations',
 36:             'table_forwards' => 'ingo_forwards',
 37:             'table_spam' => 'ingo_spam'
 38:         ));
 39: 
 40:         parent::__construct();
 41:     }
 42: 
 43:     /**
 44:      * Retrieves the specified data from the storage backend.
 45:      *
 46:      * @param integer $field     The field name of the desired data.
 47:      *                           See lib/Storage.php for the available fields.
 48:      * @param boolean $readonly  Whether to disable any write operations.
 49:      *
 50:      * @return Ingo_Storage_Rule|Ingo_Storage_Filters  The specified data.
 51:      * @throws Ingo_Exception
 52:      */
 53:     protected function _retrieve($field, $readonly = false)
 54:     {
 55:         switch ($field) {
 56:         case self::ACTION_BLACKLIST:
 57:         case self::ACTION_WHITELIST:
 58:             if ($field == self::ACTION_BLACKLIST) {
 59:                 $ob = new Ingo_Storage_Blacklist();
 60:                 $filters = $this->retrieve(self::ACTION_FILTERS);
 61:                 $rule = $filters->findRule($field);
 62:                 if (isset($rule['action-value'])) {
 63:                     $ob->setBlacklistFolder($rule['action-value']);
 64:                 }
 65:             } else {
 66:                 $ob = new Ingo_Storage_Whitelist();
 67:             }
 68:             $query = sprintf('SELECT list_address FROM %s WHERE list_owner = ? AND list_blacklist = ?',
 69:                              $this->_params['table_lists']);
 70:             $values = array(Ingo::getUser(),
 71:                             (int)($field == self::ACTION_BLACKLIST));
 72:             try {
 73:                 $addresses = $this->_db->selectValues($query, $values);
 74:             } catch (Horde_Db_Exception $e) {
 75:                 Horde::logMessage($e->getMessage(), 'ERR');
 76:                 throw new Ingo_Exception($e);
 77:             }
 78:             if ($field == self::ACTION_BLACKLIST) {
 79:                 $ob->setBlacklist($addresses, true);
 80:             } else {
 81:                 $ob->setWhitelist($addresses, true);
 82:             }
 83:             break;
 84: 
 85:         case self::ACTION_FILTERS:
 86:             $ob = new Ingo_Storage_Filters_Sql($this->_db, $this->_params);
 87:             $ob->init($readonly);
 88:             break;
 89: 
 90:         case self::ACTION_FORWARD:
 91:             $query = sprintf('SELECT * FROM %s WHERE forward_owner = ?',
 92:                              $this->_params['table_forwards']);
 93: 
 94:             try {
 95:                 $data = $this->_db->selectOne($query, array(Ingo::getUser()));
 96:             } catch (Horde_Db_Exception $e) {
 97:                 throw new Ingo_Exception($e);
 98:             }
 99:             $ob = new Ingo_Storage_Forward();
100:             if (!empty($data)) {
101:                 $ob->setForwardAddresses(explode("\n", $data['forward_addresses']), false);
102:                 $ob->setForwardKeep((bool)$data['forward_keep']);
103:                 $ob->setSaved(true);
104:             } elseif ($data = @unserialize($GLOBALS['prefs']->getDefault('forward'))) {
105:                 $ob->setForwardAddresses($data['a'], false);
106:                 $ob->setForwardKeep($data['k']);
107:             }
108:             break;
109: 
110:         case self::ACTION_VACATION:
111:             $query = sprintf('SELECT * FROM %s WHERE vacation_owner = ?',
112:                              $this->_params['table_vacations']);
113: 
114:             try {
115:                 $data = $this->_db->selectOne($query, array(Ingo::getUser()));
116:             } catch (Horde_Db_Exception $e) {
117:                 throw new Ingo_Exception($e);
118:             }
119:             $ob = new Ingo_Storage_Vacation();
120:             if (!empty($data)) {
121:                 $ob->setVacationAddresses(explode("\n", $data['vacation_addresses']), false);
122:                 $ob->setVacationDays((int)$data['vacation_days']);
123:                 $ob->setVacationStart((int)$data['vacation_start']);
124:                 $ob->setVacationEnd((int)$data['vacation_end']);
125:                 $ob->setVacationExcludes(explode("\n", $data['vacation_excludes']), false);
126:                 $ob->setVacationIgnorelist((bool)$data['vacation_ignorelists']);
127:                 $ob->setVacationReason(Horde_String::convertCharset($data['vacation_reason'], $this->_params['charset'], 'UTF-8'));
128:                 $ob->setVacationSubject(Horde_String::convertCharset($data['vacation_subject'], $this->_params['charset'], 'UTF-8'));
129:                 $ob->setSaved(true);
130:             } elseif ($data = @unserialize($GLOBALS['prefs']->getDefault('vacation'))) {
131:                 $ob->setVacationAddresses($data['addresses'], false);
132:                 $ob->setVacationDays($data['days']);
133:                 $ob->setVacationExcludes($data['excludes'], false);
134:                 $ob->setVacationIgnorelist($data['ignorelist']);
135:                 $ob->setVacationReason($data['reason']);
136:                 $ob->setVacationSubject($data['subject']);
137:                 if (isset($data['start'])) {
138:                     $ob->setVacationStart($data['start']);
139:                 }
140:                 if (isset($data['end'])) {
141:                     $ob->setVacationEnd($data['end']);
142:                 }
143:             }
144:             break;
145: 
146:         case self::ACTION_SPAM:
147:             $query = sprintf('SELECT * FROM %s WHERE spam_owner = ?',
148:                              $this->_params['table_spam']);
149: 
150:             try {
151:                 $data = $this->_db->selectOne($query, array(Ingo::getUser()));
152:             } catch (Horde_Db_Exception $e) {
153:                 throw new Ingo_Exception($e);
154:             }
155:             $ob = new Ingo_Storage_Spam();
156:             if (!empty($data)) {
157:                 $ob->setSpamFolder($data['spam_folder']);
158:                 $ob->setSpamLevel((int)$data['spam_level']);
159:                 $ob->setSaved(true);
160:             } elseif ($data = @unserialize($GLOBALS['prefs']->getDefault('spam'))) {
161:                 $ob->setSpamFolder($data['folder']);
162:                 $ob->setSpamLevel($data['level']);
163:             }
164:             break;
165: 
166:         default:
167:             $ob = false;
168:         }
169: 
170:         return $ob;
171:     }
172: 
173:     /**
174:      * Stores the specified data in the storage backend.
175:      *
176:      * @access private
177:      *
178:      * @param Ingo_Storage_Rule|Ingo_Storage_Filters $ob  The object to store.
179:      */
180:     protected function _store($ob)
181:     {
182:         switch ($ob->obType()) {
183:         case self::ACTION_BLACKLIST:
184:         case self::ACTION_WHITELIST:
185:             $is_blacklist = (int)($ob->obType() == self::ACTION_BLACKLIST);
186:             if ($is_blacklist) {
187:                 $filters = $this->retrieve(self::ACTION_FILTERS);
188:                 $id = $filters->findRuleId(self::ACTION_BLACKLIST);
189:                 if ($id !== null) {
190:                     $rule = $filters->getRule($id);
191:                     if (!isset($rule['action-value']) ||
192:                         $rule['action-value'] != $ob->getBlacklistFolder()) {
193:                         $rule['action-value'] = $ob->getBlacklistFolder();
194:                         $filters->updateRule($rule, $id);
195:                     }
196:                 }
197:             }
198:             $query = sprintf('DELETE FROM %s WHERE list_owner = ? AND list_blacklist = ?',
199:                              $this->_params['table_lists']);
200:             $values = array(Ingo::getUser(), $is_blacklist);
201:             try {
202:                 $this->_db->delete($query, $values);
203:             } catch (Horde_Db_Exception $e) {
204:                 Horde::logMessage($e, 'ERR');
205:                 throw new Ingo_Exception($e);
206:             }
207:             $query = sprintf('INSERT INTO %s (list_owner, list_blacklist, list_address) VALUES (?, ?, ?)',
208:                              $this->_params['table_lists']);
209: 
210:             $addresses = $is_blacklist ? $ob->getBlacklist() : $ob->getWhitelist();
211:             foreach ($addresses as $address) {
212:                 try {
213:                     $result = $this->_db->insert(
214:                         $query,
215:                         array(Ingo::getUser(),
216:                               $is_blacklist,
217:                               $address));
218:                 } catch (Horde_Db_Exception $e) {
219:                     Horde::logMessage($result, 'ERR');
220:                     throw new Ingo_Exception($e);
221:                 }
222:             }
223:             $ob->setSaved(true);
224:             break;
225: 
226:         case self::ACTION_FORWARD:
227:             $values = array(
228:                 implode("\n", $ob->getForwardAddresses()),
229:                 (int)(bool)$ob->getForwardKeep(),
230:                 Ingo::getUser());
231:             try {
232:                 if ($ob->isSaved()) {
233:                     $query = sprintf('UPDATE %s SET forward_addresses = ?, forward_keep = ? WHERE forward_owner = ?', $this->_params['table_forwards']);
234:                     $this->_db->update($query, $values);
235:                 } else {
236:                     $query = sprintf('INSERT INTO %s (forward_addresses, forward_keep, forward_owner) VALUES (?, ?, ?)', $this->_params['table_forwards']);
237:                     $this->_db->insert($query, $values);
238:                 }
239:             } catch (Horde_Db_Exception $e) {
240:                 throw new Ingo_Exception($e);
241:             }
242:             $ob->setSaved(true);
243:             break;
244: 
245:         case self::ACTION_VACATION:
246:             $values = array(
247:                 implode("\n", $ob->getVacationAddresses()),
248:                 Horde_String::convertCharset($ob->getVacationSubject(),
249:                                              'UTF-8',
250:                                              $this->_params['charset']),
251:                 Horde_String::convertCharset($ob->getVacationReason(),
252:                                              'UTF-8',
253:                                              $this->_params['charset']),
254:                 (int)$ob->getVacationDays(),
255:                 (int)$ob->getVacationStart(),
256:                 (int)$ob->getVacationEnd(),
257:                 implode("\n", $ob->getVacationExcludes()),
258:                 (int)(bool)$ob->getVacationIgnorelist(),
259:                 Ingo::getUser()
260:             );
261:             try {
262:                 if ($ob->isSaved()) {
263:                     $query = sprintf('UPDATE %s SET vacation_addresses = ?, vacation_subject = ?, vacation_reason = ?, vacation_days = ?, vacation_start = ?, vacation_end = ?, vacation_excludes = ?, vacation_ignorelists = ? WHERE vacation_owner = ?', $this->_params['table_vacations']);
264:                     $this->_db->update($query, $values);
265:                 } else {
266:                     $query = sprintf('INSERT INTO %s (vacation_addresses, vacation_subject, vacation_reason, vacation_days, vacation_start, vacation_end, vacation_excludes, vacation_ignorelists, vacation_owner) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $this->_params['table_vacations']);
267:                     $this->_db->insert($query, $values); 
268:                 }
269:             } catch (Horde_Db_Exception $e) {
270:                 throw new Ingo_Exception($e);
271:             }
272:             $ob->setSaved(true);
273:             break;
274: 
275:         case self::ACTION_SPAM:
276:             $values = array(
277:                 (int)$ob->getSpamLevel(),
278:                 $ob->getSpamFolder(),
279:                 Ingo::getUser());
280:             try {
281:                 if ($ob->isSaved()) {
282:                     $query = sprintf('UPDATE %s SET spam_level = ?, spam_folder = ? WHERE spam_owner = ?', $this->_params['table_spam']);
283:                     $this->_db->update($query, $values);
284:                 } else {
285:                     $query = sprintf('INSERT INTO %s (spam_level, spam_folder, spam_owner) VALUES (?, ?, ?)', $this->_params['table_spam']);
286:                     $this->_db->insert($query, $values);
287:                 }
288:             } catch (Horde_Db_Exception $e) {
289:                 throw new Ingo_Exception($e);
290:             }
291:             $ob->setSaved(true);
292:             break;
293:         }
294:     }
295: 
296:     /**
297:      * Removes the data of the specified user from the storage backend.
298:      *
299:      * @param string $user  The user name to delete filters for.
300:      *
301:      * @throws Ingo_Exception
302:      */
303:     public function removeUserData($user)
304:     {
305:         if (!$GLOBALS['registry']->isAdmin() &&
306:             $user != $GLOBALS['registry']->getAuth()) {
307:             throw new Ingo_Exception(_("Permission Denied"));
308:         }
309: 
310:         $queries = array(sprintf('DELETE FROM %s WHERE rule_owner = ?',
311:                                  $this->_params['table_rules']),
312:                          sprintf('DELETE FROM %s WHERE list_owner = ?',
313:                                  $this->_params['table_lists']),
314:                          sprintf('DELETE FROM %s WHERE vacation_owner = ?',
315:                                  $this->_params['table_vacations']),
316:                          sprintf('DELETE FROM %s WHERE forward_owner = ?',
317:                                  $this->_params['table_forwards']),
318:                          sprintf('DELETE FROM %s WHERE spam_owner = ?',
319:                                  $this->_params['table_spam']));
320: 
321:         $values = array($user);
322:         foreach ($queries as $query) {
323:             try {
324:                 $this->_db->delete($query, $values);
325:             } catch (Horde_Db_Exception $e) {
326:                 throw new Ingo_Exception($e);
327:             }
328:         }
329: 
330:         return true;
331:     }
332: 
333: }
334: 
API documentation generated by ApiGen