szurubooru/src/core.php

182 lines
4.8 KiB
PHP
Raw Normal View History

2013-10-09 19:23:39 +02:00
<?php
2014-05-04 21:23:12 +02:00
$startTime = microtime(true);
//basic settings and preparation
2013-11-17 14:39:50 +01:00
define('DS', DIRECTORY_SEPARATOR);
$rootDir = __DIR__ . DS . '..' . DS;
2014-05-06 15:09:06 +02:00
chdir($rootDir);
date_default_timezone_set('UTC');
setlocale(LC_CTYPE, 'en_US.UTF-8');
ini_set('memory_limit', '128M');
2013-10-12 22:54:25 +02:00
//basic include calls, autoloader init
2014-04-12 16:22:30 +02:00
require_once $rootDir . 'lib' . DS . 'TextCaseConverter' . DS . 'TextCaseConverter.php';
2013-11-17 14:39:50 +01:00
require_once $rootDir . 'lib' . DS . 'php-markdown' . DS . 'Michelf' . DS . 'Markdown.php';
require_once $rootDir . 'lib' . DS . 'php-markdown' . DS . 'Michelf' . DS . 'MarkdownExtra.php';
2014-04-29 21:35:29 +02:00
require_once $rootDir . 'lib' . DS . 'chibi-core' . DS . 'include.php';
\Chibi\AutoLoader::registerFilesystem($rootDir . 'lib' . DS . 'chibi-sql');
\Chibi\AutoLoader::registerFilesystem(__DIR__);
2014-05-07 17:39:40 +02:00
require_once $rootDir . 'src' . DS . 'routes.php';
2014-05-15 09:39:48 +02:00
final class Core
2014-04-29 21:35:29 +02:00
{
2014-05-15 09:39:48 +02:00
private static $context;
private static $config;
2014-04-29 21:35:29 +02:00
2014-05-15 09:39:48 +02:00
static function getConfig()
{
return self::$config;
}
2014-05-04 21:23:12 +02:00
2014-05-15 09:39:48 +02:00
static function getContext()
{
return self::$context;
}
2014-05-04 21:23:12 +02:00
2014-05-15 09:39:48 +02:00
static function prepareConfig($testEnvironment)
2014-05-06 12:01:29 +02:00
{
2014-05-15 09:39:48 +02:00
//load config manually
global $rootDir;
$configPaths = [];
if (!$testEnvironment)
{
$configPaths []= $rootDir . DS . 'data' . DS . 'config.ini';
$configPaths []= $rootDir . DS . 'data' . DS . 'local.ini';
}
else
{
$configPaths []= $rootDir . DS . 'tests' . DS . 'config.ini';
}
self::$config = new \Chibi\Config();
foreach ($configPaths as $path)
if (file_exists($path))
self::$config->loadIni($path);
self::$config->rootDir = $rootDir;
2014-05-06 12:01:29 +02:00
}
2014-05-15 09:39:48 +02:00
static function prepareEnvironment($testEnvironment)
2014-05-06 12:01:29 +02:00
{
2014-05-15 09:39:48 +02:00
//prepare context
global $startTime;
self::$context = new StdClass;
self::$context->startTime = $startTime;
$config = self::getConfig();
TransferHelper::createDirectory($config->main->filesPath);
TransferHelper::createDirectory($config->main->thumbsPath);
//extension sanity checks
$requiredExtensions = ['pdo', 'pdo_' . $config->main->dbDriver, 'gd', 'openssl', 'fileinfo'];
foreach ($requiredExtensions as $ext)
if (!extension_loaded($ext))
die('PHP extension "' . $ext . '" must be enabled to continue.' . PHP_EOL);
if (\Chibi\Database::connected())
\Chibi\Database::disconnect();
if ($testEnvironment)
Auth::setCurrentUser(null);
Access::init();
Logger::init();
Mailer::init();
PropertyModel::init();
\Chibi\Database::connect(
$config->main->dbDriver,
TextHelper::absolutePath($config->main->dbLocation),
isset($config->main->dbUser) ? $config->main->dbUser : null,
isset($config->main->dbPass) ? $config->main->dbPass : null);
2014-05-06 12:01:29 +02:00
}
2014-05-15 09:39:48 +02:00
static function getDbVersion()
{
try
{
$dbVersion = PropertyModel::get(PropertyModel::DbVersion);
}
catch (Exception $e)
{
return [null, null];
}
if (strpos($dbVersion, '.') !== false)
{
list ($dbVersionMajor, $dbVersionMinor) = explode('.', $dbVersion);
}
elseif ($dbVersion)
{
$dbVersionMajor = $dbVersion;
$dbVersionMinor = null;
}
else
{
$dbVersionMajor = 0;
$dbVersionMinor = 0;
}
return [$dbVersionMajor, $dbVersionMinor];
}
static function upgradeDatabase()
{
$config = self::getConfig();
$upgradesPath = TextHelper::absolutePath($config->rootDir
. DS . 'src' . DS . 'Upgrades' . DS . $config->main->dbDriver);
$upgrades = glob($upgradesPath . DS . '*.sql');
natcasesort($upgrades);
foreach ($upgrades as $upgradePath)
{
preg_match('/(\d+)\.sql/', $upgradePath, $matches);
$upgradeVersionMajor = intval($matches[1]);
list ($dbVersionMajor, $dbVersionMinor) = self::getDbVersion();
if (($upgradeVersionMajor > $dbVersionMajor)
or ($upgradeVersionMajor == $dbVersionMajor and $dbVersionMinor !== null))
{
printf('%s: executing' . PHP_EOL, $upgradePath);
$upgradeSql = file_get_contents($upgradePath);
$upgradeSql = preg_replace('/^[ \t]+(.*);/m', '\0--', $upgradeSql);
$queries = preg_split('/;\s*[\r\n]+/s', $upgradeSql);
$queries = array_map('trim', $queries);
$queries = array_filter($queries);
$upgradeVersionMinor = 0;
foreach ($queries as $query)
{
$query = preg_replace('/\s*--(.*?)$/m', '', $query);
++ $upgradeVersionMinor;
if ($upgradeVersionMinor > $dbVersionMinor)
{
try
{
\Chibi\Database::execUnprepared(new \Chibi\Sql\RawStatement($query));
}
catch (Exception $e)
{
echo $e . PHP_EOL;
echo $query . PHP_EOL;
die;
}
PropertyModel::set(PropertyModel::DbVersion, $upgradeVersionMajor . '.' . $upgradeVersionMinor);
}
}
PropertyModel::set(PropertyModel::DbVersion, $upgradeVersionMajor);
}
else
{
printf('%s: no need to execute' . PHP_EOL, $upgradePath);
}
}
list ($dbVersionMajor, $dbVersionMinor) = self::getDbVersion();
printf('Database version: %d.%d' . PHP_EOL, $dbVersionMajor, $dbVersionMinor);
}
2014-05-06 12:01:29 +02:00
}
2014-05-04 21:23:12 +02:00
2014-05-15 09:39:48 +02:00
Core::prepareConfig(false);
Core::prepareEnvironment(false);