Beispiel #1
0
        public LoginModule()
        {
            var config = TinyfxCore.Configuration;

            _tinyfxPageRender = new TinyfxPageRender(config);

            Get("/login", _ =>
            {
                bool islogin = false;
                if (Context.CurrentUser != null && Context.CurrentUser.Identity != null && Context.CurrentUser.Identity.Name != null && Context.CurrentUser.Identity.Name.Length > 0)
                {
                    islogin = true;
                }
                return(Response.AsText(_tinyfxPageRender.RenderLogin("GET", islogin, false), "text/html"));
            });

            Post("/login", _ =>
            {
                DateTime _lastLoginFail = DateTime.MinValue;
                DateTime.TryParse(TStorage.GetInstance()["_last_login_fail"] + "", out _lastLoginFail);

                double tspan = (DateTime.Now - _lastLoginFail).TotalSeconds;

                if (tspan < TinyfxCore.Configuration.LoginRetryTimeSpanSeconds)
                {
                    return(Response.AsText(ResourceHelper.LoadStringResource("login.html").AsHtmlFromTemplate(new
                    {
                        Error = "拒绝登录,请" + (TinyfxCore.Configuration.LoginRetryTimeSpanSeconds - (int)tspan) + "秒后重试!"
                    }), "text/html"));
                }
                else
                {
                    string username = Request.Form.username;
                    string password = Request.Form.password;

                    LogHelper.WriteLog(LogHelper.LogType.INFO, "HTTP POST /login username="******",password="******"_POSTS"] = null;
                        return(this.LoginAndRedirect(uobj.Value, DateTime.Now.AddSeconds(config.AuthExpireSeconds)));
                    }
                    else
                    {
                        TStorage.GetInstance()["_last_login_fail"] = DateTime.Now;
                        return(Response.AsText(_tinyfxPageRender.RenderLogin("POST", false, false), "text/html"));
                    }
                }
            });

            Get("/logout", _ =>
            {
                TStorage.GetInstance()["_POSTS"] = null;
                return(this.LogoutAndRedirect("/login"));
            });
        }
Beispiel #2
0
        public HomeModule()
        {
            var config = TinyfxCore.Configuration;

            if (!config.IsSitePublic)
            {
                this.RequiresAuthentication();
            }

            _tinyfxPageRender = new Cores.TinyfxPageRender(config);

            Get("/", _ =>
            {
                return(Response.AsText(_tinyfxPageRender.RenderPageOrPost(1, 0), "text/html"));
            });

            Get("/page/{page}", _ =>
            {
                int page = 0;
                try
                {
                    page = _.page;
                }
                catch
                {
                    page = 1;
                }
                if (page < 1)
                {
                    page = 1;
                }
                return(Response.AsText(_tinyfxPageRender.RenderPageOrPost(page, 0), "text/html"));
            });

            Get("/post/{post}", _ =>
            {
                long post = 0;
                try
                {
                    post = _.post;
                }
                catch
                {
                    post = 0;
                }
                return(Response.AsText(_tinyfxPageRender.RenderPageOrPost(0, post), "text/html"));
            });

            Get("/files/{filename}", _ =>
            {
                string filename = _.filename;
                if (filename == null || filename.Length < 1)
                {
                    return(new NotFoundResponse());
                }
                else
                {
                    string[] seqs = filename.Split(new char[] { '_' });
                    if (seqs.Length != 3)
                    {
                        return(new NotFoundResponse());
                    }
                    else
                    {
                        string realfile = System.IO.Path.Combine(config.DataDirectory, TinyfxCore.IMAGE_UPLOAD_DIR, seqs[0], seqs[1], seqs[2]);
                        if (!String.IsNullOrEmpty(TinyfxCore.Configuration.DataDirectory))
                        {
                            realfile = System.IO.Path.Combine(config.DataDirectory, TinyfxCore.IMAGE_UPLOAD_DIR, seqs[0], seqs[1], seqs[2]);
                        }
                        if (System.IO.File.Exists(realfile))
                        {
                            string mime = "application/octet-stream";

                            string ext = System.IO.Path.GetExtension(filename);
                            if (!string.IsNullOrEmpty(ext))
                            {
                                if (TinyfxCore.Mime.ContainsKey(ext))
                                {
                                    mime = TinyfxCore.Mime[ext];
                                }
                            }

                            var fs = System.IO.File.OpenRead(realfile);

                            if (TinyfxCore.Configuration.Encryption)
                            {
                                var ms    = new System.IO.MemoryStream();
                                Faes faes = new Faes();
                                faes.Decrypt(fs, ms);
                                ms.Seek(0, System.IO.SeekOrigin.Begin);

                                Nancy.Responses.StreamResponse streamResponse = new Nancy.Responses.StreamResponse(() => { return(ms); }, mime);
                                if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif")
                                {
                                    return(streamResponse.WithHeader("Cache-Control", "max-age=315360000"));
                                }
                                else
                                {
                                    return(streamResponse);
                                }
                            }
                            else
                            {
                                Nancy.Responses.StreamResponse streamResponse = new Nancy.Responses.StreamResponse(() => { return(fs); }, mime);
                                if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif")
                                {
                                    return(streamResponse.WithHeader("Cache-Control", "max-age=315360000"));
                                }
                                else
                                {
                                    return(streamResponse);
                                }
                            }
                        }
                        else
                        {
                            return(new NotFoundResponse());
                        }
                    }
                }
            });
        }
Beispiel #3
0
        public AdminModule() : base("/admin")
        {
            _tinyfxPageRender = new Cores.TinyfxPageRender(TinyfxCore.Configuration);

            this.RequiresAuthentication();

            // 全局文本替换
            Get("/global-replace", _ => {
                bool enable = false;

                if (enable)
                {
                    // 原始
                    string srcText = "/images/";
                    // 替换为
                    string dstTest = "/files/";

                    int bb    = 0;
                    var ps    = new PressService();
                    var data  = ps.AllPosts;
                    var newpp = new List <Models.Post>();
                    foreach (var item in data)
                    {
                        var pc     = item;
                        string ori = new Faes().Decrypt(item.Content);
                        int poc    = ori.IndexOf(srcText);
                        if (poc >= 0)
                        {
                            string mr  = ori.Replace(srcText, dstTest);
                            pc.Content = new Faes().Encrypt(mr);
                            bb++;
                        }

                        newpp.Add(pc);
                    }
                    string xml = new XmlSerializor().SerializorToString(newpp);
                    return(xml);
                }
                else
                {
                    return(new NotFoundResponse());
                }
            });


            Get("/", _ =>
            {
                return(Response.AsText(_tinyfxPageRender.RenderAdminDashboard(), "text/html"));
            });

            Get("/dashboard", _ =>
            {
                return(Response.AsText(_tinyfxPageRender.RenderAdminDashboard(), "text/html"));
            });

            Get("/edit-post", _ =>
            {
                long pid      = 0;
                string pidstr = Request.Query.Pid;
                pid           = pidstr.AsLong();
                return(Response.AsText(_tinyfxPageRender.RenderCreatePost("GET", pid, null, null, false), "text/html"));
            });

            Post("/edit-post", _ =>
            {
                long pid      = 0;
                string pidstr = Request.Query.Pid;
                pid           = pidstr.AsLong();

                string title   = Request.Form.title;
                string content = Request.Form.content;
                bool isPublic  = false;
                if (Request.Form.isPublic != null && Request.Form.isPublic == "on")
                {
                    isPublic = true;
                }

                string html = _tinyfxPageRender.RenderCreatePost("POST", pid, title, content, isPublic);

                if (html != null)
                {
                    return(Response.AsText(html, "text/html"));
                }
                else
                {
                    return(Response.AsRedirect(this.ModulePath + "/post-list"));
                }
            });

            Get("/post-list", _ =>
            {
                string pageStr = Request.Query.page;
                string pidStr  = Request.Query.pid;
                string action  = Request.Query.action + "";

                int page = pageStr.AsInt();
                long pid = pidStr.AsLong();

                return(Response.AsText(_tinyfxPageRender.RenderPostList(page, action, pid), "text/html"));
            });

            Post("/upload", _ =>
            {
                var faes   = new Faes();
                var config = TinyfxCore.Configuration;
                var file   = this.Request.Files.FirstOrDefault();
                if (file != null)
                {
                    try
                    {
                        DateTime now = DateTime.Now;

                        string ext = System.IO.Path.GetExtension(file.Name).ToLower();
                        if (!TinyfxCore.Mime.ContainsKey(ext))
                        {
                            return(Response.AsJson(new { error = 3, url = "" }));
                        }
                        string filename = now.Ticks.ToString() + ext;
                        string year     = now.Year.ToString();
                        string month    = now.Month.ToString();
                        string dir      = System.IO.Path.Combine(config.DataDirectory, TinyfxCore.IMAGE_UPLOAD_DIR, year, month);
                        if (!string.IsNullOrEmpty(TinyfxCore.Configuration.DataDirectory))
                        {
                            dir = System.IO.Path.Combine(TinyfxCore.Configuration.DataDirectory, TinyfxCore.IMAGE_UPLOAD_DIR, year, month);
                        }
                        string fullname = System.IO.Path.Combine(dir, filename);
                        if (!System.IO.Directory.Exists(dir))
                        {
                            System.IO.Directory.CreateDirectory(dir);
                        }

                        if (TinyfxCore.Configuration.Encryption)
                        {
                            System.IO.MemoryStream ms = new System.IO.MemoryStream();
                            faes.Encrypt(file.Value, ms);
                            ms.Seek(0, System.IO.SeekOrigin.Begin);
                            using (var fs = System.IO.File.Open(fullname, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite))
                            {
                                ms.CopyTo(fs);
                            }
                        }
                        else
                        {
                            using (var fs = System.IO.File.Open(fullname, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite))
                            {
                                file.Value.CopyTo(fs);
                            }
                        }

                        string url = "/files/" + year + "_" + month + "_" + filename;
                        return(Response.AsJson(new { error = 0, url = url }));
                    }
                    catch (Exception)
                    {
                        return(Response.AsJson(new { error = 1, url = "" }));
                    }
                }
                else
                {
                    return(Response.AsJson(new { error = 2, url = "" }));
                }
            });

            Get("/change-password", _ =>
            {
                LogHelper.WriteLog(LogHelper.LogType.INFO, "HTTP GET /change-password", null);
                return(Response.AsText(_tinyfxPageRender.RenderChangePassword(Request.Method, null, null, null), "text/html"));
            });

            Post("/change-password", _ =>
            {
                LogHelper.WriteLog(LogHelper.LogType.INFO, "HTTP POST /change-password", null);

                string username   = Request.Form.username + "";
                string password   = Request.Form.password + "";
                string repassword = Request.Form.repassword + "";

                return(Response.AsText(_tinyfxPageRender.RenderChangePassword(Request.Method, username, password, repassword), "text/html"));
            });
        }