# # IOElement Server Library - For Perl Servers # For use with DynAPI IOElement client-side javascript # # The DynAPI Distribution is distributed under the terms of the GNU LGPL license. # # Returned Data type: integer, float, string, date, array, object (associative array) # use strict; # Restrict unsafe variables, references, barewords use CGI; use Carp; #-----------------------------------------------------------# # Public Variables # #-----------------------------------------------------------# my %wso_jsCommands; # stores javascript commands to be executed on client my %wso_vars; # stores javascript variables to be returned to client my $wso_endDocWrite; # Prevent ws__docWrite from sending data to client my $wso_IOResponse; # Returned Content Format: text/html (default) or text/xml my $wso_reqMethod; # stores request method - get,post or upload - upload is use by dynapi when unloading files wso_IOResponse="text/html"; #-----------------------------------------------------------# # Public Methods # #-----------------------------------------------------------# <% /* Add Variables - javascript variables to be sent to client - should only be used when html content is being returned to the client */ function wsAddVariable(name,value){ if(!name) return; wso_vars[name]=value; // add variable to be sent to client }; /* Add JS Command - javascript command to be executed on client - should only be used when html content is being returned to the client */ function wsAddJSCommand(cmd){ wso_jsCommands[wso_jsCommands.length]=cmd; // add js commands to be executed on client }; /* Dispatch Variables - dispatch data to the client - should only be used when html content is being returned to the client */ function wsDispatchVariables() { var i,v,arr=[]; // variables for(i in wso_vars){ v=wso_vars[i] arr[arr.length]="var "+i+"="+ ws__Var2Text(v); } // jscommands for(i=0;i or on the client */ function ws__docWrite(h){ var html; if (wso_endDocWrite) return; if(wso_IOResponse=="text/xml") html=h; else{ html='' } Response.Write(html); }; /* Var2Text - used to convert a server-side variable into a javascript client-side variable */ ws__Var2Text=function(v){ var i,c,data,vtype=typeof(v); if(!v) data='null'; else if(vtype=="number"||vtype=="boolean"||vtype=="function") { data=v; }else if(vtype=="string") { data="'"+ws__Var2TextEncode(v)+"'"; }else if(vtype=="object") { if(v.constructor==Array){ // Array Object data='['; for (i=0;i0)? ','+ws__Var2Text(v[i]):ws__Var2Text(v[i]) } data+=']'; }else if(v.constructor==Date){ // Date object data='Date("'+v+'")' }else{ data='{';c=0; for (i in v){ if(c>0) data+=',' data+='\''+ws__Var2TextEncode(i)+'\':'+ws__Var2Text(v[i]); c++; } data+='}'; } } return data; }; /* Var2Text Encode - converts multiline text into single line javascript */ ws__Var2TextEncode=function (text){ if (!text) return text=text.replace(/\\/g,"\\\\"); // replace \ with \\ text=text.replace(/\'/g,"\\'"); // replace ' with \' text=text.replace(/\r\n/g,"\\n"); // replace CrLf with \n text=text.replace(/\n/g,"\\n"); // replace single Lf with \n text=text.replace(/\r/g,"\\r"); // replace single Cr with \n return text; }; %> # Constructor sub new { my $proto = shift; my $args = shift; my $class = ref($proto) || $proto; my $self = { wso_vars => {}, # stores javascript variables to be returned to client wso_endDocWrite => 0, # Prevent ws__docWrite from sending data to client wso_IOResponse => "text/html", # Returned Content Format: # text/html (default) or text/xml wso_reqMethod => "", # stores request method - get,post or upload - # upload is use by dynapi when unloading files q_obj => new CGI, # Instance of the CGI object, q stands for query q_hash => {} # Hash of form's field names and values }; $self->{q_hash} = $self->{q_obj}->Vars(); return( bless $self, $class ); } sub Version { return $VERSION; } sub q_hash_ref { my $self = shift; return( $self->{q_hash} ); } # Add Variables - javascript variables to be sent to client # # should only be used when html content is being returned to the # client # sub wsAddVariable { my $self = shift; my $name = shift || carp "Not enough args (0 of 2) sent to wsAddVariable: $!"; my $value = shift || carp "Not enough args (1 of 2) sent to wsAddVariable: $!"; return if ( ! ( $name || defined( $name ) ) ); ${ $self->{wso_vars} }{ $name } = $value; # add variable to be sent to client }; # Dispatch Variables - dispatch data to the client # # should only be used when html content is being returned to the # client # sub wsDispatchVariables { my $self = shift; my( @arr ); foreach my $key ( sort keys %{ $self->{wso_vars} } ) { my $val = ${ $self->{wso_vars} }{ $key }; push( @arr , "var " . $key . "=" . $self->ws__Var2Text( $val ) ); } $self->{wso_IOResponse} = ${ $self->{q_hash} }{IOResponse}; $self->ws__docWrite( join( ";\n" , @arr ) ); }; # End Response - prevents ws__docWrite from sending any more information # to the client # sub wsEndResponse { my $self = shift; $self->{wso_endDocWrite} = 1; }; # Get Request - returns a value from the submitted form sub wsGetRequest { my $self = shift; my( $name, $val, $mode ); $name = shift || carp "Not enough args (0 of 1) sent to wsAddVariable: $!"; # Using CGI.pm makes this all superfluous code. #$mode = $self->wsGetRequestMethod(); #$val = ${ $self->{q_hash} }{ $name } if( $mode =~ /post/i ); #$val = ${ $self->{q_hash} }{ $name } if( ! ( $val || defined( $val ) ) ); $val = ${ $self->{q_hash} }{ $name }; return( ( defined( $val ) ) ? $val : "" ); }; sub wsGetRequestMethod { my $self = shift; if ( ! $self->{wso_reqMethod} ) { # used to indicate GET, POST or UPLOAD # - these are use by dynapi on client-side my $rm = ${ $self->{q_hash} }{IOMethod}; if ( ! ( $rm || defined( $rm ) ) ) { $rm = ${ $self->{q_obj} }->request_method(); } $self->{wso_reqMethod} = ( ! $rm ) ? "post" : "\L$rm\E"; } return( $self->{wso_reqMethod} ); }; # -------- [Private] Functions ----------------------------------------- # Doc Write - generates an html page containing JavaScript codes that # will be loaded into an