Guest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace Lychee\Access;
  3. use Lychee\Modules\Album;
  4. use Lychee\Modules\Albums;
  5. use Lychee\Modules\Photo;
  6. use Lychee\Modules\Response;
  7. use Lychee\Modules\Session;
  8. use Lychee\Modules\Validator;
  9. final class Guest extends Access {
  10. public static function init($fn) {
  11. switch ($fn) {
  12. // Albums functions
  13. case 'Albums::get': self::getAlbumsAction(); break;
  14. // Album functions
  15. case 'Album::get': self::getAlbumAction(); break;
  16. case 'Album::getPublic': self::checkAlbumAccessAction(); break;
  17. // Photo functions
  18. case 'Photo::get': self::getPhotoAction(); break;
  19. // Session functions
  20. case 'Session::init': self::initAction(); break;
  21. case 'Session::login': self::loginAction(); break;
  22. case 'Session::logout': self::logoutAction(); break;
  23. // $_GET functions
  24. case 'Album::getArchive': self::getAlbumArchiveAction(); break;
  25. case 'Photo::getArchive': self::getPhotoArchiveAction(); break;
  26. }
  27. self::fnNotFound();
  28. }
  29. // Albums functions
  30. private static function getAlbumsAction() {
  31. $albums = new Albums();
  32. Response::json($albums->get(true));
  33. }
  34. // Album functions
  35. private static function getAlbumAction() {
  36. Validator::required(isset($_POST['albumID'], $_POST['password']), __METHOD__);
  37. $album = new Album($_POST['albumID']);
  38. if ($album->getPublic()===true) {
  39. // Album public
  40. if ($album->checkPassword($_POST['password'])===true) Response::json($album->get());
  41. else Response::warning('Wrong password!');
  42. } else {
  43. // Album private
  44. Response::warning('Album private!');
  45. }
  46. }
  47. private static function checkAlbumAccessAction() {
  48. Validator::required(isset($_POST['albumID'], $_POST['password']), __METHOD__);
  49. $album = new Album($_POST['albumID']);
  50. if ($album->getPublic()===true) {
  51. // Album public
  52. if ($album->checkPassword($_POST['password'])===true) Response::json(true);
  53. else Response::json(false);
  54. } else {
  55. // Album private
  56. Response::json(false);
  57. }
  58. }
  59. // Photo functions
  60. private static function getPhotoAction() {
  61. Validator::required(isset($_POST['photoID'], $_POST['albumID'], $_POST['password']), __METHOD__);
  62. $photo = new Photo($_POST['photoID']);
  63. $pgP = $photo->getPublic($_POST['password']);
  64. if ($pgP===2) Response::json($photo->get($_POST['albumID']));
  65. else if ($pgP===1) Response::warning('Wrong password!');
  66. else if ($pgP===0) Response::warning('Photo private!');
  67. }
  68. // Session functions
  69. private static function initAction() {
  70. $session = new Session();
  71. Response::json($session->init(true));
  72. }
  73. private static function loginAction() {
  74. Validator::required(isset($_POST['user'], $_POST['password']), __METHOD__);
  75. $session = new Session();
  76. Response::json($session->login($_POST['user'], $_POST['password']));
  77. }
  78. private static function logoutAction() {
  79. $session = new Session();
  80. Response::json($session->logout());
  81. }
  82. // $_GET functions
  83. private static function getAlbumArchiveAction() {
  84. Validator::required(isset($_GET['albumID'], $_GET['password']), __METHOD__);
  85. $album = new Album($_GET['albumID']);
  86. if ($album->getPublic()&&$album->getDownloadable()) {
  87. // Album Public
  88. if ($album->checkPassword($_GET['password'])) $album->getArchive();
  89. else Response::warning('Wrong password!');
  90. } else {
  91. // Album Private
  92. Response::warning('Album private or not downloadable!');
  93. }
  94. }
  95. private static function getPhotoArchiveAction() {
  96. Validator::required(isset($_GET['photoID'], $_GET['password']), __METHOD__);
  97. $photo = new Photo($_GET['photoID']);
  98. $pgP = $photo->getPublic($_GET['password']);
  99. // Photo Download
  100. if ($pgP===2) {
  101. // Photo Public
  102. $photo->getArchive();
  103. } else {
  104. // Photo Private
  105. Response::warning('Photo private or password incorrect!');
  106. }
  107. }
  108. }
  109. ?>