require "digest/sha2"
require "RMagick"
class AdminController < ApplicationController
layout "sweet"
def index
unless session[:current_user].nil?
redirect_to({:action => "interface"})
end
@valeur_captcha = rand(99999).to_s
captcha = Magick::Image.new(100, 15)
texte = Magick::Draw.new
texte.gravity = Magick::CenterGravity
texte.annotate(captcha, 0, 0, 0, 0, @valeur_captcha)
captcha.write("#{RAILS_ROOT}/public/images/captcha/#{request.env['REMOTE_ADDR']}.jpg")
end
def connexion
@admin_tst = Admin.new(params[:admin])
if @admin_tst[:captcha] == @valeur_captcha
@admin = Admin.find(:first, {:conditions => ["pseudo= ? and pass= ?", @admin_tst.pseudo, Digest::SHA512.hexdigest(@admin_tst.pass)]})
if @admin.nil? then
@error_message = true
@admin = @admin_tst
render({ :action => "index"})
badguy_ip = File.new("#{RAILS_ROOT}/public/badguy", "a+")
@list_badguy_ip = badguy_ip.readlines
suspect = File.new("#{RAILS_ROOT}/public/suspect", "a+")
list_suspect_ip = suspect.readlines
@ip = request.env['REMOTE_ADDR']
if list_suspect_ip.include?(@ip) then
unless (@list_badguy_ip.include?(@ip))
badguy_ip.write @ip
end
badguy_ip.close
end
cookies[:try] = {:value => "01", :expires => 24.hour.from_now}
unless (list_suspect_ip.include?(@ip))
suspect.write @ip
end
suspect.close
else
session[:current_user] = @admin
redirect_to({ :action => "interface"})
end
else
@bad_captcha = true
render({ :action => "index"})
end
end
def interface
if session[:current_user].nil? then
redirect_to({ :action => "index"})
else
@news = News.find(:all, :order => "created_at DESC")
end
end
def ajout
if session[:current_user].nil? then
redirect_to({ :action => "index"})
else
if request.post? then
@news = News.new(params[:idnews])
@news.save
redirect_to(:action => "interface")
end
end
end
def suppression
if session[:current_user].nil? then
redirect_to({ :action => "index"})
else
News.find(params[:id]).destroy
redirect_to(:action => "interface")
end
end
def modification
if session[:current_user].nil? then
redirect_to({ :action => "index"})
else
@news = News.find(params[:id])
if request.post? then
@news.update_attributes(params[:news])
redirect_to(:action => "interface")
end
end
end
end