Set up Open Access API

This commit is contained in:
hsl-access 2011-12-22 22:22:07 -07:00
parent 60c2d681e2
commit ca711a7f1e
7 changed files with 274 additions and 2 deletions

18
access.php Normal file
View File

@ -0,0 +1,18 @@
<?php
if(isset($_GET['id']))
$id = $_GET['id'];
if(isset($_GET['device']))
$device = $_GET['device'];
$accessdb = file_get_contents('../access-db.txt');
$access = explode("\n", $accessdb);
foreach($access as $line) {
$user = explode(" ", $line);
if(isset($user[2]) && strtoupper($user[2]) == strtoupper($id)) {
echo "^".$user[3]."|OK$";
}
}

View File

@ -29,7 +29,6 @@ puts "Content-type: text/html \r\n\r\n"
if users[cgi['user']]['pass'].to_s == (Digest::SHA2.new(bitlen=512) << cgi['pass']).to_s then if users[cgi['user']]['pass'].to_s == (Digest::SHA2.new(bitlen=512) << cgi['pass']).to_s then
2.times do #do the serial stuff twice as sometimes the serial port is occupied
serial = SerialPort.new("/dev/ttyUSB0", 57600, 8, 1, SerialPort::NONE) serial = SerialPort.new("/dev/ttyUSB0", 57600, 8, 1, SerialPort::NONE)
serial.print "e 1234\r" serial.print "e 1234\r"
@ -47,6 +46,22 @@ if users[cgi['user']]['pass'].to_s == (Digest::SHA2.new(bitlen=512) << cgi['pass
when "lock" when "lock"
puts "Doors locked." puts "Doors locked."
serial.print "l\r" serial.print "l\r"
when "status"
serial.print "9\r"
sleep 1
continue = 1
while continue == 1 do
serial.read_timeout = -1
lines = serial.readlines
if lines.length > 0
for l in lines
puts l
puts "\n"
end
else
continue = 0
end
end
when "arm" when "arm"
if(users[cgi['user']]['admin'] == true) then if(users[cgi['user']]['admin'] == true) then
puts "Armed." puts "Armed."
@ -68,7 +83,6 @@ if users[cgi['user']]['pass'].to_s == (Digest::SHA2.new(bitlen=512) << cgi['pass
serial.close serial.close
puts ' <a href="/~access">Return.</a>' puts ' <a href="/~access">Return.</a>'
end
else else
puts "Invalid username or password." puts "Invalid username or password."

15
cgi-bin/spaceapi.conf Normal file
View File

@ -0,0 +1,15 @@
{
"api" : "0.11",
"space" : "HeatSync Labs",
"logo" : "http://farm5.static.flickr.com/4053/4411484429_029466651b_o.gif",
"url" : "http://heatsynclabs.org",
"address" : "140 w. Main Street, Mesa, AZ 85281",
"contact" : {
"phone" : "",
"irc" : "irc://irc.freenode.net/#heatsynclabs",
"twitter" : "@heatsynclabs",
"ml" : "http://groups.google.com/group/heatsynclabs"
},
"cam" : "http://live.heatsynclabs.org"
}

68
cgi-bin/spaceapi.rb Executable file
View File

@ -0,0 +1,68 @@
#!/usr/bin/env ruby
###############################################################################
#
# 23b interface to SpaceAPI (https://hackerspaces.nl/spaceapi/)
#
# 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/>.
#
# Copyright 2011 Ryan Rix <ry@n.rix.si>
#
###############################################################################
#config = []
#config['space_name'] = "HeatSync Labs"
require 'rubygems'
require 'cgi'
require 'serialport'
require 'json'
#cgi = CGI.new
json = JSON.parse(File.read("spaceapi.conf"))
puts "Content-type: text/json \r\n\r\n"
# Basically, this is nicked from access.rb
# start by getting the current status of the lock system
serial = SerialPort.new("/dev/ttyUSB0", 57600, 8, 1, SerialPort::NONE)
serial.print "e 1234\r"
# query for status
serial.print "9\r"
sleep 1
serial.read_timeout = 1000
lines = serial.readlines
# ugly as shit
caps = []
for line in lines
if m = /\(\d=(\w*)\)/.match(line) then
caps << m.captures
end
end
# more ugly. Space is open when the doors are open or unlocked
hs_open = false # because when is anyone open these days?
if caps[2] == "open" then hs_open = true end
if caps[3] == "open" then hs_open = true end
if caps[4] == "unlocked" then hs_open = true end
if caps[5] == "unlocked" then hs_open = true end
#take all those nice unformatted garbages from 23b and put'm in a json
json["open"] = hs_open
puts JSON.generate json

89
cgi-bin/usermanagement.rb Normal file
View File

@ -0,0 +1,89 @@
#!/usr/bin/env ruby
# Copyright 2011 Will Bradley <bradley.will@gmail.com>
#
# Released under the Chicken Dance License, as detailed
# http://supertunaman.com/cdl/cdl_v0-1.txt
#
# Ruby interface to 23b's Open Access Control system
# By Will Bradley, twitter.com/willbradley of HeatSync Labs
#
# Valid access control commands:
#(d)ate, (s)show user, (m)odify user <num> <usermask> <tagnumber>
#(a)ll user dump,(r)emove_user <num>,(o)open door <num>
#(u)nlock all doors,(l)lock all doors
#(1)disarm_alarm, (2)arm_alarm,(3)train_alarm (9)show_status
#(e)nable <password> - enable or disable priveleged mode
require 'rubygems'
require 'cgi'
require 'serialport'
require 'json'
require 'digest/sha2'
cgi = CGI.new
userfile = File.read('../../users.json')
users = JSON.parse(userfile)
puts "Content-type: text/html \r\n\r\n"
if users[cgi['adminuser']]['pass'].to_s == (Digest::SHA2.new(bitlen=512) << cgi['adminpass']).to_s then
serial = SerialPort.new("/dev/ttyUSB0", 57600, 8, 1, SerialPort::NONE)
serial.print "e 1234\r"
if params['submit'] == "Add User" then
newuser = cgi['newuser']
newpass = cgi['newpass']
else if params['submit'] == "Delete User" then
end
/*
when "open-front"
puts "Front door opened."
serial.print "o 1\r"
when "open-rear"
puts "Rear door opened."
serial.print "o 2\r"
when "unlock"
if(users[cgi['user']]['admin'] == true) then
puts "Doors unlocked, remember to re-lock them."
serial.print "u\r"
else
puts "Fail. Don't be a naughty user!"
end
when "lock"
if(users[cgi['user']]['admin'] == true) then
puts "Doors locked."
serial.print "l\r"
else
puts "Fail. Don't be a naughty user!"
end
when "arm"
if(users[cgi['user']]['admin'] == true) then
puts "Armed."
serial.print "2\r"
else
puts "Fail. Don't be a naughty user!"
end
when "disarm"
if(users[cgi['user']]['admin'] == true) then
puts "Disarmed."
serial.print "1\r"
else
puts "Fail. Don't be a naughty user!"
end
else
puts "Fail. Don't be a naughty user!"
end
*/
serial.close
puts ' <a href="/~access/management.html">Return.</a>'
else
puts "Invalid administrator username or password."
end

View File

@ -56,6 +56,7 @@
<option value="unlock">Unlock all doors</option> <option value="unlock">Unlock all doors</option>
<option value="arm">Arm alarm</option> <option value="arm">Arm alarm</option>
<option value="disarm">Disarm alarm</option> <option value="disarm">Disarm alarm</option>
<option value="status">System Status</option>
</select> </select>
</label> </label>
<input type="submit" id="submit" name="submit" value="Go!" /> <input type="submit" id="submit" name="submit" value="Go!" />

67
management.html Normal file
View File

@ -0,0 +1,67 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style type="text/css">
body { background-color: #2C2C29; color: #2C2C29; font-family: Tahoma; font-size: 11px; margin: 0; padding: 0; }
#wrapper { width: 811px; margin: 0 auto; }
#top { background:url('http://www.heatsynclabs.org/wp-content/themes/lightword/images/header-image.png') no-repeat; height:116px; margin: 0 0 0 5px; }
#top span { display: none; }
#content { background-color: #fff; padding: 1em; font-size: 1.2em; }
.caption {
background-color: #F3F3F3;
border: 1px solid #DDD;
padding: 4px;
margin: 0 30px;
display: inline-block;
}
.footer {
clear: both;
}
label {
display: block;
font-weight: bold;
}
h2 {
font-family: Helvetica, Georgia;
font-size: 24px;
letter-spacing: -1px;
margin: 10px 0px 3px;
border-bottom: 1px solid #DCDCDB;
}
</style>
</head>
<body>
<div id="wrapper">
<a href="http://www.heatsynclabs.org"><h1 id="top"><span>HeatSync Labs</span></h1></a>
<div id="content">
<h2>HeatSync Labs Access Control</h2>
<p>Beware all ye who enter here! Only authorized users are allowed to open and close the lab. If you don't have a username/password to this system, don't be a naughty user.</p>
<div class="caption">
<form method="post" action="cgi-bin/usermanage.rb">
<h3>Administration Details</h3>
<label for="adminuser">Admin Username <input type="text" id="adminuser" name="adminuser" /></label>
<label for="adminpass">Admin Password <input type="password" id="adminpass" name="adminpass" /></label>
<h3>Add a User</h3>
<label for="newuser">New Username <input type="text" id="newuser" name="newuser" /></label>
<label for="newpass">Password <input type="password" id="adminpass" name="adminpass" /></label>
<label for="isuseradmin">Grant Admin <input type="checkbox" id="isusradmin" name="isuseradmin" /></label>
<input type="submit" id="submit" name="submit" value="Add User" />
<h3>Delete User</h3>
<label for="deletename">Username <input type="text" id="deletename" name="deletename" /></label>
<input type="submit" id="submit" name="submit" value="Delete User" />
</form>
</div>
<p class="footer">To report problems or ask for help/access, contact <a href="http://twitter.com/willbradley">@willbradley</a></p>
</div>
</div>
</body>
</html>