Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Copyright (C) 2012 - Jasper van der Gronde
- * --------------------------------------
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * --------------------------------------
- */
- // Include all required files.
- require_once __DIR__.'/vendor/autoload.php';
- require_once __DIR__.'/const.php';
- require_once __DIR__.'/route.php';
- require_once __DIR__.'/../model/database.php';
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpKernel\HttpKernelInterface;
- class Controller
- {
- public static $appProvider = null;
- public static $expireTime = 5400;
- public static $timeStamp = null;
- public static $routeArray = null;
- public static $routeMatched = null;
- public static $subRouteMatched = null;
- public static function getSilex()
- {
- if(self::$appProvider == null)
- {
- self::$appProvider = new Silex\Application();
- // Twig Service Provider
- self::$appProvider->register(
- new Silex\Provider\TwigServiceProvider(),
- array('twig.path' => __DIR__.'/../view/',
- 'twig.options' => array('cache' => __DIR__.'/../cache'),));
- self::$appProvider['debug'] = true;
- }
- return self::$appProvider;
- }
- public static function invoke()
- {
- Database::get();
- $silexProvider = self::getSilex();
- $routeArray = self::getRoutes();
- $routeMatched = null;
- $subRouteMatched = null;
- if(count($routeArray) > 0)
- {
- // Ok to go! Routes are made
- $playIntro = false;
- if(!isset($_COOKIE['User']['timeStamp']))
- $playIntro = true;
- setcookie('User[timeStamp]', time(), time() + self::$expireTime, '/', $_SERVER['SERVER_NAME']);
- self::$timeStamp = time() + self::$expireTime;
- foreach($routeArray as $routeString => $routeInfo)
- {
- if($routeInfo['variable'])
- {
- $silexProvider->match($routeString, function($variable, Request $request)
- use(
- $routeInfo,
- $silexProvider,
- $playIntro
- // add more, if needed...
- )
- {
- $renderTwig = null;
- $actionDone = false;
- $routeMatched = $routeInfo['route'];
- if(sizeof($routeInfo['sub_routes']) > 0)
- {
- foreach($routeInfo['sub_routes'] as $subRoute)
- {
- if(strpos($subRoute, $variable) == true)
- {
- $subRouteMatched = $subRoute;
- break;
- }
- }
- }
- switch($routeInfo['route'])
- {
- // This switch logic contains routes with variable variables.
- case Route::VIDEO_LISTN:
- {
- break;
- }
- case Route::VIDEO_SHOW:
- {
- break;
- }
- case Route::MUSIC_LISTN:
- {
- break;
- }
- case Route::MUSIC_ALBUM:
- {
- break;
- }
- case Route::SHOWS_DETAIL:
- {
- break;
- }
- case Route::ADMIN_ACTION:
- {
- if(count($routeInfo['sub_routes']) > 0)
- {
- switch($variable)
- {
- case Route::ADMIN_ACTION_LOGIN:
- {
- //$authNum
- //if(isset($_COOKIE['User']['username']))
- }
- case Route::ADMIN_ACTION_LOGOUT:
- {
- break 2;
- }
- }
- }
- break;
- }
- case Route::ADMIN_PANEL_PAGES:
- {
- break;
- }
- case Route::ADMIN_PANEL_ACTION:
- {
- break;
- }
- default:
- {
- throw new Exception(PAGE_NOT_EXIST, 400);
- break;
- }
- }
- if($renderTwig != null && !actionDone)
- return $renderTwig;
- else
- throw new Exception(PAGE_NOT_EXIST, 400);
- })->before(function(Request $request) use ($silexProvider, $routeArray) {
- $loginRequest = $request->get('login');
- // Function of before closure.
- $setCookiesLogin = (function($username, $password) {
- setcookie('User[username]', $username, time() + COOKIE_LOGIN_EXPIRE, '/', $_SERVER['SERVER_NAME']);
- setcookie('User[password]', $password, time() + COOKIE_LOGIN_EXPIRE, '/', $_SERVER['SERVER_NAME']);
- });
- if($loginRequest)
- {
- $username = $request->get('username');
- $password = $request->get('password');
- $remember = $request->get('remember');
- $errorNum = 0;
- $errorMsg = array(
- 'username' => '',
- 'password' => '',
- );
- if(!empty($username))
- {
- $regex = "/[^a-zA-Z0-9]/";
- if(preg_match($regex, $username) == 1)
- {
- //$username = sqlite_escape_string($username);
- $errorMsg['username'] = ERR_NAME_C;
- $errorNum++;
- }
- else if(!(strlen($username) > 6))
- {
- $errorMsg['username'] = ERR_NAME_L;
- $errorNum++;
- }
- else
- { } //setCookie('LoginError[username]', '')
- }
- else
- {
- $errorMsg['username'] = ERR_NAME_E;
- $errorNum++;
- }
- if(!empty($password))
- {
- } else {
- $errorMsg['password'] = ERR_PASS_E;
- $errorNum++; }
- if($errorNum == 0)
- {
- Database::get();
- if(Database::$database != null)
- {
- if($remember)
- {
- $loginResult = &Database::selectLogin($username, sha1(&$password));
- $setCookiesLogin($username, $password);
- return new Response("Result of login:".$loginResult['result']."<br>".
- ."Your username is: ". $_COOKIE['User']['username'] .", password: ". $_COOKIE['User']['password']);
- }
- else {
- return new Response("No cookie made.");
- }
- }
- }
- else
- {
- $array = array(
- 'page' => array(
- 'title' => $routeArray['/admin']['title'],
- 'login_username' => $username,
- 'login_password' => $password,
- 'errors' => array(
- 'username' => $errorMsg['username'],
- 'password' => $errorMsg['password'],
- ),
- )
- );
- $renderTwig = $silexProvider['twig']->render($routeArray['/admin']['template'], $array);
- return new Response($renderTwig);
- }
- }
- });
- }
- else
- {
- $silexProvider->match($routeString, function(Request $request)
- use(
- $routeInfo,
- $silexProvider,
- $playIntro
- // add more, if needed...
- )
- {
- $renderTwig = null;
- switch($routeInfo['route'])
- {
- // See Route class for the constant definitions.
- // Views a list of posts.
- case Route::INDEX:
- {
- $pageData = array('page' => array('title' => $routeInfo['title'], 'intro' => $playIntro));
- $renderTwig = $silexProvider['twig']->render($routeInfo['template'], $pageData);
- break;
- }
- case Route::VIDEO:
- {
- break;
- }
- case Route::MUSIC:
- {
- break;
- }
- case Route::SHOWS:
- {
- break;
- }
- case Route::CONTACT:
- {
- break;
- }
- case Route::SITEMAP:
- {
- break;
- }
- case Route::ADMIN:
- {
- $array = array('page' => array('title' => $routeInfo['title']));
- $array['page']['login_username'] = '';
- $array['page']['login_password'] = '';
- $array['page']['errors'] = array('username' => '', 'password' => '');
- $authNum = 2;
- if(isset($_COOKIE['User']['username']))
- {
- $array['page']['login_username'] = $_COOKIE['User']['username'];
- $authNum--;
- }
- if(isset($_COOKIE['User']['password']))
- {
- $array['page']['login_password'] = $_COOKIE['User']['password'];
- $authNum--;
- }
- if(!$authNum)
- {
- return new Response("Your username is: "
- .$array['page']['login_username']
- .", password: "
- .$array['page']['login_password']);
- break;
- }
- else
- $renderTwig = $silexProvider['twig']->render($routeInfo['template'], $array);
- break;
- }
- case Route::ADMIN_PANEL:
- {
- break;
- }
- default:
- {
- throw new Exception(PAGE_NOT_EXIST, 400);
- break;
- }
- }
- if($renderTwig != null)
- return $renderTwig;
- else
- throw new Exception(PAGE_NOT_EXIST, 400);
- });
- }
- }
- self::getSilex()->run();
- }
- else
- {
- }
- }
- public static function addRoutes()
- {
- self::addRoute(Route::INDEX, false, null, 'Welcome!', 'home.twig');
- self::addRoute(Route::VIDEO, false, null, 'Videos', 'videos.twig');
- self::addRoute(Route::VIDEO_LISTN, true, null, 'Videos', 'videos.twig');
- self::addRoute(Route::VIDEO_SHOW, true, null, 'Video - ', 'video_play.twig');
- self::addRoute(Route::MUSIC, false, null, 'Music', 'music.twig');
- self::addRoute(Route::MUSIC_LISTN, true, null, 'Music', 'music.twig');
- self::addRoute(Route::MUSIC_ALBUM, true, null, 'Album - ', 'music_play.twig');
- self::addRoute(Route::SHOWS, false, null, 'Shows', 'shows.twig');
- self::addRoute(Route::SHOWS_DETAIL, true, null, 'Show - ', 'show_detail.twig');
- self::addRoute(Route::CONTACT, false, null, 'Contact', 'contact.twig');
- self::addRoute(Route::SITEMAP, false, null, 'Sitemap', 'sitemap.twig');
- self::addRoute(Route::ADMIN, false, null, 'Website Administration', 'admin.twig');
- self::addRoute(Route::ADMIN_ACTION, true, array(Route::ADMIN_ACTION_LOGIN, Route::ADMIN_ACTION_LOGOUT), 'action', 'admin.twig');
- self::addRoute(Route::ADMIN_PANEL, false, null, 'Administration Panel', 'admin_panel.twig');
- self::addRoute(Route::ADMIN_PANEL_PAGES, true, null, 'Administration Panel', 'unused');
- self::addRoute(Route::ADMIN_PANEL_ACTION, true, null, 'action', 'unused');
- }
- public static function getRoutes()
- {
- if(isset(self::$routeArray)
- && is_array(self::$routeArray)
- && count(self::$routeArray) > 0)
- {
- return self::$routeArray;
- }
- else
- {
- self::addRoutes();
- return self::$routeArray;
- }
- }
- public static function addRoute($routeString, $variableBoolean, $subRoutes, $pageTitle, $twigTemplate)
- {
- $routeObject = new Route($routeString, $variableBoolean, $subRoutes, $pageTitle, $twigTemplate);
- self::$routeArray[$routeString] = array(
- 'route' => $routeObject->routeString,
- 'variable' => $routeObject->variableBoolean,
- 'sub_routes' => $routeObject->subRoutes,
- 'title' => $routeObject->pageTitle,
- 'template' => $routeObject->twigTemplate
- );
- $routeObject = null;
- }
- }*/
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement