Solved

Need help understanding a piece of Perl code...

Posted on 2004-09-29
14
551 Views
Last Modified: 2013-12-25
Hi everyone.
I stuck with a piece of code written in Perl.
I didn't wrote it but I am trying to modify it and it doesn't work the way I want it to be.
The thing is I am using Apache mod rewrite to show user friendly URL in the browser hence I have a clean reference in .htaccess for each file in my cgi-bin directory (I have ten files in cgi-bin directory) The only thing which doesn't want to work the way I want is the option when the user wants to modify/add the content on the page. He clicks on modify/add link (the URL looks like this when user wants to access modify/add link directly without clicking on the login link first http://www.mydomain.com/login?url=http//:www.mydomain.com/cgi-bin/modify.cgi&from=modify) and it takes him to the login screen where after he gets authorized he gets redirected back to the page he requested in this case it’s modify.cgi.
By following this procedure user always goes directly to my cgi-bin directory rather then to the clean URL I specified in my .htaccess file and I don't know how to fix it. The thing is when user goes directly to the login screen by clicking on the login link rather then on modify/add link he gets to the URL which I specified in .htaccess. I want my user to get this kind of link when he clicks on add/modify link http://www.mydomain.com/login?url=http//:www.mydomain.com/login&from=whateverpage, so /login can be preserved in the url variable. Below is the piece of code that I think is responsible for redirecting user from login page to the page user requested.

sub user_page {
# ------------------------------------------------------------------
# Load a user template and return it.
#
(defined $_[0] and $_[0] eq 'Links') and shift;
my ($file, $vars, $opts) = @_;

# Replace $vars with hash if it is a CGI object.
(ref $vars eq 'GT::CGI') and ($vars = $vars->get_hash);

# Figure out what template set we will use.
my $template_set = delete $opts->{template};
$template_set ||= $IN->param('t') || $CFG->{build_default_tpl};

if ($template_set eq 'admin' or $template_set eq 'help') {
$template_set = $CFG->{build_default_tpl} || 'default';
}
unless (($template_set =~ /^\w+\.?\w*$/) and (-e "$CFG->{admin_root_path}/templates/$template_set")) {
$template_set = $CFG->{build_default_tpl} || 'default';
}
unless ((ref $opts and exists $opts->{string}) or (($file =~ /^[\w\-]+\.\w+$/) and (-e "$CFG->{admin_root_path}/templates/$template_set/$file" or -e "$CFG->{admin_root_path}/templates/$template_set/local/$file"))) {
die "Invalid template name: $file";
}

# Load our global variables.
$GLOBALS ||= GT::Config->load ("$CFG->{admin_root_path}/templates/$template_set/globals.txt", { inheritance => 1, compile_subs => 'Links', local => 1, cache => 1 });

# Add in selected config options.
foreach (qw/db_cgi_url build_root_url build_images_url build_new_url build_ratings_url build_cool_url/) {
exists $vars->{$_} or ($vars->{$_} = $CFG->{$_});
}

# Parse and return the template, turn off compression for .txt templates.
my $dynamic = delete $opts->{dynamic};
$opts ||= {};
$opts->{root} ||= "$CFG->{admin_root_path}/templates/$template_set";
$opts->{compress} ||= ($file =~ /txt$/) ? 0 : $CFG->{compress};
$opts->{print} ||= 0;
my $output = GT::Template->parse ( $file, [$IN, $GLOBALS, $USER || (), $vars], $opts );
if ($dynamic and ! $opts->{print} and $IN->param('d')) {
clean_output(\$output);
}
return $output;
}

