1: <?php
2: /**
3: * Utility methods to upgrade Horde 3 preference values.
4: *
5: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
6: *
7: * See the enclosed file COPYING for license information (LGPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
9: *
10: * @author Michael Slusarz <slusarz@horde.org>
11: * @category Horde
12: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
13: * @package Core
14: */
15: class Horde_Core_Prefs_Storage_Upgrade
16: {
17: /**
18: * Upgrades the given preferences from the old H3 way of storing
19: * serialized data.
20: * OLD method: convert charset, serialize, store.
21: * NEW method: serialize, convert charset, store.
22: *
23: * @param Horde_Prefs $prefs_ob The preferences object.
24: * @param array $names The list of names to upgrade.
25: */
26: public function upgradeSerialized($prefs_ob, array $names)
27: {
28: /* Only do upgrade for SQL driver. */
29: foreach ($prefs_ob->getStorage() as $storage) {
30: if ($storage instanceof Horde_Prefs_Storage_Sql) {
31: break;
32: }
33: }
34: if (!($storage instanceof Horde_Prefs_Storage_Sql)) {
35: return;
36: }
37:
38: /* Only do upgrade if charset is not UTF-8. */
39: $charset = $storage->getCharset();
40: if (strcasecmp($charset, 'UTF-8') === 0) {
41: return;
42: }
43:
44: foreach ($names as $name) {
45: if (!$prefs_ob->isDefault($name)) {
46: $data = $prefs_ob->getValue($name);
47:
48: /* Need to convert only if unserialize fails. If it succeeds,
49: * the data has already been converted or there is no need
50: * to convert. */
51: if (@unserialize($data) === false) {
52: /* Re-convert to original charset. */
53: $data = Horde_String::convertCharset($data, 'UTF-8', $charset);
54:
55: /* Unserialize. If we fail here, remove the value
56: * outright since it is invalid and can not be fixed. */
57: if (($data = @unserialize($data)) !== false) {
58: $data = Horde_String::convertCharset($data, $charset, 'UTF-8');
59:
60: /* Re-save in the prefs backend in the new format. */
61: $prefs_ob->setValue($name, serialize($data));
62: }
63: }
64: }
65: }
66: }
67:
68: }
69: