Reputation: 383
Resume :
I (php beginner) am having a problem where a global variable
is not getting defined in a specific include gator::display("main_filelist.php", $params)
that seems to be a function. Others includes on the same don't have this problem. //
I'm using a File Manager called FileGator on my website.
In this File Manager there is file called main.php
which is the page every users are on when they want to navigate through the folders or upload/download/manipulate files.
Now this main.php
file includes several others php files (for configurations, templates, tables...).
Also in this main.php
file, at the very beggining of the file, I have a function that defines a global variable called $getprivileges.
part of main.php
:
<!-- Determine User's permissions on current directory -->
<?php
$directory = $_SESSION['cwd']; // current directory
$user = $_SESSION['simple_auth']['username']; // get username
$repository = gatorconf::get('repository'); // get base repertory of the file manager
$userdir = $repository.DS.'user'.DS.$user; // user's repertory
$getprivileges = scanDirectory($userdir, $directory);
function scanDirectory($userdir = '', $directory){
$folders = glob($userdir . '/*' , GLOB_ONLYDIR);
foreach($folders as $folder){
if (($folder == $directory && gator::checkPermissions('r')) || (gator::checkPermissions('ru')) || ($userdir == $directory && gator::checkPermissions('r'))) {
return true;
}
$scan_result = scanDirectory($folder, $directory);
if($scan_result) {
return true;
}
}
return false;
}
scanDirectory($userdir, $directory);
if ((gator::checkPermissions('ru')) || ($userdir == $directory && gator::checkPermissions('r'))) {
$getprivileges = true;
}
?>
...code
...more code
<?php gator::display("main_filelist.php", $params)?>
...code again
Since my variable $getprivileges
is global and located at the top of the file it is supposed to be taken into account by every others includes in this page, and it does for every of the includes exept one.
FileGator doesn't includes his files the classic way it looks since the include that causes me troubles has this shape : <?php gator::display("main_filelist.php", $params)?>
After some digging to find out the meaning of this gibberish (note that FileGator doesn't provide advanced support any longer and that the ressources of the FAQ are limited in my problem) here are the informations I could gather to try solving this :
gator::display()
looks to be a function defined in a file named file-gator.php, which is also the page that generates my main.php
file (for instance if I am not logged in it can displays login.php instead).
Here is what I could find about gator::display()
in this file-gator.php
:
<?php>
class gator {
public static function display($view, $params = null){
require_once gatorconf::get('base_path')."/include/views/".$view;
}
}
<?>
And here is a part of main_filelist.php
so you can see what $params is used for :
<table class="file-list">
<?php $i = 1;?>
<tbody>
<?php if(!empty($params['dirs'])) foreach ($params['dirs'] as $file):?>
<?php if (($file['name'] == "user") && ($_SESSION['cwd'] == gatorconf::get('repository'))):?>
<?php else:?>
<tr class="directory <?php if ($file['type'] == "back") echo 'back-button';?>">
<?php if ($getprivileges == true): ?>
<?php if ($file['type'] != 'back' && $file['type'] != 'user'):?>
<td class="chkboxes"><input type="checkbox" name="<?php echo $i++;?>" value="<?php echo $file['crypt']?>" /></td>
<?php elseif ($file['type'] == 'user'):?>
<td class="chkboxes"><a class="user" href="?cd=<?php echo $file['link']?>"></a></td>
<?php else:?>
<td class="chkboxes"><a class="back" href="?cd=<?php echo $file['link']?>"></a></td>
<?php endif;?>
<?php else:?>
<?php if ($file['type'] != 'back' && $file['type'] != 'user'):?>
<td class="chkboxes"><a class="folder" href="?cd=<?php echo $file['link']?>"></a></td>
<?php elseif ($file['type'] == 'user'):?>
<td class="chkboxes"><a class="user" href="?cd=<?php echo $file['link']?>"></a></td>
<?php else:?>
<td class="chkboxes"><a class="back" href="?cd=<?php echo $file['link']?>"></a></td>
<?php endif;?>
<?php endif;?>
So it looks like the file main_filelist.php included on my main.php is included through a require_once function (gatorconf::get('base_path')
is just the installation path of the file manager) exept this require_once function
is apparently inside the function public static function display
with a variable $param
on top of the require_once...
I am positive that this is the reason why my variable does not work for the code being included by this function.
If I remplace gator::display("main_filelist.php", $params)
by gator::display("main_filelist.php")
most of the code won't work.
But if I replace gator::display("main_filelist.php", $params)
by a classic include include('main_filelist.php')
then the most of the code is executed and I can actually see my variable $getprivileges
being taken into account.
$getprivileges
not working with this type of
include ? (debugging reports me that the variable is not defined for
the code included by gator::display("main_filelist.php", $params)
)$params
? (I am afraid to break the code and
there is the possibility that I have to do it for another file in the
future on which I may not have the choice)Sorry for the lenght of this post, I am trying to give as much informations as possible and I'm actually figuring a lot out by recapitulating this situation.
Also, I am a beginner in php so feel free to give as much details as you want if you have some suggestions :s Thanks a lot for your help !
-apatik
Update :
I added this code on the main-filelist.php
(the included file that wouldn't get my $getprivilege
and so far it looks ok ! Going to test every possibilities.
<?php
$directory = $_SESSION['cwd'];
$user = $_SESSION['simple_auth']['username'];
$repository = gatorconf::get('repository');
$userdir = $repository.DS.'user'.DS.$user;
$getprivileges = scanDirectory($userdir, $directory);
if ((gator::checkPermissions('rw')) || ($userdir == $directory && gator::checkPermissions('r'))) {
$getprivileges = true;
}
?>
Upvotes: 0
Views: 141
Reputation: 96159
Have a read of http://docs.php.net/language.variables.scope and http://docs.php.net/get_defined_vars and then the output of
<?php // test2.php
var_export( get_defined_vars() );
and
<?php
$globalvar = 1;
require 'test2.php';
class Foo {
public static function bar($params) {
$localvar = 2;
require 'test2.php';
}
}
echo "\r\n--------------------\r\n";
$foo = new Foo;
$foo->bar( array('getprivileges'=>'something') );
should illustrate your problem.
Upvotes: 1