| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | if (!defined('DEBUG_MODE')) { die(); } |
| 9: | |
| 10: | require_once APP_PATH.'modules/dynamic_login/hm-discover-service.php'; |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | class Hm_Handler_process_dynamic_login extends Hm_Handler_login { |
| 17: | |
| 18: | |
| 19: | |
| 20: | public function process() { |
| 21: | if ($this->config->get('auth_type') != 'dynamic') { |
| 22: | return parent::process(); |
| 23: | } |
| 24: | $login_config = dynamic_login_config($this->config); |
| 25: | $this->out('dynamic_config', $login_config); |
| 26: | $this->out('dynamic_login', true); |
| 27: | list($success, $form) = $this->process_form(array('username', 'password', 'email_provider')); |
| 28: | if ($success) { |
| 29: | $discover = new Hm_Discover_Services($form['username'], $login_config, $form['email_provider'], $this->request->server['HTTP_HOST']); |
| 30: | $details = $discover->get_host_details(); |
| 31: | $auth_details = array(); |
| 32: | if (array_key_exists('server', $details)) { |
| 33: | if ($this->module_is_supported('imap') && $details['type'] == 'imap') { |
| 34: | $this->config->set('imap_auth_server', $details['server']); |
| 35: | $this->config->set('imap_auth_port', $details['port']); |
| 36: | $this->config->set('imap_auth_tls', $details['tls']); |
| 37: | $this->session->auth_class = 'Hm_Auth_IMAP'; |
| 38: | $this->session->site_config = $this->config; |
| 39: | Hm_Debug::add('Dynamic login override, using Hm_Auth_IMAP', 'info'); |
| 40: | $auth_details = $details; |
| 41: | $this->session->set('auth_class', 'Hm_Auth_IMAP'); |
| 42: | } |
| 43: | } |
| 44: | $this->session->check($this->request, rtrim($form['username']), $form['password']); |
| 45: | $this->session->set('username', rtrim($form['username'])); |
| 46: | } |
| 47: | else { |
| 48: | $this->session->check($this->request); |
| 49: | } |
| 50: | if ($this->session->is_active()) { |
| 51: | if ($this->session->loaded && array_key_exists('server', $auth_details)) { |
| 52: | $auth_details['username'] = $form['username']; |
| 53: | $auth_details['password'] = $form['password']; |
| 54: | if ($auth_details['type'] == 'imap') { |
| 55: | $this->session->set('imap_auth_server_settings', $auth_details); |
| 56: | } |
| 57: | if (array_key_exists('smtp', $auth_details) && count($auth_details['smtp']) > 0) { |
| 58: | $this->config->set('default_smtp_server', $auth_details['smtp']['server']); |
| 59: | $this->config->set('default_smtp_port', $auth_details['smtp']['port']); |
| 60: | $this->config->set('default_smtp_tls', $auth_details['smtp']['tls']); |
| 61: | $this->config->set('default_smtp_name', $auth_details['name']); |
| 62: | } |
| 63: | } |
| 64: | else { |
| 65: | $imap_details = $this->session->get('imap_auth_server_settings', array()); |
| 66: | if (count($imap_details) > 0) { |
| 67: | $this->config->set('imap_auth_server', $imap_details['server']); |
| 68: | $this->config->set('imap_auth_port', $imap_details['port']); |
| 69: | $this->config->set('imap_auth_tls', $imap_details['tls']); |
| 70: | } |
| 71: | } |
| 72: | $this->session->auth_class = $this->session->get('auth_class'); |
| 73: | $this->out('changed_settings', $this->session->get('changed_settings', array()), false); |
| 74: | $this->out('is_logged', true); |
| 75: | } |
| 76: | Hm_Request_Key::load($this->session, $this->request, $this->session->loaded); |
| 77: | $this->validate_method($this->session, $this->request); |
| 78: | $this->process_key(); |
| 79: | if (!$this->config->get('disable_origin_check', false)) { |
| 80: | $this->validate_origin($this->session, $this->request, $this->config); |
| 81: | } |
| 82: | } |
| 83: | } |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | class Hm_Output_dynamic_login extends Hm_Output_login { |
| 90: | |
| 91: | |
| 92: | |
| 93: | protected function output() { |
| 94: | if (!$this->get('dynamic_login')) { |
| 95: | return parent::output(); |
| 96: | } |
| 97: | $config = $this->get('dynamic_config', array ()); |
| 98: | if (!$this->get('router_login_state')) { |
| 99: | $res = '<h1 class="title">'.$this->html_safe($this->get('router_app_name', '')).'</h1>'. |
| 100: | ' <input type="hidden" name="hm_page_key" value="'.Hm_Request_Key::generate().'" />'. |
| 101: | ' <label class="screen_reader" for="username">'.$this->trans('E-mail').'</label>'. |
| 102: | '<input autofocus required type="text" placeholder="'.$this->trans('Username').'" id="username" name="username" value="">'. |
| 103: | ' <label class="screen_reader" for="password">'.$this->trans('Password').'</label>'. |
| 104: | '<input required type="password" id="password" placeholder="'.$this->trans('Password').'" name="password">'; |
| 105: | if (array_key_exists('user', $config) && $config['user'] || array_key_exists('host', $config) && $config['host']) { |
| 106: | $res .= '<input type="hidden" value="other" name="email_provider" />'; |
| 107: | } |
| 108: | else { |
| 109: | $res .= '<select class="dynamic_service_select form-select m-3 w-auto" required name="email_provider"><option value="">'. |
| 110: | $this->trans('E-mail Provider').'</option>'.Nux_Quick_Services::option_list(false, $this). |
| 111: | '<option value="other">'.$this->trans('Other').'</option></select><br />'; |
| 112: | } |
| 113: | $res .= ' <input type="submit" value="'.$this->trans('Login').'" />'; |
| 114: | return $res; |
| 115: | } |
| 116: | else { |
| 117: | $settings = $this->get('changed_settings', array()); |
| 118: | return '<input type="hidden" id="unsaved_changes" value="'. |
| 119: | (!empty($settings) ? '1' : '0').'" />'. |
| 120: | '<input type="hidden" name="hm_page_key" value="'.$this->html_safe(Hm_Request_Key::generate()).'" />'. |
| 121: | '<div class="confirm_logout"><div class="confirm_text">'. |
| 122: | $this->trans('Unsaved changes will be lost! Re-enter your password to save and exit.').' '. |
| 123: | '<a href="?page=save">'.$this->trans('More info').'</a></div>'. |
| 124: | '<label class="screen_reader" for="logout_password">'.$this->trans('Password').'</label>'. |
| 125: | '<input id="logout_password" name="password" class="save_settings_password warn_on_paste" type="password" placeholder="'.$this->trans('Password').'" />'. |
| 126: | '<input class="save_settings" type="submit" name="save_and_logout" value="'.$this->trans('Save and Logout').'" />'. |
| 127: | '<input class="save_settings" id="logout_without_saving" type="submit" name="logout" value="'.$this->trans('Just Logout').'" />'. |
| 128: | '<input class="cancel_logout save_settings" type="button" value="'.$this->trans('Cancel').'" />'. |
| 129: | '</div>'; |
| 130: | } |
| 131: | } |
| 132: | } |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | if (!hm_exists('dynamic_login_config')) { |
| 138: | function dynamic_login_config($config) { |
| 139: | $settings = array( |
| 140: | 'host' => false, |
| 141: | 'user' => false, |
| 142: | 'host_pre' => '', |
| 143: | 'mail_pre' => '', |
| 144: | 'smtp_pre' => '' |
| 145: | ); |
| 146: | $res = $config->dump(); |
| 147: | if (array_key_exists('dynamic_host', $res) && $res['dynamic_host']) { |
| 148: | $settings['host'] = $res['dynamic_host']; |
| 149: | } |
| 150: | if (array_key_exists('dynamic_user', $res) && $res['dynamic_user']) { |
| 151: | $settings['user'] = $res['dynamic_user']; |
| 152: | } |
| 153: | if (array_key_exists('dynamic_host_subdomain', $res) && $res['dynamic_host_subdomain']) { |
| 154: | $settings['host_pre'] = $res['dynamic_host_subdomain']; |
| 155: | } |
| 156: | if (array_key_exists('dynamic_mail_subdomain', $res) && $res['dynamic_mail_subdomain']) { |
| 157: | $settings['mail_pre'] = $res['dynamic_mail_subdomain']; |
| 158: | } |
| 159: | if (array_key_exists('dynamic_smtp_subdomain', $res) && $res['dynamic_smtp_subdomain']) { |
| 160: | $settings['smtp_pre'] = $res['dynamic_smtp_subdomain']; |
| 161: | } |
| 162: | return $settings; |
| 163: | }} |
| 164: | |