###############################################################################
# BizDesign ImageFolio Lite Edition 4.2 (Four-dot-Two)
###############################################################################
#
#          ##          ###        VERSION       :  4.2
#        ####        ##   ##      RELEASED ON   :  02/15/2007
#      ##  ##              ##     LAST MODIFIED :  02/15/2007
#    ##########          ##       
#          ##   ###    ##         
#          ##   ###  ########     
#
###############################################################################
# Released by BizDesign, Inc.
# written by Dirk Koppers and Greg Raaum of BizDesign
#
# Purchasing :  http://imagefolio.com/purchase/
# Support    :  http://imagefolio.com/support/
# Phone      :  (214) 642-9787
# Email      :  sales@bizdesign.com
###############################################################################
# COPYRIGHT AND LICENSE INFORMATION :
#
# Copyright (c) 1999-2007 BizDesign, Inc. All rights reserved.
#
# Selling or distributing the code for this program without prior written
# consent is expressly forbidden.
#
# One licensed copy of the program may reside on a single server, in use by a
# single domain.  For each installed instance of the program, a separate
# license is required.
#
# Licensed users may alter or modify this software, at their own risk, of
# course.  They may also hire others to modify their own copy of the code, as
# long as the code is not transferred to or retained by the individual who is
# hired (unless he/she is also a license holder). Although license-holders
# may modify the code for their use, modified code may NOT be resold or
# distributed.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BIZDESIGN,
# INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# BizDesign, Inc. is not liable for any conduct associated with image gallery
# activity, nor for any binary media posted using this program, including but
# not imited to images, photographs, movies, and clipart.
#
# The user must assume the entire risk of using the program.  Although this
# program has been thoroughly tested on BizDesign's servers, BizDesign does not
# warrant that it works on all servers and will not be held liable for anything,
# including but not limited to, misusage, error, or loss of data.  ANY
# LIABILITY OF THE SELLER WILL BE LIMITED EXCLUSIVELY TO PRODUCT REPLACEMENT
# OR REFUND OF PURCHASE PRICE.  Use at your own risk!
###############################################################################
# Do not modify below this line unless you know what you are doing.
###############################################################################

use Socket;

if ($FORM{'set_proxy'}) {
   &set_proxy_ic;
}

if (-e "$data_directory/proxy.pl") {
   require "$data_directory/proxy.pl";
}

eval {
   require LWP::UserAgent;
   require HTTP::Request;
   $ua = new LWP::UserAgent;
   $ua->env_proxy();
   if ($proxy_address) {
      $proxy_dev = ":" if $proxy_port;
      $ua->proxy(http => "http://${proxy_address}${proxy_dev}${proxy_port}");
   }
   $ua->agent("IFLite::$ENV{'HTTP_HOST'}");
   1;
} or $socket_loading = 1;

sub if_web_get {
   my ($request, $file) = @_;
   my $response = ();
   if ($socket_loading) {
      $request =~ s/ /+/g;
      $server = $request;
      $server =~ s/^https?:\/\///i;
      @server = split(/\//, $server);
      $server = shift(@server);
      $request =~ s/^https?:\/\///i;
      $request =~ s/^$server//;
      ($server, $port) = split(/\:/, $server);
      if ($proxy_address) {
         $hostname = $proxy_address;
         if ($proxy_port) {
            $port = $proxy_port;
         }
      }
      else {
         $hostname = $server;
      }
      $port = '80' if !$port;
      $submit = "GET ${request} HTTP/1.0\015\012HOST: ${server}\015\012USER_AGENT: IFLite::$ENV{'HTTP_HOST'}\015\012\015\012";
      $iaddr = inet_aton($hostname);
      $paddr = sockaddr_in($port, $iaddr);
      $proto   = getprotobyname('tcp');
      if ($file) {
         open (FILE, ">$file");
         binmode FILE;
      }
      socket(SOCK, PF_INET, SOCK_STREAM, $proto);
      connect(SOCK, $paddr);
      send(SOCK,$submit,0);
      if ($file) {
         while(<SOCK>) {
            if ( /^[\r]??$/ ) {
               while (<SOCK>) {
                  print FILE $_;
               }
            }
         }
         close(FILE);
         chmod(0777, $file);
         if (-e $file) {
            $response = "done";
         }
      }
      else {
         while(<SOCK>) {
            if ( /^[\r]??$/ ) {
               while (<SOCK>) {
                  $response .= $_;
               }
            }
         }
      }
      close SOCK;
   }
   else {
      eval { $request = new HTTP::Request('GET', $request); };
      if ($file) {
         open (FILE, ">$file");
         binmode FILE;
         $response = $ua->simple_request($request, \&callback_ic, 4096);
         close FILE;
         chmod(0777, $file);
         if (-e $file) {
            $response = "done";
         }
      }
      else {
         $response = $ua->simple_request($request);
         $response = $response->content;
      }
   }
   return $response;
}

sub callback_ic {
   my ($data) = @_;
   print FILE $data;
}

sub set_proxy_ic {
   $FORM{'set_proxy_address'} =~ s/^https?:\/\///i;
   @proxydomain = split(/\//, $FORM{'set_proxy_address'});
   $FORM{'set_proxy_address'} = shift(@proxydomain);
   @proxydomain = split(/\:/, $FORM{'set_proxy_address'});
   $FORM{'set_proxy_address'} = shift(@proxydomain);
   if ($FORM{'set_proxy_port'} =~ /\D/) {
      $FORM{'set_proxy_port'} = ();
   }
   open (IC_PROXY,">$data_directory/proxy.pl") or &Error("Could not create data/proxy.pl. Reason: $!");
   print IC_PROXY '$proxy_address = \'' . $FORM{'set_proxy_address'} . '\';' . "\n";
   print IC_PROXY '$proxy_port = \'' . $FORM{'set_proxy_port'} . '\';' . "\n";
   print IC_PROXY "1;\n";
   close(IC_PROXY);
   chmod(0777,"$data_directory/proxy.pl");
}



1;
