Beispiel #1
0
 /**
  * @preserveGlobalState disabled
  * @runInSeparateProcess
  */
 public function test_show()
 {
     Hm_Msgs::add('msg two');
     $this->assertTrue(strstr(flatten(join('', Hm_Msgs::show('return'))), 'msgtwo') !== false);
     ob_start();
     Hm_Msgs::show();
     $output = ob_get_contents();
     ob_end_clean();
     $this->assertTrue(strlen($output) > 0);
     Hm_Msgs::show('log');
 }
Beispiel #2
0
 public function process()
 {
     $idle_time = 0;
     if (array_key_exists('idle_time', $this->request->post)) {
         $idle_time = $this->request->post['idle_time'] / 60;
     }
     $max = $this->user_config->get('idle_time', 1) * 60;
     if ($max && $idle_time >= $max) {
         Hm_Msgs::add('Logged out after idle period');
         $this->session->destroy($this->request);
     }
 }
Beispiel #3
0
 public function process()
 {
     $contacts = $this->get('contact_store');
     list($success, $form) = $this->process_form(array('contact_email', 'contact_name'));
     if ($success) {
         $details = array('email_address' => $form['contact_email'], 'display_name' => $form['contact_name']);
         if (array_key_exists('contact_phone', $this->request->post) && $this->request->post['contact_phone']) {
             $details['phone_number'] = $this->request->post['contact_phone'];
         }
         $contacts->add_contact($details);
         $contacts->save($this->user_config);
         $this->session->record_unsaved('Contact added');
         Hm_Msgs::add('Contact Added');
     }
 }
Beispiel #4
0
 public function process()
 {
     if ($this->session->is_admin()) {
         list($success, $form) = $this->process_form(array('create_username', 'create_password', 'create_password_again'));
         if ($success) {
             if ($form['create_password'] == $form['create_password_again']) {
                 if ($this->session->internal_users) {
                     $this->session->create($this->request, $form['create_username'], $form['create_password']);
                 }
             } else {
                 Hm_Msgs::add('ERRPasswords did not match');
             }
         }
     }
 }
Beispiel #5
0
 /**
  * Connect to a DB server
  * @param object $site_config site settings
  * @return object database connection on success
  */
 public static function connect($site_config)
 {
     self::parse_config($site_config);
     $key = self::db_key();
     if (array_key_exists($key, self::$dbh) && self::$dbh[$key]) {
         return self::$dbh[$key];
     }
     $dsn = sprintf('%s:host=%s;dbname=%s', self::$config['db_driver'], self::$config['db_host'], self::$config['db_name']);
     try {
         self::$dbh[$key] = new PDO($dsn, self::$config['db_user'], self::$config['db_pass']);
         Hm_Debug::add(sprintf('Connecting to dsn: %s', $dsn));
         return self::$dbh[$key];
     } catch (Exception $oops) {
         Hm_Debug::add($oops->getMessage());
         Hm_Msgs::add("An error occurred communicating with the database");
         self::$dbh[$key] = false;
         return false;
     }
 }
Beispiel #6
0
 /**
  * Check for an existing session or a new user/pass login request
  * @param object $request request details
  * @param string $user username
  * @param string $pass password
  * @return bool
  */
 public function check($request, $user = false, $pass = false)
 {
     if ($user && $pass) {
         if ($this->auth($user, $pass)) {
             $this->set_key($request);
             $this->loaded = true;
             $this->start($request);
             $this->set_fingerprint($request);
             $this->save_auth_detail();
             $this->just_started();
         } else {
             Hm_Msgs::add("ERRInvalid username or password");
         }
     } elseif (array_key_exists($this->cname, $request->cookie)) {
         $this->get_key($request);
         $this->start($request);
         $this->check_fingerprint($request);
     }
     if ($this->is_active() && $request->invalid_input_detected) {
         Hm_Debug::add(sprintf('Invalid input fields: %s', implode(',', $request->invalid_input_fields)));
         $this->destroy($request);
     }
     return $this->is_active();
 }
Beispiel #7
0
/**
 * Save user settings from the session to permanent storage
 * @param object $handler hm handler module object
 * @param array $form sanitized user input
 * @param bool $logout true if this is a save + logout request
 * @return void
 */
