Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Decide if plugin executed on admin interface or website?

Quote Reply
Decide if plugin executed on admin interface or website?
How to decide, if a plugin is executed or called through admin interface or by a website script (page.cgi, add.cgi, etc.)? Is there a variable to check this for?

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Decide if plugin executed on admin interface or website? In reply to
How about checking the $ENV variables? admin.cgi should appear in at least one of them if being run by the admin panel, and not be shown if being called by one of the other scripts. I'm just guessing here, as I have never done/had to do this Tongue

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Decide if plugin executed on admin interface or website? In reply to
Thanks, but is not good.
I need a global variable, that is sure, it was set by the admin.cgi process flow.
Or a function, that can safely tell me, if we are in the admin process, or not.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Decide if plugin executed on admin interface or website? In reply to
Code:
my $is_admin = $0 =~ /admin\.cgi$/ ? 1 : 0;
Quote Reply
Re: [Paul] Decide if plugin executed on admin interface or website? In reply to
Is it a safe way? Can not be hacked in some way, *Any* way?
I do not really trust any data, which comes outside from perl.

I'm sure, there should be another way to check, if we are in the admin process.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Decide if plugin executed on admin interface or website? In reply to
I've just given you a solution, I really don't know what else you are expecting. Larry Wall didn't make a special perl function to check if Links SQL is being run from admin.cgi or not.

As for hacking, I don't know what you mean there either.....what exactly is this code going to be doing?

Last edited by:

Paul: Sep 5, 2002, 4:23 AM
Quote Reply
Re: [webmaster33] Decide if plugin executed on admin interface or website? In reply to
What are you trying to do? That might help for finding a solution....

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [Paul] Decide if plugin executed on admin interface or website? In reply to
Quote:
Larry Wall didn't make a special perl function to check if Links SQL is being run from admin.cgi or not.
Paul, you are very sarcastic again... Pirate
You even not miss a point to make fun with me, right?
Let others decide, if your style is fair or not.

At this point I finished discussing with you.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [yogi] Decide if plugin executed on admin interface or website? In reply to
Thanks, Yogi. You are always fair with others, not like Paul.

I have to execute a plugin function, depending if it is used in admin process, or not.
Basically I do process the plugin, but I do not print out anything if it is executed on website.
If executed in admin process, it will display informative text about what is happening.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Decide if plugin executed on admin interface or website? In reply to
I think Paul's solution is perfectly OK and safe. $0 is the program name which is set by perl.

Anyway, why don't you write a wrapper function for your admin things, in the wrapper function you set a parameter (admin = 1) and then execute the function you really want to execute. Or you write a wrapper function for the user side....

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [webmaster33] Decide if plugin executed on admin interface or website? In reply to
Yep I was sarcastic because I provided a solution that worked and you just rejected it because you don't like taking advice from me.

Quote:
I do not really trust any data, which comes outside from perl.

I'm sure, there should be another way to check, if we are in the admin process.

That quote makes me think you didn't understand what the code even did as $0 is a special perl variable.
Quote Reply
Re: [Paul] Decide if plugin executed on admin interface or website? In reply to
Sorry if you misunderstood.
I did *not rejected* your solution, just wanted to know, if there is another possible solution.
I just wanted an enviroment independent solution. What if I do not call it admin.cgi? What if I do want to change it sometimes? That's what I want to avoid, by environment independent, internal solution.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [yogi] Decide if plugin executed on admin interface or website? In reply to
It happened, that I had to rename my admin.cgi, because somebody tried to hack it. My rule is: it is better to avoid the possibility that hacker can find the admin script and he can try to hack it.
So if I rename admin.cgi, I will have to change all references to "admin.cgi".

In my old modified Links 2.0, I solved it by setting an $admin global at the beginning of admin.cgi, so I was later able to check, if I'm in admin process or not. If we are in admin, display additional info, if not do not display anything, just log into error file if error occured.

That was the logic I used in Links 2.0.
Same should work in Links SQL, too.

