Esempio n. 1
0
        public oResult job_remove(Dictionary <string, object> paramenter = null, Dictionary <string, object> request = null)
        {
            if (paramenter == null)
            {
                paramenter = new Dictionary <string, object>();
            }
            if (request == null)
            {
                request = new Dictionary <string, object>();
            }
            oResult r = new oResult()
            {
                ok = false, request = request, input = paramenter
            };

            try
            {
                if (paramenter.Count == 0 ||
                    !paramenter.ContainsKey("job_name"))
                {
                    r.error = "Paramenter missing job_name";
                    return(r);
                }
                var job_name = paramenter.getValue("job_name");
                r.ok = clsGlobal.remove_job(job_name);
            }
            catch (Exception e)
            {
                r.error = "ERROR_THROW: API[job_remove] " + e.Message;
            }
            return(r);
        }
Esempio n. 2
0
        public oResult job_list(Dictionary <string, object> paramenter = null, Dictionary <string, object> request = null)
        {
            if (paramenter == null)
            {
                paramenter = new Dictionary <string, object>();
            }
            if (request == null)
            {
                request = new Dictionary <string, object>();
            }
            oResult r = new oResult()
            {
                ok = false, request = request, input = paramenter
            };

            try
            {
                r.ok   = true;
                r.data = clsGlobal.list_jobs();
            }
            catch (Exception e)
            {
                r.error = "ERROR_THROW: API[job_list] " + e.Message;
            }
            return(r);
        }
Esempio n. 3
0
        public oResult db_execute(Dictionary <string, object> paramenter = null, Dictionary <string, object> request = null)
        {
            if (paramenter == null)
            {
                paramenter = new Dictionary <string, object>();
            }
            if (request == null)
            {
                request = new Dictionary <string, object>();
            }
            oResult r = new oResult()
            {
                ok = false, request = request, input = paramenter
            };

            try
            {
                if (paramenter.Count == 0 ||
                    !paramenter.ContainsKey("connect_string") ||
                    !paramenter.ContainsKey("script_command"))
                {
                    r.error = "Paramenter missing connect_string or script_command";
                    return(r);
                }

                var connect_string = paramenter.getValue("connect_string");
                var script_command = paramenter.getValue("script_command");

                using (SqlConnection connection = new SqlConnection(connect_string))
                {
                    SqlCommand cmd = new SqlCommand(script_command, connection);

                    foreach (var kv in paramenter)
                    {
                        cmd.Parameters.AddWithValue("@" + kv.Key, kv.Value);
                    }

                    foreach (var kv in request)
                    {
                        cmd.Parameters.AddWithValue("@" + kv.Key, kv.Value);
                    }

                    SqlDataAdapter dataAdapt = new SqlDataAdapter();
                    dataAdapt.SelectCommand = cmd;
                    DataTable dataTable = new DataTable();
                    dataAdapt.Fill(dataTable);

                    r.ok   = true;
                    r.data = dataTable;
                }
            }
            catch (Exception e)
            {
                r.error = "ERROR_THROW: API[db_execute] " + e.Message;
            }
            return(r);
        }
Esempio n. 4
0
        public static oResult test_http(Dictionary <string, object> request = null)
        {
            oResult rs = new oResult()
            {
                ok = false, request = request
            };

            string url = request.getValue <string>("url");

            if (string.IsNullOrWhiteSpace(url))
            {
                url = "http://localhost/";
            }

            try
            {
                Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

                Easy               easy = new Easy();
                StringBuilder      bi   = new StringBuilder();
                Easy.WriteFunction wf   = new Easy.WriteFunction((buf, size, nmemb, extraData) =>
                {
                    string si = Encoding.UTF8.GetString(buf);
                    bi.Append(si);
                    return(size * nmemb);
                });

                easy.SetOpt(CURLoption.CURLOPT_URL, url);
                easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);

                easy.Perform();
                //easy.Cleanup();
                easy.Dispose();

                Curl.GlobalCleanup();

                rs.data = bi.ToString();
                rs.ok   = true;
                rs.type = DATA_TYPE.HTML_TEXT;
            }
            catch (Exception ex)
            {
                rs.error = ex.Message;
            }

            return(rs);
        }
