From aaa9c2e1bf189872ad000c0b6576b8af29d14d23 Mon Sep 17 00:00:00 2001 From: "CORP\\phedor" Date: Fri, 20 Apr 2018 18:03:39 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D1=80=D1=83=D0=B3=D0=B0=D1=8F=20=D0=B8?= =?UTF-8?q?=D0=BD=D1=82=D0=B5=D1=80=D0=BF=D1=80=D0=B5=D1=82=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D1=80=D0=B5=D0=B5=D1=81=D1=82=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Controller/Action.php | 25 ++++++++------- src/Controller/Component.php | 60 +++++++++++++++++------------------ src/Controller/Front.php | 14 ++++---- src/Controller/Installer.php | 9 +++--- src/Controller/Service.php | 5 +-- src/Controller/State.php | 3 +- src/Excel/Document.php | 2 +- src/Excel/Table.php | 4 ++- src/Filter/ActionAccess.php | 7 ++-- src/Filter/Authorization.php | 23 ++++++++------ src/Filter/Login.php | 5 +-- src/Form/OptionFactory.php | 2 +- src/Path.php | 2 +- src/Registry.php | 40 +++++++++++++++++++++++ src/Settings.php | 28 ++++++++++------ src/Tales.php | 2 +- src/Url.php | 2 +- src/Validator/Rule/Count.php | 2 +- src/View/Page.php | 7 ++-- src/View/Top.php | 4 +-- src/WrongRequestException.php | 2 +- 21 files changed, 156 insertions(+), 92 deletions(-) create mode 100644 src/Registry.php diff --git a/src/Controller/Action.php b/src/Controller/Action.php index 2078409..be0dfb8 100644 --- a/src/Controller/Action.php +++ b/src/Controller/Action.php @@ -9,6 +9,8 @@ use ctiso\Shortcut, ctiso\HttpRequest, ctiso\Functions, ctiso\Settings, + ctiso\Registry, + ctiso\Role\User, ctiso\View\Composite, ctiso\Filter\ActionAccess, ctiso\View\View, @@ -45,8 +47,8 @@ class Action private $helpers = array(); // Помошники для действий public $part = null; // Параметры для ссылки - public /*.Settings.*/$settings; // Ссылка на настройки - public $user; // Обьект пользователя + public /*.Registry.*/$config; // Ссылка на настройки + public /*.User.*/$user; // Обьект пользователя // Для Widgets public $view = null; @@ -62,7 +64,7 @@ class Action } public function loadConfig($name) { - $basePath = $this->settings['base']; + $basePath = $this->config->get('site', 'path'); $filename = Path::join($basePath, 'modules', $name); $settings = []; @@ -81,7 +83,7 @@ class Action public function installPath($name) { - $basePath = $this->settings['base']; + $basePath = $this->config->get('site', 'path'); return Path::join($basePath, "modules", $name); } @@ -94,7 +96,7 @@ class Action } function findIcon($icon, $size) { - $webPath = $this->settings['web']; + $webPath = $this->config->get('site', 'web'); return Path::join($webPath, 'icons', $size . 'x' . $size, $icon . '.png'); } @@ -108,8 +110,8 @@ class Action { $file = $name . self::TEMPLATE_EXTENSION; - $basePath = $this->settings['base']; - $webPath = $this->settings['web']; + $basePath = $this->config->get('site', 'path'); + $webPath = $this->config->get('site', 'web'); $list = array( Path::join($this->modulePath, 'templates', $this->viewPathPrefix) => Path::join($webPath, "modules", $this->name, 'templates', $this->viewPathPrefix), @@ -151,7 +153,7 @@ class Action public function getModel($name) { if (!$this->factory) { - $this->factory = new Factory($this->db, $this->settings['registry']); + $this->factory = new Factory($this->db, $this->config); } return $this->factory->getModel($name); } @@ -327,7 +329,8 @@ class Action */ public function render() { - if ($this->view instanceof View) { + $view = $this->view; + if ($view instanceof View) { $this->view->assignValues($this->ctrlValues); /*.Composite.*/$node = null; @@ -369,8 +372,8 @@ class Action $this->_getActionPath()->getPath($this, ($action) ? $action : $request->getAction()); } - function redirect($action) { - header('location: ' . $action->toString()); + function redirect(/*.string.*/$action) { + header('location: ' . $action); exit(); } } diff --git a/src/Controller/Component.php b/src/Controller/Component.php index c87cf59..7210d32 100644 --- a/src/Controller/Component.php +++ b/src/Controller/Component.php @@ -12,20 +12,11 @@ use ctiso\HttpRequest, ctiso\Database, ctiso\Database\PDOStatement, ctiso\Collection, - ctiso\Settings, + ctiso\Registry, App\Controller\Site, PHPTAL, PHPTAL_PreFilter_Normalize; -function replaceContent($match) { - $result = Tales\Component::phptal_component(htmlspecialchars_decode($match[3])); - return $result; -} - -function applyComponents($text) { - return preg_replace_callback('/<(\w+)(\s+[a-zA-Z\-]+=\"[^\"]*\")*\s+tal:replace="structure\s+component:([^\"]*)"[^>]*>/u', 'replaceContent', $text); -} - class FakeTemplate { public $_data = []; public $_name = ''; @@ -56,11 +47,13 @@ class Component public $component_id; public $component_title; + public $COMPONENTS_WEB; - public /*.Settings.*/$registry; + public /*.Registry.*/$config; public /*.Database.*/$db; public /*.Collection.*/$parameter; + public $output = 'html'; public $module; @@ -70,7 +63,12 @@ class Component function before() { } - function get($request, $key, $default) { + static function replaceContent($match) { + return \ctiso\Tales::phptal_component(htmlspecialchars_decode($match[3])); + } + + static function applyComponents($text) { + return preg_replace_callback('/<(\w+)(\s+[a-zA-Z\-]+=\"[^\"]*\")*\s+tal:replace="structure\s+component:([^\"]*)"[^>]*>/u', 'ctiso\\Controller\\Component::replaceContent', $text); } function execute(HttpRequest $request, $has_id = true) { @@ -97,8 +95,9 @@ class Component return new FakeTemplate($name); } - /*.Settings.*/$registry = $this->registry; - $template = ($this->template) ? $this->template : $registry->readKey(array('system', 'template')); + /*.Registry.*/$config = $this->config; + $default = $config->get('site', 'template'); + $template = ($this->template) ? $this->template : $default; $selected = null; foreach ($this->viewPath as $index => $viewPath) { @@ -121,13 +120,14 @@ class Component $tpl->stripComments(true); $tpl->addPreFilter(new PHPTAL_PreFilter_Normalize()); - $tpl->set('common', Path::join(WWW_PATH, '../', 'common')); - $tpl->set('script', Path::join(WWW_PATH, 'js')); - $tpl->set('media', Path::join(TEMPLATE_WEB, $template)); - if ($registry) { - $tpl->set('site_template', SITE_WWW_PATH . '/templates' . $registry->readKey(array('system', 'template'))); + $tpl->set('common', Path::join($this->config->get('system', 'web'), '../', 'common')); + $tpl->set('script', Path::join($this->config->get('system', 'web'), 'js')); + $tpl->set('media', Path::join($this->config->get('system', 'templates_web'), $template)); + + if ($default) { + $tpl->set('site_template', $this->config->get('site', 'web') . '/templates/' . $default); } - $tpl->set('base', SITE_WWW_PATH); + $tpl->set('base', $this->config->get('site', 'web')); $tpl->set('component_base', $this->webPath[$selected]); $tpl->set('component', Path::join($this->webPath[$selected], 'templates', $template)); @@ -199,7 +199,7 @@ class Component public function setParameters(/*.Composite.*/ $view) { $form = new Form(); - $options = new OptionFactory($this->db, $this->registry); + $options = new OptionFactory($this->db, $this->config); $settings = $this->getInfo(); $form->addFieldList($settings['parameter'], $options); @@ -226,7 +226,7 @@ class Component } $name = $path; - $path = Path::join (BASE_PATH, 'components', $name, $name . '.php'); + $path = Path::join ($this->config->get('site', 'path'), 'components', $name, $name . '.php'); $className = 'Component_' . $name; /*.Component.*/$component = null; @@ -235,26 +235,26 @@ class Component require_once ($path); $component = new $className(); - $component->viewPath = array(BASE_PATH . '/components/' . $name . '/'); - $component->webPath = array(SITE_WWW_PATH . '/components/' . $name); - $component->COMPONENTS_WEB = SITE_WWW_PATH . '/components/'; + $component->viewPath = array($this->config->get('site', 'path') . '/components/' . $name . '/'); + $component->webPath = array($this->config->get('site', 'web') . '/components/' . $name); + $component->COMPONENTS_WEB = $this->config->get('site', 'web') . '/components/'; } else { - $path = Path::join (COMPONENTS, $name, $name . '.php'); + $path = Path::join ($this->config->get('system', 'components'), $name, $name . '.php'); require_once ($path); $component = new $className(); - $component->viewPath = array(COMPONENTS . '/' . $name . '/', BASE_PATH . '/components/' . $name . '/'); + $component->viewPath = array($this->config->get('system', 'components') . '/' . $name . '/', $this->config->get('site', 'path') . '/components/' . $name . '/'); if (defined('COMPONENTS_WEB')) { - $component->webPath = array(COMPONENTS_WEB . '/' . $name, SITE_WWW_PATH . '/components/' . $name); - $component->COMPONENTS_WEB = COMPONENTS_WEB; + $component->webPath = array($this->config->get('system', 'components_web') . '/' . $name, $this->config->get('site', 'web') . '/components/' . $name); + $component->COMPONENTS_WEB = $this->config->get('system', 'components_web'); } } $db = $site->db; $component->db = $db; - $component->registry = $site->registry; + $component->config = $site->config; $component->site = $site; $stmt = $db->prepareStatement("SELECT * FROM component WHERE code = ?"); diff --git a/src/Controller/Front.php b/src/Controller/Front.php index 5f243af..bc4096c 100644 --- a/src/Controller/Front.php +++ b/src/Controller/Front.php @@ -26,9 +26,9 @@ class Front extends Action /** * @param Settings $settings */ - public function __construct($db, $settings, $user, $default) { + public function __construct($db, $config, $user, $default) { parent::__construct(); - $this->settings = $settings; + $this->config = $config; $this->db = $db; $this->user = $user; $this->default = $default; @@ -50,11 +50,11 @@ class Front extends Action $module = $this->modules[$name]; return $module->access->execute($request); } + $config = $this->config; - /*.Settings.*/$system = $this->settings['system']; + $moulesPath = Path::join($config->get('system', 'path'), 'modules'); + $logPath = Path::join($config->get('site', 'path'), $config->get('system', 'access.log')); - $moulesPath = Path::join($this->settings['base'], $system->readKey(['path', 'modules'])); - $logPath = Path::join($this->settings['site'], $system->readKey(['path', 'access.log'])); $moduleFile = Path::join($moulesPath, $name, 'classes', $controller ? $controller : $name); $ucname = ucfirst($name); @@ -66,14 +66,14 @@ class Front extends Action $module->modulePath = $modPath; $module->name = $name; // - $module->settings = $this->settings; + $module->config = $this->config; $module->db = $this->db; $module->user = $this->user; // Ведение лога $logger = new ActionLogger($module, $logPath, $this->user); $logger->before = $this->loadSettings(Path::join($modPath, 'filter', 'logger.php')); // Управление доступом - $module->access = new ActionAccess($logger); + $module->access = new ActionAccess($logger, $this->user); $module->access->access = $this->loadSettings(Path::join($modPath, 'filter', 'access.php')); $module->setUp(); diff --git a/src/Controller/Installer.php b/src/Controller/Installer.php index a5a55fa..e544921 100644 --- a/src/Controller/Installer.php +++ b/src/Controller/Installer.php @@ -35,7 +35,7 @@ class Installer // Проверка версии обновления function isChanged($name) // Информация о модулях { - $item = $this->_registry->readKey(array($name)); + $item = $this->_registry->get('system', $name); if ($item) { $setup = $this->getSetupFile($name); if (file_exists($setup) && (filemtime($setup) > $item['time'])) { @@ -81,7 +81,7 @@ class Installer $settings = new Settings($setup); $settings->read(); - $item = $registry->readKey(array($name)); + $item = $registry->get('system', $name); $version_new = $settings->get('version'); if ($item) { @@ -101,8 +101,9 @@ class Installer // Обновление версии меню $registry->removeKey($name); - $registry->writeKey(array($name), $settings->get('settings')); - $registry->writeKey(array($name), + + $registry->set($name, $settings->get('settings')); + $registry->set($name, array('version' => $version_new, 'time' => filemtime($setup))); } diff --git a/src/Controller/Service.php b/src/Controller/Service.php index 26c81cc..fbcbb69 100644 --- a/src/Controller/Service.php +++ b/src/Controller/Service.php @@ -5,13 +5,14 @@ */ namespace ctiso\Controller; use ctiso\Path, + ctiso\Registry, ctiso\Database\PDOStatement; class Service { public $viewPath = []; public $webPath = []; - public $registry; // Registry->getInstance + public /*.Registry.*/$config; public $template; public $templatePath; public $COMPONENTS_WEB; @@ -33,7 +34,7 @@ class Service */ private function getModelPath($name) { - return Path::join (CMS_PATH, "model", $name . ".php"); + return Path::join ($this->config->get('system', 'path'), "model", $name . ".php"); } /** diff --git a/src/Controller/State.php b/src/Controller/State.php index 481faaf..225de1a 100644 --- a/src/Controller/State.php +++ b/src/Controller/State.php @@ -1,7 +1,8 @@ createStyles($doc); foreach ($this->table as $table) { - if ($table instanceof Excel_Table) { + if ($table instanceof Table) { $table->createTable($doc); } else { $table_data = call_user_func($table); diff --git a/src/Excel/Table.php b/src/Excel/Table.php index 1861a36..5a1786f 100644 --- a/src/Excel/Table.php +++ b/src/Excel/Table.php @@ -4,7 +4,9 @@ * Клетка таблицы */ namespace ctiso\Excel; -use XMLWriter; +use XMLWriter, + ctiso\Excel\DateTime as Excel_DateTime, + ctiso\Excel\Number as Excel_Number; class TableCell { diff --git a/src/Filter/ActionAccess.php b/src/Filter/ActionAccess.php index da01325..c27b1b2 100644 --- a/src/Filter/ActionAccess.php +++ b/src/Filter/ActionAccess.php @@ -5,15 +5,18 @@ */ namespace ctiso\Filter; use ctiso\Filter\UserAccess, - ctiso\HttpRequest; + ctiso\HttpRequest, + ctiso\Role\User; class ActionAccess { public $access = array(); public $processor; + public /*.User.*/$user; - function __construct(/*.Filter.*/$processor) { + function __construct(/*.Filter.*/$processor, $user) { $this->processor = $processor; + $this->user = $user; } /** diff --git a/src/Filter/Authorization.php b/src/Filter/Authorization.php index 56ebc4d..0538eb2 100644 --- a/src/Filter/Authorization.php +++ b/src/Filter/Authorization.php @@ -5,38 +5,41 @@ namespace ctiso\Filter; class Authorization { const SESSION_BROWSER_SIGN_SECRET = '@w3dsju45Msk#'; const SESSION_BROWSER_SIGN_KEYNAME = 'session.app.browser.sign'; + public $group; - static function isLogged($group = 'access') { + function __construct($group) { + $this->group = $group; + } + + function isLogged() { // echo session_status(); if (session_status() == PHP_SESSION_NONE) { session_start(); } $hash = self::getBrowserSign(); // Если $hash не совпадает $_SESSION['hash'] то удаляем сессию - if (isset($_SESSION[$group]) && isset($_SESSION[self::SESSION_BROWSER_SIGN_KEYNAME])) { + if (isset($_SESSION[$this->group]) && isset($_SESSION[self::SESSION_BROWSER_SIGN_KEYNAME])) { if ($hash == $_SESSION[self::SESSION_BROWSER_SIGN_KEYNAME]) { // UserAccess::getUserById($_SESSION ['access']); // Поиск по идентификатору return true; } else { - return false; + return false; } } return false; } - static function enter($id, $group = 'access') - { + function enter($id) { // $db->executeQuery("UPDATE visitor SET sid = '' WHERE id_visitor = " . $result->getInt('id_user')); // session_register("access"); // session_register("time"); -// $_SESSION ["group"] = $result->getInt('access'); - $_SESSION [$group] = $id; // id_user + $_SESSION [$this->group] = $id; $_SESSION [self::SESSION_BROWSER_SIGN_KEYNAME] = self::getBrowserSign(); - $_SESSION ["time"] = time(); + $_SESSION ["time"] = time(); } - private static function getBrowserSign() + static function getBrowserSign() { $rawSign = self::SESSION_BROWSER_SIGN_SECRET; // $signParts = array('HTTP_USER_AGENT', 'HTTP_ACCEPT_ENCODING'); @@ -48,7 +51,7 @@ class Authorization { return md5($rawSign); } - static function logout() { + function logout() { session_destroy(); } } diff --git a/src/Filter/Login.php b/src/Filter/Login.php index 74f2259..7d976bd 100644 --- a/src/Filter/Login.php +++ b/src/Filter/Login.php @@ -13,6 +13,7 @@ use ctiso\Filter\Filter, ctiso\HttpRequest, ctiso\Settings, ctiso\Database, + ctiso\Role\User, ctiso\Collection; class Login extends Filter @@ -22,7 +23,7 @@ class Login extends Filter public $mode = 'ajax'; public $user; - public $role; + public /*.User.*/$role; public $whitelist; function __construct($processor, $role, $whitelist = []) { @@ -140,7 +141,7 @@ class Login extends Filter // Параметры при неправильной авторизации // Действия по умолчанию !! Возможно переход на форму регистрации if ($request->get('mode') == 'ajax') { - if (!$this->requestIsWhite($request, $this->whiteRequestList)) { + if (!$this->requestIsWhite($request, $this->whitelist)) { return json_encode(array('result' => 'fail', 'message' =>"NOT_AUTHORIZED")); } } else { diff --git a/src/Form/OptionFactory.php b/src/Form/OptionFactory.php index 42ae288..d0fd9e8 100644 --- a/src/Form/OptionFactory.php +++ b/src/Form/OptionFactory.php @@ -38,7 +38,7 @@ class OptionFactory { try { $query_result = $this->db->executeQuery("SELECT * FROM $table"); $field->options = $this->optionsDB($key, $value, $query_result); - } catch(Exception $ex) { + } catch(\Exception $ex) { $field->options = []; } } elseif (isset($input['options.pair'])) { diff --git a/src/Path.php b/src/Path.php index 803952e..5234e73 100644 --- a/src/Path.php +++ b/src/Path.php @@ -372,7 +372,7 @@ class Path */ public function getContent($allow = null, $ignore = array()) { - $ignore = array_merge(array(".", ".."), $ignore); + $ignore = array_merge(array(".", ".."), $ignore); return self::fileList($this->__toString(), $allow, $ignore); } diff --git a/src/Registry.php b/src/Registry.php new file mode 100644 index 0000000..552c086 --- /dev/null +++ b/src/Registry.php @@ -0,0 +1,40 @@ +namespace[$namespace] = [ + 'path' => $filePath, + 'data' => $data + ]; + } + + function importArray($namespace, $data = []) { + if (isset($this->namespace[$namespace])) { + $data = array_merge($this->namespace[$namespace]['data'], $data); + } + $this->namespace[$namespace] = [ + 'path' => null, + 'data' => $data + ]; + } + + public function get($ns, $key) { + return $this->namespace[$ns]['data'][$key]; + } + + public function has($ns, $key) { + return isset($this->namespace[$ns]['data'][$key]); + } + + function set($ns, $key, $value) { + $this->namespace[$ns]['data'][$key] = $value; + } +} diff --git a/src/Settings.php b/src/Settings.php index 4a677a8..5ea6a52 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -10,12 +10,12 @@ * Имя необходимо чтобы потом легко было удалить ненужные ветки дерева */ namespace ctiso; -use ctiso\Collection, - ctiso\File, +use ctiso\File, Exception; -class Settings extends Collection +class Settings { + public $data; protected $file; protected $format = 'php'; @@ -45,7 +45,8 @@ class Settings extends Collection if (!is_array($settings)) { throw new Exception($this->file); } - return $this->import($settings); + + $this->data = $settings; } /** @@ -175,11 +176,20 @@ class Settings extends Collection file_put_contents (($file) ? $file : $this->file, $result); } - /** - * Список модулей - */ - public function getModules() + public function set($key, $value) { + $this->data[$key] = $value; + } + + public function get($key, $default = null) { - return array_keys($this->data); + return isset($this->data[$key]) && $this->data[$key] != '' ? $this->data[$key] : $default; + } + + function export() { + return $this->data; + } + + function import($data) { + $this->data = $data; } } diff --git a/src/Tales.php b/src/Tales.php index c0e09d4..55bffc6 100644 --- a/src/Tales.php +++ b/src/Tales.php @@ -32,7 +32,7 @@ class Tales_Component implements PHPTAL_Tales } class Tales { - static $site; + static /*.Site.*/$site; static function phptal_date ($e) { return date("d.m.Y", $e); diff --git a/src/Url.php b/src/Url.php index 3b879ef..a7b16fd 100644 --- a/src/Url.php +++ b/src/Url.php @@ -4,7 +4,7 @@ namespace ctiso; class Url { public $parts = []; - public $parent; + public /*.Url.*/$parent; function __construct() { } diff --git a/src/Validator/Rule/Count.php b/src/Validator/Rule/Count.php index 71d60a9..ff030a4 100644 --- a/src/Validator/Rule/Count.php +++ b/src/Validator/Rule/Count.php @@ -30,7 +30,7 @@ class Count extends AbstractRule $count = count(array_filter(array_map('trim', explode(";", $container->get($this->field))), array($this, 'not_empty'))); - return $count >= $this->size && $count <= $this->max; + return $count >= $this->size && $count <= ((int)$this->max); } } diff --git a/src/View/Page.php b/src/View/Page.php index 4ccb457..d224fc2 100644 --- a/src/View/Page.php +++ b/src/View/Page.php @@ -2,7 +2,7 @@ namespace ctiso\View; use ctiso\View\View, - App\Controller\Site, + App\Controller\Site, /* Нужно импортровать интерфейс */ ctiso\Controller\Component, ctiso\HttpRequest; @@ -10,7 +10,7 @@ class Page extends View { private $counter; public $text; - public $site; + public /*.Site.*/$site; function __construct($data, $site) { @@ -55,8 +55,7 @@ class Page extends View function replaceContent($match, $offset) { - /*.Component.*/$component = null; - $component = $this->site->loadComponent($match); + /*.Component.*/$component = $this->site->loadComponent($match); $req = new HttpRequest(); unset($req['active_page']); diff --git a/src/View/Top.php b/src/View/Top.php index eb67c02..47d1a2c 100644 --- a/src/View/Top.php +++ b/src/View/Top.php @@ -29,7 +29,7 @@ class Top extends Composite { private function groupFiles(array $list, $debugMode = true) { $debug = ($debugMode) ? 'debug=1' : ''; - $path = parse_url(WWW_PATH, PHP_URL_PATH); + $path = parse_url($this->config->get('system', 'web'), PHP_URL_PATH); $groups = array(); $use = array(); @@ -120,7 +120,7 @@ class Top extends Composite { $this->set('deps', implode(",", array_values($this->require))); $this->set('title', $this->getTitle()); - $this->set('jspath', enableHttps(WWW_PATH)); + $this->set('jspath', enableHttps($this->config->get('system', 'web'))); // return $this->execute(); // execute+phptal ?? } diff --git a/src/WrongRequestException.php b/src/WrongRequestException.php index 6ebddfc..34863be 100644 --- a/src/WrongRequestException.php +++ b/src/WrongRequestException.php @@ -2,6 +2,6 @@ namespace ctiso; -class WrongRequestException extends Exception +class WrongRequestException extends \Exception { }