diff --git a/config/app.php b/config/app.php index 7eda3d5978..629a1b8cd5 100644 --- a/config/app.php +++ b/config/app.php @@ -724,15 +724,6 @@ // */ // 'feeds', - // /* - // | ---- - // | POP3 - // | ---- - // | - // | POP3 E-mail account support - // */ - // 'pop3', - // /* // | ----- // | JMAP diff --git a/lib/framework.php b/lib/framework.php index c4144ed9c5..78538ea4b2 100644 --- a/lib/framework.php +++ b/lib/framework.php @@ -9,6 +9,7 @@ define('VERSION', .1); /* load the framework */ +require APP_PATH.'lib/repository.php'; require APP_PATH.'lib/module.php'; require APP_PATH.'lib/modules.php'; require APP_PATH.'lib/modules_exec.php'; diff --git a/lib/module.php b/lib/module.php index 6837c2ddf9..ce1216a5d8 100644 --- a/lib/module.php +++ b/lib/module.php @@ -283,151 +283,6 @@ private function validate_source($target, $source, $session, $request) { } } -abstract class Hm_Repository { - - private $name; - - private $entities; - - private $default_entity_id; - - private $session; - - private $user_config; - - public function __construct($name, $session, $user_config) { - $this->session = $session; - $this->name = $name; - $this->user_config = $user_config; - $this->load(); - } - - private function load() { - $this->entities = $this->user_config->get($this->name.'_entities'); - $this->default_entity_id = $this->user_config->get($this->name.'_entity_default_id'); - } - - public function __call(string $name, array $arguments) { - if (preg_match('/findBy([A-Z])\w+/', $name)) { - return ''; - } - throw new Exception("Argument " . $name . ' not found in ' . get_class($this)); - } - - public function setDefault($default) { - if (is_scalar($default)) { - $this->default_entity_id = $default; - } - if (is_array($default)) { - $this->default_entity_id = $default['id']; - } - if (is_object($default)) { - $this->default_entity_id = $default->id; - } - $this->commit(); - } - - public function count() { - return count($this->entities); - } - - public function getAll() { - return $this->entities; - } - - public function findById($id) { - if (array_key_exists($id, $this->entities)) { - return $this->entities[$id]; - } - return false; - } - - private function generateId() { - return uniqid(); - } - - private function commit() { - $this->user_config->set($this->name.'_entities', $this->entities); - $this->user_config->set($this->name.'_entity_default_id', $this->default_entity_id); - $this->session->set('user_data', $this->user_config->dump()); - } - - private function saveArrayEntity($entity) { - $created = false; - if (!array_key_exists('id', $entity)) { - $created = true; - $entity['id'] = $this->generateId(); - } - $this->entities[$entity['id']] = $entity; - $this->commit(); - if ($created) { - $this->session->record_unsaved(ucfirst($this->name).' added'); - } else { - $this->session->record_unsaved(ucfirst($this->name).' updated'); - } - } - - public function save($entity) { - if (is_array($entity)) { - return $this->saveArrayEntity($entity); - } - throw new Exception("Entity must be an array or object"); - } - - public function deleteById($id) { - unset($this->entities[$id]); - $this->commit(); - return true; - } - - public function delete($id_or_entity) { - if (is_scalar($id_or_entity)) { - return $this->deleteById($id_or_entity); - } - } -} - -class Hm_ModulesRepositoryRegistry { - - private $modules; - - private $repositories = array(); - - private $session; - - private $user_config; - - public function add_repository($name, $object) { - $this->repositories[$name] = $object; - } - - public function __construct($modules, $session, $user_config) { - $this->modules = $modules; - $this->session = $session; - $this->user_config = $user_config; - $this->load_default_repositories(); - #$this->repositories = array(); - #$modules_path = getcwd() . '../modules'; - #foreach (glob($modules_path . '/*' , GLOB_ONLYDIR) as $dir) { - # - #} - } - - private function load_default_repositories() { - require_once getcwd() . '/lib/repository.php'; - foreach ($this->modules as $module) { - $this->add_repository($module, new Hm_BaseRepository($module, $this->session, $this->user_config)); - } - } - - public function __get(string $name) { - if (array_key_exists($name, $this->repositories)) { - return $this->repositories[$name]; - } - throw new Exception("Repository not registered"); - } -} - /** * Base class for data input processing modules, called "handler modules" * @@ -466,8 +321,6 @@ abstract class Hm_Handler_Module { public $cache; - public $repositories; - /** * Assign input and state sources * @param object $parent instance of the Hm_Request_Handler class @@ -484,7 +337,6 @@ public function __construct($parent, $page, $output=array(), $protected=array()) $this->user_config = $parent->user_config; $this->output = $output; $this->protected = $protected; - $this->repositories = new Hm_ModulesRepositoryRegistry($parent->site_config->get_modules(), $this->session, $this->user_config); } /** diff --git a/lib/modules_exec.php b/lib/modules_exec.php index 5a390f0e43..2df65262a1 100644 --- a/lib/modules_exec.php +++ b/lib/modules_exec.php @@ -218,7 +218,6 @@ class Hm_Module_Exec { public $filters = array(); public $handlers = array(); public $outputs = array(); - public $repositories = array(); /** * @param object $config site config diff --git a/lib/repository.php b/lib/repository.php index 5f6d8f9bb9..c690e54afd 100644 --- a/lib/repository.php +++ b/lib/repository.php @@ -1,6 +1,73 @@ get(self::$name, []); + foreach ($initial as $entity) { + self::add($entity, false); + } + } + + protected static function generateId() { + return uniqid(); + } + + public static function save() { + self::$user_config->set(self::$name, self::$entities); + self::$session->set('user_data', self::$user_config->dump()); + } + + public static function add($entity, $save = true) { + if (! array_key_exists('id', $entity)) { + $entity['id'] = self::generateId(); + } + self::$entities[$entity['id']] = $entity; + if ($save) { + self::save(); + } + } + + public static function edit($id, $entity) { + if (array_key_exists($id, self::$entities)) { + self::$entities[$id] = $entity; + self::save(); + return true; + } + return false; + } + + public static function del($id) { + if (array_key_exists($id, self::$entities)) { + unset(self::$entities[$id]); + self::save(); + return true; + } + return false; + + } + + public static function get($id) { + if (array_key_exists($id, self::$entities)) { + return self::$entities[$id]; + } + return false; + } + + public static function getAll() { + return self::$entities; + } + + public static function count() { + return count(self::$entities); + } +} diff --git a/lib/servers.php b/lib/servers.php index c16b0513c3..c15bf91be8 100644 --- a/lib/servers.php +++ b/lib/servers.php @@ -23,11 +23,7 @@ public static function connect($id, $cache=false, $user=false, $pass=false, $sav if (!array_key_exists($id, self::$server_list)) { return false; } - foreach (self::$server_list as $server_l) { - if ($server['id'] == $id) { - $server = $server_l; - } - } + $server = self::$server_list[$id]; if ($server['object']) { return $server['object']; @@ -179,6 +175,14 @@ public static function toggle_hidden($id, $hide) { trait Hm_Server_List { use Hm_Server_Modify; + use Hm_Repository { + Hm_Repository::add as repo_add; + Hm_Repository::get as repo_get; + } + + public static function init($name, $user_config) { + self::initRepo($name, $user_config, self::$server_list); + } /** * Add a server definition @@ -189,22 +193,7 @@ trait Hm_Server_List { public static function add($atts) { $atts['object'] = false; $atts['connected'] = false; - self::$server_list[] = $atts; - } - - /** - * Remove a server - * @param int $id server id - * @return bool true on success - */ - public static function del($id) { - foreach (self::$server_list as $idx => $srv) { - if ($srv['id'] == $id) { - unset(self::$server_list[$idx]); - return true; - } - } - return false; + self::repo_add($atts); } /** @@ -233,16 +222,11 @@ public static function dump($id=false, $full=false) { * @return array */ public static function get($id, $full) { - foreach (self::$server_list as $srv) { - if ($srv['id'] == $id) { - $server = $srv; - } - if (!$full) { - return self::clean($server); - } - return $server; + $server = self::repo_get($id); + if ($server && ! $full) { + return self::clean($server); } - return array(); + return $server; } /* @@ -250,7 +234,7 @@ public static function get($id, $full) { * @return array */ public static function clean($server) { - if (!array_key_exists('pass', $server) || !$server['pass']) { + if (! array_key_exists('pass', $server) || ! $server['pass']) { $server['nopass'] = true; } unset($server['pass']); diff --git a/modules/core/handler_modules.php b/modules/core/handler_modules.php index efbb38bec4..fde7f0eb0a 100644 --- a/modules/core/handler_modules.php +++ b/modules/core/handler_modules.php @@ -42,7 +42,7 @@ public function process() { $current = Hm_SMTP_List::dump($server['id']); $current['pass'] = $form['password']; unset($current['nopass']); - Hm_SMTP_List::add($current, $server['id']); + Hm_SMTP_List::edit($server['id'], $current); $smtp = Hm_SMTP_List::connect($server['id'], false); if ($smtp->state == 'authed') { Hm_Msgs::add('Password Updated'); @@ -50,7 +50,7 @@ public function process() { } else { unset($current['pass']); - Hm_SMTP_List::add($current, $server['id']); + Hm_SMTP_List::edit($server['id'], $current); Hm_Msgs::add('ERRUnable to authenticate to the SMTP server'); $this->out('connect_status', false); } @@ -59,7 +59,7 @@ public function process() { $current = Hm_IMAP_List::dump($server['id']); $current['pass'] = $form['password']; unset($current['nopass']); - Hm_IMAP_List::add($current, $server['id']); + Hm_IMAP_List::edit($server['id'], $current); $imap = Hm_IMAP_List::connect($server['id'], false); if ($imap->get_state() == 'authenticated') { Hm_Msgs::add('Password Updated'); @@ -67,7 +67,7 @@ public function process() { } else { unset($current['pass']); - Hm_IMAP_List::add($current, $server['id']); + Hm_IMAP_List::edit($server['id'], $current); Hm_Msgs::add('ERRUnable to authenticate to the IMAP server'); $this->out('connect_status', false); } @@ -92,7 +92,6 @@ public function process() { if ($this->module_is_supported('imap')) { foreach (Hm_IMAP_List::dump() as $index => $vals) { if (array_key_exists('nopass', $vals)) { - $vals['id'] = $index; $vals['type'] = 'IMAP'; $key = 'imap_'.$index; $missing[$key] = $vals; @@ -102,7 +101,6 @@ public function process() { if ($this->module_is_supported('smtp')) { foreach (Hm_SMTP_List::dump() as $index => $vals) { if (array_key_exists('nopass', $vals)) { - $vals['id'] = $index; $vals['type'] = 'SMTP'; $key = 'smtp_'.$index; $missing[$key] = $vals; diff --git a/modules/core/setup.php b/modules/core/setup.php index 84e7e572c1..b129365c5e 100644 --- a/modules/core/setup.php +++ b/modules/core/setup.php @@ -193,7 +193,7 @@ 'formatted_message_list' => array(FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY), 'just_saved_credentials' => array(FILTER_VALIDATE_BOOLEAN, false), 'just_forgot_credentials' => array(FILTER_VALIDATE_BOOLEAN, false), - 'deleted_server_id' => array(FILTER_VALIDATE_INT, false), + 'deleted_server_id' => array(FILTER_SANITIZE_FULL_SPECIAL_CHARS, false), 'msg_headers' => array(FILTER_UNSAFE_RAW, false), 'msg_text' => array(FILTER_UNSAFE_RAW, false), 'msg_parts' => array(FILTER_UNSAFE_RAW, false), diff --git a/modules/feeds/hm-feed.php b/modules/feeds/hm-feed.php index 4a131f6daa..8e085ec1e4 100644 --- a/modules/feeds/hm-feed.php +++ b/modules/feeds/hm-feed.php @@ -16,6 +16,10 @@ class Hm_Feed_List { use Hm_Server_List; + public static function init($user_config, $session) { + self::initRepo('feeds', $user_config, $session, self::$server_list); + } + /* * Connect to an RSS/ATOM feed * @param int $id server id diff --git a/modules/feeds/modules.php b/modules/feeds/modules.php index 8521447994..6469096d86 100644 --- a/modules/feeds/modules.php +++ b/modules/feeds/modules.php @@ -23,13 +23,13 @@ public function process() { $this->out('message_list_since', $this->user_config->get('feed_since_setting', DEFAULT_SINCE)); $this->out('per_source_limit', $this->user_config->get('feed_limit_setting', DEFAULT_PER_SOURCE)); } - elseif (preg_match("/^feeds_\d+$/", $path)) { + elseif (preg_match("/^feeds_.+$/", $path)) { $this->out('message_list_since', $this->user_config->get('feed_since_setting', DEFAULT_SINCE)); $this->out('per_source_limit', $this->user_config->get('feed_limit_setting', DEFAULT_PER_SOURCE)); $this->out('list_path', $path, false); $this->out('custom_list_controls', ' '); $parts = explode('_', $path, 2); - $details = Hm_Feed_List::dump(intval($parts[1])); + $details = Hm_Feed_List::dump($parts[1]); if (!empty($details)) { $this->out('mailbox_list_title', array('Feeds', $details['name'])); } @@ -398,7 +398,6 @@ public function process() { if ($found) { $this->out('reload_folders', true); Hm_Feed_List::add(array( - 'id' => uniqid(), 'name' => $form['new_feed_name'], 'server' => $href, 'tls' => false, @@ -415,12 +414,7 @@ public function process() { */ class Hm_Handler_load_feeds_from_config extends Hm_Handler_Module { public function process() { - $feeds = $this->user_config->get('feeds', array()); - $index = 0; - foreach ($feeds as $index => $feed) { - Hm_Feed_List::add($feed, $index); - $index++; - } + Hm_Feed_List::init($this->user_config, $this->session); Hm_Feed_Uid_Cache::load($this->cache->get('feed_read_uids', array(), true)); } } @@ -482,7 +476,7 @@ public function process() { $callback = 'feeds_combined_content'; break; default: - if (preg_match("/^feeds_(\d+)$/", $path, $matches)) { + if (preg_match("/^feeds_(.+)$/", $path, $matches)) { $server_id = $matches[1]; $callback = 'load_feed_list'; } @@ -504,8 +498,7 @@ public function process() { */ class Hm_Handler_save_feeds extends Hm_Handler_Module { public function process() { - $feeds = Hm_Feed_List::dump(); - $this->user_config->set('feeds', $feeds); + Hm_Feed_List::save(); $this->cache->set('feed_read_uids', Hm_Feed_Uid_Cache::dump(), 0, true); } } diff --git a/modules/feeds/setup.php b/modules/feeds/setup.php index f51c2db947..c8647adbc3 100644 --- a/modules/feeds/setup.php +++ b/modules/feeds/setup.php @@ -53,9 +53,9 @@ add_handler('ajax_feed_combined', 'login', false, 'core'); add_handler('ajax_feed_combined', 'load_user_data', true, 'core'); add_handler('ajax_feed_combined', 'language', true, 'core'); +add_handler('ajax_feed_combined', 'load_feeds_from_config', true); add_handler('ajax_feed_combined', 'message_list_type', true, 'core'); add_handler('ajax_feed_combined', 'feed_list_type', true); -add_handler('ajax_feed_combined', 'load_feeds_from_config', true); add_handler('ajax_feed_combined', 'close_session_early', true, 'core'); add_handler('ajax_feed_combined', 'feed_list_content', true); add_handler('ajax_feed_combined', 'date', true, 'core'); @@ -113,7 +113,7 @@ 'feed_connect_time' => array(FILTER_SANITIZE_FULL_SPECIAL_CHARS, false), 'feed_detail_display' => array(FILTER_UNSAFE_RAW, false), 'feed_status_display' => array(FILTER_UNSAFE_RAW, false), - 'feed_status_server_id' => array(FILTER_VALIDATE_INT, false), + 'feed_status_server_id' => array(FILTER_SANITIZE_FULL_SPECIAL_CHARS, false), 'feed_server_ids' => array(FILTER_SANITIZE_FULL_SPECIAL_CHARS, false), 'feed_msg_headers' => array(FILTER_UNSAFE_RAW, false), 'feed_msg_text' => array(FILTER_UNSAFE_RAW, false), diff --git a/modules/gmail_contacts/modules.php b/modules/gmail_contacts/modules.php index bb7a783781..57579861f3 100644 --- a/modules/gmail_contacts/modules.php +++ b/modules/gmail_contacts/modules.php @@ -83,7 +83,7 @@ function fetch_gmail_contacts($config, $contact_store, $session=false, $max_goog $results = imap_refresh_oauth2_token($server, $config); if (!empty($results)) { if (Hm_IMAP_List::update_oauth2_token($id, $results[1], $results[0])) { - Hm_Debug::add(sprintf('Oauth2 token refreshed for IMAP server id %d', $id)); + Hm_Debug::add(sprintf('Oauth2 token refreshed for IMAP server id %s', $id)); $server = Hm_IMAP_List::dump($id, true); } } diff --git a/modules/imap/functions.php b/modules/imap/functions.php index 03ff5ccae9..27743d88f9 100644 --- a/modules/imap/functions.php +++ b/modules/imap/functions.php @@ -118,23 +118,23 @@ function format_imap_folder_section($folders, $id, $output_mod) { $folder_name = bin2hex($folder_name); $results .= '