function save_user_settings($handler, $form, $logout)
{
    $user = $handler->session->get('username', false);
    $path = $handler->config->get('user_settings_dir', false);
    if ($handler->session->auth($user, $form['password'])) {
        $pass = $form['password'];
    } else {
        Hm_Msgs::add('ERRIncorrect password, could not save settings to the server');
        $pass = false;
    }
    if ($user && $path && $pass) {
        $handler->user_config->save($user, $pass);
        $handler->session->set('changed_settings', array());
        if ($logout) {
            $handler->session->destroy($handler->request);
            Hm_Msgs::add('Saved user data on logout');
            Hm_Msgs::add('Session destroyed on logout');
        } else {
            Hm_Msgs::add('Settings saved');
        }
    }
}
Beispiel #8
0
 public function process()
 {
     if (isset($this->request->post['submit_feed'])) {
         $found = false;
         list($success, $form) = $this->process_form(array('new_feed_name', 'new_feed_address'));
         if ($success) {
             $connection_test = address_from_url($form['new_feed_address']);
             if ($con = @fsockopen($connection_test, 80, $errno, $errstr, 2)) {
                 $feed = is_feed($form['new_feed_address']);
                 if (!$feed) {
                     $feed = new Hm_Feed();
                     $homepage = $feed->get_feed_data($form['new_feed_address']);
                     if (trim($homepage)) {
                         list($type, $href) = search_for_feeds($homepage);
                         if ($type && $href) {
                             Hm_Msgs::add('Discovered a feed at that address');
                             $found = true;
                         } else {
                             Hm_Msgs::add('ERRCould not find an RSS or ATOM feed at that address');
                         }
                     } else {
                         Hm_Msgs::add('ERRCound not find a feed at that address');
                     }
                 } else {
                     Hm_Msgs::add('Successfully connected to feed');
                     $found = true;
                     if (stristr('<feed', $feed->xml_data)) {
                         $type = 'application/atom+xml';
                     } else {
                         $type = 'application/rss+xml';
                     }
                     $href = $form['new_feed_address'];
                 }
             } else {
                 Hm_Msgs::add(sprintf('ERRCound not add feed: %s', $errstr));
             }
         } else {
             Hm_Msgs::add('ERRFeed Name and Address are required');
         }
         if ($found) {
             $this->out('reload_folders', true);
             Hm_Feed_List::add(array('name' => $form['new_feed_name'], 'server' => $href, 'tls' => false, 'port' => 80));
             $this->session->record_unsaved('Feed added');
         }
     }
 }
Beispiel #9
0
 /**
  * Used on the servers page to process adding a new POP3 server
  */
 public function process()
 {
     if (isset($this->request->post['submit_pop3_server'])) {
         list($success, $form) = $this->process_form(array('new_pop3_name', 'new_pop3_address', 'new_pop3_port'));
         if (!$success) {
             $this->out('old_form', $form);
             Hm_Msgs::add('ERRYou must supply a name, a server and a port');
         } else {
             $tls = false;
             if (isset($this->request->post['tls'])) {
                 $tls = true;
             }
             if ($con = fsockopen($form['new_pop3_address'], $form['new_pop3_port'], $errno, $errstr, 2)) {
                 Hm_POP3_List::add(array('name' => $form['new_pop3_name'], 'server' => $form['new_pop3_address'], 'port' => $form['new_pop3_port'], 'tls' => $tls));
                 Hm_Msgs::add('Added server!');
                 $this->session->record_unsaved('POP3 server added');
             } else {
                 Hm_Msgs::add(sprintf('ERRCound not add server: %s', $errstr));
             }
         }
     }
 }
Beispiel #10
0
 public function process()
 {
     if (array_key_exists('github_disconnect', $this->request->post)) {
         $this->user_config->set('github_connect_details', array());
         $user_data = $this->user_config->dump();
         $this->session->set('user_data', $user_data);
         $this->out('reload_folders', true, false);
         $this->session->record_unsaved('Github connection deleted');
         Hm_Msgs::add('Github connection deleted');
     }
 }
