Browse Source

Code formatting

master
Anton Terekhov 13 years ago
parent
commit
3ebfac9db4
  1. 20
      app/Action.php
  2. 5
      app/AjaxAction.php
  3. 12
      app/ErrorAction.php
  4. 32
      app/FrontController.php
  5. 9
      app/PagerAction.php
  6. 6
      app/StaticAction.php
  7. 7
      cache/CacheKey.php
  8. 7
      cache/Cacher.php
  9. 40
      captcha/Captcha.php
  10. 4
      captcha/CaptchaImageAction.php
  11. 4
      captcha/CaptchaValidator.php
  12. 32
      exception/ErrorHandler.php

20
app/Action.php

@ -11,21 +11,21 @@
abstract class Action
{
protected $template;
/**
* @var PHPView
*/
protected $view;
public function __construct()
{
$this->view = FrontController::getInstance()->getView();
$this->extractParams();
$this->execute();
}
protected function extractParams()
{
foreach (Env::getParam() as $name => $value) {
@ -34,12 +34,12 @@ abstract class Action
}
}
}
abstract protected function execute();
/**
* Redirect
*
*
* @param mixed $url
*/
protected function redirect($url = null)
@ -47,15 +47,15 @@ abstract class Action
header('Location: ' . (($url) ? $url : Env::getRequestUri()));
exit();
}
protected function getTemplate()
{
$class = get_class($this);
$template = ($this->template) ? $this->template : substr($class, 0, -6/*strlen('Action')*/);
$template = ($this->template) ? $this->template : substr($class, 0, -6 /*strlen('Action')*/);
$dir = array_slice(explode('/', Load::getFilePath($class)), -2, 1);
return '/actions/' . array_pop($dir) . '/' . $template;
}
public function fetch()
{
$this->view->assignObject($this);

5
app/AjaxAction.php

@ -12,11 +12,12 @@
*/
/**
* базовый класс для всей экшенов выполняющихся по аякс-запросу
*/
* базовый класс для всей экшенов выполняющихся по аякс-запросу
*/
abstract class AjaxAction extends Action
{
public $data = 1;
protected $encode = true;
function __construct()

12
app/ErrorAction.php

@ -11,18 +11,18 @@
class ErrorAction extends Action
{
/**
* @var ErrorException
*/
public $exception;
public function __construct($exception)
{
$this->exception = $exception;
parent::__construct();
}
protected function execute()
{
$this->template = 500;
@ -38,12 +38,12 @@ class ErrorAction extends Action
$this->logError();
$this->sendHTTPCode();
}
protected function getTemplate()
{
return '/actions/' . $this->template;
}
protected function sendHttpCode()
{
if (headers_sent()) {
@ -67,7 +67,7 @@ class ErrorAction extends Action
if ($ex instanceof ErrorException) {
$error = $ex->getSeverity();
}
switch ($error) {
case E_NOTICE:
$error = 'Notice';

32
app/FrontController.php

@ -15,20 +15,20 @@ class FrontController
* @var Router
*/
protected $router;
/**
* @var string
*/
protected $view = 'PHPView';
protected $base_url = '';
/**
* @var FrontController
*/
protected static $instance;
private function __construct()
{
ErrorHandler::init();
@ -37,7 +37,7 @@ class FrontController
}
$this->router = new Router();
}
/**
* Refuse cloning
*/
@ -53,7 +53,7 @@ class FrontController
}
return self::$instance;
}
/**
* @param string $view
* @return FrontController
@ -63,9 +63,9 @@ class FrontController
$this->view = $view;
return $this;
}
/**
*
*
* @return iView
*/
public function getView($view = null)
@ -73,7 +73,7 @@ class FrontController
$view = ($view) ? $view : $this->view;
return new $view(Config::get($view));
}
/**
* @param string $url
*/
@ -82,12 +82,12 @@ class FrontController
$this->base_url = rtrim($url, '/');
return $this;
}
public function getBaseUrl()
{
return $this->base_url;
}
/**
* @return Router
*/
@ -95,7 +95,7 @@ class FrontController
{
return $this->router;
}
public function execute()
{
try {
@ -104,18 +104,18 @@ class FrontController
if (!$route) {
throw new Error404Exception('Route for "' . $request . '" not found');
}
$action_class = $route->getAction();
if (!class_exists($action_class)) {
throw new GeneralException('Action class "' . $action_class . '" not found.');
}
$action = new $action_class();
$layout_class = $route->getLayout();
if (!class_exists($layout_class)) {
throw new GeneralException('Layout class "' . $layout_class . '" not found.');
}
$layout = new $layout_class();
$html = $layout->fetch($action);
if (DEBUG) {

9
app/PagerAction.php

@ -11,9 +11,12 @@
class PagerAction extends Action
{
public $page = 1;
public $page = 1;
public $last_page = 1;
protected $offset = 0;
protected $limit;
public function __construct($limit = 20)
@ -45,10 +48,10 @@ class PagerAction extends Action
{
return (int) $this->limit;
}
protected function getTemplate()
{
$template = ($this->template) ? $this->template : substr(get_class($this), 0, -6/*strlen('Action')*/);
$template = ($this->template) ? $this->template : substr(get_class($this), 0, -6 /*strlen('Action')*/);
return '/actions/' . $template;
}
}

6
app/StaticAction.php

@ -11,13 +11,13 @@
abstract class StaticAction extends Action
{
protected function getTemplate()
{
$template = ($this->template) ? $this->template : substr(get_class($this), 0, -6/*strlen('Action')*/);
$template = ($this->template) ? $this->template : substr(get_class($this), 0, -6 /*strlen('Action')*/);
return '/static/' . $template;
}
public function fetch()
{
return $this->view->fetch($this->getTemplate());

7
cache/CacheKey.php

@ -16,11 +16,12 @@ class CacheKey
* @var Cacher
*/
protected $cacher;
/**
* @var CacheKey
*/
protected $key;
protected $expire = 0;
/**
@ -32,11 +33,11 @@ class CacheKey
*/
public function __construct($cacher, $key, $params = array(), $expire = 0)
{
$this->cacher = $cacher;
$this->cacher = $cacher;
$this->key = $key;
if ($params) {
$params = (is_array($params)) ? implode('|', $params) : $params;
$this->key = $key . '_' . $params;
$this->key = $key . '_' . $params;
}
$this->expire = $expire;
}

7
cache/Cacher.php

@ -11,15 +11,14 @@
class Cacher
{
/**
* Initialized cachers
*
*
* @var array
*/
static protected $caches = array();
static public function get($cacher, $config = null)
{
if (!isset(self::$caches[$cacher])) {

40
captcha/Captcha.php

@ -11,47 +11,49 @@
class Captcha
{
protected $font_size = 38;
protected $width = 200;
protected $height = 70;
protected $width = 200;
protected $height = 70;
public function getImage($token)
{
$font = dirname(__FILE__) . '/poh.ttf';
$image = imagecreatetruecolor($this->width, $this->height);
// белый фон
$background = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, $this->width, $this->height, $background);
// основная магия тут
if (Session::get('_ctoken') == $token && Session::get('_ccode')) {
$code = Session::get('_ccode');
$color = imagecolorallocate($image, 81, 81, 81);
for ($j = 0; $j < 5; $j++) {
imageellipse($image, rand(-$this->width, $this->width * 2),
rand(-$this->height, $this->height * 2),
imageellipse($image, rand(-$this->width, $this->width * 2),
rand(-$this->height, $this->height * 2),
$this->width, $this->width, $color);
}
$letters = preg_split("//u", strtoupper($code));
$length = count($letters);
$step = floor($this->width / $length);
$i = 2;
foreach ($letters as $key => $letter) {
imagettftext($image, $this->font_size, 0,
rand($i + 2, $i + 4), ($this->font_size + $this->height) / 2,
imagettftext($image, $this->font_size, 0,
rand($i + 2, $i + 4), ($this->font_size + $this->height) / 2,
$color, $font, $letter);
$i = $i + $step ;
$i = $i + $step;
}
}
ob_start();
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Content-Type: image/jpg");
@ -59,7 +61,7 @@ class Captcha
imagedestroy($image);
return ob_get_clean();
}
public function getToken()
{
$token = md5(microtime() . uniqid());
@ -68,10 +70,10 @@ class Captcha
Session::set('_ccode', $code);
return $token;
}
public function checkCode($token, $code)
{
if (Session::get('_ctoken') == $token && Session::get('_ccode') == strtoupper($code)){
if (Session::get('_ctoken') == $token && Session::get('_ccode') == strtoupper($code)) {
// Housekeeping
Session::del('_ccode');
Session::del('_ctoken');

4
captcha/CaptchaImageAction.php

@ -11,11 +11,11 @@
class CaptchaImageAction
{
public function __construct()
{
$captcha = new Captcha();
echo $captcha->getImage(Env::Get('ctoken'));
exit();
exit();
}
}

4
captcha/CaptchaValidator.php

@ -13,9 +13,9 @@ class CaptchaValidator extends Validator
{
const WRONG_CODE = 'is_empty';
protected $templates = array(self::WRONG_CODE => 'Entered code wrong');
public function isValid($value, $context = null)
{
if (!$context || !isset($context['ctoken']) || !isset($context['ccode'])) {

32
exception/ErrorHandler.php

@ -11,39 +11,39 @@
class ErrorHandler
{
static public function init()
{
set_error_handler(array('ErrorHandler', 'error_handler'));
}
static public function error_handler($errno, $errstr, $errfile, $errline )
static public function error_handler($errno, $errstr, $errfile, $errline)
{
ob_clean();
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
return false;
}
static protected function getSource($file, $hiline)
{
$code = array();
$i = 0;
foreach(file($file) as $line) {
foreach (file($file) as $line) {
$i++;
if($i >= $hiline - 10 && $i <= $hiline + 10) {
if ($i >= $hiline - 10 && $i <= $hiline + 10) {
if ($i == $hiline) {
$code[] = '<tr class="error"><th>' . $i . '</th><td><span class="specific">' . htmlentities($line, ENT_QUOTES, 'UTF-8') . '</span></td></tr>';
}else{
} else {
$code[] = '<tr><th>' . $i . '</th><td>' . htmlentities($line, ENT_QUOTES, 'UTF-8') . '</td></tr>';
}
}
if($i > $hiline + 10) {
if ($i > $hiline + 10) {
break;
}
}
return implode('', $code);
}
static protected function wrapArray($array, $name)
{
if (!$array) {
@ -60,12 +60,12 @@ class ErrorHandler
$text .= '</tbody></table>';
return $text;
}
static protected function wrapTrace($trace)
{
return '<code>' . nl2br($trace) . '</code>';
}
/**
* @param Exception $exception
*/
@ -73,22 +73,22 @@ class ErrorHandler
{
ob_clean();
$class = get_class($exception);
$method = Env::Server('REQUEST_METHOD', '');
$uri = Env::getRequestUri();
$source = self::getSource($exception->getFile(), $exception->getLine());
$time = date('r', Env::Server('REQUEST_TIME', time()));
$trace = nl2br($exception->getTraceAsString());
$get = self::wrapArray(Env::Get(), 'GET');
$post = self::wrapArray(Env::Post(), 'POST');
$session = self::wrapArray(Session::get(), 'SESSION');
$files = self::wrapArray(Env::Files(), 'FILES');
$cookies = self::wrapArray(Env::Cookie(), 'COOKIE');
$server = self::wrapArray(Env::Server(), 'SERVER');
$message =<<<EOD
$message = <<<EOD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

Loading…
Cancel
Save