2015-03-08 04:19:06 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Main Class
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Flashii;
|
|
|
|
|
2015-03-21 14:12:51 +00:00
|
|
|
class Main {
|
2015-03-08 04:19:06 +00:00
|
|
|
|
2015-03-08 05:36:00 +00:00
|
|
|
public $_TPL;
|
|
|
|
public $_MD;
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
// Constructor
|
2015-03-21 14:12:51 +00:00
|
|
|
public static function init($config) {
|
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
// Stop the execution if the PHP Version is older than 5.4.0
|
|
|
|
if(version_compare(phpversion(), '5.4.0', '<'))
|
|
|
|
die('<h3>Upgrade your PHP Version to at least PHP 5.4!</h3>');
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 05:36:00 +00:00
|
|
|
// Start session
|
|
|
|
if(session_status() != PHP_SESSION_ACTIVE)
|
|
|
|
session_start();
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 05:36:00 +00:00
|
|
|
// Configuration Management and local configuration
|
|
|
|
Configuration::init($config);
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 05:36:00 +00:00
|
|
|
// Database
|
|
|
|
Database::init();
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 05:36:00 +00:00
|
|
|
// "Dynamic" Configuration
|
|
|
|
Configuration::initDB();
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 05:36:00 +00:00
|
|
|
// Templating engine
|
2015-03-21 14:12:51 +00:00
|
|
|
self::initTwig();
|
|
|
|
|
2015-03-08 05:36:00 +00:00
|
|
|
// Markdown Parser
|
2015-03-21 14:12:51 +00:00
|
|
|
self::initParsedown();
|
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
}
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 05:36:00 +00:00
|
|
|
// Alias for Configuration::getConfig(), only exists because I'm lazy
|
|
|
|
public static function getConfig($key) {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 05:36:00 +00:00
|
|
|
return Configuration::getConfig($key);
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
}
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
// Initialise Twig
|
2015-03-21 14:12:51 +00:00
|
|
|
private static function initTwig() {
|
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
// Initialise Twig Filesystem Loader
|
2015-03-21 14:12:51 +00:00
|
|
|
$twigLoader = new \Twig_Loader_Filesystem(Configuration::getLocalConfig('etc', 'templatesPath') .'/'. Configuration::getLocalConfig('etc', 'design'));
|
2015-03-08 04:19:06 +00:00
|
|
|
|
|
|
|
// And now actually initialise the templating engine
|
2015-03-21 14:12:51 +00:00
|
|
|
self::$_TPL = new \Twig_Environment($twigLoader, array(
|
|
|
|
|
|
|
|
// 'cache' => SATOKO_ROOT_DIRECTORY. self::getConfig('path', 'cache') // Set cache directory
|
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
));
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 05:49:21 +00:00
|
|
|
// Load String template loader
|
2015-03-21 14:12:51 +00:00
|
|
|
self::$_TPL->addExtension(new \Twig_Extension_StringLoader());
|
|
|
|
|
2015-03-08 05:36:00 +00:00
|
|
|
}
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 05:36:00 +00:00
|
|
|
// Initialise Parsedown
|
|
|
|
private function initParsedown() {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
|
|
|
self::$_MD = new \Parsedown();
|
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
}
|
2015-03-21 14:12:51 +00:00
|
|
|
|
|
|
|
// Verify ReCAPTCHA
|
|
|
|
public static function verifyCaptcha($response) {
|
|
|
|
|
|
|
|
// Attempt to get the response
|
|
|
|
$resp = @file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='. self::getConfig('recaptcha_private') .'&response='. $response);
|
|
|
|
|
|
|
|
// In the highly unlikely case that it failed to get anything forge a false
|
|
|
|
if(!$resp)
|
|
|
|
return array('success' => false, 'error-codes' => array('Could not connect to the ReCAPTCHA server.'));
|
|
|
|
|
|
|
|
// Decode the response JSON from the servers
|
|
|
|
$resp = json_decode($resp, true);
|
|
|
|
|
|
|
|
// Return shit
|
|
|
|
return $resp;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
// Error Handler
|
|
|
|
public static function ErrorHandler($errno, $errstr, $errfile, $errline) {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
// Set some variables to work with including A HUGE fallback hackjob for the templates folder
|
|
|
|
$errstr = str_replace(self::getConfig('etc', 'localPath'), '', $errstr);
|
|
|
|
$errfile = str_replace(self::getConfig('etc', 'localPath'), '', $errfile);
|
|
|
|
$templates = (self::getConfig('etc', 'templatesPath') !== null && !empty(self::getConfig('etc', 'templatesPath'))) ? self::getConfig('etc', 'templatesPath') : '/var/www/flashii.net/_sakuya/templates/';
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
switch ($errno) {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
case E_ERROR:
|
|
|
|
case E_USER_ERROR:
|
|
|
|
$error = '<b>FATAL ERROR</b>: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile;
|
2015-03-21 14:12:51 +00:00
|
|
|
break;
|
2015-03-08 04:19:06 +00:00
|
|
|
|
|
|
|
case E_WARNING:
|
|
|
|
case E_USER_WARNING:
|
|
|
|
$error = '<b>WARNING</b>: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile;
|
2015-03-21 14:12:51 +00:00
|
|
|
break;
|
2015-03-08 04:19:06 +00:00
|
|
|
|
|
|
|
case E_NOTICE:
|
|
|
|
case E_USER_NOTICE:
|
|
|
|
$error = '<b>NOTICE</b>: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile;
|
2015-03-21 14:12:51 +00:00
|
|
|
break;
|
2015-03-08 04:19:06 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
$error = '<b>Unknown error type</b> [' . $errno . ']: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile;
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
}
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
// Use file_get_contents instead of Twig in case the problem is related to twig
|
|
|
|
$errorPage = file_get_contents($templates. 'errorPage.tpl');
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
// str_replace {{ error }} on the error page with the error data
|
|
|
|
$error = str_replace('{{ error }}', $error, $errorPage);
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
// Truncate all previous outputs
|
|
|
|
ob_clean();
|
|
|
|
|
|
|
|
// Die and display error message
|
|
|
|
die($error);
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
}
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
// Legacy password hashing to be able to validate passwords from users on the old backend.
|
|
|
|
public static function legacyPasswordHash($data) {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
return hash('sha512', strrev(hash('sha512', $data)));
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:19:06 +00:00
|
|
|
}
|
2015-03-21 14:12:51 +00:00
|
|
|
|
|
|
|
}
|