Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for PHP warning "is_file(): open_basedir restriction in effect", round 2 #48

Merged
merged 2 commits into from
Sep 6, 2014
Merged

fix for PHP warning "is_file(): open_basedir restriction in effect", round 2 #48

merged 2 commits into from
Sep 6, 2014

Conversation

rindeal
Copy link
Contributor

@rindeal rindeal commented Sep 5, 2014

This reverts commit d234b8e in PR #47 and returns back the is_dir() check.

I was working with too many hosts simultaneously and checked the file_exists() method on a host that worked perfectly even before the fix and as I updated the files on the problematic host the problems returned and the warning message changed to:

PHP Warning: file_exists(): open_basedir restriction in effect. File(/home/users/.../web/user/plugins/.gitkeep/.gitkeep.yaml) is not within the allowed path(s): (/home/users/.../:/usr/share/pear/) in .../web/system/src/Grav/Common/Config.php:237

I'm sorry for this and have triple-checked that this code really finally fixes the issue.

@w00fz
Copy link
Member

w00fz commented Sep 5, 2014

Question: how come the user/plugins in question is out of scope of your open_basedir?

Can you explain how is your setup? It is a bit strange it fails like that when trying to open a file that should be child of the root app.

@rhukster
Copy link
Member

rhukster commented Sep 5, 2014

Let's back up a bit here. I think there is another issue that is the root cause here. The fact that this file is outside the basedir restriction is more the problem. This build() method loops over all the files in the PLUGINS_DIR. If it cannot access the first file, it won't be able to access any plugins either.

/home/users/.../web/user/plugins/.gitkeep/.gitkeep.yaml

This is not right for you, not sure why that would be. Can you debug in your system and see what PLUGINS_DIR is? perhaps we can work out what's going on here.

@rindeal
Copy link
Contributor Author

rindeal commented Sep 6, 2014

PLUGINS_DIR resolves normally to /home/users/.../web/user/plugins/. Please note that the three dots notes a censored content. The whole issue I see in this is that the DirectoryIterator iterates over everything in the PLUGINS_DIR, including files. And as the file_exists()/is_file() tries to check for a file in a file, the warning is triggered. I have also googled a bit and found out that this is actually a PHP bug and was repeatedly reported since 2010-06-12 14:58 though still not fixed. So just do checks for a directory before you start checking for a file and everything will be OK.

@w00fz
Copy link
Member

w00fz commented Sep 6, 2014

Then it should be like this:

/** @var \DirectoryIterator $plugin */
foreach ($iterator as $plugin) {
    if (!$iterator->isFile() || $iterator->isDot()) continue;

    $name = $plugin->getBasename();
    $file = $plugin->getPathname() . DS . $name . YAML_EXT;

    $modified = filemtime($file);
    $plugins["plugins/{$name}"] = $modified;
}

Since the DirectoryIterator has already its own isFile method check and it will also skip the dot files.

Can you try?

@rhukster
Copy link
Member

rhukster commented Sep 6, 2014

I think that the @w00fz code should solve the case of this .gitkeep file sitting in the plugins/ folder from tripping it up, but I think we still need that file_exist() check to ensure the plugin has an appropriate configuration file in the format pluginname/pluginname.yaml. What do you think @w00fz ?

@w00fz
Copy link
Member

w00fz commented Sep 6, 2014

Yeah you are right. I also have a typo up there in the check for isFile():

/** @var \DirectoryIterator $plugin */
foreach ($iterator as $plugin) {
    if ($iterator->isFile() || $iterator->isDot()) continue;

    $name = $plugin->getBasename();
    $file = $plugin->getPathname() . DS . $name . YAML_EXT;

    if (!file_exists($file)) continue;

    $modified = filemtime($file);
    $plugins["plugins/{$name}"] = $modified;
}

@rindeal
Copy link
Contributor Author

rindeal commented Sep 6, 2014

Yes the second snippet is OK, it var_dumped only non-dot directories and thus triggered no warning.

@w00fz
Copy link
Member

w00fz commented Sep 6, 2014

Ok that's cool. I just realized we can just revert that file_exists back to is_file, since it's faster and we are skipping undesired files already.

@rindeal
Copy link
Contributor Author

rindeal commented Sep 6, 2014

Well, I benchmarked it now and file_exists() has been always slightly faster...

$glob=glob(__DIR__ . '/*');
$t = microtime(true);
for ($i = 0; $i < 10000; $i++) {
    foreach ($glob as $file) {
        file_exists($file);
    }
}
$t=microtime(true)-$t;
echo round($t*1000,2);
echo "\n";
$t = microtime(true);
for ($i = 0; $i < 10000; $i++) {
    foreach ($glob as $file) {
        is_file($file);
    }
}
$t=microtime(true)-$t;
echo round($t*1000,2);
2269.7
2356.83

@rindeal
Copy link
Contributor Author

rindeal commented Sep 6, 2014

most probably because it only checks if inode exists and doesnt look at it's content.

@w00fz
Copy link
Member

w00fz commented Sep 6, 2014

You need to use clearstatcache() if you run a benchmark like that or it will be cached see here.

But the thing is file_exists checks for both files and folders, is_file only for files (and i think symlinks too). Since we know we are expecting a file it is more appropriate to use is_file i think.

@rindeal
Copy link
Contributor Author

rindeal commented Sep 6, 2014

Right, now its

2771.75
2413.48

So should I commit it to this branch or will you create a new one?

@w00fz
Copy link
Member

w00fz commented Sep 6, 2014

Commit in here and we can merge.

@rindeal
Copy link
Contributor Author

rindeal commented Sep 6, 2014

Committed

w00fz added a commit that referenced this pull request Sep 6, 2014
fix for PHP warning "is_file(): open_basedir restriction in effect", round 2
@w00fz w00fz merged commit 8ddfa57 into getgrav:develop Sep 6, 2014
@w00fz
Copy link
Member

w00fz commented Sep 6, 2014

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants