diff options
author | Sergey Linnik <sergey.linnik@onlyoffice.com> | 2020-09-17 10:58:02 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-17 10:58:02 +0300 |
commit | a4dcd81d4e5da4a7db95feff32f043b4f096ae76 (patch) | |
tree | cea95abfafef859bfaa2b042694270d02c4f4414 /lib/keymanager.php | |
parent | dc5082828f943fb0b6cb274447680dbc07451eec (diff) | |
parent | 44d8e51712777ffd15a064650d0bc8c6b5bbbbcd (diff) |
Release/6.0.0v6.0.0
Release/6.0.0
Diffstat (limited to 'lib/keymanager.php')
-rw-r--r-- | lib/keymanager.php | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/lib/keymanager.php b/lib/keymanager.php new file mode 100644 index 0000000..3571696 --- /dev/null +++ b/lib/keymanager.php @@ -0,0 +1,150 @@ +<?php +/** + * + * (c) Copyright Ascensio System SIA 2020 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +namespace OCA\Onlyoffice; + + +/** + * Key manager + * + * @package OCA\Onlyoffice + */ +class KeyManager { + + /** + * Table name + */ + private const TableName_Key = "onlyoffice_filekey"; + + /** + * Get document identifier + * + * @param integer $fileId - file identifier + * + * @return string + */ + public static function get($fileId) { + $connection = \OC::$server->getDatabaseConnection(); + $select = $connection->prepare(" + SELECT `key` + FROM `*PREFIX*" . self::TableName_Key . "` + WHERE `file_id` = ? + "); + $result = $select->execute([$fileId]); + + $keys = $result ? $select->fetch() : []; + $key = is_array($keys) && isset($keys["key"]) ? $keys["key"] : ""; + + return $key; + } + + /** + * Store document identifier + * + * @param integer $fileId - file identifier + * @param integer $key - file key + * + * @return bool + */ + public static function set($fileId, $key) { + $connection = \OC::$server->getDatabaseConnection(); + $insert = $connection->prepare(" + INSERT INTO `*PREFIX*" . self::TableName_Key . "` + (`file_id`, `key`) + VALUES (?, ?) + "); + return (bool)$insert->execute([$fileId, $key]); + } + + /** + * Delete document identifier + * + * @param integer $fileId - file identifier + * @param bool $unlock - delete even with lock label + * + * @return bool + */ + public static function delete($fileId, $unlock = false) { + $connection = \OC::$server->getDatabaseConnection(); + $delete = $connection->prepare(" + DELETE FROM `*PREFIX*" . self::TableName_Key . "` + WHERE `file_id` = ? + " . ($unlock === false ? "AND `lock` != 1" : "") + ); + return (bool)$delete->execute([$fileId]); + } + + /** + * Change lock status + * + * @param integer $fileId - file identifier + * @param integer $lock - status + * + * @return bool + */ + public static function lock($fileId, $lock = true) { + $connection = \OC::$server->getDatabaseConnection(); + $update = $connection->prepare(" + UPDATE `*PREFIX*" . self::TableName_Key . "` + SET `lock` = ? + WHERE `file_id` = ? + "); + return (bool)$update->execute([$lock === true ? 1 : 0, $fileId]); + } + + /** + * Change forcesave status + * + * @param integer $fileId - file identifier + * @param integer $fs - status + * + * @return bool + */ + public static function setForcesave($fileId, $fs = true) { + $connection = \OC::$server->getDatabaseConnection(); + $update = $connection->prepare(" + UPDATE `*PREFIX*" . self::TableName_Key . "` + SET `fs` = ? + WHERE `file_id` = ? + "); + return (bool)$update->execute([$fs === true ? 1 : 0, $fileId]); + } + + /** + * Get forcesave status + * + * @param integer $fileId - file identifier + * + * @return bool + */ + public static function wasForcesave($fileId) { + $connection = \OC::$server->getDatabaseConnection(); + $select = $connection->prepare(" + SELECT `fs` + FROM `*PREFIX*" . self::TableName_Key . "` + WHERE `file_id` = ? + "); + $result = $select->execute([$fileId]); + + $rows = $result ? $select->fetch() : []; + $fs = is_array($rows) && isset($rows["fs"]) ? $rows["fs"] : ""; + + return $fs === "1"; + } +} |