Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link
added 156 characters in body
Source Link
Kid Diamond
  • 2.6k
  • 17
  • 35
namespace View;
use View\View;
use Http\HttpResponse;

class LoginView extends View
{
 private $templatePath;
 private $templateData;
 private $isUserLoggedIn;
 private $isFormTokenValid;
 public function __construct(HttpResponse $httpResponse, $templatePath)
 {
 parent::__construct($httpResponse);
 $this->templatePath = $templatePath;
 }
 public function index()
 {
 // Logged in Users have no reason to view this page.
 if ($this->isUserLoggedIn) {
 $this->httpResponse->redirect('/');
 }
 // Assume sabotagecsrf attack, refresh self for a fresh View.
 if ($this->isFormTokenValid === false) {
 $this->httpResponse->redirect();
 }
 // PageHeader title.
 $this->templateData['title'] = 'Log In - Site Name';
 // PageHeader stylesheets.
 $this->templateData['styleSheets'] = [
 '/stylesheets/login.css'
 ];
 // PageHeader javascripts.
 $this->templateData['javaScripts'] = [
 '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js',
 '/javascripts/login-form-handler.min.js'
 ];
 // Form input error messages.
 $formErrorMessages = [
 'email' => [
 'NotEmpty' => 'Enter your email address.',
 'Email' => 'You did not enter a valid email address.'
 ],
 'password' => [
 'NotEmpty' => 'Enter your password.'
 ]
 ];
 // InterchangeReplace form error codes with corresponding error messages.
 if (isset($this->templateData['form']['errors'])) {
 $this->templateData['form']['errors'] = $this->replaceArrayValuesByKey($this->templateData['form']['errors'], $formErrorMessages);
 }
 // If not set by User input, check 'remember' checkbox by default.
 if (!isset($this->templateData['form']['values']['remember'])) {
 $this->templateData['form']['values']['remember'] = true;
 }
 // Render corresponding template and its processed data
 $this->renderTemplate($this->templatePath, $this->templateData);
 }
 public function setIsUserLoggedIn($boolean)
 {
 $this->isUserLoggedIn = $boolean;
 }
 public function setIsFormTokenValid($boolean)
 {
 $this->isFormTokenValid = $boolean;
 }
 public function setFormValueOf($name, $value)
 {
 $this->templateData['form']['values'][$name] = $value;
 }
 public function setFormErrorCodeOf($name, $code)
 {
 $this->templateData['form']['errors'][$name] = $code;
 }
 public function setPersistentUserLoginCookie($userId, $seriesNumber, $token)
 {
 $this->httpResponse->makeCookie('auth', [
 'uid' => $userId,
 'sno' => $seriesNumber,
 'token' => $token
 ], strtotime('+90 days'));
 }
 public function setHasLoginFailed($boolean)
 {
 $this->templateData['hasLoginFailed'] = $boolean;
 }
}
namespace View;
use Http\HttpResponse;
abstract class View
{
 protected $httpResponse;
 protected $templatePaths;
 protected $templateData;
  public function __construct(HttpResponse $httpResponse),
 $headerTemplatePath = null,
 $bodyTemplatePath  = null,
 $footerTemplatePath = null
 ) {
 $this->httpResponse = $httpResponse;
 if ($headerTemplatePath) {
 $this->templatePaths['header'] = $headerTemplatePath;
 }
 if ($bodyTemplatePath) {
 $this->templatePaths['body'] = $bodyTemplatePath;
 }
 if ($footerTemplatePath) {
 $this->templatePaths['footer'] = $footerTemplatePath;
 }
 }
 public function replaceArrayValuesByKey(array $target, array $source)
 {
 foreach ($target as $k => $v) {
 for ($i = 0, $c = count($v); $i < $c; ++$i) {
 if (isset($source[$k][$v[$i]])) {
 $target[$k][$i] = $source[$k][$v[$i]];
 }
 }
 }
 return $target;
 }
 public function renderTemplate($path, $data)
 {
 extract($data$this->templateData);
 include_onceif ABSPATH(isset($this->templatePaths['header'])) .{
 DS . 'View' . DS . 'Template' . DS . 'Header.php'; require_once $this->templatePaths['header'];
 }

 if (isset($this->templatePaths['body'])) {
  ob_start([$this, 'ob_indent']'autoIndent']);
 require_once $path;$this->templatePaths['body'];

 ob_end_flush();
 include_once}
 ABSPATH . DS . 'View' . DS .if 'Template'(isset($this->templatePaths['footer'])) .{
 DS . 'Footer.php'; require_once $this->templatePaths['footer'];
 }
 }
 private function ob_indentautoIndent($buffer)
 {
 $content = '';
 $lines = explode(PHP_EOL, $buffer);
 // TODO: calculate indentation levels of header template
 foreach ($lines as $line) {
 $content .= str_repeat(' ', 12) . $line . PHP_EOL;
 }
 return $content;
 }
}
namespace View;
use View\View;
use Http\HttpResponse;

