forked from lbryio/lbry.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReportActions.class.php
116 lines (94 loc) · 3.05 KB
/
ReportActions.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
class ReportActions extends Actions
{
/**
* Handles all routing requests without a claimId as parameter.
* E.g. /dmca
*
* @return array
* @throws Exception
*/
public static function executeDmca() {
self::setResponseHeader();
self::redirectIfPostRequest(false, '');
$values = [];
$errors = [];
self::validateForm($values, $errors);
return ['report/dmca', [
'errors' => $errors,
'values' => $values
]];
}
/**
* Handles all routing requests with a claimId as routing parameter since the used router doesn't support optional parameters.
* E.g. /dmca/this-is-a-claim-id
*
* @param string $claimId
*
* @return array
* @throws Exception
*/
public static function executeDmcaWithClaimId(string $claimId): array {
$claimId = htmlspecialchars($claimId);
self::setResponseHeader();
self::redirectIfPostRequest(true, $claimId);
$values = [];
$errors = [];
self::validateForm($values, $errors);
return ['report/dmca', [
'errors' => $errors,
'values' => $values,
'claimId' => $claimId
]];
}
/**
* @param array $values
* @param array $errors
*
* @return array
* @throws Exception
*/
private static function validateForm(array $values, array $errors)
{
foreach (['name', 'email', 'rightsholder', 'identifier'] as $field) {
$value = Request::getPostParam($field);
if (!$value) {
$errors[$field] = __('form_error.required');
} elseif ($field === 'email' && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
$errors[$field] = __('form_error.invalid');
}
$values[$field] = $value;
}
if (!$errors) {
$values['report_id'] = Encoding::base58Encode(random_bytes(6));
Mailgun::sendDmcaReport($values);
Session::setFlash('success', '<h3>Report Submitted</h3><p>We will respond shortly.</p><p>This ID for this report is <strong>' . $values['report_id'] . '</strong>. Please reference this ID when contacting us regarding this report.</p>');
return Controller::redirect(Request::getRelativeUri(), 303);
}
}
/**
* Sets the response header.
*/
private static function setResponseHeader()
{
Response::setHeader(Response::HEADER_CROSS_ORIGIN, "*");
}
/**
* Chooses the right template according to passed variables.
*
* @param bool $hasClaimId
* @param string $claimId
*
* @return array
*/
private static function redirectIfPostRequest(bool $hasClaimId = false, string $claimId = '') {
if ($hasClaimId && !empty($claimId)) {
$returnValue = ['report/dmca', ['claimId' => $claimId]];
} else {
$returnValue = ['report/dmca'];
}
if (!Request::isPost()) {
return $returnValue;
}
}
}