Beispiel #11
0
 /**
  * Build message header HTML
  */
 protected function output()
 {
     if ($this->get('msg_headers')) {
         $txt = '';
         $from = '';
         $small_headers = array('subject', 'date', 'from');
         $reply_args = sprintf('&amp;list_path=imap_%d_%s&amp;uid=%d', $this->html_safe($this->get('msg_server_id')), $this->html_safe($this->get('msg_folder')), $this->html_safe($this->get('msg_text_uid')));
         $headers = $this->get('msg_headers', array());
         $txt .= '<table class="msg_headers"><col class="header_name_col"><col class="header_val_col"></colgroup>';
         foreach ($small_headers as $fld) {
             foreach ($headers as $name => $value) {
                 if ($fld == strtolower($name)) {
                     if ($fld == 'from') {
                         $from = $value;
                     }
                     if ($fld == 'subject') {
                         $txt .= '<tr class="header_' . $fld . '"><th colspan="2">';
                         if (isset($headers['Flags']) && stristr($headers['Flags'], 'flagged')) {
                             $txt .= ' <img alt="" class="account_icon" src="' . Hm_Image_Sources::$star . '" width="16" height="16" /> ';
                         }
                         $txt .= $this->html_safe($value) . '</th></tr>';
                     } else {
                         $txt .= '<tr class="header_' . $fld . '"><th>' . $this->trans($name) . '</th><td>' . $this->html_safe($value) . '</td></tr>';
                     }
                     break;
                 }
             }
         }
         foreach ($headers as $name => $value) {
             if (!in_array(strtolower($name), $small_headers)) {
                 $txt .= '<tr style="display: none;" class="long_header"><th>' . $this->trans($name) . '</th><td>' . $this->html_safe($value) . '</td></tr>';
             }
         }
         $txt .= '<tr><th colspan="2" class="header_links">' . '<a href="#" class="hlink header_toggle">' . $this->trans('all') . '</a>' . '<a class="hlink header_toggle" style="display: none;" href="#">' . $this->trans('small') . '</a>' . ' | <a class="hlink" href="?page=compose&amp;reply=1' . $reply_args . '">' . $this->trans('reply') . '</a>' . ' | <a class="hlink" href="?page=compose&amp;reply_all=1' . $reply_args . '">' . $this->trans('reply-all') . '</a>' . ' | <a class="hlink" href="?page=compose&amp;forward=1' . $reply_args . '">' . $this->trans('forward') . '</a>' . ' | <a class="hlink" href="?page=compose&amp;attach=1' . $reply_args . '">' . $this->trans('attach') . '</a>' . ' | <a class="hlink msg_part_link" data-message-part="0" href="#">' . $this->trans('raw') . '</a>';
         if (isset($headers['Flags']) && stristr($headers['Flags'], 'flagged')) {
             $txt .= ' | <a style="display: none;" class="hlink" id="flag_msg" data-state="unflagged" href="#">' . $this->trans('flag') . '</a>';
             $txt .= '<a id="unflag_msg" class="hlink" data-state="flagged" href="#">' . $this->trans('unflag') . '</a>';
         } else {
             $txt .= ' | <a id="flag_msg" class="hlink" data-state="unflagged" href="#">' . $this->trans('flag') . '</a>';
             $txt .= '<a style="display: none;" class="hlink" id="unflag_msg" data-state="flagged" href="#">' . $this->trans('unflag') . '</a>';
         }
         $txt .= ' | <a class="hlink" id="delete_message" href="#">' . $this->trans('delete') . '</a>';
         $txt .= '</th></tr></table>';
         $this->out('msg_headers', $txt, false);
     } elseif (!$this->get('imap_prefetch')) {
         Hm_Msgs::add('ERR' . $this->trans('Could not fetch the message, it was moved or deleted'));
     }
 }
Beispiel #12
0
 public function process()
 {
     if (array_key_exists('smtp_send', $this->request->post)) {
         list($success, $form) = $this->process_form(array('compose_to', 'compose_subject', 'smtp_server_id'));
         if ($success) {
             $draft = array('draft_to' => $form['compose_to'], 'draft_body' => '', 'draft_subject' => $form['compose_subject']);
             $to = $form['compose_to'];
             $subject = $form['compose_subject'];
             $body = '';
             $from = '';
             $cc = '';
             $bcc = '';
             $in_reply_to = '';
             if (array_key_exists('compose_body', $this->request->post)) {
                 $body = $this->request->post['compose_body'];
                 $draft['draft_body'] = $this->request->post['compose_body'];
             }
             if (array_key_exists('compose_cc', $this->request->post)) {
                 $cc = $this->request->post['compose_cc'];
                 $draft['draft_cc'] = $this->request->post['compose_cc'];
             }
             if (array_key_exists('compose_bcc', $this->request->post)) {
                 $bcc = $this->request->post['compose_bcc'];
                 $draft['draft_bcc'] = $this->request->post['compose_bcc'];
             }
             if (array_key_exists('compose_in_reply_to', $this->request->post)) {
                 $in_reply_to = $this->request->post['compose_in_reply_to'];
                 $draft['draft_in_reply_to'] = $this->request->post['compose_in_reply_to'];
             }
             $smtp_details = Hm_SMTP_List::dump($form['smtp_server_id'], true);
             if ($smtp_details) {
                 $from = $smtp_details['user'];
                 if (array_key_exists('auth', $smtp_details) && $smtp_details['auth'] == 'xoauth2') {
                     $results = smtp_refresh_oauth2_token($smtp_details, $this->config);
                     if (!empty($results)) {
                         if (Hm_SMTP_List::update_oauth2_token($form['smtp_server_id'], $results[1], $results[0])) {
                             Hm_Debug::add(sprintf('Oauth2 token refreshed for SMTP server id %d', $form['smtp_server_id']));
                             $servers = Hm_SMTP_List::dump(false, true);
                             $this->user_config->set('smtp_servers', $servers);
                             $this->session->set('user_data', $this->user_config->dump());
                         }
                     }
                 }
                 $smtp = Hm_SMTP_List::connect($form['smtp_server_id'], false);
                 if ($smtp && $smtp->state == 'authed') {
                     $mime = new Hm_MIME_Msg($to, $subject, $body, $from, $this->get('smtp_compose_type', 0), $cc, $bcc, $in_reply_to);
                     $mime->add_attachments($this->session->get('uploaded_files', array()));
                     $recipients = $mime->get_recipient_addresses();
                     if (empty($recipients)) {
                         Hm_Msgs::add("ERRNo valid receipts found");
                     } else {
                         $err_msg = $smtp->send_message($from, $recipients, $mime->get_mime_msg());
                         if ($err_msg) {
                             Hm_Msgs::add(sprintf("ERR%s", $err_msg));
                         } else {
                             $draft = array();
                             delete_uploaded_files($this->session);
                             Hm_Msgs::add("Message Sent");
                         }
                     }
                 } else {
                     Hm_Msgs::add("ERRFailed to authenticate to the SMTP server");
                 }
             }
             $this->session->set('compose_draft', $draft);
         } else {
             Hm_Msgs::add('ERRRequired field missing');
         }
     }
 }
Beispiel #13
0
 public function process()
 {
     list($success, $form) = $this->process_form(array('nux_pass', 'nux_service', 'nux_email', 'nux_name'));
     if ($success) {
         if (Nux_Quick_Services::exists($form['nux_service'])) {
             $details = Nux_Quick_Services::details($form['nux_service']);
             $details['name'] = $form['nux_name'];
             Hm_IMAP_List::add(array('name' => $details['name'], 'server' => $details['server'], 'port' => $details['port'], 'tls' => $details['tls'], 'user' => $form['nux_email'], 'pass' => $form['nux_pass']));
             $servers = Hm_IMAP_List::dump(false, true);
             $ids = array_keys($servers);
             $new_id = array_pop($ids);
             $imap = Hm_IMAP_List::connect($new_id, false);
             if ($imap && $imap->get_state() == 'authenticated') {
                 if (isset($details['smtp'])) {
                     Hm_SMTP_List::add(array('name' => $details['name'], 'server' => $details['smtp']['server'], 'port' => $details['smtp']['port'], 'tls' => $details['smtp']['tls'], 'user' => $form['nux_email'], 'pass' => $form['nux_pass']));
                     $this->session->record_unsaved('SMTP server added');
                     $smtp_servers = Hm_SMTP_List::dump(false, true);
                     $this->user_config->set('smtp_servers', $smtp_servers);
                 }
                 $this->user_config->set('imap_servers', $servers);
                 Hm_IMAP_List::clean_up();
                 $user_data = $this->user_config->dump();
                 if (!empty($user_data)) {
                     $this->session->set('user_data', $user_data);
                 }
                 $this->session->record_unsaved('IMAP server added');
                 $this->session->record_unsaved('SMTP server added');
                 $this->session->secure_cookie($this->request, 'hm_reload_folders', '1');
                 Hm_Msgs::add('E-mail account successfully added');
                 $msgs = Hm_Msgs::get();
                 if (!empty($msgs)) {
                     $this->session->secure_cookie($this->request, 'hm_msgs', base64_encode(serialize($msgs)), 0);
                 }
                 $this->session->close_early();
                 $this->out('nux_account_added', true);
             } else {
                 Hm_IMAP_List::del($new_id);
                 Hm_Msgs::add('ERRAuthentication failed');
             }
         }
     }
 }
