misuzu/public/relations.php

81 lines
2.2 KiB
PHP
Raw Normal View History

2018-09-19 00:16:29 +02:00
<?php
namespace Misuzu;
2020-05-29 19:07:18 +00:00
use Misuzu\Config;
2020-05-25 19:58:06 +00:00
use Misuzu\Users\User;
2020-05-29 19:07:18 +00:00
use Misuzu\Users\UserNotFoundException;
use Misuzu\Users\UserRelation;
2020-05-25 19:58:06 +00:00
require_once '../misuzu.php';
2018-09-19 00:16:29 +02:00
// basing whether or not this is an xhr request on whether a referrer header is present
// this page is never directy accessed, under normal circumstances
$redirect = !empty($_SERVER['HTTP_REFERER']) && empty($_SERVER['HTTP_X_MISUZU_XHR']) ? $_SERVER['HTTP_REFERER'] : '';
$isXHR = !$redirect;
2019-06-10 19:04:53 +02:00
if($isXHR) {
header('Content-Type: application/json; charset=utf-8');
2019-06-10 19:04:53 +02:00
} elseif(!is_local_url($redirect)) {
echo render_info('Possible request forgery detected.', 403);
2018-09-19 00:16:29 +02:00
return;
}
2019-12-11 19:10:54 +01:00
if(!CSRF::validateRequest()) {
echo render_info_or_json($isXHR, "Couldn't verify this request, please refresh the page and try again.", 403);
return;
}
2019-12-11 19:10:54 +01:00
header(CSRF::header());
2020-05-25 19:58:06 +00:00
$currentUser = User::getCurrent();
if($currentUser === null) {
echo render_info_or_json($isXHR, 'You must be logged in to manage relations.', 401);
return;
}
2020-05-25 19:58:06 +00:00
if(user_warning_check_expiration($currentUser->getId(), MSZ_WARN_BAN) > 0) {
echo render_info_or_json($isXHR, 'You have been banned, check your profile for more information.', 403);
2018-09-19 00:16:29 +02:00
return;
}
2019-03-18 23:02:30 +01:00
$subjectId = !empty($_GET['u']) && is_string($_GET['u']) ? (int)$_GET['u'] : 0;
2019-04-03 19:48:55 +02:00
$relationType = isset($_GET['m']) && is_string($_GET['m']) ? (int)$_GET['m'] : -1;
2020-05-29 19:07:18 +00:00
if($relationType < 0) {
echo render_info_or_json($isXHR, 'Invalid relation type.', 400);
return;
}
2020-05-29 19:07:18 +00:00
$relationType = $relationType > 0 ? UserRelation::TYPE_FOLLOW : UserRelation::TYPE_NONE;
2020-05-29 19:07:18 +00:00
try {
$subjectInfo = User::byId($subjectId);
} catch(UserNotFoundException $ex) {
echo render_info_or_json($isXHR, "That user doesn't exist.", 400);
return;
}
2020-05-29 19:07:18 +00:00
if($relationType > 0)
$subjectInfo->addFollower($currentUser);
else
$subjectInfo->removeRelation($currentUser);
2019-04-03 19:48:55 +02:00
2020-05-29 19:07:18 +00:00
if(in_array($subjectInfo->getId(), Config::get('relations.replicate', Config::TYPE_ARR))) {
if($relationType > 0)
$currentUser->addFollower($subjectInfo);
else
$currentUser->removeRelation($subjectInfo);
2019-04-03 19:48:55 +02:00
}
2019-06-10 19:04:53 +02:00
if(!$isXHR) {
redirect($redirect);
return;
}
2018-09-19 00:16:29 +02:00
echo json_encode([
2020-05-25 19:58:06 +00:00
'user_id' => $currentUser->getId(),
2020-05-29 19:07:18 +00:00
'subject_id' => $subjectInfo->getId(),
'relation_type' => $relationType,
]);