public static function open($path, $flags, $cb, $mode = null, $pri = EIO_PRI_DEFAULT)
{
$cb = CallbackWrapper::forceWrap($cb);
if (!FileSystem::$supported) {
$mode = File::convertFlags($flags, true);
$fd = fopen($path, $mode);
if (!$fd) {
$cb(false);
return false;
}
$file = new File($fd, $path);
$cb($file);
return true;
}
$fdCacheKey = $path . "" . $flags;
$noncache = mb_orig_strpos($flags, '!') !== false;
$flags = File::convertFlags($flags);
if (!$noncache && ($item = FileSystem::$fdCache->get($fdCacheKey))) {
// cache hit
$file = $item->getValue();
if ($file === null) {
// operation in progress
$item->addListener($cb);
} else {
// hit
$cb($file);
}
return null;
} elseif (!$noncache) {
$item = FileSystem::$fdCache->put($fdCacheKey, null);
$item->addListener($cb);
}
return eio_open($path, $flags, $mode, $pri, function ($path, $fd) use($cb, $flags, $fdCacheKey, $noncache) {
if ($fd === -1) {
if ($noncache) {
$cb(false);
} else {
FileSystem::$fdCache->put($fdCacheKey, false, self::$badFDttl);
}
return;
}
$file = new File($fd, $path);
$file->append = ($flags | EIO_O_APPEND) === $flags;
if ($file->append) {
$file->stat(function ($file, $stat) use($cb, $noncache, $fdCacheKey) {
$file->offset = $stat['size'];
if (!$noncache) {
$file->fdCacheKey = $fdCacheKey;
FileSystem::$fdCache->put($fdCacheKey, $file);
} else {
$cb($file);
}
});
} else {
if (!$noncache) {
$file->fdCacheKey = $fdCacheKey;
FileSystem::$fdCache->put($fdCacheKey, $file);
} else {
$cb($file);
}
}
}, $path);
}
/** * Prepares the request body * @return void */ public function postPrepare() { if (!$this->attrs->server['REQUEST_METHOD_POST']) { return; } if (isset($this->attrs->server['REQUEST_PREPARED_UPLOADS']) && $this->attrs->server['REQUEST_PREPARED_UPLOADS'] === 'nginx') { if (isset($this->attrs->server['REQUEST_PREPARED_UPLOADS_URL_PREFIX'])) { $URLprefix = $this->attrs->server['REQUEST_PREPARED_UPLOADS_URL_PREFIX']; $l = mb_orig_strlen($URLprefix); foreach (['PHP_SELF', 'REQUEST_URI', 'SCRIPT_NAME', 'DOCUMENT_URI'] as $k) { if (!isset($this->attrs->server[$k])) { continue; } if (strncmp($this->attrs->server[$k], $URLprefix, $l) === 0) { $this->attrs->server[$k] = substr($this->attrs->server[$k], $l - 1); } } } $prefix = 'file.'; $prefixlen = mb_orig_strlen($prefix); foreach ($this->attrs->post as $k => $v) { if (strncmp($k, $prefix, $prefixlen) === 0) { $e = explode('.', substr($k, $prefixlen)); if (!isset($e[1])) { $e = ['file', $e[0]]; } if (!isset($this->attrs->files[$e[0]])) { $this->attrs->files[$e[0]] = ['error' => UPLOAD_ERR_OK]; } $this->attrs->files[$e[0]][$e[1]] = $v; unset($this->attrs->post[$k]); } } $uploadTmp = $this->getUploadTempDir(); foreach ($this->attrs->files as $k => &$file) { if (!isset($file['tmp_name']) || !isset($file['name']) || !ctype_digit(basename($file['tmp_name'])) || mb_orig_strpos(pathinfo($file['tmp_name'], PATHINFO_DIRNAME), $uploadTmp) !== 0) { unset($this->attrs->files[$k]); continue; } FileSystem::open($file['tmp_name'], 'c+!', function ($fp) use(&$file) { if (!$fp) { return; } $file['fp'] = $fp; }); } unset($file); } if (isset($this->attrs->server['REQUEST_BODY_FILE']) && $this->upstream->pool->config->autoreadbodyfile->value) { $this->readBodyFile(); } }