Advertisement
Guest User

Controller_Silex

a guest
Nov 15th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.40 KB | None | 0 0
  1. <?php
  2.  
  3. /* Copyright (C) 2012 - Jasper van der Gronde
  4.  * --------------------------------------
  5.  * This program is free software: you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation, either version 3 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  * --------------------------------------
  18.  */
  19.  
  20. // Include all required files.
  21. require_once __DIR__.'/vendor/autoload.php';
  22. require_once __DIR__.'/const.php';
  23. require_once __DIR__.'/route.php';
  24. require_once __DIR__.'/../model/database.php';
  25.  
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpFoundation\Response;
  28. use Symfony\Component\HttpKernel\HttpKernelInterface;
  29.  
  30.  
  31. class Controller
  32. {
  33.     public static $appProvider = null;
  34.     public static $expireTime = 5400;
  35.     public static $timeStamp = null;
  36.     public static $routeArray = null;
  37.     public static $routeMatched = null;
  38.     public static $subRouteMatched = null;
  39.    
  40.     public static function getSilex()
  41.     {
  42.         if(self::$appProvider == null)
  43.         {
  44.             self::$appProvider = new Silex\Application();
  45.             // Twig Service Provider
  46.             self::$appProvider->register(
  47.                 new Silex\Provider\TwigServiceProvider(),
  48.                 array('twig.path' => __DIR__.'/../view/',
  49.                       'twig.options' => array('cache' => __DIR__.'/../cache'),));
  50.             self::$appProvider['debug'] = true;
  51.         }
  52.         return self::$appProvider;
  53.     }
  54.    
  55.     public static function invoke()
  56.     {      
  57.         Database::get();
  58.         $silexProvider = self::getSilex();
  59.         $routeArray = self::getRoutes();
  60.         $routeMatched = null;
  61.         $subRouteMatched = null;
  62.                
  63.         if(count($routeArray) > 0)
  64.         {
  65.             // Ok to go! Routes are made
  66.             $playIntro = false;
  67.             if(!isset($_COOKIE['User']['timeStamp']))
  68.                 $playIntro = true;
  69.             setcookie('User[timeStamp]', time(), time() + self::$expireTime, '/', $_SERVER['SERVER_NAME']);
  70.             self::$timeStamp = time() + self::$expireTime;
  71.            
  72.             foreach($routeArray as $routeString => $routeInfo)
  73.             {
  74.                 if($routeInfo['variable'])
  75.                 {
  76.                     $silexProvider->match($routeString, function($variable, Request $request)
  77.                     use(
  78.                         $routeInfo,
  79.                         $silexProvider,
  80.                         $playIntro
  81.                         // add more, if needed...
  82.                     )
  83.                     {
  84.                         $renderTwig = null;
  85.                         $actionDone = false;
  86.                         $routeMatched = $routeInfo['route'];
  87.                         if(sizeof($routeInfo['sub_routes']) > 0)
  88.                         {
  89.                             foreach($routeInfo['sub_routes'] as $subRoute)
  90.                             {
  91.                                 if(strpos($subRoute, $variable) == true)
  92.                                 {
  93.                                     $subRouteMatched = $subRoute;
  94.                                     break;
  95.                                 }      
  96.                             }
  97.                         }
  98.                        
  99.                         switch($routeInfo['route'])
  100.                         {
  101.                             // This switch logic contains routes with variable variables.
  102.                             case Route::VIDEO_LISTN:
  103.                             {
  104.                                 break;
  105.                             }
  106.                             case Route::VIDEO_SHOW:
  107.                             {
  108.                                 break;
  109.                             }
  110.                             case Route::MUSIC_LISTN:
  111.                             {
  112.                                 break;
  113.                             }
  114.                             case Route::MUSIC_ALBUM:
  115.                             {
  116.                                 break;
  117.                             }                              
  118.                             case Route::SHOWS_DETAIL:
  119.                             {
  120.                                 break;
  121.                             }
  122.                             case Route::ADMIN_ACTION:                              
  123.                             {
  124.                                  
  125.                                 if(count($routeInfo['sub_routes']) > 0)
  126.                                 {  
  127.                                     switch($variable)
  128.                                     {
  129.                                         case Route::ADMIN_ACTION_LOGIN:
  130.                                         {
  131.                                             //$authNum
  132.                                             //if(isset($_COOKIE['User']['username']))
  133.                                         }
  134.                                         case Route::ADMIN_ACTION_LOGOUT:
  135.                                         {
  136.                                             break 2;
  137.                                         }
  138.                                     }
  139.                                 }
  140.                                 break;
  141.                             }
  142.                             case Route::ADMIN_PANEL_PAGES:
  143.                             {
  144.                                 break;
  145.                             }                              
  146.                             case Route::ADMIN_PANEL_ACTION:
  147.                             {
  148.                                 break;
  149.                             }                              
  150.                             default:
  151.                             {
  152.                                 throw new Exception(PAGE_NOT_EXIST, 400);
  153.                                 break;
  154.                             }
  155.                         }
  156.                         if($renderTwig != null && !actionDone)
  157.                             return $renderTwig;
  158.                         else
  159.                             throw new Exception(PAGE_NOT_EXIST, 400);
  160.                
  161.                     })->before(function(Request $request) use ($silexProvider, $routeArray)  {
  162.                         $loginRequest = $request->get('login');
  163.                         // Function of before closure.
  164.                         $setCookiesLogin = (function($username, $password) {
  165.                             setcookie('User[username]', $username, time() + COOKIE_LOGIN_EXPIRE, '/', $_SERVER['SERVER_NAME']);
  166.                             setcookie('User[password]', $password, time() + COOKIE_LOGIN_EXPIRE, '/', $_SERVER['SERVER_NAME']);
  167.                         });
  168.                        
  169.                        
  170.                        
  171.                         if($loginRequest)
  172.                         {
  173.                             $username = $request->get('username');
  174.                             $password = $request->get('password');
  175.                             $remember = $request->get('remember');
  176.                            
  177.                             $errorNum = 0;
  178.                             $errorMsg = array(
  179.                                 'username' => '',
  180.                                 'password' => '',
  181.                             );
  182.                             if(!empty($username))
  183.                             {
  184.                                 $regex = "/[^a-zA-Z0-9]/";
  185.                                 if(preg_match($regex, $username) == 1)
  186.                                 {
  187.                                     //$username = sqlite_escape_string($username);
  188.                                     $errorMsg['username']  = ERR_NAME_C;
  189.                                     $errorNum++;
  190.                                 }
  191.                                 else if(!(strlen($username) > 6))
  192.                                 {
  193.                                     $errorMsg['username'] = ERR_NAME_L;
  194.                                     $errorNum++;
  195.                                 }
  196.                                 else
  197.                                 { } //setCookie('LoginError[username]', '')
  198.                                
  199.                             }
  200.                             else
  201.                             {
  202.                                 $errorMsg['username'] = ERR_NAME_E;
  203.                                 $errorNum++;
  204.                             }
  205.                            
  206.                             if(!empty($password))
  207.                             {
  208.                             } else {
  209.                                 $errorMsg['password'] = ERR_PASS_E;
  210.                                 $errorNum++; }
  211.                            
  212.                             if($errorNum == 0)
  213.                             {
  214.                                 Database::get();
  215.                                 if(Database::$database != null)
  216.                                 {
  217.                                     if($remember)
  218.                                     {
  219.                                         $loginResult = &Database::selectLogin($username, sha1(&$password));                                        
  220.                                         $setCookiesLogin($username, $password);
  221.                                         return new Response("Result of login:".$loginResult['result']."<br>".
  222.                                         ."Your username is: ". $_COOKIE['User']['username'] .", password: ". $_COOKIE['User']['password']);
  223.                                     }
  224.                                     else {
  225.                                         return new Response("No cookie made.");
  226.                                     }
  227.                                 }
  228.                             }
  229.                             else
  230.                             {
  231.                                 $array = array(
  232.                                     'page' => array(
  233.                                         'title' => $routeArray['/admin']['title'],
  234.                                         'login_username' => $username,
  235.                                         'login_password' => $password,
  236.                                         'errors' => array(
  237.                                             'username' => $errorMsg['username'],
  238.                                             'password' => $errorMsg['password'],
  239.                                         ),
  240.                                     )
  241.                                 );
  242.                                 $renderTwig = $silexProvider['twig']->render($routeArray['/admin']['template'], $array);
  243.                                 return new Response($renderTwig);
  244.                             }
  245.                         }
  246.                     });
  247.                 }
  248.                 else
  249.                 {
  250.                     $silexProvider->match($routeString, function(Request $request)
  251.                     use(
  252.                         $routeInfo,
  253.                         $silexProvider,
  254.                         $playIntro
  255.                         // add more, if needed...
  256.                     )
  257.                     {
  258.                         $renderTwig = null;
  259.                         switch($routeInfo['route'])
  260.                         {
  261.                             // See Route class for the constant definitions.
  262.                             // Views a list of posts.                      
  263.                             case Route::INDEX:
  264.                             {
  265.                                 $pageData = array('page' => array('title' => $routeInfo['title'], 'intro' => $playIntro));
  266.                                 $renderTwig = $silexProvider['twig']->render($routeInfo['template'], $pageData);
  267.                                 break;
  268.                             }  
  269.                             case Route::VIDEO:
  270.                             {
  271.                                 break;
  272.                             }  
  273.                             case Route::MUSIC:
  274.                             {
  275.                                 break;
  276.                             }                                  
  277.                             case Route::SHOWS:
  278.                             {
  279.                                 break;
  280.                             }                              
  281.                             case Route::CONTACT:
  282.                             {
  283.                                 break;
  284.                             }                                  
  285.                             case Route::SITEMAP:
  286.                             {
  287.                                 break;
  288.                             }                                  
  289.                             case Route::ADMIN:
  290.                             {
  291.                                 $array = array('page' => array('title' => $routeInfo['title']));
  292.                                 $array['page']['login_username'] = '';
  293.                                 $array['page']['login_password'] = '';
  294.                                 $array['page']['errors'] = array('username' => '', 'password' => '');
  295.                                 $authNum = 2;
  296.                                 if(isset($_COOKIE['User']['username']))
  297.                                 {
  298.                                     $array['page']['login_username'] = $_COOKIE['User']['username'];
  299.                                     $authNum--;
  300.                                 }
  301.                                 if(isset($_COOKIE['User']['password']))
  302.                                 {
  303.                                     $array['page']['login_password'] = $_COOKIE['User']['password'];
  304.                                     $authNum--;
  305.                                 }
  306.                                 if(!$authNum)
  307.                                 {
  308.                                     return new Response("Your username is: "
  309.                                         .$array['page']['login_username']
  310.                                         .", password: "
  311.                                         .$array['page']['login_password']);
  312.                                         break;
  313.                                 }
  314.                                 else
  315.                                     $renderTwig = $silexProvider['twig']->render($routeInfo['template'], $array);
  316.                                 break;
  317.                                
  318.                             }                                  
  319.                             case Route::ADMIN_PANEL:
  320.                             {
  321.                                 break;
  322.                             }  
  323.                             default:
  324.                             {                              
  325.                                 throw new Exception(PAGE_NOT_EXIST, 400);
  326.                                 break;
  327.                             }
  328.                         }
  329.                         if($renderTwig != null)
  330.                             return $renderTwig;
  331.                         else
  332.                             throw new Exception(PAGE_NOT_EXIST, 400);
  333.                     });
  334.                 }
  335.             }
  336.             self::getSilex()->run();
  337.         }
  338.         else
  339.         {
  340.         }
  341.     }
  342.    
  343.     public static function addRoutes()
  344.     {
  345.         self::addRoute(Route::INDEX, false, null, 'Welcome!', 'home.twig');
  346.         self::addRoute(Route::VIDEO, false, null, 'Videos', 'videos.twig');
  347.         self::addRoute(Route::VIDEO_LISTN, true, null, 'Videos', 'videos.twig');
  348.         self::addRoute(Route::VIDEO_SHOW, true, null, 'Video - ', 'video_play.twig');
  349.         self::addRoute(Route::MUSIC, false, null, 'Music', 'music.twig');
  350.         self::addRoute(Route::MUSIC_LISTN, true, null, 'Music', 'music.twig');
  351.         self::addRoute(Route::MUSIC_ALBUM, true, null, 'Album - ', 'music_play.twig');
  352.         self::addRoute(Route::SHOWS, false, null, 'Shows', 'shows.twig');
  353.         self::addRoute(Route::SHOWS_DETAIL, true, null, 'Show - ', 'show_detail.twig');
  354.         self::addRoute(Route::CONTACT, false, null, 'Contact', 'contact.twig');
  355.         self::addRoute(Route::SITEMAP, false, null,  'Sitemap', 'sitemap.twig');
  356.         self::addRoute(Route::ADMIN, false, null, 'Website Administration', 'admin.twig');
  357.         self::addRoute(Route::ADMIN_ACTION, true, array(Route::ADMIN_ACTION_LOGIN, Route::ADMIN_ACTION_LOGOUT), 'action', 'admin.twig');
  358.         self::addRoute(Route::ADMIN_PANEL, false, null, 'Administration Panel', 'admin_panel.twig');
  359.         self::addRoute(Route::ADMIN_PANEL_PAGES, true, null, 'Administration Panel', 'unused');
  360.         self::addRoute(Route::ADMIN_PANEL_ACTION, true, null, 'action', 'unused');
  361.     }
  362.    
  363.     public static function getRoutes()
  364.     {
  365.         if(isset(self::$routeArray)
  366.             && is_array(self::$routeArray)
  367.             && count(self::$routeArray) > 0)
  368.         {
  369.             return self::$routeArray;
  370.         }
  371.         else
  372.         {
  373.             self::addRoutes();
  374.             return self::$routeArray;
  375.         }
  376.     }
  377.    
  378.     public static function addRoute($routeString, $variableBoolean, $subRoutes, $pageTitle, $twigTemplate)
  379.     {
  380.         $routeObject = new Route($routeString, $variableBoolean, $subRoutes, $pageTitle, $twigTemplate);
  381.         self::$routeArray[$routeString] = array(
  382.             'route' => $routeObject->routeString,
  383.             'variable' => $routeObject->variableBoolean,
  384.             'sub_routes' => $routeObject->subRoutes,
  385.             'title' => $routeObject->pageTitle,
  386.             'template'  => $routeObject->twigTemplate
  387.         );
  388.         $routeObject = null;
  389.     }
  390. }*/
  391.  
  392. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement