CRM-14091, CRM-14092 - restrictAccess - Avoid unnecessary writes
authorTim Otten <totten@civicrm.org>
Thu, 6 Feb 2014 16:52:50 +0000 (08:52 -0800)
committerTim Otten <totten@civicrm.org>
Thu, 6 Feb 2014 16:52:50 +0000 (08:52 -0800)
CRM/Utils/File.php

index 10c14de8e591fd57c3b73371f03b532c36f43e56..1e4aee46244bdb0dd54c606a7370e5b080bb9f94 100644 (file)
@@ -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);
+        }
       }
     }
   }