Best place for making custom includes?
Posted by chris
|
Best place for making custom includes? August 28, 2008 03:48AM |
Registered: 18 years ago Posts: 38 |
Hello,
I started integrating phorum5.2.8 with a custom app. I've previously used the embed phorum mod with no problems but this time i'll build a custom mod to go with the latest stable phorum.
The 'order' of things to do is something like this in my head:
1. before phorum 'runs', include the minimum required files from the parent app, which are basically the parent conf file, and the session handling.
2. a custom phorum mod implementing user_session_restore to inherit the user session (basically already working).
3. custom template header/footer to embed phorum (largely done).
Now this is largely working already in my test (with minor glitches which i'll not mention yet before spending some more time digging things), but I'm not entirely sure what would be the best place to include the files for step #1.
I think i would avoid common.php cause it's ugly and do it in the custom mod too. Maybe using the parse_request hook, or common_pre? Then again i'm thinking of side-effects with the hook approach because the files would be included in a different scope, not global.
Anyway thought it would be a good idea to ask to get a more educated suggestion. :)
Thanks
I started integrating phorum5.2.8 with a custom app. I've previously used the embed phorum mod with no problems but this time i'll build a custom mod to go with the latest stable phorum.
The 'order' of things to do is something like this in my head:
1. before phorum 'runs', include the minimum required files from the parent app, which are basically the parent conf file, and the session handling.
2. a custom phorum mod implementing user_session_restore to inherit the user session (basically already working).
3. custom template header/footer to embed phorum (largely done).
Now this is largely working already in my test (with minor glitches which i'll not mention yet before spending some more time digging things), but I'm not entirely sure what would be the best place to include the files for step #1.
I think i would avoid common.php cause it's ugly and do it in the custom mod too. Maybe using the parse_request hook, or common_pre? Then again i'm thinking of side-effects with the hook approach because the files would be included in a different scope, not global.
Anyway thought it would be a good idea to ask to get a more educated suggestion. :)
Thanks
|
Re: Best place for making custom includes? August 28, 2008 04:26AM |
Admin Registered: 21 years ago Posts: 8,532 |
What is the functionality of the files that you include from the main app? If it is for authentication purposes only, then inside the user_session_restore hook would be fine. If you need it for template support only, then you could even put it at the start of header.tpl. So where to include it, also depends on the functionality I think.
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
|
Re: Best place for making custom includes? August 28, 2008 06:08AM |
Registered: 18 years ago Posts: 38 |
Maurice,
You're right it's hard to say without without knowing the full functionality, it's the session management but more stuff too that i'm doing additionally (eg. custom avatars, integrating some 'blocks' into phorums' templates and so on), but without going into too much unneeded detail i think it's best to rephrase/focus my question(s) to take advantage of your inside-out knowledge of phorum, and avoid making your head explode with how the foreign code functions.
- Making the includes in common.php is the only way to ensure that some values from my configuration will be de initialized in a global scope right? (most are defines, but there are some arrays too, that i need to access from some other function).
I tried including them at the top of my custom module, outside of any function but apparently, the module itself is included within a function.
- If i settle on including the code in common.php i have to make sure that my includes do not produce any output, correct?
I already run into a small glitch, where my included code registered a shutdown function, which simply appended some debugging information at the end of the page. Then i found out this caused problems since common.php is also included in phorums console tools, and files like javascript.php, resulting in a "What's that html doing in phorums javascript code?!" moment :)
Having corrected that now, everything runs like a charm.
Chris
You're right it's hard to say without without knowing the full functionality, it's the session management but more stuff too that i'm doing additionally (eg. custom avatars, integrating some 'blocks' into phorums' templates and so on), but without going into too much unneeded detail i think it's best to rephrase/focus my question(s) to take advantage of your inside-out knowledge of phorum, and avoid making your head explode with how the foreign code functions.
- Making the includes in common.php is the only way to ensure that some values from my configuration will be de initialized in a global scope right? (most are defines, but there are some arrays too, that i need to access from some other function).
I tried including them at the top of my custom module, outside of any function but apparently, the module itself is included within a function.
- If i settle on including the code in common.php i have to make sure that my includes do not produce any output, correct?
I already run into a small glitch, where my included code registered a shutdown function, which simply appended some debugging information at the end of the page. Then i found out this caused problems since common.php is also included in phorums console tools, and files like javascript.php, resulting in a "What's that html doing in phorums javascript code?!" moment :)
Having corrected that now, everything runs like a charm.
Chris
|
Re: Best place for making custom includes? August 28, 2008 06:17AM |
Admin Registered: 21 years ago Posts: 8,532 |
You could do the includes from some early hook as well. Defines are always accessible, so these are no problem. You will however have to add a global statement for making your array variables (and other variables that you need) global. If you do that, then you will have code that can safely be included from within a function. It's what I do with my own web system too and it works very well that way.
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
|
Re: Best place for making custom includes? August 28, 2008 07:39AM |
Registered: 18 years ago Posts: 38 |
Good suggestion, I hadn't thought of that.
Did a quick test including my files with the common_pre() hook and it seems to work well indeed!
Totally unrelated, but i've noticed that my session is inherited with no probs in all phorum pages, except whenever i click on Control Center (control.php), which seems to consistently log me out.
When done i will remove this link completely, but maybe i'm missing something important in the user_session_restore code? Or is there something special-different about control.php ?
[code="php"]
function phorum_mod_integrate_user_session_restore() {
global $user;
if (!isset($user['uid']) || !$user['uid']) {
$phorum_user_id = FALSE;
} else {
$phorum_user_id = $user['uid'];
}
$sessions[PHORUM_SESSION_SHORT_TERM] = $phorum_user_id;
$sessions[PHORUM_SESSION_LONG_TERM] = $phorum_user_id;
$sessions[PHORUM_SESSION_ADMIN] = FALSE;
if ($phorum_user_id) {
$user_data = phorum_api_user_get($phorum_user_id);
if ($user_data && ($user_data['admin'] > 0)) {
$sessions[PHORUM_SESSION_ADMIN] = $phorum_user_id;
}
}
return $sessions;
}
[/code]
Thanks!
Did a quick test including my files with the common_pre() hook and it seems to work well indeed!
Totally unrelated, but i've noticed that my session is inherited with no probs in all phorum pages, except whenever i click on Control Center (control.php), which seems to consistently log me out.
When done i will remove this link completely, but maybe i'm missing something important in the user_session_restore code? Or is there something special-different about control.php ?
[code="php"]
function phorum_mod_integrate_user_session_restore() {
global $user;
if (!isset($user['uid']) || !$user['uid']) {
$phorum_user_id = FALSE;
} else {
$phorum_user_id = $user['uid'];
}
$sessions[PHORUM_SESSION_SHORT_TERM] = $phorum_user_id;
$sessions[PHORUM_SESSION_LONG_TERM] = $phorum_user_id;
$sessions[PHORUM_SESSION_ADMIN] = FALSE;
if ($phorum_user_id) {
$user_data = phorum_api_user_get($phorum_user_id);
if ($user_data && ($user_data['admin'] > 0)) {
$sessions[PHORUM_SESSION_ADMIN] = $phorum_user_id;
}
}
return $sessions;
}
[/code]
Thanks!
|
Re: Best place for making custom includes? August 28, 2008 09:18AM |
Admin Registered: 21 years ago Posts: 8,532 |
The control script is in no way special. But the variable $user is used in that script, so it's most probably overwriting your global variable. This is a variable name space issue and not an issue with your code.
What you could do, is in the common_pre hook, include your code and then to additionally:
[code="php"]
global $my_user;
$my_user =& $user;
[/code]
Then, use $my_user in your integration code instead of $user. Using that variable gives you a much better chance of not running into duplicate variable name usage. The way Phorum tries to prevent this kind of thing from happening, is by storing all important data in the global $PHORUM variable (like $PHORUM['user']). There are no other software packages that I know of that would overwrite the $PHORUM variable. Of course, it's not an easy idea to implement for an already completed system, so you will probably have to revert to the method of using a variable name that doesn't clash with the variables that are used by Phorum.
And a little programming tip for some added programming laziness:
[code="php"]
if (!isset($user['uid']) || !$user['uid']) {
// .....
}
[/code]
can safely be written as:
[code="php"]
if (empty($user['uid'])) {
// ....
}
[/code]
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
What you could do, is in the common_pre hook, include your code and then to additionally:
[code="php"]
global $my_user;
$my_user =& $user;
[/code]
Then, use $my_user in your integration code instead of $user. Using that variable gives you a much better chance of not running into duplicate variable name usage. The way Phorum tries to prevent this kind of thing from happening, is by storing all important data in the global $PHORUM variable (like $PHORUM['user']). There are no other software packages that I know of that would overwrite the $PHORUM variable. Of course, it's not an easy idea to implement for an already completed system, so you will probably have to revert to the method of using a variable name that doesn't clash with the variables that are used by Phorum.
And a little programming tip for some added programming laziness:
[code="php"]
if (!isset($user['uid']) || !$user['uid']) {
// .....
}
[/code]
can safely be written as:
[code="php"]
if (empty($user['uid'])) {
// ....
}
[/code]
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
|
Re: Best place for making custom includes? August 28, 2008 10:57AM |
Registered: 18 years ago Posts: 38 |
Blimey, why is everybody storing user related information in $user. It's a mystery.
Thanks a lot Maurice, everything works well, and if nothing else comes up after some more testing, i think i'm done already.
Yeah my double checking here is probably a side-effect of me always turning error reporting to "E_ALL".
But you're right, empty will not generate a notice if the var is not set for whatever reason. So adopting empty should save a nanosecond. ;) I usually find that the empty command can be a tad unintuitive, depending on context, when reading old code.
Thanks a lot Maurice, everything works well, and if nothing else comes up after some more testing, i think i'm done already.
Yeah my double checking here is probably a side-effect of me always turning error reporting to "E_ALL".
But you're right, empty will not generate a notice if the var is not set for whatever reason. So adopting empty should save a nanosecond. ;) I usually find that the empty command can be a tad unintuitive, depending on context, when reading old code.
|
Re: Best place for making custom includes? August 28, 2008 12:37PM |
Admin Registered: 21 years ago Posts: 8,532 |
Agreed, you need to be aware of the cases in which "empty()" returns TRUE as the result. But it's a nice shorthand if you are aware of it. Sometimes, I encounter code in which it's used to do check for an empty string. Of course that case is broken by misunderstanding the function call. Of course you can decide on your favorite technique ;-)
And good that it's fixed now, with the user variable fumbling. I share the amazement about the use of $user for that purpose. What were they thinking!!??!!!
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
And good that it's fixed now, with the user variable fumbling. I share the amazement about the use of $user for that purpose. What were they thinking!!??!!!
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
Sorry, only registered users may post in this forum.