Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/media/gigs/ruby/bin/ruby
- # Header file. Contains session stuff, and the top of each HTML page. Should be required by all scripts serving HTML
- # Used to get URL details
- require 'uri'
- require 'net/http'
- # Used to encode passwords
- require 'digest/sha1'
- # Session stuff
- require 'cgi'
- require 'cgi/session'
- cgi = CGI.new('html5')
- sess = CGI::Session.new(cgi, 'session_key' => 'gallerylogin')
- # Page class
- class Page {
- # Creates all the vital statistics for a page
- def initialize(name, loginrequired, content, user) {
- @name = name
- @loginrequired = loginrequired
- if user.class == User {
- # We are logged in; display the content
- @topbar = '<span id="register">Logged in as ' + user.name + '</span>'
- @content = content
- } else {
- # We are not logged in.
- @topbar = '<form action="login.rb" method="POST">
- <label for="email">Login:</label>
- <input type="email" name="email" id="email" placeholder="Email address" />
- <input type="password" name="password" id="password" placeholder="Password" />
- </form>
- <span id="register">or <a href="register.rb" title="Register">Register</a></span>'
- # Do we need to be logged in to see the content?
- if @loginrequired == 1 {
- # We are not allowed to be here!
- @content = 'Get logged in, boyo.'
- } else {
- # We're allowed to be here.
- @content = content
- }
- }
- }
- # Returns a completed HTML page
- def output {
- # Output HMTL
- return 'Content-Type: text/html\n\n
- <!DOCTYPE html>
- <html>
- <head>
- <!-- Title -->
- <title>Magic Gallery :: ' + @name + '</title>
- <!-- UTF-8 charset -->
- <meta charset="utf-8" />
- <!-- HTML 5 -->
- <link rel="stylesheet" type="text/css" href="style/reset.css" />
- <!-- Custom fonts -->
- <link rel="stylesheet" type="text/css" href="style/fonts.css" />
- <!-- Style -->
- <link rel="stylesheet" type="text/css" href="style/style.css" />
- </head>
- <body lang="en">
- <!-- Content -->
- <div id="centre">
- <section id="topbar">' + @topbar + '</section>
- <section id="main">
- <h1>Merlin\'s Magic Gallery :: ' + @name + '</h1>' + @content + '</section>
- </div>
- <!-- End content -->
- <!-- JS -->
- </body>
- </html>'
- }
- }
- # User class
- class User {
- # Create User
- def initialize(mode,id,pass,email,name,age,gender) {
- if mode == 'register' {
- @pass = Digest::SHA1.hexdigest(pass)
- @email = email
- @name = name
- @age = age
- @gender = gender
- self.register
- } else if mode == 'login' {
- # Loading an existing user
- @email = email
- @pass = Digest::SHA1.hexdigest(pass)
- self.login
- } else if mode == 'load' {
- @id = id
- }
- @ip = request.env['REMOTE_ADDR']
- }
- attr_reader :name, :id, :email, :age, :gender, :ip
- # Register a user
- def register {
- # Creating a new user
- # Get the last numbered user
- biggestuser = 0
- self.everyUser.each {
- |userid|
- if userid.to_i > biggestuser {
- biggestuser = userid.to_i
- }
- }
- # We now have the latest user as 'biggestuser'. Let's make the new id... one more!
- @id = biggestuser + 1
- # Create file
- userfile = File.new('users/' + @id + '.user', 'w')
- userfile.puts @pass
- userfile.puts @email
- userfile.puts @name
- userfile.puts @age
- userfile.puts @gender
- userfile.close
- }
- # Login from email/password given
- def login {
- # Hi Callum!
- }
- # Returns an array of all users on disk
- def everyUser {
- Dir.entries('/users/').each {
- |filename|
- if (filename != '.' && filename != '..') {
- # Trim '.user' from the filename
- allusers[] = filename[0,(filename.length - 5)]
- }
- }
- return allusers
- }
- }
- # Product class
- class Product {
- def initialize(id,name,description) {
- @id = id
- @name = name
- @description = description
- }
- attr_reader :id, :name, :description
- }
- # Log class
- class Log {
- # Create log object
- def initialize(type) {
- # This is used by pretty much everything
- @type = type
- # Only log if it's a real log type
- if @type == 'visitor' {
- # Visitor log format
- @format = '%userid%:%name%,%email%,%age%,%gender%'
- } else if @type == 'interest' {
- # Product log format
- @format = '%product%:%userid%'
- } else if @type == 'gallery_access' {
- # Gallery Access log format
- @format = '%ip% [%time%] %url%:%method% | %userid%'
- } else {
- # Illegal log type
- @error = 'Illegal log type'
- }
- # Only create @file if we haven't errored
- if @error == nil {
- # Creating @file!
- @file = File.new('logs/' + @type + '.log', 'a')
- } else {
- # Otherwise, display error
- self.error
- }
- }
- # Append to log
- def update(user,product) {
- if user.class == User {
- if (@type == 'interest' && product.class != Product) {
- # Invalid product
- @error = 'Illegal product details'
- }
- } else {
- # Not a valid user
- @error = 'Invalid user details'
- }
- # Only continue if we have no errors
- if @error == nil {
- # This is where the magic happens
- @file.puts format(user,product)
- } else {
- # Display error
- self.error
- }
- }
- # Formats the line for the log
- def format(user,product) {
- # Create a new version of the format, so this can safely be called multiple times with changed values (like time/ip)
- data = String.new(@format)
- format(data,'%userid%',user.id)
- format(data,'%name%',user.name)
- format(data,'%email%',user.email)
- format(data,'%age%',user.age)
- format(data,'%gender%',user.gender)
- format(data,'%ip%',user.ip)
- format(data,'%time%',Time.now)
- format(data,'%url%',Net::HTTP.get_reponse(URI.parse(url)))
- format(data,'%method%',request.method)
- if (product.class == Product) {
- format(data,'%product%',product.id)
- }
- return data
- }
- # Formats part of a log (call multiple times)
- def formatentry(entry,place,replacement) {
- start = entry.index(place)
- entry[start, place.length] = replacement
- return entry
- }
- # Catches, displays errors
- def error {
- # Just a backup; this method should only be called when there *is* an @error
- if @error == nil {
- puts '[No error!]'
- } else {
- puts '[Error: ' + @error + '.]'
- }
- }
- # Closes the log file
- def end {
- if @file.class = File {
- # Obviously, only close it if it was a file in the first place
- @file.close
- }
- }
- }
- # Check if logged in. Assigns $me (either the integer 0 if you aren't logged in, or your user details if you are)
- if sess['userid'] != nil {
- # The session thinks we're logged in. Do we have a file?
- if userfile = File.new('users/' + sess['userid'] + '.user', 'r') {
- # We have a file and the session thinks we're logged in. Let's open the file and put the data into a User object.
- userdetails = userfile.readlines
- userfile.close
- # Security. Check the passwords:
- if sess['password'] == userdetails[0]
- # The passwords match
- $me = new User(sess['userid'],userdetails[0],userdetails[1],userdetails[2],userdetails[3],userdetails[4],request.env['REMOTE_ADDR'])
- } else {
- # Alert! Alert! The passwords do not match!
- sess['userid'] = nil
- $me = 0
- }
- } else {
- # We don't have a file. Best empty that broken cookie, and set user to 0.
- sess['userid'] = nil
- $me = 0
- }
- } else {
- # The session doesn't know us. Our user data is the integer 0. If we were logged in, it would be of class User.
- $me = 0
- }
Add Comment
Please, Sign In to add comment