29#define O_RDONLY _O_RDONLY
34#include <sys/socket.h>
35#include <netinet/in.h>
37#ifdef HAVE_ARPA_INET_H
90static const char base32alphabet[] =
"0123456789abcdefghijklmnopqrstuv";
92static int php_do_open_temporary_file(
const char *path,
const char *pfx,
zend_string **opened_path_p)
95 char *opened_path =
NULL;
96 size_t opened_path_len;
97 wchar_t *cwdw, *random_prefix_w, pathw[
MAXPATHLEN];
100 char *trailing_slash;
110 int open_flags = O_CREAT | O_TRUNC | O_RDWR
117 if (!path || !path[0]) {
123 SetLastError(ERROR_INVALID_NAME);
141 if (php_random_bytes_silent(&random,
sizeof(random)) ==
FAILURE) {
149 p = zend_mempcpy(random_prefix, pfx,
strlen(pfx));
150 while (
p + 1 < random_prefix +
len) {
151 *
p = base32alphabet[random %
strlen(base32alphabet)];
153 random /=
strlen(base32alphabet);
161 trailing_slash =
"/";
165 efree(random_prefix);
174 if (!cwdw || !random_prefix_w) {
176 free(random_prefix_w);
177 efree(random_prefix);
182 if (GetTempFileNameW(cwdw, random_prefix_w, 0, pathw)) {
184 if (!opened_path || opened_path_len >=
MAXPATHLEN) {
186 free(random_prefix_w);
187 efree(random_prefix);
197 free(random_prefix_w);
198 efree(random_prefix);
207 free(random_prefix_w);
208#elif defined(HAVE_MKSTEMP)
209 fd = mkstemp(opened_path);
211 if (mktemp(opened_path)) {
217 if (
fd != -1 && opened_path_p) {
218 *opened_path_p = zend_string_init(opened_path, opened_path_len, 0);
222 if (
fd != -1 && opened_path_p) {
223 *opened_path_p = zend_string_init(opened_path,
strlen(opened_path), 0);
227 efree(random_prefix);
238 if (
PG(php_sys_temp_dir)) {
239 return PG(php_sys_temp_dir);
244 char *sys_temp_dir =
PG(sys_temp_dir);
249 return PG(php_sys_temp_dir);
252 return PG(php_sys_temp_dir);
279 return PG(php_sys_temp_dir);
294 return PG(php_sys_temp_dir);
301 return PG(php_sys_temp_dir);
306 return PG(php_sys_temp_dir);
320 const char *temp_dir;
326 *opened_path_p =
NULL;
329 if (!
dir || *
dir ==
'\0') {
336 return php_do_open_temporary_file(temp_dir, pfx, opened_path_p);
347 fd = php_do_open_temporary_file(
dir, pfx, opened_path_p);
372 fp = fdopen(
fd,
"r+b");
getenv(?string $name=null, bool $local_only=false)
dir(string $directory, $context=null)
assert(mixed $assertion, Throwable|string|null $description=null)
#define PHP_WIN32_CP_IGNORE_LEN
PHPAPI int php_check_open_basedir(const char *path)
#define php_win32_ioutil_any_to_w(in)
#define php_win32_ioutil_conv_w_to_any
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, zend_string **opened_path_p)
PHPAPI FILE * php_open_temporary_file(const char *dir, const char *pfx, zend_string **opened_path_p)
PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, zend_string **opened_path_p, uint32_t flags)
PHPAPI const char * php_get_temporary_directory(void)
#define PHP_TMP_FILE_SILENT
#define PHP_TMP_FILE_DEFAULT
#define PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ON_EXPLICIT_DIR
#define PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ON_FALLBACK
PHPAPI uint64_t php_random_generate_fallback_seed(void)
int php_win32_check_trailing_space(const char *path, const size_t path_len)
#define estrndup(s, length)
struct _zend_string zend_string
CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath)
#define VCWD_OPEN_MODE(path, flags, mode)
#define VCWD_OPEN(path, flags)
#define VCWD_GETCWD(buff, size)
#define VCWD_CHMOD(path, mode)
struct _cwd_state cwd_state