Merge branch 'master' into tests
This commit is contained in:
@ -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);
|
||||
|
@ -12,11 +12,12 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* базовый класс для всей экшенов выполняющихся по аякс-запросу
|
||||
*/
|
||||
* базовый класс для всей экшенов выполняющихся по аякс-запросу
|
||||
*/
|
||||
abstract class AjaxAction extends Action
|
||||
{
|
||||
public $data = 1;
|
||||
|
||||
protected $encode = true;
|
||||
|
||||
function __construct()
|
||||
|
@ -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';
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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
vendored
7
cache/CacheKey.php
vendored
@ -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
vendored
7
cache/Cacher.php
vendored
@ -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])) {
|
||||
|
@ -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');
|
||||
|
@ -11,11 +11,11 @@
|
||||
|
||||
class CaptchaImageAction
|
||||
{
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$captcha = new Captcha();
|
||||
echo $captcha->getImage(Env::Get('ctoken'));
|
||||
exit();
|
||||
exit();
|
||||
}
|
||||
}
|
@ -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'])) {
|
||||
|
@ -1,66 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Обработчик эксепшенов
|
||||
*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link
|
||||
* @package Majestic
|
||||
* @subpackage Core
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
class MJException extends Exception
|
||||
{
|
||||
private $line_range = 6;
|
||||
|
||||
public function terminate()
|
||||
{
|
||||
if (!DEBUG_ENABLE) {
|
||||
trigger_error($this->getMessage());
|
||||
throw new StaticPageException(500);
|
||||
}
|
||||
|
||||
$return = "<b>MJ Error:</b> ";
|
||||
$return .= str_replace("\n", "<br/>\n", $this->getMessage())."<br>\n";
|
||||
|
||||
$trace = $this->getTrace();
|
||||
|
||||
$file = reset($trace);
|
||||
|
||||
//смещение в трейсе, указаное при вызове эксепшена (2й параметр). Нужно для более инофрмативного вывода
|
||||
if ($shift = abs($this->getCode())) {
|
||||
while($shift--) {
|
||||
$file = next($trace);
|
||||
}
|
||||
}
|
||||
|
||||
if ($fp = fopen($file['file'], 'r')) {
|
||||
$error_line = $file['line'];
|
||||
$start = $error_line - $this->line_range;
|
||||
$end = $error_line + $this->line_range;
|
||||
$i = 1;
|
||||
$return .= "<pre style=\"background-color:#e4e4e4\">";
|
||||
while ($line = fgets($fp, 4096) and $i<=$end) {
|
||||
$line = htmlspecialchars($line);
|
||||
if ($i >= $start && $i <= $end) {
|
||||
if ($i == $error_line) $return .= '<div style="background-color:#cccccc">'.$i.' '.$line.'</div>';
|
||||
else $return .= $i.' '.$line;
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
$return .= "</pre>";
|
||||
fclose($fp);
|
||||
}
|
||||
|
||||
$return .= '<table border="1" cellpadding="2" cellspacing="0"> <caption><b>Backtrace</b></caption>';
|
||||
$return .= "\n<tr><td><b>".$this->getFile().'</b></td><td><b>'.$this->getLine().'</b></td></tr>';
|
||||
foreach($trace as $row) {
|
||||
if (isset($row['file'])) { //throwing exception from __call method will not return file and line
|
||||
$return .= "\n<tr".($file['file'] == $row['file'] ? ' style="background-color:#ffcccc"': '')."><td>".$row['file'].'</td><td>'.$row['line'].'</td></tr>';
|
||||
}
|
||||
}
|
||||
return $return . '</table>';
|
||||
}
|
||||
}
|
||||
?>
|
@ -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>
|
||||
|
@ -16,7 +16,7 @@ class RedisDebug
|
||||
public function __construct($redis)
|
||||
{
|
||||
if (!is_a($redis, 'Redis')) {
|
||||
throw new MJException();
|
||||
throw new GeneralException();
|
||||
}
|
||||
$this->redis = $redis;
|
||||
}
|
||||
|
@ -101,4 +101,20 @@ class Profiler
|
||||
}
|
||||
FB::table('Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]', $table);
|
||||
}
|
||||
|
||||
public function getCli()
|
||||
{
|
||||
$this->end = microtime(true);
|
||||
$queriesTime = 0;
|
||||
$temp = str_pad(PHP_EOL, 60, '-', STR_PAD_LEFT);
|
||||
foreach ($this->queries as $query) {
|
||||
$temp .= sprintf('%-25s[% 10sms] %s', '(' . $query->getType() .')', round($query->getElapsed() * 1000, 2), $query->getCommand()) . PHP_EOL;
|
||||
$queriesTime += $query->getElapsed();
|
||||
}
|
||||
$html = str_pad(PHP_EOL, 60, '-', STR_PAD_LEFT);
|
||||
$html .= 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.' . PHP_EOL
|
||||
. 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms] ' . PHP_EOL;
|
||||
$html .= $temp;
|
||||
return $html;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user