Beispiel #14
0
 /**
  * Clean up everything on logout
  */
 public function process()
 {
     if (array_key_exists('logout', $this->request->post) && !$this->session->loaded) {
         $this->session->destroy($this->request);
         Hm_Msgs::add('Session destroyed on logout');
     } elseif (array_key_exists('save_and_logout', $this->request->post)) {
         list($success, $form) = $this->process_form(array('password'));
         if ($success) {
             $user = $this->session->get('username', false);
             $path = $this->config->get('user_settings_dir', false);
             $pages = $this->session->get('saved_pages', array());
             if (!empty($pages)) {
                 $this->user_config->set('saved_pages', $pages);
             }
             if ($this->session->auth($user, $form['password'])) {
                 $pass = $form['password'];
             } else {
                 Hm_Msgs::add('ERRIncorrect password, could not save settings to the server');
                 $pass = false;
             }
             if ($user && $path && $pass) {
                 $this->user_config->save($user, $pass);
                 $this->session->destroy($this->request);
                 Hm_Msgs::add('Saved user data on logout');
                 Hm_Msgs::add('Session destroyed on logout');
             }
         } else {
             Hm_Msgs::add('ERRYour password is required to save your settings to the server');
         }
     }
 }
Beispiel #15
0
 /**
  * Remove an IMAP server completely, used on the servers page
  */
 public function process()
 {
     if (isset($this->request->post['imap_delete'])) {
         list($success, $form) = $this->process_form(array('imap_server_id'));
         if ($success) {
             $res = Hm_IMAP_List::del($form['imap_server_id']);
             if ($res) {
                 $this->out('deleted_server_id', $form['imap_server_id']);
                 Hm_Msgs::add('Server deleted');
                 $this->session->record_unsaved('IMAP server deleted');
                 Hm_Page_Cache::flush($this->session);
             }
         } else {
             $this->out('old_form', $form);
         }
     }
 }
Beispiel #16
0
 /**
  * Redirect the page after a POST form is submitted and forward any user notices
  * @return mixed
  */
 public function check_for_redirect()
 {
     if (array_key_exists('no_redirect', $this->module_exec->handler_response) && $this->module_exec->handler_response['no_redirect']) {
         return 'noredirect';
     }
     if (!empty($this->request->post) && $this->request->type == 'HTTP') {
         $msgs = Hm_Msgs::get();
         if (!empty($msgs)) {
             $this->session->secure_cookie($this->request, 'hm_msgs', base64_encode(serialize($msgs)), 0);
         }
         $this->session->end();
         if (array_key_exists('REQUEST_URI', $this->request->server)) {
             Hm_Dispatch::page_redirect($this->request->server['REQUEST_URI']);
         }
         return 'redirect';
     } elseif (array_key_exists('hm_msgs', $this->request->cookie) && trim($this->request->cookie['hm_msgs'])) {
         $msgs = @unserialize(base64_decode($this->request->cookie['hm_msgs']));
         if (is_array($msgs)) {
             array_walk($msgs, function ($v) {
                 Hm_Msgs::add($v);
             });
         }
         $this->session->secure_cookie($this->request, 'hm_msgs', '', 0);
         return 'msg_forward';
     }
     return false;
 }
Beispiel #17
0
 /**
  * Create a new user in the DB
  * @param object $request request details
  * @param string $user username
  * @param string $pass password
  * @return bool
  */
 public function create($user, $pass)
 {
     $this->connect();
     $created = false;
     $sql = $this->dbh->prepare("select username from hm_user where username = ?");
     if ($sql->execute(array($user))) {
         $res = $sql->fetch();
         if (!empty($res)) {
             Hm_Msgs::add("ERRThat username is already in use");
         } else {
             $sql = $this->dbh->prepare("insert into hm_user values(?,?)");
             $hash = Hm_Crypt::hash_password($pass);
             if ($sql->execute(array($user, $hash))) {
                 Hm_Msgs::add("Account created");
                 $created = true;
             }
         }
     }
     return $created;
 }
Beispiel #18
0
 public function process()
 {
     list($success, $form) = $this->process_form(array('search_name'));
     if ($success) {
         $data = get_search_from_post($this->request);
         $searches = new Hm_Saved_Searches($this->user_config->get('saved_searches', array()));
         if ($searches->add($form['search_name'], $data)) {
             $this->session->record_unsaved('Saved a search');
             $this->user_config->set('saved_searches', $searches->dump());
             $this->session->set('user_data', $this->user_config->dump());
             $this->out('saved_search', true);
         } else {
             Hm_Msgs::add('ERRYou already have a search by that name');
         }
     }
 }