2023-03-23 20:32:23 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\Memories;
|
|
|
|
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
|
|
|
|
trait UtilController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Run a function and catch exceptions to return HTTP response.
|
|
|
|
*
|
2023-03-23 20:49:26 +00:00
|
|
|
* @param mixed $function
|
2023-03-23 20:32:23 +00:00
|
|
|
*/
|
|
|
|
public static function guardEx($function): Http\Response
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $function();
|
|
|
|
} catch (\OCA\Memories\HttpResponseException $e) {
|
|
|
|
return $e->response;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return new DataResponse([
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
], Http::STATUS_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
/**
|
|
|
|
* Return a callback response with guarded exceptions.
|
|
|
|
*/
|
|
|
|
public static function guardExDirect(\Closure $closure): Http\Response
|
|
|
|
{
|
2023-10-14 08:25:50 +00:00
|
|
|
/** @psalm-suppress MissingTemplateParam */
|
2023-04-20 18:33:54 +00:00
|
|
|
return new class($closure) extends Http\Response implements Http\ICallbackResponse {
|
|
|
|
private \Closure $_closure;
|
|
|
|
|
|
|
|
public function __construct(\Closure $closure)
|
|
|
|
{
|
2023-08-04 03:54:44 +00:00
|
|
|
parent::__construct();
|
2023-04-20 18:33:54 +00:00
|
|
|
$this->_closure = $closure;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function callback(Http\IOutput $output)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
($this->_closure)($output);
|
|
|
|
} catch (\OCA\Memories\HttpResponseException $e) {
|
|
|
|
$res = $e->response;
|
|
|
|
$output->setHttpResponseCode($res->getStatus());
|
|
|
|
if ($res instanceof Http\DataResponse) {
|
|
|
|
$output->setHeader('Content-Type: application/json');
|
|
|
|
$output->setOutput(json_encode($res->getData()));
|
|
|
|
} else {
|
|
|
|
$output->setOutput($res->render());
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$output->setHttpResponseCode(Http::STATUS_INTERNAL_SERVER_ERROR);
|
|
|
|
$output->setHeader('Content-Type: application/json');
|
|
|
|
$output->setOutput(json_encode([
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
/**
|
|
|
|
* Get the current user.
|
|
|
|
*
|
|
|
|
* @throws \OCA\Memories\HttpResponseException if the user is not logged in
|
|
|
|
*/
|
|
|
|
public static function getUser(): \OCP\IUser
|
|
|
|
{
|
|
|
|
$user = \OC::$server->get(\OCP\IUserSession::class)->getUser();
|
|
|
|
if (null === $user) {
|
|
|
|
throw Exceptions::NotLoggedIn();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current user ID.
|
|
|
|
*
|
|
|
|
* @throws \OCA\Memories\HttpResponseException if the user is not logged in
|
|
|
|
*/
|
|
|
|
public static function getUID(): string
|
|
|
|
{
|
|
|
|
return self::getUser()->getUID();
|
|
|
|
}
|
|
|
|
|
2023-03-23 23:58:49 +00:00
|
|
|
/**
|
|
|
|
* Check if the user is logged in.
|
|
|
|
*/
|
|
|
|
public static function isLoggedIn(): bool
|
|
|
|
{
|
|
|
|
return null !== \OC::$server->get(\OCP\IUserSession::class)->getUser();
|
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
/**
|
|
|
|
* Get a user's home folder.
|
|
|
|
*
|
|
|
|
* @param null|string $uid User ID, or null for current user
|
|
|
|
*
|
|
|
|
* @throws \OCA\Memories\HttpResponseException if the user is not logged in
|
|
|
|
*/
|
|
|
|
public static function getUserFolder(?string $uid = null): \OCP\Files\Folder
|
|
|
|
{
|
|
|
|
if (null === $uid) {
|
|
|
|
$uid = self::getUID();
|
|
|
|
}
|
|
|
|
|
|
|
|
return \OC::$server->get(\OCP\Files\IRootFolder::class)->getUserFolder($uid);
|
|
|
|
}
|
2023-03-28 01:53:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the language code for the current user.
|
|
|
|
*/
|
|
|
|
public static function getUserLang(): string
|
|
|
|
{
|
|
|
|
// Default language
|
|
|
|
$config = \OC::$server->get(\OCP\IConfig::class);
|
|
|
|
$default = $config->getSystemValue('default_language', 'en');
|
|
|
|
|
|
|
|
// Get UID of the user
|
|
|
|
try {
|
|
|
|
$uid = self::getUID();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return 'en';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get language of the user
|
|
|
|
return $config->getUserValue($uid, 'core', 'lang', $default);
|
|
|
|
}
|
2023-03-23 20:32:23 +00:00
|
|
|
}
|