Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.05 KB | None | 0 0
  1. <?php
  2.    
  3.     require 'snoken/drivers/driver.php';
  4.     require 'snoken/drivers/mysql.php';
  5.     require 'snoken/base.php';
  6.     require 'snoken/manager.php';
  7.     require 'Models.php';
  8.     require 'Session.php';
  9.  
  10.     Slim_Route::setDefaultConditions(array(
  11.  
  12.         // 'ad' refers to advertisement ID's in the URL.
  13.         // should always be numeric, and atleast 1 character.
  14.         'ad' => '[0-9]{1,}'
  15.        
  16.     ));
  17.  
  18.     // Database settings, herpaderp.
  19.     $databaseSettings = array(
  20.         'host' => 'localhost',
  21.         'dbname' => 'test',
  22.         'user' => 'root',
  23.         'pass' => ''
  24.     );
  25.  
  26.    
  27.     // Starts the Snoken driver and assigns it to the Manager:
  28.     Snoken\Manager::setDriver(
  29.             new Snoken\Drivers\MySQLDriver("host={$databaseSettings['host']};dbname={$databaseSettings['dbname']}",
  30.                                             $databaseSettings['user'], $databaseSettings['pass'])
  31.         );
  32.  
  33.     /*
  34.      * Look for a session cookie. If it's set, use it to
  35.      * start the active session, otherwise the default
  36.      * behavior is to start a new empty session.
  37.      */
  38.     Session::start(Slim::getEncryptedCookie('adpanelSession'));
  39.  
  40.     /*
  41.      * Landing page:
  42.      * - Check if the user is logged in. If not,
  43.      *   redirect to the login page.
  44.      */
  45.     Slim::get('/', function() {
  46.         if( !Session::param('logged_in') )
  47.             Slim::redirect('login');
  48.        
  49.         render('home', array('title' => 'Oh teehee!'));
  50.     });
  51.  
  52.     /*
  53.      * Log-in page for ADPanel.
  54.      *
  55.      * Flashes errors, if any. POST's to /login.
  56.      */
  57.     Slim::get('/login', function() {
  58.  
  59.         // Skip the login page if we're already logged in.
  60.         if( Session::param('logged_in') )
  61.             Slim::redirect('.', 302);
  62.        
  63.         render('login', array(
  64.  
  65.                 'title' => 'Please log-in to your account.',
  66.                 'csrf_token' => Session::csrf_token(),
  67.                 'error' => Session::flash('login_error')
  68.  
  69.             ));
  70.     });
  71.  
  72.     /*
  73.      * The actual login process takes place here.
  74.      * Redirects to / on success, or back to /login
  75.      * with an error component, if there's a reason to.
  76.      */
  77.     Slim::post('/login', function() {
  78.        
  79.         $login_error = function($message) {
  80.             Session::param('login_error', $message);
  81.             Slim::redirect('login', 302);
  82.         };
  83.  
  84.         $request = Slim::request();
  85.  
  86.         // Verify the csrf token:
  87.         if( !Session::csrf_token($request->post('csrf_token')) )
  88.             $login_error('An error has ocurred with your request, please try again.');
  89.  
  90.         // Return to the login page with an error:
  91.         if(!$user = $request->post('username') or !$pass = $request->post('password'))
  92.             $login_error('Please provide a username and a password.');
  93.            
  94.  
  95.         // Get the User related to this name:
  96.         $user = User::select(array('name' => strtolower($user)), null, 1)->one();
  97.         if( !$user )
  98.             $login_error('No user was found with that name.');
  99.  
  100.         // Hash the provided password and compare it:
  101.         $hashedPass = User::hash($pass, $user->salt);  
  102.         if( $hashedPass !== $user->pass )
  103.             $login_error('Invalid password provided for that user.');
  104.  
  105.         Session::param('logged_in', true);
  106.         Session::param('started', time());
  107.         Session::param('user', $user);
  108.  
  109.         Slim::redirect('.', 302);
  110.     });
  111.  
  112.     Slim::get('/logout', function() {
  113.         Session::clear();
  114.         Slim::redirect('.');
  115.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement