diff --git a/.babelrc b/.babelrc deleted file mode 100644 index 7ccd9923..00000000 --- a/.babelrc +++ /dev/null @@ -1,9 +0,0 @@ -{ - "presets": [ - ["env", { - "targets": { - "browsers": ["last 2 versions", "safari >= 7"] - } - }] - ] -} diff --git a/.editorconfig b/.editorconfig index 5a999757..0b5d680f 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,18 +1,21 @@ -# EditorConfig is awesome: http://EditorConfig.org - -# top-most EditorConfig file root = true -# Unix-style newlines with a newline ending every file [*] -end_of_line = lf charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space insert_final_newline = true trim_trailing_whitespace = true -indent_style = space -indent_size = 4 -# Tab indentation -[Makefile] -indent_style = tab -tab_width = 4 +[*.{js,css}] +indent_size = 2 + +[*.md] +trim_trailing_whitespace = false + +[*.{yml,yaml}] +indent_size = 2 + +[docker-compose.yml] +indent_size = 4 diff --git a/.env.dusk.testing b/.env.dusk.testing deleted file mode 100644 index 756f4074..00000000 --- a/.env.dusk.testing +++ /dev/null @@ -1,14 +0,0 @@ -APP_ENV=testing -APP_DEBUG=true -APP_KEY=base64:6DJhvZLVjE6dD4Cqrteh+6Z5vZlG+v/soCKcDHLOAH0= -APP_URL=http://localhost:8000 -APP_LONGURL=localhost -APP_SHORTURL=local - -DB_CONNECTION=travis - -CACHE_DRIVER=array -SESSION_DRIVER=file -QUEUE_DRIVER=sync - -SCOUT_DRIVER=pgsql diff --git a/.env.example b/.env.example index 21e860aa..4eb61db5 100644 --- a/.env.example +++ b/.env.example @@ -1,62 +1,90 @@ APP_NAME=Laravel -APP_ENV=production -APP_KEY=SomeRandomString # Leave this -APP_DEBUG=false -APP_LOG_LEVEL=warning +APP_ENV=local +APP_KEY= +APP_DEBUG=true +APP_TIMEZONE=UTC +APP_URL=https://example.com -DB_CONNECTION=pgsql +APP_LOCALE=en +APP_FALLBACK_LOCALE=en +APP_FAKER_LOCALE=en_US + +APP_MAINTENANCE_DRIVER=file +# APP_MAINTENANCE_STORE=database + +PHP_CLI_SERVER_WORKERS=4 + +BCRYPT_ROUNDS=12 + +LOG_CHANNEL=stack +LOG_STACK=single +LOG_DEPRECATIONS_CHANNEL=null +LOG_LEVEL=debug + +DB_CONNECTION=mysql DB_HOST=127.0.0.1 -DB_PORT=5432 -DB_DATABASE= -DB_USERNAME= +DB_PORT=3306 +DB_DATABASE=laravel +DB_USERNAME=root DB_PASSWORD= -BROADCAST_DRIVER=log -CACHE_DRIVER=file -SESSION_DRIVER=file -QUEUE_DRIVER=sync +SESSION_DRIVER=database +SESSION_LIFETIME=120 +SESSION_ENCRYPT=false +SESSION_PATH=/ +SESSION_DOMAIN=null +BROADCAST_CONNECTION=log +FILESYSTEM_DISK=local +QUEUE_CONNECTION=database + +CACHE_STORE=database +# CACHE_PREFIX= + +MEMCACHED_HOST=127.0.0.1 + +REDIS_CLIENT=phpredis REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379 -MAIL_DRIVER=smtp -MAIL_HOST=smtp.mailtrap.io +MAIL_MAILER=log +MAIL_SCHEME=null +MAIL_HOST=127.0.0.1 MAIL_PORT=2525 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null +MAIL_FROM_ADDRESS="hello@example.com" +MAIL_FROM_NAME="${APP_NAME}" -PUSHER_APP_ID= -PUSHER_APP_KEY= -PUSHER_APP_SECRET= +AWS_ACCESS_KEY_ID= +AWS_SECRET_ACCESS_KEY= +AWS_DEFAULT_REGION=us-east-1 +AWS_BUCKET= +AWS_USE_PATH_STYLE_ENDPOINT=false -AWS_S3_KEY=your-key -AWS_S3_SECRET=your-secret -AWS_S3_REGION=region -AWS_S3_BUCKET=your-bucket -AWS_S3_URL=https://xxxxxxx.s3-region.amazonaws.com +VITE_APP_NAME="${APP_NAME}" -APP_URL=https://example.com # This one is necessary -APP_LONGURL=example.com -APP_SHORTURL=examp.le - -ADMIN_USER=admin # pick something better, this is used for `/admin` +ADMIN_USER=admin# pick something better, this is used for `/admin` ADMIN_PASS=password -DISPLAY_NAME='Joe Bloggs' # This is used for example in the header and titles +DISPLAY_NAME='Joe Bloggs'# This is used for example in the header and titles TWITTER_CONSUMER_KEY= TWITTER_CONSUMER_SECRET= TWITTER_ACCESS_TOKEN= TWITTER_ACCESS_TOKEN_SECRET= -SCOUT_DRIVER=pgsql +SCOUT_DRIVER=database +SCOUT_QUEUE=false -PIWIK=false -PIWIK_ID=1 -PIWIK_URL=https://analytics.jmb.lv/piwik.php +SESSION_SECURE_COOKIE=true +SESSION_SAME_SITE=strict -APP_TIMEZONE=UTC -APP_LANG=en -APP_LOG=daily -SECURE_SESSION_COOKIE=true +LOG_SLACK_WEBHOOK_URL= + +FLARE_KEY= + +IGNITION_OPEN_AI_KEY= + +BRIDGY_MASTODON_TOKEN= diff --git a/.env.travis b/.env.travis deleted file mode 100644 index a160c615..00000000 --- a/.env.travis +++ /dev/null @@ -1,14 +0,0 @@ -APP_ENV=testing -APP_DEBUG=true -APP_KEY=base64:6DJhvZLVjE6dD4Cqrteh+6Z5vZlG+v/soCKcDHLOAH0= -APP_URL=http://localhost:8000 -APP_LONGURL=localhost -APP_SHORTURL=local - -DB_CONNECTION=travis - -CACHE_DRIVER=array -SESSION_DRIVER=array -QUEUE_DRIVER=sync - -SCOUT_DRIVER=pgsql diff --git a/.eslintrc.yml b/.eslintrc.yml deleted file mode 100644 index b6ca2fd4..00000000 --- a/.eslintrc.yml +++ /dev/null @@ -1,24 +0,0 @@ -parserOptions: - sourceType: 'module' -extends: 'eslint:recommended' -env: - browser: true - es6: true -rules: - indent: - - error - - 4 - linebreak-style: - - error - - unix - quotes: - - error - - single - semi: - - error - - always - no-console: - - error - - allow: - - warn - - error diff --git a/.gitattributes b/.gitattributes index 967315dd..78f41d7a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,5 +1,7 @@ -* text=auto -*.css linguist-vendored -*.scss linguist-vendored -*.js linguist-vendored -CHANGELOG.md export-ignore +* text=auto eol=lf + +*.blade.php diff=html +*.css diff=css +*.html diff=html +*.md diff=markdown +*.php diff=php diff --git a/.gitignore b/.gitignore index 27dc57b2..a0c2459a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,16 +1,24 @@ +/.phpunit.cache /node_modules +/public/build +/public/coverage /public/hot +/public/files /public/storage /storage/*.key /vendor -/.idea -/.vagrant -Homestead.yaml +.env +.env.backup +.env.production +.phpunit.result.cache Homestead.json +Homestead.yaml +auth.json npm-debug.log yarn-error.log -.env -/public/files -/public/keybase.txt -/coverage -/LegacyTests +/.fleet +/.idea +/.vscode +ray.php +/public/gpg.key +/public/assets/img/favicon.png diff --git a/.styleci.yml b/.styleci.yml deleted file mode 100644 index 5e728eb1..00000000 --- a/.styleci.yml +++ /dev/null @@ -1,9 +0,0 @@ -preset: laravel - -disabled: - - concat_without_spaces - - simplified_null_return - - single_import_per_statement - -finder: - path: app/ diff --git a/.stylelintrc b/.stylelintrc index 6449c3f2..a9a9091b 100644 --- a/.stylelintrc +++ b/.stylelintrc @@ -1,6 +1,3 @@ { - "extends": "stylelint-config-standard", - "rules": { - "indentation": 4 - } + "extends": ["stylelint-config-standard"] } diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 7013cd90..00000000 --- a/.travis.yml +++ /dev/null @@ -1,63 +0,0 @@ -language: php - -sudo: false -dist: trusty - -cache: - - apt - -addons: - postgresql: "9.6" - apt: - sources: - - sourceline: 'deb http://ppa.launchpad.net/nginx/development/ubuntu trusty main' - packages: - - nginx - - realpath - - postgresql-9.6-postgis-2.3 - - imagemagick - #- google-chrome-stable - artifacts: - s3_region: "eu-west-1" - paths: - - $(ls tests/Browser/screenshots/*.png | tr "\n" ":") - - $(ls tests/Browser/console/*.log | tr "\n" ":") - - $(ls storage/logs/*.log | tr "\n" ":") - - $(ls /tmp/*.log | tr "\n" ":") - -services: - - postgresql - -env: - global: - - setup=basic - -php: - - 7.1 - - 7.2 - -before_install: - - printf "\n" | pecl install imagick - - cp .env.travis .env - - echo 'error_log = "/tmp/php.error.log"' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini - - psql -U travis -c 'create database travis_ci_test' - - psql -U travis -d travis_ci_test -c 'create extension postgis' - - travis_retry composer self-update --preview - -install: - - if [[ $setup = 'basic' ]]; then travis_retry composer install --no-interaction --prefer-dist; fi - - if [[ $setup = 'stable' ]]; then travis_retry composer update --no-interaction --prefer-dist --prefer-stable; fi - - if [[ $setup = 'lowest' ]]; then travis_retry composer update --no-interaction --prefer-dist --prefer-lowest --prefer-stable; fi - - travis/install-nginx.sh - -before_script: - - php artisan key:generate - - php artisan migrate - - php artisan db:seed - #- google-chrome-stable --headless --disable-gpu --remote-debugging-port=9515 http://localhost:8000 & - #- sleep 5 - -script: - - php vendor/bin/phpunit --coverage-text - #- php artisan dusk - - php vendor/bin/security-checker security:check ./composer.lock --end-point=http://security.sensiolabs.org/check_lock diff --git a/app/Article.php b/app/Article.php deleted file mode 100644 index 22d8b5e8..00000000 --- a/app/Article.php +++ /dev/null @@ -1,154 +0,0 @@ - [ - 'source' => 'title', - ], - ]; - } - - /** - * Define the relationship with webmentions. - * - * @var array - */ - public function webmentions() - { - return $this->morphMany('App\WebMention', 'commentable'); - } - - /** - * We shall set a blacklist of non-modifiable model attributes. - * - * @var array - */ - protected $guarded = ['id']; - - /** - * Process the article for display. - * - * @return string - */ - public function getHtmlAttribute() - { - $markdown = new CommonMarkConverter(); - $html = $markdown->convertToHtml($this->main); - //change
[lang] ~>
- $match = '/\[(.*)\]\n/';
- $replace = '';
- $text = preg_replace($match, $replace, $html);
- $default = preg_replace('//', '', $text);
-
- return $default;
- }
-
- /**
- * Convert updated_at to W3C time format.
- *
- * @return string
- */
- public function getW3cTimeAttribute()
- {
- return $this->updated_at->toW3CString();
- }
-
- /**
- * Convert updated_at to a tooltip appropriate format.
- *
- * @return string
- */
- public function getTooltipTimeAttribute()
- {
- return $this->updated_at->toRFC850String();
- }
-
- /**
- * Convert updated_at to a human readable format.
- *
- * @return string
- */
- public function getHumanTimeAttribute()
- {
- return $this->updated_at->diffForHumans();
- }
-
- /**
- * Get the pubdate value for RSS feeds.
- *
- * @return string
- */
- public function getPubdateAttribute()
- {
- return $this->updated_at->toRSSString();
- }
-
- /**
- * A link to the article, i.e. `/blog/1999/12/25/merry-christmas`.
- *
- * @return string
- */
- public function getLinkAttribute()
- {
- return '/blog/' . $this->updated_at->year . '/' . $this->updated_at->format('m') . '/' . $this->titleurl;
- }
-
- /**
- * Scope a query to only include articles from a particular year/month.
- *
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeDate($query, $year = null, $month = null)
- {
- if ($year == null) {
- return $query;
- }
- $start = $year . '-01-01 00:00:00';
- $end = ($year + 1) . '-01-01 00:00:00';
- if (($month !== null) && ($month !== '12')) {
- $start = $year . '-' . $month . '-01 00:00:00';
- $end = $year . '-' . ($month + 1) . '-01 00:00:00';
- }
- if ($month === '12') {
- $start = $year . '-12-01 00:00:00';
- //$end as above
- }
-
- return $query->where([
- ['updated_at', '>=', $start],
- ['updated_at', '<', $end],
- ]);
- }
-}
diff --git a/app/Bookmark.php b/app/Bookmark.php
deleted file mode 100644
index 3da5826d..00000000
--- a/app/Bookmark.php
+++ /dev/null
@@ -1,31 +0,0 @@
-belongsToMany('App\Tag');
- }
-
- /**
- * The full url of a bookmark.
- */
- public function getLongurlAttribute()
- {
- return config('app.url') . '/bookmarks/' . $this->id;
- }
-}
diff --git a/app/CommonMark/Generators/MentionGenerator.php b/app/CommonMark/Generators/MentionGenerator.php
new file mode 100644
index 00000000..2ac1a797
--- /dev/null
+++ b/app/CommonMark/Generators/MentionGenerator.php
@@ -0,0 +1,17 @@
+getIdentifier())->first();
+
+ // If we have a contact, render a mini-hcard
+ if ($contact) {
+ // rendering a blade template to a string, so can’t be an HtmlElement
+ return trim(view('templates.mini-hcard', ['contact' => $contact])->render());
+ }
+
+ // Otherwise, check the link is to the Mastodon profile
+ $mentionText = $node->getIdentifier();
+ $parts = explode('@', $mentionText);
+
+ // This is not [@]handle@instance, so return a Twitter link
+ if (count($parts) === 1) {
+ return new HtmlElement('a', ['href' => 'https://twitter.com/' . $parts[0]], '@' . $mentionText);
+ }
+
+ // Render the Mastodon profile link
+ return new HtmlElement('a', ['href' => 'https://' . $parts[1] . '/@' . $parts[0]], '@' . $mentionText);
+ }
+}
diff --git a/app/Console/Commands/CopyMediaToLocal.php b/app/Console/Commands/CopyMediaToLocal.php
new file mode 100644
index 00000000..2e8d2bce
--- /dev/null
+++ b/app/Console/Commands/CopyMediaToLocal.php
@@ -0,0 +1,69 @@
+path;
+
+ $this->info('Processing: ' . $filename);
+
+ // If the file is already saved locally skip to next one
+ if (Storage::disk('local')->exists('public/' . $filename)) {
+ $this->info('File already exists locally, skipping');
+
+ continue;
+ }
+
+ // Copy the file from S3 to the local filesystem
+ if (! Storage::disk('s3')->exists($filename)) {
+ $this->error('File does not exist on S3');
+
+ continue;
+ }
+ $contents = Storage::disk('s3')->get($filename);
+ Storage::disk('local')->put('public/' . $filename, $contents);
+
+ // Copy -medium and -small versions if they exist
+ $filenameParts = explode('.', $filename);
+ $extension = array_pop($filenameParts);
+ $basename = trim(implode('.', $filenameParts), '.');
+ $mediumFilename = $basename . '-medium.' . $extension;
+ $smallFilename = $basename . '-small.' . $extension;
+ if (Storage::disk('s3')->exists($mediumFilename)) {
+ Storage::disk('local')->put('public/' . $mediumFilename, Storage::disk('s3')->get($mediumFilename));
+ }
+ if (Storage::disk('s3')->exists($smallFilename)) {
+ Storage::disk('local')->put('public/' . $smallFilename, Storage::disk('s3')->get($smallFilename));
+ }
+ }
+ }
+}
diff --git a/app/Console/Commands/MigratePlaceDataFromPostgis.php b/app/Console/Commands/MigratePlaceDataFromPostgis.php
new file mode 100644
index 00000000..8d5d2c92
--- /dev/null
+++ b/app/Console/Commands/MigratePlaceDataFromPostgis.php
@@ -0,0 +1,75 @@
+exists) {
+ $this->info('There is no Postgis location data in the table. Exiting.');
+
+ return 0;
+ }
+
+ $latitudeColumn = DB::selectOne(DB::raw("
+ SELECT EXISTS (
+ SELECT 1
+ FROM information_schema.columns
+ WHERE table_name = 'places'
+ AND column_name = 'latitude'
+ )
+ "));
+
+ if (! $latitudeColumn->exists) {
+ $this->error('Latitude and longitude columns have not been created yet');
+
+ return 1;
+ }
+
+ $places = Place::all();
+
+ $places->each(function ($place) {
+ $this->info('Extracting Postgis data for place: ' . $place->name);
+
+ $place->latitude = $place->location->getLat();
+ $place->longitude = $place->location->getLng();
+ $place->save();
+ });
+
+ return 0;
+ }
+}
diff --git a/app/Console/Commands/ParseCachedWebMentions.php b/app/Console/Commands/ParseCachedWebMentions.php
index caf244cd..a6b29176 100644
--- a/app/Console/Commands/ParseCachedWebMentions.php
+++ b/app/Console/Commands/ParseCachedWebMentions.php
@@ -1,9 +1,12 @@
allFiles(storage_path() . '/HTML');
- foreach ($HTMLfiles as $file) {
- if ($file->getExtension() != 'backup') { //we don’t want to parse.backup files
+ $htmlFiles = $filesystem->allFiles(storage_path() . '/HTML');
+ foreach ($htmlFiles as $file) {
+ if ($file->getExtension() !== 'backup') { // we don’t want to parse `.backup` files
$filepath = $file->getPathname();
$this->info('Loading HTML from: ' . $filepath);
$html = $filesystem->get($filepath);
- $url = $this->URLFromFilename($filepath);
- $microformats = \Mf2\parse($html, $url);
+ $url = $this->urlFromFilename($filepath);
$webmention = WebMention::where('source', $url)->firstOrFail();
+ $microformats = \Mf2\parse($html, $url);
$webmention->mf2 = json_encode($microformats);
$webmention->save();
$this->info('Saved the microformats to the database.');
@@ -57,16 +50,13 @@ class ParseCachedWebMentions extends Command
/**
* Determine the source URL from a filename.
- *
- * @param string
- * @return string
*/
- private function URLFromFilename($filepath)
+ private function urlFromFilename(string $filepath): string
{
$dir = mb_substr($filepath, mb_strlen(storage_path() . '/HTML/'));
$url = str_replace(['http/', 'https/'], ['http://', 'https://'], $dir);
- if (mb_substr($url, -10) == 'index.html') {
- $url = mb_substr($url, 0, mb_strlen($url) - 10);
+ if (mb_substr($url, -10) === 'index.html') {
+ $url = mb_substr($url, 0, -10);
}
return $url;
diff --git a/app/Console/Commands/ReDownloadWebMentions.php b/app/Console/Commands/ReDownloadWebMentions.php
index da686229..c6452ba9 100644
--- a/app/Console/Commands/ReDownloadWebMentions.php
+++ b/app/Console/Commands/ReDownloadWebMentions.php
@@ -1,10 +1,12 @@
securityChecker = $securityChecker;
- }
-
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $alerts = $this->securityChecker->check(base_path() . '/composer.lock');
- if (count($alerts) === 0) {
- $this->info('No security vulnerabilities found.');
-
- return 0;
- }
- $this->error('vulnerabilities found');
-
- return 1;
- }
-}
diff --git a/app/Console/Commands/UpdateWebmentionsRelationship.php b/app/Console/Commands/UpdateWebmentionsRelationship.php
new file mode 100644
index 00000000..f5bc1114
--- /dev/null
+++ b/app/Console/Commands/UpdateWebmentionsRelationship.php
@@ -0,0 +1,36 @@
+where('commentable_type', '=', 'App\Model\Note')
+ ->update(['commentable_type' => Note::class]);
+
+ $this->info('All webmentions updated to relate to the correct note model class');
+ }
+}
diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php
index cec36b39..432844ad 100644
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -7,34 +7,19 @@ use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
- /**
- * The Artisan commands provided by your application.
- *
- * @var array
- */
- protected $commands = [
- Commands\SecurityCheck::class,
- Commands\ParseCachedWebMentions::class,
- Commands\ReDownloadWebMentions::class,
- ];
-
/**
* Define the application's command schedule.
- *
- * @param \Illuminate\Console\Scheduling\Schedule $schedule
- * @return void
*/
- protected function schedule(Schedule $schedule)
+ protected function schedule(Schedule $schedule): void
{
$schedule->command('horizon:snapshot')->everyFiveMinutes();
+ $schedule->command('cache:prune-stale-tags')->hourly();
}
/**
* Register the commands for the application.
- *
- * @return void
*/
- protected function commands()
+ protected function commands(): void
{
$this->load(__DIR__.'/Commands');
diff --git a/app/Contact.php b/app/Contact.php
deleted file mode 100644
index 7f31ad52..00000000
--- a/app/Contact.php
+++ /dev/null
@@ -1,22 +0,0 @@
-match($request);
- }
-
- return parent::render($request, $exception);
+ $this->reportable(function (Throwable $_e) {
+ //
+ });
}
}
diff --git a/app/Exceptions/InternetArchiveException.php b/app/Exceptions/InternetArchiveException.php
new file mode 100644
index 00000000..99d5cab7
--- /dev/null
+++ b/app/Exceptions/InternetArchiveException.php
@@ -0,0 +1,5 @@
+orderBy('id', 'desc')->get();
return view('admin.articles.index', ['posts' => $posts]);
}
- /**
- * Show the new article form.
- *
- * @return \Illuminate\View\Factory view
- */
- public function create()
+ public function create(): View
{
$message = session('message');
return view('admin.articles.create', ['message' => $message]);
}
- /**
- * Process an incoming request for a new article and save it.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\View\Factory view
- */
- public function store(Request $request)
+ public function store(): RedirectResponse
{
- //if a `.md` is attached use that for the main content.
- if ($request->hasFile('article')) {
- $file = $request->file('article')->openFile();
+ // if a `.md` is attached use that for the main content.
+ if (request()->hasFile('article')) {
+ $file = request()->file('article')->openFile();
$content = $file->fread($file->getSize());
}
- $main = $content ?? $request->input('main');
- $article = Article::create(
- [
- 'url' => $request->input('url'),
- 'title' => $request->input('title'),
- 'main' => $main,
- 'published' => $request->input('published') ?? 0,
- ]
- );
+ $main = $content ?? request()->input('main');
+ Article::create([
+ 'url' => request()->input('url'),
+ 'title' => request()->input('title'),
+ 'main' => $main,
+ 'published' => request()->input('published') ?? 0,
+ ]);
return redirect('/admin/blog');
}
- /**
- * Show the edit form for an existing article.
- *
- * @param string The article id
- * @return \Illuminate\View\Factory view
- */
- public function edit($articleId)
+ public function edit(Article $article): View
{
- $post = Article::select(
- 'title',
- 'main',
- 'url',
- 'published'
- )->where('id', $articleId)->get();
-
- return view('admin.articles.edit', ['id' => $articleId, 'post' => $post]);
+ return view('admin.articles.edit', ['article' => $article]);
}
- /**
- * Process an incoming request to edit an article.
- *
- * @param \Illuminate\Http\Request $request
- * @param string
- * @return \Illuminate|View\Factory view
- */
- public function update(Request $request, $articleId)
+ public function update(int $articleId): RedirectResponse
{
$article = Article::find($articleId);
- $article->title = $request->input('title');
- $article->url = $request->input('url');
- $article->main = $request->input('main');
- $article->published = $request->input('published') ?? 0;
+ $article->title = request()->input('title');
+ $article->url = request()->input('url');
+ $article->main = request()->input('main');
+ $article->published = request()->input('published') ?? 0;
$article->save();
return redirect('/admin/blog');
}
- /**
- * Process a request to delete an aricle.
- *
- * @param string The article id
- * @return \Illuminate\View\Factory view
- */
- public function destroy($articleId)
+ public function destroy(int $articleId): RedirectResponse
{
Article::where('id', $articleId)->delete();
diff --git a/app/Http/Controllers/Admin/BioController.php b/app/Http/Controllers/Admin/BioController.php
new file mode 100644
index 00000000..c760e12c
--- /dev/null
+++ b/app/Http/Controllers/Admin/BioController.php
@@ -0,0 +1,32 @@
+ $bio,
+ ]);
+ }
+
+ public function update(Request $request): RedirectResponse
+ {
+ $bio = Bio::firstOrNew();
+ $bio->content = $request->input('content');
+ $bio->save();
+
+ return redirect()->route('admin.bio.show');
+ }
+}
diff --git a/app/Http/Controllers/Admin/ClientsController.php b/app/Http/Controllers/Admin/ClientsController.php
index f57603bf..38524b62 100644
--- a/app/Http/Controllers/Admin/ClientsController.php
+++ b/app/Http/Controllers/Admin/ClientsController.php
@@ -1,19 +1,20 @@
$request->input('client_url'),
- 'client_name' => $request->input('client_name'),
+ 'client_url' => request()->input('client_url'),
+ 'client_name' => request()->input('client_name'),
]);
return redirect('/admin/clients');
@@ -48,11 +44,8 @@ class ClientsController extends Controller
/**
* Show a form to edit a client name.
- *
- * @param string The client id
- * @return \Illuminate\View\Factory view
*/
- public function edit($clientId)
+ public function edit(int $clientId): View
{
$client = MicropubClient::findOrFail($clientId);
@@ -65,16 +58,12 @@ class ClientsController extends Controller
/**
* Process the request to edit a client name.
- *
- * @param string The client id
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\View\Factory view
*/
- public function update($clientId, Request $request)
+ public function update(int $clientId): RedirectResponse
{
$client = MicropubClient::findOrFail($clientId);
- $client->client_url = $request->input('client_url');
- $client->client_name = $request->input('client_name');
+ $client->client_url = request()->input('client_url');
+ $client->client_name = request()->input('client_name');
$client->save();
return redirect('/admin/clients');
@@ -82,13 +71,10 @@ class ClientsController extends Controller
/**
* Process a request to delete a client.
- *
- * @param string The client id
- * @return redirect
*/
- public function destroy($articleId)
+ public function destroy(int $clientId): RedirectResponse
{
- MicropubClient::where('id', $articleId)->delete();
+ MicropubClient::where('id', $clientId)->delete();
return redirect('/admin/clients');
}
diff --git a/app/Http/Controllers/Admin/ContactsController.php b/app/Http/Controllers/Admin/ContactsController.php
index b77b630b..eb45320c 100644
--- a/app/Http/Controllers/Admin/ContactsController.php
+++ b/app/Http/Controllers/Admin/ContactsController.php
@@ -1,21 +1,23 @@
name = $request->input('name');
- $contact->nick = $request->input('nick');
- $contact->homepage = $request->input('homepage');
- $contact->twitter = $request->input('twitter');
- $contact->facebook = $request->input('facebook');
+ $contact = new Contact;
+ $contact->name = request()->input('name');
+ $contact->nick = request()->input('nick');
+ $contact->homepage = request()->input('homepage');
+ $contact->twitter = request()->input('twitter');
+ $contact->facebook = request()->input('facebook');
$contact->save();
return redirect('/admin/contacts');
@@ -53,11 +50,8 @@ class ContactsController extends Controller
/**
* Show the form to edit an existing contact.
- *
- * @param string The contact id
- * @return \Illuminate\View\Factory view
*/
- public function edit($contactId)
+ public function edit(int $contactId): View
{
$contact = Contact::findOrFail($contactId);
@@ -68,31 +62,25 @@ class ContactsController extends Controller
* Process the request to edit a contact.
*
* @todo Allow saving profile pictures for people without homepages
- *
- * @param string The contact id
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\View\Factory view
*/
- public function update($contactId, Request $request)
+ public function update(int $contactId): RedirectResponse
{
$contact = Contact::findOrFail($contactId);
- $contact->name = $request->input('name');
- $contact->nick = $request->input('nick');
- $contact->homepage = $request->input('homepage');
- $contact->twitter = $request->input('twitter');
- $contact->facebook = $request->input('facebook');
+ $contact->name = request()->input('name');
+ $contact->nick = request()->input('nick');
+ $contact->homepage = request()->input('homepage');
+ $contact->twitter = request()->input('twitter');
+ $contact->facebook = request()->input('facebook');
$contact->save();
- if ($request->hasFile('avatar')) {
- if ($request->input('homepage') != '') {
- $dir = parse_url($request->input('homepage'))['host'];
- $destination = public_path() . '/assets/profile-images/' . $dir;
- $filesystem = new Filesystem();
- if ($filesystem->isDirectory($destination) === false) {
- $filesystem->makeDirectory($destination);
- }
- $request->file('avatar')->move($destination, 'image');
+ if (request()->hasFile('avatar') && (request()->input('homepage') != '')) {
+ $dir = parse_url(request()->input('homepage'), PHP_URL_HOST);
+ $destination = public_path() . '/assets/profile-images/' . $dir;
+ $filesystem = new Filesystem;
+ if ($filesystem->isDirectory($destination) === false) {
+ $filesystem->makeDirectory($destination);
}
+ request()->file('avatar')->move($destination, 'image');
}
return redirect('/admin/contacts');
@@ -100,11 +88,8 @@ class ContactsController extends Controller
/**
* Process the request to delete a contact.
- *
- * @param string The contact id
- * @return \Illuminate\View\Factory view
*/
- public function destroy($contactId)
+ public function destroy(int $contactId): RedirectResponse
{
$contact = Contact::findOrFail($contactId);
$contact->delete();
@@ -118,42 +103,51 @@ class ContactsController extends Controller
* This method attempts to find the microformat marked-up profile image
* from a given homepage and save it accordingly
*
- * @param string The contact id
- * @return \Illuminate\View\Factory view
+ * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
*/
- public function getAvatar($contactId)
+ public function getAvatar(int $contactId)
{
+ // Initialising
+ $avatarURL = null;
+ $avatar = null;
$contact = Contact::findOrFail($contactId);
- $homepage = $contact->homepage;
- if (($homepage !== null) && ($homepage !== '')) {
- $client = new Client();
+ if ($contact->homepage !== null && mb_strlen($contact->homepage) !== 0) {
+ $client = resolve(Client::class);
try {
- $response = $client->get($homepage);
- $html = (string) $response->getBody();
- $mf2 = \Mf2\parse($html, $homepage);
+ $response = $client->get($contact->homepage);
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
- return "Bad Response from $homepage";
+ return redirect('/admin/contacts/' . $contactId . '/edit')
+ ->with('error', 'Bad resposne from contact’s homepage');
}
- $avatarURL = null; // Initialising
+ $mf2 = \Mf2\parse((string) $response->getBody(), $contact->homepage);
foreach ($mf2['items'] as $microformat) {
- if ($microformat['type'][0] == 'h-card') {
- $avatarURL = $microformat['properties']['photo'][0];
+ if (Arr::get($microformat, 'type.0') === 'h-card') {
+ $avatarURL = Arr::get($microformat, 'properties.photo.0.value');
break;
}
}
- try {
- $avatar = $client->get($avatarURL);
- } catch (\GuzzleHttp\Exception\BadResponseException $e) {
- return "Unable to get $avatarURL";
+ if ($avatarURL !== null) {
+ try {
+ $avatar = $client->get($avatarURL);
+ } catch (\GuzzleHttp\Exception\BadResponseException $e) {
+ return redirect('/admin/contacts/' . $contactId . '/edit')
+ ->with('error', 'Unable to download avatar');
+ }
}
- $directory = public_path() . '/assets/profile-images/' . parse_url($homepage)['host'];
- $filesystem = new Filesystem();
- if ($filesystem->isDirectory($directory) === false) {
- $filesystem->makeDirectory($directory);
- }
- $filesystem->put($directory . '/image', $avatar->getBody());
+ if ($avatar !== null) {
+ $directory = public_path() . '/assets/profile-images/' . parse_url($contact->homepage, PHP_URL_HOST);
+ $filesystem = new Filesystem;
+ if ($filesystem->isDirectory($directory) === false) {
+ $filesystem->makeDirectory($directory);
+ }
+ $filesystem->put($directory . '/image', $avatar->getBody());
- return view('admin.contacts.getavatarsuccess', ['homepage' => parse_url($homepage)['host']]);
+ return view('admin.contacts.getavatarsuccess', [
+ 'homepage' => parse_url($contact->homepage, PHP_URL_HOST),
+ ]);
+ }
}
+
+ return redirect('/admin/contacts/' . $contactId . '/edit');
}
}
diff --git a/app/Http/Controllers/Admin/HomeController.php b/app/Http/Controllers/Admin/HomeController.php
index ebb06f31..ae4f4d36 100644
--- a/app/Http/Controllers/Admin/HomeController.php
+++ b/app/Http/Controllers/Admin/HomeController.php
@@ -1,12 +1,18 @@
config('admin.user')]);
}
diff --git a/app/Http/Controllers/Admin/LikesController.php b/app/Http/Controllers/Admin/LikesController.php
new file mode 100644
index 00000000..9ebd7e74
--- /dev/null
+++ b/app/Http/Controllers/Admin/LikesController.php
@@ -0,0 +1,81 @@
+ normalize_url(request()->input('like_url')),
+ ]);
+ ProcessLike::dispatch($like);
+
+ return redirect('/admin/likes');
+ }
+
+ /**
+ * Display the form to edit a specific like.
+ */
+ public function edit(int $likeId): View
+ {
+ $like = Like::findOrFail($likeId);
+
+ return view('admin.likes.edit', [
+ 'id' => $like->id,
+ 'like_url' => $like->url,
+ ]);
+ }
+
+ /**
+ * Process a request to edit a like.
+ */
+ public function update(int $likeId): RedirectResponse
+ {
+ $like = Like::findOrFail($likeId);
+ $like->url = normalize_url(request()->input('like_url'));
+ $like->save();
+ ProcessLike::dispatch($like);
+
+ return redirect('/admin/likes');
+ }
+
+ /**
+ * Process the request to delete a like.
+ */
+ public function destroy(int $likeId): RedirectResponse
+ {
+ Like::where('id', $likeId)->delete();
+
+ return redirect('/admin/likes');
+ }
+}
diff --git a/app/Http/Controllers/Admin/NotesController.php b/app/Http/Controllers/Admin/NotesController.php
index 0ace90fb..c6ed93ba 100644
--- a/app/Http/Controllers/Admin/NotesController.php
+++ b/app/Http/Controllers/Admin/NotesController.php
@@ -1,29 +1,22 @@
noteService = $noteService;
- }
-
/**
* List the notes that can be edited.
- *
- * @return \Illuminate\View\Factory view
*/
- public function index()
+ public function index(): View
{
$notes = Note::select('id', 'note')->orderBy('id', 'desc')->get();
foreach ($notes as $note) {
@@ -35,57 +28,29 @@ class NotesController extends Controller
/**
* Show the form to make a new note.
- *
- * @return \Illuminate\View\Factory view
*/
- public function create()
+ public function create(): View
{
return view('admin.notes.create');
}
/**
* Process a request to make a new note.
- *
- * @param Illuminate\Http\Request $request
- * @todo Sort this mess out
*/
- public function store(Request $request)
+ public function store(Request $request): RedirectResponse
{
- $validator = Validator::make(
- $request->all(),
- ['photo' => 'photosize'],
- ['photosize' => 'At least one uploaded file exceeds size limit of 5MB']
- );
- if ($validator->fails()) {
- return redirect('/admin/notes/create')
- ->withErrors($validator)
- ->withInput();
- }
-
- $data = [];
- $data['content'] = $request->input('content');
- $data['in-reply-to'] = $request->input('in-reply-to');
- $data['location'] = $request->input('location');
- $data['syndicate'] = [];
- if ($request->input('twitter')) {
- $data['syndicate'][] = 'twitter';
- }
- if ($request->input('facebook')) {
- $data['syndicate'][] = 'facebook';
- }
-
- $note = $this->noteService->createNote($data);
+ Note::create([
+ 'in_reply_to' => $request->input('in-reply-to'),
+ 'note' => $request->input('content'),
+ ]);
return redirect('/admin/notes');
}
/**
* Display the form to edit a specific note.
- *
- * @param string The note id
- * @return \Illuminate\View\Factory view
*/
- public function edit($noteId)
+ public function edit(int $noteId): View
{
$note = Note::find($noteId);
$note->originalNote = $note->getOriginal('note');
@@ -96,19 +61,16 @@ class NotesController extends Controller
/**
* Process a request to edit a note. Easy since this can only be done
* from the admin CP.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\View\Factory view
*/
- public function update($noteId, Request $request)
+ public function update(int $noteId): RedirectResponse
{
- //update note data
+ // update note data
$note = Note::findOrFail($noteId);
- $note->note = $request->input('content');
- $note->in_reply_to = $request->input('in-reply-to');
+ $note->note = request()->input('content');
+ $note->in_reply_to = request()->input('in-reply-to');
$note->save();
- if ($request->input('webmentions')) {
+ if (request()->input('webmentions')) {
dispatch(new SendWebMentions($note));
}
@@ -117,13 +79,10 @@ class NotesController extends Controller
/**
* Delete the note.
- *
- * @param int id
- * @return view
*/
- public function destroy($id)
+ public function destroy(int $noteId): RedirectResponse
{
- $note = Note::findOrFail($id);
+ $note = Note::findOrFail($noteId);
$note->delete();
return redirect('/admin/notes');
diff --git a/app/Http/Controllers/Admin/PasskeysController.php b/app/Http/Controllers/Admin/PasskeysController.php
new file mode 100644
index 00000000..9f635f10
--- /dev/null
+++ b/app/Http/Controllers/Admin/PasskeysController.php
@@ -0,0 +1,326 @@
+user();
+ $passkeys = $user->passkey;
+
+ return view('admin.passkeys.index', compact('passkeys'));
+ }
+
+ /**
+ * @throws RandomException
+ * @throws \JsonException
+ */
+ public function getCreateOptions(Request $request): JsonResponse
+ {
+ /** @var User $user */
+ $user = auth()->user();
+
+ // RP Entity i.e. the application
+ $rpEntity = PublicKeyCredentialRpEntity::create(
+ name: config('app.name'),
+ id: config('app.url'),
+ );
+
+ // User Entity
+ $userEntity = PublicKeyCredentialUserEntity::create(
+ name: $user->name,
+ id: (string) $user->id,
+ displayName: $user->name,
+ );
+
+ // Challenge
+ $challenge = random_bytes(16);
+
+ // List of supported public key parameters
+ $pubKeyCredParams = collect([
+ Algorithms::COSE_ALGORITHM_EDDSA,
+ Algorithms::COSE_ALGORITHM_ES256,
+ Algorithms::COSE_ALGORITHM_RS256,
+ ])->map(
+ fn ($algorithm) => PublicKeyCredentialParameters::create('public-key', $algorithm)
+ )->toArray();
+
+ $authenticatorSelectionCriteria = AuthenticatorSelectionCriteria::create(
+ userVerification: AuthenticatorSelectionCriteria::USER_VERIFICATION_REQUIREMENT_REQUIRED,
+ residentKey: AuthenticatorSelectionCriteria::RESIDENT_KEY_REQUIREMENT_REQUIRED,
+ );
+
+ $publicKeyCredentialCreationOptions = PublicKeyCredentialCreationOptions::create(
+ rp: $rpEntity,
+ user: $userEntity,
+ challenge: $challenge,
+ pubKeyCredParams: $pubKeyCredParams,
+ authenticatorSelection: $authenticatorSelectionCriteria,
+ attestation: PublicKeyCredentialCreationOptions::ATTESTATION_CONVEYANCE_PREFERENCE_NONE
+ );
+
+ $attestationStatementSupportManager = new AttestationStatementSupportManager;
+ $attestationStatementSupportManager->add(new NoneAttestationStatementSupport);
+ $webauthnSerializerFactory = new WebauthnSerializerFactory(
+ attestationStatementSupportManager: $attestationStatementSupportManager
+ );
+ $webauthnSerializer = $webauthnSerializerFactory->create();
+ $publicKeyCredentialCreationOptions = $webauthnSerializer->serialize(
+ data: $publicKeyCredentialCreationOptions,
+ format: 'json'
+ );
+
+ $request->session()->put('create_options', $publicKeyCredentialCreationOptions);
+
+ return JsonResponse::fromJsonString($publicKeyCredentialCreationOptions);
+ }
+
+ /**
+ * @throws Throwable
+ * @throws WebauthnException
+ * @throws \JsonException
+ */
+ public function create(Request $request): JsonResponse
+ {
+ /** @var User $user */
+ $user = auth()->user();
+
+ $publicKeyCredentialCreationOptionsData = session('create_options');
+ // Unset session data to mitigate replay attacks
+ $request->session()->forget('create_options');
+ if (empty($publicKeyCredentialCreationOptionsData)) {
+ throw new WebAuthnException('No public key credential request options found');
+ }
+
+ $attestationStatementSupportManager = new AttestationStatementSupportManager;
+ $attestationStatementSupportManager->add(new NoneAttestationStatementSupport);
+ $webauthnSerializerFactory = new WebauthnSerializerFactory(
+ attestationStatementSupportManager: $attestationStatementSupportManager
+ );
+ $webauthnSerializer = $webauthnSerializerFactory->create();
+
+ $publicKeyCredential = $webauthnSerializer->deserialize(
+ json_encode($request->all(), JSON_THROW_ON_ERROR),
+ PublicKeyCredential::class,
+ 'json'
+ );
+
+ if (! $publicKeyCredential->response instanceof AuthenticatorAttestationResponse) {
+ throw new WebAuthnException('Invalid response type');
+ }
+
+ $algorithmManager = new Manager;
+ $algorithmManager->add(new Ed25519);
+ $algorithmManager->add(new ES256);
+ $algorithmManager->add(new RS256);
+
+ $ceremonyStepManagerFactory = new CeremonyStepManagerFactory;
+ $ceremonyStepManagerFactory->setAlgorithmManager($algorithmManager);
+ $ceremonyStepManagerFactory->setAttestationStatementSupportManager(
+ $attestationStatementSupportManager
+ );
+ $ceremonyStepManagerFactory->setExtensionOutputCheckerHandler(
+ ExtensionOutputCheckerHandler::create()
+ );
+ $allowedOrigins = [];
+ if (App::environment('local', 'development')) {
+ $allowedOrigins = [config('app.url')];
+ }
+ $ceremonyStepManagerFactory->setAllowedOrigins($allowedOrigins);
+
+ $authenticatorAttestationResponseValidator = AuthenticatorAttestationResponseValidator::create(
+ ceremonyStepManager: $ceremonyStepManagerFactory->creationCeremony()
+ );
+
+ $publicKeyCredentialCreationOptions = $webauthnSerializer->deserialize(
+ $publicKeyCredentialCreationOptionsData,
+ PublicKeyCredentialCreationOptions::class,
+ 'json'
+ );
+
+ $publicKeyCredentialSource = $authenticatorAttestationResponseValidator->check(
+ authenticatorAttestationResponse: $publicKeyCredential->response,
+ publicKeyCredentialCreationOptions: $publicKeyCredentialCreationOptions,
+ host: config('app.url')
+ );
+
+ $user->passkey()->create([
+ 'passkey_id' => Base64UrlSafe::encodeUnpadded($publicKeyCredentialSource->publicKeyCredentialId),
+ 'passkey' => json_encode($publicKeyCredentialSource, JSON_THROW_ON_ERROR),
+ ]);
+
+ return response()->json([
+ 'success' => true,
+ 'message' => 'Passkey created successfully',
+ ]);
+ }
+
+ /**
+ * @throws RandomException
+ * @throws \JsonException
+ */
+ public function getRequestOptions(Request $request): JsonResponse
+ {
+ $publicKeyCredentialRequestOptions = PublicKeyCredentialRequestOptions::create(
+ challenge: random_bytes(16),
+ userVerification: PublicKeyCredentialRequestOptions::USER_VERIFICATION_REQUIREMENT_REQUIRED
+ );
+
+ $attestationStatementSupportManager = AttestationStatementSupportManager::create();
+ $attestationStatementSupportManager->add(NoneAttestationStatementSupport::create());
+ $factory = new WebauthnSerializerFactory(
+ attestationStatementSupportManager: $attestationStatementSupportManager
+ );
+ $serializer = $factory->create();
+ $publicKeyCredentialRequestOptions = $serializer->serialize(data: $publicKeyCredentialRequestOptions, format: 'json');
+
+ $request->session()->put('request_options', $publicKeyCredentialRequestOptions);
+
+ return JsonResponse::fromJsonString($publicKeyCredentialRequestOptions);
+ }
+
+ /**
+ * @throws \JsonException
+ */
+ public function login(Request $request): JsonResponse
+ {
+ $requestOptions = session('request_options');
+ $request->session()->forget('request_options');
+
+ if (empty($requestOptions)) {
+ return response()->json([
+ 'success' => false,
+ 'message' => 'No request options found',
+ ], 400);
+ }
+
+ $attestationStatementSupportManager = new AttestationStatementSupportManager;
+ $attestationStatementSupportManager->add(new NoneAttestationStatementSupport);
+
+ $webauthnSerializerFactory = new WebauthnSerializerFactory(
+ attestationStatementSupportManager: $attestationStatementSupportManager
+ );
+ $webauthnSerializer = $webauthnSerializerFactory->create();
+
+ $publicKeyCredential = $webauthnSerializer->deserialize(
+ json_encode($request->all(), JSON_THROW_ON_ERROR),
+ PublicKeyCredential::class,
+ 'json'
+ );
+
+ if (! $publicKeyCredential->response instanceof AuthenticatorAssertionResponse) {
+ return response()->json([
+ 'success' => false,
+ 'message' => 'Invalid response type',
+ ], 400);
+ }
+
+ $passkey = Passkey::firstWhere('passkey_id', $publicKeyCredential->id);
+ if (! $passkey) {
+ return response()->json([
+ 'success' => false,
+ 'message' => 'Passkey not found',
+ ], 404);
+ }
+
+ $publicKeyCredentialSource = $webauthnSerializer->deserialize(
+ $passkey->passkey,
+ PublicKeyCredentialSource::class,
+ 'json'
+ );
+
+ $algorithmManager = new Manager;
+ $algorithmManager->add(new Ed25519);
+ $algorithmManager->add(new ES256);
+ $algorithmManager->add(new RS256);
+
+ $attestationStatementSupportManager = new AttestationStatementSupportManager;
+ $attestationStatementSupportManager->add(new NoneAttestationStatementSupport);
+
+ $ceremonyStepManagerFactory = new CeremonyStepManagerFactory;
+ $ceremonyStepManagerFactory->setAlgorithmManager($algorithmManager);
+ $ceremonyStepManagerFactory->setAttestationStatementSupportManager(
+ $attestationStatementSupportManager
+ );
+ $ceremonyStepManagerFactory->setExtensionOutputCheckerHandler(
+ ExtensionOutputCheckerHandler::create()
+ );
+ $allowedOrigins = [];
+ if (App::environment('local', 'development')) {
+ $allowedOrigins = [config('app.url')];
+ }
+ $ceremonyStepManagerFactory->setAllowedOrigins($allowedOrigins);
+
+ $authenticatorAssertionResponseValidator = AuthenticatorAssertionResponseValidator::create(
+ ceremonyStepManager: $ceremonyStepManagerFactory->requestCeremony()
+ );
+
+ $publicKeyCredentialRequestOptions = $webauthnSerializer->deserialize(
+ $requestOptions,
+ PublicKeyCredentialRequestOptions::class,
+ 'json'
+ );
+
+ try {
+ $authenticatorAssertionResponseValidator->check(
+ publicKeyCredentialSource: $publicKeyCredentialSource,
+ authenticatorAssertionResponse: $publicKeyCredential->response,
+ publicKeyCredentialRequestOptions: $publicKeyCredentialRequestOptions,
+ host: config('app.url'),
+ userHandle: null,
+ );
+ } catch (Throwable) {
+ return response()->json([
+ 'success' => false,
+ 'message' => 'Passkey could not be verified',
+ ], 500);
+ }
+
+ $user = User::find($passkey->user_id);
+ Auth::login($user);
+
+ return response()->json([
+ 'success' => true,
+ 'message' => 'Passkey verified successfully',
+ ]);
+ }
+}
diff --git a/app/Http/Controllers/Admin/PlacesController.php b/app/Http/Controllers/Admin/PlacesController.php
index 9d1ef100..e5e82bcd 100644
--- a/app/Http/Controllers/Admin/PlacesController.php
+++ b/app/Http/Controllers/Admin/PlacesController.php
@@ -1,16 +1,18 @@
name;
- $data['description'] = $request->description;
- $data['latitude'] = $request->latitude;
- $data['longitude'] = $request->longitude;
- $place = $this->placeService->createPlace($data);
+ $this->placeService->createPlace(
+ request()->only([
+ 'name',
+ 'description',
+ 'latitude',
+ 'longitude',
+ ])
+ );
return redirect('/admin/places');
}
/**
* Display the form to edit a specific place.
- *
- * @param string The place id
- * @return \Illuminate\View\Factory view
*/
- public function edit($placeId)
+ public function edit(int $placeId): View
{
$place = Place::findOrFail($placeId);
- return view('admin.places.edit', [
- 'id' => $placeId,
- 'name' => $place->name,
- 'description' => $place->description,
- 'latitude' => $place->latitude,
- 'longitude' => $place->longitude,
- 'icon' => $place->icon ?? 'marker',
- ]);
+ return view('admin.places.edit', compact('place'));
}
/**
* Process a request to edit a place.
- *
- * @param string The place id
- * @param Illuminate\Http\Request $request
- * @return Illuminate\View\Factory view
*/
- public function update($placeId, Request $request)
+ public function update(int $placeId): RedirectResponse
{
$place = Place::findOrFail($placeId);
- $place->name = $request->name;
- $place->description = $request->description;
- $place->location = new Point((float) $request->latitude, (float) $request->longitude);
- $place->icon = $request->icon;
+ $place->name = request()->input('name');
+ $place->description = request()->input('description');
+ $place->latitude = request()->input('latitude');
+ $place->longitude = request()->input('longitude');
+ $place->icon = request()->input('icon');
$place->save();
return redirect('/admin/places');
@@ -98,14 +82,11 @@ class PlacesController extends Controller
/**
* List the places we can merge with the current place.
- *
- * @param string Place id
- * @return Illuminate\View\Factory view
*/
- public function mergeIndex($placeId)
+ public function mergeIndex(int $placeId): View
{
$first = Place::find($placeId);
- $results = Place::near(new Point($first->latitude, $first->longitude))->get();
+ $results = Place::near((object) ['latitude' => $first->latitude, 'longitude' => $first->longitude])->get();
$places = [];
foreach ($results as $place) {
if ($place->slug !== $first->slug) {
@@ -116,27 +97,33 @@ class PlacesController extends Controller
return view('admin.places.merge.index', compact('first', 'places'));
}
- public function mergeEdit($place1_id, $place2_id)
+ /**
+ * Show a form for merging two specific places.
+ */
+ public function mergeEdit(int $placeId1, int $placeId2): View
{
- $place1 = Place::find($place1_id);
- $place2 = Place::find($place2_id);
+ $place1 = Place::find($placeId1);
+ $place2 = Place::find($placeId2);
return view('admin.places.merge.edit', compact('place1', 'place2'));
}
- public function mergeStore(Request $request)
+ /**
+ * Process the request to merge two places.
+ */
+ public function mergeStore(): RedirectResponse
{
- $place1 = Place::find($request->input('place1'));
- $place2 = Place::find($request->input('place2'));
+ $place1 = Place::find(request()->input('place1'));
+ $place2 = Place::find(request()->input('place2'));
- if ($request->input('delete') === '1') {
+ if (request()->input('delete') === '1') {
foreach ($place1->notes as $note) {
$note->place()->dissociate();
$note->place()->associate($place2->id);
}
$place1->delete();
}
- if ($request->input('delete') === '2') {
+ if (request()->input('delete') === '2') {
foreach ($place2->notes as $note) {
$note->place()->dissociate();
$note->place()->associate($place1->id);
diff --git a/app/Http/Controllers/Admin/SyndicationTargetsController.php b/app/Http/Controllers/Admin/SyndicationTargetsController.php
new file mode 100644
index 00000000..dc14a2d2
--- /dev/null
+++ b/app/Http/Controllers/Admin/SyndicationTargetsController.php
@@ -0,0 +1,94 @@
+validate([
+ 'uid' => 'required|string',
+ 'name' => 'required|string',
+ 'service_name' => 'nullable|string',
+ 'service_url' => 'nullable|string',
+ 'service_photo' => 'nullable|string',
+ 'user_name' => 'nullable|string',
+ 'user_url' => 'nullable|string',
+ 'user_photo' => 'nullable|string',
+ ]);
+
+ SyndicationTarget::create($validated);
+
+ return redirect('/admin/syndication');
+ }
+
+ /**
+ * Show a form to edit a syndication target.
+ */
+ public function edit(SyndicationTarget $syndicationTarget): View
+ {
+ return view('admin.syndication.edit', [
+ 'syndication_target' => $syndicationTarget,
+ ]);
+ }
+
+ /**
+ * Process the request to edit a client name.
+ */
+ public function update(Request $request, SyndicationTarget $syndicationTarget): RedirectResponse
+ {
+ $validated = $request->validate([
+ 'uid' => 'required|string',
+ 'name' => 'required|string',
+ 'service_name' => 'nullable|string',
+ 'service_url' => 'nullable|string',
+ 'service_photo' => 'nullable|string',
+ 'user_name' => 'nullable|string',
+ 'user_url' => 'nullable|string',
+ 'user_photo' => 'nullable|string',
+ ]);
+
+ $syndicationTarget->update($validated);
+
+ return redirect('/admin/syndication');
+ }
+
+ /**
+ * Process a request to delete a client.
+ */
+ public function destroy(SyndicationTarget $syndicationTarget): RedirectResponse
+ {
+ $syndicationTarget->delete();
+
+ return redirect('/admin/syndication');
+ }
+}
diff --git a/app/Http/Controllers/ArticlesController.php b/app/Http/Controllers/ArticlesController.php
index 77a35ef5..9ab860d7 100644
--- a/app/Http/Controllers/ArticlesController.php
+++ b/app/Http/Controllers/ArticlesController.php
@@ -1,52 +1,58 @@
date($year, $month)
- ->orderBy('updated_at', 'desc')
- ->simplePaginate(5);
+ ->date($year, $month)
+ ->orderBy('updated_at', 'desc')
+ ->simplePaginate(5);
return view('articles.index', compact('articles'));
}
/**
* Show a single article.
- *
- * @return \Illuminate\View\Factory view
*/
- public function show($year, $month, $slug)
+ public function show(int $year, int $month, string $slug): RedirectResponse|View
{
- $article = Article::where('titleurl', $slug)->firstOrFail();
+ try {
+ $article = Article::where('titleurl', $slug)->firstOrFail();
+ } catch (ModelNotFoundException $exception) {
+ abort(404);
+ }
+
if ($article->updated_at->year != $year || $article->updated_at->month != $month) {
- throw new \Exception;
+ return redirect('/blog/'
+ . $article->updated_at->year
+ . '/' . $article->updated_at->format('m')
+ . '/' . $slug);
}
return view('articles.show', compact('article'));
}
/**
- * We only have the ID, work out post title, year and month
- * and redirect to it.
- *
- * @return \Illuminte\Routing\RedirectResponse redirect
+ * We only have the ID, work out post title, year and month and redirect to it.
*/
- public function onlyIdInUrl($inURLId)
+ public function onlyIdInUrl(string $idFromUrl): RedirectResponse
{
- $numbers = new Numbers();
- $realId = $numbers->b60tonum($inURLId);
+ $realId = resolve(Numbers::class)->b60tonum($idFromUrl);
+
$article = Article::findOrFail($realId);
return redirect($article->link);
diff --git a/app/Http/Controllers/Auth/ForgotPasswordController.php b/app/Http/Controllers/Auth/ForgotPasswordController.php
deleted file mode 100644
index 6a247fef..00000000
--- a/app/Http/Controllers/Auth/ForgotPasswordController.php
+++ /dev/null
@@ -1,32 +0,0 @@
-middleware('guest');
- }
-}
diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php
deleted file mode 100644
index b2ea669a..00000000
--- a/app/Http/Controllers/Auth/LoginController.php
+++ /dev/null
@@ -1,39 +0,0 @@
-middleware('guest')->except('logout');
- }
-}
diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php
deleted file mode 100644
index f77265ab..00000000
--- a/app/Http/Controllers/Auth/RegisterController.php
+++ /dev/null
@@ -1,71 +0,0 @@
-middleware('guest');
- }
-
- /**
- * Get a validator for an incoming registration request.
- *
- * @param array $data
- * @return \Illuminate\Contracts\Validation\Validator
- */
- protected function validator(array $data)
- {
- return Validator::make($data, [
- 'name' => 'required|string|max:255',
- 'email' => 'required|string|email|max:255|unique:users',
- 'password' => 'required|string|min:6|confirmed',
- ]);
- }
-
- /**
- * Create a new user instance after a valid registration.
- *
- * @param array $data
- * @return \App\User
- */
- protected function create(array $data)
- {
- return User::create([
- 'name' => $data['name'],
- 'email' => $data['email'],
- 'password' => bcrypt($data['password']),
- ]);
- }
-}
diff --git a/app/Http/Controllers/Auth/ResetPasswordController.php b/app/Http/Controllers/Auth/ResetPasswordController.php
deleted file mode 100644
index cf726eec..00000000
--- a/app/Http/Controllers/Auth/ResetPasswordController.php
+++ /dev/null
@@ -1,39 +0,0 @@
-middleware('guest');
- }
-}
diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php
index b58ed184..bd0022d6 100644
--- a/app/Http/Controllers/AuthController.php
+++ b/app/Http/Controllers/AuthController.php
@@ -1,34 +1,62 @@
input('username') === config('admin.user')
- &&
- $request->input('password') === config('admin.pass')
- ) {
- session(['loggedin' => true]);
+ $credentials = $request->only('name', 'password');
- return redirect()->intended('admin');
+ if (Auth::attempt($credentials, true)) {
+ return redirect()->intended('/admin');
}
return redirect()->route('login');
}
+
+ /**
+ * Show the form to allow a user to log-out.
+ */
+ public function showLogout(): View|RedirectResponse
+ {
+ if (Auth::check() === false) {
+ // The user is not logged in, just redirect them home
+ return redirect('/');
+ }
+
+ return view('logout');
+ }
+
+ /**
+ * Log the user out from their current session.
+ */
+ public function logout(): RedirectResponse
+ {
+ Auth::logout();
+
+ return redirect('/');
+ }
}
diff --git a/app/Http/Controllers/BookmarksController.php b/app/Http/Controllers/BookmarksController.php
index d2d66888..b4bb3c13 100644
--- a/app/Http/Controllers/BookmarksController.php
+++ b/app/Http/Controllers/BookmarksController.php
@@ -1,22 +1,43 @@
with('tags')->withCount('tags')->paginate(10);
return view('bookmarks.index', compact('bookmarks'));
}
- public function show(Bookmark $bookmark)
+ /**
+ * Show a single bookmark.
+ */
+ public function show(Bookmark $bookmark): View
{
$bookmark->loadMissing('tags');
return view('bookmarks.show', compact('bookmark'));
}
+
+ /**
+ * Show bookmarks tagged with a specific tag.
+ */
+ public function tagged(string $tag): View
+ {
+ $bookmarks = Bookmark::whereHas('tags', function ($query) use ($tag) {
+ $query->where('tag', $tag);
+ })->latest()->with('tags')->withCount('tags')->paginate(10);
+
+ return view('bookmarks.tagged', compact('bookmarks', 'tag'));
+ }
}
diff --git a/app/Http/Controllers/ContactsController.php b/app/Http/Controllers/ContactsController.php
index 6d07234f..280cc3ed 100644
--- a/app/Http/Controllers/ContactsController.php
+++ b/app/Http/Controllers/ContactsController.php
@@ -1,20 +1,21 @@
homepageHost = parse_url($contact->homepage, PHP_URL_HOST);
@@ -30,15 +31,13 @@ class ContactsController extends Controller
/**
* Show a single contact.
- *
- * @return \Illuminate\View\Factory view
*/
- public function show($nick)
+ public function show(Contact $contact): View
{
- $filesystem = new Filesystem();
- $contact = Contact::where('nick', '=', $nick)->firstOrFail();
$contact->homepageHost = parse_url($contact->homepage, PHP_URL_HOST);
$file = public_path() . '/assets/profile-images/' . $contact->homepageHost . '/image';
+
+ $filesystem = new Filesystem;
$image = ($filesystem->exists($file)) ?
'/assets/profile-images/' . $contact->homepageHost . '/image'
:
diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php
index 03e02a23..8677cd5c 100644
--- a/app/Http/Controllers/Controller.php
+++ b/app/Http/Controllers/Controller.php
@@ -2,12 +2,7 @@
namespace App\Http\Controllers;
-use Illuminate\Foundation\Bus\DispatchesJobs;
-use Illuminate\Routing\Controller as BaseController;
-use Illuminate\Foundation\Validation\ValidatesRequests;
-use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
-
-class Controller extends BaseController
+abstract class Controller
{
- use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
+ //
}
diff --git a/app/Http/Controllers/FeedsController.php b/app/Http/Controllers/FeedsController.php
index 574ead24..eb0847a3 100644
--- a/app/Http/Controllers/FeedsController.php
+++ b/app/Http/Controllers/FeedsController.php
@@ -1,83 +1,85 @@
latest('updated_at')->take(20)->get();
$buildDate = $articles->first()->updated_at->toRssString();
return response()
- ->view('articles.rss', compact('articles', 'buildDate'))
- ->header('Content-Type', 'application/rss+xml; charset=utf-8');
+ ->view('articles.rss', compact('articles', 'buildDate'))
+ ->header('Content-Type', 'application/rss+xml; charset=utf-8');
}
/**
* Returns the blog Atom feed.
- *
- * @return \Illuminate\Http\Response
*/
- public function blogAtom()
+ public function blogAtom(): Response
{
$articles = Article::where('published', '1')->latest('updated_at')->take(20)->get();
return response()
- ->view('articles.atom', compact('articles'))
- ->header('Content-Type', 'application/atom+xml; charset=utf-8');
+ ->view('articles.atom', compact('articles'))
+ ->header('Content-Type', 'application/atom+xml; charset=utf-8');
}
/**
* Returns the notes RSS feed.
- *
- * @return \Illuminate\Http\Response
*/
- public function notesRss()
+ public function notesRss(): Response
{
$notes = Note::latest()->take(20)->get();
$buildDate = $notes->first()->updated_at->toRssString();
return response()
- ->view('notes.rss', compact('notes', 'buildDate'))
- ->header('Content-Type', 'application/rss+xml; charset=utf-8');
+ ->view('notes.rss', compact('notes', 'buildDate'))
+ ->header('Content-Type', 'application/rss+xml; charset=utf-8');
}
/**
* Returns the notes Atom feed.
- *
- * @return \Illuminate\Http\Response
*/
- public function notesAtom()
+ public function notesAtom(): Response
{
$notes = Note::latest()->take(20)->get();
return response()
- ->view('notes.atom', compact('notes'))
- ->header('Content-Type', 'application/atom+xml; charset=utf-8');
+ ->view('notes.atom', compact('notes'))
+ ->header('Content-Type', 'application/atom+xml; charset=utf-8');
}
+ /** @todo sort out return type for json responses */
+
/**
* Returns the blog JSON feed.
- *
- * @return \Illuminate\Http\response
*/
- public function blogJson()
+ public function blogJson(): array
{
$articles = Article::where('published', '1')->latest('updated_at')->take(20)->get();
$data = [
- 'version' => 'https://jsonfeed.org/version/1',
- 'title' => 'The JSON Feed for ' . config('app.display_name') . '’s blog',
+ 'version' => 'https://jsonfeed.org/version/1.1',
+ 'title' => 'The JSON Feed for ' . config('user.display_name') . '’s blog',
'home_page_url' => config('app.url') . '/blog',
'feed_url' => config('app.url') . '/blog/feed.json',
+ 'authors' => [
+ [
+ 'name' => config('user.display_name'),
+ 'url' => config('app.url'),
+ ],
+ ],
'items' => [],
];
@@ -89,9 +91,6 @@ class FeedsController extends Controller
'content_html' => $article->main,
'date_published' => $article->created_at->tz('UTC')->toRfc3339String(),
'date_modified' => $article->updated_at->tz('UTC')->toRfc3339String(),
- 'author' => [
- 'name' => config('app.display_name'),
- ],
];
}
@@ -100,34 +99,109 @@ class FeedsController extends Controller
/**
* Returns the notes JSON feed.
- *
- * @return \Illuminate\Http\response
*/
- public function notesJson()
+ public function notesJson(): array
{
- $notes = Note::latest()->take(20)->get();
+ $notes = Note::latest()->with('media', 'place', 'tags')->take(20)->get();
$data = [
- 'version' => 'https://jsonfeed.org/version/1',
- 'title' => 'The JSON Feed for ' . config('app.display_name') . '’s notes',
+ 'version' => 'https://jsonfeed.org/version/1.1',
+ 'title' => 'The JSON Feed for ' . config('user.display_name') . '’s notes',
'home_page_url' => config('app.url') . '/notes',
'feed_url' => config('app.url') . '/notes/feed.json',
+ 'authors' => [
+ [
+ 'name' => config('user.display_name'),
+ 'url' => config('app.url'),
+ ],
+ ],
'items' => [],
];
foreach ($notes as $key => $note) {
$data['items'][$key] = [
- 'id' => $note->longurl,
- 'title' => $note->getOriginal('note'),
- 'url' => $note->longurl,
- 'content_html' => $note->note,
+ 'id' => $note->uri,
+ 'url' => $note->uri,
+ 'content_text' => $note->content,
'date_published' => $note->created_at->tz('UTC')->toRfc3339String(),
'date_modified' => $note->updated_at->tz('UTC')->toRfc3339String(),
- 'author' => [
- 'name' => config('app.display_name'),
- ],
];
+ if ($note->tags->count() > 0) {
+ $data['items'][$key]['tags'] = implode(',', $note->tags->pluck('tag')->toArray());
+ }
}
return $data;
}
+
+ /**
+ * Returns the blog JF2 feed.
+ */
+ public function blogJf2(): JsonResponse
+ {
+ $articles = Article::where('published', '1')->latest('updated_at')->take(20)->get();
+ $items = [];
+ foreach ($articles as $article) {
+ $items[] = [
+ 'type' => 'entry',
+ 'published' => $article->created_at,
+ 'uid' => config('app.url') . $article->link,
+ 'url' => config('app.url') . $article->link,
+ 'content' => [
+ 'text' => $article->main,
+ 'html' => $article->html,
+ ],
+ 'post-type' => 'article',
+ ];
+ }
+
+ return response()->json([
+ 'type' => 'feed',
+ 'name' => 'Blog feed for ' . config('app.name'),
+ 'url' => url('/blog'),
+ 'author' => [
+ 'type' => 'card',
+ 'name' => config('user.display_name'),
+ 'url' => config('app.url'),
+ ],
+ 'children' => $items,
+ ], 200, [
+ 'Content-Type' => 'application/jf2feed+json',
+ ]);
+ }
+
+ /**
+ * Returns the notes JF2 feed.
+ */
+ public function notesJf2(): JsonResponse
+ {
+ $notes = Note::latest()->take(20)->get();
+ $items = [];
+ foreach ($notes as $note) {
+ $items[] = [
+ 'type' => 'entry',
+ 'published' => $note->created_at,
+ 'uid' => $note->uri,
+ 'url' => $note->uri,
+ 'content' => [
+ 'text' => $note->getRawOriginal('note'),
+ 'html' => $note->note,
+ ],
+ 'post-type' => 'note',
+ ];
+ }
+
+ return response()->json([
+ 'type' => 'feed',
+ 'name' => 'Notes feed for ' . config('app.name'),
+ 'url' => url('/notes'),
+ 'author' => [
+ 'type' => 'card',
+ 'name' => config('user.display_name'),
+ 'url' => config('app.url'),
+ ],
+ 'children' => $items,
+ ], 200, [
+ 'Content-Type' => 'application/jf2feed+json',
+ ]);
+ }
}
diff --git a/app/Http/Controllers/FrontPageController.php b/app/Http/Controllers/FrontPageController.php
new file mode 100644
index 00000000..19537663
--- /dev/null
+++ b/app/Http/Controllers/FrontPageController.php
@@ -0,0 +1,47 @@
+with(['media', 'client', 'place'])->withCount(['webmentions AS replies' => function ($query) {
+ $query->where('type', 'in-reply-to');
+ }])
+ ->withCount(['webmentions AS likes' => function ($query) {
+ $query->where('type', 'like-of');
+ }])
+ ->withCount(['webmentions AS reposts' => function ($query) {
+ $query->where('type', 'repost-of');
+ }])->get();
+ $articles = Article::latest()->get();
+ $bookmarks = Bookmark::latest()->with('tags')->get();
+ $likes = Like::latest()->get();
+
+ $items = collect($notes)
+ ->merge($articles)
+ ->merge($bookmarks)
+ ->merge($likes)
+ ->sortByDesc('updated_at')
+ ->paginate(10);
+
+ $bio = Bio::first()?->content;
+
+ return view('front-page', [
+ 'items' => $items,
+ 'bio' => $bio,
+ ]);
+ }
+}
diff --git a/app/Http/Controllers/IndieAuthController.php b/app/Http/Controllers/IndieAuthController.php
new file mode 100644
index 00000000..45b488da
--- /dev/null
+++ b/app/Http/Controllers/IndieAuthController.php
@@ -0,0 +1,327 @@
+json([
+ 'issuer' => config('app.url'),
+ 'authorization_endpoint' => route('indieauth.start'),
+ 'token_endpoint' => route('indieauth.token'),
+ 'code_challenge_methods_supported' => ['S256'],
+ // 'introspection_endpoint' => route('indieauth.introspection'),
+ // 'introspection_endpoint_auth_methods_supported' => ['none'],
+ ]);
+ }
+
+ /**
+ * Process a GET request to the IndieAuth endpoint.
+ *
+ * This is the first step in the IndieAuth flow, where the client app sends the user to the IndieAuth endpoint.
+ */
+ public function start(Request $request): View
+ {
+ // First check all required params are present
+ $validator = Validator::make($request->all(), [
+ 'response_type' => 'required:string',
+ 'client_id' => 'required',
+ 'redirect_uri' => 'required',
+ 'state' => 'required',
+ 'code_challenge' => 'required:string',
+ 'code_challenge_method' => 'required:string',
+ ], [
+ 'response_type' => 'response_type is required',
+ 'client_id.required' => 'client_id is required to display which app is asking for authentication',
+ 'redirect_uri.required' => 'redirect_uri is required so we can progress successful requests',
+ 'state.required' => 'state is required',
+ 'code_challenge.required' => 'code_challenge is required',
+ 'code_challenge_method.required' => 'code_challenge_method is required',
+ ]);
+
+ if ($validator->fails()) {
+ return view('indieauth.error')->withErrors($validator);
+ }
+
+ if ($request->get('response_type') !== 'code') {
+ return view('indieauth.error')->withErrors(['response_type' => 'only a response_type of "code" is supported']);
+ }
+
+ if (mb_strtoupper($request->get('code_challenge_method')) !== 'S256') {
+ return view('indieauth.error')->withErrors(['code_challenge_method' => 'only a code_challenge_method of "S256" is supported']);
+ }
+
+ if (! $this->isValidRedirectUri($request->get('client_id'), $request->get('redirect_uri'))) {
+ return view('indieauth.error')->withErrors(['redirect_uri' => 'redirect_uri is not valid for this client_id']);
+ }
+
+ $scopes = $request->get('scope', '');
+ $scopes = explode(' ', $scopes);
+
+ return view('indieauth.start', [
+ 'me' => $request->get('me'),
+ 'client_id' => $request->get('client_id'),
+ 'redirect_uri' => $request->get('redirect_uri'),
+ 'state' => $request->get('state'),
+ 'scopes' => $scopes,
+ 'code_challenge' => $request->get('code_challenge'),
+ 'code_challenge_method' => $request->get('code_challenge_method'),
+ ]);
+ }
+
+ /**
+ * Confirm an IndieAuth approval request.
+ *
+ * Generates an auth code and redirects the user back to the client app.
+ *
+ * @throws RandomException
+ */
+ public function confirm(Request $request): RedirectResponse
+ {
+ $authCode = bin2hex(random_bytes(16));
+
+ $cacheKey = hash('xxh3', $request->get('client_id'));
+
+ $indieAuthRequestData = [
+ 'code_challenge' => $request->get('code_challenge'),
+ 'code_challenge_method' => $request->get('code_challenge_method'),
+ 'client_id' => $request->get('client_id'),
+ 'redirect_uri' => $request->get('redirect_uri'),
+ 'auth_code' => $authCode,
+ 'scope' => implode(' ', $request->get('scope', '')),
+ ];
+
+ Cache::put($cacheKey, $indieAuthRequestData, now()->addMinutes(10));
+
+ $redirectUri = new Uri($request->get('redirect_uri'));
+ $redirectUri = Uri::withQueryValues($redirectUri, [
+ 'code' => $authCode,
+ 'state' => $request->get('state'),
+ 'iss' => config('app.url'),
+ ]);
+
+ return redirect()->away($redirectUri);
+ }
+
+ /**
+ * Process a POST request to the IndieAuth auth endpoint.
+ *
+ * This is one possible second step in the IndieAuth flow, where the client app sends the auth code to the IndieAuth
+ * endpoint. As it is to the auth endpoint we return profile information. A similar request can be made to the token
+ * endpoint to get an access token.
+ */
+ public function processCodeExchange(Request $request): JsonResponse
+ {
+ $invalidCodeResponse = $this->validateAuthorizationCode($request);
+
+ if ($invalidCodeResponse instanceof JsonResponse) {
+ return $invalidCodeResponse;
+ }
+
+ return response()->json([
+ 'me' => config('app.url'),
+ ]);
+ }
+
+ /**
+ * Process a POST request to the IndieAuth token endpoint.
+ *
+ * This is another possible second step in the IndieAuth flow, where the client app sends the auth code to the
+ * IndieAuth token endpoint. As it is to the token endpoint we return an access token.
+ *
+ * @throws SodiumException
+ */
+ public function processTokenRequest(Request $request): JsonResponse
+ {
+ $indieAuthData = $this->validateAuthorizationCode($request);
+
+ if ($indieAuthData instanceof JsonResponse) {
+ return $indieAuthData;
+ }
+
+ if ($indieAuthData['scope'] === '') {
+ return response()->json(['errors' => [
+ 'scope' => [
+ 'The scope property must be non-empty for an access token to be issued.',
+ ],
+ ]], 400);
+ }
+
+ $tokenData = [
+ 'me' => config('app.url'),
+ 'client_id' => $request->get('client_id'),
+ 'scope' => $indieAuthData['scope'],
+ ];
+ $tokenService = resolve(TokenService::class);
+ $token = $tokenService->getNewToken($tokenData);
+
+ return response()->json([
+ 'access_token' => $token,
+ 'token_type' => 'Bearer',
+ 'scope' => $indieAuthData['scope'],
+ 'me' => config('app.url'),
+ ]);
+ }
+
+ protected function isValidRedirectUri(string $clientId, string $redirectUri): bool
+ {
+ // If client_id is not a valid URL, then it's not valid
+ $clientIdParsed = \Mf2\parseUriToComponents($clientId);
+ if (! isset($clientIdParsed['authority'])) {
+ return false;
+ }
+
+ // If redirect_uri is not a valid URL, then it's not valid
+ $redirectUriParsed = \Mf2\parseUriToComponents($redirectUri);
+ if (! isset($redirectUriParsed['authority'])) {
+ return false;
+ }
+
+ // If client_id and redirect_uri are the same host, then it's valid
+ if ($clientIdParsed['authority'] === $redirectUriParsed['authority']) {
+ return true;
+ }
+
+ // Otherwise we need to check the redirect_uri is in the client_id's redirect_uris
+ $guzzle = resolve(Client::class);
+
+ try {
+ $clientInfo = $guzzle->get($clientId);
+ } catch (Exception) {
+ return false;
+ }
+
+ $clientInfoParsed = \Mf2\parse($clientInfo->getBody()->getContents(), $clientId);
+
+ $redirectUris = $clientInfoParsed['rels']['redirect_uri'] ?? [];
+
+ return in_array($redirectUri, $redirectUris, true);
+ }
+
+ /**
+ * @throws SodiumException
+ */
+ protected function validateAuthorizationCode(Request $request): JsonResponse|array
+ {
+ // First check all the data is present
+ $validator = Validator::make($request->all(), [
+ 'grant_type' => 'required:string',
+ 'code' => 'required:string',
+ 'client_id' => 'required',
+ 'redirect_uri' => 'required',
+ 'code_verifier' => 'required',
+ ]);
+
+ if ($validator->fails()) {
+ return response()->json(['errors' => $validator->errors()], 400);
+ }
+
+ if ($request->get('grant_type') !== 'authorization_code') {
+ return response()->json(['errors' => [
+ 'grant_type' => [
+ 'Only a grant type of "authorization_code" is supported.',
+ ],
+ ]], 400);
+ }
+
+ // Check cache for auth code
+ $cacheKey = hash('xxh3', $request->get('client_id'));
+ $indieAuthRequestData = Cache::pull($cacheKey);
+
+ if ($indieAuthRequestData === null) {
+ return response()->json(['errors' => [
+ 'code' => [
+ 'The code is invalid.',
+ ],
+ ]], 404);
+ }
+
+ // Check the IndieAuth code
+ if (! array_key_exists('auth_code', $indieAuthRequestData)) {
+ return response()->json(['errors' => [
+ 'code' => [
+ 'The code is invalid.',
+ ],
+ ]], 400);
+ }
+ if ($indieAuthRequestData['auth_code'] !== $request->get('code')) {
+ return response()->json(['errors' => [
+ 'code' => [
+ 'The code is invalid.',
+ ],
+ ]], 400);
+ }
+
+ // Check code verifier
+ if (! array_key_exists('code_challenge', $indieAuthRequestData)) {
+ return response()->json(['errors' => [
+ 'code_verifier' => [
+ 'The code verifier is invalid.',
+ ],
+ ]], 400);
+ }
+ if (! hash_equals(
+ $indieAuthRequestData['code_challenge'],
+ sodium_bin2base64(
+ hash('sha256', $request->get('code_verifier'), true),
+ SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING
+ )
+ )) {
+ return response()->json(['errors' => [
+ 'code_verifier' => [
+ 'The code verifier is invalid.',
+ ],
+ ]], 400);
+ }
+
+ // Check redirect_uri
+ if (! array_key_exists('redirect_uri', $indieAuthRequestData)) {
+ return response()->json(['errors' => [
+ 'redirect_uri' => [
+ 'The redirect uri is invalid.',
+ ],
+ ]], 400);
+ }
+ if ($indieAuthRequestData['redirect_uri'] !== $request->get('redirect_uri')) {
+ return response()->json(['errors' => [
+ 'redirect_uri' => [
+ 'The redirect uri is invalid.',
+ ],
+ ]], 400);
+ }
+
+ // Check client_id
+ if (! array_key_exists('client_id', $indieAuthRequestData)) {
+ return response()->json(['errors' => [
+ 'client_id' => [
+ 'The client id is invalid.',
+ ],
+ ]], 400);
+ }
+ if ($indieAuthRequestData['client_id'] !== $request->get('client_id')) {
+ return response()->json(['errors' => [
+ 'client_id' => [
+ 'The client id is invalid.',
+ ],
+ ]], 400);
+ }
+
+ return $indieAuthRequestData;
+ }
+}
diff --git a/app/Http/Controllers/LikesController.php b/app/Http/Controllers/LikesController.php
index 02922e86..af1c483c 100644
--- a/app/Http/Controllers/LikesController.php
+++ b/app/Http/Controllers/LikesController.php
@@ -1,19 +1,28 @@
paginate(20);
return view('likes.index', compact('likes'));
}
- public function show(Like $like)
+ /**
+ * Show a single like.
+ */
+ public function show(Like $like): View
{
return view('likes.show', compact('like'));
}
diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php
index e1877bd3..758b3255 100644
--- a/app/Http/Controllers/MicropubController.php
+++ b/app/Http/Controllers/MicropubController.php
@@ -1,370 +1,112 @@
tokenService = $tokenService;
- $this->noteService = $noteService;
- $this->placeService = $placeService;
+ public function __construct(MicropubHandlerRegistry $handlerRegistry)
+ {
+ $this->handlerRegistry = $handlerRegistry;
}
/**
- * This function receives an API request, verifies the authenticity
- * then passes over the info to the relavent Service class.
+ * Respond to a POST request to the micropub endpoint.
*
- * @param \Illuminate\Http\Request request
- * @return \Illuminate\Http\Response
+ * The request is initially processed by the MicropubRequest form request
+ * class. The normalizes the data, so we can pass it into the handlers for
+ * the different micropub requests, h-entry or h-card, for example.
*/
- public function post(Request $request)
+ public function post(MicropubRequest $request): JsonResponse
{
- try {
- $tokenData = $this->tokenService->validateToken($request->bearerToken());
- } catch (InvalidTokenException $e) {
+ $type = $request->getType();
+
+ if (! $type) {
return response()->json([
- 'response' => 'error',
- 'error' => 'invalid_token',
- 'error_description' => 'The provided token did not pass validation',
+ 'error' => 'invalid_request',
+ 'error_description' => 'Microformat object type is missing, for example: h-entry or h-card',
], 400);
}
- // Log the request
- $logger = new Logger('micropub');
- $logger->pushHandler(new StreamHandler(storage_path('logs/micropub.log')), Logger::DEBUG);
- $logger->debug('MicropubLog', $request->all());
- if ($tokenData->hasClaim('scope')) {
- if (($request->input('h') == 'entry') || ($request->input('type.0') == 'h-entry')) {
- if (stristr($tokenData->getClaim('scope'), 'create') === false) {
- return $this->returnInsufficientScopeResponse();
- }
- if ($request->has('properties.like-of') || $request->has('like-of')) {
- $like = (new LikeService())->createLike($request);
- return response()->json([
- 'response' => 'created',
- 'location' => config('app.url') . "/likes/$like->id",
- ], 201)->header('Location', config('app.url') . "/likes/$like->id");
- }
- if ($request->has('properties.bookmark-of') || $request->has('bookmark-of')) {
- $bookmark = (new BookmarkService())->createBookmark($request);
+ try {
+ $handler = $this->handlerRegistry->getHandler($type);
+ $result = $handler->handle($request->getMicropubData());
- return response()->json([
- 'response' => 'created',
- 'location' => config('app.url') . "/bookmarks/$bookmark->id",
- ], 201)->header('Location', config('app.url') . "/bookmarks/$bookmark->id");
- }
- $data = [];
- $data['client-id'] = $tokenData->getClaim('client_id');
- if ($request->header('Content-Type') == 'application/json') {
- if (is_string($request->input('properties.content.0'))) {
- $data['content'] = $request->input('properties.content.0'); //plaintext content
- }
- if (is_array($request->input('properties.content.0'))
- && array_key_exists('html', $request->input('properties.content.0'))
- ) {
- $data['content'] = $request->input('properties.content.0.html');
- }
- $data['in-reply-to'] = $request->input('properties.in-reply-to.0');
- // check location is geo: string
- if (is_string($request->input('properties.location.0'))) {
- $data['location'] = $request->input('properties.location.0');
- }
- // check location is h-card
- if (is_array($request->input('properties.location.0'))) {
- if ($request->input('properties.location.0.type.0' === 'h-card')) {
- try {
- $place = $this->placeService->createPlaceFromCheckin(
- $request->input('properties.location.0')
- );
- $data['checkin'] = $place->longurl;
- } catch (\Exception $e) {
- //
- }
- }
- }
- $data['published'] = $request->input('properties.published.0');
- //create checkin place
- if (array_key_exists('checkin', $request->input('properties'))) {
- $data['swarm-url'] = $request->input('properties.syndication.0');
- try {
- $place = $this->placeService->createPlaceFromCheckin(
- $request->input('properties.checkin.0')
- );
- $data['checkin'] = $place->longurl;
- } catch (\Exception $e) {
- $data['checkin'] = null;
- $data['swarm-url'] = null;
- }
- }
- } else {
- $data['content'] = $request->input('content');
- $data['in-reply-to'] = $request->input('in-reply-to');
- $data['location'] = $request->input('location');
- $data['published'] = $request->input('published');
- }
- $data['syndicate'] = [];
- $targets = array_pluck(config('syndication.targets'), 'uid', 'service.name');
- $mpSyndicateTo = null;
- if ($request->has('mp-syndicate-to')) {
- $mpSyndicateTo = $request->input('mp-syndicate-to');
- }
- if ($request->has('properties.mp-syndicate-to')) {
- $mpSyndicateTo = $request->input('properties.mp-syndicate-to');
- }
- if (is_string($mpSyndicateTo)) {
- $service = array_search($mpSyndicateTo, $targets);
- if ($service == 'Twitter') {
- $data['syndicate'][] = 'twitter';
- }
- if ($service == 'Facebook') {
- $data['syndicate'][] = 'facebook';
- }
- }
- if (is_array($mpSyndicateTo)) {
- foreach ($mpSyndicateTo as $uid) {
- $service = array_search($uid, $targets);
- if ($service == 'Twitter') {
- $data['syndicate'][] = 'twitter';
- }
- if ($service == 'Facebook') {
- $data['syndicate'][] = 'facebook';
- }
- }
- }
- $data['photo'] = [];
- $photos = null;
- if ($request->has('photo')) {
- $photos = $request->input('photo');
- }
- if ($request->has('properties.photo')) {
- $photos = $request->input('properties.photo');
- }
- if ($photos !== null) {
- foreach ($photos as $photo) {
- if (is_string($photo)) {
- //only supporting media URLs for now
- $data['photo'][] = $photo;
- }
- }
- if (starts_with($request->input('properties.syndication.0'), 'https://www.instagram.com')) {
- $data['instagram-url'] = $request->input('properties.syndication.0');
- }
- }
- try {
- $note = $this->noteService->createNote($data);
- } catch (\Exception $exception) {
- return response()->json(['error' => true], 400);
- }
-
- return response()->json([
- 'response' => 'created',
- 'location' => $note->longurl,
- ], 201)->header('Location', $note->longurl);
- }
- if ($request->input('h') == 'card' || $request->input('type')[0] == 'h-card') {
- if (stristr($tokenData->getClaim('scope'), 'create') === false) {
- return $this->returnInsufficientScopeResponse();
- }
- $data = [];
- if ($request->header('Content-Type') == 'application/json') {
- $data['name'] = $request->input('properties.name');
- $data['description'] = $request->input('properties.description') ?? null;
- if ($request->has('properties.geo')) {
- $data['geo'] = $request->input('properties.geo');
- }
- } else {
- $data['name'] = $request->input('name');
- $data['description'] = $request->input('description');
- if ($request->has('geo')) {
- $data['geo'] = $request->input('geo');
- }
- if ($request->has('latitude')) {
- $data['latitude'] = $request->input('latitude');
- $data['longitude'] = $request->input('longitude');
- }
- }
- try {
- $place = $this->placeService->createPlace($data);
- } catch (\Exception $exception) {
- return response()->json(['error' => true], 400);
- }
-
- return response()->json([
- 'response' => 'created',
- 'location' => $place->longurl,
- ], 201)->header('Location', $place->longurl);
- }
- if ($request->input('action') == 'update') {
- if (stristr($tokenData->getClaim('scope'), 'update') === false) {
- return $this->returnInsufficientScopeResponse();
- }
- $urlPath = parse_url($request->input('url'), PHP_URL_PATH);
- //is it a note we are updating?
- if (mb_substr($urlPath, 1, 5) === 'notes') {
- try {
- $note = Note::nb60(basename($urlPath))->firstOrFail();
- } catch (ModelNotFoundException $exception) {
- return response()->json([
- 'error' => 'invalid_request',
- 'error_description' => 'No known note with given ID',
- ]);
- }
- //got the note, are we dealing with a “replace” request?
- if ($request->has('replace')) {
- foreach ($request->input('replace') as $property => $value) {
- if ($property == 'content') {
- $note->note = $value[0];
- }
- if ($property == 'syndication') {
- foreach ($value as $syndicationURL) {
- if (starts_with($syndicationURL, 'https://www.facebook.com')) {
- $note->facebook_url = $syndicationURL;
- }
- if (starts_with($syndicationURL, 'https://www.swarmapp.com')) {
- $note->swarm_url = $syndicationURL;
- }
- if (starts_with($syndicationURL, 'https://twitter.com')) {
- $note->tweet_id = basename(parse_url($syndicationURL, PHP_URL_PATH));
- }
- }
- }
- }
- $note->save();
-
- return response()->json([
- 'response' => 'updated',
- ]);
- }
- //how about “add”
- if ($request->has('add')) {
- foreach ($request->input('add') as $property => $value) {
- if ($property == 'syndication') {
- foreach ($value as $syndicationURL) {
- if (starts_with($syndicationURL, 'https://www.facebook.com')) {
- $note->facebook_url = $syndicationURL;
- }
- if (starts_with($syndicationURL, 'https://www.swarmapp.com')) {
- $note->swarm_url = $syndicationURL;
- }
- if (starts_with($syndicationURL, 'https://twitter.com')) {
- $note->tweet_id = basename(parse_url($syndicationURL, PHP_URL_PATH));
- }
- }
- }
- if ($property == 'photo') {
- foreach ($value as $photoURL) {
- if (start_with($photo, 'https://')) {
- $media = new Media();
- $media->path = $photoURL;
- $media->type = 'image';
- $media->save();
- $note->media()->save($media);
- }
- }
- }
- }
- $note->save();
-
- return response()->json([
- 'response' => 'updated',
- ]);
- }
- }
- }
+ // Return appropriate response based on the handler result
+ return response()->json([
+ 'response' => $result['response'],
+ 'location' => $result['url'] ?? null,
+ ], 201)->header('Location', $result['url']);
+ } catch (\InvalidArgumentException $e) {
+ return response()->json([
+ 'error' => 'invalid_request',
+ 'error_description' => $e->getMessage(),
+ ], 400);
+ } catch (MicropubHandlerException) {
+ return response()->json([
+ 'error' => 'Unknown Micropub type',
+ 'error_description' => 'The request could not be processed by this server',
+ ], 500);
+ } catch (InvalidTokenScopeException) {
+ return response()->json([
+ 'error' => 'invalid_scope',
+ 'error_description' => 'The token does not have the required scope for this request',
+ ], 403);
+ } catch (\Exception) {
+ return response()->json([
+ 'error' => 'server_error',
+ 'error_description' => 'An error occurred processing the request',
+ ], 500);
}
-
- return response()->json([
- 'response' => 'error',
- 'error' => 'forbidden',
- 'error_description' => 'The token has no scopes',
- ], 403);
}
/**
+ * Respond to a GET request to the micropub endpoint.
+ *
* A GET request has been made to `api/post` with an accompanying
- * token, here we check wether the token is valid and respond
+ * token, here we check whether the token is valid and respond
* appropriately. Further if the request has the query parameter
- * synidicate-to we respond with the known syndication endpoints.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
+ * syndicate-to we respond with the known syndication endpoints.
*/
- public function get(Request $request)
+ public function get(Request $request): JsonResponse
{
- try {
- $tokenData = $this->tokenService->validateToken($request->bearerToken());
- } catch (InvalidTokenException $e) {
- return response()->json([
- 'response' => 'error',
- 'error' => 'invalid_token',
- 'error_description' => 'The provided token did not pass validation',
- ], 400);
- }
- //we have a valid token, is `syndicate-to` set?
if ($request->input('q') === 'syndicate-to') {
return response()->json([
- 'syndicate-to' => config('syndication.targets'),
+ 'syndicate-to' => SyndicationTarget::all(),
]);
}
- //nope, how about a config query?
- if ($request->input('q') == 'config') {
+ if ($request->input('q') === 'config') {
return response()->json([
- 'syndicate-to' => config('syndication.targets'),
+ 'syndicate-to' => SyndicationTarget::all(),
'media-endpoint' => route('media-endpoint'),
]);
}
- //nope, how about a geo URL?
- if (substr($request->input('q'), 0, 4) === 'geo:') {
+ if ($request->has('q') && str_starts_with($request->input('q'), 'geo:')) {
preg_match_all(
- '/([0-9\.\-]+)/',
+ '/([0-9.\-]+)/',
$request->input('q'),
$matches
);
- $distance = (count($matches[0]) == 3) ? 100 * $matches[0][2] : 1000;
- $places = Place::near(new Point($matches[0][0], $matches[0][1]))->get();
- foreach ($places as $place) {
- $place->uri = config('app.url') . '/places/' . $place->slug;
- }
+ $distance = (count($matches[0]) === 3) ? 100 * $matches[0][2] : 1000;
+ $places = Place::near(
+ (object) ['latitude' => $matches[0][0], 'longitude' => $matches[0][1]],
+ $distance
+ )->get();
return response()->json([
'response' => 'places',
@@ -372,155 +114,17 @@ class MicropubController extends Controller
]);
}
- //nope, just return the token
+ // the default response is just to return the token data
+ /** @var Token $tokenData */
+ $tokenData = $request->input('token_data');
+
return response()->json([
'response' => 'token',
'token' => [
- 'me' => $tokenData->getClaim('me'),
- 'scope' => $tokenData->getClaim('scope'),
- 'client_id' => $tokenData->getClaim('client_id'),
+ 'me' => $tokenData['me'],
+ 'scope' => $tokenData['scope'],
+ 'client_id' => $tokenData['client_id'],
],
]);
}
-
- /**
- * Process a media item posted to the media endpoint.
- *
- * @param Illuminate\Http\Request $request
- * @return Illuminate\Http\Response
- */
- public function media(Request $request)
- {
- try {
- $tokenData = $this->tokenService->validateToken($request->bearerToken());
- } catch (InvalidTokenException $e) {
- return response()->json([
- 'response' => 'error',
- 'error' => 'invalid_token',
- 'error_description' => 'The provided token did not pass validation',
- ], 400);
- }
-
- $logger = new Logger('micropub');
- $logger->pushHandler(new StreamHandler(storage_path('logs/micropub.log')), Logger::DEBUG);
- $logger->debug('MicropubMediaLog', $request->all());
- //check post scope
- if ($tokenData->hasClaim('scope')) {
- if (stristr($tokenData->getClaim('scope'), 'create') === false) {
- return $this->returnInsufficientScopeResponse();
- }
- //check media valid
- if ($request->hasFile('file') && $request->file('file')->isValid()) {
- try {
- $filename = Uuid::uuid4() . '.' . $request->file('file')->extension();
- } catch (UnsatisfiedDependencyException $e) {
- return response()->json([
- 'response' => 'error',
- 'error' => 'internal_server_error',
- 'error_description' => 'A problem occured handling your request',
- ], 500);
- }
-
- $size = $request->file('file')->getClientSize();
- Storage::disk('local')->put($filename, $request->file('file')->openFile()->fread($size));
- try {
- Storage::disk('s3')->put('media/' . $filename, $request->file('file')->openFile()->fread($size));
- } catch (Exception $e) { // which exception?
- return response()->json([
- 'response' => 'error',
- 'error' => 'service_unavailable',
- 'error_description' => 'Unable to save media to S3',
- ], 503);
- }
-
- $manager = app()->make(ImageManager::class);
- try {
- $image = $manager->make($request->file('file'));
- $width = $image->width();
- } catch (\Intervention\Image\Exception\NotReadableException $exception) {
- // not an image
- $width = null;
- }
-
- $media = new Media();
- $media->token = $request->bearerToken();
- $media->path = 'media/' . $filename;
- $media->type = $this->getFileTypeFromMimeType($request->file('file')->getMimeType());
- $media->image_widths = $width;
- $media->save();
-
- dispatch(new ProcessImage($filename));
-
- return response()->json([
- 'response' => 'created',
- 'location' => $media->url,
- ], 201)->header('Location', $media->url);
- }
-
- return response()->json([
- 'response' => 'error',
- 'error' => 'invalid_request',
- 'error_description' => 'The uploaded file failed validation',
- ], 400);
- }
-
- return response()->json([
- 'response' => 'error',
- 'error' => 'invalid_request',
- 'error_description' => 'The provided token has no scopes',
- ], 400);
- }
-
- /**
- * Get the file type from the mimetype of the uploaded file.
- *
- * @param string The mimetype
- * @return string The type
- */
- private function getFileTypeFromMimeType($mimetype)
- {
- //try known images
- $imageMimeTypes = [
- 'image/gif',
- 'image/jpeg',
- 'image/png',
- 'image/svg+xml',
- 'image/tiff',
- 'image/webp',
- ];
- if (in_array($mimetype, $imageMimeTypes)) {
- return 'image';
- }
- //try known video
- $videoMimeTypes = [
- 'video/mp4',
- 'video/mpeg',
- 'video/quicktime',
- 'video/webm',
- ];
- if (in_array($mimetype, $videoMimeTypes)) {
- return 'video';
- }
- //try known audio types
- $audioMimeTypes = [
- 'audio/midi',
- 'audio/mpeg',
- 'audio/ogg',
- 'audio/x-m4a',
- ];
- if (in_array($mimetype, $audioMimeTypes)) {
- return 'audio';
- }
-
- return 'download';
- }
-
- private function returnInsufficientScopeResponse()
- {
- return response()->json([
- 'response' => 'error',
- 'error' => 'insufficient_scope',
- 'error_description' => 'The token’s scope does not have the necessary requirements.',
- ], 401);
- }
}
diff --git a/app/Http/Controllers/MicropubMediaController.php b/app/Http/Controllers/MicropubMediaController.php
new file mode 100644
index 00000000..fc804ea2
--- /dev/null
+++ b/app/Http/Controllers/MicropubMediaController.php
@@ -0,0 +1,201 @@
+input('token_data');
+
+ $scopes = $tokenData['scope'];
+ if (is_string($scopes)) {
+ $scopes = explode(' ', $scopes);
+ }
+ if (! in_array('create', $scopes, true)) {
+ return (new MicropubResponses)->insufficientScopeResponse();
+ }
+
+ if ($request->input('q') === 'last') {
+ $media = Media::where('created_at', '>=', Carbon::now()->subMinutes(30))
+ ->where('token', $request->input('access_token'))
+ ->latest()
+ ->first();
+ $mediaUrl = $media?->url;
+
+ return response()->json(['url' => $mediaUrl]);
+ }
+
+ if ($request->input('q') === 'source') {
+ $limit = $request->input('limit', 10);
+ $offset = $request->input('offset', 0);
+
+ $media = Media::latest()->offset($offset)->limit($limit)->get();
+
+ $media->transform(function ($mediaItem) {
+ return [
+ 'url' => $mediaItem->url,
+ 'published' => $mediaItem->created_at->toW3cString(),
+ 'mime_type' => $mediaItem->mimetype,
+ ];
+ });
+
+ return response()->json(['items' => $media]);
+ }
+
+ if ($request->has('q')) {
+ return response()->json([
+ 'error' => 'invalid_request',
+ 'error_description' => sprintf(
+ 'This server does not know how to handle this q parameter (%s)',
+ $request->input('q')
+ ),
+ ], 400);
+ }
+
+ return response()->json(['status' => 'OK']);
+ }
+
+ /**
+ * Process a media item posted to the media endpoint.
+ *
+ * @throws BindingResolutionException
+ * @throws Exception
+ */
+ public function media(Request $request): JsonResponse
+ {
+ $tokenData = $request->input('token_data');
+
+ $scopes = $tokenData['scope'];
+ if (is_string($scopes)) {
+ $scopes = explode(' ', $scopes);
+ }
+ if (! in_array('create', $scopes, true)) {
+ return (new MicropubResponses)->insufficientScopeResponse();
+ }
+
+ if ($request->hasFile('file') === false) {
+ return response()->json([
+ 'response' => 'error',
+ 'error' => 'invalid_request',
+ 'error_description' => 'No file was sent with the request',
+ ], 400);
+ }
+
+ /** @var UploadedFile $file */
+ $file = $request->file('file');
+
+ if ($file->isValid() === false) {
+ return response()->json([
+ 'response' => 'error',
+ 'error' => 'invalid_request',
+ 'error_description' => 'The uploaded file failed validation',
+ ], 400);
+ }
+
+ $filename = Storage::disk('local')->putFile('media', $file);
+
+ /** @var ImageManager $manager */
+ $manager = resolve(ImageManager::class);
+ try {
+ $image = $manager->read($request->file('file'));
+ $width = $image->width();
+ } catch (Exception) {
+ // not an image
+ $width = null;
+ }
+
+ $media = Media::create([
+ 'token' => $request->input('access_token'),
+ 'path' => $filename,
+ 'type' => $this->getFileTypeFromMimeType($request->file('file')->getMimeType()),
+ 'image_widths' => $width,
+ ]);
+
+ ProcessMedia::dispatch($filename);
+
+ return response()->json([
+ 'response' => 'created',
+ 'location' => $media->url,
+ ], 201)->header('Location', $media->url);
+ }
+
+ /**
+ * Return the relevant CORS headers to a pre-flight OPTIONS request.
+ */
+ public function mediaOptionsResponse(): Response
+ {
+ return response('OK', 200);
+ }
+
+ /**
+ * Get the file type from the mime-type of the uploaded file.
+ */
+ private function getFileTypeFromMimeType(string $mimeType): string
+ {
+ // try known images
+ $imageMimeTypes = [
+ 'image/gif',
+ 'image/jpeg',
+ 'image/png',
+ 'image/svg+xml',
+ 'image/tiff',
+ 'image/webp',
+ ];
+ if (in_array($mimeType, $imageMimeTypes)) {
+ return 'image';
+ }
+ // try known video
+ $videoMimeTypes = [
+ 'video/mp4',
+ 'video/mpeg',
+ 'video/ogg',
+ 'video/quicktime',
+ 'video/webm',
+ ];
+ if (in_array($mimeType, $videoMimeTypes)) {
+ return 'video';
+ }
+ // try known audio types
+ $audioMimeTypes = [
+ 'audio/midi',
+ 'audio/mpeg',
+ 'audio/ogg',
+ 'audio/x-m4a',
+ ];
+ if (in_array($mimeType, $audioMimeTypes)) {
+ return 'audio';
+ }
+
+ return 'download';
+ }
+
+ /**
+ * Save an uploaded file to the local disk.
+ *
+ * @throws Exception
+ */
+ private function saveFileToLocal(UploadedFile $file): string
+ {
+ $filename = Uuid::uuid4()->toString() . '.' . $file->extension();
+ Storage::disk('local')->putFileAs('', $file, $filename);
+
+ return $filename;
+ }
+}
diff --git a/app/Http/Controllers/NotesController.php b/app/Http/Controllers/NotesController.php
index 9801c8ea..d5c9bc90 100644
--- a/app/Http/Controllers/NotesController.php
+++ b/app/Http/Controllers/NotesController.php
@@ -1,31 +1,37 @@
wantsActivityStream()) {
- return (new ActivityStreamsService)->siteOwnerResponse();
- }
-
$notes = Note::latest()
->with('place', 'media', 'client')
- ->withCount(['webmentions As replies' => function ($query) {
+ ->withCount(['webmentions AS replies' => function ($query) {
$query->where('type', 'in-reply-to');
+ }])
+ ->withCount(['webmentions AS likes' => function ($query) {
+ $query->where('type', 'like-of');
+ }])
+ ->withCount(['webmentions AS reposts' => function ($query) {
+ $query->where('type', 'repost-of');
}])->paginate(10);
return view('notes.index', compact('notes'));
@@ -33,16 +39,22 @@ class NotesController extends Controller
/**
* Show a single note.
- *
- * @param string The id of the note
- * @return \Illuminate\View\Factory view
*/
- public function show($urlId)
+ public function show(string $urlId): View|JsonResponse|Response
{
- $note = Note::nb60($urlId)->with('webmentions')->firstOrFail();
-
- if (request()->wantsActivityStream()) {
- return (new ActivityStreamsService)->singleNoteResponse($note);
+ try {
+ $note = Note::nb60($urlId)->with('place', 'media', 'client')
+ ->withCount(['webmentions AS replies' => function ($query) {
+ $query->where('type', 'in-reply-to');
+ }])
+ ->withCount(['webmentions AS likes' => function ($query) {
+ $query->where('type', 'like-of');
+ }])
+ ->withCount(['webmentions AS reposts' => function ($query) {
+ $query->where('type', 'repost-of');
+ }])->firstOrFail();
+ } catch (ModelNotFoundException $exception) {
+ abort(404);
}
return view('notes.show', compact('note'));
@@ -50,22 +62,16 @@ class NotesController extends Controller
/**
* Redirect /note/{decID} to /notes/{nb60id}.
- *
- * @param string The decimal id of he note
- * @return \Illuminate\Routing\RedirectResponse redirect
*/
- public function redirect($decId)
+ public function redirect(int $decId): RedirectResponse
{
- return redirect(config('app.url') . '/notes/' . (new Numbers())->numto60($decId));
+ return redirect(config('app.url') . '/notes/' . (new Numbers)->numto60($decId));
}
/**
* Show all notes tagged with {tag}.
- *
- * @param string The tag
- * @return \Illuminate\View\Factory view
*/
- public function tagged($tag)
+ public function tagged(string $tag): View
{
$notes = Note::whereHas('tags', function ($query) use ($tag) {
$query->where('tag', $tag);
@@ -73,4 +79,14 @@ class NotesController extends Controller
return view('notes.tagged', compact('notes', 'tag'));
}
+
+ /**
+ * Page to create a new note.
+ *
+ * Dummy page for now.
+ */
+ public function create(): View
+ {
+ return view('notes.create');
+ }
}
diff --git a/app/Http/Controllers/PhotosController.php b/app/Http/Controllers/PhotosController.php
deleted file mode 100644
index 9eaeb769..00000000
--- a/app/Http/Controllers/PhotosController.php
+++ /dev/null
@@ -1,94 +0,0 @@
-imageResizeLimit = 800;
- }
-
- /**
- * Save an uploaded photo to the image folder.
- *
- * @param \Illuminate\Http\Request $request
- * @param string The associated note’s nb60 ID
- * @return bool
- */
- public function saveImage(Request $request, $nb60id)
- {
- if ($request->hasFile('photo') !== true) {
- return false;
- }
- $photoFilename = 'note-' . $nb60id;
- $path = public_path() . '/assets/img/notes/';
- $ext = $request->file('photo')->getClientOriginalExtension();
- $photoFilename .= '.' . $ext;
- $request->file('photo')->move($path, $photoFilename);
-
- return true;
- }
-
- /**
- * Prepare a photo for posting to twitter.
- *
- * @param string photo fileanme
- * @return string small photo filename, or null
- */
- public function makeSmallPhotoForTwitter($photoFilename)
- {
- $imagine = new Imagine();
- $orig = $imagine->open(public_path() . '/assets/img/notes/' . $photoFilename);
- $size = [$orig->getSize()->getWidth(), $orig->getSize()->getHeight()];
- if ($size[0] > $this->imageResizeLimit || $size[1] > $this->imageResizeLimit) {
- $filenameParts = explode('.', $photoFilename);
- $preExt = count($filenameParts) - 2;
- $filenameParts[$preExt] .= '-small';
- $photoFilenameSmall = implode('.', $filenameParts);
- $aspectRatio = $size[0] / $size[1];
- $box = ($aspectRatio >= 1) ?
- [$this->imageResizeLimit, (int) round($this->imageResizeLimit / $aspectRatio)]
- :
- [(int) round($this->imageResizeLimit * $aspectRatio), $this->imageResizeLimit];
- $orig->resize(new Box($box[0], $box[1]))
- ->save(public_path() . '/assets/img/notes/' . $photoFilenameSmall);
-
- return $photoFilenameSmall;
- }
- }
-
- /**
- * Get the image path for a note.
- *
- * @param string $nb60id
- * @return string | null
- */
- public function getPhotoPath($nb60id)
- {
- $filesystem = new Filesystem();
- $photoDir = public_path() . '/assets/img/notes';
- $files = $filesystem->files($photoDir);
- foreach ($files as $file) {
- $parts = explode('.', $file);
- $name = $parts[0];
- $dirs = explode('/', $name);
- $actualname = last($dirs);
- if ($actualname == 'note-' . $nb60id) {
- $ext = $parts[1];
- }
- }
- if (isset($ext)) {
- return '/assets/img/notes/note-' . $nb60id . '.' . $ext;
- }
- }
-}
diff --git a/app/Http/Controllers/PlacesController.php b/app/Http/Controllers/PlacesController.php
index ba8d5b42..b949ecde 100644
--- a/app/Http/Controllers/PlacesController.php
+++ b/app/Http/Controllers/PlacesController.php
@@ -1,17 +1,18 @@
firstOrFail();
-
return view('singleplace', ['place' => $place]);
}
}
diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php
index 9f68227b..3f366538 100644
--- a/app/Http/Controllers/SearchController.php
+++ b/app/Http/Controllers/SearchController.php
@@ -2,25 +2,33 @@
namespace App\Http\Controllers;
-use App\Note;
+use App\Models\Note;
use Illuminate\Http\Request;
+use Illuminate\View\View;
class SearchController extends Controller
{
- public function search(Request $request)
+ public function search(Request $request): View
{
- $notes = Note::search($request->terms)->paginate(10);
+ $search = $request->input('q');
+
+ $notes = Note::search($search)
+ ->paginate();
+
+ /** @var Note $note */
foreach ($notes as $note) {
- $note->iso8601_time = $note->updated_at->toISO8601String();
- $note->human_time = $note->updated_at->diffForHumans();
- $photoURLs = [];
- $photos = $note->getMedia();
- foreach ($photos as $photo) {
- $photoURLs[] = $photo->getUrl();
- }
- $note->photoURLs = $photoURLs;
+ $note->load('place', 'media', 'client')
+ ->loadCount(['webmentions AS replies' => function ($query) {
+ $query->where('type', 'in-reply-to');
+ }])
+ ->loadCount(['webmentions AS likes' => function ($query) {
+ $query->where('type', 'like-of');
+ }])
+ ->loadCount(['webmentions AS reposts' => function ($query) {
+ $query->where('type', 'repost-of');
+ }]);
}
- return view('search', compact('notes'));
+ return view('search', compact('search', 'notes'));
}
}
diff --git a/app/Http/Controllers/SessionStoreController.php b/app/Http/Controllers/SessionStoreController.php
deleted file mode 100644
index c7d239eb..00000000
--- a/app/Http/Controllers/SessionStoreController.php
+++ /dev/null
@@ -1,15 +0,0 @@
-input('css');
-
- session(['css' => $css]);
-
- return ['status' => 'ok'];
- }
-}
diff --git a/app/Http/Controllers/ShortURLsController.php b/app/Http/Controllers/ShortURLsController.php
deleted file mode 100644
index 9c34ca0f..00000000
--- a/app/Http/Controllers/ShortURLsController.php
+++ /dev/null
@@ -1,120 +0,0 @@
-b60tonum($shortURLId);
- $shorturl = ShortURL::find($num);
- $redirect = $shorturl->redirect;
-
- return redirect($redirect);
- }
-
- /**
- * I had an old redirect systme breifly, but cool URLs should still work.
- *
- * @param string URL ID
- * @return \Illuminate\Routing\Redirector redirect
- */
- public function oldRedirect($shortURLId)
- {
- $filename = base_path() . '/public/assets/old-shorturls.json';
- $handle = fopen($filename, 'r');
- $contents = fread($handle, filesize($filename));
- $object = json_decode($contents);
-
- foreach ($object as $key => $val) {
- if ($shortURLId == $key) {
- return redirect($val);
- }
- }
-
- return 'This id was never used.
- Old redirects are located at
-
- old-shorturls.json
-
.';
- }
-}
diff --git a/app/Http/Controllers/TokenEndpointController.php b/app/Http/Controllers/TokenEndpointController.php
deleted file mode 100644
index 46037ca7..00000000
--- a/app/Http/Controllers/TokenEndpointController.php
+++ /dev/null
@@ -1,78 +0,0 @@
-client = $client;
- $this->tokenService = $tokenService;
- }
-
- /**
- * If the user has auth’d via the IndieAuth protocol, issue a valid token.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function create(Request $request)
- {
- $authorizationEndpoint = $this->client->discoverAuthorizationEndpoint(normalize_url($request->input('me')));
- if ($authorizationEndpoint) {
- $auth = $this->client->verifyIndieAuthCode(
- $authorizationEndpoint,
- $request->input('code'),
- $request->input('me'),
- $request->input('redirect_uri'),
- $request->input('client_id')
- );
- if (array_key_exists('me', $auth)) {
- $scope = $auth['scope'] ?? '';
- $tokenData = [
- 'me' => $request->input('me'),
- 'client_id' => $request->input('client_id'),
- 'scope' => $scope,
- ];
- $token = $this->tokenService->getNewToken($tokenData);
- $content = http_build_query([
- 'me' => $request->input('me'),
- 'scope' => $scope,
- 'access_token' => $token,
- ]);
-
- return response($content)->header(
- 'Content-Type',
- 'application/x-www-form-urlencoded'
- );
- }
-
- return response('There was an error verifying the authorisation code.', 400);
- }
-
- return response('Can’t determine the authorisation endpoint.', 400);
- }
-}
diff --git a/app/Http/Controllers/WebMentionsController.php b/app/Http/Controllers/WebMentionsController.php
index 41d8cc9b..49eac9b2 100644
--- a/app/Http/Controllers/WebMentionsController.php
+++ b/app/Http/Controllers/WebMentionsController.php
@@ -1,70 +1,72 @@
has('target') !== true) || ($request->has('source') !== true)) {
- return new Response(
+ return response(
'You need both the target and source parameters',
400
);
}
- //next check the $target is valid
+ // next check the $target is valid
$path = parse_url($request->input('target'), PHP_URL_PATH);
$pathParts = explode('/', $path);
- switch ($pathParts[1]) {
- case 'notes':
- //we have a note
- $noteId = $pathParts[2];
- $numbers = new Numbers();
- try {
- $note = Note::findOrFail($numbers->b60tonum($noteId));
- dispatch(new ProcessWebMention($note, $request->input('source')));
- } catch (ModelNotFoundException $e) {
- return new Response('This note doesn’t exist.', 400);
- }
+ if ($pathParts[1] === 'notes') {
+ // we have a note
+ $noteId = $pathParts[2];
+ try {
+ $note = Note::findOrFail(resolve(Numbers::class)->b60tonum($noteId));
+ dispatch(new ProcessWebMention($note, $request->input('source')));
+ } catch (ModelNotFoundException $e) {
+ return response('This note doesn’t exist.', 400);
+ }
- return new Response(
- 'Webmention received, it will be processed shortly',
- 202
- );
- break;
- case 'blog':
- return new Response(
- 'I don’t accept webmentions for blog posts yet.',
- 501
- );
- break;
- default:
- return new Response(
- 'Invalid request',
- 400
- );
- break;
+ return response(
+ 'Webmention received, it will be processed shortly',
+ 202
+ );
}
+ if ($pathParts[1] === 'blog') {
+ return response(
+ 'I don’t accept webmentions for blog posts yet.',
+ 501
+ );
+ }
+
+ return response(
+ 'Invalid request',
+ 400
+ );
}
}
diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php
deleted file mode 100644
index 1f67d385..00000000
--- a/app/Http/Kernel.php
+++ /dev/null
@@ -1,67 +0,0 @@
- [
- \App\Http\Middleware\EncryptCookies::class,
- \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
- \Illuminate\Session\Middleware\StartSession::class,
- // \Illuminate\Session\Middleware\AuthenticateSession::class,
- \Illuminate\View\Middleware\ShareErrorsFromSession::class,
- \App\Http\Middleware\VerifyCsrfToken::class,
- \Illuminate\Routing\Middleware\SubstituteBindings::class,
- \App\Http\Middleware\LinkHeadersMiddleware::class,
- //\App\Http\Middleware\DevTokenMiddleware::class,
- \App\Http\Middleware\LocalhostSessionMiddleware::class,
- \App\Http\Middleware\ActivityStreamLinks::class,
- ],
-
- 'api' => [
- 'throttle:60,1',
- 'bindings',
- ],
- ];
-
- /**
- * The application's route middleware.
- *
- * These middleware may be assigned to groups or used individually.
- *
- * @var array
- */
- protected $routeMiddleware = [
- 'auth' => \App\Http\Middleware\Authenticate::class,
- 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
- 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
- 'can' => \Illuminate\Auth\Middleware\Authorize::class,
- 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
- 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
- 'micropub.token' => \App\Http\Middleware\VerifyMicropubToken::class,
- 'myauth' => \App\Http\Middleware\MyAuthMiddleware::class,
- ];
-}
diff --git a/app/Http/Middleware/ActivityStreamLinks.php b/app/Http/Middleware/ActivityStreamLinks.php
deleted file mode 100644
index 4c240759..00000000
--- a/app/Http/Middleware/ActivityStreamLinks.php
+++ /dev/null
@@ -1,28 +0,0 @@
-path() === '/') {
- $response->header('Link', '<' . config('app.url') . '>; rel="application/activity+json"', false);
- }
- if ($request->is('notes/*')) {
- $response->header('Link', '<' . $request->url() . '>; rel="application/activity+json"', false);
- }
-
- return $response;
- }
-}
diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php
new file mode 100644
index 00000000..624cd371
--- /dev/null
+++ b/app/Http/Middleware/Authenticate.php
@@ -0,0 +1,20 @@
+expectsJson() ? null : route('login');
+ }
+}
diff --git a/app/Http/Middleware/CorsHeaders.php b/app/Http/Middleware/CorsHeaders.php
new file mode 100644
index 00000000..cacf9188
--- /dev/null
+++ b/app/Http/Middleware/CorsHeaders.php
@@ -0,0 +1,29 @@
+path() === 'api/media') {
+ $response->header('Access-Control-Allow-Origin', '*');
+ $response->header('Access-Control-Allow-Methods', 'OPTIONS, POST');
+ $response->header(
+ 'Access-Control-Allow-Headers',
+ 'Authorization, Content-Type, DNT, X-CSRF-TOKEN, X-REQUESTED-WITH'
+ );
+ $response->header('Access-Control-Allow-Credentials', 'true');
+ }
+
+ return $response;
+ }
+}
diff --git a/app/Http/Middleware/DevTokenMiddleware.php b/app/Http/Middleware/DevTokenMiddleware.php
deleted file mode 100644
index dabc2ca2..00000000
--- a/app/Http/Middleware/DevTokenMiddleware.php
+++ /dev/null
@@ -1,36 +0,0 @@
- config('app.url')]);
- if (Storage::exists('dev-token')) {
- session(['token' => Storage::get('dev-token')]);
- } else {
- $data = [
- 'me' => config('app.url'),
- 'client_id' => route('micropub-client'),
- 'scope' => 'post',
- ];
- $tokenService = new \App\Services\TokenService();
- session(['token' => $tokenService->getNewToken($data)]);
- }
- }
-
- return $next($request);
- }
-}
diff --git a/app/Http/Middleware/EncryptCookies.php b/app/Http/Middleware/EncryptCookies.php
index 033136ad..867695bd 100644
--- a/app/Http/Middleware/EncryptCookies.php
+++ b/app/Http/Middleware/EncryptCookies.php
@@ -9,7 +9,7 @@ class EncryptCookies extends Middleware
/**
* The names of the cookies that should not be encrypted.
*
- * @var array
+ * @var array
*/
protected $except = [
//
diff --git a/app/Http/Middleware/LinkHeadersMiddleware.php b/app/Http/Middleware/LinkHeadersMiddleware.php
index 66dee526..467283db 100644
--- a/app/Http/Middleware/LinkHeadersMiddleware.php
+++ b/app/Http/Middleware/LinkHeadersMiddleware.php
@@ -3,23 +3,22 @@
namespace App\Http\Middleware;
use Closure;
+use Illuminate\Http\Request;
+use Symfony\Component\HttpFoundation\Response;
class LinkHeadersMiddleware
{
/**
* Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @return mixed
*/
- public function handle($request, Closure $next)
+ public function handle(Request $request, Closure $next): Response
{
$response = $next($request);
- $response->header('Link', '; rel="authorization_endpoint"', false);
- $response->header('Link', '<' . config('app.url') . '/api/token>; rel="token_endpoint"', false);
- $response->header('Link', '<' . config('app.url') . '/api/post>; rel="micropub"', false);
- $response->header('Link', '<' . config('app.url') . '/webmention>; rel="webmention"', false);
+ $response->header('Link', '<' . route('indieauth.metadata') . '>; rel="indieauth-metadata"', false);
+ $response->header('Link', '<' . route('indieauth.start') . '>; rel="authorization_endpoint"', false);
+ $response->header('Link', '<' . route('indieauth.token') . '>; rel="token_endpoint"', false);
+ $response->header('Link', '<' . route('micropub-endpoint') . '>; rel="micropub"', false);
+ $response->header('Link', '<' . route('webmention-endpoint') . '>; rel="webmention"', false);
return $response;
}
diff --git a/app/Http/Middleware/LocalhostSessionMiddleware.php b/app/Http/Middleware/LocalhostSessionMiddleware.php
index ded4f25a..060682d5 100644
--- a/app/Http/Middleware/LocalhostSessionMiddleware.php
+++ b/app/Http/Middleware/LocalhostSessionMiddleware.php
@@ -1,8 +1,12 @@
config('app.url')]` as I can’t manually log in as
* a .localhost domain.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @return mixed
*/
- public function handle($request, Closure $next)
+ public function handle(Request $request, Closure $next): Response
{
if (config('app.env') !== 'production') {
session(['me' => config('app.url')]);
diff --git a/app/Http/Middleware/LogMicropubRequest.php b/app/Http/Middleware/LogMicropubRequest.php
new file mode 100644
index 00000000..a04e80de
--- /dev/null
+++ b/app/Http/Middleware/LogMicropubRequest.php
@@ -0,0 +1,24 @@
+pushHandler(new StreamHandler(storage_path('logs/micropub.log')));
+ $logger->debug('MicropubLog', $request->all());
+
+ return $next($request);
+ }
+}
diff --git a/app/Http/Middleware/MyAuthMiddleware.php b/app/Http/Middleware/MyAuthMiddleware.php
index 5354e55b..b22e2b33 100644
--- a/app/Http/Middleware/MyAuthMiddleware.php
+++ b/app/Http/Middleware/MyAuthMiddleware.php
@@ -1,22 +1,25 @@
session()->has('loggedin') !== true) {
- //they’re not logged in, so send them to login form
+ if (Auth::check() === false) {
+ // they’re not logged in, so send them to login form
+ redirect()->setIntendedUrl($request->fullUrl());
+
return redirect()->route('login');
}
diff --git a/app/Http/Middleware/PreventRequestsDuringMaintenance.php b/app/Http/Middleware/PreventRequestsDuringMaintenance.php
new file mode 100644
index 00000000..74cbd9a9
--- /dev/null
+++ b/app/Http/Middleware/PreventRequestsDuringMaintenance.php
@@ -0,0 +1,17 @@
+
+ */
+ protected $except = [
+ //
+ ];
+}
diff --git a/app/Http/Middleware/RedirectIfAuthenticated.php b/app/Http/Middleware/RedirectIfAuthenticated.php
index e4cec9c8..a6a6c8c4 100644
--- a/app/Http/Middleware/RedirectIfAuthenticated.php
+++ b/app/Http/Middleware/RedirectIfAuthenticated.php
@@ -2,23 +2,30 @@
namespace App\Http\Middleware;
+use App\Providers\RouteServiceProvider;
use Closure;
+use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
+use Symfony\Component\HttpFoundation\Response;
+/**
+ * @codeCoverageIgnore
+ */
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @param string|null $guard
- * @return mixed
+ * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
- public function handle($request, Closure $next, $guard = null)
+ public function handle(Request $request, Closure $next, string ...$guards): Response
{
- if (Auth::guard($guard)->check()) {
- return redirect('/home');
+ $guards = empty($guards) ? [null] : $guards;
+
+ foreach ($guards as $guard) {
+ if (Auth::guard($guard)->check()) {
+ return redirect(RouteServiceProvider::HOME);
+ }
}
return $next($request);
diff --git a/app/Http/Middleware/TrimStrings.php b/app/Http/Middleware/TrimStrings.php
index 5a50e7b5..88cadcaa 100644
--- a/app/Http/Middleware/TrimStrings.php
+++ b/app/Http/Middleware/TrimStrings.php
@@ -9,9 +9,10 @@ class TrimStrings extends Middleware
/**
* The names of the attributes that should not be trimmed.
*
- * @var array
+ * @var array
*/
protected $except = [
+ 'current_password',
'password',
'password_confirmation',
];
diff --git a/app/Http/Middleware/TrustHosts.php b/app/Http/Middleware/TrustHosts.php
new file mode 100644
index 00000000..9c88c34c
--- /dev/null
+++ b/app/Http/Middleware/TrustHosts.php
@@ -0,0 +1,23 @@
+
+ */
+ public function hosts(): array
+ {
+ return [
+ $this->allSubdomainsOfApplicationUrl(),
+ ];
+ }
+}
diff --git a/app/Http/Middleware/TrustProxies.php b/app/Http/Middleware/TrustProxies.php
index ef1c00d1..f33f3eef 100644
--- a/app/Http/Middleware/TrustProxies.php
+++ b/app/Http/Middleware/TrustProxies.php
@@ -2,28 +2,27 @@
namespace App\Http\Middleware;
+use Illuminate\Http\Middleware\TrustProxies as Middleware;
use Illuminate\Http\Request;
-use Fideloper\Proxy\TrustProxies as Middleware;
class TrustProxies extends Middleware
{
/**
* The trusted proxies for this application.
*
- * @var array
+ * @var array|string|null
*/
protected $proxies;
/**
- * The current proxy header mappings.
+ * The header that should be used to detect proxies.
*
- * @var array
+ * @var int
*/
- protected $headers = [
- Request::HEADER_FORWARDED => 'FORWARDED',
- Request::HEADER_X_FORWARDED_FOR => 'X_FORWARDED_FOR',
- Request::HEADER_X_FORWARDED_HOST => 'X_FORWARDED_HOST',
- Request::HEADER_X_FORWARDED_PORT => 'X_FORWARDED_PORT',
- Request::HEADER_X_FORWARDED_PROTO => 'X_FORWARDED_PROTO',
- ];
+ protected $headers =
+ Request::HEADER_X_FORWARDED_FOR |
+ Request::HEADER_X_FORWARDED_HOST |
+ Request::HEADER_X_FORWARDED_PORT |
+ Request::HEADER_X_FORWARDED_PROTO |
+ Request::HEADER_X_FORWARDED_AWS_ELB;
}
diff --git a/app/Http/Middleware/ValidateSignature.php b/app/Http/Middleware/ValidateSignature.php
new file mode 100644
index 00000000..093bf64a
--- /dev/null
+++ b/app/Http/Middleware/ValidateSignature.php
@@ -0,0 +1,22 @@
+
+ */
+ protected $except = [
+ // 'fbclid',
+ // 'utm_campaign',
+ // 'utm_content',
+ // 'utm_medium',
+ // 'utm_source',
+ // 'utm_term',
+ ];
+}
diff --git a/app/Http/Middleware/VerifyCsrfToken.php b/app/Http/Middleware/VerifyCsrfToken.php
index 1593e373..fc7bad50 100644
--- a/app/Http/Middleware/VerifyCsrfToken.php
+++ b/app/Http/Middleware/VerifyCsrfToken.php
@@ -9,7 +9,7 @@ class VerifyCsrfToken extends Middleware
/**
* The URIs that should be excluded from CSRF verification.
*
- * @var array
+ * @var array
*/
protected $except = [
'api/media',
diff --git a/app/Http/Middleware/VerifyMicropubToken.php b/app/Http/Middleware/VerifyMicropubToken.php
index 93e2edf6..33d2cb12 100644
--- a/app/Http/Middleware/VerifyMicropubToken.php
+++ b/app/Http/Middleware/VerifyMicropubToken.php
@@ -1,21 +1,37 @@
bearerToken() === null) {
+ $rawToken = null;
+
+ if ($request->input('access_token')) {
+ $rawToken = $request->input('access_token');
+ } elseif ($request->bearerToken()) {
+ $rawToken = $request->bearerToken();
+ }
+
+ if (! $rawToken) {
return response()->json([
'response' => 'error',
'error' => 'unauthorized',
@@ -23,6 +39,43 @@ class VerifyMicropubToken
], 401);
}
- return $next($request);
+ try {
+ $tokenData = $this->validateToken($rawToken);
+ } catch (RequiredConstraintsViolated|InvalidTokenStructure|CannotDecodeContent) {
+ $micropubResponses = new MicropubResponses;
+
+ return $micropubResponses->invalidTokenResponse();
+ }
+
+ if ($tokenData->claims()->has('scope') === false) {
+ $micropubResponses = new MicropubResponses;
+
+ return $micropubResponses->tokenHasNoScopeResponse();
+ }
+
+ return $next($request->merge([
+ 'access_token' => $rawToken,
+ 'token_data' => [
+ 'me' => $tokenData->claims()->get('me'),
+ 'scope' => $tokenData->claims()->get('scope'),
+ 'client_id' => $tokenData->claims()->get('client_id'),
+ ],
+ ]));
+ }
+
+ /**
+ * Check the token signature is valid.
+ */
+ private function validateToken(string $bearerToken): Token
+ {
+ $config = resolve(Configuration::class);
+
+ $token = $config->parser()->parse($bearerToken);
+
+ $constraints = $config->validationConstraints();
+
+ $config->validator()->assert($token, ...$constraints);
+
+ return $token;
}
}
diff --git a/app/Http/Requests/MicropubRequest.php b/app/Http/Requests/MicropubRequest.php
new file mode 100644
index 00000000..d931f139
--- /dev/null
+++ b/app/Http/Requests/MicropubRequest.php
@@ -0,0 +1,106 @@
+micropubData;
+ }
+
+ public function getType(): ?string
+ {
+ // Return consistent type regardless of input format
+ return $this->micropubData['type'] ?? null;
+ }
+
+ protected function prepareForValidation(): void
+ {
+ // Normalize the request data based on content type
+ if ($this->isJson()) {
+ $this->normalizeMicropubJson();
+ } else {
+ $this->normalizeMicropubForm();
+ }
+ }
+
+ private function normalizeMicropubJson(): void
+ {
+ $json = $this->json();
+ if ($json === null) {
+ throw new \InvalidArgumentException('`isJson()` passed but there is no json data');
+ }
+
+ $data = $json->all();
+
+ // Convert JSON type (h-entry) to simple type (entry)
+ if (isset($data['type']) && is_array($data['type'])) {
+ $type = current($data['type']);
+ if (strpos($type, 'h-') === 0) {
+ $this->micropubData['type'] = substr($type, 2);
+ }
+ }
+ // Or set the type to update
+ elseif (isset($data['action']) && $data['action'] === 'update') {
+ $this->micropubData['type'] = 'update';
+ }
+
+ // Add in the token data
+ $this->micropubData['token_data'] = $data['token_data'];
+
+ // Add h-entry values
+ $this->micropubData['content'] = Arr::get($data, 'properties.content.0');
+ $this->micropubData['in-reply-to'] = Arr::get($data, 'properties.in-reply-to.0');
+ $this->micropubData['published'] = Arr::get($data, 'properties.published.0');
+ $this->micropubData['location'] = Arr::get($data, 'location');
+ $this->micropubData['bookmark-of'] = Arr::get($data, 'properties.bookmark-of.0');
+ $this->micropubData['like-of'] = Arr::get($data, 'properties.like-of.0');
+ $this->micropubData['mp-syndicate-to'] = Arr::get($data, 'properties.mp-syndicate-to');
+
+ // Add h-card values
+ $this->micropubData['name'] = Arr::get($data, 'properties.name.0');
+ $this->micropubData['description'] = Arr::get($data, 'properties.description.0');
+ $this->micropubData['geo'] = Arr::get($data, 'properties.geo.0');
+
+ // Add checkin value
+ $this->micropubData['checkin'] = Arr::get($data, 'checkin');
+ $this->micropubData['syndication'] = Arr::get($data, 'properties.syndication.0');
+ }
+
+ private function normalizeMicropubForm(): void
+ {
+ // Convert form h=entry to type=entry
+ if ($h = $this->input('h')) {
+ $this->micropubData['type'] = $h;
+ }
+
+ // Add some fields to the micropub data with default null values
+ $this->micropubData['in-reply-to'] = null;
+ $this->micropubData['published'] = null;
+ $this->micropubData['location'] = null;
+ $this->micropubData['description'] = null;
+ $this->micropubData['geo'] = null;
+ $this->micropubData['latitude'] = null;
+ $this->micropubData['longitude'] = null;
+
+ // Map form fields to micropub data
+ foreach ($this->except(['h', 'access_token']) as $key => $value) {
+ $this->micropubData[$key] = $value;
+ }
+ }
+}
diff --git a/app/Http/Responses/MicropubResponses.php b/app/Http/Responses/MicropubResponses.php
new file mode 100644
index 00000000..4f7240c2
--- /dev/null
+++ b/app/Http/Responses/MicropubResponses.php
@@ -0,0 +1,46 @@
+json([
+ 'response' => 'error',
+ 'error' => 'insufficient_scope',
+ 'error_description' => 'The token’s scope does not have the necessary requirements.',
+ ], 401);
+ }
+
+ /**
+ * Generate a response to be returned when the token is invalid.
+ */
+ public function invalidTokenResponse(): JsonResponse
+ {
+ return response()->json([
+ 'response' => 'error',
+ 'error' => 'invalid_token',
+ 'error_description' => 'The provided token did not pass validation',
+ ], 400);
+ }
+
+ /**
+ * Generate a response to be returned when the token has no scope.
+ */
+ public function tokenHasNoScopeResponse(): JsonResponse
+ {
+ return response()->json([
+ 'response' => 'error',
+ 'error' => 'invalid_request',
+ 'error_description' => 'The provided token has no scopes',
+ ], 400);
+ }
+}
diff --git a/app/Jobs/AddClientToDatabase.php b/app/Jobs/AddClientToDatabase.php
index 970f4c07..b540aac0 100644
--- a/app/Jobs/AddClientToDatabase.php
+++ b/app/Jobs/AddClientToDatabase.php
@@ -1,39 +1,40 @@
client_id = $client_id;
+ $this->client_id = $clientId;
}
/**
* Execute the job.
- *
- * @return void
*/
- public function handle()
+ public function handle(): void
{
- if (MicropubClient::where('client_url', $this->client_id)->count() == 0) {
- $client = MicropubClient::create([
+ if (MicropubClient::where('client_url', $this->client_id)->count() === 0) {
+ MicropubClient::create([
'client_url' => $this->client_id,
'client_name' => $this->client_id, // default client name is the URL
]);
diff --git a/app/Jobs/DownloadWebMention.php b/app/Jobs/DownloadWebMention.php
index c12a92d4..3c187dd4 100644
--- a/app/Jobs/DownloadWebMention.php
+++ b/app/Jobs/DownloadWebMention.php
@@ -1,68 +1,66 @@
source = $source;
- }
+ public function __construct(
+ protected string $source
+ ) {}
/**
* Execute the job.
*
- * @return void
+ * @throws GuzzleException
+ * @throws FileNotFoundException
*/
- public function handle(Client $guzzle)
+ public function handle(Client $guzzle): void
{
$response = $guzzle->request('GET', $this->source);
- //4XX and 5XX responses should get Guzzle to throw an exception,
- //Laravel should catch and retry these automatically.
- if ($response->getStatusCode() == '200') {
- $filesystem = new \Illuminate\FileSystem\FileSystem();
- $filename = storage_path() . '/HTML/' . $this->createFilenameFromURL($this->source);
- //backup file first
+ // 4XX and 5XX responses should get Guzzle to throw an exception,
+ // Laravel should catch and retry these automatically.
+ if ($response->getStatusCode() === 200) {
+ $filesystem = new FileSystem;
+ $filename = storage_path('HTML') . '/' . $this->createFilenameFromURL($this->source);
+ // backup file first
$filenameBackup = $filename . '.' . date('Y-m-d') . '.backup';
if ($filesystem->exists($filename)) {
$filesystem->copy($filename, $filenameBackup);
}
- //check if base directory exists
+ // check if base directory exists
if (! $filesystem->exists($filesystem->dirname($filename))) {
$filesystem->makeDirectory(
$filesystem->dirname($filename),
- 0755, //mode
- true //recursive
+ 0755, // mode
+ true // recursive
);
}
- //save new HTML
+ // save new HTML
$filesystem->put(
$filename,
(string) $response->getBody()
);
- //remove backup if the same
+ // remove backup if the same
if ($filesystem->exists($filenameBackup)) {
- if ($filesystem->get($filename) == $filesystem->get($filenameBackup)) {
+ if ($filesystem->get($filename) === $filesystem->get($filenameBackup)) {
$filesystem->delete($filenameBackup);
}
}
@@ -70,16 +68,12 @@ class DownloadWebMention implements ShouldQueue
}
/**
- * Create a file path from a URL. This is used when caching the HTML
- * response.
- *
- * @param string The URL
- * @return string The path name
+ * Create a file path from a URL. This is used when caching the HTML response.
*/
- private function createFilenameFromURL($url)
+ private function createFilenameFromURL(string $url): string
{
$filepath = str_replace(['https://', 'http://'], ['https/', 'http/'], $url);
- if (substr($filepath, -1) == '/') {
+ if (str_ends_with($filepath, '/')) {
$filepath .= 'index.html';
}
diff --git a/app/Jobs/ProcessBookmark.php b/app/Jobs/ProcessBookmark.php
index 7042c58f..96f65e87 100644
--- a/app/Jobs/ProcessBookmark.php
+++ b/app/Jobs/ProcessBookmark.php
@@ -1,56 +1,46 @@
bookmark = $bookmark;
- }
+ public function __construct(
+ protected Bookmark $bookmark
+ ) {}
/**
* Execute the job.
- *
- * @return void
*/
- public function handle(Browsershot $browsershot, Client $client)
+ public function handle(): void
{
- //save a local screenshot
- $uuid = Uuid::uuid4();
- $browsershot->url($this->bookmark->url)
- ->windowSize(960, 640)
- ->save(public_path() . '/assets/img/bookmarks/' . $uuid . '.png');
- $this->bookmark->screenshot = $uuid;
+ SaveScreenshot::dispatch($this->bookmark);
- //get an internet archive link
- $response = $client->request('GET', 'https://web.archive.org/save/' . $this->bookmark->url);
- if ($response->hasHeader('Content-Location')) {
- if (starts_with($response->getHeader('Content-Location')[0], '/web')) {
- $this->bookmark->archive = $response->getHeader('Content-Location')[0];
- }
+ try {
+ $archiveLink = (resolve(BookmarkService::class))->getArchiveLink($this->bookmark->url);
+ } catch (InternetArchiveException) {
+ $archiveLink = null;
}
+ $this->bookmark->archive = $archiveLink;
- //save
$this->bookmark->save();
}
}
diff --git a/app/Jobs/ProcessImage.php b/app/Jobs/ProcessImage.php
deleted file mode 100644
index 4ba0060f..00000000
--- a/app/Jobs/ProcessImage.php
+++ /dev/null
@@ -1,68 +0,0 @@
-filename = $filename;
- }
-
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle(ImageManager $manager)
- {
- //open file
- try {
- $image = $manager->make(storage_path('app') . '/' . $this->filename);
- } catch (NotReadableException $exception) {
- // not an image; delete file and end job
- unlink(storage_path('app') . '/' . $this->filename);
-
- return;
- }
- //create smaller versions if necessary
- if ($image->width() > 1000) {
- $filenameParts = explode('.', $this->filename);
- $extension = array_pop($filenameParts);
- // the following acheives this data flow
- // foo.bar.png => ['foo', 'bar', 'png'] => ['foo', 'bar'] => foo.bar
- $basename = ltrim(array_reduce($filenameParts, function ($carry, $item) {
- return $carry . '.' . $item;
- }, ''), '.');
- $medium = $image->resize(1000, null, function ($constraint) {
- $constraint->aspectRatio();
- });
- Storage::disk('s3')->put('media/'. $basename . '-medium.' . $extension, (string) $medium->encode());
- $small = $image->resize(500, null, function ($constraint) {
- $constraint->aspectRatio();
- });
- Storage::disk('s3')->put('media/' . $basename . '-small.' . $extension, (string) $small->encode());
- }
-
- // now we can delete the locally saved image
- unlink(storage_path('app') . '/' . $this->filename);
- }
-}
diff --git a/app/Jobs/ProcessLike.php b/app/Jobs/ProcessLike.php
index 84fffa7d..3c6028a9 100644
--- a/app/Jobs/ProcessLike.php
+++ b/app/Jobs/ProcessLike.php
@@ -1,59 +1,105 @@
like = $like;
- }
+ public function __construct(
+ protected Like $like
+ ) {}
/**
* Execute the job.
*
- * @return void
+ * @throws GuzzleException
*/
- public function handle(Client $client, Authorship $authorship)
+ public function handle(Client $client, Authorship $authorship): int
{
+ if ($this->isTweet($this->like->url)) {
+ $codebird = resolve(Codebird::class);
+
+ $tweet = $codebird->statuses_oembed(['url' => $this->like->url]);
+
+ $this->like->author_name = $tweet->author_name;
+ $this->like->author_url = $tweet->author_url;
+ $this->like->content = $tweet->html;
+ $this->like->save();
+
+ // POSSE like
+ try {
+ $client->request(
+ 'POST',
+ 'https://brid.gy/publish/webmention',
+ [
+ 'form_params' => [
+ 'source' => $this->like->url,
+ 'target' => 'https://brid.gy/publish/twitter',
+ ],
+ ]
+ );
+ } catch (RequestException) {
+ return 0;
+ }
+
+ return 0;
+ }
+
$response = $client->request('GET', $this->like->url);
$mf2 = \Mf2\parse((string) $response->getBody(), $this->like->url);
- if (array_has($mf2, 'items.0.properties.content')) {
+ if (Arr::has($mf2, 'items.0.properties.content')) {
$this->like->content = $mf2['items'][0]['properties']['content'][0]['html'];
}
try {
$author = $authorship->findAuthor($mf2);
if (is_array($author)) {
- $this->like->author_name = $author['name'];
- $this->like->author_url = $author['url'];
+ $this->like->author_name = Arr::get($author, 'properties.name.0');
+ $this->like->author_url = Arr::get($author, 'properties.url.0');
}
if (is_string($author) && $author !== '') {
$this->like->author_name = $author;
}
} catch (AuthorshipParserException $exception) {
- return;
+ return 1;
}
$this->like->save();
+
+ return 0;
+ }
+
+ /**
+ * Determine if a given URL is that of a Tweet.
+ */
+ private function isTweet(string $url): bool
+ {
+ $host = parse_url($url, PHP_URL_HOST);
+ $parts = array_reverse(explode('.', $host));
+
+ return $parts[0] === 'com' && $parts[1] === 'twitter';
}
}
diff --git a/app/Jobs/ProcessMedia.php b/app/Jobs/ProcessMedia.php
new file mode 100644
index 00000000..b7f36648
--- /dev/null
+++ b/app/Jobs/ProcessMedia.php
@@ -0,0 +1,69 @@
+get('media/' . $this->filename);
+
+ // Open file
+ try {
+ $image = $manager->read($file);
+ } catch (DecoderException) {
+ // not an image; delete file and end job
+ Storage::disk('local')->delete('media/' . $this->filename);
+
+ return;
+ }
+
+ // Save the file publicly
+ Storage::disk('public')->put('media/' . $this->filename, $file);
+
+ // Create smaller versions if necessary
+ if ($image->width() > 1000) {
+ $filenameParts = explode('.', $this->filename);
+ $extension = array_pop($filenameParts);
+ // the following achieves this data flow
+ // foo.bar.png => ['foo', 'bar', 'png'] => ['foo', 'bar'] => foo.bar
+ $basename = trim(implode('.', $filenameParts), '.');
+
+ $medium = $image->resize(width: 1000);
+ Storage::disk('public')->put('media/' . $basename . '-medium.' . $extension, (string) $medium->encode());
+
+ $small = $image->resize(width: 500);
+ Storage::disk('public')->put('media/' . $basename . '-small.' . $extension, (string) $small->encode());
+ }
+
+ // Now we can delete the locally saved image
+ Storage::disk('local')->delete('media/' . $this->filename);
+ }
+}
diff --git a/app/Jobs/ProcessWebMention.php b/app/Jobs/ProcessWebMention.php
index 5a24f003..d92dfa18 100644
--- a/app/Jobs/ProcessWebMention.php
+++ b/app/Jobs/ProcessWebMention.php
@@ -1,141 +1,122 @@
note = $note;
- $this->source = $source;
- }
+ public function __construct(
+ protected Note $note,
+ protected string $source
+ ) {}
/**
* Execute the job.
*
- * @param \Jonnybarnes\WebmentionsParser\Parser $parser
- * @param \GuzzleHttp\Client $guzzle
- * @return void
+ * @throws RemoteContentNotFoundException
+ * @throws GuzzleException
+ * @throws InvalidMentionException
*/
- public function handle(Parser $parser, Client $guzzle)
+ public function handle(Parser $parser, Client $guzzle): void
{
- $remoteContent = $this->getRemoteContent($this->source, $guzzle);
- if ($remoteContent === null) {
+ try {
+ $response = $guzzle->request('GET', $this->source);
+ } catch (RequestException $e) {
throw new RemoteContentNotFoundException;
}
- $microformats = Mf2\parse($remoteContent, $this->source);
+ $this->saveRemoteContent((string) $response->getBody(), $this->source);
+ $microformats = Mf2\parse((string) $response->getBody(), $this->source);
$webmentions = WebMention::where('source', $this->source)->get();
foreach ($webmentions as $webmention) {
- //check webmention still references target
- //we try each type of mention (reply/like/repost)
- if ($webmention->type == 'in-reply-to') {
- if ($parser->checkInReplyTo($microformats, $this->note->longurl) == false) {
- //it doesn't so delete
+ // check webmention still references target
+ // we try each type of mention (reply/like/repost)
+ if ($webmention->type === 'in-reply-to') {
+ if ($parser->checkInReplyTo($microformats, $this->note->uri) === false) {
+ // it doesn’t so delete
$webmention->delete();
return;
}
- //webmenion is still a reply, so update content
+ // webmention is still a reply, so update content
dispatch(new SaveProfileImage($microformats));
$webmention->mf2 = json_encode($microformats);
$webmention->save();
return;
}
- if ($webmention->type == 'like-of') {
- if ($parser->checkLikeOf($microformats, $note->longurl) == false) {
- //it doesn't so delete
+ if ($webmention->type === 'like-of') {
+ if ($parser->checkLikeOf($microformats, $this->note->uri) === false) {
+ // it doesn’t so delete
$webmention->delete();
return;
- } //note we don't need to do anything if it still is a like
+ } // note we don’t need to do anything if it still is a like
}
- if ($webmention->type == 'repost-of') {
- if ($parser->checkRepostOf($microformats, $note->longurl) == false) {
- //it doesn't so delete
+ if ($webmention->type === 'repost-of') {
+ if ($parser->checkRepostOf($microformats, $this->note->uri) === false) {
+ // it doesn’t so delete
$webmention->delete();
return;
- } //again, we don't need to do anything if it still is a repost
+ } // again, we don’t need to do anything if it still is a repost
}
- }//foreach
+ }// foreach
- //no wemention in db so create new one
- $webmention = new WebMention();
- $type = $parser->getMentionType($microformats); //throw error here?
+ // no webmention in the db so create new one
+ $webmention = new WebMention;
+ $type = $parser->getMentionType($microformats); // throw error here?
dispatch(new SaveProfileImage($microformats));
$webmention->source = $this->source;
- $webmention->target = $this->note->longurl;
+ $webmention->target = $this->note->uri;
$webmention->commentable_id = $this->note->id;
- $webmention->commentable_type = 'App\Note';
+ $webmention->commentable_type = Note::class;
$webmention->type = $type;
$webmention->mf2 = json_encode($microformats);
$webmention->save();
}
/**
- * Retreive the remote content from a URL, and caches the result.
- *
- * @param string $url
- * @param GuzzleHttp\client $guzzle
- * @return string|null
+ * Save the HTML of a webmention for future use.
*/
- private function getRemoteContent($url, Client $guzzle)
+ private function saveRemoteContent(string $html, string $url): void
{
- try {
- $response = $guzzle->request('GET', $url);
- } catch (RequestException $e) {
- return;
+ $filenameFromURL = str_replace(
+ ['https://', 'http://'],
+ ['https/', 'http/'],
+ $url
+ );
+ if (str_ends_with($url, '/')) {
+ $filenameFromURL .= 'index.html';
}
- $html = (string) $response->getBody();
- $path = storage_path() . '/HTML/' . $this->createFilenameFromURL($url);
+ $path = storage_path() . '/HTML/' . $filenameFromURL;
$parts = explode('/', $path);
$name = array_pop($parts);
$dir = implode('/', $parts);
- if (! is_dir($dir)) {
- mkdir($dir, 0755, true);
+ if (! is_dir($dir) && ! mkdir($dir, 0755, true) && ! is_dir($dir)) {
+ throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir));
}
file_put_contents("$dir/$name", $html);
-
- return $html;
- }
-
- /**
- * Create a file path from a URL. This is used when caching the HTML
- * response.
- *
- * @param string The URL
- * @return string The path name
- */
- private function createFilenameFromURL($url)
- {
- $url = str_replace(['https://', 'http://'], ['https/', 'http/'], $url);
- if (substr($url, -1) == '/') {
- $url = $url . 'index.html';
- }
-
- return $url;
}
}
diff --git a/app/Jobs/SaveProfileImage.php b/app/Jobs/SaveProfileImage.php
index ffb0452a..08152d5b 100644
--- a/app/Jobs/SaveProfileImage.php
+++ b/app/Jobs/SaveProfileImage.php
@@ -1,66 +1,79 @@
microformats = $microformats;
- }
+ public function __construct(
+ protected array $microformats
+ ) {}
/**
* Execute the job.
- *
- * @return void
*/
- public function handle(Authorship $authorship)
+ public function handle(Authorship $authorship): void
{
try {
$author = $authorship->findAuthor($this->microformats);
- } catch (AuthorshipParserException $e) {
+ } catch (AuthorshipParserException) {
return;
}
- $photo = $author['properties']['photo'][0];
- $home = $author['properties']['url'][0];
- //dont save pbs.twimg.com links
- if (parse_url($photo, PHP_URL_HOST) != 'pbs.twimg.com'
- && parse_url($photo, PHP_URL_HOST) != 'twitter.com') {
- $client = new Client();
+
+ $photo = Arr::get($author, 'properties.photo.0');
+ $home = Arr::get($author, 'properties.url.0');
+
+ if (is_array($photo) && array_key_exists('value', $photo)) {
+ $photo = $photo['value'];
+ }
+
+ if (is_array($home)) {
+ $home = array_shift($home);
+ }
+
+ // dont save pbs.twimg.com links
+ if (
+ $photo
+ && parse_url($photo, PHP_URL_HOST) !== 'pbs.twimg.com'
+ && parse_url($photo, PHP_URL_HOST) !== 'twitter.com'
+ ) {
+ $client = resolve(Client::class);
+
try {
$response = $client->get($photo);
- $image = $response->getBody(true);
- } catch (RequestException $e) {
- // we are openning and reading the default image so that
+ $image = $response->getBody();
+ } catch (RequestException) {
+ // we are opening and reading the default image so that
$default = public_path() . '/assets/profile-images/default-image';
$handle = fopen($default, 'rb');
$image = fread($handle, filesize($default));
fclose($handle);
}
+
$path = public_path() . '/assets/profile-images/' . parse_url($home, PHP_URL_HOST) . '/image';
$parts = explode('/', $path);
$name = array_pop($parts);
$dir = implode('/', $parts);
- if (! is_dir($dir)) {
- mkdir($dir, 0755, true);
+ if (! is_dir($dir) && ! mkdir($dir, 0755, true) && ! is_dir($dir)) {
+ throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir));
}
file_put_contents("$dir/$name", $image);
}
diff --git a/app/Jobs/SaveScreenshot.php b/app/Jobs/SaveScreenshot.php
new file mode 100755
index 00000000..0e07efbd
--- /dev/null
+++ b/app/Jobs/SaveScreenshot.php
@@ -0,0 +1,103 @@
+request('POST', 'https://api.cloudconvert.com/v2/capture-website', [
+ 'headers' => [
+ 'Authorization' => 'Bearer ' . config('services.cloudconvert.token'),
+ ],
+ 'json' => [
+ 'url' => $this->bookmark->url,
+ 'output_format' => 'png',
+ 'screen_width' => 1440,
+ 'screen_height' => 900,
+ 'wait_until' => 'networkidle0',
+ 'wait_time' => 100,
+ ],
+ ]);
+
+ $taskId = json_decode($takeScreenshotJobResponse->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR)->data->id;
+
+ // Now wait till the status job is finished
+ $screenshotJobStatusResponse = $retryClient->request('GET', 'https://api.cloudconvert.com/v2/tasks/' . $taskId, [
+ 'headers' => [
+ 'Authorization' => 'Bearer ' . config('services.cloudconvert.token'),
+ ],
+ 'query' => [
+ 'include' => 'payload',
+ ],
+ ]);
+
+ $finishedCaptureId = json_decode($screenshotJobStatusResponse->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR)->data->id;
+
+ // Now we can create a new job to request thst the screenshot is exported to a temporary URL we can download the screenshot from
+ $exportImageJob = $client->request('POST', 'https://api.cloudconvert.com/v2/export/url', [
+ 'headers' => [
+ 'Authorization' => 'Bearer ' . config('services.cloudconvert.token'),
+ ],
+ 'json' => [
+ 'input' => $finishedCaptureId,
+ 'archive_multiple_files' => false,
+ ],
+ ]);
+
+ $exportImageJobId = json_decode($exportImageJob->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR)->data->id;
+
+ // Again, wait till the status of this export job is finished
+ $finalImageUrlResponse = $retryClient->request('GET', 'https://api.cloudconvert.com/v2/tasks/' . $exportImageJobId, [
+ 'headers' => [
+ 'Authorization' => 'Bearer ' . config('services.cloudconvert.token'),
+ ],
+ 'query' => [
+ 'include' => 'payload',
+ ],
+ ]);
+
+ // Now we can download the screenshot and save it to the storage
+ $finalImageUrl = json_decode($finalImageUrlResponse->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR)->data->result->files[0]->url;
+
+ $finalImageUrlContent = $client->request('GET', $finalImageUrl);
+
+ Storage::disk('public')->put('/assets/img/bookmarks/' . $taskId . '.png', $finalImageUrlContent->getBody()->getContents());
+
+ $this->bookmark->screenshot = $taskId;
+ $this->bookmark->save();
+ }
+}
diff --git a/app/Jobs/SendWebMentions.php b/app/Jobs/SendWebMentions.php
index 5bcbda58..2ff5f2c6 100644
--- a/app/Jobs/SendWebMentions.php
+++ b/app/Jobs/SendWebMentions.php
@@ -1,49 +1,51 @@
note = $note;
- }
+ public function __construct(
+ protected Note $note
+ ) {}
/**
* Execute the job.
*
- * @param \GuzzleHttp\Client $guzzle
- * @return void
+ * @throws GuzzleException
*/
- public function handle(Client $guzzle)
+ public function handle(): void
{
- //grab the URLs
- $urlsInReplyTo = explode(' ', $this->note->in_reply_to);
+ $urlsInReplyTo = explode(' ', $this->note->in_reply_to ?? '');
$urlsNote = $this->getLinks($this->note->note);
- $urls = array_filter(array_merge($urlsInReplyTo, $urlsNote)); //filter out none URLs
+ $urls = array_filter(array_merge($urlsInReplyTo, $urlsNote));
foreach ($urls as $url) {
- $endpoint = $this->discoverWebmentionEndpoint($url, $guzzle);
- if ($endpoint) {
+ $endpoint = $this->discoverWebmentionEndpoint($url);
+ if ($endpoint !== null) {
+ $guzzle = resolve(Client::class);
$guzzle->post($endpoint, [
'form_params' => [
- 'source' => $this->note->longurl,
+ 'source' => $this->note->uri,
'target' => $url,
],
]);
@@ -54,32 +56,31 @@ class SendWebMentions implements ShouldQueue
/**
* Discover if a URL has a webmention endpoint.
*
- * @param string The URL
- * @param \GuzzleHttp\Client $guzzle
- * @return string The webmention endpoint URL
+ * @throws GuzzleException
*/
- public function discoverWebmentionEndpoint($url, $guzzle)
+ public function discoverWebmentionEndpoint(string $url): ?string
{
- //let’s not send webmentions to myself
- if (parse_url($url, PHP_URL_HOST) == config('app.longurl')) {
- return false;
+ // let’s not send webmentions to myself
+ if (parse_url($url, PHP_URL_HOST) === parse_url(config('app.url'), PHP_URL_HOST)) {
+ return null;
}
- if (starts_with($url, '/notes/tagged/')) {
- return false;
+ if (Str::startsWith($url, '/notes/tagged/')) {
+ return null;
}
$endpoint = null;
+ $guzzle = resolve(Client::class);
$response = $guzzle->get($url);
- //check HTTP Headers for webmention endpoint
- $links = \GuzzleHttp\Psr7\parse_header($response->getHeader('Link'));
+ // check HTTP Headers for webmention endpoint
+ $links = Header::parse($response->getHeader('Link'));
foreach ($links as $link) {
- if (mb_stristr($link['rel'], 'webmention')) {
+ if (array_key_exists('rel', $link) && mb_stristr($link['rel'], 'webmention')) {
return $this->resolveUri(trim($link[0], '<>'), $url);
}
}
- //failed to find a header so parse HTML
+ // failed to find a header so parse HTML
$html = (string) $response->getBody();
$mf2 = new \Mf2\Parser($html, $url);
@@ -89,26 +90,25 @@ class SendWebMentions implements ShouldQueue
} elseif (array_key_exists('http://webmention.org/', $rels[0])) {
$endpoint = $rels[0]['http://webmention.org/'][0];
}
- if ($endpoint) {
- return $this->resolveUri($endpoint, $url);
+
+ if ($endpoint === null) {
+ return null;
}
- return false;
+ return $this->resolveUri($endpoint, $url);
}
/**
* Get the URLs from a note.
- *
- * @param string $html
- * @return array $urls
*/
- public function getLinks($html)
+ public function getLinks(?string $html): array
{
- if ($html == '' || is_null($html)) {
+ if ($html === '' || is_null($html)) {
return [];
}
+
$urls = [];
- $dom = new \DOMDocument();
+ $dom = new \DOMDocument;
$dom->loadHTML($html);
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $anchor) {
@@ -120,20 +120,16 @@ class SendWebMentions implements ShouldQueue
/**
* Resolve a URI if necessary.
- *
- * @param string $url
- * @param string $base
- * @return string
*/
public function resolveUri(string $url, string $base): string
{
- $endpoint = \GuzzleHttp\Psr7\uri_for($url);
- if ($endpoint->getScheme() != '') {
+ $endpoint = Utils::uriFor($url);
+ if ($endpoint->getScheme() !== '') {
return (string) $endpoint;
}
- return (string) \GuzzleHttp\Psr7\Uri::resolve(
- \GuzzleHttp\Psr7\uri_for($base),
+ return (string) UriResolver::resolve(
+ Utils::uriFor($base),
$endpoint
);
}
diff --git a/app/Jobs/SyndicateBookmarkToFacebook.php b/app/Jobs/SyndicateBookmarkToFacebook.php
deleted file mode 100644
index 4dbc1641..00000000
--- a/app/Jobs/SyndicateBookmarkToFacebook.php
+++ /dev/null
@@ -1,54 +0,0 @@
-bookmark = $bookmark;
- }
-
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle(Client $guzzle)
- {
- //send webmention
- $response = $guzzle->request(
- 'POST',
- 'https://brid.gy/publish/webmention',
- [
- 'form_params' => [
- 'source' => $this->bookmark->longurl,
- 'target' => 'https://brid.gy/publish/facebook',
- 'bridgy_omit_link' => 'maybe',
- ],
- ]
- );
- //parse for syndication URL
- if ($response->getStatusCode() == 201) {
- $json = json_decode((string) $response->getBody());
- $this->bookmark->update(['syndicates->facebook' => $json->url]);
- $this->bookmark->save();
- }
- }
-}
diff --git a/app/Jobs/SyndicateBookmarkToTwitter.php b/app/Jobs/SyndicateBookmarkToTwitter.php
deleted file mode 100644
index 3d46d160..00000000
--- a/app/Jobs/SyndicateBookmarkToTwitter.php
+++ /dev/null
@@ -1,54 +0,0 @@
-bookmark = $bookmark;
- }
-
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle(Client $guzzle)
- {
- //send webmention
- $response = $guzzle->request(
- 'POST',
- 'https://brid.gy/publish/webmention',
- [
- 'form_params' => [
- 'source' => $this->bookmark->longurl,
- 'target' => 'https://brid.gy/publish/twitter',
- 'bridgy_omit_link' => 'maybe',
- ],
- ]
- );
- //parse for syndication URL
- if ($response->getStatusCode() == 201) {
- $json = json_decode((string) $response->getBody());
- $this->bookmark->update(['syndicates->twitter' => $json->url]);
- $this->bookmark->save();
- }
- }
-}
diff --git a/app/Jobs/SyndicateNoteToBluesky.php b/app/Jobs/SyndicateNoteToBluesky.php
new file mode 100644
index 00000000..e815be34
--- /dev/null
+++ b/app/Jobs/SyndicateNoteToBluesky.php
@@ -0,0 +1,62 @@
+request(
+ 'POST',
+ 'https://brid.gy/micropub',
+ [
+ 'headers' => [
+ 'Authorization' => 'Bearer ' . config('bridgy.bluesky_token'),
+ ],
+ 'json' => [
+ 'type' => ['h-entry'],
+ 'properties' => [
+ 'content' => [$this->note->getRawOriginal('note')],
+ ],
+ ],
+ ]
+ );
+
+ // Parse for syndication URL
+ if ($response->getStatusCode() === 201) {
+ $this->note->bluesky_url = $response->getHeader('Location')[0];
+ $this->note->save();
+ }
+ }
+}
diff --git a/app/Jobs/SyndicateNoteToFacebook.php b/app/Jobs/SyndicateNoteToFacebook.php
deleted file mode 100644
index 34a2e3a2..00000000
--- a/app/Jobs/SyndicateNoteToFacebook.php
+++ /dev/null
@@ -1,54 +0,0 @@
-note = $note;
- }
-
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle(Client $guzzle)
- {
- //send webmention
- $response = $guzzle->request(
- 'POST',
- 'https://brid.gy/publish/webmention',
- [
- 'form_params' => [
- 'source' => $this->note->longurl,
- 'target' => 'https://brid.gy/publish/facebook',
- 'bridgy_omit_link' => 'maybe',
- ],
- ]
- );
- //parse for syndication URL
- if ($response->getStatusCode() == 201) {
- $json = json_decode((string) $response->getBody());
- $this->note->facebook_url = $json->url;
- $this->note->save();
- }
- }
-}
diff --git a/app/Jobs/SyndicateNoteToMastodon.php b/app/Jobs/SyndicateNoteToMastodon.php
new file mode 100644
index 00000000..b79c092c
--- /dev/null
+++ b/app/Jobs/SyndicateNoteToMastodon.php
@@ -0,0 +1,63 @@
+request(
+ 'POST',
+ 'https://brid.gy/micropub',
+ [
+ 'headers' => [
+ 'Authorization' => 'Bearer ' . config('bridgy.mastodon_token'),
+ ],
+ 'json' => [
+ 'type' => ['h-entry'],
+ 'properties' => [
+ 'content' => [$this->note->getRawOriginal('note')],
+ ],
+ ],
+ ]
+ );
+
+ // Parse for syndication URL
+ if ($response->getStatusCode() === 201) {
+ $mastodonUrl = $response->getHeader('Location')[0];
+ $this->note->mastodon_url = $mastodonUrl;
+ $this->note->save();
+ }
+ }
+}
diff --git a/app/Jobs/SyndicateNoteToTwitter.php b/app/Jobs/SyndicateNoteToTwitter.php
deleted file mode 100644
index 97fa4cb5..00000000
--- a/app/Jobs/SyndicateNoteToTwitter.php
+++ /dev/null
@@ -1,56 +0,0 @@
-note = $note;
- }
-
- /**
- * Execute the job.
- *
- * @param \GuzzleHttp\Client $guzzle
- * @return void
- */
- public function handle(Client $guzzle)
- {
- //send webmention
- $response = $guzzle->request(
- 'POST',
- 'https://brid.gy/publish/webmention',
- [
- 'form_params' => [
- 'source' => $this->note->longurl,
- 'target' => 'https://brid.gy/publish/twitter',
- 'bridgy_omit_link' => 'maybe',
- ],
- ]
- );
- //parse for syndication URL
- if ($response->getStatusCode() == 201) {
- $json = json_decode((string) $response->getBody());
- $tweet_id = basename(parse_url($json->url, PHP_URL_PATH));
- $this->note->tweet_id = $tweet_id;
- $this->note->save();
- }
- }
-}
diff --git a/app/Like.php b/app/Like.php
deleted file mode 100644
index aae728e6..00000000
--- a/app/Like.php
+++ /dev/null
@@ -1,44 +0,0 @@
-attributes['url'] = normalize_url($value);
- }
-
- public function setAuthorUrlAttribute($value)
- {
- $this->attributes['author_url'] = normalize_url($value);
- }
-
- public function getContentAttribute($value)
- {
- if ($value === null) {
- return $this->url;
- }
-
- $mf2 = Mf2\parse($value, $this->url);
-
- return $this->filterHTML($mf2['items'][0]['properties']['content'][0]['html']);
- }
-
- public function filterHTML($html)
- {
- $config = HTMLPurifier_Config::createDefault();
- $config->set('Cache.SerializerPath', storage_path() . '/HTMLPurifier');
- $config->set('HTML.TargetBlank', true);
- $purifier = new HTMLPurifier($config);
-
- return $purifier->purify($html);
- }
-}
diff --git a/app/Media.php b/app/Media.php
deleted file mode 100644
index 916fca2d..00000000
--- a/app/Media.php
+++ /dev/null
@@ -1,90 +0,0 @@
-belongsTo('App\Note');
- }
-
- /**
- * Get the URL for an S3 media file.
- *
- * @return string
- */
- public function getUrlAttribute()
- {
- if (starts_with($this->path, 'https://')) {
- return $this->path;
- }
-
- return config('filesystems.disks.s3.url') . '/' . $this->path;
- }
-
- /**
- * Get the URL for the medium size of an S3 image file.
- *
- * @return string
- */
- public function getMediumurlAttribute()
- {
- $basename = $this->getBasename($this->path);
- $extension = $this->getExtension($this->path);
-
- return config('filesystems.disks.s3.url') . '/' . $basename . '-medium.' . $extension;
- }
-
- /**
- * Get the URL for the small size of an S3 image file.
- *
- * @return string
- */
- public function getSmallurlAttribute()
- {
- $basename = $this->getBasename($this->path);
- $extension = $this->getExtension($this->path);
-
- return config('filesystems.disks.s3.url') . '/' . $basename . '-small.' . $extension;
- }
-
- public function getBasename($path)
- {
- $filenameParts = explode('.', $path);
-
- // the following achieves this data flow
- // foo.bar.png => ['foo', 'bar', 'png'] => ['foo', 'bar'] => foo.bar
- $basename = ltrim(array_reduce($filenameParts, function ($carry, $item) {
- return $carry . '.' . $item;
- }, ''), '.');
-
- return $basename;
- }
-
- public function getExtension($path)
- {
- $parts = explode('.', $path);
-
- return array_pop($parts);
- }
-}
diff --git a/app/MicropubClient.php b/app/MicropubClient.php
deleted file mode 100644
index 449f0b29..00000000
--- a/app/MicropubClient.php
+++ /dev/null
@@ -1,32 +0,0 @@
-hasMany('App\Note', 'client_id', 'client_url');
- }
-}
diff --git a/app/Models/Article.php b/app/Models/Article.php
new file mode 100644
index 00000000..bfbd5d51
--- /dev/null
+++ b/app/Models/Article.php
@@ -0,0 +1,131 @@
+ */
+ protected $fillable = [
+ 'url',
+ 'title',
+ 'main',
+ 'published',
+ ];
+
+ /** @var array */
+ protected $casts = [
+ 'created_at' => 'datetime',
+ 'updated_at' => 'datetime',
+ 'deleted_at' => 'datetime',
+ ];
+
+ /**
+ * Return the sluggable configuration array for this model.
+ */
+ public function sluggable(): array
+ {
+ return [
+ 'titleurl' => [
+ 'source' => 'title',
+ ],
+ ];
+ }
+
+ protected function html(): Attribute
+ {
+ return Attribute::get(
+ get: function () {
+ $environment = new Environment;
+ $environment->addExtension(new CommonMarkCoreExtension);
+ $environment->addRenderer(FencedCode::class, new FencedCodeRenderer);
+ $environment->addRenderer(IndentedCode::class, new IndentedCodeRenderer);
+ $markdownConverter = new MarkdownConverter($environment);
+
+ return $markdownConverter->convert($this->main)->getContent();
+ },
+ );
+ }
+
+ protected function w3cTime(): Attribute
+ {
+ return Attribute::get(
+ get: fn () => $this->updated_at->toW3CString(),
+ );
+ }
+
+ protected function tooltipTime(): Attribute
+ {
+ return Attribute::get(
+ get: fn () => $this->updated_at->toRFC850String(),
+ );
+ }
+
+ protected function humanTime(): Attribute
+ {
+ return Attribute::get(
+ get: fn () => $this->updated_at->diffForHumans(),
+ );
+ }
+
+ protected function pubdate(): Attribute
+ {
+ return Attribute::get(
+ get: fn () => $this->updated_at->toRSSString(),
+ );
+ }
+
+ protected function link(): Attribute
+ {
+ return Attribute::get(
+ get: fn () => '/blog/' . $this->updated_at->year . '/' . $this->updated_at->format('m') . '/' . $this->titleurl,
+ );
+ }
+
+ /**
+ * Scope a query to only include articles from a particular year/month.
+ */
+ public function scopeDate(Builder $query, ?int $year = null, ?int $month = null): Builder
+ {
+ if ($year === null) {
+ return $query;
+ }
+ $start = $year . '-01-01 00:00:00';
+ $end = ($year + 1) . '-01-01 00:00:00';
+ if (($month !== null) && ($month !== 12)) {
+ $start = $year . '-' . $month . '-01 00:00:00';
+ $end = $year . '-' . ($month + 1) . '-01 00:00:00';
+ }
+ if ($month === 12) {
+ $start = $year . '-12-01 00:00:00';
+ $end = ($year + 1) . '-01-01 00:00:00';
+ }
+
+ return $query->where([
+ ['updated_at', '>=', $start],
+ ['updated_at', '<', $end],
+ ]);
+ }
+}
diff --git a/app/Models/Bio.php b/app/Models/Bio.php
new file mode 100644
index 00000000..b9a0e78b
--- /dev/null
+++ b/app/Models/Bio.php
@@ -0,0 +1,11 @@
+ */
+ protected $fillable = ['url', 'name', 'content'];
+
+ /** @var array */
+ protected $casts = [
+ 'syndicates' => 'array',
+ ];
+
+ public function tags(): BelongsToMany
+ {
+ return $this->belongsToMany('App\Models\Tag');
+ }
+
+ protected function local_uri(): Attribute
+ {
+ return Attribute::get(
+ get: fn () => config('app.url') . '/bookmarks/' . $this->id,
+ );
+ }
+}
diff --git a/app/Models/Contact.php b/app/Models/Contact.php
new file mode 100644
index 00000000..6f193f41
--- /dev/null
+++ b/app/Models/Contact.php
@@ -0,0 +1,36 @@
+ */
+ protected $fillable = ['nick', 'name', 'homepage', 'twitter', 'facebook'];
+
+ protected function photo(): Attribute
+ {
+ $photo = '/assets/profile-images/default-image';
+
+ if (array_key_exists('homepage', $this->attributes) && ! empty($this->attributes['homepage'])) {
+ $host = parse_url($this->attributes['homepage'], PHP_URL_HOST);
+ if (file_exists(public_path() . '/assets/profile-images/' . $host . '/image')) {
+ $photo = '/assets/profile-images/' . $host . '/image';
+ }
+ }
+
+ return Attribute::make(
+ get: fn () => $photo,
+ );
+ }
+}
diff --git a/app/Models/Like.php b/app/Models/Like.php
new file mode 100644
index 00000000..f9ac3bcb
--- /dev/null
+++ b/app/Models/Like.php
@@ -0,0 +1,56 @@
+ */
+ protected $fillable = ['url'];
+
+ protected function url(): Attribute
+ {
+ return Attribute::set(
+ set: fn ($value) => normalize_url($value),
+ );
+ }
+
+ protected function authorUrl(): Attribute
+ {
+ return Attribute::set(
+ set: fn ($value) => normalize_url($value),
+ );
+ }
+
+ protected function content(): Attribute
+ {
+ return Attribute::get(
+ get: function ($value, $attributes) {
+ if ($value === null) {
+ return null;
+ }
+
+ $mf2 = Mf2\parse($value, $attributes['url']);
+
+ if (Arr::get($mf2, 'items.0.properties.content.0.html')) {
+ return $this->filterHtml(
+ $mf2['items'][0]['properties']['content'][0]['html']
+ );
+ }
+
+ return $value;
+ }
+ );
+ }
+}
diff --git a/app/Models/Media.php b/app/Models/Media.php
new file mode 100644
index 00000000..3d923bed
--- /dev/null
+++ b/app/Models/Media.php
@@ -0,0 +1,102 @@
+ */
+ protected $fillable = ['token', 'path', 'type', 'image_widths'];
+
+ public function note(): BelongsTo
+ {
+ return $this->belongsTo(Note::class);
+ }
+
+ protected function url(): Attribute
+ {
+ return Attribute::get(
+ get: function ($value, $attributes) {
+ if (Str::startsWith($attributes['path'], 'https://')) {
+ return $attributes['path'];
+ }
+
+ return config('app.url') . '/storage/' . $attributes['path'];
+ }
+ );
+ }
+
+ protected function mediumurl(): Attribute
+ {
+ return Attribute::get(
+ get: fn ($value, $attributes) => $this->getSizeUrl($attributes['path'], 'medium'),
+ );
+ }
+
+ protected function smallurl(): Attribute
+ {
+ return Attribute::get(
+ get: fn ($value, $attributes) => $this->getSizeUrl($attributes['path'], 'small'),
+ );
+ }
+
+ protected function mimetype(): Attribute
+ {
+ return Attribute::get(
+ get: function ($value, $attributes) {
+ $extension = $this->getExtension($attributes['path']);
+
+ return match ($extension) {
+ 'gif' => 'image/gif',
+ 'jpeg', 'jpg' => 'image/jpeg',
+ 'png' => 'image/png',
+ 'svg' => 'image/svg+xml',
+ 'tiff' => 'image/tiff',
+ 'webp' => 'image/webp',
+ 'mp4' => 'video/mp4',
+ 'mkv' => 'video/mkv',
+ default => 'application/octet-stream',
+ };
+ },
+ );
+ }
+
+ private function getSizeUrl(string $path, string $size): string
+ {
+ $basename = $this->getBasename($path);
+ $extension = $this->getExtension($path);
+
+ return config('app.url') . '/storage/' . $basename . '-' . $size . '.' . $extension;
+ }
+
+ private function getBasename(string $path): string
+ {
+ // the following achieves this data flow
+ // foo.bar.png => ['foo', 'bar', 'png'] => ['foo', 'bar'] => foo.bar
+ $filenameParts = explode('.', $path);
+ array_pop($filenameParts);
+
+ return ltrim(array_reduce($filenameParts, static function ($carry, $item) {
+ return $carry . '.' . $item;
+ }, ''), '.');
+ }
+
+ private function getExtension(string $path): string
+ {
+ $parts = explode('.', $path);
+
+ return array_pop($parts);
+ }
+}
diff --git a/app/Models/MicropubClient.php b/app/Models/MicropubClient.php
new file mode 100644
index 00000000..669c7284
--- /dev/null
+++ b/app/Models/MicropubClient.php
@@ -0,0 +1,25 @@
+ */
+ protected $fillable = ['client_url', 'client_name'];
+
+ public function notes(): HasMany
+ {
+ return $this->hasMany('App\Models\Note', 'client_id', 'client_url');
+ }
+}
diff --git a/app/Models/Note.php b/app/Models/Note.php
new file mode 100644
index 00000000..74533443
--- /dev/null
+++ b/app/Models/Note.php
@@ -0,0 +1,465 @@
+contacts = null;
+ }
+
+ /** @var string */
+ protected $table = 'notes';
+
+ /** @var array */
+ protected $fillable = [
+ 'note',
+ 'in_reply_to',
+ 'client_id',
+ ];
+
+ /** @var array */
+ protected $hidden = ['searchable'];
+
+ public function tags(): BelongsToMany
+ {
+ return $this->belongsToMany(Tag::class);
+ }
+
+ public function client(): BelongsTo
+ {
+ return $this->belongsTo(MicropubClient::class, 'client_id', 'client_url');
+ }
+
+ public function webmentions(): MorphMany
+ {
+ return $this->morphMany(WebMention::class, 'commentable');
+ }
+
+ public function place(): BelongsTo
+ {
+ return $this->belongsTo(Place::class);
+ }
+
+ public function media(): HasMany
+ {
+ return $this->hasMany(Media::class);
+ }
+
+ /**
+ * @return array
+ */
+ public function toSearchableArray(): array
+ {
+ return [
+ 'note' => $this->note,
+ ];
+ }
+
+ public function setNoteAttribute(?string $value): void
+ {
+ if ($value !== null) {
+ $normalized = normalizer_normalize($value, Normalizer::FORM_C);
+ if ($normalized === '') { // we don’t want to save empty strings to the db
+ $normalized = null;
+ }
+ $this->attributes['note'] = $normalized;
+ }
+ }
+
+ /**
+ * Pre-process notes for web-view.
+ */
+ public function getNoteAttribute(?string $value): ?string
+ {
+ if ($value === null && $this->place !== null) {
+ $value = '📍: ' . $this->place->name . '';
+ }
+
+ // if $value is still null, just return null
+ if ($value === null) {
+ return null;
+ }
+
+ $hashtags = $this->autoLinkHashtag($value);
+
+ return $this->convertMarkdown($hashtags);
+ }
+
+ /**
+ * Provide the content_html for JSON feed.
+ *
+ * In particular, we want to include media links such as images.
+ */
+ public function getContentAttribute(): string
+ {
+ $note = $this->getRawOriginal('note');
+
+ foreach ($this->media as $media) {
+ if ($media->type === 'image') {
+ $note .= PHP_EOL . '
';
+ }
+ if ($media->type === 'audio') {
+ $note .= PHP_EOL . '