sub clean_output {
# --------------------------------------------------------
# Cleans up the output for any dynamically generated pages, makes sure all
# the links go through the page.cgi script.
#
my $output_ref = shift;

# Quit if we are not using dynamic pages.
return unless $CFG->{dynamic_pages};

my $original = '';
my $form = '';
my $in_hash = $IN->get_hash (0); # Don't concat multiple values.

# Build a query string.
foreach (@{$CFG->{dynamic_preserve}}) {
next if (! defined $in_hash->{$_} or ($in_hash->{$_} =~ /^\s*$/));
$original .= $IN->escape($_) . "=" . $IN->escape($in_hash->{$_}) . "&";
$form .= "<input type=hidden name='" . $IN->escape($_) . "' value='" . $IN->escape($in_hash->{$_}) . "'>\n";
}
chop $original;

# Fix up the CGI references.
my ($begin, $url, $output, $qs);
$$output_ref =~ s#(<a[^>]+href\s*=\s*["']*)$CFG->{db_cgi_url}/([^"'>]+\.(?:cgi|pl))\??([^"'>]*)#
($begin, $url, $qs) = ($1, $2, $3);
if ($original) {
if ($qs) {
$output = "$begin$CFG->{db_cgi_url}/$url?$qs";
foreach my $key (@{$CFG->{dynamic_preserve}}) {
next if (! defined $in_hash->{$key} or ($in_hash->{$key} =~ /^\s*$/));
unless ($qs =~ /(?:\A|[&;])$key=/) {
$output .= "&$key=" . $IN->escape($in_hash->{$key});
}
}
}
else {
$output = "$begin$CFG->{db_cgi_url}/$url?$original";
}
}
else { $output = "$begin$CFG->{db_cgi_url}/$url"; }
$output;
#eisog;

# Fix up any HTML forms, and insert hidden tags.
$$output_ref =~ s!(<form[^>]+>)!$1$form!isg;

# Fix up the HTML references
$$output_ref =~ s!(<a[^>]+href\s*=\s*["']*)$CFG->{db_cgi_url}/?([^"'>]*)!
($begin, $url) = ($1, $2);
if ($url =~ /\.(jpe?g|gif|cgi|pl)/) {
$output = "$begin$CFG->{db_cgi_url}/$url";
}
else {
($original) ? ($output = "$1$CFG->{build_root_url}/page?g=" . $IN->escape($2) . "&$original") : ($output = "$1$CFG->{build_root_url}/page?g=" . $IN->escape($2));
}
$output;
!eisog;
}

sub redirect_login_url {
# --------------------------------------------------------------
# Redirects a user to the login screen, through a plugin.
#
my $from = shift;
GT::Plugins->dispatch ( $CFG->{admin_root_path} . '/Plugins', 'auth_redirect_login', \&_redirect_login_url, $from );
}

sub _redirect_login_url {
# --------------------------------------------------------------
# Redirect the user to the login screen.
#
my ($from) = shift;
my $url = $IN->url ( query_string => 1 );
$url = $IN->escape ($CFG->{db_cgi_url} . '/' . $url);
$url = $CFG->{build_root_url} . "/login?url=$url&from=$from";
foreach my $preserve (@{$CFG->{dynamic_preserve}}) {
my $val = $IN->param($preserve);
defined $val or next;
$url .= "&$preserve=" . $IN->escape($val);
}
return $url;
}
I believe db_cgi_url (which holds the paht to my cgi-bin directory which is www.mydomain.com/cgi-bin) causes a problem I would like my URL to start from build_root_url (which holds the path to my domain www.mydomain.com) so I can use it as www.mydomain.com/login?url=http://www.mydomain.com/login?from=modify
If you guys can help I would appreciate it.
Thanks in advance, I need this to work badly and I am giving alot of points for it so help me out
0
Comment
Question by:mumick
  • 7
  • 6
14 Comments
 
LVL 8

Expert Comment

by:inq123
ID: 12183930
Hi mumick,

It is a bit difficult to understand exactly what you wanted to do.  You probably need to spell things out clearer and/or mark the section that you need help on out if you need help to understand it.

A suggestion is that no offense but although everyone's willing to help you get things working, a line like "I am giving alot of points for it so help me out" is probably not going to draw much help (if you see how many 500-point questions are before yours and consider the fact that you abandoned your previous and only (other) question without awarding points).

Regardless, if you clean the question up to some extent, I'm sure many would like to help you out.

Cheers!
0
 

Author Comment

by:mumick
ID: 12184490
Ok, I'll start over.
I have a program which is written in Perl, I didn't write it so I am not going to give allot info about it but I can share some pieces of code with you guys to understand and solve my problem.
I have ten files in my cgi-bin directory which is in www.mydomain.com/cgi-bin (files are add.cgi, modify.cgi and so on...). For each file in my cgi-bin directory I have a reference in my .htaccess file (e.g. original path to add.cgi is www.mydomain.com/cgi-bin/add.cgi, in .htaccess I overwrite it with www.mydomain.com/add which makes url very easy to remember and to enter if user wants to go there directly). Also for user to add something to my site he has to create an account or login with the username and the password. If user goes to the login page directly by clicking on the Login link he gets this url in the browser www.mydomain.com/login which is a reference to the original path which is www.mydomain.com/cgi-bin/login.cgi and he can be processed there without revealing original path to the cgi-bin, but if the user is not logged in yet and wants to add something and clicks on the Add link he gets redirected to the login page with url www.mydomain.com/login?url=http://www.mydomain.com/cgi-bin/add.cgi&from=add  the query string here you can see is the page which user requested and after he gets authorized he will be redirected to. My problem is I want that query string or correctly say url variable in the query string to point to the path which was set in .htaccess file which is for add.cgi is www.mydomain.com/add and the url should look like www.mydomain.com/login?url=http://www.mydomain.com/add&from=add instead of www.mydomain.com/login?url=http://www.mydomain.com/cgi-bin/add.cgi&from=add
Also I noticed that beside the query string my login page has hidden fields in the form one of the holds the path to the file the user requested so I need to change it too.

Below is the piece of code that is responsible for getting the query string and creating a hidden field so I want you guys help me figure out how to fix it so my query has the right url and the hidden field has the right url in it.

to understand some variables I give the expiation of them
db_cgi_url is the path to my cgi-bin directory which is www.mydomain.com/cgi-bin
build_root_url is the path to my domain which is www.mydomain.com


sub user_page {
# ------------------------------------------------------------------
# Load a user template and return it.
#
(defined $_[0] and $_[0] eq 'Links') and shift;
my ($file, $vars, $opts) = @_;

# Replace $vars with hash if it is a CGI object.
(ref $vars eq 'GT::CGI') and ($vars = $vars->get_hash);

# Figure out what template set we will use.
my $template_set = delete $opts->{template};
$template_set ||= $IN->param('t') || $CFG->{build_default_tpl};

if ($template_set eq 'admin' or $template_set eq 'help') {
$template_set = $CFG->{build_default_tpl} || 'default';
}
unless (($template_set =~ /^\w+\.?\w*$/) and (-e "$CFG->{admin_root_path}/templates/$template_set")) {
$template_set = $CFG->{build_default_tpl} || 'default';
}
unless ((ref $opts and exists $opts->{string}) or (($file =~ /^[\w\-]+\.\w+$/) and (-e "$CFG->{admin_root_path}/templates/$template_set/$file" or -e "$CFG->{admin_root_path}/templates/$template_set/local/$file"))) {
die "Invalid template name: $file";
}

# Load our global variables.
$GLOBALS ||= GT::Config->load ("$CFG->{admin_root_path}/templates/$template_set/globals.txt", { inheritance => 1, compile_subs => 'Links', local => 1, cache => 1 });

# Add in selected config options.
foreach (qw/db_cgi_url build_root_url build_images_url build_new_url build_ratings_url build_cool_url/) {
exists $vars->{$_} or ($vars->{$_} = $CFG->{$_});
}

# Parse and return the template, turn off compression for .txt templates.
my $dynamic = delete $opts->{dynamic};
$opts ||= {};
$opts->{root} ||= "$CFG->{admin_root_path}/templates/$template_set";
$opts->{compress} ||= ($file =~ /txt$/) ? 0 : $CFG->{compress};
$opts->{print} ||= 0;
my $output = GT::Template->parse ( $file, [$IN, $GLOBALS, $USER || (), $vars], $opts );
if ($dynamic and ! $opts->{print} and $IN->param('d')) {
clean_output(\$output);
}
return $output;
}

sub clean_output {
# --------------------------------------------------------
# Cleans up the output for any dynamically generated pages, makes sure all
# the links go through the page.cgi script.
#
my $output_ref = shift;

# Quit if we are not using dynamic pages.
return unless $CFG->{dynamic_pages};

my $original = '';
my $form = '';
my $in_hash = $IN->get_hash (0); # Don't concat multiple values.

# Build a query string.
foreach (@{$CFG->{dynamic_preserve}}) {
next if (! defined $in_hash->{$_} or ($in_hash->{$_} =~ /^\s*$/));
$original .= $IN->escape($_) . "=" . $IN->escape($in_hash->{$_}) . "&";
$form .= "<input type=hidden name='" . $IN->escape($_) . "' value='" . $IN->escape($in_hash->{$_}) . "'>\n"; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! I BELIVE THIS LINE CREATES HIDDEN VALUE FOR THE REQUESTED PAGE WHERE USER WILL BE REDIRECTED AFTER HE LOGS IN!!!!!!!!!!!
}
chop $original;


# Fix up the CGI references.
my ($begin, $url, $output, $qs);
$$output_ref =~ s#(<a[^>]+href\s*=\s*["']*)$CFG->{db_cgi_url}/([^"'>]+\.(?:cgi|pl))\??([^"'>]*)#
($begin, $url, $qs) = ($1, $2, $3);
if ($original) {
if ($qs) {
$output = "$begin$CFG->{db_cgi_url}/$url?$qs";
foreach my $key (@{$CFG->{dynamic_preserve}}) {
next if (! defined $in_hash->{$key} or ($in_hash->{$key} =~ /^\s*$/));
unless ($qs =~ /(?:\A|[&;])$key=/) {
$output .= "&$key=" . $IN->escape($in_hash->{$key});
}
}
}
else {
$output = "$begin$CFG->{db_cgi_url}/$url?$original";
}
}
else { $output = "$begin$CFG->{db_cgi_url}/$url"; }
$output;
#eisog;

# Fix up any HTML forms, and insert hidden tags.
$$output_ref =~ s!(<form[^>]+>)!$1$form!isg;

# Fix up the HTML references
$$output_ref =~ s!(<a[^>]+href\s*=\s*["']*)$CFG->{db_cgi_url}/?([^"'>]*)!
($begin, $url) = ($1, $2);
if ($url =~ /\.(jpe?g|gif|cgi|pl)/) {
$output = "$begin$CFG->{db_cgi_url}/$url";
}
else {
($original) ? ($output = "$1$CFG->{build_root_url}/page?g=" . $IN->escape($2) . "&$original") : ($output = "$1$CFG->{build_root_url}/page?g=" . $IN->escape($2));
}
$output;
!eisog;
}

sub redirect_login_url {
# --------------------------------------------------------------
# Redirects a user to the login screen, through a plugin.
#
my $from = shift;
GT::Plugins->dispatch ( $CFG->{admin_root_path} . '/Plugins', 'auth_redirect_login', \&_redirect_login_url, $from );
}

sub _redirect_login_url {
# --------------------------------------------------------------
# Redirect the user to the login screen.
#
my ($from) = shift;
my $url = $IN->url ( query_string => 1 );
$url = $IN->escape ($CFG->{db_cgi_url} . '/' . $url);
$url = $CFG->{build_root_url} . "/login?url=$url&from=$from";
foreach my $preserve (@{$CFG->{dynamic_preserve}}) {
my $val = $IN->param($preserve);
defined $val or next;
$url .= "&$preserve=" . $IN->escape($val);
}
return $url;
}



   
That's what I have. If need more information I would be happy to give you. I don't know what else to describe, as I said if you have question I can provide you all the information you need. Thanks again for looking I hope we going to solve it soon.
0
 
LVL 5

Expert Comment

by:ITcrow
ID: 12187143
I would say to begin with, look at the documentation of mod_rewrite

http://httpd.apache.org/docs/mod/mod_rewrite.html

Rewrite condition can be a regex of any kind.

You can have something as simple as the following in .htaccess:

RewriteRule   /cgi-bin/add.cgi  /add

Let's do it in steps and see what level of success you get. Post back your observations.
0
 
LVL 8

Expert Comment

by:inq123
ID: 12190658
OK, now I understand your question.  So for your first question:
1. How to redirect using www.mydomain.com/login?url=http://www.mydomain.com/add&from=add instead of www.mydomain.com/login?url=http://www.mydomain.com/cgi-bin/add.cgi&from=add ?
Answer: This step is done in "sub _redirect_login_url".  What you'll need to do is to build a hash that looks like your .htaccess file, basically something like
my %hash = ("http://www.mydomain.com/cgi-bin/add.cgi" => "http://www.mydomain.com/add", "http://www.mydomain.com/cgi-bin/login.cgi" => "http://www.mydomain.com/login", .... etc. etc.);

Then change the statement ($url = $IN->escape ($CFG->{db_cgi_url} . '/' . $url); ) in "sub _redirect_login_url" to:

$url = $hash{$IN->escape ($CFG->{db_cgi_url} . '/' . $url)};

This'll fix the first problem.

2. How to make the same change for a hidden variable?
Same principal as above, just that you fix it in the statement that you marked out yourself.  Just replace "$IN->escape($in_hash->{$_})" to
(($hash{$IN->escape($in_hash->{$_})})? $hash{$IN->escape($in_hash->{$_})} : $IN->escape($in_hash->{$_}))

Last comments: this is one messy piece of code; and I wouldn't want to use the URL to code the user request URL which defeats the purpose of pretty URLs rewritten by url_rewrite.  But it's fixed, maybe we'll just leave it there.
0
 

Author Comment

by:mumick
ID: 12191180
My server givess me an internal error. I don't know why I followed all suggestions you provided. I am so unlucky. Maybe I made a syntax error maybe something else I don't know.
This is what I have done to sub _redirect_login_url

sub _redirect_login_url {
# --------------------------------------------------------------
# Redirect the user to the login screen.
#
    my ($from) = shift;
    my %hash = ("http://www.mydomain.com/cgi-bin/add.cgi" => "http://www.mydomain.com/add", "http://www.mydomain.com/cgi-bin/modify.cgi" => "http://www.mydomain.com/modify", "http://www.mydomain.com/cgi-bin/login.cgi" => "http://www.mydomain.com/login");
    my $url = $IN->url ( query_string => 1 );
    $url = $hash{$IN->escape ($CFG->{db_cgi_url} . '/' . $url)};
    $url    = $CFG->{build_root_url} . "/login?url=$url&from=$from";
    foreach my $preserve (@{$CFG->{dynamic_preserve}}) {
        my $val = $IN->param($preserve);
        defined $val or next;
        $url .= "&$preserve=" . $IN->escape($val);
    }
    return $url;
}

This is what I have done to build a query string for hidden values

# Build a query string.
    foreach (@{$CFG->{dynamic_preserve}}) {
        next if (! defined $in_hash->{$_} or ($in_hash->{$_} =~ /^\s*$/));
        $original .= $IN->escape($_) . "=" . $IN->escape($in_hash->{$_}) . "&";
        $form     .= "<input type=hidden name='" . $IN->escape($_) . "' value='" . (($hash{$IN->escape($in_hash->{$_})})? $hash{$IN->escape($in_hash->{$_})} : $IN->escape($in_hash->{$_})) . "'>\n";
    }  
    chop $original;


Maybe you have some other thoughts on it. I would very appreciate it.
0
 
LVL 8

Expert Comment

by:inq123
ID: 12191792
The problem is that you made %hash local to _redirect_login_url, I actually meant you make my %hash globally available.  Namely, put "my %hash = ..." at beginning of your program.

I believe that's why you got internal error.  If this fix doesn't work, then you need to find your web server's log file and tell me what's in the error log.
0
 

Author Comment

by:mumick
ID: 12192221
I've changed everything as you said. The "my %hash=..." variable is at the top of my code.  Now there are no errors but the problem is my url variable which is in the query string is empty, so if you click on Add link directly you'll get this URL in your browser http://www.mydomain.com/login?url=&from=add    and also my hidden field disappeared. And if user loggs in he gets just welcome page rather then be redirected to the page he requested before. I believe we half way there but we still have to change something else for the code to work properly.  
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:inq123
ID: 12192492
if these fields are empty that means those URLs are not in your %hash.  Most common reason for it is that the case of the strings are different.  But regardless of the reason, it's VERY easy to debug these problems:

Whenever you do a $url = $hash{$someurl}, you first do a print "url befpre: $someurl<br>\n";
then after replacement you do a print "url after: $url<br>\n";  Then just check the url before and find out why this $someurl is not in your %hash.  Then just make the necessary adjustment in your %hash.  If it's a problem of case, then just do $url = $hash{lower($someurl)};  And use all lower-case string as keys in %hash.
0
 

Author Comment

by:mumick
ID: 12193094
It's all Greek to me. If you could explain me where to put thos lines and what does $someurl variable stand for in my code I would greatly appreaciate it. What I have done so far was I added couple lines to my  _redirect_login_url

sub _redirect_login_url {
# --------------------------------------------------------------
# Redirect the user to the login screen.
#
    my ($from) = shift;
    my $url = $IN->url ( query_string => 1 );      
      print "url before: $url<br>\n";                  #<-----------------------------------I addes this line         
      $url = $hash{$IN->escape ($CFG->{db_cgi_url} . '/' . $url)};
      print "url after: $url<br>\n";                     #<-----------------------------------I added this line
    $url    = $CFG->{build_root_url} . "/login?url=$url&from=$from";
    foreach my $preserve (@{$CFG->{dynamic_preserve}}) {
        my $val = $IN->param($preserve);
        defined $val or next;
        $url .= "&$preserve=" . $IN->escape($val);
    }
    return $url;
}

After I did it, I didn't get anything printed on the page. May be I am doing something wrong.
The thing is I suck in perl if it were ASP I would deffenetly understand you but here it just sucks so if you don't mind showing me the exact place where I should put those lines it would be great.
0
 
LVL 8

Expert Comment

by:inq123
ID: 12193333
mumick,

In that case, change your redirect function to:

sub _redirect_login_url {
    my ($from) = shift;
    my $url = $IN->url ( query_string => 1 );
    $url = $IN->escape ($CFG->{db_cgi_url} . '/' . $url);
    $url    = $CFG->{build_root_url} . "/login?url=$url&newurl=$hash{$url}&from=$from";
    foreach my $preserve (@{$CFG->{dynamic_preserve}}) {
        my $val = $IN->param($preserve);
        defined $val or next;
        $url .= "&$preserve=" . $IN->escape($val);
    }
    return $url;
}

This way you'd be able to see on the URL string the old URL (in url=...) and the new URL using %hash (in newurl=...), and by looking at the old URL, say it's "http://www.mydomain.com/cgi-bin/Add.cgi", then you'd have to change your %hash to my %hash = ("http://www.mydomain.com/cgi-bin/Add.cgi" => "http://www.mydomain.com/add", "http://www.mydomain.com/cgi-bin/modify.cgi" => "http://www.mydomain.com/modify", "http://www.mydomain.com/cgi-bin/login.cgi" => "http://www.mydomain.com/login");

Basically a hash is a map that associates keys with values.  In your %hash, the "http://www.mydomain.com/cgi-bin/Add.cgi", "http://www.mydomain.com/cgi-bin/modify.cgi", "http://www.mydomain.com/cgi-bin/login.cgi" are keys, and "http://www.mydomain.com/add", "http://www.mydomain.com/modify", "http://www.mydomain.com/login" are values.  $hash{$key} would return the $value associated with the given $key.  And keys are case-sensitive.  That's why you need to inspect the case of your old URLs and make sure the keys in your %hash match them.

Another possibility is that for example your old URL is simply different, it might be "www.mydomain.com/cgi-bin/add.cgi", then you need to get rid of "http://" in your %hash keys.  Regardless, the point is, whatever your old URL is, you need to make your %hash keys match it.  Otherwise you won't be able to make a translation to new simplified URLs that you want.

If the problem is purely due to mismatched case, then a safe way is to change to:

sub _redirect_login_url {
    my ($from) = shift;
    my $url = $IN->url ( query_string => 1 );
    $url = $hash{lower($IN->escape ($CFG->{db_cgi_url} . '/' . $url))};
    $url    = $CFG->{build_root_url} . "/login?url=$url&from=$from";
    foreach my $preserve (@{$CFG->{dynamic_preserve}}) {
        my $val = $IN->param($preserve);
        defined $val or next;
        $url .= "&$preserve=" . $IN->escape($val);
    }
    return $url;
}

function lower($string) makes $string to be all lower-case, and then you make sure all your %hash keys are lower-cased.  Then they have to match.
0
 

Author Comment

by:mumick
ID: 12194089
I did everything you said and it works fine with the defined set of URLs in the hash file like www.mydomain.com/add, www.mydomain.com/modify, www.mydomain.com/rate, www.mydomain.com/login  but if something else is being appended to URL like ID or Category and it happens dynamically (e.g. www.mydomain.com/rate?ID=14&Category=Business) then user is redirected to the login screen and the $url variable in the query string is empty because the program didn't find the match of that string in the hash file and it is also impossible for me to enter all possiable URLs. Is there a way to somehow define base files like /add /modify /rate /login and then it doesn't matter what you append to the end of those you still be able to get them in the hidden field and in the query string variable $url
You see this website is designed to manage a collection of links and it serves as a directory to find local businesses, colleges, shopping centers and so on. Visitor to this website will not go directly to the login page because he came to get information and then after browsing a web if he decides to rate the link or review the link he will just click on those links but not on the Login link that's why I need it to be very flexible in terms of appending any variables to the file and it will work the way it used to work with the old file structure.
Right now my sub _redirect_login_url looks like this

my ($from) = shift;
    my $url = $IN->url ( query_string => 1 );
    $url = $IN->escape ($CFG->{db_cgi_url} . '/' . $url);
    $url    = $CFG->{build_root_url} . "/login?url=$hash{$url}&from=$from";   #<--------------I have changed this line instead of url=$url I put url=$hash{$url} and it's working but as I said only with the files that are defined in the hash file
    foreach my $preserve (@{$CFG->{dynamic_preserve}}) {
        my $val = $IN->param($preserve);
        defined $val or next;
        $url .= "&$preserve=" . $IN->escape($val);
    }
    return $url;

I believe we are getting close but we have to change something else.


0
 
LVL 8

Accepted Solution

by:
inq123 earned 400 total points
ID: 12194195
That's easy and it's actually a common language construct available in most scripting/high level languages (using (boolean)? a : b;).  But I used the perl way below, although it's the same concept:

my ($from) = shift;
    my $url = $IN->url ( query_string => 1 );
    $url = $IN->escape ($CFG->{db_cgi_url} . '/' . $url);
    $url = $hash{$url} if($hash{$url}); // use hash{url} only when it exists, otherwise let old url stay
    $url    = $CFG->{build_root_url} . "/login?url=$url&from=$from";
    foreach my $preserve (@{$CFG->{dynamic_preserve}}) {
        my $val = $IN->param($preserve);
        defined $val or next;
        $url .= "&$preserve=" . $IN->escape($val);
    }
    return $url;
0
 

Author Comment

by:mumick
ID: 12194618
I did what you said. I replaced my old sub _redirect_login_url which was

 my ($from) = shift;
    my $url = $IN->url ( query_string => 1 );
    $url = $IN->escape ($CFG->{db_cgi_url} . '/' . $url);
    $url    = $CFG->{build_root_url} . "/login?url=$hash{$url}&from=$from";
    foreach my $preserve (@{$CFG->{dynamic_preserve}}) {
        my $val = $IN->param($preserve);
        defined $val or next;
        $url .= "&$preserve=" . $IN->escape($val);
    }
    return $url;
}

with the one below

my ($from) = shift;
    my $url = $IN->url ( query_string => 1 );
      $url = $hash{$url} if($hash{$url});                                
    $url = $CFG->{build_root_url} . "/login?url=$url&from=$from";
    foreach my $preserve (@{$CFG->{dynamic_preserve}}) {
        my $val = $IN->param($preserve);
        defined $val or next;
        $url .= "&$preserve=" . $IN->escape($val);
    }
    return $url;
}


Now what I'am getting in a query string and in the hidden field is

http://www.mydomain.com/login?url=rate.cgi?ID=32&from=rate

and in the hidden field

<input value="rate.cgi?ID=32" name="url">

My main goal is to have script executing without revealing the original location of files, so if I have my rate.cgi in www.mydomain.com/cgi-bin I want my visitors to see it in the main folder of my domain like www.mydomain.com/rate all the time even something is appended to it or not that's my main goal and have my hidden files to set the same way
0
 

Author Comment

by:mumick
ID: 12194699
I want to to add one more thing Is it clear for you what I want and is it possiable to perform it without making huge changes to the code?
Maybe it's easy to send you a code of this particular file then I am willing to give you 1000 points if you'll be able to solve my problem the way I want
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now