Merge pull request #3979 from eileenmcnaughton/CRM-15196
[civicrm-core.git] / CRM / Utils / File.php
index 10c14de8e591fd57c3b73371f03b532c36f43e56..97b54fa5ff9af320b9c0aa14f71fad8d3f5e2ecc 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);
+        }
       }
     }
   }
@@ -405,6 +407,10 @@ HTACCESS;
    * @param $publicDir
    */
   static function restrictBrowsing($publicDir) {
+    if (!is_dir($publicDir) || !is_writable($publicDir)) {
+      return;
+    }
+
     // base dir
     $nobrowse = realpath($publicDir) . '/index.html';
     if (!file_exists($nobrowse)) {