Advertisement
Guest User

app.class.php

a guest
Jul 22nd, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.42 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Your Roleplay UCP.
  5.  * Written by Adrian Rodriguez
  6.  * Copyright liegt bei Adrian Rodriguez
  7.  *
  8.  * Recht zum editieren hat IPrototypeI, jedoch behalte ich mir vor dieses jederzeit zu ändern.
  9.  *
  10.  * Made in 2015
  11.  */
  12.  
  13.  
  14. require_once 'app/class/Smarty/Smarty.class.php';
  15.  
  16. class App extends Smarty {
  17.  
  18.     const CLASS_PATH = 'app/class/';
  19.     const HTTP_PATH = 'app/http/';
  20.     const TPL_PATH = 'app/templates/';
  21.     const BASE_PATH = '';
  22.     const SECURE_PATH = 'http://';
  23.  
  24.     public  static  $router;
  25.     private static  $app;
  26.     private static  $viewinstance;
  27.     private static  $db;
  28.     private static  $qbuilder;
  29.     private static  $request;
  30.     private static  $user;
  31.     private static  $ts3admin;
  32.     public          $match;
  33.     private         $active_controller;
  34.     private static  $sampserver;
  35.  
  36.     public function __construct() {
  37.         parent::__construct();
  38.  
  39.         /*
  40.          * Set Smarty Configs
  41.          */
  42.         $this->cache_lifetime = 300;
  43.         $this->setTemplateDir('app/templates');
  44.         $this->setCompileDir('app/templates_c');
  45.         $this->setCacheDir('app/cache/Smarty');
  46.  
  47.         $this->caching = false;
  48.         $this->debugging = false;
  49.  
  50.         /*
  51.          * Load Class Autoloader
  52.          */
  53.         spl_autoload_register(array($this, 'loadClass'));
  54.  
  55.         /*
  56.          * Init Routes
  57.          */
  58.         self::initRouter();
  59.         self::$router->setBasePath(self::BASE_PATH);
  60.         $this->loadRoutes();
  61.  
  62.         /**
  63.          * Require Helper functions
  64.          */
  65.         require_once 'app/functions/helpers.php';
  66.  
  67.         /**
  68.          * Initialize Some Classes
  69.          */
  70.         self::initDatabase();
  71.         self::initQBuilder();
  72.         self::initUser();
  73.         self::initTS3Admin();
  74.         self::initSAMP();
  75.     }
  76.  
  77.     /**
  78.      * Builds page
  79.      *
  80.      * @return boolean
  81.      */
  82.     public function buildPage() {
  83.         if ($this->match === false) {
  84.             header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
  85.             return false;
  86.         }
  87.        
  88.         if(Session::has('_success')) {
  89.             $this->assign("_success", Session::get('_success'));
  90.         }
  91.        
  92.         if(Session::has('_errors')) {
  93.             $this->assign("_errors", Session::get('_errors'));
  94.         }
  95.        
  96.         if (Cookie::exists('hash') && !Session::has('user')) {
  97.             $hash = Cookie::get('hash');
  98.             $hash_check["query"] = self::$qbuilder->flush()->select()->from("UCP_Spieler_Remember")->where("Token = :hash");
  99.             $hash_check["result"] = self::$db->query($hash_check["query"], array(":hash" => $hash));
  100.  
  101.             if ($hash_check["result"]->count()) {
  102.                 self::$user = new User($hash_check["result"]->result(0)->SpielerID);
  103.                 self::$user->login();
  104.  
  105.                 $userData = (array) self::$user->data();
  106.                 Session::flash("_success", "Willkommen zurück " . $userData["Vorname"] . " " . $userData["Nachname"] . "!");
  107.             }
  108.         }
  109.        
  110.         $target = explode('@', $this->match['target']);
  111.         require_once self::HTTP_PATH . 'controllers/' . $target[0] . '.php';
  112.         $this->active_controller = new $target[0]();
  113.        
  114.         if (method_exists($this->active_controller, $target[1]) == false) {
  115.             throw new Exception('Die Angegebene Funktion existiert nicht: ' . $target[1]);
  116.         }
  117.        
  118.         $this->assign('URL', self::SECURE_PATH . $_SERVER["SERVER_NAME"] . self::BASE_PATH . '/');
  119.         self::initView();
  120.        
  121.         if(self::$user->data()->Gebannt != 0) {
  122.             self::$user->logout();
  123.             Session::flash('_errors', array("Dein Account wurde gesperrt. Bitte kontaktiere einen Administrator."));
  124.             return redirect(App::SECURE_PATH . $_SERVER["SERVER_NAME"] . App::BASE_PATH . '/auth/login');
  125.         }
  126.        
  127.         if(self::$user->isLoggedIn()) {
  128.             $query = self::getQBuilder()->flush()->select()->from("UCP_Conversation_To_User")->where("userID = :id");
  129.             $result = self::getDatabase()->query($query, array(":id" => self::$user->data()->id))->result();
  130.            
  131.             $new_messages = 0;
  132.             foreach ($result as $con) {
  133.                 $query = self::getQBuilder()->flush()->select()->from("UCP_Conversation")->where("id = :id")->limit(1);
  134.                 $con_result = self::getDatabase()->query($query, array(":id" => $con->conversationID))->result(0);
  135.                
  136.                 if($con->lastVisitTime < $con_result->lastPostTime) {
  137.                     $new_messages++;
  138.                 }
  139.             }
  140.             $this->assign("_new_messages", $new_messages);
  141.             $this->getUser()->updateActivity();
  142.         }
  143.        
  144.         if(!empty($this->match["params"])) {
  145.             call_user_func_array(array($this->active_controller, $target[1]), $this->match["params"]);
  146.         } else {
  147.             call_user_func(array($this->active_controller, $target[1]));
  148.         }
  149.        
  150.         return true;
  151.     }
  152.  
  153.     /**
  154.      * Loads Routes from routes.xml and maps them into the router
  155.      *
  156.      * @return boolean
  157.      */
  158.     private function loadRoutes() {
  159.         $xml_result = (array) simplexml_load_file(self::HTTP_PATH . 'routes.xml');
  160.         foreach ($xml_result["routes"] as $xml) {
  161.             $xml = (array) $xml;
  162.             $controller = explode('@', $xml["target"]);
  163.            
  164.             $controller_path = self::HTTP_PATH . 'controllers/' . $controller[0] . '.php';
  165.  
  166.             if (!file_exists($controller_path)) {
  167.                 throw new Exception('Fehler beim Routen des Templates: Controller nicht gefunden (' . $controller_path . ')');
  168.             }
  169.  
  170.             self::$router->map($xml["method"], $xml["url"], $xml["target"]);
  171.         }
  172.        
  173.         $this->match = self::$router->match();
  174.        
  175.         return true;
  176.     }
  177.  
  178.     /**
  179.      * Class Autloader
  180.      *
  181.      * @param string $class Class to be loaded
  182.      * @return boolean true on loaded, false on not loaded
  183.      */
  184.     private function loadClass($class) {
  185.         $file_path = self::CLASS_PATH . $class . '.class.php';
  186.         if (file_exists($file_path)) {
  187.             require_once $file_path;
  188.             return true;
  189.         }
  190.         return false;
  191.     }
  192.  
  193.     /**
  194.      * Init App Class
  195.      */
  196.     public static function getApp() {
  197.         if (self::$app instanceof App) {
  198.             return self::$app;
  199.         }
  200.         throw new Exception("The App wasn't initialized or was loaded wrong.");
  201.     }
  202.  
  203.     public static function initApp() {
  204.         self::$app = new App();
  205.     }
  206.  
  207.     /**
  208.      * Init Router and routes from routes.php
  209.      */
  210.     private static function initRouter() {
  211.         self::$router = new router();
  212.     }
  213.  
  214.     public static function getRouter() {
  215.         if (self::$router instanceof router) {
  216.             return self::$router;
  217.         } else {
  218.             throw new Exception("The Router wasn't initialized or was loaded wrong.");
  219.         }
  220.     }
  221.  
  222.     /**
  223.      * Inits View Class
  224.      */
  225.     public static function initView() {
  226.         self::$viewinstance = new View();
  227.     }
  228.  
  229.     public static function getView() {
  230.         if (self::$viewinstance instanceof View) {
  231.             return self::$viewinstance;
  232.         } else {
  233.             throw new Exception("The View wasn't initialized or was loaded wrong.");
  234.         }
  235.     }
  236.  
  237.     /**
  238.      * Inits Database Class
  239.      */
  240.     public static function getDatabase() {
  241.         if (self::$db instanceof Database) {
  242.             return self::$db;
  243.         } else {
  244.             throw new Exception("The Database wasn't initialized or was loaded wrong.");
  245.         }
  246.     }
  247.  
  248.     public static function initDatabase() {
  249.         try {
  250.             self::$db = new Database();
  251.         } catch (PDOException $e) {
  252.             echo 'Fehler beim Verbinden mit der Datenbank: <br>' . $e->getMessage();
  253.             exit();
  254.         }
  255.     }
  256.  
  257.     /**
  258.      * Instantiate QueryBuilder
  259.      */
  260.     public static function getQBuilder() {
  261.         if (self::$qbuilder instanceof QueryBuilder) {
  262.             return self::$qbuilder;
  263.         } else {
  264.             throw new Exception("The QueryBuilder wasn't initialized or was loaded wrong.");
  265.         }
  266.     }
  267.  
  268.     public static function initQBuilder() {
  269.         self::$qbuilder = new QueryBuilder();
  270.     }
  271.    
  272.     /**
  273.      * Instantiate QueryBuilder
  274.      */
  275.     public static function getRequest() {
  276.         if (self::$request instanceof Request) {
  277.             return self::$request;
  278.         } else {
  279.             throw new Exception("The Request wasn't initialized or was loaded wrong.");
  280.         }
  281.     }
  282.  
  283.     public static function initRequest() {
  284.         self::$request = new Request();
  285.     }
  286.    
  287.     /**
  288.      * Instantiate User
  289.      */
  290.     public static function getUser() {
  291.         if(self::$user instanceof User) {
  292.             return self::$user;
  293.         } else {
  294.             throw new Exception("The User wasn't initialized or was loaded wrong.");
  295.         }
  296.     }
  297.    
  298.     public static function initUser() {
  299.         self::$user = new User();
  300.     }
  301.    
  302.     /**
  303.      * Instantiate TS3Admin
  304.      */
  305.     public static function getTS3Admin() {
  306.         if(self::$ts3admin instanceof ts3admin) {
  307.             return self::$ts3admin;
  308.         } else {
  309.             throw new Exception("The TS3Admin wasn't initialized or was loaded wrong.");
  310.         }
  311.     }
  312.    
  313.     public static function initTS3Admin() {
  314.         self::$ts3admin = new ts3admin('127.0.0.1', 10011);
  315.         if(self::$ts3admin->getElement('success', self::$ts3admin->connect())) {
  316.             if(self::$ts3admin->login('serveradmin', 'PW') === false) {
  317.                 throw new Exception('Could not access ServerQuery!');
  318.             }
  319.             self::$ts3admin->selectServer(9987);
  320.            
  321.             //self::$ts3admin->serverGroupAddClient(2, 5);
  322.         } else {
  323.             throw new Exception('Could not Connect to Teamspeak. Offline?');
  324.         }
  325.     }
  326.    
  327.    
  328.     /**
  329.      * Instantiate TS3Admin
  330.      */
  331.     public static function getSAMP() {
  332.         if(self::$sampserver instanceof SampQuery) {
  333.             return self::$sampserver;
  334.         } else {
  335.             throw new Exception("The SampQuery wasn't initialized or was loaded wrong.");
  336.         }
  337.     }
  338.    
  339.     public static function initSAMP() {
  340.         self::$sampserver = new SampQuery('127.0.0.1', 7777);
  341.     }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement