2016-10-22 01:08:26 -04:00
<? php
class ReportActions extends Actions
{
2019-10-10 18:08:09 +02:00
/**
* Handles all routing requests without a claimId as parameter.
* E.g. /dmca
*
* @return array
* @throws Exception
*/
2021-05-14 20:10:54 +08:00
public static function executeDmca ()
{
2019-10-10 18:08:09 +02:00
self :: setResponseHeader ();
self :: redirectIfPostRequest ( false , '' );
2018-05-21 11:49:15 -04:00
$values = [];
$errors = [];
2019-10-10 18:08:09 +02:00
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
*/
2021-05-14 20:10:54 +08:00
public static function executeDmcaWithClaimId ( string $claimId ) : array
{
2019-10-10 18:08:09 +02:00
$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 )
{
2018-05-21 11:49:15 -04:00
foreach ([ 'name' , 'email' , 'rightsholder' , 'identifier' ] as $field ) {
$value = Request :: getPostParam ( $field );
if ( ! $value ) {
$errors [ $field ] = __ ( 'form_error.required' );
2019-10-10 18:08:09 +02:00
} elseif ( $field === 'email' && ! filter_var ( $value , FILTER_VALIDATE_EMAIL )) {
2018-05-21 11:49:15 -04:00
$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 );
}
2019-10-10 18:08:09 +02:00
}
/**
* 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
*/
2021-05-14 20:10:54 +08:00
private static function redirectIfPostRequest ( bool $hasClaimId = false , string $claimId = '' )
{
2019-10-10 18:08:09 +02:00
if ( $hasClaimId && ! empty ( $claimId )) {
$returnValue = [ 'report/dmca' , [ 'claimId' => $claimId ]];
} else {
$returnValue = [ 'report/dmca' ];
}
if ( ! Request :: isPost ()) {
return $returnValue ;
}
2016-10-22 01:08:26 -04:00
}
2018-05-21 11:49:15 -04:00
}