2023-01-18 03:02:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace OCA\Memories\Controller;
|
|
|
|
|
2023-03-23 23:58:49 +00:00
|
|
|
use OCA\Memories\Db\AlbumsQuery;
|
2023-01-18 03:02:00 +00:00
|
|
|
use OCP\App\IAppManager;
|
|
|
|
use OCP\AppFramework\Controller;
|
2023-03-10 02:13:29 +00:00
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
2023-01-18 03:02:00 +00:00
|
|
|
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
|
|
|
use OCP\AppFramework\Services\IInitialState;
|
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2023-03-14 20:42:55 +00:00
|
|
|
use OCP\Files\IRootFolder;
|
2023-01-18 03:02:00 +00:00
|
|
|
use OCP\IConfig;
|
2023-03-10 02:13:29 +00:00
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\IUserSession;
|
2023-01-18 03:02:00 +00:00
|
|
|
use OCP\Util;
|
|
|
|
|
|
|
|
class PublicAlbumController extends Controller
|
|
|
|
{
|
|
|
|
protected $appName;
|
|
|
|
protected IEventDispatcher $eventDispatcher;
|
|
|
|
protected IInitialState $initialState;
|
|
|
|
protected IAppManager $appManager;
|
|
|
|
protected IConfig $config;
|
2023-03-10 02:13:29 +00:00
|
|
|
protected IUserSession $userSession;
|
2023-03-14 20:42:55 +00:00
|
|
|
protected IRootFolder $rootFolder;
|
2023-03-10 02:13:29 +00:00
|
|
|
protected IURLGenerator $urlGenerator;
|
2023-03-23 23:58:49 +00:00
|
|
|
protected AlbumsQuery $albumsQuery;
|
2023-01-18 03:02:00 +00:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
string $appName,
|
|
|
|
IEventDispatcher $eventDispatcher,
|
|
|
|
IInitialState $initialState,
|
|
|
|
IAppManager $appManager,
|
|
|
|
IConfig $config,
|
2023-03-10 02:13:29 +00:00
|
|
|
IUserSession $userSession,
|
2023-03-14 20:42:55 +00:00
|
|
|
IRootFolder $rootFolder,
|
2023-03-23 18:16:28 +00:00
|
|
|
IURLGenerator $urlGenerator,
|
2023-03-23 23:58:49 +00:00
|
|
|
AlbumsQuery $albumsQuery
|
2023-01-18 03:02:00 +00:00
|
|
|
) {
|
|
|
|
$this->appName = $appName;
|
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
|
|
|
$this->initialState = $initialState;
|
|
|
|
$this->appManager = $appManager;
|
|
|
|
$this->config = $config;
|
2023-03-10 02:13:29 +00:00
|
|
|
$this->userSession = $userSession;
|
2023-03-14 20:42:55 +00:00
|
|
|
$this->rootFolder = $rootFolder;
|
2023-03-10 02:13:29 +00:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
2023-03-23 23:58:49 +00:00
|
|
|
$this->albumsQuery = $albumsQuery;
|
2023-01-18 03:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
2023-03-10 02:13:29 +00:00
|
|
|
public function showShare(string $token)
|
2023-01-18 03:02:00 +00:00
|
|
|
{
|
|
|
|
// Validate token exists
|
2023-03-23 23:58:49 +00:00
|
|
|
$album = $this->albumsQuery->getAlbumByLink($token);
|
2023-01-18 03:02:00 +00:00
|
|
|
if (!$album) {
|
|
|
|
return new TemplateResponse('core', '404', [], 'guest');
|
|
|
|
}
|
|
|
|
|
2023-03-10 02:13:29 +00:00
|
|
|
// Check if the current user has access to the album
|
|
|
|
// Just redirect to the user's page if the user is the owner or a collaborator
|
|
|
|
if ($user = $this->userSession->getUser()) {
|
|
|
|
$uid = $user->getUID();
|
|
|
|
$albumId = (int) $album['album_id'];
|
|
|
|
|
2023-03-23 23:58:49 +00:00
|
|
|
if ($uid === $album['user'] || $this->albumsQuery->userIsCollaborator($uid, $albumId)) {
|
2023-03-10 02:13:29 +00:00
|
|
|
$idStr = $album['user'].'/'.$album['name'];
|
|
|
|
$url = $this->urlGenerator->linkToRoute('memories.Page.albums', ['id' => $idStr]);
|
|
|
|
|
|
|
|
return new RedirectResponse($url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Browse anonymously if the album is accessed as a link
|
|
|
|
\OC_User::setIncognitoMode(true);
|
|
|
|
|
2023-03-14 20:42:55 +00:00
|
|
|
// Add OG metadata
|
|
|
|
$this->addOgMetadata($album, $token);
|
|
|
|
|
2023-01-18 03:02:00 +00:00
|
|
|
// Scripts
|
|
|
|
Util::addScript($this->appName, 'memories-main');
|
2023-01-18 03:09:02 +00:00
|
|
|
PageController::provideCommonInitialState($this->initialState);
|
2023-01-18 03:02:00 +00:00
|
|
|
|
|
|
|
$response = new PublicTemplateResponse($this->appName, 'main');
|
|
|
|
$response->setHeaderTitle($album['name']);
|
|
|
|
$response->setFooterVisible(false); // wth is that anyway?
|
2023-01-18 03:09:02 +00:00
|
|
|
$response->setContentSecurityPolicy(PageController::getCSP());
|
2023-01-18 03:02:00 +00:00
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2023-03-14 20:42:55 +00:00
|
|
|
|
|
|
|
private function addOgMetadata(array $album, string $token)
|
|
|
|
{
|
|
|
|
$fileId = (int) $album['last_added_photo'];
|
|
|
|
$albumId = (int) $album['album_id'];
|
2023-03-23 23:58:49 +00:00
|
|
|
$owner = $this->albumsQuery->hasFile($albumId, $fileId);
|
2023-03-14 20:42:55 +00:00
|
|
|
if (!$owner) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$nodes = $this->rootFolder->getUserFolder($owner)->getById($fileId);
|
|
|
|
if (0 === \count($nodes)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$node = $nodes[0];
|
|
|
|
|
|
|
|
$params = ['token' => $token];
|
|
|
|
$url = $this->urlGenerator->linkToRouteAbsolute('memories.PublicAlbum.showShare', $params);
|
|
|
|
\OCA\Memories\Util::addOGMetadata($node, $album['name'], $url, array_merge($params, ['album' => true]));
|
|
|
|
}
|
2023-01-18 03:02:00 +00:00
|
|
|
}
|