Esempio n. 1
0
 /**
  * Comrpueba y actualiza la versión de la aplicación.
  */
 private static function checkVersion()
 {
     if (substr(self::$_SUBURI, -9) != 'index.php' || Request::analyze('logout', 0) === 1) {
         return;
     }
     $update = false;
     $configVersion = (int) str_replace('.', '', Config::getValue('version'));
     $databaseVersion = (int) str_replace('.', '', ConfigDB::getValue('version'));
     $appVersion = (int) implode(Util::getVersion(true));
     if ($databaseVersion < $appVersion && Request::analyze('nodbupgrade', 0) === 0) {
         if (Upgrade::needDBUpgrade($databaseVersion)) {
             if (!self::checkMaintenanceMode(true)) {
                 if (Config::getValue('upgrade_key', 0) === 0) {
                     Config::setValue('upgrade_key', sha1(uniqid(mt_rand(), true)));
                     Config::setValue('maintenance', true);
                 }
                 self::initError(_('La aplicación necesita actualizarse'), sprintf(_('Si es un administrador pulse en el enlace: %s'), '<a href="index.php?upgrade=1&a=upgrade">' . _('Actualizar') . '</a>'));
             }
             $action = Request::analyze('a');
             $hash = Request::analyze('h');
             if ($action === 'upgrade' && $hash === Config::getValue('upgrade_key', 0)) {
                 if (Upgrade::doUpgrade($databaseVersion)) {
                     ConfigDB::setValue('version', $appVersion);
                     Config::setValue('maintenance', false);
                     Config::deleteParam('upgrade_key');
                     $update = true;
                 }
             } else {
                 $controller = new Controller\MainC();
                 $controller->getUpgrade();
                 $controller->view();
                 exit;
             }
         }
     }
     if ($configVersion < $appVersion && Upgrade::needConfigUpgrade($appVersion) && Upgrade::upgradeConfig($appVersion)) {
         Config::setValue('version', $appVersion);
         $update = true;
     }
     if ($update === true) {
         $log = new Log(_('Actualización'));
         $log->addDescription(_('Actualización de versión realizada.'));
         $log->addDescription(_('Versión') . ': ' . $appVersion);
         $log->writeLog();
         Email::sendEmail($log);
         self::$UPDATED = true;
     }
 }
 /**
  * Realizar la conexión con la BBDD.
  * Esta función utiliza PDO para conectar con la base de datos.
  *
  * @throws SPException
  * @return \PDO
  */
 public function getConnection()
 {
     if (!$this->_db) {
         $isInstalled = Config::getValue('installed');
         $dbhost = Config::getValue('dbhost');
         $dbuser = Config::getValue('dbuser');
         $dbpass = Config::getValue('dbpass');
         $dbname = Config::getValue('dbname');
         $dbport = Config::getValue('dbport', 3306);
         if (empty($dbhost) || empty($dbuser) || empty($dbpass) || empty($dbname)) {
             if ($isInstalled) {
                 Init::initError(_('No es posible conectar con la BD'), _('Compruebe los datos de conexión'));
             } else {
                 throw new SPException(SPException::SP_CRITICAL, _('No es posible conectar con la BD'), _('Compruebe los datos de conexión'));
             }
         }
         try {
             $dsn = 'mysql:host=' . $dbhost . ';port=' . $dbport . ';dbname=' . $dbname . ';charset=utf8';
             //                $this->db = new PDO($dsn, $dbuser, $dbpass, array(PDO::ATTR_PERSISTENT => true));
             $this->_db = new PDO($dsn, $dbuser, $dbpass);
         } catch (\Exception $e) {
             if ($isInstalled) {
                 if ($e->getCode() === 1049) {
                     Config::setValue('installed', '0');
                 }
                 Init::initError(_('No es posible conectar con la BD'), 'Error ' . $e->getCode() . ': ' . $e->getMessage());
             } else {
                 throw new SPException(SPException::SP_CRITICAL, $e->getMessage(), $e->getCode());
             }
         }
     }
     $this->_db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
     $this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     return $this->_db;
 }