Файл: EasyHost v2.0/system/htdocs/admin/system/updates.pl
Строк: 111
<?php
$system->error('This feature has been disabled in demo mode') if $system->config('demo');
my %files_current = ();
my $directory = $system->config('system_dir');
my $server = 'client.cyberscript.net';
my %files_remote = ();
my %files_local = &files_local();
my %files_log = &files_log();
$system->error('EasyHost is currently busy already updating itself, please try again') if (-e $directory.'update.lock');
EasyHost::File->new($directory.'update.lock')->write(1);
my $socket = EasyHost::Socket->connect($server,80);
$socket->send('GET /update/ HTTP/1.1');
$socket->send('host: '.$server);
$socket->send('script: '.$system->config('script_url')."rn");
my $count = 0;
my $text = $socket->recv(0);
$text =~ s/^.+?(n{2}|rnrn)//s if (substr($text,0,5) eq 'HTTP/');
foreach (split(/n/,$text))
{
my @line = split(/:/,$_,2);
$files_remote{$line[0]} = $line[1];
}
if (scalar keys %files_remote == 0)
{
unlink($directory.'update.lock');
$system->error('EasyHost wasn't able to fetch a list of files from the update server, please try again');
}
my $server_login = EasyHost::File->new($directory.'update.key')->read;
if (length $server_login == 0)
{
unlink($directory.'update.lock');
$system->error('EasyHost wasn't able to find a valid license');
}
open(LOG,'>'.$directory.'update.log');
my @newfiles = ();
foreach my $file (sort { lc($b) cmp lc($a) } keys(%files_local))
{
if ($files_remote{$file} > $files_log{$file} && $file !~ //$/)
{
rename($directory.$file,$directory.$file.'.old') if ($files_local{$file} != $files_log{$file});
print LOG $file.':'.$files_remote{$file}."n";
push(@newfiles,$file);
}
elsif (!$files_remote{$file} && $files_local{$file} == $files_log{$file})
{
if ($file =~ //$/)
{
rmdir($directory.$file);
}
else
{
unlink($directory.$file);
}
}
elsif ($files_log{$file})
{
print LOG $file.':'.$files_log{$file}."n";
}
}
foreach my $file (sort { lc($a) cmp lc($b) } grep { $files_local{$_} < 1 } keys(%files_remote))
{
if ($file =~ //$/)
{
mkdir($directory.$file,0755);
print LOG $file.':'.(stat($directory.$file))[9]."n";
utime($files_remote{$file},$files_remote{$file},$directory.$file);
}
else
{
print LOG $file.':'.$files_remote{$file}."n";
push(@newfiles,$file);
}
}
close(LOG);
my $maxfiles = ($system->config('update_files') > 0) ? $system->config('update_files') : 10;
while ($#newfiles >= 0)
{
my @temp = ();
my $amount = ($#newfiles >= $maxfiles) ? $maxfiles : $#newfiles + 1;
for (my $i = 0; $i < $amount; $i++)
{
push @temp,shift(@newfiles);
}
my $socket = EasyHost::Socket->connect($server,80);
$socket->send('GET /update/ HTTP/1.1');
$socket->send('host: '.$server);
$socket->send('code: '.$server_login);
$socket->send('encode: base64');
$socket->send('filename: '.join(',',@temp)."rn");
my $text = $socket->recv(0);
$text =~ s/^.+?(n{2}|rnrn)//s if (substr($text,0,5) eq 'HTTP/');
my @all = split /#$server_login#/,$text;
for (my $i = 0; $i < $amount; $i++)
{
my $file = shift(@temp);
if (length $all[$i] > 0)
{
open(FILE,'>'.$directory.$file);
binmode(FILE) if ($file =~ /(gif|jpg|jpeg)$/i);
print FILE &decode($all[$i]);
close(FILE);
utime($files_remote{$file},$files_remote{$file},$directory.$file);
}
}
}
unlink($directory.'update.key');
unlink($directory.'update.lock');
$socket->disconnect;
$system->redirect('finish');
sub files_log
{
my %files_log = ();
open(LOG,$directory.'update.log');
while (<LOG>)
{
chop $_ if ($_ =~ /n/);
my @line = split(/:/,$_,2);
$files_log{$line[0]} = $line[1];
}
close(LOG);
return %files_log;
}
sub files_local
{
my ($path,%temp) = @_;
opendir(DIR,$directory.$path);
my @dir = grep { $_ !~ /^.+$/ } readdir(DIR);
closedir(DIR);
foreach (@dir)
{
if (-d $directory.$path.$_)
{
$temp{$path.$_.'/'} = (stat($directory.$path.$_))[9];
%temp = &files_local($path.$_.'/',%temp);
}
else
{
$temp{$path.$_} = (stat($directory.$path.$_))[9];
}
}
return %temp;
}
sub decode
{
my $str = shift;
$str =~ tr|A-Za-z0-9+=/||cd;
return undef if (length($str) % 4);
$str =~ s/=+$//;
$str =~ tr|A-Za-z0-9+/| -_|;
return undef unless length $str;
my ($uustr,$i,$l);
$l = length($str) - 60;
for ($i = 0; $i <= $l; $i += 60)
{
$uustr .= 'M'.substr($str,$i,60);
}
$str = substr($str,$i);
$uustr .= chr(32 + length($str) * 3 / 4).$str if (length $str > 0);
return unpack('u',$uustr);
}
?>