Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Your Roleplay UCP.
- * Written by Adrian Rodriguez
- * Copyright liegt bei Adrian Rodriguez
- *
- * Recht zum editieren hat IPrototypeI, jedoch behalte ich mir vor dieses jederzeit zu ändern.
- *
- * Made in 2015
- */
- require_once 'app/class/Smarty/Smarty.class.php';
- class App extends Smarty {
- const CLASS_PATH = 'app/class/';
- const HTTP_PATH = 'app/http/';
- const TPL_PATH = 'app/templates/';
- const BASE_PATH = '';
- const SECURE_PATH = 'http://';
- public static $router;
- private static $app;
- private static $viewinstance;
- private static $db;
- private static $qbuilder;
- private static $request;
- private static $user;
- private static $ts3admin;
- public $match;
- private $active_controller;
- private static $sampserver;
- public function __construct() {
- parent::__construct();
- /*
- * Set Smarty Configs
- */
- $this->cache_lifetime = 300;
- $this->setTemplateDir('app/templates');
- $this->setCompileDir('app/templates_c');
- $this->setCacheDir('app/cache/Smarty');
- $this->caching = false;
- $this->debugging = false;
- /*
- * Load Class Autoloader
- */
- spl_autoload_register(array($this, 'loadClass'));
- /*
- * Init Routes
- */
- self::initRouter();
- self::$router->setBasePath(self::BASE_PATH);
- $this->loadRoutes();
- /**
- * Require Helper functions
- */
- require_once 'app/functions/helpers.php';
- /**
- * Initialize Some Classes
- */
- self::initDatabase();
- self::initQBuilder();
- self::initUser();
- self::initTS3Admin();
- self::initSAMP();
- }
- /**
- * Builds page
- *
- * @return boolean
- */
- public function buildPage() {
- if ($this->match === false) {
- header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
- return false;
- }
- if(Session::has('_success')) {
- $this->assign("_success", Session::get('_success'));
- }
- if(Session::has('_errors')) {
- $this->assign("_errors", Session::get('_errors'));
- }
- if (Cookie::exists('hash') && !Session::has('user')) {
- $hash = Cookie::get('hash');
- $hash_check["query"] = self::$qbuilder->flush()->select()->from("UCP_Spieler_Remember")->where("Token = :hash");
- $hash_check["result"] = self::$db->query($hash_check["query"], array(":hash" => $hash));
- if ($hash_check["result"]->count()) {
- self::$user = new User($hash_check["result"]->result(0)->SpielerID);
- self::$user->login();
- $userData = (array) self::$user->data();
- Session::flash("_success", "Willkommen zurück " . $userData["Vorname"] . " " . $userData["Nachname"] . "!");
- }
- }
- $target = explode('@', $this->match['target']);
- require_once self::HTTP_PATH . 'controllers/' . $target[0] . '.php';
- $this->active_controller = new $target[0]();
- if (method_exists($this->active_controller, $target[1]) == false) {
- throw new Exception('Die Angegebene Funktion existiert nicht: ' . $target[1]);
- }
- $this->assign('URL', self::SECURE_PATH . $_SERVER["SERVER_NAME"] . self::BASE_PATH . '/');
- self::initView();
- if(self::$user->data()->Gebannt != 0) {
- self::$user->logout();
- Session::flash('_errors', array("Dein Account wurde gesperrt. Bitte kontaktiere einen Administrator."));
- return redirect(App::SECURE_PATH . $_SERVER["SERVER_NAME"] . App::BASE_PATH . '/auth/login');
- }
- if(self::$user->isLoggedIn()) {
- $query = self::getQBuilder()->flush()->select()->from("UCP_Conversation_To_User")->where("userID = :id");
- $result = self::getDatabase()->query($query, array(":id" => self::$user->data()->id))->result();
- $new_messages = 0;
- foreach ($result as $con) {
- $query = self::getQBuilder()->flush()->select()->from("UCP_Conversation")->where("id = :id")->limit(1);
- $con_result = self::getDatabase()->query($query, array(":id" => $con->conversationID))->result(0);
- if($con->lastVisitTime < $con_result->lastPostTime) {
- $new_messages++;
- }
- }
- $this->assign("_new_messages", $new_messages);
- $this->getUser()->updateActivity();
- }
- if(!empty($this->match["params"])) {
- call_user_func_array(array($this->active_controller, $target[1]), $this->match["params"]);
- } else {
- call_user_func(array($this->active_controller, $target[1]));
- }
- return true;
- }
- /**
- * Loads Routes from routes.xml and maps them into the router
- *
- * @return boolean
- */
- private function loadRoutes() {
- $xml_result = (array) simplexml_load_file(self::HTTP_PATH . 'routes.xml');
- foreach ($xml_result["routes"] as $xml) {
- $xml = (array) $xml;
- $controller = explode('@', $xml["target"]);
- $controller_path = self::HTTP_PATH . 'controllers/' . $controller[0] . '.php';
- if (!file_exists($controller_path)) {
- throw new Exception('Fehler beim Routen des Templates: Controller nicht gefunden (' . $controller_path . ')');
- }
- self::$router->map($xml["method"], $xml["url"], $xml["target"]);
- }
- $this->match = self::$router->match();
- return true;
- }
- /**
- * Class Autloader
- *
- * @param string $class Class to be loaded
- * @return boolean true on loaded, false on not loaded
- */
- private function loadClass($class) {
- $file_path = self::CLASS_PATH . $class . '.class.php';
- if (file_exists($file_path)) {
- require_once $file_path;
- return true;
- }
- return false;
- }
- /**
- * Init App Class
- */
- public static function getApp() {
- if (self::$app instanceof App) {
- return self::$app;
- }
- throw new Exception("The App wasn't initialized or was loaded wrong.");
- }
- public static function initApp() {
- self::$app = new App();
- }
- /**
- * Init Router and routes from routes.php
- */
- private static function initRouter() {
- self::$router = new router();
- }
- public static function getRouter() {
- if (self::$router instanceof router) {
- return self::$router;
- } else {
- throw new Exception("The Router wasn't initialized or was loaded wrong.");
- }
- }
- /**
- * Inits View Class
- */
- public static function initView() {
- self::$viewinstance = new View();
- }
- public static function getView() {
- if (self::$viewinstance instanceof View) {
- return self::$viewinstance;
- } else {
- throw new Exception("The View wasn't initialized or was loaded wrong.");
- }
- }
- /**
- * Inits Database Class
- */
- public static function getDatabase() {
- if (self::$db instanceof Database) {
- return self::$db;
- } else {
- throw new Exception("The Database wasn't initialized or was loaded wrong.");
- }
- }
- public static function initDatabase() {
- try {
- self::$db = new Database();
- } catch (PDOException $e) {
- echo 'Fehler beim Verbinden mit der Datenbank: <br>' . $e->getMessage();
- exit();
- }
- }
- /**
- * Instantiate QueryBuilder
- */
- public static function getQBuilder() {
- if (self::$qbuilder instanceof QueryBuilder) {
- return self::$qbuilder;
- } else {
- throw new Exception("The QueryBuilder wasn't initialized or was loaded wrong.");
- }
- }
- public static function initQBuilder() {
- self::$qbuilder = new QueryBuilder();
- }
- /**
- * Instantiate QueryBuilder
- */
- public static function getRequest() {
- if (self::$request instanceof Request) {
- return self::$request;
- } else {
- throw new Exception("The Request wasn't initialized or was loaded wrong.");
- }
- }
- public static function initRequest() {
- self::$request = new Request();
- }
- /**
- * Instantiate User
- */
- public static function getUser() {
- if(self::$user instanceof User) {
- return self::$user;
- } else {
- throw new Exception("The User wasn't initialized or was loaded wrong.");
- }
- }
- public static function initUser() {
- self::$user = new User();
- }
- /**
- * Instantiate TS3Admin
- */
- public static function getTS3Admin() {
- if(self::$ts3admin instanceof ts3admin) {
- return self::$ts3admin;
- } else {
- throw new Exception("The TS3Admin wasn't initialized or was loaded wrong.");
- }
- }
- public static function initTS3Admin() {
- self::$ts3admin = new ts3admin('127.0.0.1', 10011);
- if(self::$ts3admin->getElement('success', self::$ts3admin->connect())) {
- if(self::$ts3admin->login('serveradmin', 'PW') === false) {
- throw new Exception('Could not access ServerQuery!');
- }
- self::$ts3admin->selectServer(9987);
- //self::$ts3admin->serverGroupAddClient(2, 5);
- } else {
- throw new Exception('Could not Connect to Teamspeak. Offline?');
- }
- }
- /**
- * Instantiate TS3Admin
- */
- public static function getSAMP() {
- if(self::$sampserver instanceof SampQuery) {
- return self::$sampserver;
- } else {
- throw new Exception("The SampQuery wasn't initialized or was loaded wrong.");
- }
- }
- public static function initSAMP() {
- self::$sampserver = new SampQuery('127.0.0.1', 7777);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement