Exemple #1
0
        public static bool IsDirectory(Data.Models.WebSite website, string fullpath)
        {
            if (fullpath.EndsWith("\\"))
            {
                return(true);
            }

            string path = "\\" + PathPrefix + "\\";

            if (fullpath.IndexOf(path, StringComparison.OrdinalIgnoreCase) > -1)
            {
                int    index     = fullpath.IndexOf(path, StringComparison.OrdinalIgnoreCase);
                string substring = fullpath.Substring(index + path.Length);
                if (substring.StartsWith("\\"))
                {
                    substring = substring.Substring(1);
                }
                substring = substring.TrimEnd('\\');
                if (substring.IndexOf("\\") > -1)
                {
                    return(false);
                }
                return(true);
            }

            else
            {
                string relative = GetRelativeUrl(website, fullpath);
                if (string.IsNullOrEmpty(relative))
                {
                    return(true);
                }
                var route = website.SiteDb().Routes.GetByUrl(relative);
                if (route != null && route.objectId != default(Guid))
                {
                    return(false);
                }

                if (relative.EndsWith("\\") || relative.EndsWith("/"))
                {
                    return(true);
                }

                int lastindex     = relative.LastIndexOf("\\");
                int lastbackslash = relative.LastIndexOf("/");
                if (lastbackslash > lastindex)
                {
                    lastindex = lastbackslash;
                }
                if (lastindex > -1)
                {
                    string lastpart = relative.Substring(lastindex);
                    return(!lastpart.Contains("."));
                }
                else
                {
                    return(!relative.Contains("."));
                }
            }
        }
Exemple #2
0
        public void DiskSyncUpdate(ApiCall call)
        {
            Data.Models.WebSite website = null;

            Guid SiteId = call.GetGuidValue("SiteId");

            website = Kooboo.Data.GlobalDb.WebSites.Get(SiteId);
            if (SiteId == default(Guid) || website == null)
            {
                return;
            }

            bool   enable = call.GetBoolValue("EnableDiskSync");
            string path   = call.GetValue("localpath");

            bool hasSamePath = Lib.Helper.StringHelper.IsSameValue(website.DiskSyncFolder, path);

            if (website.EnableDiskSync != enable || Lib.Helper.StringHelper.IsSameValue(website.DiskSyncFolder, path) == false)
            {
                website.EnableDiskSync = enable;
                website.DiskSyncFolder = path;
                GlobalDb.WebSites.AddOrUpdate(website);
            }

            if (enable)
            {
                // init disk..
                if (!hasSamePath)
                {
                }
                WebSiteService.InitDiskSync(website, true);
            }
        }
Exemple #3
0
        public List <CssRuleViewModel> GetCssRuleList(List <CmsCssRule> cssRules, Data.Models.WebSite website)
        {
            List <CssRuleViewModel> rules = new List <CssRuleViewModel>();

            foreach (var item in cssRules)
            {
                var ruleType = item.ruleType;
                if (item.ruleType == RuleType.ImportRule)
                {
                    CssRuleViewModel rule = new CssRuleViewModel();
                    rule.Selector = item.CssText;
                    rule.RuleType = ruleType;
                    rule.Id       = item.Id;
                    rules.Add(rule);
                }
                else if (item.ruleType == RuleType.MediaRule)
                {
                    CssRuleViewModel rule = new CssRuleViewModel();
                    rule.Selector = item.SelectorText;
                    rule.RuleType = ruleType;
                    List <CmsCssRule> subrules = website.SiteDb().CssRules.Query.Where(o => o.ParentCssRuleId == item.Id).SelectAll();

                    rule.Rules = this.GetCssRuleList(subrules, website);
                    rule.Id    = item.Id;
                    rules.Add(rule);
                }
                else
                {
                    // style rule or font face rule.

                    CssRuleViewModel rule = new CssRuleViewModel();
                    rule.RuleType = ruleType;
                    List <DeclarationViewModel> subdeclarations = new List <DeclarationViewModel>();

                    var cssdecls = Kooboo.Dom.CSS.CSSSerializer.deserializeDeclarationBlock(item.RuleText);

                    foreach (var jitem in cssdecls.item)
                    {
                        subdeclarations.Add(new DeclarationViewModel()
                        {
                            Name = jitem.propertyname, Value = jitem.value, Important = jitem.important
                        });
                    }

                    rule.Declarations = subdeclarations;
                    rule.Selector     = item.SelectorText;
                    rule.Id           = item.Id;
                    rules.Add(rule);
                }
            }

            return(rules);
        }
Exemple #4
0
        public static string CorrectCssUrl(Data.Models.WebSite website, string CssText, string FilePath)
        {
            if (website == null || string.IsNullOrEmpty(website.DiskSyncFolder))
            {
                return(CssText);
            }

            string FileRelativePath = RemoveBaseCaseInsensitive(FilePath, website.DiskSyncFolder);

            FileRelativePath = Kooboo.Lib.Helper.UrlHelper.ReplaceBackSlash(FileRelativePath, true);
            return(ProcessCssText(CssText, FileRelativePath));
        }
Exemple #5
0
 public static List <Guid> GetClusterTo(Data.Models.WebSite WebSite)
 {
     lock (_locker)
     {
         if (!ClusterTo.ContainsKey(WebSite.Id))
         {
             var tos = CalculateClusterTo(WebSite);
             ClusterTo[WebSite.Id] = tos;
         }
         return(ClusterTo[WebSite.Id]);
     }
 }
Exemple #6
0
        public static void StartDiskWatcher(Data.Models.WebSite WebSite)
        {
            if (!WebSite.EnableDiskSync)
            {
                return;
            }

            if (!watchers.ContainsKey(WebSite.Id))
            {
                lock (_lockobject)
                {
                    if (!watchers.ContainsKey(WebSite.Id))
                    {
                        var hash = Lib.Security.Hash.ComputeGuidIgnoreCase(WebSite.DiskSyncFolder);

                        if (PathHash.Contains(hash))
                        {
                            return;
                        }

                        try
                        {
                            IOHelper.EnsureDirectoryExists(WebSite.DiskSyncFolder);
                        }
                        catch (Exception)
                        {
                            return;
                        }

                        FileSystemWatcher watcher = new System.IO.FileSystemWatcher(WebSite.DiskSyncFolder);

                        watcher.IncludeSubdirectories = true;

                        watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.DirectoryName;

                        DiskSyncManager manager = DiskSyncHelper.GetSyncManager(WebSite.Id);

                        watcher.Changed += new FileSystemEventHandler(manager.OnChanged);
                        watcher.Created += new FileSystemEventHandler(manager.OnChanged);
                        watcher.Deleted += new FileSystemEventHandler(manager.OnChanged);
                        watcher.Renamed += new RenamedEventHandler(manager.OnRenamed);

                        watcher.EnableRaisingEvents = true;

                        watchers[WebSite.Id] = watcher;

                        PathHash.Add(hash);
                    }
                }
            }
        }
Exemple #7
0
        public void Post(ApiCall call)
        {
            var siteid = call.GetGuidValue("SiteId");

            Data.Models.WebSite website = Data.GlobalDb.WebSites.Get(siteid);

            if (website == null)
            {
                website = call.WebSite;
            }
            var setting = call.Context.Request.Model as SyncSetting;

            if (website != null)
            {
                ///TODO: if remotesiteId == default(guid), call to create remote site id...
                // url... /_api/site/create, FullDomain, SiteName....

                if (!setting.RemoteServerUrl.ToLower().StartsWith("http"))
                {
                    setting.RemoteServerUrl = "http://" + setting.RemoteServerUrl;
                }

                if (setting.RemoteWebSiteId == default(Guid))
                {
                    string FullDomain = call.GetValue("FullDomain");
                    string SiteName   = call.GetValue("SiteName");

                    if (!string.IsNullOrEmpty(FullDomain) && !string.IsNullOrEmpty(SiteName))
                    {
                        string url = setting.RemoteServerUrl + "/_api/site/create";
                        Dictionary <string, string> para = new Dictionary <string, string>();
                        para.Add("FullDomain", FullDomain);
                        para.Add("SiteName", SiteName);

                        var newsite = Lib.Helper.HttpHelper.Get <WebSite>(url, para, call.Context.User.UserName, call.Context.User.PasswordHash.ToString());

                        if (newsite != null)
                        {
                            setting.RemoteSiteName  = newsite.Name;
                            setting.RemoteWebSiteId = newsite.Id;
                        }
                    }
                }
                if (setting.RemoteWebSiteId != default(Guid))
                {
                    website.SiteDb().SyncSettings.AddOrUpdate(setting, call.Context.User.Id);
                }
            }
        }
