Skip to content
Snippets Groups Projects
Commit 5a280d47 authored by Tim Steiner's avatar Tim Steiner
Browse files

[gh-431] Merge branch 'issue-431' into develop

parents f101cac6 f9c792ef
No related branches found
No related tags found
No related merge requests found
...@@ -393,7 +393,7 @@ function unl_add_site_to_htaccess($site_id, $site_path, $is_alias) { ...@@ -393,7 +393,7 @@ function unl_add_site_to_htaccess($site_id, $site_path, $is_alias) {
. $stub_token . $stub_token
. substr($htaccess, $stub_pos + strlen($stub_token)); . substr($htaccess, $stub_pos + strlen($stub_token));
file_put_contents(DRUPAL_ROOT . '/.htaccess', $new_htaccess); _unl_file_put_contents_atomic(DRUPAL_ROOT . '/.htaccess', $new_htaccess);
} }
function unl_remove_site_from_htaccess($site_id, $is_alias) { function unl_remove_site_from_htaccess($site_id, $is_alias) {
...@@ -420,7 +420,7 @@ function unl_remove_site_from_htaccess($site_id, $is_alias) { ...@@ -420,7 +420,7 @@ function unl_remove_site_from_htaccess($site_id, $is_alias) {
$new_htaccess = substr($htaccess, 0, $start_pos) $new_htaccess = substr($htaccess, 0, $start_pos)
. substr($htaccess, $end_pos + strlen($site_end_token)) . substr($htaccess, $end_pos + strlen($site_end_token))
; ;
file_put_contents(DRUPAL_ROOT . '/.htaccess', $new_htaccess); _unl_file_put_contents_atomic(DRUPAL_ROOT . '/.htaccess', $new_htaccess);
} }
function unl_add_page_alias_to_htaccess($site_id, $host, $path, $to_uri) { function unl_add_page_alias_to_htaccess($site_id, $host, $path, $to_uri) {
...@@ -441,7 +441,7 @@ function unl_add_page_alias_to_htaccess($site_id, $host, $path, $to_uri) { ...@@ -441,7 +441,7 @@ function unl_add_page_alias_to_htaccess($site_id, $host, $path, $to_uri) {
. $stub_token . $stub_token
. substr($htaccess, $stub_pos + strlen($stub_token)); . substr($htaccess, $stub_pos + strlen($stub_token));
file_put_contents(DRUPAL_ROOT . '/.htaccess', $new_htaccess); _unl_file_put_contents_atomic(DRUPAL_ROOT . '/.htaccess', $new_htaccess);
} }
function unl_remove_page_alias_from_htaccess($site_id) { function unl_remove_page_alias_from_htaccess($site_id) {
...@@ -461,7 +461,7 @@ function unl_remove_page_alias_from_htaccess($site_id) { ...@@ -461,7 +461,7 @@ function unl_remove_page_alias_from_htaccess($site_id) {
$new_htaccess = substr($htaccess, 0, $start_pos) $new_htaccess = substr($htaccess, 0, $start_pos)
. substr($htaccess, $end_pos + strlen($site_end_token)) . substr($htaccess, $end_pos + strlen($site_end_token))
; ;
file_put_contents(DRUPAL_ROOT . '/.htaccess', $new_htaccess); _unl_file_put_contents_atomic(DRUPAL_ROOT . '/.htaccess', $new_htaccess);
} }
function unl_add_alias_to_sites_php($site_uri, $base_uri, $path, $alias_id) { function unl_add_alias_to_sites_php($site_uri, $base_uri, $path, $alias_id) {
...@@ -490,7 +490,7 @@ function unl_add_alias_to_sites_php($site_uri, $base_uri, $path, $alias_id) { ...@@ -490,7 +490,7 @@ function unl_add_alias_to_sites_php($site_uri, $base_uri, $path, $alias_id) {
$old_real_site_dir = unl_get_sites_subdir($alias_uri); $old_real_site_dir = unl_get_sites_subdir($alias_uri);
$new_sites_php = str_replace("'".$old_real_site_dir."'", "'".$real_site_dir."'", $new_sites_php); $new_sites_php = str_replace("'".$old_real_site_dir."'", "'".$real_site_dir."'", $new_sites_php);
file_put_contents(DRUPAL_ROOT . '/sites/sites.php', $new_sites_php); _unl_file_put_contents_atomic(DRUPAL_ROOT . '/sites/sites.php', $new_sites_php);
} }
function unl_remove_alias_from_sites_php($alias_id) { function unl_remove_alias_from_sites_php($alias_id) {
...@@ -510,7 +510,7 @@ function unl_remove_alias_from_sites_php($alias_id) { ...@@ -510,7 +510,7 @@ function unl_remove_alias_from_sites_php($alias_id) {
$new_sites_php = substr($sites_php, 0, $start_pos) $new_sites_php = substr($sites_php, 0, $start_pos)
. substr($sites_php, $end_pos + strlen($site_end_token)) . substr($sites_php, $end_pos + strlen($site_end_token))
; ;
file_put_contents(DRUPAL_ROOT . '/sites/sites.php', $new_sites_php); _unl_file_put_contents_atomic(DRUPAL_ROOT . '/sites/sites.php', $new_sites_php);
} }
function unl_require_writable($path) { function unl_require_writable($path) {
...@@ -518,3 +518,38 @@ function unl_require_writable($path) { ...@@ -518,3 +518,38 @@ function unl_require_writable($path) {
throw new Exception('The file "' . $path . '" needs to be writable and is not.'); throw new Exception('The file "' . $path . '" needs to be writable and is not.');
} }
} }
/**
* A drop-in replacement for file_put_contents that will atomically put the new file into place.
* This additionally requires you to have write access to the directory that will contain the file.
* @see file_put_contents
*/
function _unl_file_put_contents_atomic($filename, $data, $flags = 0, $context = NULL) {
// Create a temporary file with a simalar name in the destination directory.
$tempfile = tempnam(dirname($filename), basename($filename) . '_');
if ($tempfile === FALSE) {
return FALSE;
}
// Fix the permissions on the file since they will be 0600.
if (file_exists($filename)) {
$stat = stat($filename);
chmod($tempfile, $stat['mode']);
} else {
chmod($tempfile, 0666 & ~umask());
}
// Do the actual file_put contents
$bytes = file_put_contents($tempfile, $data, $flags, $context);
if ($bytes === FALSE) {
unlink($tempfile);
return FALSE;
}
// Move the new file into place atomically.
if (!rename($tempfile, $filename)) {
unlink($tempfile);
return FALSE;
}
return $bytes;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment