Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rss2 #551

Merged
merged 10 commits into from
May 13, 2020
Merged

Rss2 #551

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Exceptions\Handlers\AccessDBDenied;
use App\Exceptions\Handlers\InvalidPayload;
use App\Exceptions\Handlers\NoEncryptionKey;
use App\Exceptions\Handlers\ApplyComposer;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
Expand Down Expand Up @@ -60,6 +61,7 @@ public function render($request, Throwable $exception)
$checks[] = new NoEncryptionKey();
$checks[] = new InvalidPayload();
$checks[] = new AccessDBDenied();
$checks[] = new ApplyComposer();

foreach ($checks as $check) {
if ($check->check($request, $exception)) {
Expand Down
36 changes: 36 additions & 0 deletions app/Exceptions/Handlers/ApplyComposer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Exceptions\Handlers;

use App\Redirections\ToInstall;
use Exception;
use ErrorException;
use Illuminate\Http\Response;
use Illuminate\View\View;
use RuntimeException;
use Throwable;

class ApplyComposer
{
/**
* Render an exception into an HTTP response.
*
* @param Illuminate\Http\Request $request
* @param Throwable $exception
*
* @return bool
*/
public function check($request, Throwable $exception)
{
return $exception instanceof ErrorException && (strpos($exception->getFile(), 'laravel/framework/src/Illuminate/Routing/Router.php') !== false);
}

/**
* @return Response or View
*/
// @codeCoverageIgnoreStart
public function go()
{
return response()->view('error.error', ['code' => '500', 'message' => 'Missing dependency, please do: <code>composer install --no-dev</code><br>(or use the release channel.)']);
}
}
85 changes: 85 additions & 0 deletions app/Http/Controllers/RSSController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/** @noinspection PhpUndefinedClassInspection */

namespace App\Http\Controllers;

use App\Configs;
use App\ModelFunctions\AlbumFunctions;
use App\ModelFunctions\SymLinkFunctions;
use App\Photo;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Spatie\Feed\FeedItem;

class RSSController extends Controller
{
/**
* @var AlbumFunctions
*/
private $albumFunctions;

/**
* @var SymLinkFunctions
*/
private $symLinkFunctions;

/**
* @param AlbumFunctions $albumFunctions
* @param SymLinkFunctions $symLinkFunctions
*/
public function __construct(AlbumFunctions $albumFunctions, SymLinkFunctions $symLinkFunctions)
{
$this->albumFunctions = $albumFunctions;
$this->symLinkFunctions = $symLinkFunctions;
}

/**
* @return Collection
*/
public function getRSS()
{
if (Configs::get_value('rss_enable', '0') != '1') {
abort(404);
}

$photos = Photo::with('album', 'owner')
->where('created_at', '>=', Carbon::now()->subDays(intval(Configs::get_value('rss_recent_days', '7')))
->toDateTimeString())
->where(function ($q) {
$q->whereIn('album_id',
$this->albumFunctions->getPublicAlbums())
->orWhere('public', '=', '1');
})
->limit(Configs::get_Value('rss_max_items', '100'))
->get();

$photos = $photos->map(function ($photo_model) {
$photo = $photo_model->prepareData();
$this->symLinkFunctions->getUrl($photo_model, $photo);
$id = null;
if ($photo_model->album_id != null) {
$album = $photo_model->album;
if (!$album->full_photo_visible()) {
$photo_model->downgrade($photo);
}
$id = '#' . $photo_model->album_id . '/' . $photo_model->id;
} else { // Unsorted
if (Configs::get_value('full_photo', '1') != '1') {
$photo_model->downgrade($photo);
}
$id = 'view?p=' . $photo_model->id;
}

return FeedItem::create([
'id' => url('/' . $id),
'title' => $photo_model->title,
'summary' => $photo_model->description,
'updated' => $photo_model->created_at,
'link' => $photo['url'],
'author' => $photo_model->owner->username, ]);
});

return $photos;
}
}
129 changes: 129 additions & 0 deletions bootstrap/PanicAttack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

class PanicAttack {

private string $title = '';
private int $code = 0;
private string $message = '';

/**
* Check if all the elements of the array are in a string.
*
* @param string $haystack string to check against
* @param array $needles array of needles to check
*
* @return bool
*/
private function contains(string $haystack, array $needles)
{
foreach ($needles as $needle)
{
if (strpos($haystack, $needle) === false)
{
return false;
}
}
return true;
}

/**
* Display errors as nice page.
*
* This function is an EXIT.
*/
private function displaySimpleError()
{
$error_display = file_get_contents(__DIR__ . '/../simple_error_template.html');
$replacing = [
'$title' => $this->title,
'$code' => $this->code,
'$message' => $this->message,
];
http_response_code($this->code);
echo strtr($error_display, $replacing);
exit;
}

/**
* Called from ../index.php
*/
public function root()
{
$this->title = 'ROOT';
$this->code = 403;
$this->message = '<span class="important">This is the root directory and it MUST NOT BE PUBLICALLY ACCESSIBLE.</span><br>
To access Lychee, go <a href="public/">here</a>.';
$this->displaySimpleError();
}

/**
* Called from bootstrap/initialize.php if apache rewrite is not enabled.
*/
public function apacheRewrite()
{
$this->title = 'mod_rewrite is not enabled';
$this->code = 503;
$this->message = 'You are using apache but <code>mod_rewrite</code> is not enabled.<br>
Please do: <code>a2enmod rewrite</code>';
$this->displaySimpleError();
}

/*
|--------------------------------------------------------------------------
| Catch error where composer is loading properly.
|--------------------------------------------------------------------------
| When composer has not been run yet the ../vendor/autoload.php file does
| not exists. We check if the error message contains 'require' and
| 'vendor/autoload', in such case we display a nice error.
*/
public function composerVendorNotFound()
{
$this->title = 'vendor/autoload.php not found';
$this->code = 503;
$this->message = '<code>../vendor/autoload.php</code> not found.<br>
Please do: <code>composer install --no-dev --prefer-dist</code>';
$this->displaySimpleError();
}

/*
|--------------------------------------------------------------------------
| Catch error on missing access rights.
|--------------------------------------------------------------------------
*/
public function checkAccessRightsViews()
{
$this->title = 'Invalid access rights';
$this->code = 503;
$this->message = '<code>../storage</code> and sub-directories are not writable.<br>
Please set the proper access rights.';
$this->displaySimpleError();
}

/**
* dispatcher
*/
public function handle(string $error_message)
{

$handling = [
'composerVendorNotFound' => ['require', 'Failed opening required', 'vendor/autoload.php'],
'checkAccessRightsViews' => ['file_put_contents', 'storage/framework/views', 'Permission denied'],
];

foreach ($handling as $fun => $needles)
{
if ($this->contains($error_message, $needles))
{
$this->$fun();
}
}
// var_dump($error_message);
// if nothing has been catched so far
// $this->title = 'Error !';
// $this->code = 500;
// $this->message = 'Oups, something went wrong !';
// $this->message = $last_error['message'];
// $this->displaySimpleError();
}
}

30 changes: 13 additions & 17 deletions bootstrap/initialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,30 @@
| protocol.
*/
if (function_exists('apache_get_modules') && !in_array('mod_rewrite', apache_get_modules())) {
displaySimpleError('mod_rewrite is not enabled', 503, 'You are using apache but <code>mod_rewrite</code> is not enabled.<br>
Please do: <code>a2enmod rewrite</code>');
exit;
$oups = new PanicAttack();
$oups->apacheRewrite();
}



/*
|--------------------------------------------------------------------------
| Catch error where composer is loading properly.
| Catch error (worse case scenario)
|--------------------------------------------------------------------------
|
| Try-catch does not work on require. As a result we use the
| register_shutdown_function to handle such errors.
|
| Here we assume this will only fail if the file is not present as the
| most probable error source. We set up a kind reminder that composer
| needs to be run in order to have Lychee working.
|
| As there is no way to unregister such function in php we use the global
| variable $composer_not_found to disable this behavior at later steps of
| the execution.
*/
function onComposerNotFoundDie()
function panicHelp()
{
global $composer_not_found;
if ($composer_not_found) {
displaySimpleError('vendor/autoload.php not found', 503, '<code>../vendor/autoload.php</code> not found.<br>
Please do: <code>composer install --no-dev --prefer-dist</code>');
$last_error = error_get_last();
if ($last_error && $last_error['type']==E_ERROR)
{
$oups = new PanicAttack();
$message = substr($last_error['message'], 0, 200);
$oups->handle($message);
}
}
register_shutdown_function('onComposerNotFoundDie');
register_shutdown_function('panicHelp');

13 changes: 0 additions & 13 deletions bootstrap/simple-errors.php

This file was deleted.

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"php-http/guzzle6-adapter": "^2.0",
"php-http/message": "^1.8",
"spatie/guzzle-rate-limiter-middleware": "^1.0",
"spatie/laravel-feed": "^2.6",
"symfony/cache": "^5.0"
},
"require-dev": {
Expand Down Expand Up @@ -84,4 +85,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
Loading