Exemple #8
0
        private string GetStartRelativeUrl(Data.Models.WebSite site)
        {
            var startpages = site.StartPages();

            if (startpages != null && startpages.Count() > 0)
            {
                foreach (var item in startpages)
                {
                    Route route = site.SiteDb().Routes.Query.Where(o => o.objectId == item.Id).FirstOrDefault();

                    if (route != null && !route.Name.Contains("{") && !route.Name.Contains("%"))
                    {
                        return(route.Name);
                    }
                }
            }

            var allpages = site.SiteDb().Pages.All();

            if (allpages != null && allpages.Count() > 0)
            {
                foreach (var item in allpages)
                {
                    Route route = site.SiteDb().Routes.Query.Where(o => o.objectId == item.Id).FirstOrDefault();

                    if (route != null && !route.Name.Contains("{") && !route.Name.Contains("%"))
                    {
                        return(route.Name);
                    }
                }
            }

            if (allpages != null && allpages.Count() > 0)
            {
                foreach (var item in allpages)
                {
                    Route route = site.SiteDb().Routes.Query.Where(o => o.objectId == item.Id).FirstOrDefault();

                    if (route != null)
                    {
                        return(route.Name);
                    }
                }
            }

            return("/");
        }
Exemple #9
0
        public static void StopDiskWatcher(Data.Models.WebSite website)
        {
            lock (_lockobject)
            {
                if (watchers.ContainsKey(website.Id))
                {
                    var watcher = watchers[website.Id];
                    watcher.EnableRaisingEvents = false;
                    watchers.Remove(website.Id);
                    watcher.Dispose();
                    DiskSyncHelper.RemoveDiskSyncManager(website.Id);

                    var hash = Lib.Security.Hash.ComputeGuidIgnoreCase(website.DiskSyncFolder);

                    PathHash.Remove(hash);
                }
            }
        }
Exemple #10
0
        internal static List <Guid> CalculateClusterTo(Data.Models.WebSite WebSite)
        {
            HashSet <Guid> result = new HashSet <Guid>();

            var store = Stores.ClusterNodes(WebSite.SiteDb());

            var allitems = store.Filter.SelectAll().OrderBy(o => o.ServerUrl).ToList();

            var count = allitems.Count();

            if (count == 0)
            {
                return(new List <Guid>());
            }
            int currentposition = -1;

            for (int i = 0; i < count; i++)
            {
                if (allitems[i].ServerWebSiteId == WebSite.Id)
                {
                    currentposition = i;
                }
            }

            int one   = currentposition + 1;
            int two   = one + 2;
            int three = two + 4;

            one   = CorrectIndex(one, count);
            two   = CorrectIndex(two, count);
            three = CorrectIndex(three, count);

            result.Add(allitems[one].Id);
            result.Add(allitems[two].Id);
            result.Add(allitems[three].Id);
            if (currentposition > -1)
            {
                result.Remove(allitems[currentposition].Id);
            }

            return(result.ToList());
        }
Exemple #11
0
        public static string GetFullDiskPath(Data.Models.WebSite WebSite, string relativeUrl)
        {
            relativeUrl = ReplaceQuestionMark(relativeUrl, false);

            var relativepath = EscapeChar(relativeUrl, false);

            string FullPath = Lib.Compatible.CompatibleManager.Instance.System.CombinePath(WebSite.DiskSyncFolder, relativepath);

            if (FullPath.EndsWith("/") || FullPath.EndsWith("\\"))
            {
                FullPath = FullPath + FolderFileName;
            }
            else
            {
                System.IO.FileInfo file = new System.IO.FileInfo(FullPath);
                if (String.IsNullOrEmpty(file.Extension))
                {
                    FullPath = FullPath + DefaultExtension;
                }
            }

            return(FullPath);
        }
