Skip to content

Commit

Permalink
fixing Coder Sniffer issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cisvarun committed Feb 25, 2016
1 parent a4c157c commit ae47181
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 155 deletions.
32 changes: 16 additions & 16 deletions protected_pages.module
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
<?php

use Drupal\Component\Utility\Html;

/**
* @file
* This module allows you to protect any page of your website by secure
* password. You can enter urls of pages to protect and set password per page.
* Admin (uid = 1) or user with bypass protection permission can view page.
*/

use Drupal\Component\Utility\Html;

/**
* Implements hook_mail().
*
*/
function protected_pages_mail($key, &$message, $params) {

switch ($key) {
// Send a simple message from the contact form.
case 'protected_pages_details_mail':
switch ($key) {
// Send a simple message from the contact form.
case 'protected_pages_details_mail':

$tokens = array('[protected-page-url]', '[site-name]');
$replcements = array(
$params['protected_page_url'], \Drupal::config('system.site')->get('name'),
);
$tokens = array('[protected-page-url]', '[site-name]');
$replcements = array(
$params['protected_page_url'],
\Drupal::config('system.site')->get('name'),
);

$body = str_replace($tokens, $replcements, $params['body']);
$body = str_replace($tokens, $replcements, $params['body']);

$subject = $params['subject'];
$message['subject'] = Html::escape($subject);
$message['body'][] = Html::escape($body);
$subject = $params['subject'];
$message['subject'] = Html::escape($subject);
$message['body'][] = Html::escape($body);

break;
}
break;
}
}

137 changes: 76 additions & 61 deletions src/Controller/ProtectedPagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,75 +19,90 @@
*/
class ProtectedPagesController extends ControllerBase {

/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;

/**
* Constructs a ProtectedPagesController object.
*
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
*/
public function __construct(RendererInterface $renderer) {
$this->renderer = $renderer;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('renderer')
);
}
/**
* Constructs a ProtectedPagesController object.
*
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
*/
public function __construct(RendererInterface $renderer) {
$this->renderer = $renderer;
}

/**
* Generate the list of protected page.
*/
public function protectedPagesList() {
$content = array();
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('renderer')
);
}

