Advertisement
Guest User

Untitled

a guest
Apr 6th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. <?php
  2. /* Orignally written by Andrew Niemantsverdriet
  3. * email: andrewniemants@gmail.com
  4. * website: http://www.rimrockhosting.com
  5. *
  6. * This code is on Github: https://github.com/kaptk2/portal
  7. *
  8. * Copyright (c) 2015, Andrew Niemantsverdriet <andrewniemants@gmail.com>
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright notice, this
  15. * list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  24. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  27. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * The views and conclusions contained in the software and documentation are those
  32. * of the authors and should not be interpreted as representing official policies,
  33. * either expressed or implied, of the FreeBSD Project.
  34. */
  35.  
  36. define('__ROOT__', dirname((__FILE__)));
  37.  
  38. session_start();
  39.  
  40. /**********************************************************************/
  41. // Configuraion //
  42. /**********************************************************************/
  43. $supportEmail = 'support@example.com';
  44.  
  45. // UniFi Connection details
  46. $unifi = array(
  47. 'unifiServer' => "https://unifitest:8443",
  48. 'unifiUser' => "admin",
  49. 'unifiPass' => "Rammlied9@"
  50. );
  51.  
  52. // SQL Database Connection
  53. $dbType = 'sqlite';
  54. $dbName = __ROOT__.'/database/unifi.sqlite';
  55.  
  56. // Date settings
  57. date_default_timezone_set('America/Denver');
  58.  
  59. /**********************************************************************/
  60. // End configuration //
  61. /**********************************************************************/
  62. try {
  63. //open the database
  64. $db = new PDO($dbType.':'.$dbName);
  65. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  66. } catch(PDOException $e) {
  67. error_log('Database exception: '.$e->getMessage(), 1, $supportEmail);
  68. echo('Exception: '.$e->getMessage());
  69. }
  70. function generatePass() {
  71. // Generate random password
  72. srand(make_seed());
  73. $alfa = "23456789qwertyuiopasdfghjkzxcvbnmQWERTYUIPASDFGHJKLZXCVBNM";
  74. $password = "";
  75. for($i = 0; $i < 7; $i ++) {
  76. $password .= $alfa[rand(0, strlen($alfa)-1)];
  77. }
  78. return $password;
  79. }
  80. function make_seed() {
  81. // Generate a random seed for use in password generation
  82. list($usec, $sec) = explode(' ', microtime());
  83. return (float) $sec + ((float) $usec * 100000);
  84. }
  85. function addUser($guest) {
  86. // Add a user to the database
  87. global $db;
  88. $expires = time() + (1 * 24 * 60 * 60);
  89. $notes = 'zxc'; // adaugat de mine
  90. $pass = generatePass(); // adaugat de mine
  91.  
  92. $pwd = password_hash($pass, PASSWORD_DEFAULT); // adaugat de mine
  93. try {
  94. // Insert a new user database
  95. $sql = "INSERT INTO guests (username, password, expires, notes) VALUES (:username, :password, :expires, :notes)";
  96. $sth = $db->prepare($sql);
  97. $sth->bindParam(':username', $guest['username'], PDO::PARAM_STR);
  98. $sth->bindParam(':password', $pwd, PDO::PARAM_STR);
  99. $sth->bindParam(':expires', $expires, PDO::PARAM_STR);
  100. $sth->bindParam(':notes', $notes, PDO::PARAM_STR);
  101. $sth->execute();
  102. } catch(PDOException $e) {
  103. error_log('addUser exception: '.$guest['username'].' Message: '.$e->getMessage(), 0);
  104. return false;
  105. }
  106.  
  107.  
  108. $mail = new PHPMailer;
  109.  
  110. $mail->isSMTP();
  111. $mail->Host = 'cas.zxc.ro';
  112. $mail->SMTPAuth = true;
  113.  
  114. $mail->Username = 'zxc@zxc.ro';
  115. $mail->Password = 'zzxc';
  116. $mail->SMTPSecure = 'TLS';
  117. $mail->Port = 465;
  118.  
  119. $mail->setFrom('eduard.buta@altex.ro', 'Mailer');
  120. $mail->addAddress($guest['username']);
  121.  
  122. $mail->isHTML(true);
  123.  
  124. $mail->Subject = 'Autentificare retea wireless Altex Romania';
  125. $mail->Body = "Stimate client, va rugam sa utilizati parola : {$pass} ";
  126. $mail->AltBody = 'Stimate client, va rugam sa utilizati parola : ';
  127.  
  128. $mail->send();
  129.  
  130. // Everything went okay
  131. return true;
  132. }
  133. function removeUser($id) {
  134. global $db;
  135. try {
  136. $sql = 'DELETE FROM guests WHERE id=:id';
  137. $sth = $db->prepare($sql);
  138. $sth->bindParam(':id', $id, PDO::PARAM_STR);
  139. $sth->execute();
  140. } catch(PDOException $e) {
  141. error_log('removeUser exception: '.$id.' Message: '.$e->getMessage(), 0);
  142. return false;
  143. }
  144. // Everything went okay
  145. return true;
  146. }
  147. function authorizeSQL($username, $password) {
  148. global $db;
  149. try {
  150. // See if user is in the database
  151. $sth = $db->prepare('SELECT password, expires FROM guests WHERE username = :username');
  152. $sth->bindParam(':username', $username, PDO::PARAM_STR);
  153. $sth->execute();
  154. $result = $sth->fetch();
  155. $db = NULL; // Close the connection
  156. } catch(PDOException $e) {
  157. error_log('authorizeSQL exception: '.$e->getMessage(), 0);
  158. return false;
  159. }
  160. $t = time(); // Unix time stamp
  161. // See if the user has a valid password and remaining time
  162. if (password_verify($password, $result['password'])) {
  163. if ($result['expires'] > $t) {
  164. return true;
  165. }
  166. }
  167. // Always return false if something goes wrong
  168. return false;
  169. }
  170. function sendAuthorization($id, $minutes, $unifi) {
  171. // Start Curl for login
  172. $ch = curl_init();
  173. // Return output instead of displaying it
  174. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  175. // We are posting data
  176. curl_setopt($ch, CURLOPT_POST, TRUE);
  177. // Set up cookies
  178. $cookie_file = "/tmp/unifi_cookie";
  179. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  180. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  181. // Allow Self Signed Certs
  182. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  183. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  184. curl_setopt($ch, CURLOPT_SSLVERSION, 1);
  185. // Login to the UniFi controller
  186. curl_setopt($ch, CURLOPT_URL, $unifi['unifiServer']."/api/login");
  187. $data = json_encode(array("username" => $unifi['unifiUser'],"password" => $unifi['unifiPass']));
  188. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  189. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  190. curl_exec($ch);
  191. // Send user to authorize and the time allowed
  192. $data = json_encode(array(
  193. 'cmd'=>'authorize-guest',
  194. 'mac'=>$id,
  195. 'minutes'=>$minutes));
  196. // Make the API Call
  197. curl_setopt($ch, CURLOPT_URL, $unifi['unifiServer'].'/api/s/default/cmd/stamgr');
  198. curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='.$data);
  199. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  200. curl_exec ($ch);
  201.  
  202. // Logout of the connection
  203. curl_setopt($ch, CURLOPT_URL, $unifi['unifiServer']."/logout");
  204. curl_exec ($ch);
  205. curl_close ($ch);
  206. sleep(6); // Small sleep to allow controller time to authorize
  207. }
  208. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement