public function getTemplatePaths() { $templatesPaths = []; if ('' == $this->getModuleName()) { $templatesPaths[] = $this->app->getPath() . DS . 'Templates' . DS . str_replace('\\', DS, $this->getShortName()); } else { $templatesPaths[] = $this->app->getPath() . DS . 'Modules' . DS . $this->getModuleName() . DS . 'Templates' . DS . str_replace('\\', DS, $this->getShortName()); } if ('' != $this->getModuleName() && is_readable($moduleLayoutPath = $this->app->getPath() . DS . 'Layouts' . DS . $this->getModuleName())) { $templatesPaths[] = $moduleLayoutPath; } $templatesPaths[] = $this->app->getPath() . DS . 'Layouts'; return $templatesPaths; }
public function getFunctions() { $app = Application::getInstance(); return ['asset' => new \Twig_Function_Function(function ($path) use($app) { return $app->assets->publish($path); }), 'publish' => new \Twig_Function_Function(function ($path) use($app) { $app->assets->publish($path); return ''; }), 'publishCss' => new \Twig_Function_Function(function () use($app) { return $app->assets->getPublishedCss(); }, ['is_safe' => ['html']]), 'publishJs' => new \Twig_Function_Function(function () use($app) { return $app->assets->getPublishedJs(); }, ['is_safe' => ['html']]), 'widget' => new \Twig_Function_Function(function ($name, $options = []) { $widget = WidgetsFactory::getInstance($name, $options); return $widget->render(); }, ['is_safe' => ['html']]), 'helper' => new \Twig_Function_Function(function ($name) { return $this->helper($name, array_slice(func_get_args(), 1)); }, ['is_safe' => ['html']]), 'element' => new \Twig_Function_Function(function ($el, $name = '', $options = [], $attrs = []) { return Helpers::element($el, $name, $options, $attrs); }, ['is_safe' => ['html']]), 'selectTreeByModel' => new \Twig_Function_Function(function ($model, $selected = 0, $htmlOptions = [], $options = []) { return Helpers::selectTreeByModel($model, $selected, $htmlOptions, $options); }, ['is_safe' => ['html']]), 'blockOptionInput' => new \Twig_Function_Function(function ($name, $settings, $value = null, $htmlOptions = []) { return Helpers::blockOptionInput($name, $settings, $value, $htmlOptions); }, ['is_safe' => ['html']])]; }
public static function redirect($url) { $protocol = \T4\Mvc\Application::getInstance()->request->protocol; $host = \T4\Mvc\Application::getInstance()->request->host; header('Location: ' . $protocol . '://' . $host . $url, true, 302); exit; }
public function __construct(Config $config) { $this->config = $config; $reflect = new \ReflectionClass($this); $this->path = dirname($reflect->getFileName()); $this->assetsPath = '/' . str_replace(DS, '/', str_replace(\T4\ROOT_PATH, '', $this->path)); $this->app = Application::instance(); }
public static function getLocation() { if (null == Session::get('location')) { $app = \T4\Mvc\Application::getInstance(); Session::set('location', $app->extensions->sxgeo->getLocation($app->request->ip)); } return Session::get('location'); }
public function __construct($paths = []) { $this->paths = (array) $paths; $this->links = new Std(); $this->links->app = Application::instance(); $loader = new \Twig_Loader_Filesystem($paths); $this->twig = new \Twig_Environment($loader, ['cache' => ROOT_PATH_PROTECTED . '/Cache/Twig', 'auto_reload' => true]); $this->twig->addGlobal('app', $this->links->app); $this->twig->addExtension(new TwigExtensions()); }
protected function getConfig() { $config = Application::getInstance()->config; if (empty($config->mail)) { $config->mail = new Std(); } if (empty($config->mail->method)) { $config->mail->method = 'php'; } return $config->mail; }
public static function redirect($url) { if (false === strpos($url, 'http') && false === strpos($url, 'https')) { $protocol = \T4\Mvc\Application::instance()->request->protocol; $host = \T4\Mvc\Application::instance()->request->host; header('Location: ' . $protocol . '://' . $host . $url, true, 302); } else { header('Location: ' . $url, true, 302); } exit; }
/** * This function must check the user session to be sure that he/she is * authorized to upload and access files in the File Browser. * * @return boolean */ function CheckAuthentication() { // WARNING : DO NOT simply return "true". By doing so, you are allowing // "anyone" to upload and list the files in your server. You must implement // some kind of session validation here. Even something very simple as... // return isset($_SESSION['IsAuthorized']) && $_SESSION['IsAuthorized']; // ... where $_SESSION['IsAuthorized'] is set to "true" as soon as the // user logs in your system. To be able to use session variables don't // forget to add session_start() at the top of this file. $app = \T4\Mvc\Application::getInstance(); return null !== $app->user; }
protected function getConnection() : Connection { if (isset($this->options['model']) && $this->options['model'] instanceof Model) { return $this->options['model']->getDbConnection(); } elseif (isset($this->options['connection']) && $this->options['connection'] instanceof Connection) { return $this->options['connection']; } elseif (isset($this->options['connection']) && is_string($this->options['connection'])) { return Application::instance()->db->{$this->options['connection']}; } else { return Application::instance()->db->default; } }
public function __invoke($name = '') { if (empty($this->formFieldName) && !empty($name)) { $this->formFieldName = $name; } if (empty($this->formFieldName)) { throw new Exception('Empty form field name for file upload'); } if (empty($this->uploadPath)) { throw new Exception('Invalid upload path'); } $realUploadPath = \T4\Fs\Helpers::getRealPath($this->uploadPath); if (!is_dir($realUploadPath)) { try { \T4\Fs\Helpers::mkDir($realUploadPath); } catch (\T4\Fs\Exception $e) { throw new Exception($e->getMessage()); } } $request = Application::getInstance()->request; if (!$request->isUploaded($this->formFieldName)) { throw new Exception('File for \'' . $this->formFieldName . '\' is not uploaded'); } if (!$this->isUploaded($this->formFieldName)) { throw new Exception('Error while uploading file \'' . $this->formFieldName . '\': ' . $request->files->{$this->formFieldName}->error); } if ($request->isUploadedArray($this->formFieldName)) { $ret = []; foreach ($request->files->{$this->formFieldName} as $n => $file) { if (!$this->checkExtension($file->name)) { throw new Exception('Invalid file extension'); } $uploadedFileName = $this->suggestUploadedFileName($realUploadPath, $file->name); if (move_uploaded_file($file->tmp_name, $realUploadPath . DS . $uploadedFileName)) { $ret[$n] = $this->uploadPath . '/' . $uploadedFileName; } else { $ret[$n] = false; } } return $ret; } else { $file = $request->files->{$this->formFieldName}; if (!$this->checkExtension($file->name)) { throw new Exception('Invalid file extension'); } $uploadedFileName = $this->suggestUploadedFileName($realUploadPath, $file->name); if (move_uploaded_file($file->tmp_name, $realUploadPath . DS . $uploadedFileName)) { return $this->uploadPath . '/' . $uploadedFileName; } else { return false; } } }
/** * @return \T4\Dbal\Connection */ protected function getConnection() { if (!empty($this->class)) { return $this->class::getDbConnection(); } else { if ('cli' == PHP_SAPI) { $app = \T4\Console\Application::instance(); } else { $app = \T4\Mvc\Application::instance(); } return $app->db->default; } }
public function getAllTemplates() { $route = new Route($this->path); $controller = Application::getInstance()->createController($route->module, $route->controller); $templates = []; foreach ($controller->getTemplatePaths() as $path) { foreach (glob($path . DS . $route->action . '.*.block.html') as $filename) { preg_match('~.*\\.([^\\.]+)\\.block\\.html~', basename($filename), $m); $templates[] = $m[1]; } } return $templates; }
public function render() { try { $app = Application::instance(); $path = $this->params->path; unset($this->params->path); $template = isset($this->params->template) ? $this->params->template : ''; unset($this->params->template); $block = $app->callBlock($path, $template, $this->params); return $block; } catch (Exception $e) { return $e->getMessage(); } }
protected function getConfig() { if ('cli' == PHP_SAPI) { $config = \T4\Console\Application::instance()->config; } else { $config = \T4\Mvc\Application::instance()->config; } if (empty($config->mail)) { $config->mail = new Std(); } if (empty($config->mail->method)) { $config->mail->method = 'php'; } return $config->mail; }
public function logout() { if (!\T4\Http\Helpers::issetCookie(self::AUTH_COOKIE_NAME)) { return; } $hash = \T4\Http\Helpers::getCookie(self::AUTH_COOKIE_NAME); $session = UserSession::findByHash($hash); if (empty($session)) { \T4\Http\Helpers::unsetCookie(self::AUTH_COOKIE_NAME); return; } $session->delete(); \T4\Http\Helpers::unsetCookie(self::AUTH_COOKIE_NAME); $app = Application::getInstance(); $app->user = null; }
public function testGetPageLink() { $pager = new Pager(['total' => 7, 'url' => 'http://example.com/test.html?foo=1&bar[baz]=2&page=%d']); $getLink = new \ReflectionMethod($pager, 'getPageLink'); $getLink->setAccessible(true); $link = $getLink->invokeArgs($pager, ['page' => 3]); $this->assertEquals('http://example.com/test.html?foo=1&bar%5Bbaz%5D=2&page=3', $link); $_SERVER = ['SERVER_PORT' => '80', 'HTTP_HOST' => 'example.com', 'REQUEST_URI' => '/test.html?foo=1&bar%5Bbaz%5D=2']; $pager = new Pager(['total' => 7]); $link = $getLink->invokeArgs($pager, ['page' => 3]); $this->assertEquals('http://example.com/test.html?foo=1&bar%5Bbaz%5D=2&page=3', $link); Application::instance()->request->url->query = new QueryString(); $pager = new Pager(['total' => 7]); $link = $getLink->invokeArgs($pager, ['page' => 3]); $this->assertEquals('http://example.com/test.html?page=3', $link); }
public function render() { $id = $this->params->id; $app = Application::instance(); $blocks = \App\Models\Block::findAllBySection($id, ['order' => '`order`']); $ret = '<section role="section" data-section-id="' . $id . '">' . "\n"; foreach ($blocks as $block) { try { $content = $app->callBlock($block->path, $block->template, new Std(json_decode($block->options, true))); } catch (Exception $e) { $content = $e->getMessage(); } $ret .= '<article role="block" data-block-id="' . $block->getPk() . '">' . $content . '</article>' . "\n"; } return $ret . '</section>' . "\n"; }
public function uploadImage($formFieldName) { $request = Application::getInstance()->request; if (!$request->existsFilesData() || !$request->isUploaded($formFieldName) || $request->isUploadedArray($formFieldName)) { return $this; } try { $uploader = new Uploader($formFieldName); $uploader->setPath('/public/news/stories/images'); $image = $uploader(); if ($this->image) { $this->deleteImage(); } $this->image = $image; } catch (Exception $e) { $this->image = null; } return $this; }
/** * @param string|\T4\Dbal\Connection $connection */ public static function setConnection($connection) { if (is_string($connection)) { if ('cli' == PHP_SAPI) { $app = \T4\Console\Application::instance(); } else { $app = \T4\Mvc\Application::instance(); } $connection = $app->db->{$connection}; } self::$connections[get_called_class()] = $connection; }
public final function action($name, $params = []) { if ($params instanceof Std) { $params = $params->toArray(); } $name = ucfirst($name); $actionMethodName = 'action' . $name; if (!method_exists($this, $actionMethodName)) { throw new E404Exception('Action ' . $name . ' is not found in controller ' . get_class($this)); } if (method_exists($this, 'access')) { $check = $this->access($name); if (false === $check) { throw new E403Exception('Access denied'); } } // Продолжаем выполнение действия только если из beforeAction не передано false if ($this->beforeAction($name)) { $p = []; $request = Application::getInstance()->request; foreach ($this->getActionParameters($name) as $param) { if (isset($params[$param->name])) { $p[$param->name] = $params[$param->name]; unset($params[$param->name]); } elseif (isset($request->post[$param->name])) { $p[$param->name] = $request->post[$param->name]; } elseif (isset($request->get[$param->name])) { $p[$param->name] = $request->get[$param->name]; } elseif ($param->isDefaultValueAvailable()) { $p[$param->name] = $param->getDefaultValue(); } else { throw new ControllerException('Missing argument ' . $param->name . ' for action ' . $actionMethodName); } } $p = array_merge($p, $params); call_user_func_array([$this, $actionMethodName], $p); $this->afterAction($name); } return $this->data; }
/** * Пытается подобрать соответствующий роутинг для URL, отсутствующего в конфиге роутинга * @param \T4\Mvc\Route $url * @return Route * @throws RouterException */ protected function guessInternalPath($url) { $urlParts = preg_split('~/~', $url->basepath, -1, PREG_SPLIT_NO_EMPTY); $app = \T4\Mvc\Application::instance(); if (0 == count($urlParts)) { return new Route(['module' => '', 'controller' => self::DEFAULT_CONTROLLER, 'action' => self::DEFAULT_ACTION, 'params' => [], 'format' => $url->extension ?: 'html']); } if (1 == count($urlParts)) { if ($app->existsModule($urlParts[0])) { return new Route(['module' => ucfirst($urlParts[0]), 'controller' => self::DEFAULT_CONTROLLER, 'action' => self::DEFAULT_ACTION, 'params' => [], 'format' => $url->extension ?: 'html']); } elseif ($app->existsController(null, $urlParts[0])) { return new Route(['module' => '', 'controller' => ucfirst($urlParts[0]), 'action' => self::DEFAULT_ACTION, 'params' => [], 'format' => $url->extension ?: 'html']); } else { return new Route(['module' => '', 'controller' => self::DEFAULT_CONTROLLER, 'action' => ucfirst($urlParts[0]), 'params' => [], 'format' => $url->extension ?: 'html']); } } if (2 == count($urlParts)) { if ($app->existsModule($urlParts[0])) { if ($app->existsController($urlParts[0], $urlParts[1])) { return new Route(['module' => ucfirst($urlParts[0]), 'controller' => ucfirst($urlParts[1]), 'action' => self::DEFAULT_ACTION, 'params' => [], 'format' => $url->extension ?: 'html']); } else { return new Route(['module' => ucfirst($urlParts[0]), 'controller' => self::DEFAULT_CONTROLLER, 'action' => ucfirst($urlParts[1]), 'params' => [], 'format' => $url->extension ?: 'html']); } } elseif ($app->existsController(null, $urlParts[0])) { return new Route(['module' => '', 'controller' => ucfirst($urlParts[0]), 'action' => ucfirst($urlParts[1]), 'params' => [], 'format' => $url->extension ?: 'html']); } } if (3 == count($urlParts)) { if ($app->existsModule($urlParts[0]) && $app->existsController($urlParts[0], $urlParts[1])) { return new Route(['module' => ucfirst($urlParts[0]), 'controller' => ucfirst($urlParts[1]), 'action' => ucfirst($urlParts[2]), 'params' => [], 'format' => $url->extension ?: 'html']); } } throw new RouterException('Route to path \'' . $url->basepath . '\' is not found'); }
<?php require realpath(__DIR__ . '/../t4/framework/boot.php'); \T4\Mvc\Application::getInstance()->run();
public function __construct($options = []) { $this->app = Application::getInstance(); $this->options = new Std(); $this->options->fromArray($options); }
/** * @param string|\T4\Dbal\Connection $connection */ public static function setConnection($connection) { if (is_string($connection)) { if ('cli' == PHP_SAPI) { $app = \T4\Console\Application::getInstance(); } else { $app = \T4\Mvc\Application::getInstance(); } static::$connection = $app->db->{$connection}; } elseif ($connection instanceof Connection) { static::$connection = $connection; } }
public function action($name, $params = []) { $name = ucfirst($name); $actionMethodName = 'action' . $name; if (!method_exists($this, $actionMethodName)) { throw new E404Exception('Action ' . $name . ' is not found in controller ' . get_class($this)); } // Params if ($params instanceof Std) { $params = $params->toArray(); } $p = []; $request = Application::instance()->request; foreach ($this->getActionParameters($name) as $param) { if (isset($params[$param->name])) { $p[$param->name] = $params[$param->name]; unset($params[$param->name]); } elseif (isset($request->body[$param->name])) { $p[$param->name] = $request->body[$param->name]; } elseif (isset($request->post[$param->name])) { $p[$param->name] = $request->post[$param->name]; } elseif (isset($request->get[$param->name])) { $p[$param->name] = $request->get[$param->name]; } elseif ($param->isDefaultValueAvailable()) { $p[$param->name] = $param->getDefaultValue(); } else { throw new ControllerException('Missing argument ' . $param->name . ' for action ' . $actionMethodName); } // Arguments class hinting! if (isset($p[$param->name])) { $class = $param->getClass(); if (null !== $class && $class instanceof \ReflectionClass) { if (is_a($class->name, Std::class, true)) { $val = $p[$param->name]; if (is_array($val)) { $p[$param->name] = new $class->name($val); } elseif ($val instanceof IArrayable) { $p[$param->name] = new $class->name($val->toArray()); } } } } } $p = array_merge($p, $params); // /Params if (method_exists($this, 'access')) { $check = $this->access($name, $p); if (false === $check) { throw new E403Exception('Access denied'); } } // Продолжаем выполнение действия только если из beforeAction не передано false if ($this->beforeAction($name, $p)) { $this->{$actionMethodName}(...array_values($p)); $this->afterAction($name, $p); } return $this->data; }