Exemple #12
0
        public static string GetRelativeUrl(Data.Models.WebSite WebSite, string FullPath)
        {
            if (FullPath.EndsWith(FolderFileName, StringComparison.OrdinalIgnoreCase))
            {
                FullPath = FullPath.Substring(0, FullPath.Length - FolderFileName.Length);
            }
            else if (FullPath.EndsWith(DefaultExtension))
            {
                FullPath = FullPath.Substring(0, FullPath.Length - DefaultExtension.Length);
            }

            FullPath = EscapeChar(FullPath, true);

            string relative = Lib.Helper.StringHelper.ReplaceIgnoreCase(FullPath, WebSite.DiskSyncFolder, "");

            if (!string.IsNullOrEmpty(relative))
            {
                relative = relative.Replace("\\", "/");
            }

            relative = ReplaceQuestionMark(relative, true);

            return(relative);
        }
Exemple #13
0
        public static string CorrectDomUrl(Data.Models.WebSite WebSite, string PageSource, string FilePath)
        {
            if (WebSite == null || string.IsNullOrEmpty(WebSite.DiskSyncFolder))
            {
                return(PageSource);
            }

            if (string.IsNullOrEmpty(PageSource))
            {
                return(PageSource);
            }
            string FileRelativePath = RemoveBaseCaseInsensitive(FilePath, WebSite.DiskSyncFolder);

            FileRelativePath = FileRelativePath.Replace("\\", "/");

            if (!FileRelativePath.StartsWith("/"))
            {
                FileRelativePath = "/" + FileRelativePath;
            }

            var dom = Kooboo.Dom.DomParser.CreateDom(PageSource);

            List <SourceUpdate> updates = new List <SourceUpdate>();

            foreach (var item in dom.Links.item)
            {
                string itemsrc = Service.DomUrlService.GetLinkOrSrc(item);

                if (string.IsNullOrEmpty(itemsrc))
                {
                    continue;
                }

                string rightsrc = itemsrc.Replace("\\", "/");

                string RelativeUrl = Kooboo.Lib.Helper.UrlHelper.Combine(FileRelativePath, rightsrc);

                if (itemsrc != RelativeUrl)
                {
                    string oldstring = Kooboo.Sites.Service.DomService.GetOpenTag(item);
                    string newstring = oldstring.Replace(itemsrc, RelativeUrl);


                    updates.Add(new SourceUpdate()
                    {
                        StartIndex = item.location.openTokenStartIndex,
                        EndIndex   = item.location.openTokenEndIndex,
                        NewValue   = newstring
                    });
                }
            }

            if (updates.Count > 0)
            {
                return(Kooboo.Sites.Service.DomService.UpdateSource(PageSource, updates));
            }
            else
            {
                return(PageSource);
            }
        }
Exemple #14
0
        internal void ProcessFromDisk()
        {
            if (this.CanAccept)
            {
                Interlocked.Increment(ref this.CurrentThreadCount);
                DiskChangeEvent task = PeekTask();

                if (task != null)
                {
                    try
                    {
                        Data.Models.WebSite website = Data.GlobalDb.WebSites.Get(WebSiteId);
                        var sitedb = website.SiteDb();

                        if (task.ChangeType == DiskChangeType.Rename)
                        {
                            if (DiskPathService.IsDirectory(website, task.OldFullPath) && DiskPathService.IsDirectory(website, task.FullPath))
                            {
                                SyncService.DiskFolderRename(sitedb, task.OldFullPath, task.FullPath);
                            }
                            else
                            {
                                SyncService.DiskFileRename(sitedb, task.OldFullPath, task.FullPath);
                            }
                        }
                        else if (task.ChangeType == DiskChangeType.Deleted)
                        {
                            if (DiskPathService.IsDirectory(website, task.FullPath))
                            {
                                SyncService.DeleteDiskFolder(task.FullPath, sitedb);
                            }
                            else
                            {
                                this.DeleteFromDb(task.FullPath, sitedb);
                            }
                        }
                        else
                        {
                            //if (task.ChangeType == DiskChangeType.Created)
                            //{
                            //    contentbytes = new byte[0];
                            //}

                            this.SyncToDb(task.FullPath, sitedb, null);
                        }
                    }
                    catch (Exception ex)
                    {
                        var error = ex.Message;
                    }

                    this.RemoveTask(task);
                }
                Interlocked.Decrement(ref this.CurrentThreadCount);

                if (task != null)
                {
                    startNewFromDisk();
                }
            }
        }