2022-08-20 02:53:21 +00:00
|
|
|
<?php
|
2022-10-19 17:10:36 +00:00
|
|
|
|
2022-08-20 02:53:21 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\Memories;
|
|
|
|
|
2023-03-14 20:20:24 +00:00
|
|
|
use OC\Files\Search\SearchBinaryOperator;
|
|
|
|
use OC\Files\Search\SearchComparison;
|
|
|
|
use OC\Files\Search\SearchQuery;
|
2023-04-16 23:03:59 +00:00
|
|
|
use OCA\Memories\AppInfo\Application;
|
2023-10-22 19:58:33 +00:00
|
|
|
use OCA\Memories\Settings\SystemConfig;
|
2022-12-08 21:00:53 +00:00
|
|
|
use OCP\App\IAppManager;
|
2023-03-10 00:39:26 +00:00
|
|
|
use OCP\Files\Node;
|
2023-03-14 20:20:24 +00:00
|
|
|
use OCP\Files\Search\ISearchBinaryOperator;
|
|
|
|
use OCP\Files\Search\ISearchComparison;
|
2022-12-08 21:00:53 +00:00
|
|
|
use OCP\IConfig;
|
|
|
|
|
2022-10-19 17:10:36 +00:00
|
|
|
class Util
|
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
use UtilController;
|
|
|
|
|
2023-10-20 01:47:58 +00:00
|
|
|
public const ARCHIVE_FOLDER = '.archive';
|
2022-09-25 23:02:26 +00:00
|
|
|
|
2022-11-09 09:23:12 +00:00
|
|
|
/**
|
|
|
|
* Get host CPU architecture (amd64 or aarch64).
|
2023-10-14 08:25:50 +00:00
|
|
|
*
|
|
|
|
* @psalm-return 'aarch64'|'amd64'|null
|
2022-11-09 09:23:12 +00:00
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
public static function getArch(): ?string
|
2022-11-09 09:23:12 +00:00
|
|
|
{
|
2023-10-15 19:46:35 +00:00
|
|
|
$uname = strtolower(php_uname('m') ?: 'unknown');
|
|
|
|
if (str_contains($uname, 'aarch64') || str_contains($uname, 'arm64')) {
|
2022-11-09 09:23:12 +00:00
|
|
|
return 'aarch64';
|
|
|
|
}
|
2023-10-15 19:46:35 +00:00
|
|
|
if (str_contains($uname, 'x86_64') || str_contains($uname, 'amd64')) {
|
2022-11-09 09:23:12 +00:00
|
|
|
return 'amd64';
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the libc type for host (glibc or musl).
|
2023-10-14 08:25:50 +00:00
|
|
|
*
|
|
|
|
* @psalm-return 'glibc'|'musl'|null
|
2022-11-09 09:23:12 +00:00
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
public static function getLibc(): ?string
|
2022-11-09 09:23:12 +00:00
|
|
|
{
|
2023-10-14 23:06:25 +00:00
|
|
|
/** @psalm-suppress ForbiddenCode */
|
2023-10-15 19:46:35 +00:00
|
|
|
$ldd = strtolower(shell_exec('ldd --version 2>&1') ?: 'unknown');
|
|
|
|
if (str_contains($ldd, 'musl')) {
|
|
|
|
return 'musl';
|
|
|
|
}
|
|
|
|
if (str_contains($ldd, 'glibc')) {
|
|
|
|
return 'glibc';
|
2022-11-09 09:23:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-10-27 20:45:03 +00:00
|
|
|
/**
|
|
|
|
* Check if albums are enabled for this user.
|
|
|
|
*/
|
2023-03-23 20:49:26 +00:00
|
|
|
public static function albumsIsEnabled(): bool
|
2022-10-27 20:45:03 +00:00
|
|
|
{
|
2023-03-23 20:49:26 +00:00
|
|
|
$appManager = \OC::$server->get(IAppManager::class);
|
|
|
|
|
2022-10-27 20:45:03 +00:00
|
|
|
if (!$appManager->isEnabledForUser('photos')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-29 16:57:03 +00:00
|
|
|
$v = $appManager->getAppVersion('photos');
|
2022-10-27 20:45:03 +00:00
|
|
|
|
|
|
|
return version_compare($v, '1.7.0', '>=');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if tags is enabled for this user.
|
|
|
|
*/
|
2023-03-23 20:49:26 +00:00
|
|
|
public static function tagsIsEnabled(): bool
|
2022-10-27 20:45:03 +00:00
|
|
|
{
|
2023-10-15 19:46:35 +00:00
|
|
|
return \OC::$server->get(IAppManager::class)->isEnabledForUser('systemtags');
|
2022-10-27 20:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if recognize is enabled for this user.
|
|
|
|
*/
|
2023-03-23 20:49:26 +00:00
|
|
|
public static function recognizeIsEnabled(): bool
|
2023-08-25 04:14:41 +00:00
|
|
|
{
|
|
|
|
if (!self::recognizeIsInstalled()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
$config = \OC::$server->get(IConfig::class);
|
|
|
|
if ('true' !== $config->getAppValue('recognize', 'faces.enabled', 'false')) {
|
2023-08-25 04:14:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if recognize is installed.
|
|
|
|
*/
|
|
|
|
public static function recognizeIsInstalled(): bool
|
2022-10-27 20:45:03 +00:00
|
|
|
{
|
2023-03-23 20:49:26 +00:00
|
|
|
$appManager = \OC::$server->get(IAppManager::class);
|
|
|
|
|
2022-10-27 20:45:03 +00:00
|
|
|
if (!$appManager->isEnabledForUser('recognize')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-29 16:57:03 +00:00
|
|
|
$v = $appManager->getAppVersion('recognize');
|
2023-04-29 05:29:10 +00:00
|
|
|
if (!version_compare($v, '3.8.0', '>=')) {
|
2023-03-18 00:03:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-10-27 20:45:03 +00:00
|
|
|
}
|
2022-11-07 03:36:11 +00:00
|
|
|
|
2022-12-08 21:00:53 +00:00
|
|
|
/**
|
|
|
|
* Check if Face Recognition is enabled by the user.
|
|
|
|
*/
|
2023-03-23 20:49:26 +00:00
|
|
|
public static function facerecognitionIsEnabled(): bool
|
2022-12-08 21:00:53 +00:00
|
|
|
{
|
2023-08-25 04:14:41 +00:00
|
|
|
if (!self::facerecognitionIsInstalled()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-23 20:49:26 +00:00
|
|
|
try {
|
2023-10-15 19:46:35 +00:00
|
|
|
return 'true' === \OC::$server->get(IConfig::class)
|
|
|
|
->getUserValue(self::getUID(), 'facerecognition', 'enabled', 'false')
|
|
|
|
;
|
|
|
|
} catch (\Exception) {
|
|
|
|
// not logged in
|
2023-03-23 20:49:26 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
return false;
|
2022-12-08 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if Face Recognition is installed and enabled for this user.
|
|
|
|
*/
|
2023-03-23 20:49:26 +00:00
|
|
|
public static function facerecognitionIsInstalled(): bool
|
2022-12-08 21:00:53 +00:00
|
|
|
{
|
2023-03-23 20:49:26 +00:00
|
|
|
$appManager = \OC::$server->get(IAppManager::class);
|
|
|
|
|
2022-12-08 21:00:53 +00:00
|
|
|
if (!$appManager->isEnabledForUser('facerecognition')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$v = $appManager->getAppInfo('facerecognition')['version'];
|
|
|
|
|
|
|
|
return version_compare($v, '0.9.10-beta.2', '>=');
|
|
|
|
}
|
|
|
|
|
2023-08-03 17:11:39 +00:00
|
|
|
/**
|
|
|
|
* Check if preview generator is installed.
|
|
|
|
*/
|
|
|
|
public static function previewGeneratorIsEnabled(): bool
|
|
|
|
{
|
2023-10-15 19:46:35 +00:00
|
|
|
return \OC::$server->get(IAppManager::class)->isEnabledForUser('previewgenerator');
|
2023-08-03 17:11:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 03:36:11 +00:00
|
|
|
/**
|
|
|
|
* Check if link sharing is allowed.
|
2023-10-15 01:15:01 +00:00
|
|
|
*
|
|
|
|
* @todo Check if link sharing is enabled to show the button
|
|
|
|
*
|
|
|
|
* @psalm-suppress PossiblyUnusedMethod
|
2022-11-07 03:36:11 +00:00
|
|
|
*/
|
2023-03-23 20:49:26 +00:00
|
|
|
public static function isLinkSharingEnabled(): bool
|
2022-11-07 03:36:11 +00:00
|
|
|
{
|
2023-03-23 20:49:26 +00:00
|
|
|
$config = \OC::$server->get(IConfig::class);
|
|
|
|
|
2022-11-07 03:36:11 +00:00
|
|
|
// Check if the shareAPI is enabled
|
|
|
|
if ('yes' !== $config->getAppValue('core', 'shareapi_enabled', 'yes')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether public sharing is enabled
|
|
|
|
if ('yes' !== $config->getAppValue('core', 'shareapi_allow_links', 'yes')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-11-21 10:13:43 +00:00
|
|
|
|
2023-03-10 00:39:26 +00:00
|
|
|
/**
|
|
|
|
* Force a fileinfo value on a node.
|
|
|
|
* This is a hack to avoid subclassing everything.
|
|
|
|
*
|
2023-10-15 19:46:35 +00:00
|
|
|
* @param Node $node File to patch
|
|
|
|
* @param string $key Key to set
|
|
|
|
* @param mixed $value Value to set
|
2023-03-10 00:39:26 +00:00
|
|
|
*/
|
2023-10-15 19:46:35 +00:00
|
|
|
public static function forceFileInfo(Node &$node, string $key, mixed $value): void
|
2023-03-10 00:39:26 +00:00
|
|
|
{
|
|
|
|
/** @var \OC\Files\Node\Node */
|
|
|
|
$node = $node;
|
2023-10-14 08:25:50 +00:00
|
|
|
|
|
|
|
/** @psalm-suppress UndefinedInterfaceMethod */
|
2023-03-10 00:39:26 +00:00
|
|
|
$node->getFileInfo()[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Force permissions on a node.
|
|
|
|
*
|
2023-10-15 19:46:35 +00:00
|
|
|
* @param Node $node File to patch
|
|
|
|
* @param int $permissions Permissions to set
|
2023-03-10 00:39:26 +00:00
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
public static function forcePermissions(Node &$node, int $permissions): void
|
2023-03-10 00:39:26 +00:00
|
|
|
{
|
|
|
|
self::forceFileInfo($node, 'permissions', $permissions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert permissions to string.
|
|
|
|
*/
|
|
|
|
public static function permissionsToStr(int $permissions): string
|
|
|
|
{
|
|
|
|
$str = '';
|
|
|
|
if ($permissions & \OCP\Constants::PERMISSION_CREATE) {
|
|
|
|
$str .= 'C';
|
|
|
|
}
|
|
|
|
if ($permissions & \OCP\Constants::PERMISSION_READ) {
|
|
|
|
$str .= 'R';
|
|
|
|
}
|
|
|
|
if ($permissions & \OCP\Constants::PERMISSION_UPDATE) {
|
|
|
|
$str .= 'U';
|
|
|
|
}
|
|
|
|
if ($permissions & \OCP\Constants::PERMISSION_DELETE) {
|
|
|
|
$str .= 'D';
|
|
|
|
}
|
|
|
|
if ($permissions & \OCP\Constants::PERMISSION_SHARE) {
|
|
|
|
$str .= 'S';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2023-03-14 20:20:24 +00:00
|
|
|
/**
|
|
|
|
* Add OG metadata to a page for a node.
|
|
|
|
*
|
2023-10-15 19:46:35 +00:00
|
|
|
* @param Node $node Node to get metadata from
|
|
|
|
* @param string $title Title of the page
|
|
|
|
* @param string $url URL of the page
|
|
|
|
* @param array $previewArgs Preview arguments (e.g. token)
|
2023-03-14 20:20:24 +00:00
|
|
|
*/
|
2023-10-14 09:07:18 +00:00
|
|
|
public static function addOgMetadata(Node $node, string $title, string $url, array $previewArgs): void
|
2023-03-14 20:20:24 +00:00
|
|
|
{
|
|
|
|
// Add title
|
|
|
|
\OCP\Util::addHeader('meta', ['property' => 'og:title', 'content' => $title]);
|
|
|
|
|
|
|
|
// Get first node if folder
|
|
|
|
if ($node instanceof \OCP\Files\Folder) {
|
2023-10-06 16:55:39 +00:00
|
|
|
if (null === ($node = self::getAnyMedia($node))) {
|
|
|
|
return; // no media in folder
|
2023-03-14 20:20:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add file type
|
|
|
|
$mimeType = $node->getMimeType();
|
|
|
|
if (str_starts_with($mimeType, 'image/')) {
|
|
|
|
\OCP\Util::addHeader('meta', ['property' => 'og:type', 'content' => 'image']);
|
|
|
|
} elseif (str_starts_with($mimeType, 'video/')) {
|
|
|
|
\OCP\Util::addHeader('meta', ['property' => 'og:type', 'content' => 'video']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add OG url
|
|
|
|
\OCP\Util::addHeader('meta', ['property' => 'og:url', 'content' => $url]);
|
|
|
|
|
|
|
|
// Get URL generator
|
|
|
|
$urlGenerator = \OC::$server->get(\OCP\IURLGenerator::class);
|
|
|
|
|
|
|
|
// Add OG image
|
|
|
|
$preview = $urlGenerator->linkToRouteAbsolute('memories.Image.preview', array_merge($previewArgs, [
|
|
|
|
'id' => $node->getId(),
|
|
|
|
'x' => 1024,
|
|
|
|
'y' => 1024,
|
|
|
|
'a' => true,
|
|
|
|
]));
|
|
|
|
\OCP\Util::addHeader('meta', ['property' => 'og:image', 'content' => $preview]);
|
|
|
|
}
|
|
|
|
|
2023-10-06 16:55:39 +00:00
|
|
|
/**
|
|
|
|
* Get a random image or video from a given folder.
|
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
public static function getAnyMedia(\OCP\Files\Folder $folder): ?Node
|
2023-10-06 16:55:39 +00:00
|
|
|
{
|
|
|
|
$query = new SearchQuery(new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, [
|
|
|
|
new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'image/%'),
|
|
|
|
new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'video/%'),
|
|
|
|
]), 1, 0, [], null);
|
|
|
|
|
|
|
|
$nodes = $folder->search($query);
|
|
|
|
if (0 === \count($nodes)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $nodes[0];
|
|
|
|
}
|
|
|
|
|
2022-11-21 10:13:43 +00:00
|
|
|
/**
|
|
|
|
* Check if any encryption is enabled that we can not cope with
|
|
|
|
* such as end-to-end encryption.
|
|
|
|
*/
|
2022-12-04 17:33:20 +00:00
|
|
|
public static function isEncryptionEnabled(): bool
|
2022-11-21 10:13:43 +00:00
|
|
|
{
|
2022-12-04 17:57:31 +00:00
|
|
|
$encryptionManager = \OC::$server->get(\OCP\Encryption\IManager::class);
|
2022-11-21 10:13:43 +00:00
|
|
|
if ($encryptionManager->isEnabled()) {
|
|
|
|
// Server-side encryption (OC_DEFAULT_MODULE) is okay, others like e2e are not
|
|
|
|
return 'OC_DEFAULT_MODULE' !== $encryptionManager->getDefaultEncryptionModuleId();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2023-01-04 20:32:36 +00:00
|
|
|
|
2023-04-16 23:03:59 +00:00
|
|
|
/**
|
|
|
|
* Get list of timeline paths as array.
|
2023-10-15 19:46:35 +00:00
|
|
|
*
|
|
|
|
* @return string[] List of paths
|
2023-04-16 23:03:59 +00:00
|
|
|
*/
|
|
|
|
public static function getTimelinePaths(string $uid): array
|
|
|
|
{
|
2023-10-15 19:46:35 +00:00
|
|
|
$paths = \OC::$server->get(IConfig::class)
|
|
|
|
->getUserValue($uid, Application::APPNAME, 'timelinePath', null)
|
2023-10-22 19:58:33 +00:00
|
|
|
?: SystemConfig::get('memories.timeline.default_path');
|
2023-10-15 19:46:35 +00:00
|
|
|
|
|
|
|
return array_map(
|
2023-10-20 01:47:58 +00:00
|
|
|
static fn ($path) => self::sanitizePath(trim($path))
|
|
|
|
?? throw new \InvalidArgumentException("Invalid timeline path: {$path}"),
|
2023-10-15 19:46:35 +00:00
|
|
|
explode(';', $paths),
|
|
|
|
);
|
2023-04-16 23:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitize a path to keep only ASCII characters and special characters.
|
2023-10-20 01:47:58 +00:00
|
|
|
* Null will be returned on error.
|
2023-04-16 23:03:59 +00:00
|
|
|
*/
|
2023-10-20 01:47:58 +00:00
|
|
|
public static function sanitizePath(string $path): ?string
|
2023-04-16 23:03:59 +00:00
|
|
|
{
|
2023-10-20 01:47:58 +00:00
|
|
|
// remove double slashes and such
|
|
|
|
$normalized = \OC\Files\Filesystem::normalizePath($path, false);
|
2023-04-16 23:03:59 +00:00
|
|
|
|
2023-10-20 01:47:58 +00:00
|
|
|
// look for invalid characters and pattern
|
|
|
|
if (!\OC\Files\Filesystem::isValidPath($normalized)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $normalized;
|
2023-04-16 23:03:59 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 04:30:45 +00:00
|
|
|
/**
|
|
|
|
* Convert SQL UTC date to timestamp.
|
|
|
|
*/
|
2023-10-15 19:46:35 +00:00
|
|
|
public static function sqlUtcToTimestamp(string $sqlDate): int
|
2023-05-09 04:30:45 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
return (new \DateTime($sqlDate, new \DateTimeZone('UTC')))->getTimestamp();
|
2023-10-15 19:46:35 +00:00
|
|
|
} catch (\Throwable) {
|
2023-05-09 04:30:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 23:32:29 +00:00
|
|
|
/**
|
|
|
|
* Explode a string into fixed number of components.
|
|
|
|
*
|
2023-10-15 19:46:35 +00:00
|
|
|
* @param non-empty-string $delimiter Delimiter
|
|
|
|
* @param string $string String to explode
|
|
|
|
* @param int $count Number of components
|
2023-04-19 23:32:29 +00:00
|
|
|
*
|
|
|
|
* @return string[] Array of components
|
|
|
|
*/
|
|
|
|
public static function explode_exact(string $delimiter, string $string, int $count): array
|
|
|
|
{
|
|
|
|
return array_pad(explode($delimiter, $string, $count), $count, '');
|
|
|
|
}
|
|
|
|
|
2023-05-04 02:59:23 +00:00
|
|
|
/**
|
|
|
|
* Checks if the API call was made from a native interface.
|
|
|
|
*/
|
|
|
|
public static function callerIsNative(): bool
|
|
|
|
{
|
2023-05-26 17:51:31 +00:00
|
|
|
// Should not use IRequest here since this method is called during registration
|
2023-06-03 16:06:46 +00:00
|
|
|
if (\array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER)) {
|
2023-06-03 03:05:34 +00:00
|
|
|
return 'gallery.memories' === $_SERVER['HTTP_X_REQUESTED_WITH'];
|
|
|
|
}
|
|
|
|
|
2023-10-15 02:20:21 +00:00
|
|
|
return str_contains($_SERVER['HTTP_USER_AGENT'] ?? '', 'MemoriesNative');
|
2023-05-04 02:59:23 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 06:16:11 +00:00
|
|
|
/**
|
|
|
|
* Get the version of the native caller.
|
|
|
|
*/
|
|
|
|
public static function callerNativeVersion(): ?string
|
|
|
|
{
|
|
|
|
$userAgent = \OC::$server->get(\OCP\IRequest::class)->getHeader('User-Agent');
|
|
|
|
|
|
|
|
$matches = [];
|
|
|
|
if (preg_match('/MemoriesNative\/([0-9.]+)/', $userAgent, $matches)) {
|
|
|
|
return $matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2022-10-19 17:10:36 +00:00
|
|
|
}
|