405 lines
14 KiB
PHP
405 lines
14 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
|
|
*/
|
|
|
|
/**
|
|
* RestoreItemsList Module.
|
|
*/
|
|
class RestoreItemsListModule extends ListModule {
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @param int $id unique id
|
|
* @param array $data list of all actions
|
|
*/
|
|
public function __construct($id, $data) {
|
|
parent::__construct($id, $data);
|
|
|
|
$this->properties = $GLOBALS['properties']->getRestoreItemListProperties();
|
|
}
|
|
|
|
/**
|
|
* Executes all the actions in the $data variable.
|
|
*/
|
|
#[Override]
|
|
public function execute() {
|
|
foreach ($this->data as $actionType => $action) {
|
|
if (isset($actionType)) {
|
|
try {
|
|
if (!is_array($action)) {
|
|
throw new MAPIException(_("Could not process request data properly."), MAPI_E_INVALID_PARAMETER);
|
|
}
|
|
$store = $this->getActionStore($action);
|
|
$parententryid = $this->getActionParentEntryID($action);
|
|
$folderentryid = $this->getActionSingleEntryID($action);
|
|
if ($store === false || is_array($store)) {
|
|
throw new MAPIException(_("Could not process request data properly."), MAPI_E_INVALID_PARAMETER);
|
|
}
|
|
|
|
switch ($actionType) {
|
|
case "list":
|
|
case "updatelist":
|
|
if (!is_string($folderentryid)) {
|
|
throw new MAPIException(_("Could not process request data properly."), MAPI_E_INVALID_PARAMETER);
|
|
}
|
|
if (isset($action["message_action"])) {
|
|
if (!is_string($action["message_action"]["action_type"] ?? null)) {
|
|
throw new MAPIException(_("Could not process request data properly."), MAPI_E_INVALID_PARAMETER);
|
|
}
|
|
$subActionType = $action["message_action"]["action_type"];
|
|
|
|
switch ($subActionType) {
|
|
case "restoreAll":
|
|
$this->restoreAll($store, $folderentryid, $action);
|
|
break;
|
|
|
|
case "deleteAll":
|
|
$this->deleteAll($store, $folderentryid, $action);
|
|
break;
|
|
}
|
|
}
|
|
else {
|
|
$this->itemList($store, $folderentryid, $action);
|
|
}
|
|
break;
|
|
|
|
case "delete":
|
|
if ($parententryid === false || !is_string($action["message_action"]["action_type"] ?? null)) {
|
|
throw new MAPIException(_("Could not process request data properly."), MAPI_E_INVALID_PARAMETER);
|
|
}
|
|
$itemType = $action["message_action"]["action_type"];
|
|
|
|
switch ($itemType) {
|
|
case "restorefolder":
|
|
if (!is_string($folderentryid)) {
|
|
throw new MAPIException(_("Could not process request data properly."), MAPI_E_INVALID_PARAMETER);
|
|
}
|
|
$this->restoreFolder($store, $parententryid, $folderentryid);
|
|
break;
|
|
|
|
case "deletefolder":
|
|
$this->deleteFolder($store, $parententryid, $action);
|
|
break;
|
|
|
|
case "restoremessage":
|
|
$this->restoreItems($store, $parententryid, $action);
|
|
break;
|
|
|
|
case "deletemessage":
|
|
$this->deleteItems($store, $parententryid, $action);
|
|
break;
|
|
|
|
default:
|
|
$this->handleUnknownActionType($itemType);
|
|
}
|
|
break;
|
|
|
|
default:
|
|
$this->handleUnknownActionType($actionType);
|
|
}
|
|
}
|
|
catch (MAPIException $e) {
|
|
$this->processException($e, $actionType);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Function which permanently delete all folder or message items.
|
|
*
|
|
* @param resource $store MAPI message store
|
|
* @param string $folderentryid entry ID of the target folder
|
|
* @param array $action action data sent by the client
|
|
*/
|
|
public function deleteAll($store, $folderentryid, $action) {
|
|
$folder = mapi_msgstore_openentry($store, $folderentryid);
|
|
|
|
// delete all folders.
|
|
if (isset($action["itemType"]) && $action["itemType"] == "folder") {
|
|
$table = mapi_folder_gethierarchytable($folder, MAPI_DEFERRED_ERRORS | SHOW_SOFT_DELETES);
|
|
$restoreItems = array_column(mapi_table_queryallrows($table, [PR_ENTRYID]), PR_ENTRYID);
|
|
foreach ($restoreItems as $restoreItem) {
|
|
mapi_folder_deletefolder($folder, $restoreItem, DEL_FOLDERS | DEL_MESSAGES | DELETE_HARD_DELETE);
|
|
}
|
|
}
|
|
else {
|
|
// delete all messages
|
|
$table = mapi_folder_getcontentstable($folder, MAPI_DEFERRED_ERRORS | SHOW_SOFT_DELETES);
|
|
$restoreItems = array_column(mapi_table_queryallrows($table, [PR_ENTRYID]), PR_ENTRYID);
|
|
mapi_folder_deletemessages($folder, $restoreItems, DELETE_HARD_DELETE);
|
|
}
|
|
|
|
$this->addActionData("list", ["item" => []]);
|
|
$GLOBALS["bus"]->addData($this->getResponseData());
|
|
}
|
|
|
|
/**
|
|
* Function used to restore all folders.
|
|
*
|
|
* @param resource $store MAPI message store
|
|
* @param resource $folder folder whose contents need to be restored
|
|
*
|
|
* @throws MAPIException
|
|
*/
|
|
public function restoreAllFolders($store, $folder) {
|
|
$table = mapi_folder_gethierarchytable($folder, MAPI_DEFERRED_ERRORS | SHOW_SOFT_DELETES);
|
|
$rows = mapi_table_queryallrows($table, [PR_ENTRYID, PR_DISPLAY_NAME]);
|
|
|
|
foreach ($rows as $row) {
|
|
$restoreItem = $row[PR_ENTRYID];
|
|
$name = $row[PR_DISPLAY_NAME] ?? '';
|
|
|
|
// a move within the parent only renames, so copy and hard-delete the source
|
|
try {
|
|
mapi_folder_copyfolder($folder, $restoreItem, $folder, $name);
|
|
}
|
|
catch (MAPIException $e) {
|
|
if ($e->getCode() != MAPI_E_COLLISION) {
|
|
throw $e;
|
|
}
|
|
$name = $GLOBALS["operations"]->checkFolderNameConflict($store, $folder, $name);
|
|
mapi_folder_copyfolder($folder, $restoreItem, $folder, $name);
|
|
}
|
|
mapi_folder_deletefolder($folder, $restoreItem, DEL_MESSAGES | DEL_FOLDERS | DELETE_HARD_DELETE);
|
|
}
|
|
|
|
// Notify the parent folder.
|
|
$this->notifyParentFolder($store, $folder, $folder);
|
|
}
|
|
|
|
/**
|
|
* Restore every message in the given folder.
|
|
*
|
|
* @param resource $folder folder whose contents need to be restored
|
|
*/
|
|
public function restoreAllItems($folder) {
|
|
$table = mapi_folder_getcontentstable($folder, MAPI_DEFERRED_ERRORS | SHOW_SOFT_DELETES);
|
|
$restoreItems = array_column(mapi_table_queryallrows($table, [PR_ENTRYID]), PR_ENTRYID);
|
|
|
|
mapi_folder_copymessages($folder, $restoreItems, $folder, MESSAGE_MOVE);
|
|
|
|
// as after moving the message/s the entryid gets changed, so need to notify about the folder
|
|
// so that we can update the folder on parent page.
|
|
$folderProps = mapi_getprops($folder, [PR_STORE_ENTRYID, PR_ENTRYID]);
|
|
|
|
/* when we restore any message from soft deleted system then we are actually copying the item, so at that time entryid of
|
|
* that particular item gets changed, so to notify client about changes we need to
|
|
* notify parent folder where we have restored the message
|
|
*/
|
|
$props = [];
|
|
$props[PR_PARENT_ENTRYID] = $folderProps[PR_ENTRYID];
|
|
$props[PR_STORE_ENTRYID] = $folderProps[PR_STORE_ENTRYID];
|
|
|
|
$GLOBALS["bus"]->notify(bin2hex((string) $folderProps[PR_ENTRYID]), TABLE_SAVE, $props);
|
|
}
|
|
|
|
/**
|
|
* Restore all folders or messages according to the requested item type.
|
|
*
|
|
* @param resource $store MAPI message store
|
|
* @param string $folderentryid entry ID of the target folder
|
|
* @param array $action action data sent by the client
|
|
*
|
|
* @throws MAPIException
|
|
*/
|
|
public function restoreAll($store, $folderentryid, $action) {
|
|
$folder = mapi_msgstore_openentry($store, $folderentryid);
|
|
|
|
if (isset($action["itemType"]) && $action["itemType"] == "folder") {
|
|
$this->restoreAllFolders($store, $folder);
|
|
}
|
|
else {
|
|
$this->restoreAllItems($folder);
|
|
}
|
|
|
|
$this->addActionData("list", ["item" => []]);
|
|
$GLOBALS["bus"]->addData($this->getResponseData());
|
|
}
|
|
|
|
/**
|
|
* Function to retrieve the list of messages or folder of particular folder.
|
|
*
|
|
* @param resource $store MAPI message store
|
|
* @param string $entryid entry ID of the folder
|
|
* @param array $action request data
|
|
*/
|
|
public function itemList($store, $entryid, $action) {
|
|
// Restriction
|
|
$this->parseRestriction($action);
|
|
|
|
// set the this->$sort variable.
|
|
$this->parseSortOrder($action);
|
|
|
|
$limit = $action['restriction']['limit'] ?? $GLOBALS['settings']->get('grommunio/v1/main/page_size', 50);
|
|
|
|
$getHierarchy = isset($action["itemType"]) && $action["itemType"] == "folder";
|
|
|
|
$data = $GLOBALS["operations"]->getTable(
|
|
$store,
|
|
$entryid,
|
|
$this->properties,
|
|
$this->sort,
|
|
$this->start,
|
|
$limit,
|
|
$this->restriction,
|
|
$getHierarchy,
|
|
MAPI_DEFERRED_ERRORS | SHOW_SOFT_DELETES
|
|
);
|
|
|
|
$this->addActionData("list", $data);
|
|
$GLOBALS["bus"]->addData($this->getResponseData());
|
|
}
|
|
|
|
/**
|
|
* Function to delete selected items of particular folder.
|
|
*
|
|
* @param resource $store MAPI message store
|
|
* @param string $parententryid entry ID of the folder containing the item to delete
|
|
* @param array $items items to delete
|
|
*/
|
|
public function deleteItems($store, $parententryid, $items) {
|
|
if (is_assoc_array($items)) {
|
|
// wrap single item in an array
|
|
$items = [$items];
|
|
}
|
|
|
|
$sfolder = mapi_msgstore_openentry($store, $parententryid);
|
|
|
|
for ($index = 0, $len = count($items); $index < $len; ++$index) {
|
|
mapi_folder_deletemessages($sfolder, [hex2bin((string) $items[$index]['entryid'])], DELETE_HARD_DELETE);
|
|
}
|
|
$this->sendFeedback(true);
|
|
}
|
|
|
|
/**
|
|
* Function to restore message present into the selected folder.
|
|
*
|
|
* @param resource $store MAPI message store
|
|
* @param string $parententryid entry ID of the folder containing the item to restore
|
|
* @param array $items items to restore
|
|
*/
|
|
public function restoreItems($store, $parententryid, $items) {
|
|
if (is_assoc_array($items)) {
|
|
// wrap single item in an array
|
|
$items = [$items];
|
|
}
|
|
|
|
$sfolder = mapi_msgstore_openentry($store, $parententryid);
|
|
|
|
for ($index = 0, $len = count($items); $index < $len; ++$index) {
|
|
mapi_folder_copymessages($sfolder, [hex2bin((string) $items[$index]['entryid'])], $sfolder, MESSAGE_MOVE);
|
|
}
|
|
|
|
// as after moving the message/s the entryid gets changed, so need to notify about the folder
|
|
// so that we can update the folder on parent page.
|
|
$folderProps = mapi_getprops($sfolder, [PR_STORE_ENTRYID, PR_ENTRYID]);
|
|
|
|
/* when we restore any message from soft deleted system then we are actually copying the item, so at that time entryid of
|
|
* that particular item gets changed, so to notify client about changes we need to
|
|
* notify parent folder where we have restored the message
|
|
*/
|
|
$props = [];
|
|
$props[PR_PARENT_ENTRYID] = $folderProps[PR_ENTRYID];
|
|
$props[PR_STORE_ENTRYID] = $folderProps[PR_STORE_ENTRYID];
|
|
$GLOBALS["bus"]->notify(bin2hex((string) $folderProps[PR_ENTRYID]), TABLE_SAVE, $props);
|
|
$this->sendFeedback(true);
|
|
}
|
|
|
|
/**
|
|
* Function to delete selected folders.
|
|
*
|
|
* @param resource $store MAPI message store
|
|
* @param string $parententryid entry ID of the parent folder
|
|
* @param array $folders folders to delete
|
|
*/
|
|
public function deleteFolder($store, $parententryid, $folders) {
|
|
if (is_assoc_array($folders)) {
|
|
// wrap single folder in an array
|
|
$folders = [$folders];
|
|
}
|
|
|
|
$sfolder = mapi_msgstore_openentry($store, $parententryid);
|
|
|
|
for ($index = 0, $len = count($folders); $index < $len; ++$index) {
|
|
mapi_folder_deletefolder($sfolder, hex2bin((string) $folders[$index]['entryid']), DEL_FOLDERS | DEL_MESSAGES | DELETE_HARD_DELETE);
|
|
}
|
|
|
|
$this->sendFeedback(true);
|
|
}
|
|
|
|
/**
|
|
* Function to restore soft deleted folder of particular folder. The folder
|
|
* restore action involves two operations: copy the folder marked as deleted
|
|
* as a new one and then delete the source folder.
|
|
*
|
|
* @param resource $store MAPI message store
|
|
* @param string $parententryid entry ID of the parent folder
|
|
* @param string $folderentryid entry ID of the folder to restore
|
|
*/
|
|
public function restoreFolder($store, $parententryid, $folderentryid) {
|
|
$sfolder = mapi_msgstore_openentry($store, $parententryid);
|
|
$folder = mapi_msgstore_openentry($store, $folderentryid, SHOW_SOFT_DELETES);
|
|
$folderNameProps = mapi_getprops($folder, [PR_DISPLAY_NAME]);
|
|
|
|
try {
|
|
/*
|
|
* We should first try to copy folder and if it returns MAPI_E_COLLISION then
|
|
* only we should check for the conflicting folder names and generate a new name
|
|
* and restore folder with the generated name.
|
|
*/
|
|
mapi_folder_copyfolder($sfolder, $folderentryid, $sfolder, $folderNameProps[PR_DISPLAY_NAME]);
|
|
}
|
|
catch (MAPIException $e) {
|
|
if ($e->getCode() == MAPI_E_COLLISION) {
|
|
$foldername = $GLOBALS["operations"]->checkFolderNameConflict($store, $sfolder, $folderNameProps[PR_DISPLAY_NAME]);
|
|
mapi_folder_copyfolder($sfolder, $folderentryid, $sfolder, $foldername);
|
|
}
|
|
else {
|
|
// all other errors should be propagated to higher level exception handlers
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
// Reaching this point means the folder was copied successfully.
|
|
mapi_folder_deletefolder($sfolder, $folderentryid, DEL_MESSAGES | DEL_FOLDERS | DELETE_HARD_DELETE);
|
|
|
|
// notify the parent folder
|
|
$parentFolder = mapi_msgstore_openentry($store, $parententryid);
|
|
$this->notifyParentFolder($store, $sfolder, $parentFolder);
|
|
$this->sendFeedback(true);
|
|
}
|
|
|
|
/**
|
|
* Notify the parent folder about restoration.
|
|
*
|
|
* @param resource $store MAPI message store
|
|
* @param resource $folder folder containing the restored folder
|
|
* @param resource $parentFolder folder to notify
|
|
*/
|
|
public function notifyParentFolder($store, $folder, $parentFolder) {
|
|
/* when we restore any folder from soft deleted system then we are actually copying the folder, so at that time entryid of
|
|
* folder gets changed and we don't have notification system to get new entryid, so to notify client about changes we need to
|
|
* notify all subfolders of parent folder where we have restored the folder
|
|
*/
|
|
$hierarchyTable = mapi_folder_gethierarchytable($folder, CONVENIENT_DEPTH | MAPI_DEFERRED_ERRORS);
|
|
mapi_table_sort($hierarchyTable, [PR_DISPLAY_NAME => TABLE_SORT_ASCEND], TBL_BATCH);
|
|
|
|
$subfolders = mapi_table_queryallrows($hierarchyTable, [PR_ENTRYID]);
|
|
|
|
foreach ($subfolders as $subfolder) {
|
|
$folderObject = mapi_msgstore_openentry($store, $subfolder[PR_ENTRYID]);
|
|
$folderProps = mapi_getprops($folderObject, [PR_ENTRYID, PR_STORE_ENTRYID]);
|
|
$GLOBALS["bus"]->notify(bin2hex((string) $subfolder[PR_ENTRYID]), OBJECT_SAVE, $folderProps);
|
|
}
|
|
|
|
$folderProps = mapi_getprops($parentFolder, [PR_ENTRYID, PR_STORE_ENTRYID]);
|
|
$GLOBALS["bus"]->notify(bin2hex((string) $folderProps[PR_ENTRYID]), OBJECT_SAVE, $folderProps);
|
|
}
|
|
}
|