Esempio n. 5
0
        public oResult job_create(Dictionary <string, object> paramenter = null, Dictionary <string, object> request = null)
        {
            if (paramenter == null)
            {
                paramenter = new Dictionary <string, object>();
            }
            if (request == null)
            {
                request = new Dictionary <string, object>();
            }
            oResult r = new oResult()
            {
                ok = false, request = request, input = paramenter
            };

            try
            {
                if (paramenter.Count == 0 ||
                    !paramenter.ContainsKey("type") ||
                    !paramenter.ContainsKey("group_name") ||
                    !paramenter.ContainsKey("schedule"))
                {
                    r.error = "Paramenter missing type or group_name or schedule";
                    return(r);
                }

                var type       = (JOB_TYPE)paramenter.getValue <int>("type");
                var group_name = paramenter.getValue("group_name");
                var schedule   = paramenter.getValue("schedule");

                r.name = clsGlobal.create_job(type, group_name, paramenter, schedule);

                r.ok = string.IsNullOrEmpty(r.name) == false;
            }
            catch (Exception e)
            {
                r.error = "ERROR_THROW: API[job_create] " + e.Message;
            }
            return(r);
        }
Esempio n. 6
0
        public oResult cache_get_items_by_ids(Dictionary <string, object> paramenter = null, Dictionary <string, object> request = null)
        {
            if (paramenter == null)
            {
                paramenter = new Dictionary <string, object>();
            }
            if (request == null)
            {
                request = new Dictionary <string, object>();
            }
            oResult r = new oResult()
            {
                ok = false, request = request, input = paramenter
            };

            try { }
            catch (Exception e)
            {
                r.error = "ERROR_THROW: API[] " + e.Message;
            }
            return(r);
        }
Esempio n. 7
0
        public oResult html_remove_tag_keep_content(Dictionary <string, object> paramenter = null, Dictionary <string, object> request = null)
        {
            if (paramenter == null)
            {
                paramenter = new Dictionary <string, object>();
            }
            if (request == null)
            {
                request = new Dictionary <string, object>();
            }
            oResult r = new oResult()
            {
                ok = false, request = request, input = paramenter
            };

            try { }
            catch (Exception e)
            {
                r.error = "ERROR_THROW: API[] " + e.Message;
            }
            return(r);
        }
Esempio n. 8
0
        oResult get_api_parameters()
        {
            oResult r = oResult.Error();

            string path = Request.Url.AbsolutePath.Substring(1).ToLower();
            Dictionary <string, object> para = new Dictionary <string, object>()
            {
                { "___domain", Request.Url.Host },
                { "___port", Request.Url.Port },
                { "___url", path },
                { "___method", Request.HttpMethod },
                { "___token", string.Empty },
            };

            try
            {
                var a = Request.QueryString.Keys.Cast <string>().ToArray();
                foreach (var key in a)
                {
                    if (!para.ContainsKey(key))
                    {
                        para.Add(key, Uri.UnescapeDataString(Request.QueryString[key]));
                    }
                }

                if (Request.HttpMethod == "POST")
                {
                    string s = new StreamReader(Request.InputStream).ReadToEnd();
                    if (s == null || string.IsNullOrWhiteSpace(s))
                    {
                        return(oResult.Error("Body of POST is not null or emtpy"));
                    }
                    else
                    {
                        s = s.Trim();
                        if (s.Length > 1 && s[0] == '{' && s[s.Length - 1] == '}')
                        {
                            try
                            {
                                var d = JsonConvert.DeserializeObject <Dictionary <string, object> >(s);
                                foreach (var kv in d)
                                {
                                    para.Add(kv.Key, kv.Value);
                                }
                            }
                            catch (Exception e)
                            {
                                return(oResult.Error("Convert to json of body error: " + e.Message));
                            }
                        }
                        else
                        {
                            para.Add("___BODY", s);
                        }
                    }
                }

                r.ok   = true;
                r.data = para;
            }
            catch (Exception e)
            {
                r.error = "ERROR[clsRouter.get_api_parameters()] " + e.Message;
            }

            return(r);
        }
Esempio n. 9
0
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            Uri uri = Request.Url;

            string path = uri.AbsolutePath.Substring(1);

            if (path == "favicon.ico")
            {
                Response.End(); return;
            }
            string[] a = path.Split('/');

            string domain = uri.Host;

            if (domain == "localhost" || domain == "127.0.0.1")
            {
                domain = _CONFIG.DOMAIN_LOCALHOST;
            }

            //[1] api/{cache_name}/{api_name}/{id}
            if (a[0] == "api" && a.Length > 2)
            {
                Response.ContentType = "application/json";
                if (m_cache.existCache_ApiJS(a[1], a[2]))
                {
                    Response.Write(JsonConvert.SerializeObject(oResult.Error("Cannot found cache name: " + a[1])));
                    Response.End();
                    return;
                }

                oResult rv = null;
                oResult rp = get_api_parameters();
                if (rp.ok)
                {
                    rv = clsEngineJS.Execute(a[1] + "___" + a[2], (Dictionary <string, object>)rp.data, null);
                }
                else
                {
                    rv = rp;
                }

                Response.Write(JsonConvert.SerializeObject(rv));
                Response.End();
                return;
            }

            //[2] Files
            string file   = string.Empty;
            bool   isHome = false;

            if (path.Length == 0)
            {
                isHome = true; path = "index.html";
            }
            if (path.EndsWith(".html"))
            {
                path = "_site\\" + domain + "\\" + path;
            }
            if (path == "admin")
            {
                path = "admin.html";
            }

            string contentType = MimeMapping.GetMimeMapping(path);

            if (path[0] != '_' && // break dirs: _lib, _view,..
                contentType != "text/html" && // break files *.html
                Request.UrlReferrer != null)    // Ref from other request as request on page html
            {
                path = "_site\\" + domain + "\\" + path;
            }

            file = Path.Combine(_CONFIG.PATH_ROOT, path).Replace("/", "\\");

            if (File.Exists(file))
            {
                Response.ContentType = contentType;
                Response.TransmitFile(file);
            }
            else
            {
                if (isHome)
                {
                    Response.ContentType = contentType;
                    Response.Write("<h1>Cannot found page " + domain + "</h1>");
                }
                else
                {
                    Response.StatusCode = 404;
                }
            }

            Response.End();
        }
Esempio n. 10
0
        public static oResult test_https(Dictionary <string, object> request = null)
        {
            oResult rs = new oResult()
            {
                ok = false, request = request
            };

            string url = request.getValue <string>("url");

            if (string.IsNullOrWhiteSpace(url))
            {
                url = "https://vnexpress.net/ha-noi-de-xuat-keo-dai-cach-ly-xa-hoi-den-30-4-4084947.html";
            }

            try
            {
                Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

                Easy easy = new Easy();

                StringBuilder      bi = new StringBuilder();
                Easy.WriteFunction wf = new Easy.WriteFunction((buf, size, nmemb, extraData) =>
                {
                    string si = Encoding.UTF8.GetString(buf);
                    bi.Append(si);
                    return(size * nmemb);
                });
                easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);

                Easy.SSLContextFunction sf = new Easy.SSLContextFunction((ctx, extraData) => CURLcode.CURLE_OK);
                easy.SetOpt(CURLoption.CURLOPT_SSL_CTX_FUNCTION, sf);

                easy.SetOpt(CURLoption.CURLOPT_URL, url);
                //easy.SetOpt(CURLoption.CURLOPT_CAINFO, "ca-bundle.crt");
                string file_crt = Path.Combine(_CONFIG.PATH_ROOT, @"bin\ca-bundle.crt");
                if (File.Exists(file_crt) == false)
                {
                    rs.error = "Cannot found file: " + file_crt;
                    return(rs);
                }
                easy.SetOpt(CURLoption.CURLOPT_CAINFO, file_crt);

                easy.Perform();
                easy.Dispose();

                Curl.GlobalCleanup();

                string s = bi.ToString();

                //string title = Regex.Match(s, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;

                s = new Regex(@"<script[^>]*>[\s\S]*?</script>").Replace(s, string.Empty);
                s = new Regex(@"<style[^>]*>[\s\S]*?</style>").Replace(s, string.Empty);
                s = new Regex(@"<noscript[^>]*>[\s\S]*?</noscript>").Replace(s, string.Empty);
                s = Regex.Replace(s, @"<meta(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<link(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<use(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<figure(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<!DOCTYPE(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<!--(.|\n)*?-->", string.Empty, RegexOptions.Singleline);

                s = Regex.Replace(s, @"(?:\r\n|\r(?!\n)|(?<!\r)\n){2,}", "\r\n");

                rs.data = s;
                rs.ok   = true;
                rs.type = DATA_TYPE.HTML_TEXT;
            }
            catch (Exception ex)
            {
                rs.error = "ERR: " + ex.Message;
            }

            return(rs);
        }
Esempio n. 11
0
        public static oResult Execute(string file___api, Dictionary <string, object> parameters = null, Dictionary <string, object> request = null)
        {
            if (request == null)
            {
                request = new Dictionary <string, object>();
            }
            if (parameters == null)
            {
                parameters = new Dictionary <string, object>();
            }

            oResult r = new oResult()
            {
                ok      = false,
                name    = file___api,
                input   = parameters,
                request = request
            };

            string file = Path.Combine(_CONFIG.PATH_ROOT, "_api\\" + file___api + ".js");

            if (File.Exists(file) == false)
            {
                r.error = "ERROR[clsEngineJS.Execute] Cannot found file: " + file;
                return(r);
            }

            if (!file.Contains("___"))
            {
                r.error = "ERROR[clsEngineJS.Execute] file___api: " + file___api + " must be format cache_name___api_name ";
                return(r);
            }

            string[] a = file___api.Split(new string[] { "___" }, StringSplitOptions.None);
            string   cache_name = a[0], api_name = a[1];

            try
            {
                string js = string.Empty;
                js = @"
                (function() {
                    'use strict';
                    try {
                        var ___log_text = function(text){ log___.write('" + file___api + @"', '_', text); };
                        var ___log_key = function(key, text){ log___.write('" + file___api + @"', key, text); };
                        var ___api_call = function(api_name, paramenter, request, result_type){ var v = ___api.js_call(api_name, JSON.stringify(paramenter), JSON.stringify(request)); if(result_type == 'text') return v; else return JSON.parse(v); };

                        var ___request = " + JsonConvert.SerializeObject(request) + @";
                        var ___parameters = " + JsonConvert.SerializeObject(parameters) + @";

                        ___log_text('This is from JavaScript...');

                        " + File.ReadAllText(file) + @"

                    } catch(e) {
                        return { ok: false, name: '" + file___api + @"', error: e.message };
                    }
                })();
                ";

                var toReturn = m_engine.Evaluate(js);
                if (toReturn is Microsoft.ClearScript.Undefined)
                {
                    r.ok = true;
                }
                else if (toReturn is string)
                {
                    string json = toReturn.ToString();
                    try
                    {
                        r = JsonConvert.DeserializeObject <oResult>(json);
                    }
                    catch (Exception ejs)
                    {
                        r.error = ejs.Message;
                        r.data  = json;
                    }
                }

                //////else
                //////{
                //////    try
                //////    {
                //////        dynamic dynamicResult = toReturn;
                //////        foreach (string name in dynamicResult.GetDynamicMemberNames())
                //////        {
                //////            switch (name)
                //////            {
                //////                case "ok":
                //////                    r.ok = Convert.ToBoolean(dynamicResult[name]);
                //////                    break;
                //////                case "request":
                //////                    break;
                //////                case "input":
                //////                    break;
                //////                case "name":
                //////                    r.name = Convert.ToString(dynamicResult[name]);
                //////                    break;
                //////                case "error":
                //////                    r.error = Convert.ToString(dynamicResult[name]);
                //////                    break;
                //////            }
                //////        }
                //////    }
                //////    catch (Exception ejs)
                //////    {
                //////        r.error = ejs.Message;
                //////    }
                //////}
            }
            catch (Exception e)
            {
                r.error = "ERROR_THROW: " + e.Message;
            }
            return(r);
        }
Esempio n. 12
0
        public static oResult Execute(string file___api, object parameters = null, Dictionary <string, object> request = null)
        {
            if (request == null)
            {
                request = new Dictionary <string, object>();
            }
            if (parameters == null)
            {
                parameters = new Dictionary <string, object>();
            }
            oResult r = new oResult()
            {
                ok = false, name = file___api, request = request
            };
            string file = Path.Combine(_CONFIG.PATH_ROOT, "_api\\" + file___api + ".js");

            if (File.Exists(file) == false)
            {
                r.error = "ERROR[clsEngineJS.Execute] Cannot found file: " + file;
                return(r);
            }

            if (!file.Contains("___"))
            {
                r.error = "ERROR[clsEngineJS.Execute] file___api: " + file___api + " must be format cache_name___api_name ";
                return(r);
            }

            string[] a = file___api.Split(new string[] { "___" }, StringSplitOptions.None);
            string   cache_name = a[0], api_name = a[1];

            try
            {
                string js = string.Empty;
                js = @"
                (function() {
                    'use strict';
                    try {
                        var ___log_text = function(text){ log___.write('" + file___api + @"', '_', text); };
                        var ___log_key = function(key, text){ log___.write('" + file___api + @"', key, text); };

                        var ___request = " + JsonConvert.SerializeObject(request) + @";
                        var ___parameters = " + JsonConvert.SerializeObject(parameters) + @";

                        ___log_text('This is from JavaScript...');

                        " + File.ReadAllText(file) + @"

                    } catch(e) {
                        return { ok: false, name: '" + file___api + @"', error: e.message };
                    }
                })();
                ";

                var toReturn = m_engine.Evaluate(js);
                if (toReturn is Microsoft.ClearScript.Undefined)
                {
                    r.ok = true;
                }
                else if (toReturn is oResult)
                {
                    r         = (oResult)toReturn;
                    r.request = request;
                    r.name    = file___api;
                }
                else
                {
                    r.error = "ERROR: Expected TYPE of result but got " + toReturn.GetType().FullName;
                }
            }
            catch (Exception e)
            {
                r.error = "ERROR_THROW: " + e.Message;
            }
            return(r);
        }