Added tokens

main
aymm 2021-08-26 10:56:59 +02:00
parent b27a3bbc58
commit c65bc123d3
Signed by: phlaym
GPG Key ID: A06651BAB6777237
4 changed files with 47 additions and 3 deletions

View File

@ -3,4 +3,5 @@
return [ return [
'username' => '', 'username' => '',
'password' => '' 'password' => ''
'noAlbumToken' => ''
]; ];

View File

@ -17,7 +17,6 @@ use PhotoPrismUpload\API\PhotoPrism;
label[for="album"] { label[for="album"] {
grid-column: 1; grid-column: 1;
grid-row: 1; grid-row: 1;
/* display: block; */
} }
#album { #album {
grid-column: 2; grid-column: 2;
@ -45,12 +44,24 @@ $api = new PhotoPrism($config);
$albums = []; $albums = [];
try { try {
$api->login(); $api->login();
$albums = $api->getAlbums();
} catch (\Exception $e) { } catch (\Exception $e) {
die('Fehler: ' . $e->getMessage()); die('Fehler: ' . $e->getMessage());
} }
if (!isset($_POST['submit'])) { if (!isset($_POST['submit'])) {
if (!isset($_GET['token'])) {
die('Sorry, kein Zugriff');
}
$token = $_GET['token'];
$tokens = explode(',', $token);
try {
$albums = $api->getAlbumsByTokens($tokens);
} catch (\Exception $e) {
die('Fehler: ' . $e->getMessage());
}
if (empty($albums) && (empty($config['noAlbumToken']) || !in_array($config['noAlbumToken'], $tokens))) {
die('Falscher Token');
}
?> ?>
<script> <script>
window.tooLarge = false; window.tooLarge = false;
@ -79,7 +90,7 @@ if (!isset($_POST['submit'])) {
</select> </select>
<!--<label></label> <!--<label></label>
<input type="text" />!--> <input type="text" />!-->
<input multiple type="file" name="files[]" id="input" /> <input multiple type="file" name="files[]" id="input" required/>
<input type="submit" name="submit" value="Upload" /> <input type="submit" name="submit" value="Upload" />
</form> </form>
<div id="error" style="display:none"></div> <div id="error" style="display:none"></div>

View File

@ -180,6 +180,36 @@ class PhotoPrism
return $albums; return $albums;
} }
public function getAlbumsByTokens(array $tokens, int $count = 1000, int $offset = 0): array
{
$albums = $this->getAlbums($count, 0);
$visibleAlbums = [];
foreach ($albums as $album) {
$token = $this->getAlbumToken($album);
$album->token = $token;
if (in_array($album->token, $tokens)) {
$visibleAlbums[] = $album;
} else {
$this->logger->debug('Skipping album without access: ' . $album->title);
}
}
$this->logger->debug('getAlbumsByToken' . json_encode($visibleAlbums));
return $visibleAlbums;
}
public function getAlbumToken($album): string
{
$uid = is_string($album) ? $album : $album->uid;
$res = $this->makeRequest('GET', '/albums/' . $uid . '/links');
$response = json_decode($res, true)[0];
if (!empty($response['error'])) {
throw new NetworkException($response['error']);
}
$this->logger->debug('Token response: ' . $res);
return $response['Token'];
}
public function uploadPhotos(?string $album = null) public function uploadPhotos(?string $album = null)
{ {
$path = time(); $path = time();

View File

@ -8,6 +8,8 @@ class Album
public string $uid = ''; public string $uid = '';
public string $slug = ''; public string $slug = '';
public string $title = ''; public string $title = '';
public ?string $token = null;
public function __construct( public function __construct(
array $response array $response
) { ) {