From 93312ae1297eb6267777eb4962aea4ec734a2daf Mon Sep 17 00:00:00 2001
From: HelloZeroNet <hello@noloop.me>
Date: Thu, 17 Sep 2015 02:20:43 +0200
Subject: [PATCH] Rev420, Ignore file delete errors, Schedule download user
 files later if first content.json failed at first try, Reload content.json
 content after cloning, Allow modals on owned sites to display coffeescript
 compile errors, Disable opener checking because its easy to dodge

---
 src/Config.py                 | 2 +-
 src/Content/ContentManager.py | 9 ++++++---
 src/Site/Site.py              | 9 +++++++++
 src/Ui/UiRequest.py           | 8 +++++++-
 src/Ui/template/wrapper.html  | 4 +++-
 5 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/src/Config.py b/src/Config.py
index db44d801..835e85f3 100644
--- a/src/Config.py
+++ b/src/Config.py
@@ -8,7 +8,7 @@ class Config(object):
 
     def __init__(self, argv):
         self.version = "0.3.2"
-        self.rev = 414
+        self.rev = 420
         self.argv = argv
         self.action = None
         self.createParser()
diff --git a/src/Content/ContentManager.py b/src/Content/ContentManager.py
index ef8a062e..0211b8fc 100644
--- a/src/Content/ContentManager.py
+++ b/src/Content/ContentManager.py
@@ -17,7 +17,7 @@ class ContentManager(object):
         self.site = site
         self.log = self.site.log
         self.contents = {}  # Known content.json (without files and includes)
-        self.loadContent(add_bad_files=False)
+        self.loadContent(add_bad_files=False, delete_removed_files=False)
         self.site.settings["size"] = self.getTotalSize()
 
     # Load content.json to self.content
@@ -66,8 +66,11 @@ class ContentManager(object):
                 if deleted:
                     # Deleting files that no longer in content.json
                     for file_inner_path in deleted:
-                        self.log.debug("Deleting file: %s" % file_inner_path)
-                        self.site.storage.delete(file_inner_path)
+                        try:
+                            self.site.storage.delete(file_inner_path)
+                            self.log.debug("Deleted file: %s" % file_inner_path)
+                        except Exception, err:
+                            self.log.debug("Error deleting file %s: %s" % (file_inner_path, err))
 
             # Load includes
             if load_includes and "includes" in new_content:
diff --git a/src/Site/Site.py b/src/Site/Site.py
index 8f371058..0a5a5537 100644
--- a/src/Site/Site.py
+++ b/src/Site/Site.py
@@ -111,6 +111,9 @@ class Site:
         found = self.needFile(inner_path, update=self.bad_files.get(inner_path))
         content_inner_dir = self.content_manager.toDir(inner_path)
         if not found:
+            self.log.debug("Download %s failed, check_modifications: %s" % (inner_path, check_modifications))
+            if check_modifications:  # Download failed, but check modifications if its succed later
+                self.onFileDone.once(lambda file_name: self.checkModifications(0), "check_modifications")
             return False  # Could not download content.json
 
         self.log.debug("Got %s" % inner_path)
@@ -415,9 +418,15 @@ class Site:
                         )
                         if privatekey:
                             new_site.content_manager.sign(file_inner_path.replace("-default", ""), privatekey)
+                            new_site.content_manager.loadContent(
+                                file_inner_path, add_bad_files=False, delete_removed_files=False, load_includes=False
+                            )
 
         if privatekey:
             new_site.content_manager.sign("content.json", privatekey)
+            new_site.content_manager.loadContent(
+                "content.json", add_bad_files=False, delete_removed_files=False, load_includes=False
+            )
 
         # Rebuild DB
         if new_site.storage.isFile("dbschema.json"):
diff --git a/src/Ui/UiRequest.py b/src/Ui/UiRequest.py
index 31c62d65..9e2986c4 100644
--- a/src/Ui/UiRequest.py
+++ b/src/Ui/UiRequest.py
@@ -250,6 +250,11 @@ class UiRequest(object):
             if content.get("viewport"):
                 meta_tags += '<meta name="viewport" id="viewport" content="%s">' % cgi.escape(content["viewport"], True)
 
+        if site.settings.get("own"):
+            sandbox_permissions = "allow-modals"  # For coffeescript compile errors
+        else:
+            sandbox_permissions = ""
+
         return self.render(
             "src/Ui/template/wrapper.html",
             server_url=server_url,
@@ -264,6 +269,7 @@ class UiRequest(object):
             wrapper_key=site.settings["wrapper_key"],
             permissions=json.dumps(site.settings["permissions"]),
             show_loadingscreen=json.dumps(not site.storage.isFile(file_inner_path)),
+            sandbox_permissions=sandbox_permissions,
             rev=config.rev,
             homepage=homepage
         )
@@ -461,7 +467,7 @@ class UiRequest(object):
         self.sendHeader(500)
         return self.formatError("Server error", cgi.escape(message))
 
-    def formatError(self, title, message):
+    def formatError(self, title, message, details = True):
         import sys
         import gevent
 
diff --git a/src/Ui/template/wrapper.html b/src/Ui/template/wrapper.html
index 20cca539..1ed29c71 100644
--- a/src/Ui/template/wrapper.html
+++ b/src/Ui/template/wrapper.html
@@ -18,9 +18,11 @@ if (window.self !== window.top) window.stop();
 if (window.self !== window.top && document.execCommand) document.execCommand("Stop", false)
 
 // Dont allow site to load in a popup
+/*
 if (window.opener) document.write("Opener not allowed")
 if (window.opener && document.execCommand) document.execCommand("Stop", false)
 if (window.opener && window.stop) window.stop()
+*/
 </script>
 
 <div class="progressbar">
@@ -52,7 +54,7 @@ if (window.opener && window.stop) window.stop()
 
 
 <!-- Site Iframe -->
-<iframe src='about:blank' id='inner-iframe' sandbox="allow-forms allow-scripts allow-top-navigation allow-popups"></iframe>
+<iframe src='about:blank' id='inner-iframe' sandbox="allow-forms allow-scripts allow-top-navigation allow-popups {sandbox_permissions}"></iframe>
 
 <!-- Site info -->
 <script>