Skip to content

Commit

Permalink
User authentication is now required to post pics.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexJWayne committed Aug 17, 2008
1 parent 638e2b4 commit 51d4f60
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
30 changes: 18 additions & 12 deletions app/controllers/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
class ImagesController < ApplicationController

def create
extension = params[:filename].match(/\.(.+?)$/)[1]
image_io = StringIO.new(Base64.decode64(params[:image]))
image_data = UploadIO.new(image_io, "image/#{extension}", params[:filename])
cookie = LoginCookie::ShackPics.new(params[:username], params[:password])
if cookie.success?
extension = params[:filename].match(/\.(.+?)$/)[1]
image_io = StringIO.new(Base64.decode64(params[:image]))
image_data = UploadIO.new(image_io, "image/#{extension}", params[:filename])

url = URI.parse('http://shackpics.com/upload.x')
request = Net::HTTP::Post::Multipart.new(url.path, "userfile[]" => image_data)
response = Net::HTTP.start(url.host, url.port) { |http| http.request(request) }
url = URI.parse('http://shackpics.com/upload.x')
request = Net::HTTP::Post::Multipart.new(url.path, "userfile[]" => image_data)
request['Cookie'] = cookie.string
response = Net::HTTP.start(url.host, url.port) { |http| http.request(request) }

parser = LibXML::XML::HTMLParser.new
parser.string = response.body
page = parser.parse.root
parser = LibXML::XML::HTMLParser.new
parser.string = response.body
page = parser.parse.root

puts response.body
puts response.body

render :text => "<success>#{page.find_first("//input[@id='link11']")[:value]}</success>"
end
render :text => "<success>#{page.find_first("//input[@id='link11']")[:value]}</success>", :status => :created
else
render :text => "<error>Not Authorized</error>", :status => :not_authorized
end
end
end
20 changes: 19 additions & 1 deletion app/models/login_cookie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class LoginCookie
attr_reader :string, :status

def initialize(username, password)
# Post the login
response = Net::HTTP.post_form(URI.parse('http://www.shacknews.com/login_laryn.x'), {
:username => username,
:password => password,
Expand All @@ -22,4 +21,23 @@ def initialize(username, password)
def success?
@status == :success
end



class ShackPics < LoginCookie
def initialize(username, password)

response = `curl -i -d "user_name=#{username}&user_password=#{password}" http://www.shackpics.com/users.x?act=login_go`
puts response

if cookie = response.split("\n").find { |line| line =~ /^Set-Cookie: mmh_user_cookie=(.+?\.[A-Za-z0-9\.]+?);/ }
cookie_value = $1

@string = "mmh_user_cookie=#{cookie_value}"
@status = :success
else
@status = :not_authorized
end
end
end
end

0 comments on commit 51d4f60

Please sign in to comment.