lychee/php/Access/Guest.php

182 lines
3.7 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\Photo;
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) {
// Album functions
case 'Album::getAll': self::getAlbumsAction(); break;
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
// Error
default: self::fnNotFound(); break;
2014-04-30 08:56:04 +00:00
}
return true;
}
// Album functions
2014-04-30 08:56:04 +00:00
private static function getAlbumsAction() {
2014-04-30 08:56:04 +00:00
$album = new Album(null);
2014-04-30 08:56:04 +00:00
echo json_encode($album->getAll(true));
}
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
if ($album->getPublic()) {
// Album public
if ($album->checkPassword($_POST['password'])) echo json_encode($album->get());
else echo 'Warning: Wrong password!';
2014-04-30 08:56:04 +00:00
} else {
// Album private
2014-04-30 08:56:04 +00:00
echo 'Warning: Album private!';
}
}
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
if ($album->getPublic()) {
// Album public
if ($album->checkPassword($_POST['password'])) echo true;
else echo false;
2014-04-30 08:56:04 +00:00
} else {
// Album private
2014-04-30 08:56:04 +00:00
echo false;
}
}
// 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']);
if ($pgP===2) echo json_encode($photo->get($_POST['albumID']));
else if ($pgP===1) echo 'Warning: Wrong password!';
else if ($pgP===0) echo '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();
echo json_encode($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();
2014-04-30 08:56:04 +00:00
echo $session->login($_POST['user'], $_POST['password']);
}
private static function logoutAction() {
$session = new Session();
echo $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();
else exit('Warning: Wrong password!');
2014-04-30 08:56:04 +00:00
} else {
// Album Private
2014-04-30 08:56:04 +00:00
exit('Warning: Album private or not downloadable!');
}
}
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
exit('Warning: Photo private or password incorrect!');
2014-04-30 08:56:04 +00:00
}
}
}
2016-01-31 14:53:44 +00:00
?>