/** * Get connection. */ public function getConnection($reset = false) { static $conn; if ($reset === TRUE) { $conn = null; unset($conn); static $conn; } if (!$conn) { // Connecting to mysql if (empty(Config::get('db.DATABASE'))) { die('Database parameters needed.'); } else { try { // Config mysql link $conn = new PDO('mysql:host=' . Config::get('db.HOSTNAME') . ';dbname=' . Config::get('db.DATABASE') . ';port=' . Config::get('db.PORT'), Config::get('db.USERNAME'), Config::get('db.PASSWORD')); $conn->exec('SET CHARACTER SET utf8'); $conn->exec('SET time_zone = "' . date_default_timezone_get() . '"'); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); if (Config::get('core.DEBUG_BAR') === true && $reset === false) { $config = Config::getInstance(); $pdo = new \DebugBar\DataCollector\PDO\TraceablePDO($conn); $config->debugbar->addCollector(new \DebugBar\DataCollector\PDO\PDOCollector($pdo)); } } catch (\PDOException $e) { throw new QException($e->getMessage()); } } } return $conn; }
public function execute($args) { $folders = Config::get('folders'); if (isset($folders)) { foreach ($folders as $folder) { if (!file_exists(GLOBAL_PATH . '/' . $folder . '/')) { mkdir(GLOBAL_PATH . '/' . $folder . '/', 0755, true); echo "Creating "; $this->makeGreen($folder); "\n"; } } } else { exit($this->makeRed("List of folders cannot be null (Check your Config.yml) \n")); } }
/** * setEmail. * * @param string $email * * @return bool */ public function setEmail($email) { $email = mb_strtolower($email); if (Config::get('modules.USER_VERIFY_ACCOUNT', true)) { if ($email === $this->getOriginalValue('email')) { if ($this->isEmailValidated()) { //Nothing to validate $this->set('recoveryToken', null); $this->set('emailValidating', null); } } else { if (!$this->isEmailValidated()) { parent::set('email', $email); } if ($email != $this->get('emailValidating')) { $this->makeRecoveryToken(); $this->set('emailValidating', $email); } } } else { parent::set('email', $email); } return true; }
/** * checkCsrfProtection. * * @return bool */ public function checkCsrfToken() { if (in_array($_SERVER['REQUEST_METHOD'], ['HEAD', 'GET', 'OPTIONS'])) { return true; } if ($this->checkCsrfTokenExpired()) { return false; } $cookieName = Config::get('cookies.COOKIE_NAME') . '_' . Config::get('cookies.CSRF_NAME', 'csrf'); $token = $_COOKIE[$cookieName]; $fieldKey = Config::get('app.CSRF_FIELD', 'csrf-token'); if (!empty($_REQUEST[$fieldKey])) { return $token === $_REQUEST[$fieldKey]; } $headerKey = Config::get('app.CSRF_HEADER', 'X-CSRF-TOKEN'); $headerKey = str_replace('-', '_', $headerKey); if (!empty($_SERVER["HTTP_{$headerKey}"])) { return $token === $_SERVER["HTTP_{$headerKey}"]; } return false; }
/** * notifySlack. * * @param string $message */ public static function notifySlack($message) { $ips = implode(', ', \Quaver\Core\Helper::getClientIp(true)); // Custom this array to change notificaction UI (see more at Slack.com) $data = ['text' => "{$message}\nClient: {$_SERVER['HTTP_USER_AGENT']} ({$ips})"]; if (Config::get('slack.SLACK_CHANNEL')) { $data['channel'] = '#' . Config::get('slack.SLACK_CHANNEL'); } $ch = curl_init(Config::get('slack.HOOK')); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, 'payload=' . json_encode($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); }
/** * getFromSlug. * * @param string $_slug * @param bool $_short */ public function getFromSlug($_slug, $_short = false) { $db = new DB(); $return = Config::get('core.LANG'); $slug_where = 'slug'; if ($_short) { $slug_where = 'SUBSTR(slug, 1, 2)'; } $_slug = substr($_slug, 0, 3); $language = $db->query("SELECT id FROM {$this->table} WHERE {$slug_where} = '{$_slug}' AND active = 1"); $resultLang = $language->fetchColumn(0); if ($resultLang) { $this->getFromId($resultLang); $return = $this; } return $return; }
<?php /* * Copyright (c) 2014 Alberto González * Distributed under MIT License * (see README for details) */ namespace Quaver\App\Controller; use Quaver\Core\Config; $url = $this->getCurrentRoute(); header('HTTP/1.0 408 Request Timeout'); trigger_error("[408] {$url}", E_USER_WARNING); if (!defined('AJAX_METHOD')) { $this->addTwigVars('siteTitle', 'Error 408 - ' . Config::get('app.BRAND_NAME')); $this->addTwigVars('e408', true); $this->setView('http-errors'); $this->render(); }
/** * Dispatch action. * * @param array $controller * * @return class */ public function dispatch($controller) { global $_lang, $_user; try { if ($controller == 'e401') { $controller = $this->routes[Config::get('app.THEME_BASE_QUAVER')]['/']['401']; } if ($controller == 'e404') { $controller = $this->routes[Config::get('app.THEME_BASE_QUAVER')]['/']['404']; } if ($controller == 'e408') { $controller = $this->routes[Config::get('app.THEME_BASE_QUAVER')]['/']['408']; } if ($controller == 'e500') { $controller = $this->routes[Config::get('app.THEME_BASE_QUAVER')]['/']['500']; } if ($controller == 'maintenance') { $controller = $this->routes[Config::get('app.THEME_BASE_QUAVER')]['/']['maintenance']; } $controllerPath = Config::get('core.CONTROLLER_PATH') . '/' . $controller['controller'] . '.php'; if (isset($controller['view'])) { $controllerView = $controller['view']; } // Try to load controller $defaultNamespace = '\\Quaver\\App\\Controller\\'; $controllerClass = $defaultNamespace . $controller['controller']; $controllerClassActionName = null; if (file_exists($controllerPath)) { if (empty($this->getFileClasses($controllerPath))) { $controllerLoader = new Controller($this); } } if (empty($controllerLoader)) { if (!class_exists($controllerClass)) { throw new QException("The file <{$controllerPath}> not exists."); } $controllerLoader = new $controllerClass($this); $controllerClassActionName = isset($controller['action']) ? $controller['action'] : 'indexAction'; } if (isset($controller['type']) && $controller['type'] != 'api') { $controllerLoader->setType(!empty($controller['type']) ? $controller['type'] : 'view'); } if (isset($controllerView) && $controllerView != 'none') { $controllerLoader->setView($controllerView); } if ($controllerClassActionName !== null) { return $controllerLoader->{$controllerClassActionName}(); } else { return $controllerLoader->action($controllerPath); } } catch (QException $e) { throw new QException("Unable to load controller {$controllerPath}: {$e}"); } }
/** * Check if a field is orderable. * * @param string $field * * @return bool * * @throws QException */ public function isFieldOrderable($field) { $type = isset($this->fields[$field]['type']) ? $this->getFieldType($this->fields[$field]['type']) : false; if ($type && !$type->isOrderable($field, $this)) { $orderable = false; } else { $default = Config::get('models.FIELDS_ORDERABLE', true); $orderable = $this->checkFieldOptionCondition($field, 'orderable', $default); } return $this->dispatch('isFieldOrderable', $orderable, ['field' => $field]); }
/** * getActionSender. * * @param null $user */ protected function getActionSender($user = null) { $namespaceMail = '\\Quaver\\App\\Mail\\'; $classToLoad = $namespaceMail . $this->mailAction . 'Mail'; if (!class_exists($classToLoad)) { $classToLoad = $namespaceMail . 'BaseMail'; } $actionSender = new $classToLoad(); if ($user) { $actionSender->set('User', $user); $actionSender->set('Language', $user->language); } else { $actionSender->set('Language', Config::get('core.LANG')); } if (!empty($this->mailObjects)) { foreach ($this->mailObjects as $name => $value) { $classToLoad = '\\Quaver\\App\\Model\\' . $name; if (!is_object($value) && $value == intval($value) && class_exists($classToLoad)) { $object = new $classToLoad(); $object->getFromId($value); $actionSender->set($name, $object->id ? $object : $value); } else { $actionSender->set($name, $value); } } } return $actionSender; }
public function execute($args) { if (null === Config::get('app.THEME_PATH') || null === Config::get('app.THEME_BASE_PATH')) { exit($this->makeRed("Themes cannot be null (Check your Config.yml) \n")); } $resources = Config::get('app.RESOURCES', array()); $resources[Config::get('app.THEME_PATH') . '/Resources'] = Config::get('app.THEME_URL'); if (Config::get('app.THEME_PATH') !== Config::get('app.THEME_BASE_PATH')) { $resources[Config::get('app.THEME_BASE_PATH') . '/Resources'] = Config::get('app.THEME_BASE_URL'); } // Copy all resources foreach ($resources as $source => $destRelative) { $dest = GLOBAL_PATH . '/public' . $destRelative; echo "Copying "; $this->makeGreen('/public' . $destRelative); echo "\n"; if (!file_exists($dest)) { mkdir($dest, 0755, true); } $sourceIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST); foreach ($sourceIterator as $item) { $destPath = $dest . '/' . $sourceIterator->getSubPathName(); if ($item->isDir()) { if (!is_dir($destPath)) { if (file_exists($destPath)) { unlink($destPath); } mkdir($destPath, 0755); } } else { if (file_exists($destPath)) { if (is_dir($destPath)) { $filesIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($destPath, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST); foreach ($filesIterator as $file) { if ($file->isDir()) { rmdir($file->getRealPath()); } else { unlink($file->getRealPath()); } } rmdir($destPath); } else { unlink($destPath); } } copy($item, $destPath); } } } // Remove old resources echo "Removing old files \n"; foreach ($resources as $source => $destRelative) { $dest = GLOBAL_PATH . '/public' . $destRelative; $destIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dest, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST); foreach ($destIterator as $item) { $sourcePath = $source . '/' . $destIterator->getSubPathName(); if (!file_exists($sourcePath)) { if ($item->isDir()) { rmdir($item->getRealPath()); } else { unlink($item->getRealPath()); } } } } // Update randomVar if (Config::get('app.RANDOM_VAR')) { echo "Updating randomVar \n"; $config = Config::getInstance(); $elements = $config->importYML(); $elements['app']['RANDOM_VAR'] = date('Ymd'); if (file_exists(GLOBAL_PATH . '/App/Config.yml')) { rename(GLOBAL_PATH . '/App/Config.yml', GLOBAL_PATH . '/App/Config.yml.bak'); } $config->exportYML('Config.yml', $elements); } }
public function indexAction() { $this->addTwigVars('helloWorld', 'Mi first page class -> ' . Config::get('app.STABLE_VERSION')); $this->render(); }
/** * Run Quaver instance. */ public function run() { try { if ($this->router) { // Load default routes if not present if (!$this->router->routes) { $this->addPath('/', Config::get('app.THEME_PATH') . '/Routes.yml', Config::get('app.THEME_QUAVER')); } // Load dashboard routes if (file_exists(Config::get('app.THEME_BASE_PATH') . '/Routes.yml')) { $this->addPath('/', Config::get('app.THEME_BASE_PATH') . '/Routes.yml', Config::get('app.THEME_BASE_QUAVER')); } $route = $this->router->getCurrentRoute(); $this->fixTrailingSlash($route); } if (Config::get('slack.NOTIFY_ALL_ERRORS')) { // This allows to catch memory limit fatal errors. $this->tmpBuffer = str_repeat('x', 1024 * 500); register_shutdown_function(function () { $this->tmpBuffer = ''; Log::notifyLastError(); }); } if (php_sapi_name() !== 'cli') { // Load language $GLOBALS['_lang'] = new Lang(); if (isset($_GET['lang'])) { $lang_slug = substr($_GET['lang'], 0, 3); if (is_object($GLOBALS['_lang']->getFromSlug($lang_slug))) { $GLOBALS['_lang']->setCookie(); } else { $GLOBALS['_lang']->getSiteLanguage(); } } else { $GLOBALS['_lang']->getSiteLanguage(); } // Load user $GLOBALS['_user'] = new User(); $GLOBALS['_user']->sessionStart(); // Maintenance mode if (Config::get('core.MAINTENANCE_MODE') && !$GLOBALS['_user']->isAdmin()) { if ($this->router) { $this->router->dispatch('maintenance'); } } } if ($this->router) { $this->router->route($route); } } catch (QException $e) { if (Config::get('core.DEV_MODE', false)) { throw $e; } else { $message = 'Error no controlado ' . $e->getMessage() . ' ' . $_SERVER['REQUEST_URI']; Log::notifySlack(":bangbang: {$message}"); if (isset($GLOBALS['_lang']->id) && isset($GLOBALS['_lang']->strings)) { $this->router->dispatch('e500'); } else { header('HTTP/1.0 500 Internal Server Error'); trigger_error("[500] {$url}", E_ERROR); echo 'Algo no ha salido bien. Nuestro equipo técnico está trabajando para solucionar el problema tan pronto como sea posible.'; } } } }
/** * getMailTemplate. * * @param string $_template * @param string $layout */ public static function getMailTemplate($_template, $layout = 'template-mail') { $templateVars = array('body' => $_template); // load twig to render $path = Config::get('app.THEME_PATH') . '/View'; $templatesDir = array($path); $dirsToScan = array($path); $dirKey = 0; while (count($dirsToScan) > $dirKey) { $results = scandir($dirsToScan[$dirKey]); foreach ($results as $result) { if ($result === '.' || $result === '..') { continue; } if (is_dir($dirsToScan[$dirKey] . '/' . $result)) { $templatesDir[] = $dirsToScan[$dirKey] . '/' . $result; $dirsToScan[] = $dirsToScan[$dirKey] . '/' . $result; } } ++$dirKey; } $loader = new \Twig_Loader_Filesystem($templatesDir); $twig = new \Twig_Environment($loader); $template = $twig->loadTemplate($layout . '.twig'); $html = $template->render($templateVars); return $html; }