Guest User

Untitled

a guest
Aug 27th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 KB | None | 0 0
  1. #!/media/gigs/ruby/bin/ruby
  2.  
  3. # Header file. Contains session stuff, and the top of each HTML page. Should be required by all scripts serving HTML
  4.  
  5. # Used to get URL details
  6. require 'uri'
  7. require 'net/http'
  8.  
  9. # Used to encode passwords
  10. require 'digest/sha1'
  11.  
  12. # Session stuff
  13. require 'cgi'
  14. require 'cgi/session'
  15. cgi = CGI.new('html5')
  16. sess = CGI::Session.new(cgi, 'session_key' => 'gallerylogin')
  17.  
  18. # Page class
  19. class Page {
  20.  
  21. # Creates all the vital statistics for a page
  22. def initialize(name, loginrequired, content, user) {
  23.  
  24. @name = name
  25. @loginrequired = loginrequired
  26.  
  27. if user.class == User {
  28.  
  29. # We are logged in; display the content
  30. @topbar = '<span id="register">Logged in as ' + user.name + '</span>'
  31. @content = content
  32.  
  33. } else {
  34.  
  35. # We are not logged in.
  36. @topbar = '<form action="login.rb" method="POST">
  37.  
  38. <label for="email">Login:</label>
  39. <input type="email" name="email" id="email" placeholder="Email address" />
  40. <input type="password" name="password" id="password" placeholder="Password" />
  41.  
  42. </form>
  43. <span id="register">or <a href="register.rb" title="Register">Register</a></span>'
  44.  
  45. # Do we need to be logged in to see the content?
  46. if @loginrequired == 1 {
  47.  
  48. # We are not allowed to be here!
  49. @content = 'Get logged in, boyo.'
  50.  
  51. } else {
  52.  
  53. # We're allowed to be here.
  54. @content = content
  55.  
  56. }
  57.  
  58. }
  59.  
  60. }
  61.  
  62. # Returns a completed HTML page
  63. def output {
  64.  
  65. # Output HMTL
  66. return 'Content-Type: text/html\n\n
  67. <!DOCTYPE html>
  68. <html>
  69.  
  70. <head>
  71.  
  72. <!-- Title -->
  73. <title>Magic Gallery :: ' + @name + '</title>
  74.  
  75. <!-- UTF-8 charset -->
  76. <meta charset="utf-8" />
  77.  
  78. <!-- HTML 5 -->
  79. <link rel="stylesheet" type="text/css" href="style/reset.css" />
  80.  
  81. <!-- Custom fonts -->
  82. <link rel="stylesheet" type="text/css" href="style/fonts.css" />
  83.  
  84. <!-- Style -->
  85. <link rel="stylesheet" type="text/css" href="style/style.css" />
  86.  
  87. </head>
  88. <body lang="en">
  89.  
  90. <!-- Content -->
  91. <div id="centre">
  92.  
  93. <section id="topbar">' + @topbar + '</section>
  94. <section id="main">
  95.  
  96. <h1>Merlin\'s Magic Gallery :: ' + @name + '</h1>' + @content + '</section>
  97.  
  98. </div>
  99.  
  100. <!-- End content -->
  101. <!-- JS -->
  102.  
  103. </body>
  104.  
  105. </html>'
  106.  
  107. }
  108.  
  109. }
  110.  
  111. # User class
  112. class User {
  113.  
  114. # Create User
  115. def initialize(mode,id,pass,email,name,age,gender) {
  116.  
  117. if mode == 'register' {
  118.  
  119. @pass = Digest::SHA1.hexdigest(pass)
  120. @email = email
  121. @name = name
  122. @age = age
  123. @gender = gender
  124.  
  125. self.register
  126.  
  127. } else if mode == 'login' {
  128.  
  129. # Loading an existing user
  130. @email = email
  131. @pass = Digest::SHA1.hexdigest(pass)
  132.  
  133. self.login
  134.  
  135. } else if mode == 'load' {
  136.  
  137. @id = id
  138.  
  139.  
  140. }
  141.  
  142. @ip = request.env['REMOTE_ADDR']
  143.  
  144. }
  145.  
  146. attr_reader :name, :id, :email, :age, :gender, :ip
  147.  
  148. # Register a user
  149. def register {
  150.  
  151. # Creating a new user
  152. # Get the last numbered user
  153. biggestuser = 0
  154. self.everyUser.each {
  155.  
  156. |userid|
  157. if userid.to_i > biggestuser {
  158.  
  159. biggestuser = userid.to_i
  160.  
  161. }
  162.  
  163. }
  164.  
  165. # We now have the latest user as 'biggestuser'. Let's make the new id... one more!
  166. @id = biggestuser + 1
  167.  
  168. # Create file
  169. userfile = File.new('users/' + @id + '.user', 'w')
  170. userfile.puts @pass
  171. userfile.puts @email
  172. userfile.puts @name
  173. userfile.puts @age
  174. userfile.puts @gender
  175. userfile.close
  176.  
  177. }
  178.  
  179. # Login from email/password given
  180. def login {
  181.  
  182. # Hi Callum!
  183.  
  184. }
  185.  
  186. # Returns an array of all users on disk
  187. def everyUser {
  188.  
  189. Dir.entries('/users/').each {
  190.  
  191. |filename|
  192. if (filename != '.' && filename != '..') {
  193.  
  194. # Trim '.user' from the filename
  195. allusers[] = filename[0,(filename.length - 5)]
  196. }
  197.  
  198. }
  199.  
  200. return allusers
  201.  
  202. }
  203.  
  204. }
  205.  
  206. # Product class
  207. class Product {
  208.  
  209. def initialize(id,name,description) {
  210.  
  211. @id = id
  212. @name = name
  213. @description = description
  214.  
  215. }
  216.  
  217. attr_reader :id, :name, :description
  218.  
  219.  
  220.  
  221. }
  222.  
  223. # Log class
  224. class Log {
  225.  
  226. # Create log object
  227. def initialize(type) {
  228.  
  229. # This is used by pretty much everything
  230. @type = type
  231.  
  232. # Only log if it's a real log type
  233. if @type == 'visitor' {
  234.  
  235. # Visitor log format
  236. @format = '%userid%:%name%,%email%,%age%,%gender%'
  237.  
  238. } else if @type == 'interest' {
  239.  
  240. # Product log format
  241. @format = '%product%:%userid%'
  242.  
  243. } else if @type == 'gallery_access' {
  244.  
  245. # Gallery Access log format
  246. @format = '%ip% [%time%] %url%:%method% | %userid%'
  247.  
  248. } else {
  249.  
  250. # Illegal log type
  251. @error = 'Illegal log type'
  252.  
  253. }
  254.  
  255. # Only create @file if we haven't errored
  256. if @error == nil {
  257.  
  258. # Creating @file!
  259. @file = File.new('logs/' + @type + '.log', 'a')
  260.  
  261. } else {
  262.  
  263. # Otherwise, display error
  264. self.error
  265.  
  266. }
  267.  
  268. }
  269.  
  270. # Append to log
  271. def update(user,product) {
  272.  
  273. if user.class == User {
  274.  
  275. if (@type == 'interest' && product.class != Product) {
  276.  
  277. # Invalid product
  278. @error = 'Illegal product details'
  279.  
  280. }
  281.  
  282. } else {
  283.  
  284. # Not a valid user
  285. @error = 'Invalid user details'
  286.  
  287. }
  288.  
  289. # Only continue if we have no errors
  290. if @error == nil {
  291.  
  292. # This is where the magic happens
  293. @file.puts format(user,product)
  294.  
  295. } else {
  296.  
  297. # Display error
  298. self.error
  299.  
  300. }
  301.  
  302. }
  303.  
  304. # Formats the line for the log
  305. def format(user,product) {
  306.  
  307. # Create a new version of the format, so this can safely be called multiple times with changed values (like time/ip)
  308. data = String.new(@format)
  309. format(data,'%userid%',user.id)
  310. format(data,'%name%',user.name)
  311. format(data,'%email%',user.email)
  312. format(data,'%age%',user.age)
  313. format(data,'%gender%',user.gender)
  314. format(data,'%ip%',user.ip)
  315. format(data,'%time%',Time.now)
  316. format(data,'%url%',Net::HTTP.get_reponse(URI.parse(url)))
  317. format(data,'%method%',request.method)
  318. if (product.class == Product) {
  319.  
  320. format(data,'%product%',product.id)
  321.  
  322. }
  323.  
  324. return data
  325.  
  326. }
  327.  
  328. # Formats part of a log (call multiple times)
  329. def formatentry(entry,place,replacement) {
  330.  
  331. start = entry.index(place)
  332. entry[start, place.length] = replacement
  333. return entry
  334.  
  335. }
  336.  
  337. # Catches, displays errors
  338. def error {
  339.  
  340. # Just a backup; this method should only be called when there *is* an @error
  341. if @error == nil {
  342.  
  343. puts '[No error!]'
  344.  
  345. } else {
  346.  
  347. puts '[Error: ' + @error + '.]'
  348.  
  349. }
  350.  
  351. }
  352.  
  353. # Closes the log file
  354. def end {
  355.  
  356. if @file.class = File {
  357.  
  358. # Obviously, only close it if it was a file in the first place
  359. @file.close
  360.  
  361. }
  362.  
  363. }
  364.  
  365. }
  366.  
  367. # Check if logged in. Assigns $me (either the integer 0 if you aren't logged in, or your user details if you are)
  368. if sess['userid'] != nil {
  369.  
  370. # The session thinks we're logged in. Do we have a file?
  371. if userfile = File.new('users/' + sess['userid'] + '.user', 'r') {
  372.  
  373. # 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.
  374. userdetails = userfile.readlines
  375. userfile.close
  376.  
  377. # Security. Check the passwords:
  378. if sess['password'] == userdetails[0]
  379.  
  380. # The passwords match
  381. $me = new User(sess['userid'],userdetails[0],userdetails[1],userdetails[2],userdetails[3],userdetails[4],request.env['REMOTE_ADDR'])
  382.  
  383. } else {
  384.  
  385. # Alert! Alert! The passwords do not match!
  386. sess['userid'] = nil
  387. $me = 0
  388.  
  389. }
  390.  
  391. } else {
  392.  
  393. # We don't have a file. Best empty that broken cookie, and set user to 0.
  394. sess['userid'] = nil
  395. $me = 0
  396.  
  397. }
  398.  
  399. } else {
  400.  
  401. # The session doesn't know us. Our user data is the integer 0. If we were logged in, it would be of class User.
  402. $me = 0
  403.  
  404. }
Add Comment
Please, Sign In to add comment