$content['message'] = array(
'#markup' => $this->t('List of password protected pages.'),
);
/**
* Generate the list of protected page.
*/
public function protectedPagesList() {
$content = array();

$rows = array();
$headers = array(t('#'), t('Relative Path'), t('Operations'));
$count = 1;
$content['message'] = array(
'#markup' => $this->t('List of password protected pages.'),
);

foreach ($pages = ProtectedPagesStorage::load() as $page) {
$operation_drop_button = array(array('#type' => 'dropbutton', '#links' =>
array(
'edit-protected-page' => array('title' => $this->t('Edit'), 'url' => Url::fromUri('internal:/admin/config/system/protected_pages/' . $page->pid . '/edit')),
'delete-protected-page' => array('title' => $this->t('Delete'), 'url' => Url::fromUri('internal:/admin/config/system/protected_pages/' . $page->pid . '/delete')),
'send-email' => array('title' => $this->t('Send E-mail'), 'url' => Url::fromUri('internal:/admin/config/system/protected_pages/' . $page->pid . '/send_email')))));
$rows = array();
$headers = array(t('#'), t('Relative Path'), t('Operations'));
$count = 1;

foreach (ProtectedPagesStorage::load() as $page) {
$operation_drop_button = array(
array(
'#type' => 'dropbutton',
'#links' =>
array(
'edit-protected-page' => array(
'title' => $this->t('Edit'),
'url' => Url::fromUri('internal:/admin/config/system/protected_pages/' . $page->pid . '/edit')
),
'delete-protected-page' => array(
'title' => $this->t('Delete'),
'url' => Url::fromUri('internal:/admin/config/system/protected_pages/' . $page->pid . '/delete')
),
'send-email' => array(
'title' => $this->t('Send E-mail'),
'url' => Url::fromUri('internal:/admin/config/system/protected_pages/' . $page->pid . '/send_email')
)
)
)
);

$operations = $this->renderer->render($operation_drop_button);
$rows[] = array(
'data' =>
array(
$count,
Html::escape($page->path),
$operations,
),
);
$count++;
}
$content['table'] = array(
'#type' => 'table',
'#header' => $headers,
'#rows' => $rows,
'#empty' => t('No records available.'),
);
// Don't cache this page.
$content['#cache']['max-age'] = 0;

return $content;
$operations = $this->renderer->render($operation_drop_button);
$rows[] = array(
'data' =>
array(
$count,
Html::escape($page->path),
$operations,
),
);
$count++;
}
$content['table'] = array(
'#type' => 'table',
'#header' => $headers,
'#rows' => $rows,
'#empty' => t('No records available.'),
);
// Don't cache this page.
$content['#cache']['max-age'] = 0;

return $content;
}

}
171 changes: 93 additions & 78 deletions src/EventSubscriber/ProtectedPagesSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,98 +22,113 @@
*/
class ProtectedPagesSubscriber implements EventSubscriberInterface {

use RedirectDestinationTrait;
use RedirectDestinationTrait;

/**
* Redirects user to protected page login screen if current page is password protected.
*
* @param \Symfony\Component\HttpKernel\Event\PostResponseEvent $event
* The event to process.
*/
public function checkProtectedPage(FilterResponseEvent $event) {
$account = \Drupal::currentUser();
if ($account->hasPermission('bypass pages password protection')) {
return;
}
$current_path = \Drupal::service('path.alias_manager')->getAliasByPath(\Drupal::service('path.current')->getPath());
$normal_path = Unicode::strtolower(\Drupal::service('path.alias_manager')->getPathByAlias($current_path));
$pid = $this->protectedPagesIsPageLocked($current_path, $normal_path);
/**
* Redirects user to protected page login screen if current page is password protected.
*
* @param \Symfony\Component\HttpKernel\Event\PostResponseEvent $event
* The event to process.
*/
public function checkProtectedPage(FilterResponseEvent $event) {
$account = \Drupal::currentUser();
if ($account->hasPermission('bypass pages password protection')) {
return;
}
$current_path = \Drupal::service('path.alias_manager')
->getAliasByPath(\Drupal::service('path.current')->getPath());
$normal_path = Unicode::strtolower(\Drupal::service('path.alias_manager')
->getPathByAlias($current_path));
$pid = $this->protectedPagesIsPageLocked($current_path, $normal_path);

if ($pid) {
$query = $this->getDestinationArray();
$http_referer = \Drupal::request()->server->get('HTTP_REFERER');
if (!empty($http_referer)) {
$query['back'] = urlencode($current_path);
}
$query['protected_page'] = $pid;
$response = new RedirectResponse(Url::fromUri('internal:/protected-page', array('query' => $query))
->toString());
$response->send();
return;

if ($pid) {
$query = $this->getDestinationArray();
}
else {
$page_node = \Drupal::request()->attributes->get('node');
if (is_object($page_node)) {
$nid = $page_node->id();
if (isset($nid) && is_numeric($nid)) {
$path_to_node = '/node/' . $nid;
$current_path = Unicode::strtolower(\Drupal::service('path.alias_manager')
->getAliasByPath($path_to_node));
$normal_path = Unicode::strtolower(\Drupal::service('path.alias_manager')
->getPathByAlias($current_path));
$pid = $this->protectedPagesIsPageLocked($current_path, $normal_path);
if ($pid) {
$query = \Drupal\Core\Routing::getDestinationArray();
$http_referer = \Drupal::request()->server->get('HTTP_REFERER');
if (!empty($http_referer)) {
$query['back'] = urlencode($current_path);
$query['back'] = urlencode($current_path);
}
$query['protected_page'] = $pid;
$response = new RedirectResponse(Url::fromUri('internal:/protected-page', array('query' => $query))->toString());

$response = new RedirectResponse(Url::fromUri('internal:/protected-page', array('query' => $query))
->toString());
$response->send();
return;

} else {
$page_node = \Drupal::request()->attributes->get('node');
if (is_object($page_node)) {
$nid = $page_node->id();
if (isset($nid) && is_numeric($nid)) {
$path_to_node = '/node/' . $nid;
$current_path = Unicode::strtolower(\Drupal::service('path.alias_manager')->getAliasByPath($path_to_node));
$normal_path = Unicode::strtolower(\Drupal::service('path.alias_manager')->getPathByAlias($current_path));
$pid = $this->protectedPagesIsPageLocked($current_path, $normal_path);
if ($pid) {
$query = \Drupal\Core\Routing::getDestinationArray();
$http_referer = \Drupal::request()->server->get('HTTP_REFERER');
if (!empty($http_referer)) {
$query['back'] = urlencode($current_path);
}
$query['protected_page'] = $pid;

$response = new RedirectResponse(Url::fromUri('internal:/protected-page', array('query' => $query))->toString());
$response->send();
return;
}
}
}

}
}
}
}

/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = array('checkProtectedPage');
return $events;
}
}

/**
* Returns protected page id.
*
* @param string $current_path
* Current path alias.
* @param string $normal_path
* Current normal path.
*
* @return int $pid
* The protected page id.
*/
public function protectedPagesIsPageLocked($current_path, $normal_path) {
$fields = array('pid');
$conditions = array();
$conditions['or'][] = array('field' => 'path', 'value' => $normal_path, 'operator' => '=');
$conditions['or'][] = array('field' => 'path', 'value' => $current_path, 'operator' => '=');
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = array('checkProtectedPage');
return $events;
}

$pid = ProtectedPagesStorage::load($fields, $conditions, TRUE);
/**
* Returns protected page id.
*
* @param string $current_path
* Current path alias.
* @param string $normal_path
* Current normal path.
*
* @return int $pid
* The protected page id.
*/
public function protectedPagesIsPageLocked($current_path, $normal_path) {
$fields = array('pid');
$conditions = array();
$conditions['or'][] = array(
'field' => 'path',
'value' => $normal_path,
'operator' => '='
);
$conditions['or'][] = array(
'field' => 'path',
'value' => $current_path,
'operator' => '='
);

if (isset($_SESSION['_protected_page']['passwords'][$pid]['expire_time'])) {
if (time() >= $_SESSION['_protected_page']['passwords'][$pid]['expire_time']) {
unset($_SESSION['_protected_page']['passwords'][$pid]['request_time']);
unset($_SESSION['_protected_page']['passwords'][$pid]['expire_time']);
}
}
if (isset($_SESSION['_protected_page']['passwords'][$pid]['request_time'])) {
return FALSE;
}
return $pid;
$pid = ProtectedPagesStorage::load($fields, $conditions, TRUE);

if (isset($_SESSION['_protected_page']['passwords'][$pid]['expire_time'])) {
if (time() >= $_SESSION['_protected_page']['passwords'][$pid]['expire_time']) {
unset($_SESSION['_protected_page']['passwords'][$pid]['request_time']);
unset($_SESSION['_protected_page']['passwords'][$pid]['expire_time']);
}
}
if (isset($_SESSION['_protected_page']['passwords'][$pid]['request_time'])) {
return FALSE;
}
return $pid;
}

}

0 comments on commit ae47181

Please sign in to comment.