/** * Retrieve the connection to the IMAP client. * * @return bool Whether a connection was successfully established. */ protected function get_imap_client() { global $CFG; if (!\core\message\inbound\manager::is_enabled()) { // E-mail processing not set up. mtrace("Inbound Message not fully configured - exiting early."); return false; } mtrace("Connecting to {$CFG->messageinbound_host} as {$CFG->messageinbound_hostuser}..."); $configuration = array('username' => $CFG->messageinbound_hostuser, 'password' => $CFG->messageinbound_hostpass, 'hostspec' => $CFG->messageinbound_host, 'secure' => $CFG->messageinbound_hostssl, 'debug' => empty($CFG->debugimap) ? null : fopen('php://stderr', 'w')); if (strpos($configuration['hostspec'], ':')) { $hostdata = explode(':', $configuration['hostspec']); if (count($hostdata) === 2) { // A hostname in the format hostname:port has been provided. $configuration['hostspec'] = $hostdata[0]; $configuration['port'] = $hostdata[1]; } } $this->client = new \Horde_Imap_Client_Socket($configuration); try { $this->client->login(); mtrace("Connection established."); // Ensure that mailboxes exist. $this->ensure_mailboxes_exist(); return true; } catch (\Horde_Imap_Client_Exception $e) { $message = $e->getMessage(); throw new \moodle_exception('imapconnectfailure', 'tool_messageinbound', '', null, $message); } }
/** * Retrieve the connection to the IMAP client. * * @return bool Whether a connection was successfully established. */ protected function get_imap_client() { global $CFG; if (!\core\message\inbound\manager::is_enabled()) { // E-mail processing not set up. mtrace("Inbound Message not fully configured - exiting early."); return false; } mtrace("Connecting to {$CFG->messageinbound_host} as {$CFG->messageinbound_hostuser}..."); $configuration = array('username' => $CFG->messageinbound_hostuser, 'password' => $CFG->messageinbound_hostpass, 'hostspec' => $CFG->messageinbound_host, 'secure' => $CFG->messageinbound_hostssl); $this->client = new \Horde_Imap_Client_Socket($configuration); try { $this->client->login(); mtrace("Connection established."); return true; } catch (\Horde_Imap_Client_Exception $e) { $message = $e->getMessage(); mtrace("Unable to connect to IMAP server. Failed with '{$message}'"); return false; } }
/** * Test that the enabled check perform as expected. */ public function test_is_enabled() { global $CFG; // First clear all of the settings set in the setUp. $CFG->messageinbound_domain = null; $CFG->messageinbound_enabled = null; $CFG->messageinbound_mailbox = null; $this->assertFalse(\core\message\inbound\manager::is_enabled()); // Check whether only setting the enabled flag keeps it disabled. $CFG->messageinbound_enabled = true; $this->assertFalse(\core\message\inbound\manager::is_enabled()); // Check that the mailbox entry on it's own does not enable Inbound Message handling. $CFG->messageinbound_mailbox = 'moodlemoodle123'; $CFG->messageinbound_domain = null; $this->assertFalse(\core\message\inbound\manager::is_enabled()); // And that the domain on it's own does not. $CFG->messageinbound_domain = 'example.com'; $CFG->messageinbound_mailbox = null; $this->assertFalse(\core\message\inbound\manager::is_enabled()); // And that an invalid mailbox does not. $CFG->messageinbound_mailbox = ''; $CFG->messageinbound_domain = 'example.com'; $this->assertFalse(\core\message\inbound\manager::is_enabled()); // And that an invalid domain does not. $CFG->messageinbound_domain = ''; $CFG->messageinbound_mailbox = 'moodlemoodle123'; $this->assertFalse(\core\message\inbound\manager::is_enabled()); // Finally a test that ensures that all settings correct enables the system. $CFG->messageinbound_mailbox = 'moodlemoodle123'; $CFG->messageinbound_domain = 'example.com'; $CFG->messageinbound_enabled = true; $this->assertTrue(\core\message\inbound\manager::is_enabled()); }