<?php
use Drupal\Core\StreamWrapper\LocalStream;
use Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\StreamedResponse;
const STREAM_WRAPPERS_ALL = 0x0;
const STREAM_WRAPPERS_LOCAL = 0x1;
const STREAM_WRAPPERS_READ = 0x4;
const STREAM_WRAPPERS_WRITE = 0x8;
const STREAM_WRAPPERS_VISIBLE = 0x10;
define('STREAM_WRAPPERS_HIDDEN', STREAM_WRAPPERS_READ | STREAM_WRAPPERS_WRITE);
define('STREAM_WRAPPERS_LOCAL_HIDDEN', STREAM_WRAPPERS_LOCAL | STREAM_WRAPPERS_HIDDEN);
define('STREAM_WRAPPERS_WRITE_VISIBLE', STREAM_WRAPPERS_READ | STREAM_WRAPPERS_WRITE | STREAM_WRAPPERS_VISIBLE);
define('STREAM_WRAPPERS_READ_VISIBLE', STREAM_WRAPPERS_READ | STREAM_WRAPPERS_VISIBLE);
define('STREAM_WRAPPERS_NORMAL', STREAM_WRAPPERS_WRITE_VISIBLE);
define('STREAM_WRAPPERS_LOCAL_NORMAL', STREAM_WRAPPERS_LOCAL | STREAM_WRAPPERS_NORMAL);
const FILE_CREATE_DIRECTORY = 1;
const FILE_MODIFY_PERMISSIONS = 2;
const FILE_EXISTS_RENAME = 0;
const FILE_EXISTS_REPLACE = 1;
const FILE_EXISTS_ERROR = 2;
const FILE_STATUS_PERMANENT = 1;
function file_get_stream_wrappers($filter = STREAM_WRAPPERS_ALL) {
$wrappers_storage =& drupal_static(__FUNCTION__);
if (!isset($wrappers_storage)) {
$wrappers = module_invoke_all('stream_wrappers');
foreach ($wrappers as $scheme => $info) {
$wrappers[$scheme] += array(
'type' => STREAM_WRAPPERS_NORMAL,
);
}
drupal_alter('stream_wrappers', $wrappers);
$existing = stream_get_wrappers();
foreach ($wrappers as $scheme => $info) {
if (in_array('Drupal\\Core\\StreamWrapper\\StreamWrapperInterface', class_implements($info['class']), TRUE)) {
if (in_array($scheme, $existing, TRUE)) {
$wrappers[$scheme]['override'] = TRUE;
stream_wrapper_unregister($scheme);
}
else {
$wrappers[$scheme]['override'] = FALSE;
}
if (($info['type'] & STREAM_WRAPPERS_LOCAL) == STREAM_WRAPPERS_LOCAL) {
stream_wrapper_register($scheme, $info['class']);
}
else {
stream_wrapper_register($scheme, $info['class'], STREAM_IS_URL);
}
}
$wrappers_storage[STREAM_WRAPPERS_ALL][$scheme] = $wrappers[$scheme];
if (($info['type'] & STREAM_WRAPPERS_WRITE_VISIBLE) == STREAM_WRAPPERS_WRITE_VISIBLE) {
$wrappers_storage[STREAM_WRAPPERS_WRITE_VISIBLE][$scheme] = $wrappers[$scheme];
}
}
}
if (!isset($wrappers_storage[$filter])) {
$wrappers_storage[$filter] = array();
foreach ($wrappers_storage[STREAM_WRAPPERS_ALL] as $scheme => $info) {
if (($info['type'] & $filter) == $filter) {
$wrappers_storage[$filter][$scheme] = $info;
}
}
}
return $wrappers_storage[$filter];
}
function file_stream_wrapper_get_class($scheme) {
$wrappers = file_get_stream_wrappers();
return empty($wrappers[$scheme]) ? FALSE : $wrappers[$scheme]['class'];
}
function file_uri_scheme($uri) {
$position = strpos($uri, '://');
return $position ? substr($uri, 0, $position) : FALSE;
}
function file_stream_wrapper_valid_scheme($scheme) {
$class = file_stream_wrapper_get_class($scheme);
if (class_exists($class)) {
return TRUE;
}
else {
return FALSE;
}
}
function file_uri_target($uri) {
$data = explode('://', $uri, 2);
return count($data) == 2 ? trim($data[1], '\\/') : FALSE;
}
function file_default_scheme() {
return variable_get('file_default_scheme', 'public');
}
function file_stream_wrapper_uri_normalize($uri) {
$scheme = file_uri_scheme($uri);
if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
$target = file_uri_target($uri);
if ($target !== FALSE) {
$uri = $scheme . '://' . $target;
}
}
else {
$url = 'file://' . $uri;
}
return $uri;
}
function file_stream_wrapper_get_instance_by_uri($uri) {
$scheme = file_uri_scheme($uri);
$class = file_stream_wrapper_get_class($scheme);
if (class_exists($class)) {
$instance = new $class();
$instance
->setUri($uri);
return $instance;
}
else {
return FALSE;
}
}
function file_stream_wrapper_get_instance_by_scheme($scheme) {
$class = file_stream_wrapper_get_class($scheme);
if (class_exists($class)) {
$instance = new $class();
$instance
->setUri($scheme . '://');
return $instance;
}
else {
return FALSE;
}
}
function file_create_url($uri) {
drupal_alter('file_url', $uri);
$scheme = file_uri_scheme($uri);
if (!$scheme) {
if (drupal_substr($uri, 0, 1) == '/') {
return $uri;
}
else {
return $GLOBALS['base_url'] . '/' . drupal_encode_path($uri);
}
}
elseif ($scheme == 'http' || $scheme == 'https') {
return $uri;
}
else {
if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
return $wrapper
->getExternalUrl();
}
else {
return FALSE;
}
}
}
function file_prepare_directory(&$directory, $options = FILE_MODIFY_PERMISSIONS) {
if (!file_stream_wrapper_valid_scheme(file_uri_scheme($directory))) {
$directory = rtrim($directory, '/\\');
}
if (!is_dir($directory)) {
if ($options & FILE_CREATE_DIRECTORY && @drupal_mkdir($directory, NULL, TRUE)) {
return drupal_chmod($directory);
}
return FALSE;
}
$writable = is_writable($directory);
if (!$writable && $options & FILE_MODIFY_PERMISSIONS) {
return drupal_chmod($directory);
}
return $writable;
}
function file_ensure_htaccess() {
file_save_htaccess('public://', FALSE);
if (variable_get('file_private_path', FALSE)) {
file_save_htaccess('private://', TRUE);
}
file_save_htaccess('temporary://', TRUE);
file_save_htaccess(config_get_config_directory(), TRUE);
}
function file_save_htaccess($directory, $private = TRUE) {
if (file_uri_scheme($directory)) {
$directory = file_stream_wrapper_uri_normalize($directory);
}
else {
$directory = rtrim($directory, '/\\');
}
$htaccess_path = $directory . '/.htaccess';
if (file_exists($htaccess_path)) {
return;
}
if ($private) {
$htaccess_lines = MTimeProtectedFastFileStorage::HTACCESS;
}
else {
$htaccess_lines = "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\nOptions None\nOptions +FollowSymLinks";
}
if (file_put_contents($htaccess_path, $htaccess_lines)) {
drupal_chmod($htaccess_path, 0444);
}
else {
$variables = array(
'%directory' => $directory,
'!htaccess' => '<br />' . nl2br(check_plain($htaccess_lines)),
);
watchdog('security', "Security warning: Couldn't write .htaccess file. Please create a .htaccess file in your %directory directory which contains the following lines: <code>!htaccess</code>", $variables, WATCHDOG_ERROR);
}
}
function file_valid_uri($uri) {
$uri_scheme = file_uri_scheme($uri);
if (empty($uri_scheme) || !file_stream_wrapper_valid_scheme($uri_scheme)) {
return FALSE;
}
return TRUE;
}
function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
$original_source = $source;
$original_destination = $destination;
if (!file_exists($source)) {
drupal_set_message(t('The specified file %file could not be copied because no file by that name exists. Please check that you supplied the correct filename.', array(
'%file' => $original_source,
)), 'error');
if (($realpath = drupal_realpath($original_source)) !== FALSE) {
watchdog('file', 'File %file (%realpath) could not be copied because it does not exist.', array(
'%file' => $original_source,
'%realpath' => $realpath,
));
}
else {
watchdog('file', 'File %file could not be copied because it does not exist.', array(
'%file' => $original_source,
));
}
return FALSE;
}
if (!isset($destination)) {
$destination = file_build_uri(drupal_basename($source));
}
if (file_prepare_directory($destination)) {
$destination = file_stream_wrapper_uri_normalize($destination . '/' . drupal_basename($source));
}
else {
$dirname = drupal_dirname($destination);
if (!file_prepare_directory($dirname)) {
watchdog('file', 'File %file could not be copied because the destination directory %destination is not configured correctly.', array(
'%file' => $original_source,
'%destination' => $dirname,
));
drupal_set_message(t('The specified file %file could not be copied because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', array(
'%file' => $original_source,
)), 'error');
return FALSE;
}
}
$destination = file_destination($destination, $replace);
if ($destination === FALSE) {
drupal_set_message(t('The file %file could not be copied because a file by that name already exists in the destination directory.', array(
'%file' => $original_source,
)), 'error');
watchdog('file', 'File %file could not be copied because a file by that name already exists in the destination directory (%directory)', array(
'%file' => $original_source,
'%destination' => $destination,
));
return FALSE;
}
$real_source = drupal_realpath($source);
$real_destination = drupal_realpath($destination);
if ($source == $destination || $real_source !== FALSE && $real_source == $real_destination) {
drupal_set_message(t('The specified file %file was not copied because it would overwrite itself.', array(
'%file' => $source,
)), 'error');
watchdog('file', 'File %file could not be copied because it would overwrite itself.', array(
'%file' => $source,
));
return FALSE;
}
file_ensure_htaccess();
if (!@copy($source, $destination)) {
if ($real_source === FALSE || $real_destination === FALSE || !@copy($real_source, $real_destination)) {
watchdog('file', 'The specified file %file could not be copied to %destination.', array(
'%file' => $source,
'%destination' => $destination,
), WATCHDOG_ERROR);
return FALSE;
}
}
drupal_chmod($destination);
return $destination;
}
function file_build_uri($path) {
$uri = file_default_scheme() . '://' . $path;
return file_stream_wrapper_uri_normalize($uri);
}
function file_destination($destination, $replace) {
if (file_exists($destination)) {
switch ($replace) {
case FILE_EXISTS_REPLACE:
break;
case FILE_EXISTS_RENAME:
$basename = drupal_basename($destination);
$directory = drupal_dirname($destination);
$destination = file_create_filename($basename, $directory);
break;
case FILE_EXISTS_ERROR:
return FALSE;
}
}
return $destination;
}
function file_unmanaged_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
$filepath = file_unmanaged_copy($source, $destination, $replace);
if ($filepath == FALSE || file_unmanaged_delete($source) == FALSE) {
return FALSE;
}
return $filepath;
}
function file_munge_filename($filename, $extensions, $alerts = TRUE) {
$original = $filename;
if (!variable_get('allow_insecure_uploads', 0)) {
$whitelist = array_unique(explode(' ', trim($extensions)));
$filename_parts = explode('.', $filename);
$new_filename = array_shift($filename_parts);
$final_extension = array_pop($filename_parts);
foreach ($filename_parts as $filename_part) {
$new_filename .= '.' . $filename_part;
if (!in_array($filename_part, $whitelist) && preg_match("/^[a-zA-Z]{2,5}\\d?\$/", $filename_part)) {
$new_filename .= '_';
}
}
$filename = $new_filename . '.' . $final_extension;
if ($alerts && $original != $filename) {
drupal_set_message(t('For security reasons, your upload has been renamed to %filename.', array(
'%filename' => $filename,
)));
}
}
return $filename;
}
function file_unmunge_filename($filename) {
return str_replace('_.', '.', $filename);
}
function file_create_filename($basename, $directory) {
$basename = preg_replace('/[\\x00-\\x1F]/u', '_', $basename);
if (substr(PHP_OS, 0, 3) == 'WIN') {
$basename = str_replace(array(
':',
'*',
'?',
'"',
'<',
'>',
'|',
), '_', $basename);
}
if (substr($directory, -1) == '/') {
$separator = '';
}
else {
$separator = '/';
}
$destination = $directory . $separator . $basename;
if (file_exists($destination)) {
$pos = strrpos($basename, '.');
if ($pos !== FALSE) {
$name = substr($basename, 0, $pos);
$ext = substr($basename, $pos);
}
else {
$name = $basename;
$ext = '';
}
$counter = 0;
do {
$destination = $directory . $separator . $name . '_' . $counter++ . $ext;
} while (file_exists($destination));
}
return $destination;
}
function file_delete($fid) {
return file_delete_multiple(array(
$fid,
));
}
function file_delete_multiple(array $fids) {
entity_delete_multiple('file', $fids);
}
function file_unmanaged_delete($path) {
if (is_dir($path)) {
watchdog('file', '%path is a directory and cannot be removed using file_unmanaged_delete().', array(
'%path' => $path,
), WATCHDOG_ERROR);
return FALSE;
}
if (is_file($path)) {
return drupal_unlink($path);
}
if (!file_exists($path)) {
watchdog('file', 'The file %path was not deleted because it does not exist.', array(
'%path' => $path,
), WATCHDOG_NOTICE);
return TRUE;
}
watchdog('file', 'The file %path is not of a recognized type so it was not deleted.', array(
'%path' => $path,
), WATCHDOG_ERROR);
return FALSE;
}
function file_unmanaged_delete_recursive($path, $callback = NULL) {
if (isset($callback)) {
call_user_func($callback, $path);
}
if (is_dir($path)) {
$dir = dir($path);
while (($entry = $dir
->read()) !== FALSE) {
if ($entry == '.' || $entry == '..') {
continue;
}
$entry_path = $path . '/' . $entry;
file_unmanaged_delete_recursive($entry_path, $callback);
}
$dir
->close();
return drupal_rmdir($path);
}
return file_unmanaged_delete($path);
}
function file_save_upload($source, $validators = array(), $destination = FALSE, $replace = FILE_EXISTS_RENAME) {
global $user;
static $upload_cache;
if (isset($upload_cache[$source])) {
return $upload_cache[$source];
}
if (empty($_FILES['files']['name'][$source])) {
return NULL;
}
switch ($_FILES['files']['error'][$source]) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
drupal_set_message(t('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', array(
'%file' => $_FILES['files']['name'][$source],
'%maxsize' => format_size(file_upload_max_size()),
)), 'error');
return FALSE;
case UPLOAD_ERR_PARTIAL:
case UPLOAD_ERR_NO_FILE:
drupal_set_message(t('The file %file could not be saved because the upload did not complete.', array(
'%file' => $_FILES['files']['name'][$source],
)), 'error');
return FALSE;
case UPLOAD_ERR_OK:
if (is_uploaded_file($_FILES['files']['tmp_name'][$source])) {
break;
}
default:
drupal_set_message(t('The file %file could not be saved. An unknown error has occurred.', array(
'%file' => $_FILES['files']['name'][$source],
)), 'error');
return FALSE;
}
$values = array(
'uid' => $user->uid,
'status' => 0,
'filename' => trim(drupal_basename($_FILES['files']['name'][$source]), '.'),
'uri' => $_FILES['files']['tmp_name'][$source],
'filesize' => $_FILES['files']['size'][$source],
);
$values['filemime'] = file_get_mimetype($values['filename']);
$file = entity_create('file', $values);
$extensions = '';
if (isset($validators['file_validate_extensions'])) {
if (isset($validators['file_validate_extensions'][0])) {
$extensions = $validators['file_validate_extensions'][0];
}
else {
unset($validators['file_validate_extensions']);
}
}
else {
$extensions = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
$validators['file_validate_extensions'] = array();
$validators['file_validate_extensions'][0] = $extensions;
}
if (!empty($extensions)) {
$file->filename = file_munge_filename($file->filename, $extensions);
}
if (!variable_get('allow_insecure_uploads', 0) && preg_match('/\\.(php|pl|py|cgi|asp|js)(\\.|$)/i', $file->filename) && substr($file->filename, -4) != '.txt') {
$file->filemime = 'text/plain';
$file->uri .= '.txt';
$file->filename .= '.txt';
if (!empty($extensions)) {
$validators['file_validate_extensions'][0] .= ' txt';
drupal_set_message(t('For security reasons, your upload has been renamed to %filename.', array(
'%filename' => $file->filename,
)));
}
}
if (empty($destination)) {
$destination = 'temporary://';
}
$destination_scheme = file_uri_scheme($destination);
if (!$destination_scheme || !file_stream_wrapper_valid_scheme($destination_scheme)) {
drupal_set_message(t('The file could not be uploaded because the destination %destination is invalid.', array(
'%destination' => $destination,
)), 'error');
return FALSE;
}
$file->source = $source;
if (substr($destination, -1) != '/') {
$destination .= '/';
}
$file->destination = file_destination($destination . $file->filename, $replace);
if ($file->destination === FALSE) {
drupal_set_message(t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array(
'%source' => $source,
'%directory' => $destination,
)), 'error');
return FALSE;
}
$validators['file_validate_name_length'] = array();
$errors = file_validate($file, $validators);
if (!empty($errors)) {
$message = t('The specified file %name could not be uploaded.', array(
'%name' => $file->filename,
));
if (count($errors) > 1) {
$message .= theme('item_list', array(
'items' => $errors,
));
}
else {
$message .= ' ' . array_pop($errors);
}
form_set_error($source, $message);
return FALSE;
}
$file->uri = $file->destination;
if (!drupal_move_uploaded_file($_FILES['files']['tmp_name'][$source], $file->uri)) {
form_set_error($source, t('File upload error. Could not move uploaded file.'));
watchdog('file', 'Upload error. Could not move uploaded file %file to destination %destination.', array(
'%file' => $file->filename,
'%destination' => $file->uri,
));
return FALSE;
}
drupal_chmod($file->uri);
if ($replace == FILE_EXISTS_REPLACE) {
$existing_files = entity_load_multiple_by_properties('file', array(
'uri' => $file->uri,
));
if (count($existing_files)) {
$existing = reset($existing_files);
$file->fid = $existing->fid;
}
}
$file
->save();
$upload_cache[$source] = $file;
return $file;
}
function drupal_move_uploaded_file($filename, $uri) {
$result = @move_uploaded_file($filename, $uri);
if (!$result) {
if ($realpath = drupal_realpath($uri)) {
$result = move_uploaded_file($filename, $realpath);
}
else {
$result = move_uploaded_file($filename, $uri);
}
}
return $result;
}
function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
$temp_name = drupal_tempnam('temporary://', 'file');
if (file_put_contents($temp_name, $data) === FALSE) {
drupal_set_message(t('The file could not be created.'), 'error');
return FALSE;
}
return file_unmanaged_move($temp_name, $destination, $replace);
}
function file_transfer($uri, $headers) {
return new StreamedResponse(function () use ($uri) {
if (file_exists($uri) && ($fd = fopen($uri, 'rb'))) {
while (!feof($fd)) {
print fread($fd, 1024);
}
fclose($fd);
}
}, 200, $headers);
}
function file_download() {
$args = func_get_args();
$scheme = array_shift($args);
$target = implode('/', $args);
$uri = $scheme . '://' . $target;
if (file_stream_wrapper_valid_scheme($scheme) && file_exists($uri)) {
$headers = array();
foreach (module_implements('file_download') as $module) {
$function = $module . '_file_download';
$result = $function($uri);
if ($result == -1) {
throw new AccessDeniedHttpException();
}
if (isset($result) && is_array($result)) {
$headers = array_merge($headers, $result);
}
}
if (count($headers)) {
return file_transfer($uri, $headers);
}
throw new AccessDeniedHttpException();
}
throw new NotFoundHttpException();
}
function file_scan_directory($dir, $mask, $options = array(), $depth = 0) {
$options += array(
'nomask' => '/^CVS$/',
'callback' => 0,
'recurse' => TRUE,
'key' => 'uri',
'min_depth' => 0,
);
if ($depth == 0) {
$dir = file_stream_wrapper_uri_normalize($dir);
$dir_has_slash = substr($dir, -1) === '/';
}
$options['key'] = in_array($options['key'], array(
'uri',
'filename',
'name',
)) ? $options['key'] : 'uri';
$files = array();
if (is_dir($dir)) {
if ($handle = @opendir($dir)) {
while (FALSE !== ($filename = readdir($handle))) {
if ($filename[0] != '.' && !preg_match($options['nomask'], $filename)) {
if ($depth == 0 && $dir_has_slash) {
$uri = "{$dir}{$filename}";
}
else {
$uri = "{$dir}/{$filename}";
}
if ($options['recurse'] && is_dir($uri)) {
$files = array_merge(file_scan_directory($uri, $mask, $options, $depth + 1), $files);
}
elseif ($depth >= $options['min_depth'] && preg_match($mask, $filename)) {
$file = new stdClass();
$file->uri = $uri;
$file->filename = $filename;
$file->name = pathinfo($filename, PATHINFO_FILENAME);
$key = $options['key'];
$files[$file->{$key}] = $file;
if ($options['callback']) {
$options['callback']($uri);
}
}
}
}
closedir($handle);
}
else {
watchdog('file', '@dir can not be opened', array(
'@dir' => $dir,
), WATCHDOG_ERROR);
}
}
return $files;
}
function file_upload_max_size() {
static $max_size = -1;
if ($max_size < 0) {
$max_size = parse_size(ini_get('post_max_size'));
$upload_max = parse_size(ini_get('upload_max_filesize'));
if ($upload_max > 0 && $upload_max < $max_size) {
$max_size = $upload_max;
}
}
return $max_size;
}
function file_get_mimetype($uri, $mapping = NULL) {
if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
return $wrapper
->getMimeType($uri, $mapping);
}
else {
return LocalStream::getMimeType($uri, $mapping);
}
}
function drupal_chmod($uri, $mode = NULL) {
if (!isset($mode)) {
if (is_dir($uri)) {
$mode = variable_get('file_chmod_directory', 0775);
}
else {
$mode = variable_get('file_chmod_file', 0664);
}
}
if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
if ($wrapper
->chmod($mode)) {
return TRUE;
}
}
else {
if (@chmod($uri, $mode)) {
return TRUE;
}
}
watchdog('file', 'The file permissions could not be set on %uri.', array(
'%uri' => $uri,
), WATCHDOG_ERROR);
return FALSE;
}
function drupal_unlink($uri, $context = NULL) {
$scheme = file_uri_scheme($uri);
if ((!$scheme || !file_stream_wrapper_valid_scheme($scheme)) && substr(PHP_OS, 0, 3) == 'WIN') {
chmod($uri, 0600);
}
if ($context) {
return unlink($uri, $context);
}
else {
return unlink($uri);
}
}
function drupal_realpath($uri) {
if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
return $wrapper
->realpath();
}
return realpath($uri);
}
function drupal_dirname($uri) {
$scheme = file_uri_scheme($uri);
if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
return file_stream_wrapper_get_instance_by_scheme($scheme)
->dirname($uri);
}
else {
return dirname($uri);
}
}
function drupal_basename($uri, $suffix = NULL) {
$separators = '/';
if (DIRECTORY_SEPARATOR != '/') {
$separators .= DIRECTORY_SEPARATOR;
}
$uri = rtrim($uri, $separators);
$filename = preg_match('@[^' . preg_quote($separators, '@') . ']+$@', $uri, $matches) ? $matches[0] : '';
if ($suffix) {
$filename = preg_replace('@' . preg_quote($suffix, '@') . '$@', '', $filename);
}
return $filename;
}
function drupal_mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL) {
if (!isset($mode)) {
$mode = variable_get('file_chmod_directory', 0775);
}
if (!isset($context)) {
return mkdir($uri, $mode, $recursive);
}
else {
return mkdir($uri, $mode, $recursive, $context);
}
}
function drupal_rmdir($uri, $context = NULL) {
$scheme = file_uri_scheme($uri);
if ((!$scheme || !file_stream_wrapper_valid_scheme($scheme)) && substr(PHP_OS, 0, 3) == 'WIN') {
chmod($uri, 0700);
}
if ($context) {
return rmdir($uri, $context);
}
else {
return rmdir($uri);
}
}
function drupal_tempnam($directory, $prefix) {
$scheme = file_uri_scheme($directory);
if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
$wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
if ($filename = tempnam($wrapper
->getDirectoryPath(), $prefix)) {
return $scheme . '://' . drupal_basename($filename);
}
else {
return FALSE;
}
}
else {
return tempnam($directory, $prefix);
}
}
function file_directory_temp() {
$temporary_directory = variable_get('file_temporary_path', NULL);
if (empty($temporary_directory)) {
$directories = array();
if (ini_get('upload_tmp_dir')) {
$directories[] = ini_get('upload_tmp_dir');
}
if (substr(PHP_OS, 0, 3) == 'WIN') {
$directories[] = 'c:\\windows\\temp';
$directories[] = 'c:\\winnt\\temp';
}
else {
$directories[] = '/tmp';
}
$directories[] = sys_get_temp_dir();
foreach ($directories as $directory) {
if (is_dir($directory) && is_writable($directory)) {
$temporary_directory = $directory;
break;
}
}
if (empty($temporary_directory)) {
$temporary_directory = variable_get('file_public_path', conf_path() . '/files') . '/tmp';
$temporary_directory = str_replace('\\', '/', $temporary_directory);
}
variable_set('file_temporary_path', $temporary_directory);
}
return $temporary_directory;
}