|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Inject a toggle switch into the login form that makes the session live for a |
| 5 | + * configured number of days (instead of only for the session). |
| 6 | + */ |
| 7 | + |
| 8 | +class persisted_login extends rcube_plugin |
| 9 | +{ |
| 10 | + private $rc; |
| 11 | + private $days; |
| 12 | + |
| 13 | + public function onload(): void |
| 14 | + { |
| 15 | + $this->rc = rcmail::get_instance(); |
| 16 | + $this->load_config(); |
| 17 | + $configured_days = $this->rc->config->get('persisted_login_days'); |
| 18 | + if (!is_int($configured_days)) { |
| 19 | + $configured_days = 7; |
| 20 | + } |
| 21 | + // Make sure the value is in the range 1..365. |
| 22 | + $this->days = min(max(1, $configured_days), 365); |
| 23 | + $this->rc->config->set('session_lifetime', $this->days * 24 * 60); |
| 24 | + } |
| 25 | + |
| 26 | + #[\Override] |
| 27 | + public function init(): void |
| 28 | + { |
| 29 | + $this->rc->output->set_env('persisted_login_days', $this->days); |
| 30 | + $this->add_hook('template_object_loginform', [$this, 'login_page_template']); |
| 31 | + $this->add_hook('login_after', [$this, 'login_success']); |
| 32 | + } |
| 33 | + |
| 34 | + public function login_page_template(array $args): array |
| 35 | + { |
| 36 | + $this->add_texts('localization', true); |
| 37 | + $this->include_script('persisted_login.js'); |
| 38 | + return $args; |
| 39 | + } |
| 40 | + |
| 41 | + public function login_success(array $args): array |
| 42 | + { |
| 43 | + if (empty($_POST['_persisted_login'])) { |
| 44 | + return $args; |
| 45 | + } |
| 46 | + |
| 47 | + $sessCookieName = $this->rc->config->get('session_name') ?: 'roundcube_sessid'; |
| 48 | + $authCookieName = $this->rc->config->get('session_auth_name') ?: 'roundcube_sessauth'; |
| 49 | + $sessCookieValue = session_id(); |
| 50 | + $authCookieValue = (isset($_COOKIE[$authCookieName])) ? $_COOKIE[$authCookieName] : 'Error: Auth Cookie Missing'; |
| 51 | + $exp = time() + ($this->days * 24 * 60 * 60); |
| 52 | + rcube_utils::setcookie($sessCookieName, $sessCookieValue, $exp); |
| 53 | + rcube_utils::setcookie($authCookieName, $authCookieValue, $exp); |
| 54 | + return $args; |
| 55 | + } |
| 56 | +} |
0 commit comments