Files
2026-11-30 16:32:37 +00:00

288 lines
8.3 KiB
PHP

<?php
/*
* SPDX-FileCopyrightText: Copyright 2020 - 2026 grommunio GmbH
* SPDX-FileCopyrightText: Copyright 2016 Kopano and its licensors
* SPDX-FileCopyrightText: Copyright 2005 - 2016 Zarafa B.V. and its licensors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
require_once __DIR__ . '/class.statecleaner.php';
/**
* Secondary state handling.
*
* This class works exactly the same as standard PHP sessions files. We implement it here
* so we can have improved granularity and don't have to put everything into the session
* variables. Basically we have multiple session objects corresponding to multiple webclient
* objects on the client. This class also locks the session files (in the same way as standard
* PHP sessions), in order to serialize requests from the same client object.
*
* The main reason for creating this is to improve performance; normally PHP will lock the session
* file for as long as your PHP request is running. However, we want to do multiple PHP requests at
* once. For this reason, we only use the PHP session system for login information, and use this
* class for other state information. This means that we can set a message as 'read' at the same time
* as opening a dialog to view a message.
*
* Currently, there is one 'state' for each 'subsystem'. The 'subsystem' is simply a tag which is appended
* to the request URL when the client does an XML request. Each 'subsystem' has its own state file.
*
* Currently the subsystem is equal to the module ID. This means that if you have two requests from the same
* module, they will have to wait for each other. In practice this should hardly ever happen.
*
* It can also create global state that can be accessed by every PHP request.
*/
class State {
/**
* The file pointer of the state file.
*
* @var false|resource
*/
private $fp = false;
/**
* The base directory in which the state files are found.
*/
private $basedir;
/**
* The filename which is opened by this state file.
*/
private $filename;
/**
* Name of the subsystem, used in log messages.
*/
private $subsystem;
/**
* The directory in which the session files are created.
*/
private $sessiondir = "session";
/**
* The unserialized data as it has been read from the file.
*
* @var array<string, mixed>
*/
public $sessioncache = [];
/**
* The raw data as it has been read from the file.
*
* @var null|string
*/
public $contents;
/**
* @param string $subsystem Name of the subsystem
* @param null|string $owner File prefix, defaults to the session id
*/
public function __construct($subsystem, $owner = null) {
$this->basedir = TMP_PATH . DIRECTORY_SEPARATOR . $this->sessiondir;
$this->subsystem = $subsystem;
$this->filename = $this->basedir . DIRECTORY_SEPARATOR . ($owner ?? session_id()) . "." . $subsystem;
}
/**
* State shared by every session of the store owner.
*
* @param string $subsystem Name of the subsystem
*
* @return State
*/
public static function forStore($subsystem) {
return new self($subsystem, 'store_' . hash('sha256', $GLOBALS['mapisession']->getDefaultMessageStoreEntryId()));
}
/**
* Open the session file.
*
* The session file is opened and locked so that other processes cannot access the state information.
*
* @param int $retry Reopen attempts when clean() replaced the file while waiting for its lock
*
* @return bool true when the file is locked
*/
public function open($retry = 2) {
if ($this->fp === false) {
if (!is_dir($this->basedir)) {
if (!@mkdir($this->basedir, 0755, true /* recursive */) && !is_dir($this->basedir)) {
error_log('[STATE ERROR] State directory "' . $this->basedir . '" could not be created.');
return false;
}
}
$cleanupLock = StateCleaner::lock($this->basedir, LOCK_SH);
if ($cleanupLock === false) {
error_log('[STATE ERROR] State cleanup lock could not be acquired.');
return false;
}
$this->fp = @fopen($this->filename, "a+");
if ($this->fp === false) {
StateCleaner::unlock($cleanupLock);
error_log('[STATE ERROR] State file for "' . $this->subsystem . '" could not be opened.');
return false;
}
$this->sessioncache = [];
// Never wait for a busy state file while holding the cleanup lock
$locked = flock($this->fp, LOCK_EX | LOCK_NB);
StateCleaner::unlock($cleanupLock);
if (!$locked && !flock($this->fp, LOCK_EX)) {
fclose($this->fp);
$this->fp = false;
error_log('[STATE ERROR] State file for "' . $this->subsystem . '" could not be locked.');
return false;
}
if (!$locked && !$this->isLinked()) {
$this->close();
if ($retry <= 0) {
error_log('[STATE ERROR] State file for "' . $this->subsystem . '" was replaced while locking.');
return false;
}
return $this->open($retry - 1);
}
if (!@touch($this->filename)) {
error_log('[STATE ERROR] State file for "' . $this->subsystem . '" could not be timestamped.');
}
}
return true;
}
/**
* @return bool true when the locked handle still is the file at $this->filename
*/
private function isLinked() {
if (!is_resource($this->fp)) {
return false;
}
$handle = $this->fp;
clearstatcache(true, $this->filename);
$open = fstat($handle);
$disk = @stat($this->filename);
return $open !== false && $disk !== false && $open['ino'] === $disk['ino'] && $open['dev'] === $disk['dev'];
}
/**
* Read a setting from the state file.
*
* @param string $name Name of the setting to retrieve
*
* @return mixed Value of the state value, or null if not found
*/
public function read($name) {
if (!is_resource($this->fp)) {
dump('[STATE ERROR] State file "' . $this->filename . '" is not open. Open it before reading.');
return null;
}
$handle = $this->fp;
// If the file has already been read, we only have to access
// our cache to obtain the requested data.
if (empty($this->sessioncache)) {
rewind($handle);
$contents = stream_get_contents($handle);
$this->contents = $contents === false ? '' : $contents;
$this->sessioncache = $this->contents === '' ? [] : unserialize($this->contents);
if (!is_array($this->sessioncache)) {
$this->sessioncache = [];
}
}
if (isset($this->sessioncache[$name])) {
return $this->sessioncache[$name];
}
return null;
}
/**
* Write a setting to the state file.
*
* @param string $name Name of the setting to write
* @param mixed $object Value of the object to be written to the setting
* @param bool $flush false to prevent the changes written to disk
* This requires a call to flush() to write the changes to disk
*/
public function write($name, $object, $flush = true) {
if (is_resource($this->fp)) {
// If the file has already been read, then we don't
// need to read the entire file again.
if (empty($this->sessioncache)) {
$this->read($name);
}
$this->sessioncache[$name] = $object;
if ($flush === true) {
$this->flush();
}
}
else {
dump('[STATE ERROR] State file "' . $this->filename . '" is not open. Open it before writing.');
}
}
/**
* Flushes all changes to disk.
*
* This flushes all changes made to $this->sessioncache to disk.
*/
public function flush() {
if (!is_resource($this->fp)) {
dump('[STATE ERROR] State file "' . $this->filename . '" is not open. Open it before writing.');
return;
}
$handle = $this->fp;
if (!empty($this->sessioncache)) {
$contents = serialize($this->sessioncache);
if ($contents !== $this->contents) {
ftruncate($handle, 0);
fseek($handle, 0);
fwrite($handle, $contents);
$this->contents = $contents;
}
}
}
/**
* Close the state file.
*
* This closes and unlocks the state file so that other processes can access the state
*/
public function close() {
if (!is_resource($this->fp)) {
return;
}
$handle = $this->fp;
// release write lock -- fclose does this automatically
// but only in PHP <= 5.3.2
flock($handle, LOCK_UN);
fclose($handle);
$this->fp = false;
}
public function __destruct() {
$this->close();
}
/**
* Cleans all old state information in the session directory.
*
* @param int $maxLifeTime the maximum allowed age of files in seconds
*/
public function clean($maxLifeTime = STATE_FILE_MAX_LIFETIME) {
StateCleaner::clean($this->basedir, $maxLifeTime);
}
}