# Copyright (C) 2003, 2004 Gregoire Lejeune <gregoire.lejeune@free.fr>
#
# 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

module Asp
  class Server
    require 'asp/common'
#    require 'asp/compile'

    require 'xml/xslt'
    require 'net/smtp'
    
    attr_accessor :bEnd
    
    public
    def file
      return @oApacheRequest.filename.dup
    end

    def exist?( xFilename )
      return File.exist?( xFilename.untaint ).untaint
    end
    
    alias :fileExist :exist?

    def html_encode( data )
      return Common::html_escape( data )
    end

    alias :htmlEncode :html_encode

    def transfer( filename, *args )
      @bEnd = 1
      self.execute( filename, args )
      goto :e
    end

    def execute( filename, *args )
      funcname = "i__"+rand( Time::new.to_i ).to_s+"__"+rand( Time::new.to_i ).to_s
      code = @oAspHandler.compile(filename)
      ##code = compile(filename)
      totalcode = "def " + funcname + "( args=nil )\n" + code + "\nend\n"

      @oAspHandler.load_code( totalcode, nil )
      eval( "@oAspHandler." + funcname + "( args )" )
    end

    def mapPath( path )
      global = @oApacheRequest.filename.dup
      local = @oApacheRequest.uri
      www = global.gsub( local, "" )
      mmm = global.gsub( /[^\/]*$/, "" )

      result = ""
      if /^\// =~ path
        result = www + path
      else
        result = File::expand_path( mmm + path )
      end

      return result
    end
    
    def mail( hMailOps, hSMTPOps=nil )
      if hSMTPOps.nil? == true
        hSMTPOps = Hash::new
        hSMTPOps["server"] = ENV["MailHost"].untaint || "localhost"
        hSMTPOps["port"]   = ENV["MailPort"].untaint || 25
      end

      body = "From: " + String( hMailOps["From"] ) + "\n" +
             "To: " + String( hMailOps["To"] ) + "\n" +
             "Subject: " + ( String( hMailOps["Subject"] ) || "[No Subject]" ) + "\n\n" +
             ( String( hMailOps["Body"] ) || "" )
      
      begin
        smtp = Net::SMTP::new( hSMTPOps["server"], hSMTPOps["port"] )
        smtp.start( )
        smtp.sendmail( body, hMailOps["From"], hMailOps["To"] )
        smtp.finish
      end
    end

    def url_encode( data )
      return Common::url_encode( data )
    end

    alias :urlEncode :url_encode
    alias :urlPathEncode :url_encode

    def url( uri, hParams=nil )
      args = ""
      sep = "?"
      if hParams.nil? == false
        hParams.each_pair { |k,v|
          args += sep + self.url_encode( k ) + "=" + self.url_encode( v )
          sep = "&"
        }
      end

      return( uri + args )
    end

    def getLastError( )
      return @oAspError
    end
    
    def xslt( xsl_data, xml_data )
      xslt = XML::XSLT.new()
      xslt.xml = xml_data
      xslt.xsl = xsl_data
      return xslt.serve()
    end
    
    # TODO
    def machineName
      ## -> string
    end
  
    # TODO
    def scriptTimeout
      ## -> int
    end

    # TODO
    def clearError( )
      ## -> void
    end

    # TODO
    def createObject( xType )
      ## -> object
    end

    # TODO
    def createObjectFromClsid( xClsid )
      ## -> object
    end
  
    # TODO
    def htmlDecode( xData )
      ## -> string
    end

    def urlDecode( xUrl )
      ## -> string
      return Common::url_decode( xUrl )
    end

    private 
#    def compile(filename)
#      open(filename) do |f|
#        return @compiler.compile(f.read)
#      end
#    end

    def initialize( r, oAspError, aspHandler )
      @oApacheRequest = r
      @oAspError = oAspError
      @oAspHandler = aspHandler

      @bEnd = 0
#      @compiler = AspCompiler.new
#      @compiler.put_cmd = 'print'
    end
  
  end
end 
