CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Need help understanding the code...

 



mumick
New User

Sep 29, 2004, 10:25 AM

Post #1 of 1 (809 views)
Need help understanding the code... Can't Post

Hi everyone.
I stuck with a piece of code written in Perl.
The thing is I am trying to use 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. 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. 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%3A%2F%2Fwww.mydomain.com%2Fcgi-bin%2Fmodify.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%3A%2F%2Fwww.mydomain.com/login?andwhatever&whatever, 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 he 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 is www.mydomain.com/cgi-bin) causes a problem I would like my URl to start from build_root_url (which is 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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives