1
0
Fork 0
pixelfed/app/Http/Controllers/Api/ApiV1Dot1Controller.php

637 lines
20 KiB
PHP
Raw Normal View History

2022-07-20 02:05:00 +00:00
<?php
namespace App\Http\Controllers\Api;
use Cache;
2022-09-25 02:52:19 +00:00
use DB;
2022-07-20 02:05:00 +00:00
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use League\Fractal;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
2022-09-25 02:52:19 +00:00
use App\AccountLog;
use App\EmailVerification;
2022-07-20 02:05:00 +00:00
use App\Status;
use App\Report;
use App\Profile;
2023-01-28 02:29:44 +00:00
use App\StatusArchived;
2022-11-26 03:49:06 +00:00
use App\User;
use App\Services\AccountService;
2022-09-19 09:49:45 +00:00
use App\Services\StatusService;
use App\Services\ProfileStatusService;
2022-11-26 03:49:06 +00:00
use App\Util\Lexer\RestrictedNames;
use App\Services\EmailService;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
2022-09-25 02:52:19 +00:00
use Jenssegers\Agent\Agent;
2022-09-25 04:31:17 +00:00
use Mail;
use App\Mail\PasswordChange;
2022-11-26 03:49:06 +00:00
use App\Mail\ConfirmAppEmail;
2023-01-28 02:29:44 +00:00
use App\Http\Resources\StatusStateless;
2022-07-20 02:05:00 +00:00
class ApiV1Dot1Controller extends Controller
{
protected $fractal;
public function __construct()
{
$this->fractal = new Fractal\Manager();
$this->fractal->setSerializer(new ArraySerializer());
}
public function json($res, $code = 200, $headers = [])
{
return response()->json($res, $code, $headers, JSON_UNESCAPED_SLASHES);
}
public function error($msg, $code = 400, $extra = [], $headers = [])
{
$res = [
"msg" => $msg,
"code" => $code
];
return response()->json(array_merge($res, $extra), $code, $headers, JSON_UNESCAPED_SLASHES);
}
public function report(Request $request)
{
$user = $request->user();
abort_if(!$user, 403);
abort_if($user->status != null, 403);
$report_type = $request->input('report_type');
$object_id = $request->input('object_id');
$object_type = $request->input('object_type');
$types = [
'spam',
'sensitive',
'abusive',
'underage',
'violence',
'copyright',
'impersonation',
'scam',
'terrorism'
];
if (!$report_type || !$object_id || !$object_type) {
return $this->error("Invalid or missing parameters", 400, ["error_code" => "ERROR_INVALID_PARAMS"]);
}
if (!in_array($report_type, $types)) {
return $this->error("Invalid report type", 400, ["error_code" => "ERROR_TYPE_INVALID"]);
}
if ($object_type === "user" && $object_id == $user->profile_id) {
return $this->error("Cannot self report", 400, ["error_code" => "ERROR_NO_SELF_REPORTS"]);
}
$rpid = null;
switch ($object_type) {
case 'post':
$object = Status::find($object_id);
if (!$object) {
return $this->error("Invalid object id", 400, ["error_code" => "ERROR_INVALID_OBJECT_ID"]);
}
$object_type = 'App\Status';
$exists = Report::whereUserId($user->id)
->whereObjectId($object->id)
->whereObjectType('App\Status')
->count();
$rpid = $object->profile_id;
break;
case 'user':
$object = Profile::find($object_id);
if (!$object) {
return $this->error("Invalid object id", 400, ["error_code" => "ERROR_INVALID_OBJECT_ID"]);
}
$object_type = 'App\Profile';
$exists = Report::whereUserId($user->id)
->whereObjectId($object->id)
->whereObjectType('App\Profile')
->count();
$rpid = $object->id;
break;
default:
return $this->error("Invalid report type", 400, ["error_code" => "ERROR_REPORT_OBJECT_TYPE_INVALID"]);
break;
}
if ($exists !== 0) {
return $this->error("Duplicate report", 400, ["error_code" => "ERROR_REPORT_DUPLICATE"]);
}
if ($object->profile_id == $user->profile_id) {
return $this->error("Cannot self report", 400, ["error_code" => "ERROR_NO_SELF_REPORTS"]);
}
$report = new Report;
$report->profile_id = $user->profile_id;
$report->user_id = $user->id;
$report->object_id = $object->id;
$report->object_type = $object_type;
$report->reported_profile_id = $rpid;
$report->type = $report_type;
$report->save();
$res = [
"msg" => "Successfully sent report",
"code" => 200
];
return $this->json($res);
}
/**
* DELETE /api/v1.1/accounts/avatar
*
* @return \App\Transformer\Api\AccountTransformer
*/
public function deleteAvatar(Request $request)
{
$user = $request->user();
abort_if(!$user, 403);
abort_if($user->status != null, 403);
$avatar = $user->profile->avatar;
if( $avatar->media_path == 'public/avatars/default.png' ||
$avatar->media_path == 'public/avatars/default.jpg'
) {
return AccountService::get($user->profile_id);
}
if(is_file(storage_path('app/' . $avatar->media_path))) {
@unlink(storage_path('app/' . $avatar->media_path));
}
$avatar->media_path = 'public/avatars/default.jpg';
$avatar->change_count = $avatar->change_count + 1;
$avatar->save();
Cache::forget('avatar:' . $user->profile_id);
Cache::forget("avatar:{$user->profile_id}");
Cache::forget('user:account:id:'.$user->id);
AccountService::del($user->profile_id);
return AccountService::get($user->profile_id);
}
2022-09-19 09:49:45 +00:00
/**
* GET /api/v1.1/accounts/{id}/posts
*
* @return \App\Transformer\Api\StatusTransformer
*/
public function accountPosts(Request $request, $id)
{
$user = $request->user();
abort_if(!$user, 403);
abort_if($user->status != null, 403);
$account = AccountService::get($id);
if(!$account || $account['username'] !== $request->input('username')) {
return $this->json([]);
}
$posts = ProfileStatusService::get($id);
if(!$posts) {
return $this->json([]);
}
$res = collect($posts)
->map(function($id) {
return StatusService::get($id);
})
->filter(function($post) {
return $post && isset($post['account']);
})
->toArray();
return $this->json($res);
}
2022-09-25 02:52:19 +00:00
/**
* POST /api/v1.1/accounts/change-password
*
* @return \App\Transformer\Api\AccountTransformer
*/
public function accountChangePassword(Request $request)
{
$user = $request->user();
abort_if(!$user, 403);
abort_if($user->status != null, 403);
$this->validate($request, [
'current_password' => 'bail|required|current_password',
'new_password' => 'required|min:' . config('pixelfed.min_password_length', 8),
'confirm_password' => 'required|same:new_password'
],[
'current_password' => 'The password you entered is incorrect'
]);
$user->password = bcrypt($request->input('new_password'));
$user->save();
2022-09-25 04:31:17 +00:00
$log = new AccountLog;
$log->user_id = $user->id;
$log->item_id = $user->id;
$log->item_type = 'App\User';
$log->action = 'account.edit.password';
$log->message = 'Password changed';
$log->link = null;
$log->ip_address = $request->ip();
$log->user_agent = $request->userAgent();
$log->save();
Mail::to($request->user())->send(new PasswordChange($user));
2022-09-25 02:52:19 +00:00
return $this->json(AccountService::get($user->profile_id));
}
/**
* GET /api/v1.1/accounts/login-activity
*
* @return array
*/
public function accountLoginActivity(Request $request)
{
$user = $request->user();
abort_if(!$user, 403);
abort_if($user->status != null, 403);
$agent = new Agent();
2022-09-25 05:20:45 +00:00
$currentIp = $request->ip();
2022-09-25 02:52:19 +00:00
$activity = AccountLog::whereUserId($user->id)
->whereAction('auth.login')
->orderBy('created_at', 'desc')
2022-09-25 05:38:08 +00:00
->groupBy('ip_address')
2022-09-25 02:52:19 +00:00
->limit(10)
->get()
2022-09-25 05:20:45 +00:00
->map(function($item) use($agent, $currentIp) {
2022-09-25 02:52:19 +00:00
$agent->setUserAgent($item->user_agent);
return [
'id' => $item->id,
'action' => $item->action,
'ip' => $item->ip_address,
2022-09-25 05:20:45 +00:00
'ip_current' => $item->ip_address === $currentIp,
2022-09-25 02:52:19 +00:00
'is_mobile' => $agent->isMobile(),
'device' => $agent->device(),
'browser' => $agent->browser(),
'platform' => $agent->platform(),
'created_at' => $item->created_at->format('c')
];
});
return $this->json($activity);
}
/**
* GET /api/v1.1/accounts/two-factor
*
* @return array
*/
public function accountTwoFactor(Request $request)
{
$user = $request->user();
abort_if(!$user, 403);
abort_if($user->status != null, 403);
$res = [
'active' => (bool) $user->{'2fa_enabled'},
'setup_at' => $user->{'2fa_setup_at'}
];
return $this->json($res);
}
/**
* GET /api/v1.1/accounts/emails-from-pixelfed
*
* @return array
*/
public function accountEmailsFromPixelfed(Request $request)
{
$user = $request->user();
abort_if(!$user, 403);
abort_if($user->status != null, 403);
2022-09-25 09:00:00 +00:00
$from = config('mail.from.address');
2022-09-25 02:52:19 +00:00
$emailVerifications = EmailVerification::whereUserId($user->id)
->orderByDesc('id')
->where('created_at', '>', now()->subDays(14))
->limit(10)
->get()
2022-09-25 09:00:00 +00:00
->map(function($mail) use($user, $from) {
2022-09-25 02:52:19 +00:00
return [
'type' => 'Email Verification',
2022-09-25 09:00:00 +00:00
'subject' => 'Confirm Email',
'to_address' => $user->email,
'from_address' => $from,
2022-09-25 09:28:43 +00:00
'created_at' => str_replace('@', 'at', $mail->created_at->format('M j, Y @ g:i:s A'))
2022-09-25 02:52:19 +00:00
];
})
->toArray();
$passwordResets = DB::table('password_resets')
->whereEmail($user->email)
->where('created_at', '>', now()->subDays(14))
->orderByDesc('created_at')
->limit(10)
->get()
2022-09-25 09:00:00 +00:00
->map(function($mail) use($user, $from) {
2022-09-25 02:52:19 +00:00
return [
'type' => 'Password Reset',
2022-09-25 09:00:00 +00:00
'subject' => 'Reset Password Notification',
'to_address' => $user->email,
'from_address' => $from,
2022-09-25 09:28:43 +00:00
'created_at' => str_replace('@', 'at', now()->parse($mail->created_at)->format('M j, Y @ g:i:s A'))
2022-09-25 02:52:19 +00:00
];
})
->toArray();
2022-09-25 04:31:17 +00:00
$passwordChanges = AccountLog::whereUserId($user->id)
->whereAction('account.edit.password')
->where('created_at', '>', now()->subDays(14))
->orderByDesc('created_at')
->limit(10)
->get()
2022-09-25 09:00:00 +00:00
->map(function($mail) use($user, $from) {
2022-09-25 04:31:17 +00:00
return [
'type' => 'Password Change',
2022-09-25 09:00:00 +00:00
'subject' => 'Password Change',
'to_address' => $user->email,
'from_address' => $from,
2022-09-25 09:28:43 +00:00
'created_at' => str_replace('@', 'at', now()->parse($mail->created_at)->format('M j, Y @ g:i:s A'))
2022-09-25 04:31:17 +00:00
];
})
->toArray();
2022-09-25 09:00:00 +00:00
$res = collect([])
->merge($emailVerifications)
->merge($passwordResets)
->merge($passwordChanges)
->sortByDesc('created_at')
->values();
2022-09-25 02:52:19 +00:00
return $this->json($res);
}
/**
* GET /api/v1.1/accounts/apps-and-applications
*
* @return array
*/
public function accountApps(Request $request)
{
$user = $request->user();
abort_if(!$user, 403);
abort_if($user->status != null, 403);
2022-09-25 14:16:01 +00:00
$res = $user->tokens->sortByDesc('created_at')->take(10)->map(function($token, $key) {
2022-09-25 02:52:19 +00:00
return [
'id' => $key + 1,
'did' => encrypt($token->id),
2022-09-25 14:11:44 +00:00
'name' => $token->client->name,
2022-09-25 02:52:19 +00:00
'scopes' => $token->scopes,
'revoked' => $token->revoked,
2022-09-25 14:11:44 +00:00
'created_at' => str_replace('@', 'at', now()->parse($token->created_at)->format('M j, Y @ g:i:s A')),
'expires_at' => str_replace('@', 'at', now()->parse($token->expires_at)->format('M j, Y @ g:i:s A'))
2022-09-25 02:52:19 +00:00
];
});
return $this->json($res);
}
2022-11-26 03:49:06 +00:00
public function inAppRegistrationPreFlightCheck(Request $request)
{
return [
'open' => config('pixelfed.open_registration'),
'iara' => config('pixelfed.allow_app_registration')
];
}
public function inAppRegistration(Request $request)
{
abort_if($request->user(), 404);
abort_unless(config('pixelfed.open_registration'), 404);
abort_unless(config('pixelfed.allow_app_registration'), 404);
abort_unless($request->hasHeader('X-PIXELFED-APP'), 403);
$this->validate($request, [
'email' => [
'required',
'string',
'email',
'max:255',
'unique:users',
function ($attribute, $value, $fail) {
$banned = EmailService::isBanned($value);
if($banned) {
return $fail('Email is invalid.');
}
},
],
'username' => [
'required',
'min:2',
'max:15',
'unique:users',
function ($attribute, $value, $fail) {
$dash = substr_count($value, '-');
$underscore = substr_count($value, '_');
$period = substr_count($value, '.');
if(ends_with($value, ['.php', '.js', '.css'])) {
return $fail('Username is invalid.');
}
if(($dash + $underscore + $period) > 1) {
return $fail('Username is invalid. Can only contain one dash (-), period (.) or underscore (_).');
}
if (!ctype_alnum($value[0])) {
return $fail('Username is invalid. Must start with a letter or number.');
}
if (!ctype_alnum($value[strlen($value) - 1])) {
return $fail('Username is invalid. Must end with a letter or number.');
}
$val = str_replace(['_', '.', '-'], '', $value);
if(!ctype_alnum($val)) {
return $fail('Username is invalid. Username must be alpha-numeric and may contain dashes (-), periods (.) and underscores (_).');
}
$restricted = RestrictedNames::get();
if (in_array(strtolower($value), array_map('strtolower', $restricted))) {
return $fail('Username cannot be used.');
}
},
],
'password' => 'required|string|min:8',
]);
$email = $request->input('email');
$username = $request->input('username');
$password = $request->input('password');
if(config('database.default') == 'pgsql') {
$username = strtolower($username);
$email = strtolower($email);
}
$user = new User;
$user->name = $username;
$user->username = $username;
$user->email = $email;
$user->password = Hash::make($password);
$user->register_source = 'app';
$user->app_register_ip = $request->ip();
$user->app_register_token = Str::random(32);
$user->save();
$rtoken = Str::random(mt_rand(64, 70));
$verify = new EmailVerification();
$verify->user_id = $user->id;
$verify->email = $user->email;
$verify->user_token = $user->app_register_token;
$verify->random_token = $rtoken;
$verify->save();
2022-12-02 12:29:26 +00:00
$appUrl = url('/api/v1.1/auth/iarer?ut=' . $user->app_register_token . '&rt=' . $rtoken);
2022-11-26 03:49:06 +00:00
Mail::to($user->email)->send(new ConfirmAppEmail($verify, $appUrl));
return response()->json([
'success' => true,
]);
}
2022-12-02 12:29:26 +00:00
public function inAppRegistrationEmailRedirect(Request $request)
{
$this->validate($request, [
'ut' => 'required',
'rt' => 'required'
]);
$ut = $request->input('ut');
$rt = $request->input('rt');
$url = 'pixelfed://confirm-account/'. $ut . '?rt=' . $rt;
return redirect()->away($url);
}
2022-11-26 03:49:06 +00:00
public function inAppRegistrationConfirm(Request $request)
{
abort_if($request->user(), 404);
abort_unless(config('pixelfed.open_registration'), 404);
abort_unless(config('pixelfed.allow_app_registration'), 404);
abort_unless($request->hasHeader('X-PIXELFED-APP'), 403);
$this->validate($request, [
'user_token' => 'required',
'random_token' => 'required',
'email' => 'required'
]);
$verify = EmailVerification::whereEmail($request->input('email'))
->whereUserToken($request->input('user_token'))
->whereRandomToken($request->input('random_token'))
->first();
if(!$verify) {
return response()->json(['error' => 'Invalid tokens'], 403);
}
if($verify->created_at->lt(now()->subHours(24))) {
$verify->delete();
return response()->json(['error' => 'Invalid tokens'], 403);
}
2022-11-26 03:49:06 +00:00
$user = User::findOrFail($verify->user_id);
$user->email_verified_at = now();
2022-11-28 09:34:30 +00:00
$user->last_active_at = now();
2022-11-26 03:49:06 +00:00
$user->save();
$token = $user->createToken('Pixelfed');
return response()->json([
2022-11-28 09:34:30 +00:00
'access_token' => $token->accessToken
2022-11-26 03:49:06 +00:00
]);
}
2023-01-28 02:29:44 +00:00
public function archive(Request $request, $id)
{
abort_if(!$request->user(), 403);
$status = Status::whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->whereProfileId($request->user()->profile_id)
->findOrFail($id);
if($status->scope === 'archived') {
return [200];
}
$archive = new StatusArchived;
$archive->status_id = $status->id;
$archive->profile_id = $status->profile_id;
$archive->original_scope = $status->scope;
$archive->save();
$status->scope = 'archived';
$status->visibility = 'draft';
$status->save();
StatusService::del($status->id, true);
AccountService::syncPostCount($status->profile_id);
return [200];
}
public function unarchive(Request $request, $id)
{
abort_if(!$request->user(), 403);
$status = Status::whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->whereProfileId($request->user()->profile_id)
->findOrFail($id);
if($status->scope !== 'archived') {
return [200];
}
$archive = StatusArchived::whereStatusId($status->id)
->whereProfileId($status->profile_id)
->firstOrFail();
$status->scope = $archive->original_scope;
$status->visibility = $archive->original_scope;
$status->save();
$archive->delete();
StatusService::del($status->id, true);
AccountService::syncPostCount($status->profile_id);
return [200];
}
public function archivedPosts(Request $request)
{
abort_if(!$request->user(), 403);
$statuses = Status::whereProfileId($request->user()->profile_id)
->whereScope('archived')
->orderByDesc('id')
->cursorPaginate(10);
return StatusStateless::collection($statuses);
}
2022-07-20 02:05:00 +00:00
}