| 1: | <?php |
| 2: | |
| 3: | trait Hm_Repository { |
| 4: | |
| 5: | protected static $name; |
| 6: | protected static $user_config; |
| 7: | protected static $session; |
| 8: | protected static $entities; |
| 9: | |
| 10: | protected static function initRepo($name, $user_config, $session, &$entities, ? callable $init = null) { |
| 11: | self::$name = $name; |
| 12: | self::$user_config = $user_config; |
| 13: | self::$session = $session; |
| 14: | self::$entities = &$entities; |
| 15: | self::migrateFromIntegerIds(); |
| 16: | $initial = self::$user_config->get(self::$name, []); |
| 17: | if ($init) { |
| 18: | $init($initial); |
| 19: | } else { |
| 20: | foreach ($initial as $key => $entity) { |
| 21: | if (! array_key_exists('id', $entity)) { |
| 22: | $entity['id'] = $key; |
| 23: | } |
| 24: | self::add($entity, false); |
| 25: | } |
| 26: | } |
| 27: | } |
| 28: | |
| 29: | protected static function generateId() { |
| 30: | return uniqid(); |
| 31: | } |
| 32: | |
| 33: | public static function save() { |
| 34: | self::$user_config->set(self::$name, self::$entities); |
| 35: | self::$session->set('user_data', self::$user_config->dump()); |
| 36: | } |
| 37: | |
| 38: | public static function add($entity, $save = true) { |
| 39: | if (is_array($entity)) { |
| 40: | if (! array_key_exists('id', $entity)) { |
| 41: | $entity['id'] = self::generateId(); |
| 42: | } |
| 43: | $id = $entity['id']; |
| 44: | } elseif (method_exists($entity, 'value')) { |
| 45: | if (! $entity->value('id')) { |
| 46: | $entity->update('id', self::generateId()); |
| 47: | } |
| 48: | $id = $entity->value('id'); |
| 49: | } else { |
| 50: | throw new Exception('Unrecognized entity found in the repository.'); |
| 51: | } |
| 52: | self::$entities[$id] = $entity; |
| 53: | if ($save) { |
| 54: | self::save(); |
| 55: | } |
| 56: | return $id; |
| 57: | } |
| 58: | |
| 59: | public static function edit($id, $entity) { |
| 60: | if (array_key_exists($id, self::$entities)) { |
| 61: | if (is_array($entity)) { |
| 62: | self::$entities[$id] = array_merge(self::$entities[$id], $entity); |
| 63: | } else { |
| 64: | self::$entities[$id] = $entity; |
| 65: | } |
| 66: | self::save(); |
| 67: | return true; |
| 68: | } |
| 69: | return false; |
| 70: | } |
| 71: | |
| 72: | public static function del($id) { |
| 73: | if (array_key_exists($id, self::$entities)) { |
| 74: | unset(self::$entities[$id]); |
| 75: | self::save(); |
| 76: | return true; |
| 77: | } |
| 78: | return false; |
| 79: | |
| 80: | } |
| 81: | |
| 82: | public static function get($id) { |
| 83: | if (is_array(self::$entities) && array_key_exists($id, self::$entities)) { |
| 84: | return self::$entities[$id]; |
| 85: | } |
| 86: | return false; |
| 87: | } |
| 88: | |
| 89: | public static function getAll() { |
| 90: | return self::$entities; |
| 91: | } |
| 92: | |
| 93: | public static function count() { |
| 94: | return count(self::$entities); |
| 95: | } |
| 96: | |
| 97: | protected static function migrateFromIntegerIds() { |
| 98: | $config = self::$user_config->dump(); |
| 99: | $replacements = [ |
| 100: | 'imap' => [], |
| 101: | 'smtp' => [], |
| 102: | ]; |
| 103: | $changed = false; |
| 104: | if (! empty($config['imap_servers'])) { |
| 105: | $replacements['imap'] = self::replaceIntegerIds($config['imap_servers']); |
| 106: | $changed = $changed || ! empty($replacements['imap']); |
| 107: | } |
| 108: | if (! empty($config['smtp_servers'])) { |
| 109: | $replacements['smtp'] = self::replaceIntegerIds($config['smtp_servers']); |
| 110: | $changed = $changed || ! empty($replacements['smtp']); |
| 111: | } |
| 112: | if (! empty($config['feeds'])) { |
| 113: | $result = self::replaceIntegerIds($config['feeds']); |
| 114: | $changed = $changed || ! empty($result); |
| 115: | } |
| 116: | if (! empty($config['profiles'])) { |
| 117: | $result = self::replaceIntegerIds($config['profiles']); |
| 118: | $changed = $changed || ! empty($result); |
| 119: | foreach ($config['profiles'] as $id => $profile) { |
| 120: | if (isset($profile['smtp_id']) && is_numeric($profile['smtp_id']) && isset($replacements['smtp'][$profile['smtp_id']])) { |
| 121: | $config['profiles'][$id]['smtp_id'] = $replacements['smtp'][$profile['smtp_id']]; |
| 122: | $changed = true; |
| 123: | } |
| 124: | } |
| 125: | } |
| 126: | if (! empty($config['special_imap_folders'])) { |
| 127: | foreach ($config['special_imap_folders'] as $id => $special) { |
| 128: | if (is_numeric($id)) { |
| 129: | if (isset($replacements['imap'][$id])) { |
| 130: | $config['special_imap_folders'][$replacements['imap'][$id]] = $special; |
| 131: | } |
| 132: | unset($config['special_imap_folders'][$id]); |
| 133: | $changed = true; |
| 134: | } |
| 135: | } |
| 136: | } |
| 137: | if (! empty($config['custom_imap_sources'])) { |
| 138: | foreach ($config['custom_imap_sources'] as $id => $val) { |
| 139: | if (preg_match('/^imap_(\d+)_([0-9a-z]+)$/', $id, $m)) { |
| 140: | $old_id = $m[1]; |
| 141: | if (isset($replacements['imap'][$old_id])) { |
| 142: | $config['custom_imap_sources']['imap_' . $replacements['imap'][$old_id] . '_' . $m[2]] = $val; |
| 143: | } |
| 144: | unset($config['custom_imap_sources'][$id]); |
| 145: | $changed = true; |
| 146: | } |
| 147: | } |
| 148: | } |
| 149: | if ($changed) { |
| 150: | self::$user_config->reload($config, self::$session->get('username')); |
| 151: | self::$session->set('user_data', self::$user_config->dump()); |
| 152: | } |
| 153: | } |
| 154: | |
| 155: | protected static function replaceIntegerIds(&$list) { |
| 156: | $replacements = []; |
| 157: | foreach ($list as $id => $server) { |
| 158: | if (is_numeric($id)) { |
| 159: | $new_id = self::generateId(); |
| 160: | $server['id'] = $new_id; |
| 161: | $list[$new_id] = $server; |
| 162: | unset($list[$id]); |
| 163: | $replacements[$id] = $new_id; |
| 164: | } |
| 165: | } |
| 166: | return $replacements; |
| 167: | } |
| 168: | } |
| 169: | |