diff --git a/.gitignore b/.gitignore index 8506351..176666e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ src/db/todolist.db -src/tmp/sessions/sess* src/db/config.php src/db/config-* diff --git a/src/content/lang/en.json b/src/content/lang/en.json index be905a6..a2ca7d6 100644 --- a/src/content/lang/en.json +++ b/src/content/lang/en.json @@ -145,9 +145,6 @@ "set_timezone": "Time zone", "set_autotag": "Autotagging", "set_autotag_descr": "(automatically adds tag of current tag filter to newly created task)", - "set_sessions": "Session handling mechanism", - "set_sessions_php": "PHP", - "set_sessions_files": "Files", "set_firstdayofweek": "First day of week", "set_custom": "Custom", "set_date": "Date format", diff --git a/src/content/lang/ru.json b/src/content/lang/ru.json index 3d91676..8c59aad 100644 --- a/src/content/lang/ru.json +++ b/src/content/lang/ru.json @@ -145,9 +145,6 @@ "set_timezone": "Часовой пояс", "set_autotag": "Autotagging", "set_autotag_descr": "(автодобавление текущего тега из фильтра в новую задачу)", - "set_sessions": "Хранилище сессий", - "set_sessions_php": "PHP", - "set_sessions_files": "Файлы", "set_firstdayofweek": "Первый день недели", "set_custom": "другой", "set_date": "Формат даты", diff --git a/src/includes/class.sessionhandler.php b/src/includes/class.sessionhandler.php new file mode 100644 index 0000000..0354e60 --- /dev/null +++ b/src/includes/class.sessionhandler.php @@ -0,0 +1,97 @@ +db = DBConnection::instance(); + return true; + } + + /** @return bool */ + public function close() + { + return true; + } + + /** + * @param string $id + * @return string + * @throws Exception + */ + public function read($id) + { + // read session data if not expired + $expire = time(); + $r = $this->db->sq("SELECT data,last_access FROM {$this->db->prefix}sessions WHERE id = ? AND expires >= $expire", $id); + if ( is_null($r) ) return ''; + + // update last access time and set expires in 14 days + // refresh once in a second + if ( $r[1] < time() ) { + $expire = time() + 14 * 86400; + $this->db->ex("UPDATE {$this->db->prefix}sessions SET last_access=?,expires=? WHERE id = ?", + array(time(), $expire, $id) ); + } + return $r[0]; + } + + /** + * @param string $id + * @param string $data + * @return bool + * @throws Exception + */ + public function write($id, $data) + { + $exists = $this->db->sq("SELECT COUNT(*) FROM {$this->db->prefix}sessions WHERE id = ?", $id); + if (!$exists) { + // Create new session with 14 days lifetime + $expire = time() + 14 * 86400; + $this->db->ex("INSERT INTO {$this->db->prefix}sessions (id,data,expires) VALUES (?,?,?)", + array($id, $data, $expire) ); + } + else { + // Update existing session + $this->db->ex("UPDATE {$this->db->prefix}sessions SET data = ? WHERE id = ?", + array($data, $id) ); + } + return true; + } + + /** + * @param string $id + * @return bool + * @throws Exception + */ + public function destroy($id) + { + $this->db->ex("DELETE FROM {$this->db->prefix}sessions WHERE id = ?", $id); + return true; + } + + /** + * @param int $max_lifetime + * @return int|false + */ + public function gc($max_lifetime) + { + // We ignore php runtime 'session.gc_maxlifetime' + $expire = time(); + $affected = $this->db->ex("DELETE FROM {$this->db->prefix}sessions WHERE expires < $expire"); + return $affected; + } +} + +?> \ No newline at end of file diff --git a/src/init.php b/src/init.php index 162650d..31c2f09 100644 --- a/src/init.php +++ b/src/init.php @@ -92,14 +92,8 @@ $_mttinfo = array(); if (need_auth() && !isset($dontStartSession)) { - if(Config::get('session') == 'files') - { - session_save_path(MTTPATH. 'tmp/sessions'); - ini_set('session.gc_maxlifetime', '1209600'); # 14 days session file minimum lifetime - ini_set('session.gc_probability', 1); - ini_set('session.gc_divisor', 10); - } - + require_once(MTTINC. 'class.sessionhandler.php'); + session_set_save_handler(new MTTSessionHandler()); ini_set('session.use_cookies', true); ini_set('session.use_only_cookies', true); session_set_cookie_params(1209600, url_dir(Config::get('url')=='' ? getRequestUri() : Config::getUrl('url'))); # 14 days session cookie lifetime diff --git a/src/mytinytodo_lang.php b/src/mytinytodo_lang.php index 8b2e248..f7f2b94 100644 --- a/src/mytinytodo_lang.php +++ b/src/mytinytodo_lang.php @@ -146,9 +146,6 @@ class DefaultLang 'set_timezone' => "Time zone", 'set_autotag' => "Autotagging", 'set_autotag_descr' => "(automatically adds tag of current tag filter to newly created task)", - 'set_sessions' => "Session handling mechanism", - 'set_sessions_php' => "PHP", - 'set_sessions_files' => "Files", 'set_firstdayofweek' => "First day of week", 'set_custom' => "Custom", 'set_date' => "Date format", diff --git a/src/settings.php b/src/settings.php index 242a688..7ac72ac 100644 --- a/src/settings.php +++ b/src/settings.php @@ -41,7 +41,6 @@ if(isset($_POST['save'])) catch (Exception $e) { } Config::set('autotag', (int)_post('autotag')); - Config::set('session', _post('session')); Config::set('firstdayofweek', (int)_post('firstdayofweek')); Config::set('clock', (int)_post('clock')); Config::set('dateformat', _post('dateformat')); @@ -231,13 +230,6 @@ header('Content-type:text/html; charset=utf-8'); -
-
:
-
-
- (<mytinytodo_dir>/tmp/sessions) -
-
:
diff --git a/src/setup.php b/src/setup.php index 42bc8fe..234b775 100644 --- a/src/setup.php +++ b/src/setup.php @@ -167,6 +167,17 @@ if (!$ver) UNIQUE KEY `param_key` (`param_key`) ) CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci "); + + $db->ex( +"CREATE TABLE {$db->prefix}sessions ( + `id` VARCHAR(64) NOT NULL default '', /* upto 64 bytes for sha256 */ + `data` TEXT, + `last_access` INT UNSIGNED NOT NULL default 0, /* time() timestamp */ + `expires` INT UNSIGNED NOT NULL default 0, /* time() timestamp */ +UNIQUE KEY `id` (`id`) +) CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci "); + + } catch (Exception $e) { exitMessage("Error: ". htmlarray($e->getMessage())); } @@ -239,6 +250,17 @@ UNIQUE KEY `param_key` (`param_key`) $db->ex("CREATE UNIQUE INDEX settings_key ON {$db->prefix}settings (param_key COLLATE NOCASE)"); + + $db->ex( +"CREATE TABLE {$db->prefix}sessions ( + id VARCHAR(64) NOT NULL default '', + data TEXT, + last_access INTEGER UNSIGNED NOT NULL default 0, + expires INTEGER UNSIGNED NOT NULL default 0 +) "); + + $db->ex("CREATE UNIQUE INDEX sessions_id ON {$db->prefix}sessions (id COLLATE NOCASE)"); + } catch (Exception $e) { exitMessage("Error: ". htmlarray($e->getMessage())); } @@ -390,6 +412,7 @@ function myExceptionHandler($e) function update_14_17(Database_Abstract $db, $dbtype) { $db->ex("BEGIN"); + if($dbtype=='mysql') { # convert charset to utf8mb4 @@ -407,6 +430,18 @@ function update_14_17(Database_Abstract $db, $dbtype) `param_value` TEXT, UNIQUE KEY `param_key` (`param_key`) ) CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci "); + + # create sessions table + + $db->ex( +"CREATE TABLE {$db->prefix}sessions ( + `id` VARCHAR(64) NOT NULL default '', + `data` TEXT, + `last_access` INT UNSIGNED NOT NULL default 0, + `expires` INT UNSIGNED NOT NULL default 0, +UNIQUE KEY `id` (`id`) +) CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci "); + } else #sqlite @@ -417,7 +452,20 @@ UNIQUE KEY `param_key` (`param_key`) param_value TEXT ) "); $db->ex("CREATE UNIQUE INDEX settings_key ON {$db->prefix}settings (param_key COLLATE NOCASE)"); + + # sessions + + $db->ex( +"CREATE TABLE {$db->prefix}sessions ( + id VARCHAR(100) NOT NULL default '', + data TEXT, + last_access INTEGER UNSIGNED NOT NULL default 0, + expires INTEGER UNSIGNED NOT NULL default 0 +) "); + + $db->ex("CREATE UNIQUE INDEX sessions_id ON {$db->prefix}sessions (id COLLATE NOCASE)"); } + $db->ex("COMMIT"); Config::save(); diff --git a/src/tmp/.htaccess b/src/tmp/.htaccess deleted file mode 100644 index 8d2f256..0000000 --- a/src/tmp/.htaccess +++ /dev/null @@ -1 +0,0 @@ -deny from all diff --git a/src/tmp/sessions/empty b/src/tmp/sessions/empty deleted file mode 100644 index e69de29..0000000