class LoginView extends View
{
 private $templatePath;
 private $templateData;
 private $isUserLoggedIn;
 private $isFormTokenValid;
 public function __construct(HttpResponse $httpResponse, $templatePath)
 {
 parent::__construct($httpResponse);
 $this->templatePath = $templatePath;
 }
 public function index()
 {
 // Logged in Users have no reason to view this page
 if ($this->isUserLoggedIn) {
 $this->httpResponse->redirect('/');
 }
 // Assume sabotage, refresh self for a fresh View
 if ($this->isFormTokenValid === false) {
 $this->httpResponse->redirect();
 }
 // Page title
 $this->templateData['title'] = 'Log In - Site Name';
 // Page stylesheets
 $this->templateData['styleSheets'] = [
 '/stylesheets/login.css'
 ];
 // Page javascripts
 $this->templateData['javaScripts'] = [
 '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js',
 '/javascripts/login-form-handler.min.js'
 ];
 // Form input error messages
 $formErrorMessages = [
 'email' => [
 'NotEmpty' => 'Enter your email address.',
 'Email' => 'You did not enter a valid email address.'
 ],
 'password' => [
 'NotEmpty' => 'Enter your password.'
 ]
 ];
 // Interchange form error codes with corresponding error messages
 if (isset($this->templateData['form']['errors'])) {
 $this->templateData['form']['errors'] = $this->replaceArrayValuesByKey($this->templateData['form']['errors'], $formErrorMessages);
 }
 // If not set by User input, check 'remember' checkbox by default
 if (!isset($this->templateData['form']['values']['remember'])) {
 $this->templateData['form']['values']['remember'] = true;
 }
 // Render corresponding template and its processed data
 $this->renderTemplate($this->templatePath, $this->templateData);
 }
 public function setIsUserLoggedIn($boolean)
 {
 $this->isUserLoggedIn = $boolean;
 }
 public function setIsFormTokenValid($boolean)
 {
 $this->isFormTokenValid = $boolean;
 }
 public function setFormValueOf($name, $value)
 {
 $this->templateData['form']['values'][$name] = $value;
 }
 public function setFormErrorCodeOf($name, $code)
 {
 $this->templateData['form']['errors'][$name] = $code;
 }
 public function setPersistentUserLoginCookie($userId, $seriesNumber, $token)
 {
 $this->httpResponse->makeCookie('auth', [
 'uid' => $userId,
 'sno' => $seriesNumber,
 'token' => $token
 ], strtotime('+90 days'));
 }
 public function setHasLoginFailed($boolean)
 {
 $this->templateData['hasLoginFailed'] = $boolean;
 }
}
namespace View;
use Http\HttpResponse;
abstract class View
{
 protected $httpResponse;
 public function __construct(HttpResponse $httpResponse)
 {
 $this->httpResponse = $httpResponse;
 }
 public function replaceArrayValuesByKey(array $target, array $source)
 {
 foreach ($target as $k => $v) {
 for ($i = 0, $c = count($v); $i < $c; ++$i) {
 if (isset($source[$k][$v[$i]])) {
 $target[$k][$i] = $source[$k][$v[$i]];
 }
 }
 }
 return $target;
 }
 public function renderTemplate($path, $data)
 {
 extract($data);
 include_once ABSPATH . DS . 'View' . DS . 'Template' . DS . 'Header.php';
 ob_start([$this, 'ob_indent']);
 require_once $path;
 ob_end_flush();
 include_once ABSPATH . DS . 'View' . DS . 'Template' . DS . 'Footer.php';
 }
 private function ob_indent($buffer)
 {
 $content = '';
 $lines = explode(PHP_EOL, $buffer);
 foreach ($lines as $line) {
 $content .= str_repeat(' ', 12) . $line . PHP_EOL;
 }
 return $content;
 }
}
namespace View;
use View\View;
class LoginView extends View
{
 private $isUserLoggedIn;
 private $isFormTokenValid;
 public function index()
 {
 // Logged in Users have no reason to view this page.
 if ($this->isUserLoggedIn) {
 $this->httpResponse->redirect('/');
 }
 // Assume csrf attack, refresh self for a fresh View.
 if ($this->isFormTokenValid === false) {
 $this->httpResponse->redirect();
 }
 // Header title.
 $this->templateData['title'] = 'Log In - Site Name';
 // Header stylesheets.
 $this->templateData['styleSheets'] = [
 '/stylesheets/login.css'
 ];
 // Header javascripts.
 $this->templateData['javaScripts'] = [
 '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js',
 '/javascripts/login-form-handler.min.js'
 ];
 // Form input error messages.
 $formErrorMessages = [
 'email' => [
 'NotEmpty' => 'Enter your email address.',
 'Email' => 'You did not enter a valid email address.'
 ],
 'password' => [
 'NotEmpty' => 'Enter your password.'
 ]
 ];
 // Replace form error codes with corresponding error messages.
 if (isset($this->templateData['form']['errors'])) {
 $this->templateData['form']['errors'] = $this->replaceArrayValuesByKey($this->templateData['form']['errors'], $formErrorMessages);
 }
 // If not set by User input, check 'remember' checkbox by default.
 if (!isset($this->templateData['form']['values']['remember'])) {
 $this->templateData['form']['values']['remember'] = true;
 }
 $this->renderTemplate();
 }
 public function setIsUserLoggedIn($boolean)
 {
 $this->isUserLoggedIn = $boolean;
 }
 public function setIsFormTokenValid($boolean)
 {
 $this->isFormTokenValid = $boolean;
 }
 public function setFormValueOf($name, $value)
 {
 $this->templateData['form']['values'][$name] = $value;
 }
 public function setFormErrorCodeOf($name, $code)
 {
 $this->templateData['form']['errors'][$name] = $code;
 }
 public function setHasLoginFailed($boolean)
 {
 $this->templateData['hasLoginFailed'] = $boolean;
 }
}
namespace View;
use Http\HttpResponse;
abstract class View
{
 protected $httpResponse;
 protected $templatePaths;
 protected $templateData;
  public function __construct(HttpResponse $httpResponse,
 $headerTemplatePath = null,
 $bodyTemplatePath  = null,
 $footerTemplatePath = null
 ) {
 $this->httpResponse = $httpResponse;
 if ($headerTemplatePath) {
 $this->templatePaths['header'] = $headerTemplatePath;
 }
 if ($bodyTemplatePath) {
 $this->templatePaths['body'] = $bodyTemplatePath;
 }
 if ($footerTemplatePath) {
 $this->templatePaths['footer'] = $footerTemplatePath;
 }
 }
 public function replaceArrayValuesByKey(array $target, array $source)
 {
 foreach ($target as $k => $v) {
 for ($i = 0, $c = count($v); $i < $c; ++$i) {
 if (isset($source[$k][$v[$i]])) {
 $target[$k][$i] = $source[$k][$v[$i]];
 }
 }
 }
 return $target;
 }
 public function renderTemplate()
 {
 extract($this->templateData);
 if (isset($this->templatePaths['header'])) {
  require_once $this->templatePaths['header'];
 }

 if (isset($this->templatePaths['body'])) {
  ob_start([$this, 'autoIndent']);
 require_once $this->templatePaths['body'];

 ob_end_flush();
 }
 if (isset($this->templatePaths['footer'])) {
  require_once $this->templatePaths['footer'];
 }
 }
 private function autoIndent($buffer)
 {
 $content = '';
 $lines = explode(PHP_EOL, $buffer);
 // TODO: calculate indentation levels of header template
 foreach ($lines as $line) {
 $content .= str_repeat(' ', 12) . $line . PHP_EOL;
 }
 return $content;
 }
}
Post Undeleted by Kid Diamond
Post Deleted by Kid Diamond
fixed image
Source Link
Kid Diamond
  • 2.6k
  • 17
  • 35
deleted 3029 characters in body
Source Link
Kid Diamond
  • 2.6k
  • 17
  • 35
Loading
Tweeted twitter.com/#!/StackCodeReview/status/509671573658165248
deleted 46 characters in body
Source Link
Kid Diamond
  • 2.6k
  • 17
  • 35
Loading
Code update
Source Link
Kid Diamond
  • 2.6k
  • 17
  • 35
Loading
deleted 20 characters in body
Source Link
Kid Diamond
  • 2.6k
  • 17
  • 35
Loading
edited title
Link
Kid Diamond
  • 2.6k
  • 17
  • 35
Loading
Source Link
Kid Diamond
  • 2.6k
  • 17
  • 35
Loading
lang-php

AltStyle によって変換されたページ (->オリジナル) /