From ea3b22b5d6931945d97cb6fc4cf9a5ea9020ca6b Mon Sep 17 00:00:00 2001
From: Tim Otten <totten@civicrm.org>
Date: Thu, 6 Feb 2014 08:52:50 -0800
Subject: [PATCH] CRM-14091, CRM-14092 - restrictAccess - Avoid unnecessary
 writes

---
 CRM/Utils/File.php | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/CRM/Utils/File.php b/CRM/Utils/File.php
index 10c14de8e5..1e4aee4624 100644
--- a/CRM/Utils/File.php
+++ b/CRM/Utils/File.php
@@ -380,11 +380,11 @@ class CRM_Utils_File {
    *
    * @param string $dir  the directory to be secured
    */
-  static function restrictAccess($dir) {
+  static function restrictAccess($dir, $overwrite = FALSE) {
     // note: empty value for $dir can play havoc, since that might result in putting '.htaccess' to root dir
     // of site, causing site to stop functioning.
     // FIXME: we should do more checks here -
-    if (!empty($dir)) {
+    if (!empty($dir) && is_dir($dir)) {
       $htaccess = <<<HTACCESS
 <Files "*">
   Order allow,deny
@@ -393,8 +393,10 @@ class CRM_Utils_File {
 
 HTACCESS;
       $file = $dir . '.htaccess';
-      if (file_put_contents($file, $htaccess) === FALSE) {
-        CRM_Core_Error::movedSiteError($file);
+      if ($overwrite || !file_exists($file)) {
+        if (file_put_contents($file, $htaccess) === FALSE) {
+          CRM_Core_Error::movedSiteError($file);
+        }
       }
     }
   }
-- 
GitLab