Esempio n. 1
0
 /**
  * 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;
 }
Esempio n. 2
0
 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"));
     }
 }
Esempio n. 3
0
 /**
  * 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;
 }
Esempio n. 4
0
 /**
  * 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;
 }
Esempio n. 5
0
 /**
  * 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);
 }
Esempio n. 6
0
 /**
  * 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;
 }
Esempio n. 7
0
<?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();
}
Esempio n. 8
0
 /**
  * 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}");
     }
 }
Esempio n. 9
0
 /**
  * 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]);
 }
Esempio n. 10
0
 /**
  * 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;
 }
Esempio n. 11
0
 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);
     }
 }
Esempio n. 12
0
 public function execute($args)
 {
     echo "Insert database information: \n\n";
     for ($i = 0; $i < 6; $i++) {
         switch ($i) {
             case 0:
                 $command = $this->makeGreen("HOSTNAME (localhost): ", false);
                 break;
             case 1:
                 $command = $this->makeGreen("USERNAME (root): ", false);
                 break;
             case 2:
                 $command = $this->makeGreen("PASSWORD (root): ", false);
                 break;
             case 3:
                 $command = $this->makeGreen("DATABASE: ", false);
                 break;
             case 4:
                 $command = $this->makeGreen("PORT (3306): ", false);
                 break;
             case 5:
                 $command = $this->makeGreen("CIPHER_KEY (AAAAAAAA): ", false);
                 break;
         }
         $line = readline($command);
         readline_add_history($line);
     }
     // dump history
     $params = readline_list_history();
     $params[0] = empty($params[0]) ? 'localhost' : $params[0];
     $params[1] = empty($params[1]) ? 'root' : $params[1];
     $params[2] = empty($params[2]) ? 'root' : $params[2];
     $params[4] = empty($params[4]) ? '3306' : $params[4];
     $params[5] = empty($params[5]) ? 'AAAAAAAA' : $params[5];
     if (empty($params[3])) {
         $this->makeRed("\nDATABASE cannot be null\n");
         exit;
     }
     // Name of the file
     $filename = 'quaver-skeleton.sql';
     // MySQL host
     $mysql_host = $params[0];
     // MySQL username
     $mysql_username = $params[1];
     // MySQL password
     $mysql_password = $params[2];
     // Database name
     $mysql_database = $params[3];
     // Connect to MySQL server
     $mysqli = new \mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database);
     if (mysqli_connect_errno()) {
         die($this->makeRed("Error connecting to MySQL server: (" . mysqli_connect_errno() . ") \n", false));
     }
     // Read in entire file
     $sql = file_get_contents(GLOBAL_PATH . '/vendor/millolab/quaver-cli/Quaver/' . $filename) or die($this->makeRed("Unable to open file! \n", false));
     // Execute statement
     if ($mysqli->multi_query($sql)) {
         $this->makeGreen("Tables imported successfully \n");
     } else {
         die($this->makeRed("An error has been occurred: " . $mysqli->error . " \n", false));
     }
     // Disconnect MySQL
     $mysqli->close();
     // Change Config.yml and export it
     $config = Config::getInstance();
     $configYML = $config->importYML();
     if (!isset($configYML)) {
         $configYML = $config->importYML(GLOBAL_PATH . '/App/Config_example.yml');
     }
     if (isset($configYML['db'])) {
         $configYML['db']['HOSTNAME'] = $mysql_host;
         $configYML['db']['USERNAME'] = $mysql_username;
         $configYML['db']['PASSWORD'] = $mysql_password;
         $configYML['db']['DATABASE'] = $mysql_database;
         $configYML['db']['PORT'] = (int) $params[4];
         $configYML['db']['CIPHER_KEY'] = $params[5];
         $config->exportYML('Config.yml', $configYML);
     } else {
         die($this->makeRed("An error has been occurred with Config.yml \n", false));
     }
     // Folders
     $class = new Folders();
     $class->execute(null);
     // Resources
     $class = new Resources();
     $class->execute(null);
 }
Esempio n. 13
0
 public function indexAction()
 {
     $this->addTwigVars('helloWorld', 'Mi first page class -> ' . Config::get('app.STABLE_VERSION'));
     $this->render();
 }
Esempio n. 14
0
 /**
  * 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.';
             }
         }
     }
 }
Esempio n. 15
0
 /**
  * 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;
 }