# 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

require 'stringio'
require 'asp/goto'

module Asp
  class Common
   
    def Common::version
      return '0.4.1'
    end
    
    def Common::sessionID
      return( Asp::Common::getEnv( 'SessionID' ) || 'session-id' )
    end
  
    def Common::sessionTimeout
      return( Asp::Common::getEnv( 'SessionTimeout' ) || 20 )
    end
  
    def Common::appID
      return '_app'
    end
  
    def Common::url_decode( value ) 
      value.tr( "+", " " )
      return value.to_s.gsub(/%[0-9a-fA-F]{2}/n){ |x| sprintf("%s", x[1..-1].hex.chr) }
    end
  
    def Common::url_encode( s )
      return s.to_s.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
    end
    
    def Common::time2str( time )
      return time.strftime( "%a, %d %b %Y %H:%M:%S GMT" ) # Wed, 09 Feb 1994 22:23:32 GMT
    end
    
    def Common::str2time( time )
      return time ## TODO !!!
    end
    
    def Common::html_escape( s )
      return s.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
    end
  
    def Common::getEnv( s )
      if ENV[s].nil? == false
        tmpenv = String::new( "" )
        tmpenv << ENV[s]
        tmpenv.untaint
        return tmpenv
      else
        return nil
      end
    end
    
    def Common::stateDir
#      tmpdir = (ENV['StateDir'] || '/tmp').untaint
      tmpdir = ( Asp::Common::getEnv( 'StateDir' ) || '/tmp')
      if File::directory?( tmpdir ) == false
        Dir::mkdir( tmpdir, 0700 )
      end
      return tmpdir
    end

    def Common::debug
      return( Asp::Common::getEnv( 'Debug' ) || 1 )
    end
    
    # just for compatibility
    def Common::require( file )
      return file
    end

    def Common::globalASA
      return Asp::Common::getEnv( "Global" )
    end

    def Common::convert( o, d )
      if o.type == nil
        t = String( d.type )
      else
        t = String( o.type )
      end
      case t
        when "Fixnum" 
          return Integer( d )
        when "Bignum"
          return Integer( d )
        when "Integer"
          return Integer( d )
        when "String"
          return String( d )
        when "Array"
          return Array( d )
        when "Hash"
          return Hash( d )
        when "Float"
          return Float( d )
        else
          begin
            eval( "return #{t}( d )" )
          rescue
            return String( d )
          end
      end
    end

    def Common::parseCookie( value )
      values = Hash::new
    
      parts = value.split( /[\&\;]/ )
      parts.each { |v|
        key, value = v.split( /\=/, 2 ).map { |x|
          Asp::Common::url_decode( x )
        }
        values[key] = value
      }
        
      return values
    end

    def Common::redirect
      orig_defout = $stdout
      $stdout = StringIO.new
      frame_start 
      label(:a){  
        yield     
      }
      label(:e) {}
      frame_end
      $stdout.string
    ensure
      $stdout = orig_defout
    end
    # evalstr = 'puts "this is the string"'
    # outputStr = redirect { eval evalstr }
    # p outputStr

  end
end
