Skip to content

Commit 8fa806c

Browse files
committed
Merge pull request #250 from khoaofgod/final
Open Base Dir for shared hosting
2 parents 8ec77fd + abdf552 commit 8fa806c

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
namespace phpFastCache\Util;
3+
define('PHP_OPEN_BASEDIR', @ini_get("open_basedir"));
4+
5+
class OpenBaseDir {
6+
public static $stores = array();
7+
public static function checkBaseDir($path) {
8+
if(!is_null(PHP_OPEN_BASEDIR) && PHP_OPEN_BASEDIR != "") {
9+
/*
10+
* ONLY check ONE time if System Have Open Base Dir
11+
* Else, always return TRUE for system without OPenBaseDir
12+
*/
13+
$index = md5($path);
14+
if (!isset(self::$stores[$index])) {
15+
// never check before, then check it 1 one time for the src dir only
16+
$list = explode(":", PHP_OPEN_BASEDIR);
17+
foreach ($list as $allowed_path) {
18+
$tmp = explode($allowed_path, $path, 2);
19+
if ($tmp[0] != $path) {
20+
// echo "<br>".$tmp[0]." = ".$path." BY {$allowed_path}";
21+
self::$stores[$index] = true;
22+
return true;
23+
}
24+
25+
}
26+
} else {
27+
return self::$stores[$index];
28+
}
29+
return false;
30+
}
31+
return true;
32+
}
33+
}

src/phpFastCache/phpFastCache.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313
*/
1414

1515
use phpFastCache\CacheManager;
16+
use phpFastCache\Util\OpenBaseDir;
1617
define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
17-
18+
require_once __DIR__."/Util/OpenBaseDir.php";
1819
/**
1920
* Register Autoload
2021
*/
2122
spl_autoload_register(function ($entity) {
2223
// Explode is faster than substr & strstr also more control
2324
$module = explode('\\',$entity,2);
24-
if ($module[0] !== 'phpFastCache') {
25+
if ($module[0] !== 'phpFastCache'
26+
|| !OpenBaseDir::checkBaseDir(__DIR__)) {
2527
/**
2628
* Not a part of phpFastCache file
2729
* then we return here.
@@ -30,7 +32,6 @@
3032
}
3133

3234
$entity = str_replace('\\', '/', $module[1]);
33-
3435
$path = __DIR__ . '/' . $entity . '.' . PHP_EXT;
3536
if (is_readable($path)) {
3637
require_once $path;

0 commit comments

Comments
 (0)