package Plugins::filefactory;
#
# filefactory.pm module is a part of Web Share Downloader
# Copyright (C) 2006-2008 by Aleksey Luzin
#
#

use lib '..';
use MIME::Base64;
use HTTP::Request::Common qw(POST);
use sql;
use WSD::Download qw/&direct_download &create_request/;
use Common;
use Data::Dumper;
use strict;
use warnings;
use Exporter ();
use vars qw($VERSION);

our (@ISA, @EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw(&check_link &download);
$VERSION = "1.6" ;

sub check_link {
	shift;
	my $link = shift;
	if ($link =~ /filefactory\.com/) {
		return 1;
	}
	return 0;
}
sub strtohash {
	my $str = shift;
	my %hash;
	my @a = split(/\|/, $str);
	foreach my $a (@a) {
		my @a1 = split('=', $a);
		$hash{$a1[0]} = $a1[1];
	}
	return \%hash;
}
sub hashtostr {
	my $h = shift;
	my @a = map {$_.'='.$h->{$_}} keys %{$h};
	return join('|', @a);
}
sub step1 {
	my $link = shift;
	my $id = shift;
	$log->write(2,"filefactory::Step1");
	my $req = create_request($link);
	my $response = $browser->request($req);
	unless ($response->is_success) {return '';  }
	my $content = $response->content;
	if($content =~ /This file has been deleted/) {
		my $error = 'This file has been deleted';
		$links->setstatus($id, {status=>3, statustext=>$error});
		return '';
	}
	my $link1 ='';
	$link1 = $1 if(($content =~ /a\s+id="basicLink"\s+href="(.*?)"/) || ($content =~ /a\s+href="(.*?)"\s+id="basicLink"/));
	unless ($link1) {
		$links->setstatus($id, {status=>3, statustext=>'Не могу получить прямую ссылку'});
		return '';
	}
	$link1='http://www.filefactory.com'.$link1 unless($link1 =~ /http/);
	my $req1 = create_request($link1,'GET', $link);
	$response = $browser->request($req1);
	unless ($response->is_success) {return ''; }
	$content = $response->content;
	$content =~ /iframe.*?src=\"(.*?check.*?)\"/;
	my $link2 = 'http://www.filefactory.com'.$1;
	$link2 =~ s/\&amp;/\&/g;
	$req1 = create_request($link2,'GET', $link1);
	$response = $browser->request($req1);
	unless ($response->is_success) {return ''; }
	$content = $response->content;
	$content =~ /img.*?src=[\"\'](.*?)[\"\']/;
	my $img = 'http://www.filefactory.com'.$1;
	my %h;
	while($content =~ /name=\"(.*?)\"\s+value=\"(.*?)\"/g){$h{$1}= $2};
	$img =~ s/\&amp;/\&/g;
	$req1 = create_request($img,'GET', $link2);
	$response = $browser->get($img);
	$content = $response->content;
	my $image = encode_base64($content);
	my $rnd = int(rand(1000000));
	#wait for input image numbers
	my $tmp = 'rnd='.$rnd.'|link='.encode_base64($link2).'|'.hashtostr(\%h);
	$tmp.='|image='.$image;
	$links->setstatus($id, {status=>5, statustext=>'Waiting for input', tmp=>$tmp, input=>'', nexttry=>'5 minute', hostname=>$config->{host} });
	return '';    
}


sub step2 {
	my $link = shift;
	$log->write(2,"filefactory::Step2");
	my $h = strtohash($link->{tmp});
	my $content = ['f'=>$h->{f}, 'h'=>$h->{h}, 'b'=>$h->{b}, 'captcha'=>$link->{input}];
	$h->{link} = decode_base64($h->{link});
	my $req = POST $h->{link},
		Referer=>$h->{link},
		Content => $content;
	my $response = $browser->request($req);
	unless ($response->is_success) {
		my $rc = $response->code;
		if($rc==301) {
		    return $response->header('location');
		}
		return '';  
	}
	$content = $response->content;
	$content =~ /a.*?href="(http:\/\/.*?)"/;
	my $dlink = $1;
	$log->write(2,"directlink: $dlink");
	return $dlink;
}
sub download {
	shift;
	my $id = shift;
	my $link = $links->getlink($id);
	my $dlink = '';
	if(($link->{status} == '5')&&($link->{nexttry} > $link->{now})) {
		$links->setstatus($id, {status=>1});
		$dlink = step2($link) if($link->{input});
	} else {
		$links->setstatus($id, {status=>1});
		step1($link->{link}, $id);
		return '';
	}
	if ($dlink) {
		my $req = create_request($dlink);
		WSD::Download::direct_download($id, $req);    
	} else {
		my $error = 'Не могу получить прямую ссылку';
		$links->setstatus($id, {status=>3, statustext=>$error, nexttry=>"5 minute"});
	}
}

1;