This commit is contained in:
flash 2015-04-01 19:34:36 +02:00
parent 58011071c6
commit df27981146
2 changed files with 177 additions and 165 deletions

View file

@ -1,164 +1,174 @@
<?php <?php
/* /*
* Main Class * Main Class
*/ */
namespace Sakura; namespace Sakura;
class Main { class Main {
public static $_TPL; public static $_TPL;
public static $_MD; public static $_MD;
// Constructor // Constructor
public static function init($config) { public static function init($config) {
// Stop the execution if the PHP Version is older than 5.4.0 // Stop the execution if the PHP Version is older than 5.4.0
if(version_compare(phpversion(), '5.4.0', '<')) if(version_compare(phpversion(), '5.4.0', '<'))
die('<h3>Upgrade your PHP Version to at least PHP 5.4!</h3>'); die('<h3>Upgrade your PHP Version to at least PHP 5.4!</h3>');
// Configuration Management and local configuration // Configuration Management and local configuration
Configuration::init($config); Configuration::init($config);
// Database // Database
Database::init(); Database::init();
// "Dynamic" Configuration // "Dynamic" Configuration
Configuration::initDB(); Configuration::initDB();
// Create new session // Create new session
Session::init(); Session::init();
// Templating engine // Templating engine
self::initTwig(); self::initTwig();
// Markdown Parser // Markdown Parser
self::initParsedown(); self::initParsedown();
} }
// Initialise Twig // Initialise Twig
private static function initTwig() { private static function initTwig() {
// Initialise Twig Filesystem Loader // Initialise Twig Filesystem Loader
$twigLoader = new \Twig_Loader_Filesystem(Configuration::getLocalConfig('etc', 'templatesPath') .'/'. Configuration::getLocalConfig('etc', 'design')); $twigLoader = new \Twig_Loader_Filesystem(Configuration::getLocalConfig('etc', 'templatesPath') .'/'. Configuration::getLocalConfig('etc', 'design'));
// And now actually initialise the templating engine // And now actually initialise the templating engine
self::$_TPL = new \Twig_Environment($twigLoader, array( self::$_TPL = new \Twig_Environment($twigLoader, array(
// 'cache' => SATOKO_ROOT_DIRECTORY. self::getConfig('path', 'cache') // Set cache directory // 'cache' => SATOKO_ROOT_DIRECTORY. self::getConfig('path', 'cache') // Set cache directory
)); ));
// Load String template loader // Load String template loader
self::$_TPL->addExtension(new \Twig_Extension_StringLoader()); self::$_TPL->addExtension(new \Twig_Extension_StringLoader());
} }
// Initialise Parsedown // Initialise Parsedown
private static function initParsedown() { private static function initParsedown() {
self::$_MD = new \Parsedown(); self::$_MD = new \Parsedown();
} }
// Verify ReCAPTCHA // Verify ReCAPTCHA
public static function verifyCaptcha($response) { public static function verifyCaptcha($response) {
// Attempt to get the response // Attempt to get the response
$resp = @file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='. Configuration::getConfig('recaptcha_private') .'&response='. $response); $resp = @file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='. Configuration::getConfig('recaptcha_private') .'&response='. $response);
// In the highly unlikely case that it failed to get anything forge a false // In the highly unlikely case that it failed to get anything forge a false
if(!$resp) if(!$resp)
return array('success' => false, 'error-codes' => array('Could not connect to the ReCAPTCHA server.')); return array('success' => false, 'error-codes' => array('Could not connect to the ReCAPTCHA server.'));
// Decode the response JSON from the servers // Decode the response JSON from the servers
$resp = json_decode($resp, true); $resp = json_decode($resp, true);
// Return shit // Return shit
return $resp; return $resp;
} }
// Error Handler // Error Handler
public static function ErrorHandler($errno, $errstr, $errfile, $errline) { public static function ErrorHandler($errno, $errstr, $errfile, $errline) {
// Set some variables to work with including A HUGE fallback hackjob for the templates folder // Set some variables to work with including A HUGE fallback hackjob for the templates folder
$errstr = str_replace(Configuration::getLocalConfig('etc', 'localPath'), '', $errstr); $errstr = str_replace(Configuration::getLocalConfig('etc', 'localPath'), '', $errstr);
$errfile = str_replace(Configuration::getLocalConfig('etc', 'localPath'), '', $errfile); $errfile = str_replace(Configuration::getLocalConfig('etc', 'localPath'), '', $errfile);
$templates = (Configuration::getLocalConfig('etc', 'templatesPath') !== null && !empty(Configuration::getLocalConfig('etc', 'templatesPath'))) ? Configuration::getLocalConfig('etc', 'templatesPath') : '/var/www/flashii.net/_sakuya/templates/'; $templates = (Configuration::getLocalConfig('etc', 'templatesPath') !== null && !empty(Configuration::getLocalConfig('etc', 'templatesPath'))) ? Configuration::getLocalConfig('etc', 'templatesPath') : '/var/www/flashii.net/_sakuya/templates/';
switch ($errno) { switch ($errno) {
case E_ERROR: case E_ERROR:
case E_USER_ERROR: case E_USER_ERROR:
$error = '<b>FATAL ERROR</b>: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile; $error = '<b>FATAL ERROR</b>: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile;
break; break;
case E_WARNING: case E_WARNING:
case E_USER_WARNING: case E_USER_WARNING:
$error = '<b>WARNING</b>: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile; $error = '<b>WARNING</b>: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile;
break; break;
case E_NOTICE: case E_NOTICE:
case E_USER_NOTICE: case E_USER_NOTICE:
$error = '<b>NOTICE</b>: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile; $error = '<b>NOTICE</b>: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile;
break; break;
default: default:
$error = '<b>Unknown error type</b> [' . $errno . ']: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile; $error = '<b>Unknown error type</b> [' . $errno . ']: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile;
} }
// Use file_get_contents instead of Twig in case the problem is related to twig // Use file_get_contents instead of Twig in case the problem is related to twig
$errorPage = file_get_contents($templates. 'errorPage.tpl'); $errorPage = file_get_contents($templates. 'errorPage.tpl');
// str_replace {{ error }} on the error page with the error data // str_replace {{ error }} on the error page with the error data
$error = str_replace('{{ error }}', $error, $errorPage); $error = str_replace('{{ error }}', $error, $errorPage);
// Truncate all previous outputs // Truncate all previous outputs
ob_clean(); ob_clean();
// Die and display error message // Die and display error message
die($error); die($error);
} }
// Legacy password hashing to be able to validate passwords from users on the old backend. // Legacy password hashing to be able to validate passwords from users on the old backend.
public static function legacyPasswordHash($data) { public static function legacyPasswordHash($data) {
return hash('sha512', strrev(hash('sha512', $data))); return hash('sha512', strrev(hash('sha512', $data)));
} }
// Cleaning strings // Cleaning strings
public static function cleanString($string, $lower = false) { public static function cleanString($string, $lower = false) {
$string = htmlentities($string, ENT_QUOTES | ENT_IGNORE, Configuration::getConfig('charset')); $string = htmlentities($string, ENT_QUOTES | ENT_IGNORE, Configuration::getConfig('charset'));
$string = stripslashes($string); $string = stripslashes($string);
$string = strip_tags($string); $string = strip_tags($string);
if($lower) if($lower)
$string = strtolower($string); $string = strtolower($string);
return $string; return $string;
} }
// Getting news posts // Getting news posts
public static function getNewsPosts($limit = null) { public static function getNewsPosts($limit = null) {
// Get news posts // Get news posts
$newsPosts = Database::fetch('news', true, null, ['id', true], ($limit ? [$limit] : null)); $newsPosts = Database::fetch('news', true, null, ['id', true], ($limit ? [$limit] : null));
// Get user data // Get user data
foreach($newsPosts as $newsId => $newsPost) { foreach($newsPosts as $newsId => $newsPost) {
$newsPosts[$newsId]['udata'] = Users::getUser($newsPost['uid']); $newsPosts[$newsId]['udata'] = Users::getUser($newsPost['uid']);
$newsPosts[$newsId]['gdata'] = Users::getGroup($newsPosts[$newsId]['udata']['group_main']); $newsPosts[$newsId]['gdata'] = Users::getGroup($newsPosts[$newsId]['udata']['group_main']);
} }
// Return posts // Return posts
return $newsPosts; return $newsPosts;
} }
} // Loading info pages
public static function loadInfoPage($id) {
// Get contents from the database
$infopage = Database::fetch('infopages', false, ['shorthand' => [$id, '=']]);
print_r($infopage);
}
}

View file

@ -9,11 +9,13 @@ namespace Sakura;
// Include components // Include components
require_once '/var/www/flashii.net/_sakura/sakura.php'; require_once '/var/www/flashii.net/_sakura/sakura.php';
// Do parsing etc. // Set default variables
$renderData['page'] = [ $renderData['page'] = [
'title' => 'Info pages', 'title' => 'Info pages',
'content' => 'Unable to load the requested info page.' 'content' => 'Unable to load the requested info page.'
]; ];
$ipData = Main::loadInfoPage(isset($_GET['r']) ? strtolower($_GET['r']) : '');
// Print page contents // Print page contents
print Main::$_TPL->render('main/infopage.tpl', $renderData); print Main::$_TPL->render('main/infopage.tpl', $renderData);