package Plugins::axifile;
#
# axifile.pm module is a part of Web Share Downloader
# Copyright (C) 2006,2008 by Aleksey Luzin
# Thank to Dimoniusis for this algorithm
#
#

use strict;
use warnings;
use HTTP::Request::Common qw(POST);
use WSD::Download qw/&direct_download &create_request/;
use sql;
use Common;
use Exporter ();
use vars qw(@ISA $VERSION);

our ( @EXPORT);

@ISA = qw(Exporter);
@EXPORT = qw(&check_link &download);
$VERSION = "1.3" ;

sub check_link {
    shift;
    my $link = shift;
    if ($link =~ /axifile.com/) {
	return 1;
    }
    return 0;
}
my $cookie = '';
my $id;
sub get_directlink {
    my $link = shift;
    my $response = $browser->get($link);
    unless ($response->is_success) {$log->write(4, $response->status_line());return '';    }
    my $content = $response->content;
    if ($content =~ /File ID is not correct/s) {return ''}
    if ($content =~ /This file is password protected./s) {
	$content =~ /FORM action="(.*?)"/i;
	my $action = $1;
	my $lnk = $links->getlink($id);    
	my $pwd = '';
	$pwd = $1 if($lnk->{comment}=~ /pass=\"(.*?)\"/);
	my $req = POST 'http://www.axifile.com/'.$action,
	    Referer=> $link,
	    Content => [pwd=>$pwd];
	$response = $browser->request($req);
	$content = $response->content;
    }
    $response = $browser->get('http://www.axifile.com/javascript/donwload.js');
    unless ($response->is_success) {	return '';    }
    $content = $response->content;
    $content =~ /setCookie\(\"pv\",\"(.*?)\"/;
    my $req = create_request('http://www.axifile.com/flash/baner.swf', 'GET', 'http://www.axifile.com/javascript/donwload.js', "pv=$1");
    $browser->cookie_jar->clear;
    $response = $browser->request($req);
    my $s = $response->content;
    my (@arr1, @arr4, $i, $n);
    $s =~ /v9973/;
    $n = $-[0]+6;
    for($i = 17; $i<32; $i++) {
	$arr1[$i] = substr $s, $n, 1;
        $n+=2;
    }
    my $z =  chr(150).chr(2).chr(0).chr(8).chr(16).chr(28).chr(150).chr(5).chr(0).chr(7);
    my $z1 = chr(29).chr(153).chr(2).chr(0).chr(8).chr(0).chr(150).chr(4).chr(0).chr(8).chr(15).chr(8);
    my @arr2 = split($z, $s);
    my @arr3 = reverse(split($z1, $s));
    shift @arr2;
    my $ss = '';
    map {$_ = ord(substr ($_,0,1));}@arr2;
    map {$_ = ord(substr ($_,0,1));}@arr3;
    for($i=0;$i<32;$i++){
	$arr4[$arr2[$i]]=$arr1[$arr3[$i]];
    }
    $s =~ /http:\/\/(dl.*?)\.axifile\.com.(.*?).javascript/;
    my $dlink = "http://$1.axifile.com/".join('',@arr4).$2;
    $dlink =~ s/\x00//g;
    $log->write(3,"$dlink");
    return $dlink;    
}

sub download {
    shift;
    $id = shift;
    $links->setstatus($id, {status=>1});
    $browser->cookie_jar->clear;
    my $link = $links->getlink($id);    
    my $dlink = get_directlink($link->{link});
    if ($dlink) {
	sleep(40);
        $browser->cookie_jar->clear;
	my $req = create_request($dlink);
	WSD::Download::direct_download($id, $req);    
    } else {
	my $error = 'Не могу получить прямую ссылку';
	$log->write(2,"$error");
	$links->setstatus($id, {status=>3, statustext=>$error, nexttry=>'5 minute'});
    }
}
1;
