Files
Kelx/grommunio-web-5.2.73.g1be61255c/server/includes/util.php
T
2026-11-30 16:32:37 +00:00

1265 lines
39 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
*/
/**
* Utility functions.
*/
require_once BASE_PATH . 'server/includes/exceptions/class.JSONException.php';
require_once __DIR__ . '/hexutil.php';
/**
* Function which reads the data stream. This data is send by the WebClient.
*
* @return string data
*/
function readData() {
static $cache = null;
if ($cache !== null) {
return $cache;
}
$data = "";
$putData = fopen("php://input", "r");
while ($block = fread($putData, 1024)) {
$data .= $block;
}
fclose($putData);
$cache = $data;
return $data;
}
/*
* Add in config specified default domain to email if no domain is set in form.
* If no default domain is set in config, the input string will be return without changes.
*
* @param string $user the user to append the domain to
* @return string email
*/
function appendDefaultDomain($user) {
if (empty($user)) {
return '';
}
if (!defined('DEFAULT_DOMAIN') || empty(DEFAULT_DOMAIN) || str_contains((string) $user, '@')) {
return $user;
}
return $user . "@" . DEFAULT_DOMAIN;
}
/**
* Function which is called every time the "session_start" method is called.
* It unserializes the objects in the session. This function called by PHP.
*
* @param string $className the class name of the object in the session
*/
function sessionNotifierLoader($className) {
$className = strtolower((string) $className); // for PHP5 set className to lower case to find the file (see ticket #839 for more information)
switch ($className) {
case "bus":
require_once BASE_PATH . 'server/includes/core/class.bus.php';
break;
default:
$path = BASE_PATH . 'server/includes/notifiers/class.' . $className . '.php';
if (is_file($path)) {
require_once $path;
}
else {
$path = $GLOBALS['PluginManager']->getNotifierFilePath($className);
if (is_file($path)) {
require_once $path;
}
}
break;
}
if (!class_exists($className)) {
trigger_error("Can't load " . $className . " while unserializing the session.", E_USER_WARNING);
}
}
/**
* Function which checks if an array is an associative array.
*
* @param array $data array which should be verified
*
* @return bool true if the given array is an associative array, false if not
*/
function is_assoc_array($data) {
return is_array($data) && !empty($data) && !preg_match('/^\d+$/', implode('', array_keys($data)));
}
/**
* gets maximum upload size of attachment from php ini settings
* important settings are upload_max_filesize and post_max_size
* upload_max_filesize specifies maximum upload size for attachments
* post_max_size must be larger than upload_max_filesize.
* these values are overwritten in .htaccess file of WA.
*
* @param mixed $as_string
*
* @return string return max value either upload max filesize or post max size
*/
function getMaxUploadSize($as_string = false) {
$upload_max_value = strtoupper(ini_get('upload_max_filesize'));
$post_max_value = getMaxPostRequestSize();
/*
* If POST_MAX_SIZE is lower than UPLOAD_MAX_FILESIZE, it becomes the
* effective upload limit because the request also contains header data.
*/
// calculate upload_max_value value to bytes
if (str_contains($upload_max_value, "K")) {
$upload_max_value = ((int) $upload_max_value) * 1024;
}
elseif (str_contains($upload_max_value, "M")) {
$upload_max_value = ((int) $upload_max_value) * 1024 * 1024;
}
elseif (str_contains($upload_max_value, "G")) {
$upload_max_value = ((int) $upload_max_value) * 1024 * 1024 * 1024;
}
// check which one is larger
$value = $upload_max_value;
if ($upload_max_value > $post_max_value) {
$value = $post_max_value;
}
if ($as_string) {
// make user readable string
if ($value > (1024 * 1024 * 1024)) {
$value = round($value / (1024 * 1024 * 1024), 1) . " " . _("GB");
}
elseif ($value > (1024 * 1024)) {
$value = round($value / (1024 * 1024), 1) . " " . _("MB");
}
elseif ($value > 1024) {
$value = round($value / 1024, 1) . " " . _("KB");
}
else {
$value = $value . " " . _("B");
}
}
return $value;
}
/**
* Gets maximum post request size of attachment from php ini settings.
* post_max_size specifies maximum size of a post request,
* we are uploading attachment using post method.
*
* @return string returns the post request size with proper unit(MB, GB, KB etc.).
*/
function getMaxPostRequestSize() {
$post_max_value = strtoupper(ini_get('post_max_size'));
// calculate post_max_value value to bytes
if (str_contains($post_max_value, "K")) {
$post_max_value = ((int) $post_max_value) * 1024;
}
elseif (str_contains($post_max_value, "M")) {
$post_max_value = ((int) $post_max_value) * 1024 * 1024;
}
elseif (str_contains($post_max_value, "G")) {
$post_max_value = ((int) $post_max_value) * 1024 * 1024 * 1024;
}
return $post_max_value;
}
/**
* Get maximum number of files that can be uploaded in single request from php ini settings.
* max_file_uploads specifies maximum number of files allowed in post request.
*
* @return number maximum number of files can uploaded in single request
*/
function getMaxFileUploads() {
return (int) ini_get('max_file_uploads');
}
/**
* cleanTemp.
*
* Cleans up the temp directory.
*
* @param string $directory the path to the temp dir or sessions dir
* @param int $maxLifeTime the maximum allowed age of files in seconds
* @param bool $recursive False to prevent the folder to be cleaned up recursively
* @param bool $removeSubs False to prevent empty subfolders from being deleted
*
* @return bool True if the folder is empty
*/
function cleanTemp($directory = TMP_PATH, $maxLifeTime = STATE_FILE_MAX_LIFETIME, $recursive = true, $removeSubs = true) {
if (!is_dir($directory)) {
return;
}
// PHP doesn't do this by itself, so before running through
// the folder, we should flush the statcache, so the 'atime'
// is current.
clearstatcache();
$dir = @opendir($directory);
if ($dir === false) {
return false;
}
$is_empty = true;
while (($file = readdir($dir)) !== false) {
// Skip special folders
if ($file === '.' || $file === '..') {
continue;
}
$path = $directory . DIRECTORY_SEPARATOR . $file;
if (is_dir($path)) {
// If it is a directory, check if we need to
// recursively clean this subfolder.
if ($recursive) {
// If cleanTemp indicates the subfolder is empty,
// and $removeSubs is true, we must delete the subfolder
// otherwise the currently folder is not empty.
if (cleanTemp($path, $maxLifeTime, $recursive) && $removeSubs) {
rmdir($path);
}
else {
$is_empty = false;
}
}
else {
// We are not cleaning recursively, the current
// folder is not empty.
$is_empty = false;
}
}
else {
$fileinfo = stat($path);
if ($fileinfo !== false && $fileinfo["atime"] < time() - $maxLifeTime) {
unlink($path);
}
else {
$is_empty = false;
}
}
}
closedir($dir);
return $is_empty;
}
function cleanSearchFolders() {
$store = $GLOBALS["mapisession"]->getDefaultMessageStore();
$storeProps = mapi_getprops($store, [PR_STORE_SUPPORT_MASK, PR_FINDER_ENTRYID]);
if (($storeProps[PR_STORE_SUPPORT_MASK] & STORE_SEARCH_OK) !== STORE_SEARCH_OK) {
return;
}
$finderfolder = mapi_msgstore_openentry($store, $storeProps[PR_FINDER_ENTRYID]);
// Match both legacy ("grommunio Web Search Folder") and new-style
// ("grommunio Web Search YYYYMMDD-xxxx") search folders using a
// common prefix. The time-based filter removes folders that have
// not been modified since the session GC lifetime expired.
$hierarchytable = mapi_folder_gethierarchytable($finderfolder, MAPI_DEFERRED_ERRORS);
mapi_table_restrict($hierarchytable, [RES_AND,
[
[RES_CONTENT,
[
FUZZYLEVEL => FL_PREFIX,
ULPROPTAG => PR_DISPLAY_NAME,
VALUE => [PR_DISPLAY_NAME => 'grommunio Web Search '],
],
],
[RES_PROPERTY,
[
RELOP => RELOP_LT,
ULPROPTAG => PR_LAST_MODIFICATION_TIME,
VALUE => [PR_LAST_MODIFICATION_TIME => (time() - ini_get("session.gc_maxlifetime"))],
],
],
],
], TBL_BATCH);
$folders = mapi_table_queryallrows($hierarchytable, [PR_ENTRYID]);
foreach ($folders as $folder) {
mapi_folder_deletefolder($finderfolder, $folder[PR_ENTRYID]);
}
}
function dechex_32($dec) {
// Because on 64bit systems PHP handles integers as 64bit,
// we need to convert these 64bit integers to 32bit when we
// want the hex value
$result = unpack("H*", pack("N", $dec));
return $result[1];
}
/**
* This function will encode the input string for the header based on the browser that makes the
* HTTP request. MSIE and Edge has an issue with unicode filenames. All browsers do not seem to follow
* the RFC specification. Firefox requires an unencoded string in the HTTP header. MSIE and Edge will
* break on this and requires encoding.
*
* @param string $input Unencoded string
*
* @return string Encoded string
*/
function browserDependingHTTPHeaderEncode($input) {
$input = removeHTTPControlCharacters($input);
if (!isEdge()) {
return $input;
}
return rawurlencode((string) $input);
}
/**
* Remove characters which cannot safely occur in an HTTP response header.
*
* @param mixed $value
*
* @return string
*/
function removeHTTPControlCharacters($value) {
return (string) preg_replace('/[\x00-\x1f\x7f]/', '', (string) $value);
}
/**
* Normalize an untrusted MIME type for use in a Content-Type header.
*
* Parameters are deliberately discarded. Download responses do not need
* them, and accepting only the media type keeps the header grammar small.
*
* @param mixed $contentType MIME type to normalize
* @param string $fallback MIME type returned for invalid input
*
* @return string normalized lowercase MIME type
*/
function normalizeHTTPContentType($contentType, $fallback = 'application/octet-stream') {
$contentType = trim(explode(';', (string) $contentType, 2)[0]);
if (preg_match('/\A[a-z0-9][a-z0-9.+_-]*\/[a-z0-9][a-z0-9.+_-]*\z/i', $contentType) !== 1) {
return $fallback;
}
return strtolower($contentType);
}
/**
* Check whether a MIME type is safe to render inline in the webapp origin.
*
* Scriptable document formats such as HTML, XML and SVG are intentionally not
* included. Keep this list aligned with the file preview components.
*
* @param mixed $contentType normalized or raw MIME type
*
* @return bool true when inline rendering is allowed
*/
function isSafeInlineContentType($contentType) {
return in_array(normalizeHTTPContentType($contentType), [
'application/pdf',
'audio/aac',
'audio/flac',
'audio/mp4',
'audio/mpeg',
'audio/ogg',
'audio/wav',
'audio/webm',
'audio/x-wav',
'image/avif',
'image/bmp',
'image/gif',
'image/jpeg',
'image/png',
'image/tiff',
'image/vnd.microsoft.icon',
'image/webp',
'image/x-icon',
'text/plain',
'video/mp4',
'video/mpeg',
'video/ogg',
'video/quicktime',
'video/webm',
], true);
}
/**
* Select a safe Content-Disposition value for a download response.
*
* @param mixed $requestedDisposition requested value
* @param mixed $contentType response MIME type
*
* @return string "inline" for safe preview types, otherwise "attachment"
*/
function getDownloadContentDisposition($requestedDisposition, $contentType) {
return $requestedDisposition === 'inline' && isSafeInlineContentType($contentType) ? 'inline' : 'attachment';
}
/**
* Prevent downloaded content from being promoted to an executable document.
*/
function sendDownloadSecurityHeaders() {
header('X-Content-Type-Options: nosniff');
header("Content-Security-Policy: sandbox; default-src 'none'");
}
/**
* Helps to detect if the request came from Edge or not.
*
* @return bool true if Edge is the requester, position of the word otherwise
*/
function isEdge() {
return str_contains((string) $_SERVER['HTTP_USER_AGENT'], 'Edge');
}
/**
* This function will return base name of the file from the full path of the file.
* PHP's basename() does not properly support streams or filenames beginning with a non-US-ASCII character.
* The default implementation in php for basename is locale aware. So it will truncate umlauts which can not be
* parsed by the current set locale.
* This problem only occurs with PHP < 5.2.
*
* @see http://bugs.php.net/bug.php?id=37738, https://bugs.php.net/bug.php?id=37268
*
* @param string $filepath full path of the file
* @param string $suffix suffix that will be trimmed from file name
*
* @return string base name of the file
*/
function mb_basename($filepath, $suffix = '') {
// Remove right-most slashes when $uri points to directory.
$filepath = rtrim($filepath, DIRECTORY_SEPARATOR . ' ');
// Returns the trailing part of the $uri starting after one of the directory
// separators.
$filename = preg_match('@[^' . preg_quote(DIRECTORY_SEPARATOR, '@') . ']+$@', $filepath, $matches) ? $matches[0] : '';
// Cuts off a suffix from the filename.
if ($suffix) {
$filename = preg_replace('@' . preg_quote($suffix, '@') . '$@', '', $filename);
}
return $filename;
}
/**
* Function is used to get data from query string and store it in session
* for use when webapp is completely loaded.
*/
function storeURLDataToSession() {
$data = [];
$urlData = urldecode((string) $_SERVER['QUERY_STRING']);
if (!empty($_GET['action']) && $_GET['action'] === 'mailto') {
$data['mailto'] = $_GET['to'];
// There may be some data after to field, like cc, subject, body
// So add them in the urlData string as well
$pos = stripos($urlData, (string) $_GET['to']) + strlen((string) $_GET['to']);
$subString = substr($urlData, $pos);
$data['mailto'] .= $subString;
}
if (!empty($_GET['action']) && $_GET['action'] === 'profile') {
$data['profile'] = true;
}
if (!empty($data)) {
// finally store all data to session
$_SESSION['url_action'] = $data;
}
}
// Constants for regular expressions which are used in get method to verify the input string
define("ID_REGEX", "/\\A[a-z0-9_]+\\z/i");
define("STRING_REGEX", "/\\A[a-z0-9_\\s()@]+\\z/i");
define("USERNAME_REGEX", "/\\A[a-z0-9\\-\\.\\'_@]+\\z/i");
define("ALLOWED_EMAIL_CHARS_REGEX", "/\\A[-a-z0-9_\\.@!#\$%&'\\*\\+\\/\\=\\?\\^_`\\{\\|\\}~]+\\z/i");
define("NUMERIC_REGEX", "/\\A[0-9]+\\z/i");
// Don't allow "\/:*?"<>|" characters in filename.
define("FILENAME_REGEX", "/\\A[^\\/\\:\\*\\?\"\\<\\>\\|]+\\z/i");
/**
* Function to sanitize user input values to prevent XSS attacks.
*
* @param mixed $value value that should be sanitized
* @param mixed $default default value to return when value is not safe
* @param false|string $regex regex to validate values based on type of value passed
*/
function sanitizeValue($value, $default = '', $regex = false) {
$value = (string) $value;
if (preg_match('/[\x00-\x1f\x7f]/', $value)) {
return $default;
}
$result = addslashes($value);
if ($regex) {
if (preg_match($regex, $result) !== 1) {
$result = $default;
}
}
return $result;
}
/**
* Function to sanitize user input values to prevent XSS attacks.
*
* @param string $key key that should be used to get value from $_GET to sanitize value
* @param mixed $default default value to return when value is not safe
* @param false|string $regex regex to validate values based on type of value passed
*/
function sanitizeGetValue($key, $default = '', $regex = false) {
// check if value really exists
if (isset($_GET[$key])) {
return sanitizeValue($_GET[$key], $default, $regex);
}
return $default;
}
/**
* Function to sanitize user input values to prevent XSS attacks.
*
* @param string $key key that should be used to get value from $_POST to sanitize value
* @param mixed $default default value to return when value is not safe
* @param false|string $regex regex to validate values based on type of value passed
*/
function sanitizePostValue($key, $default = '', $regex = false) {
// check if value really exists
if (isset($_POST[$key])) {
return sanitizeValue($_POST[$key], $default, $regex);
}
return $default;
}
function parse_smime__join_xph(&$prop, $msg) {
$a = mapi_getprops($msg, [PR_TRANSPORT_MESSAGE_HEADERS]);
$a = $a[PR_TRANSPORT_MESSAGE_HEADERS] ?? "";
$prop[PR_TRANSPORT_MESSAGE_HEADERS] =
"# Outer headers:\n" . ($prop[PR_TRANSPORT_MESSAGE_HEADERS] ?? "") .
"# Inner headers:\n" . $a;
}
/**
* Keep the outer PR_SUBJECT in $prop only when the unwrapped message has none.
*
* @param array $prop properties about to be set on $msg
* @param resource $msg unwrapped message
*/
function parse_smime__keep_subject(&$prop, $msg) {
$inner = mapi_getprops($msg, [PR_SUBJECT]);
if (isset($inner[PR_SUBJECT]) || !isset($prop[PR_SUBJECT])) {
unset($prop[PR_SUBJECT]);
}
}
/**
* Function will be used to decode smime messages and convert it to normal messages.
*
* @param resource $store user's store
* @param resource $message S/MIME message
*/
function parse_smime($store, $message) {
// OpenPGP and S/MIME share the request-local secure-message lifecycle.
// Give a protocol plugin the original MIME before the S/MIME parser unwraps
// it; OpenPGP also uses MultipartSigned in standards-compliant MAPI stores.
$handled = false;
$GLOBALS['PluginManager']->triggerHook('server.util.parse_secure.before', [
'store' => $store,
'message' => &$message,
'handled' => &$handled,
]);
if ($handled) {
return;
}
$props = mapi_getprops($message, [PR_MESSAGE_CLASS, PR_MESSAGE_FLAGS,
PR_SENT_REPRESENTING_NAME, PR_SENT_REPRESENTING_ENTRYID, PR_SENT_REPRESENTING_SEARCH_KEY,
PR_SENT_REPRESENTING_EMAIL_ADDRESS, PR_SENT_REPRESENTING_SMTP_ADDRESS,
PR_SENT_REPRESENTING_ADDRTYPE, PR_CLIENT_SUBMIT_TIME, PR_TRANSPORT_MESSAGE_HEADERS, PR_REPLY_RECIPIENT_ENTRIES,
PR_SUBJECT]);
$read = $props[PR_MESSAGE_FLAGS] & MSGFLAG_READ;
$smimeMessage = false;
if (class_match_prefix($props[PR_MESSAGE_CLASS], "IPM.Note.SMIME.MultipartSigned")) {
// this is a signed message. decode it.
$atable = mapi_message_getattachmenttable($message);
$rows = mapi_table_queryallrows($atable, [PR_ATTACH_MIME_TAG, PR_ATTACH_NUM]);
$attnum = false;
foreach ($rows as $row) {
if (isset($row[PR_ATTACH_MIME_TAG]) && $row[PR_ATTACH_MIME_TAG] == 'multipart/signed') {
$attnum = $row[PR_ATTACH_NUM];
}
}
if ($attnum !== false) {
$att = mapi_message_openattach($message, $attnum);
$data = mapi_openproperty($att, PR_ATTACH_DATA_BIN);
// Allowing to hook in before the signed attachment is removed
$GLOBALS['PluginManager']->triggerHook('server.util.parse_smime.signed', [
'store' => $store,
'props' => $props,
'message' => &$message,
'data' => &$data,
]);
// also copy recipients because they are lost after mapi_inetmapi_imtomapi
$origRcptTable = mapi_message_getrecipienttable($message);
if (!isset($GLOBALS["properties"])) {
$GLOBALS["properties"] = new Properties();
}
$origRecipients = mapi_table_queryallrows($origRcptTable, $GLOBALS["properties"]->getRecipientProperties());
mapi_inetmapi_imtomapi($GLOBALS['mapisession']->getSession(), $store, $GLOBALS['mapisession']->getAddressbook(), $message, $data, ["parse_smime_signed" => 1]);
parse_smime__join_xph($props, $message);
$decapRcptTable = mapi_message_getrecipienttable($message);
$decapRecipients = mapi_table_queryallrows($decapRcptTable, $GLOBALS["properties"]->getRecipientProperties());
if (empty($decapRecipients) && !empty($origRecipients)) {
mapi_message_modifyrecipients($message, MODRECIP_ADD, $origRecipients);
}
$tmpprops = [
PR_MESSAGE_CLASS => $props[PR_MESSAGE_CLASS],
PR_SENT_REPRESENTING_NAME => $props[PR_SENT_REPRESENTING_NAME],
PR_SENT_REPRESENTING_ENTRYID => $props[PR_SENT_REPRESENTING_ENTRYID],
PR_SENT_REPRESENTING_SEARCH_KEY => $props[PR_SENT_REPRESENTING_SEARCH_KEY],
PR_SENT_REPRESENTING_EMAIL_ADDRESS => $props[PR_SENT_REPRESENTING_EMAIL_ADDRESS] ?? '',
PR_SENT_REPRESENTING_SMTP_ADDRESS => $props[PR_SENT_REPRESENTING_SMTP_ADDRESS] ?? '',
PR_SENT_REPRESENTING_ADDRTYPE => $props[PR_SENT_REPRESENTING_ADDRTYPE] ?? 'SMTP',
PR_CLIENT_SUBMIT_TIME => $props[PR_CLIENT_SUBMIT_TIME] ?? time(),
PR_TRANSPORT_MESSAGE_HEADERS => ($props[PR_TRANSPORT_MESSAGE_HEADERS] ?? ""),
PR_SUBJECT => $props[PR_SUBJECT] ?? null,
];
if (isset($props[PR_REPLY_RECIPIENT_ENTRIES])) {
$tmpprops[PR_REPLY_RECIPIENT_ENTRIES] = $props[PR_REPLY_RECIPIENT_ENTRIES];
}
parse_smime__keep_subject($tmpprops, $message);
mapi_setprops($message, $tmpprops);
}
$smimeMessage = true;
}
elseif (class_match_prefix($props[PR_MESSAGE_CLASS], "IPM.Note.SMIME")) {
// this is a encrypted message. decode it.
$attachTable = mapi_message_getattachmenttable($message);
$rows = mapi_table_queryallrows($attachTable, [PR_ATTACH_MIME_TAG, PR_ATTACH_NUM, PR_ATTACH_LONG_FILENAME]);
$attnum = false;
foreach ($rows as $row) {
if (isset($row[PR_ATTACH_MIME_TAG]) && in_array($row[PR_ATTACH_MIME_TAG], ['application/x-pkcs7-mime', 'application/pkcs7-mime'])) {
$attnum = $row[PR_ATTACH_NUM];
}
}
if ($attnum !== false) {
$att = mapi_message_openattach($message, $attnum);
$data = mapi_openproperty($att, PR_ATTACH_DATA_BIN);
// also copy recipients because they are lost after decrypting
$origRcptTable = mapi_message_getrecipienttable($message);
if (!isset($GLOBALS["properties"])) {
$GLOBALS["properties"] = new Properties();
}
$origRecipients = mapi_table_queryallrows($origRcptTable, $GLOBALS["properties"]->getRecipientProperties());
// Allowing to hook in before the encrypted attachment is removed
$GLOBALS['PluginManager']->triggerHook('server.util.parse_smime.encrypted', [
'store' => $store,
'props' => $props,
'message' => &$message,
'data' => &$data,
]);
// after decrypting $message is a IPM.Note message,
// deleting an attachment removes an actual attachment of the message
$mprops = mapi_getprops($message, [PR_MESSAGE_CLASS]);
if (isSmimePluginEnabled() &&
class_match_prefix($mprops[PR_MESSAGE_CLASS], "IPM.Note.SMIME")) {
mapi_message_deleteattach($message, $attnum);
}
$decapRcptTable = mapi_message_getrecipienttable($message);
$decapRecipients = mapi_table_queryallrows($decapRcptTable, $GLOBALS["properties"]->getRecipientProperties());
if (empty($decapRecipients) && !empty($origRecipients)) {
mapi_message_modifyrecipients($message, MODRECIP_ADD, $origRecipients);
}
}
$smimeMessage = true;
}
// mark the message as read if the main message has read flag
if ($smimeMessage && $read) {
$mprops = mapi_getprops($message, [PR_MESSAGE_FLAGS]);
mapi_setprops($message, [PR_MESSAGE_FLAGS => $mprops[PR_MESSAGE_FLAGS] | MSGFLAG_READ]);
}
}
/**
* Helper function which used to check smime plugin is enabled.
*
* @return bool true if smime plugin is enabled else false
*/
function isSmimePluginEnabled() {
return $GLOBALS['settings']->get("grommunio/v1/plugins/smime/enable", false);
}
/**
* Kept for plugins; new code calls readMapiPropStream() directly.
*
* @param resource $mapiobj MAPI message or store
* @param int $proptag MAPI property tag
*
* @return string the streamed data
*/
function streamProperty($mapiobj, $proptag) {
return readMapiPropStream($mapiobj, $proptag);
}
/**
* Function will decode JSON string into objects.
*
* @param string $jsonString JSON data that should be decoded
* @param bool $toAssoc flag to indicate that associative arrays should be
* returned as objects or arrays, true means it will return associative array as arrays and
* false will return associative arrays as objects
*
* @return mixed decoded data
*/
function json_decode_data($jsonString, $toAssoc = false) {
$data = json_decode($jsonString, $toAssoc);
$errorString = '';
switch (json_last_error()) {
case JSON_ERROR_DEPTH:
$errorString = _("The maximum stack depth has been exceeded");
break;
case JSON_ERROR_CTRL_CHAR:
$errorString = _("Control character error, possibly incorrectly encoded");
break;
case JSON_ERROR_STATE_MISMATCH:
$errorString = _("Invalid or malformed JSON");
break;
case JSON_ERROR_SYNTAX:
$errorString = _("Syntax error");
break;
case JSON_ERROR_UTF8:
$errorString = _("Malformed UTF-8 characters, possibly incorrectly encoded");
break;
}
if (!empty($errorString)) {
throw new JsonException(sprintf(_("Some problem encountered when encoding/decoding JSON data: - %s"), $errorString), json_last_error(), null);
}
return $data;
}
/**
* Tries to open the IPM subtree.
*
* @param resource $store the store to retrieve IPM subtree from
* @param bool $logErrors whether to log open failures
*
* @return mixed false if the subtree cannot be opened,
* the IPM subtree resource otherwise
*/
function getSubTree($store, $logErrors = true) {
$storeProps = mapi_getprops($store, [PR_IPM_SUBTREE_ENTRYID]);
if (!isset($storeProps[PR_IPM_SUBTREE_ENTRYID])) {
return false;
}
try {
return mapi_msgstore_openentry($store, $storeProps[PR_IPM_SUBTREE_ENTRYID]);
}
catch (MAPIException $e) {
if ($logErrors && ($e->getCode() == MAPI_E_NOT_FOUND || $e->getCode() == MAPI_E_INVALID_ENTRYID)) {
$username = $GLOBALS["mapisession"]->getUserName();
error_log(sprintf("Unable to open IPM_SUBTREE (%s) for %s",
bin2hex($storeProps[PR_IPM_SUBTREE_ENTRYID]), $username) . ': ' . $e->getMessage());
}
}
return false;
}
/**
* Fetches the full hierarchy and returns an array with a cache of the stat
* of the folders in the hierarchy. Passing the folderType is required for cases where
* the user has permission on the inbox folder, but no folder visible
* rights on the rest of the store.
*
* @param string $username the user who's store to retrieve hierarchy counters from.
* If no username is given, the currently logged in user's store will be used.
* @param string $folderType if inbox use the inbox as root folder
* @param mixed $store optional already opened store to retrieve hierarchy counters from
* @param bool $logErrors whether to log root folder open failures
*
* @return array folderStatCache a cache of the hierarchy folders
*/
function updateHierarchyCounters($username = '', $folderType = '', $store = null, $logErrors = true) {
// Open the correct store
if (!$store) {
if ($username) {
$userEntryid = $GLOBALS["mapisession"]->getStoreEntryIdOfUser($username);
$store = $userEntryid ? $GLOBALS["mapisession"]->openMessageStore($userEntryid) : false;
}
else {
$store = $GLOBALS["mapisession"]->getDefaultMessageStore();
}
}
if (!$store) {
return [];
}
$props = [PR_DISPLAY_NAME, PR_LOCAL_COMMIT_TIME_MAX, PR_CONTENT_COUNT, PR_CONTENT_UNREAD, PR_ENTRYID, PR_STORE_ENTRYID, PR_CONTAINER_CLASS];
if ($folderType === 'inbox') {
try {
$rootFolder = mapi_msgstore_getreceivefolder($store);
}
catch (MAPIException $e) {
if ($logErrors) {
$username = $GLOBALS["mapisession"]->getUserName();
error_log(sprintf("Unable to open Inbox for %s. MAPI Error '%s'", $username, get_mapi_error_name($e->getCode())) . ": " . $e->getMessage());
}
return [];
}
}
else {
$rootFolder = getSubTree($store, $logErrors);
if (!$rootFolder) {
return [];
}
}
$hierarchy = mapi_folder_gethierarchytable($rootFolder, CONVENIENT_DEPTH | MAPI_DEFERRED_ERRORS);
$rows = mapi_table_queryallrows($hierarchy, $props);
// Append the Inbox folder itself.
if ($folderType === 'inbox') {
array_push($rows, mapi_getprops($rootFolder, $props));
}
$folderStatCache = [];
foreach ($rows as $folder) {
$entryid = bin2hex((string) $folder[PR_ENTRYID]);
$folderStatCache[$entryid] = [
'commit_time' => $folder[PR_LOCAL_COMMIT_TIME_MAX] ?? "0000000000",
'entryid' => $entryid,
'store_entryid' => bin2hex((string) $folder[PR_STORE_ENTRYID]),
'content_count' => $folder[PR_CONTENT_COUNT] ?? -1,
'content_unread' => $folder[PR_CONTENT_UNREAD] ?? -1,
'display_name' => $folder[PR_DISPLAY_NAME],
// Folders without the property are mail folders, as in Operations::setFolder.
'container_class' => $folder[PR_CONTAINER_CLASS] ?? 'IPF.Note',
];
}
return $folderStatCache;
}
/**
* Returns the list of plugins which are disabled for the current user, the
* configured ones together with the ones the admin API reports.
*
* @return string semicolon separated list of plugin names
*/
function getDisabledPluginsList() {
$adminApiPlugins = getAdminApiDisabledPlugins();
return $adminApiPlugins === '' ? DISABLED_PLUGINS_LIST : DISABLED_PLUGINS_LIST . ';' . $adminApiPlugins;
}
/**
* Asks the admin API which plugins are disabled for the current user.
*
* Every request of a session would otherwise repeat this HTTP request, so the
* answer is cached for the session. A failed lookup is cached as well, for a
* shorter time, so an unreachable endpoint is not contacted over and over.
*
* @return string semicolon separated list of plugin names, empty when the
* admin API named none or could not be asked
*/
function getAdminApiDisabledPlugins() {
return adminApiCached('adminapi', 'queryAdminApiDisabledPlugins') ?? '';
}
/**
* Answers a session-cached admin API query; a failed lookup (null) is kept
* for the shorter retry time.
*
* @param string $key cache key in the session's state file
* @param callable $query returns the value, or null when the endpoint could not be read
*
* @return mixed
*/
function adminApiCached($key, callable $query) {
// Without a session the cache would be shared between users.
if (session_id() === '') {
return $query();
}
$readState = new State('disabledplugins');
$readState->open();
$cache = $readState->read($key);
$readState->close();
if (is_array($cache) && array_key_exists('value', $cache) && isset($cache['expires']) && $cache['expires'] > time()) {
return $cache['value'];
}
// Deliberately not while holding the state lock, so a slow endpoint does
// not hold up the other requests of this session.
$value = $query();
$lifetime = $value === null ? ADMIN_API_DISABLEDPLUGINS_RETRY_TIME : ADMIN_API_DISABLEDPLUGINS_CACHE_TIME;
$writeState = new State('disabledplugins');
$writeState->open();
$writeState->write($key, ['value' => $value, 'expires' => time() + $lifetime]);
$writeState->close();
return $value;
}
/**
* Performs the actual admin API request for the current user.
*
* The endpoint resolves the domain out of the address, so it needs the SMTP
* address rather than the name the user logged on with.
*
* @return null|string semicolon separated list of plugin names, or null when
* the endpoint could not be read
*/
function queryAdminApiDisabledPlugins() {
$user = $GLOBALS["mapisession"]->getSMTPAddress() ?: $GLOBALS["mapisession"]->getEmailAddress();
if (!$user) {
return null;
}
$res = @json_decode((string) adminApiGet(ADMIN_API_DISABLEDPLUGINS_ENDPOINT . urlencode((string) $user)), true);
return isset($res['data']) ? implode(';', $res['data']) : null;
}
/**
* Whether the admin API reports LDAP users.
*
* @return bool
*/
function getAdminApiUsersInLdap() {
return (bool) adminApiCached('status', 'queryAdminApiUsersInLdap');
}
/**
* @return null|bool null when the endpoint could not be read
*/
function queryAdminApiUsersInLdap() {
$result = @json_decode((string) adminApiGet(ADMIN_API_STATUS_ENDPOINT), true);
return is_array($result) ? !empty($result['ldap']) : null;
}
/**
* Reads from the admin API with a bounded wait, so an unresponsive endpoint
* cannot stall the page.
*
* @param string $url
*
* @return false|string
*/
function adminApiGet($url) {
$context = stream_context_create(['http' => ['timeout' => ADMIN_API_TIMEOUT]]);
return @file_get_contents($url, false, $context);
}
/**
* Helper function which provide protocol used by current request.
*
* @return string it can be either https or http
*/
function getRequestProtocol() {
if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
return $_SERVER['HTTP_X_FORWARDED_PROTO'];
}
return !empty($_SERVER['HTTPS']) ? "https" : "http";
}
/**
* Helper function which defines that webapp has to use secure cookies
* or not. by default webapp always use secure cookies whether or not
* 'SECURE_COOKIES' defined. webapp only use insecure cookies
* where a user has explicitly set 'SECURE_COOKIES' to false.
*
* @return bool return false only when a user has explicitly set
* 'SECURE_COOKIES' to false else returns true
*/
function useSecureCookies() {
return !defined('SECURE_COOKIES') || SECURE_COOKIES !== false;
}
/**
* Check if the eml stream is corrupted or not.
*
* @param string $attachment content fetched from PR_ATTACH_DATA_BIN property of an attachment
*
* @return bool true if the EML is broken, false otherwise
*/
function isBrokenEml($attachment) {
// Get header part to process further
$splittedContent = preg_split("/\r?\n\r?\n/", $attachment);
// Fetch raw header
if (preg_match_all('/([^\n^:]+:)/', $splittedContent[0], $matches)) {
$rawHeaders = $matches[1];
}
// Compare if necessary headers are present or not
if (isset($rawHeaders) && in_array('From:', $rawHeaders) && in_array('Date:', $rawHeaders)) {
return false;
}
return true;
}
/**
* Whether an attachment carries a whole mail as a file rather than as an
* embedded message, i.e. an .eml.
*
* @param array $attachProps properties of the attachment
*/
function isEmlAttachment($attachProps) {
if (strcasecmp(trim((string) ($attachProps[PR_ATTACH_MIME_TAG] ?? '')), 'message/rfc822') === 0) {
return true;
}
$name = (string) ($attachProps[PR_ATTACH_LONG_FILENAME] ?? $attachProps[PR_ATTACH_FILENAME] ?? '');
return strcasecmp((string) pathinfo($name, PATHINFO_EXTENSION), 'eml') === 0;
}
/**
* Opens the message an attachment holds: the embedded message of a
* message-in-message attachment, or an .eml file turned into one.
*
* The converted message is created but never saved, so it exists for this
* request only and leaves nothing behind in the mailbox.
*
* @param resource $attachment the attachment to open
*
* @return false|resource the message, or false when the attachment holds none
*/
function openAttachedMessage($attachment) {
$props = mapi_attach_getprops($attachment, [PR_ATTACH_METHOD, PR_ATTACH_MIME_TAG,
PR_ATTACH_LONG_FILENAME, PR_ATTACH_FILENAME, ]);
if (($props[PR_ATTACH_METHOD] ?? ATTACH_BY_VALUE) == ATTACH_EMBEDDED_MSG) {
return mapi_attach_openobj($attachment);
}
if (!isEmlAttachment($props)) {
return false;
}
try {
$eml = readMapiPropStream($attachment, PR_ATTACH_DATA_BIN);
}
catch (MAPIException $e) {
$e->setHandled();
return false;
}
return convertEmlToMessage($eml);
}
/**
* Turns an RFC822 mail into a MAPI message living in the user's own store.
*
* The message is deliberately not saved: callers read it and drop it, which
* keeps a preview from filing a copy of the mail in a folder.
*
* @param string $eml the raw mail
*
* @return false|resource the message, or false when it cannot be converted
*/
function convertEmlToMessage($eml) {
if (empty($eml) || isBrokenEml($eml)) {
return false;
}
$store = $GLOBALS['mapisession']->getDefaultMessageStore();
try {
// The drafts entryid sits on the root container, not on the store.
$root = mapi_msgstore_openentry($store);
$rootProps = mapi_getprops($root, [PR_IPM_DRAFTS_ENTRYID]);
if (empty($rootProps[PR_IPM_DRAFTS_ENTRYID])) {
return false;
}
$folder = mapi_msgstore_openentry($store, $rootProps[PR_IPM_DRAFTS_ENTRYID]);
$message = mapi_folder_createmessage($folder);
$ok = mapi_inetmapi_imtomapi($GLOBALS['mapisession']->getSession(), $store,
$GLOBALS['mapisession']->getAddressbook(), $message, $eml, []);
}
catch (MAPIException $e) {
$e->setHandled();
return false;
}
return $ok === true ? $message : false;
}
/**
* Helper function which return the webapp version.
*
* @return string webapp version
*/
function getWebappVersion() {
static $version = null;
if ($version === null) {
$version = trim((string) @file_get_contents(BASE_PATH . 'version'));
}
return $version;
}
/**
* Runs $fn with the PHP session open. Authentication closes the session before
* the MAPI logon, so later writes to $_SESSION are lost without this.
*/
function updateSession(callable $fn) {
$wasActive = session_status() === PHP_SESSION_ACTIVE;
if (!$wasActive) {
session_start();
}
$fn();
if (!$wasActive) {
session_write_close();
}
}
/**
* Append the grommunio Web version to an asset URL, so that an upgrade
* is never served from the browser cache.
*
* @param string $url relative URL of the asset, may already carry a query string
*
* @return string
*/
function versionedUrl($url) {
return $url . (str_contains($url, '?') ? '&' : '?') . 'version=' . getWebappVersion();
}
/**
* function which remove double quotes or PREF from vcf stream
* if it has.
*
* @param string $attachmentStream The attachment stream
*/
function processVCFStream(&$attachmentStream) {
/*
* https://github.com/libical/libical/issues/488
* https://github.com/libical/libical/issues/490
*
* Because of above issues we need to remove
* double quotes or PREF from vcf stream if
* it exists in vcf stream.
*/
if (preg_match('/"/', $attachmentStream) > 0) {
$attachmentStream = str_replace('"', '', $attachmentStream);
}
if (preg_match('/EMAIL;PREF=/', $attachmentStream) > 0) {
$rows = explode("\n", $attachmentStream);
foreach ($rows as $key => $row) {
if (preg_match("/EMAIL;PREF=/", $row)) {
unset($rows[$key]);
}
}
$attachmentStream = join("\n", $rows);
}
}
/**
* Calculates the local startime for a timezone from a timestamp.
*
* @param int $ts
* @param string $tz
*
* @return int
*/
function getLocalStart($ts, $tz) {
$calItemStart = new DateTime();
$calItemStart->setTimestamp($ts);
$clientDate = DateTime::createFromInterface($calItemStart);
$clientDate->setTimezone(new DateTimeZone($tz));
// It's only necessary to calculate new start and end times
// if the appointment does not start at midnight
if ((int) $clientDate->format("His") != 0) {
$clientMidnight = DateTimeImmutable::createFromFormat(
"Y-m-d H:i:s",
$clientDate->format("Y-m-d ") . "00:00:00",
$clientDate->getTimezone()
);
$interval = $clientDate->getTimestamp() - $clientMidnight->getTimestamp();
// The code here is based on assumption that if the interval
// is greater than 12 hours, the appointment takes place
// on the day before or after. This should be fine for all the
// timezones which do not exceed 12 hour difference to UTC.
$ts = $interval > 0 ?
$ts - ($interval < 43200 ? $interval : $interval - 86400) :
$ts + ($interval > -43200 ? $interval : $interval - 86400);
}
return $ts;
}
/**
* Check whether a message class matches a prefix at a component boundary.
*
* @param mixed $h PR_MESSAGE_CLASS value
* @param mixed $n prefix to test for
*/
function class_match_prefix($h, $n) {
if (!isset($h)) {
return false;
}
$z = strlen($n);
$r = strncasecmp($h, $n, $z);
if ($r != 0) {
return false;
}
return strlen($h) == $z || $h[$z] == '.' ? true : false;
}