1) Solution of Paul works, but anytime later I may run in the problem I described
2) Wrapper function is good idea, but we still need to find out if we are in admin or not.
I do not want to create 2 functions of everything I do (1 for admin, 1 for site), since it would offend against the reusable codes rules.

The truth is, the solution what Andy and Paul suggested is not bad, but I wanted to look for an internal solution if there exists such.
I was also lazy a bit to look for internal solutions in the core code. Wink
But I may have to check it.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Decide if plugin executed on admin interface or website? In reply to
Yes you are right, if you rename admin.cgi then the code won't work so you simply have to edit the regex with the new name. You are still going to have the same problem using an "internal" method. How can Links SQL know whether you are using the admin script or not?

>>
I will have to change all references to "admin.cgi".
<<

No you don't $is_admin would be global.
Quote Reply
Re: [Paul] Decide if plugin executed on admin interface or website? In reply to
Quote:
You are still going to have the same problem using an "internal" method. How can Links SQL know whether you are using the admin script or not?
I checked a bit into admin.cgi and I see just one solution:
- to ask Alex, to add $ADMIN variable within sub main or just before calling main function (this is how I solved it in Links 2.0).
I simply do not see other solution.

Quote:
No you don't $is_admin would be global.
Yes, I understood.
But $is_admin could be only defined, right at the start of a plugin, not earlier (the best would be in admin.cgi, but due to plugin system this is not possible of course). So all plugins will have the /admin\.cgi$/ hard coded text. Therefore I will have to change all references in those plugins, which are used on both admin and site pages.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Decide if plugin executed on admin interface or website? In reply to
Hi,

You are better off seperating in your code the entry point for an admin function and for a user function. This makes it clear in the code that it is for the admin only.

Alternatively, you could do if ($ENV{REMOTE_USER}) as that means the user has authenticated via .htaccess.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Decide if plugin executed on admin interface or website? In reply to
Alex, I see your point. I also prefer to separate admin functions from user options is possible.
But in this case, if I want to have separate admin and user function, I have to duplicate the code, almost the same code (1-5% difference), except the admin has a few prints to display informational text on admin interface.

So, I have to maintain 2, almost the same (1-5% difference) functions, and I think this is infringement of the reusable code rule. I think, I should not duplicate the same code, because it will make my life difficulter.

Would cause any problem, if you would add a global variable ($admin = 1;), right at the start of admin.cgi?

I collected some advantages, disadvantages of that change:
- Disadvantage:
. . - takes a bit more namespace,
. . - and may need to wrap the admin part in a package (but not necessarily, I think)
- Advantage:
. . - at any point in the code flow, we can know if we are on the admin interface or we are displaying the site pages.
. . - makes possible more flexible coding, by avoiding so stick to the admin.cgi name,
. . - makes possible to avoid duplicating some codes

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Decide if plugin executed on admin interface or website? In reply to
Quote:
But in this case, if I want to have separate admin and user function, I have to duplicate the code, almost the same code (1-5% difference), except the admin has a few prints to display informational text on admin interface.


I'm not sure what you are doing, but surely:

sub modify {
my $am_admin = shift;
# your code here
}
sub admin_modify {
modify(1);
}
sub user_modify {
modify(0);
}

could accomplish what you want.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Decide if plugin executed on admin interface or website? In reply to
This should explain, what I mean:
Code:
sub modify {
my $modify = call_modify_function(); # returns 1 if was successful
if ($is_admin) {
print "This executed in admin";
print "The modify function was called successfully";
}
}

So I have to decide if we are in admin or not.
If we are, then print info on admin interface, otherwise nothing, because we display the webpage.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Decide if plugin executed on admin interface or website? In reply to
Hmm, how about:

sub admin_modify {
my $modify = call_modify_function(); # returns 1 if was successful
print "This executed in admin";
print "The modify function was called successfully";
}

sub user_modify {
my $modify = call_modify_function(); # returns 1 if was successful
}

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Decide if plugin executed on admin interface or website? In reply to
Alex, not this is the problem, but the decision to find, if we are on admin interface.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...