Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
veegishx committed Jul 12, 2018
0 parents commit 1a4a53b
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 0 deletions.
13 changes: 13 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "veegish/censorship_http451",
"description": "A drupal module to handle http 451 responses",
"license": "GPL-2.0+",
"authors": [
{
"name": "veegishx",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {}
}
13 changes: 13 additions & 0 deletions config/install/http451.settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
https451:
page_title: '451 Unavailable For Legal Reasons'
page_status: '451'
page_content: '<html>
<head><title>Unavailable For Legal Reasons</title></head>
<body>
<h1>Unavailable For Legal Reasons</h1>
<p>This request may not be serviced in the Roman Province
of Judea due to the Lex Julia Majestatis, which disallows
access to resources hosted on servers deemed to be
operated by the People's Front of Judea.</p>
</body>
</html>'
23 changes: 23 additions & 0 deletions config/schema/http451.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
http451.settings:
type: config_object
label: 'HTTP451 Settings'
mapping:
http451:
type: mapping
mapping:
page_title:
type: text
label: 'Default title for HTTP451 status page:'
page_content:
type: text
label: 'Default message for HTTP451 error page:'
page_status:
type: text
label: 'Server HTTP 451 Response code'
block.settings.http451:
type: block_settings
label: 'HTTP451 block'
mapping:
http451_block_settings:
type: text
label: 'HTTP451 block settings'
7 changes: 7 additions & 0 deletions http451.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: Http 451
description: This module allows pages to display a http 451 error message
package: Http451
type: module
core: 8.x

configure: http451.form
5 changes: 5 additions & 0 deletions http451.links.menu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
http451.form:
title: http451 Settings
description: Configure the settings for http451 module
route_name: http451.form
parent: system.admin_config_system
16 changes: 16 additions & 0 deletions http451.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
use Drupal\Core\Routing\RouteMatchInterface;

/**
* Implements hook_help().
*/

function http451_help($route_name, RouteMatchInterface $route_match) {
switch($route_name) {
case 'help.page.censorship_http451' :
return t('
<h3> About </h3>
<p>This Drupal module implements http 451 handling as defined by <a href="https://tools.ietf.org/html/rfc7725">RFC 7725</a></p>
');
}
}
7 changes: 7 additions & 0 deletions http451.permissions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
configure_http451:
title: 'Module Settings'
description: 'Configuration permissions for administration'

access_content:
title: 'Block Page'
description: 'This will allow http451 module to block user access to certain pages as per RFC 7725'
17 changes: 17 additions & 0 deletions http451.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# TODO: Route for Http451Controller functions
# Route for module configuration
http451.form:
path: '/admin/config/system/http451'
defaults:
_form: 'Drupal\http451\Form\Http451Form'
_title: 'HTTP451 Configuration settings'
requirements:
_permission: 'configure_http451'

http451.content:
path: '/node/123'
defaults:
_title: '451 Unavailable for legal reasons'
_controller: '\Drupal\http451\Controller\Http451Controller::http451'
requirements:
_permission: 'access_content'
5 changes: 5 additions & 0 deletions http451.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
http451.event_subscriber:
class: Drupal\http451\EventSubscriber\Http451RedirectSubscriber
tags:
- { name: event_subscriber }
18 changes: 18 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# HTTP541 Drupal Module

This is a basic implementation of [RFC 7725](https://tools.ietf.org/html/rfc7725) as a Drupal Module.

## Installation
* Upload plugin directory to /modules/custom/ directory of your Drupal installation.

* Make sure the directory `src/Form` is writtable


* Activate the plugin on your browser through the 'Extend' service in menu.

## Usage
* Go to `configuration->System->http451 Settings` and fill in the form to block a post and return a HTTP 451 status

![configuration_screenshot](screenshot.png "Logo Title Text 1")

Reference-style:
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions src/Controller/Http451Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* This controller will return a 451 status page
* Nodes will be redirected to this page if they are censored after going through the Http451RedirectSubscriber
*/

namespace Drupal\http451\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Response;


class Http451Controller extends ControllerBase {
/**
* Http451.
*
* @return string
* Return Hello string.
*/
public function http451() {
$response = new Response();

$response->setContent('<html>
<head><title>Unavailable For Legal Reasons</title></head>
<body>
<h1>Unavailable For Legal Reasons</h1><h3>Article with ID '.$node_id.' has been censored</h3>
<p>This request may not be serviced in the Roman Province
of Judea due to the Lex Julia Majestatis, which disallows
access to resources hosted on servers deemed to be
operated by the People\'s Front of Judea.</p><h1></h1>
</body>
</html>');

$response->setStatusCode(Response::HTTP_UNAVAILABLE_FOR_LEGAL_REASONS, 'Unavailable For Legal Reasons');

$response->headers->set('Content-Type', 'text/html');

$response->send();
}
}
?>
71 changes: 71 additions & 0 deletions src/EventSubscriber/Http451RedirectSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Http451RedirectSubcriber.php
* This file contains the core logic to determine whether a node will return a 451 status code or not
*/

namespace Drupal\http451\EventSubscriber;

use Drupal\Core\Url;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Response;

class Http451RedirectSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return([
KernelEvents::REQUEST => [
['redirectMyContentTypeNode'],
]
]);
}

/**
* Redirect requests for my_content_type node detail pages to node/123.
*
* @param GetResponseEvent $event
* @return void
*/
public function redirectMyContentTypeNode(GetResponseEvent $event) {
$request = $event->getRequest();
$filename = 'blocked_ids.json';

// prevents pages like "edit", "revisions", etc from bring redirected.
if ($request->attributes->get('_route') !== 'entity.node.canonical') {
return;
} else {
$current_nodeId = $request->attributes->get('node')->id();
}

// Read blocked ids from blocked_ids.json file
$root_dir = realpath(dirname(__FILE__));
if(file_exists("$root_dir/$filename")) {
$blocked_ids = json_decode(file_get_contents("$root_dir/$filename"));
}

if ($request->attributes->get('node')->id() !== '4') {
return;
}

// This is where you set the destination.
$response = new Response();

$response->setContent('<html>
<head><title>Unavailable For Legal Reasons</title></head>
<body>
<h1>Unavailable For Legal Reasons</h1><h3>Article with ID '.$current_nodeId.' has been censored</h3>
<p>This request may not be serviced in the Roman Province
of Judea due to the Lex Julia Majestatis, which disallows
access to resources hosted on servers deemed to be
operated by the People\'s Front of Judea.</p><h1></h1>
</body>
</html>');

$response->setStatusCode(Response::HTTP_UNAVAILABLE_FOR_LEGAL_REASONS, 'Unavailable For Legal Reasons');

$response->headers->set('Content-Type', 'text/html');

$response->send();
}
}
Empty file added src/Form/Http451BlockForm.php
Empty file.
97 changes: 97 additions & 0 deletions src/Form/Http451Form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Drupal\http451\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

class Http451Form extends ConfigFormBase {
public function getFormId() {
return 'http451_form';
}

protected function getEditableConfigNames() {
return ['http451.settings'];
}

public function buildForm(array $form, FormStateInterface $form_state) {
/**
*
* List of fields
* -----------------------------------
*
* Authority Requesting the censorship
* Custom page title
* Custom page message
* -----------------------------------
*
* */
// Invoking Form constructor
$form = parent::buildForm($form, $form_state);

// Default parameters
$config = $this->config('http451.settings');
$form['content_id'] = array (
'#type' => 'textfield',
'#title' => $this->t('Blocked Post IDs: '),
'#required' => TRUE,
);

$form['blocking_authority'] = array(
'#type' => 'textfield',
'#title' => $this->t('Name of Authority: '),
'#required' => TRUE,
'#description' => $this->t('You need to specify the name of the authority who requested the takedown.'),
);

$form['page_title'] = array(
'#type' => 'textfield',
'#title' => $this->t('Censored page title: '),
'#default_value' => $config->get('http451.page_title'),
'#description' => $this->t('If you wish to use a custom non-standard title to show up on this page, you can set it here.'),
);

$form['page_content'] = array(
'#type' => 'textarea',
'#title' => $this->t('Reason for censorship: '),
'#default_value' => $config->get('http451.page_content'),
'#description' => $this->t('If you wish to use a custom message to show up on this page, you can set it here.'),
);

$form['save'] = array (
'#type' => 'submit',
'#value' => $this->t('Block Item')
);

return $form;
}

public function validateForm(array &$form, FormStateInterface $form_state) {
return;
}

public function submitForm(array &$form, FormStateInterface $form_state) {
$root_dir = realpath(dirname(__FILE__));
$filename = 'blocked_ids.json';
$id = $form_state->getValue('content_id');
$authority = $form_state->getValue('blocking_authority');
$title = $form_state->getValue('page_title');
$content = $form_state->getValue('page_content');
if(is_writable("$root_dir/$filename")) {
$stdin = fopen("$root_dir/$filename", "a+");
$data = array(
"nid" => $id,
"authority" => $authority,
"title" => $title,
"content" => $content
);
fwrite($stdin, json_encode($data));
fclose($stdin);
drupal_set_message(t('WARNING: Resource has been successfully blocked!'), 'warning');
return true;
} else {
drupal_set_message(t('Error: Please make sure that the module directory is writable. PATH:' . "$root_dir/$filename"), 'error');
}
return parent::submitForm($form, $form_state);
}
}
Empty file added src/Form/blocked_ids.json
Empty file.

0 comments on commit 1a4a53b

Please sign in to comment.