Advertisement
Guest User

Untitled

a guest
Oct 18th, 2013
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.62 KB | None | 0 0
  1. <?php
  2. /* Orignally written by Andrew Niemantsverdriet
  3.  * website: http://www.rimrockhosting.com
  4.  *
  5.  * This code is on github: https://github.com/kaptk2/portal
  6.  *
  7.  * Copyright (c) 2012, Andrew Niemantsverdriet
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions are met:
  12.  *
  13.  * 1. Redistributions of source code must retain the above copyright notice, this
  14.  *    list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright notice,
  16.  *    this list of conditions and the following disclaimer in the documentation
  17.  *    and/or other materials provided with the distribution.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22.  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  23.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * The views and conclusions contained in the software and documentation are those
  31.  * of the authors and should not be interpreted as representing official policies,
  32.  * either expressed or implied, of the FreeBSD Project.
  33.  */
  34.  
  35. // Start the session to get access to the saved variables
  36. session_start();
  37.  
  38. // Get the config file
  39. require_once("config.php");
  40.  
  41. function authorizeMySQL($username, $password)
  42. {
  43.   // md5 the password
  44.   $password = md5($password);
  45.  
  46.   // Checks to see if user is in the MySQL database
  47.   global $dbServer;
  48.   global $dbName;
  49.   global $dbUser;
  50.   global $dbPassword;
  51.  
  52.   mysql_connect($dbServer, $dbUser, $dbPassword) or die(mysql_error());
  53.   mysql_select_db($dbName) or die(mysql_error());
  54.  
  55.   // Check to see if the username and password exist in the table
  56.   $result = mysql_query('SELECT expires FROM guests WHERE
  57.            username="'.$username.'" AND password="'.$password.'"');
  58.  
  59.   $count = mysql_num_rows($result);
  60.   $row = mysql_fetch_row($result);
  61.   $t = time(); // Unix time stamp
  62.  
  63.   if ($count == 1 && ($row[0] > $t))
  64.   {
  65.     // Exactly one row should be returned AND the user must
  66.     // not be expired. $row[0] is the when the account expires.
  67.     return true;
  68.   }
  69.   // Query did not return true so it must be false
  70.   return false;
  71. }
  72.  
  73. function sendAuthorization($id, $minutes)
  74. {
  75.   global $unifiServer;
  76.   global $unifiUser;
  77.   global $unifiPass;
  78.  
  79.   // Start Curl for login
  80.   $ch = curl_init();
  81.   // We are posting data
  82.   curl_setopt($ch, CURLOPT_POST, TRUE);
  83.   // Set up cookies
  84.   $cookie_file = "/tmp/unifi_cookie";
  85.   curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  86.   curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  87.   // Allow Self Signed Certs
  88.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  89.   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  90.   // Force SSL3 only
  91.   curl_setopt($ch, CURLOPT_SSLVERSION, 3);
  92.   // Login to the UniFi controller
  93.   curl_setopt($ch, CURLOPT_URL, "$unifiServer/login");
  94.   curl_setopt($ch, CURLOPT_POSTFIELDS,
  95.             "login=login&username=$unifiUser&password=$unifiPass");
  96.   curl_exec ($ch);
  97.   curl_close ($ch);
  98.  
  99.   // Send user to authorize and the time allowed
  100.   $data = json_encode(array(
  101.           'cmd'=>'authorize-guest',
  102.           'mac'=>$id,
  103.           'minutes'=>$minutes));
  104.  
  105.   $ch = curl_init();
  106.   // We are posting data
  107.   curl_setopt($ch, CURLOPT_POST, TRUE);
  108.   // Set up cookies
  109.   $cookie_file = "/tmp/unifi_cookie";
  110.   curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  111.   curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  112.   // Allow Self Signed Certs
  113.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  114.   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  115.   // Force SSL3 only
  116.   curl_setopt($ch, CURLOPT_SSLVERSION, 3);
  117.   // Make the API Call
  118.   curl_setopt($ch, CURLOPT_URL, $unifiServer.'/api/cmd/stamgr');
  119.   curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='.$data);
  120.   curl_exec ($ch);
  121.   curl_close ($ch);
  122.  
  123.   // Logout of the connection
  124.   $ch = curl_init();
  125.   // We are posting data
  126.   curl_setopt($ch, CURLOPT_POST, TRUE);
  127.   // Set up cookies
  128.   $cookie_file = "/tmp/unifi_cookie";
  129.   curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  130.   curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  131.   // Allow Self Signed Certs
  132.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  133.   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  134.   // Force SSL3 only
  135.   curl_setopt($ch, CURLOPT_SSLVERSION, 3);
  136.   // Make the API Call
  137.   curl_setopt($ch, CURLOPT_URL, $unifiServer.'/logout');
  138.   curl_exec ($ch);
  139.   curl_close ($ch);
  140.   //header("Location: success.php");
  141.   sleep(8); // Small sleep to allow controller time to authorize
  142.   header('Location: '.$_SESSION['url']);
  143. }
  144.  
  145. if ($_POST) // Check to see if the form has been posted to
  146. {
  147.   // Set and sanitze the posted variables
  148.   $user = preg_replace("/[^a-zA-Z0-9.]/", "", $_POST['username']);
  149.   $pass = $_POST['password'];
  150.  
  151.   if (authorizeMySQL($user, $pass))
  152.   {
  153.     // See if the user exists in mySQL
  154.     sendAuthorization($_SESSION['id'], '480');
  155.   }
  156. }
  157. echo "A valid username or password was not found."
  158. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement