###############################################################################
# AvantGo.pm - this code is for AvantGo
#
# Copyright (C) 2001 Norbert "Momo_102" Kuemin
# momo_102@bluemail.ch
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
###############################################################################

package AvantGo;

$VERSION = "1.01";

sub new {
   my $proto = shift;

   my $userfile = undef;
   if (scalar(@_) > 0) {
      $userfile = shift;
   }

   my $self = {
      'UserFile' => $userfile,
      'User' => {},
      'Env' => {},
      'Delimiter' => "\t"
   };
   bless $self, $proto;
   return $self;
}

sub get_version {
   return $VERSION;
}

sub env_load {
   my $self = shift;
   foreach (keys %ENV) {
      if (/HTTP_X_AVANTGO_(.*)$/) {
         my $key = "HTTP_X_AVANTGO_".$1;
         if (($1 ne "VERSION") && ($1 ne "ONLINEREQUEST")) {
            $self->{'Env'}{$1} = $self->mime_base64_convert($ENV{$key});
         } else {
            $self->{'Env'}{$1} = $ENV{$key};
         }
      }
   }
}

sub env_get {
   my $self = shift;
   my ($key) = @_;
   if (defined($self->{'Env'}{$key})) {
      return $self->{'Env'}{$key};
   } else {
      return "";
   }
}

sub env_keys {
   my $self = shift;
   *AVENV = $self->{'Env'};
   return keys %AVENV;
}

sub user_load {
   my $self = shift;
   if (-r $self->{'UserFile'}) {
      open FD, $self->{'UserFile'};
      while (<FD>) {
         chomp;
         my ($user,$data) = split($self->{'Delimiter'},$_,2);
         $self->{'User'}{$user} = $data;
      }
      close FD;
   }
}

sub user_save {
   my $self = shift;
   *AVUSER = $self->{'User'};
   if (-w $self->{'UserFile'}) {
      open FD, ">".$self->{'UserFile'};
      foreach (keys %AVUSER) {
         print FD $_.$self->{'Delimiter'}.$self->{'User'}{$_}."\n";
      }
      close FD;
   }
}

sub user_get {
   my $self = shift;
   my $user = $self->env_get("USERID");
   if (scalar(@_) > 0) { $user = shift; }
   return split($self->{'Delimiter'},$self->{'User'}{$user});
}

sub user_set {
   my $self = shift;
   my $user = shift;
   $self->{'Delimiter'},$self->{'User'}{$user} = join($self->{'Delimiter'},@_);
}

sub mime_base64_convert {
   my $self = shift;
   my ($text) = @_;
   my $poss = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
   my $newtext = "";
   for (my $i=0 ; $i < (length($text)/4) ; $i++) {
      my $char1 = "";
      my $char2 = "";
      my $char3 = "";
      my $frag = substr($text,($i*4),4);
      my $byte1 = index($poss,substr($frag,0,1));
      my $byte2 = index($poss,substr($frag,1,1));
      my $byte3 = index($poss,substr($frag,2,1));
      my $byte4 = index($poss,substr($frag,3,1));
      if (($byte1 < 64) && ($byte2 < 64)) {
         $char1 = chr($byte1*4 + ($byte2 & 48)/16);
      }
      if (($byte2 < 64) && ($byte3 < 64)) {
         $char2 = chr(($byte2 & 15)*16 + ($byte3 & 60)/4);
      }
      if (($byte3 < 64) && ($byte4 < 64)) {
         $char3 = chr(($byte3 & 3)*64 + $byte4);
      }
      $newtext = $newtext . $char1 . $char2 . $char3;
   }
   return $newtext;
}
