lychee/php/Access/Guest.php

185 lines
3.9 KiB
PHP
Raw Normal View History

2014-04-30 08:56:04 +00:00
<?php
namespace Lychee\Access;
2014-04-30 08:56:04 +00:00
use Lychee\Modules\Album;
use Lychee\Modules\Albums;
use Lychee\Modules\Photo;
2016-02-07 13:32:46 +00:00
use Lychee\Modules\Response;
use Lychee\Modules\Session;
use Lychee\Modules\Validator;
2014-04-30 08:56:04 +00:00
final class Guest extends Access {
2014-04-30 08:56:04 +00:00
public static function init($fn) {
2014-04-30 08:56:04 +00:00
switch ($fn) {
// Albums functions
case 'Albums::get': self::getAlbumsAction(); break;
// Album functions
case 'Album::get': self::getAlbumAction(); break;
case 'Album::getPublic': self::checkAlbumAccessAction(); break;
2014-04-30 08:56:04 +00:00
// Photo functions
case 'Photo::get': self::getPhotoAction(); break;
2014-04-30 08:56:04 +00:00
// Session functions
case 'Session::init': self::initAction(); break;
case 'Session::login': self::loginAction(); break;
case 'Session::logout': self::logoutAction(); break;
2014-04-30 08:56:04 +00:00
// $_GET functions
case 'Album::getArchive': self::getAlbumArchiveAction(); break;
case 'Photo::getArchive': self::getPhotoArchiveAction(); break;
2014-04-30 08:56:04 +00:00
}
self::fnNotFound();
2014-04-30 08:56:04 +00:00
}
// Albums functions
2014-04-30 08:56:04 +00:00
private static function getAlbumsAction() {
2014-04-30 08:56:04 +00:00
$albums = new Albums();
Response::json($albums->get(true));
2014-04-30 08:56:04 +00:00
}
// Album functions
private static function getAlbumAction() {
Validator::required(isset($_POST['albumID'], $_POST['password']), __METHOD__);
2014-04-30 08:56:04 +00:00
$album = new Album($_POST['albumID']);
2014-04-30 08:56:04 +00:00
2016-02-13 22:33:39 +00:00
if ($album->getPublic()===true) {
2014-04-30 08:56:04 +00:00
// Album public
2016-02-13 22:38:04 +00:00
if ($album->checkPassword($_POST['password'])===true) Response::json($album->get());
else Response::warning('Wrong password!');
2014-04-30 08:56:04 +00:00
} else {
// Album private
2016-02-07 13:32:46 +00:00
Response::warning('Album private!');
2014-04-30 08:56:04 +00:00
}
}
private static function checkAlbumAccessAction() {
Validator::required(isset($_POST['albumID'], $_POST['password']), __METHOD__);
2014-04-30 08:56:04 +00:00
$album = new Album($_POST['albumID']);
2014-04-30 08:56:04 +00:00
2016-02-13 22:33:39 +00:00
if ($album->getPublic()===true) {
2014-04-30 08:56:04 +00:00
// Album public
2016-02-13 22:38:04 +00:00
if ($album->checkPassword($_POST['password'])===true) Response::json(true);
else Response::json(false);
2014-04-30 08:56:04 +00:00
} else {
// Album private
2016-02-13 22:38:04 +00:00
Response::json(false);
2014-04-30 08:56:04 +00:00
}
}
// Photo functions
2014-04-30 08:56:04 +00:00
private static function getPhotoAction() {
Validator::required(isset($_POST['photoID'], $_POST['albumID'], $_POST['password']), __METHOD__);
2014-04-30 08:56:04 +00:00
$photo = new Photo($_POST['photoID']);
2014-04-30 08:56:04 +00:00
$pgP = $photo->getPublic($_POST['password']);
2016-02-07 13:32:46 +00:00
if ($pgP===2) Response::json($photo->get($_POST['albumID']));
else if ($pgP===1) Response::warning('Wrong password!');
else if ($pgP===0) Response::warning('Photo private!');
2014-04-30 08:56:04 +00:00
}
// Session functions
2014-04-30 08:56:04 +00:00
private static function initAction() {
2014-04-30 08:56:04 +00:00
$session = new Session();
2016-02-07 13:32:46 +00:00
Response::json($session->init(true));
2014-04-30 08:56:04 +00:00
}
private static function loginAction() {
Validator::required(isset($_POST['user'], $_POST['password']), __METHOD__);
2014-04-30 08:56:04 +00:00
$session = new Session();
2016-02-13 22:38:04 +00:00
Response::json($session->login($_POST['user'], $_POST['password']));
2014-04-30 08:56:04 +00:00
}
private static function logoutAction() {
$session = new Session();
2016-02-13 22:38:04 +00:00
Response::json($session->logout());
}
// $_GET functions
2014-04-30 08:56:04 +00:00
private static function getAlbumArchiveAction() {
Validator::required(isset($_GET['albumID'], $_GET['password']), __METHOD__);
2014-04-30 08:56:04 +00:00
$album = new Album($_GET['albumID']);
2014-04-30 08:56:04 +00:00
2014-08-17 18:22:46 +00:00
if ($album->getPublic()&&$album->getDownloadable()) {
2014-04-30 08:56:04 +00:00
// Album Public
if ($album->checkPassword($_GET['password'])) $album->getArchive();
2016-02-07 13:32:46 +00:00
else Response::warning('Wrong password!');
2014-04-30 08:56:04 +00:00
} else {
// Album Private
2016-02-07 13:32:46 +00:00
Response::warning('Album private or not downloadable!');
2014-04-30 08:56:04 +00:00
}
}
private static function getPhotoArchiveAction() {
Validator::required(isset($_GET['photoID'], $_GET['password']), __METHOD__);
2014-04-30 08:56:04 +00:00
$photo = new Photo($_GET['photoID']);
2014-04-30 08:56:04 +00:00
$pgP = $photo->getPublic($_GET['password']);
// Photo Download
if ($pgP===2) {
2014-04-30 08:56:04 +00:00
// Photo Public
2014-04-30 08:56:04 +00:00
$photo->getArchive();
} else {
// Photo Private
2016-02-07 13:32:46 +00:00
Response::warning('Photo private or password incorrect!');
2014-04-30 08:56:04 +00:00
}
}
}
2016-01-31 14:53:44 +00:00
?>