コード例 #1
0
ファイル: Sendeplan.cs プロジェクト: FBLipke/Webradio-CMS
    public string GetSendeBanner()
    {
        var banners = new List <string>();

        banners.Add("<img src=\"uploads/events/here_could.png\" />");
        for (var i = 0; i < 14; i++)
        {
            var d_ts    = GetMonday().AddDays(i).Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
            var dbEntry = db.SQLQuery <T>(string.Format("SELECT banner FROM sendeplan WHERE spident='{0}' AND timestamp >= '{1}'", spident, d_ts));

            if (dbEntry.Count == 0)
            {
                continue;
            }

            for (var ib = ulong.MinValue; ib < (ulong)dbEntry.Count; ib++)
            {
                var _ib = (T)Convert.ChangeType(ib, typeof(T));
                if (!string.IsNullOrEmpty(dbEntry[_ib]["banner"]))
                {
                    if (fs.Exists(Filesystem.Combine("uploads/events/", dbEntry[_ib]["banner"])))
                    {
                        banners.Add(string.Format("<img src=\"uploads/events/{0}\" />", dbEntry[_ib]["banner"]));
                    }
                }
            }
        }

        return(banners.Count != 0 ? banners[new Random().Next(0, banners.Count)] : string.Empty);
    }
コード例 #2
0
        /// <summary>
        /// Read a Template from the backend server or from a local file.
        /// </summary>
        /// <param name="fs">the Filesystem instance.</param>
        /// <param name="name">The Name of the Template</param>
        /// <param name="cookies">Cookies which needs to be passed to the Backend.</param>
        /// <returns>The parsed Template with Content.</returns>
        public static string ReadTemplate(Guid userid, string name, HttpListenerContext context, string extradata,
                                          Dictionary <string, string> parameters)
        {
            if (name.Contains("~"))
            {
                name = name.Replace("##UID##", userid.ToString());

                var parts    = name.Split('~');
                var provider = parts[0];
                var id       = parts[1];

                // In Version 3 des Dispatcher kann das Template auch Optionen beinhalten xD
                if (parts[0].Contains("(") && parts[0].Contains(")"))
                {
                    provider = parts[0].Substring(0, parts[0].IndexOf("("));
                    var options = ExtractString(parts[0], "(", ")").Split(';');

                    foreach (var item in options)
                    {
                        var option = item.Split(':');
                        if (option[0] == "Load")
                        {
                            if (!parameters.ContainsKey("Request-Type"))
                            {
                                parameters.Add("Request-Type", option[1]);
                            }
                        }

                        if (option[0] == "Target")
                        {
                            if (!parameters.ContainsKey("Target"))
                            {
                                parameters.Add("Target", option[1]);
                            }
                            else
                            {
                                parameters["Target"] = option[1];
                            }
                        }
                    }
                }

                var response = MakeBackendRequest(userid, provider, id, extradata, context, parameters);
                return(response);
            }
            else
            {
                using (var _fs = new Filesystem(Filesystem.Combine(Environment.CurrentDirectory, "http_root")))
                {
                    var tpl_path = string.Format("templates/{0}.tpl", name.Replace("-", "_"));
                    if (!_fs.Exists(tpl_path))
                    {
                        return(string.Format("<p class=\"exclaim\">Template \"{0}\" not found!</p>", name));
                    }

                    var output = _fs.Read(tpl_path).Result;
                    return(Encoding.UTF8.GetString(output, 0, output.Length));
                }
            }
        }
コード例 #3
0
		public static async Task<Dictionary<string, string>> GetPostData(string path, HttpListenerContext context)
		{
			var formdata = new Dictionary<string, string>();
			var tmp = HttpUtility.UrlDecode(path);

			switch (context.Request.HttpMethod)
			{
				case "POST":
					var encoding = context.Request.ContentEncoding;
					var line = string.Empty;

					using (var reader = new StreamReader(context.Request.InputStream, encoding))
						line = reader.ReadToEndAsync().Result;

					if (string.IsNullOrEmpty(line))
						return formdata;

					if (!string.IsNullOrEmpty(context.Request.ContentType))
					{
						if (context.Request.ContentType.Split(';')[0] != "application/x-www-form-urlencoded")
						{
							var boundary = context.Request.ContentType.Split('=')[1];

							if (string.IsNullOrEmpty(line))
								return null;

							var start = line.IndexOf(boundary) + (boundary.Length + 2);
							var end = line.LastIndexOf(boundary) + boundary.Length;
							line = line.Substring(start, end - start);
							var formparts = new List<string>();

							while (line.Contains(boundary))
								if (line.StartsWith("Content-Disposition:"))
								{
									start = line.IndexOf("Content-Disposition: form-data;") +
									"Content-Disposition: form-data;".Length;

									end = line.IndexOf(boundary);
									formparts.Add(line.Substring(start, end - start).TrimStart());
									line = line.Remove(0, end);
								}

								if (line.StartsWith(boundary))
								{
									if (line.Length > boundary.Length + 2)
										line = line.Remove(0, boundary.Length + 2);
									else
										break;
								}

							if (formparts.Count != 0)
							{
								foreach (var item in formparts)
									if (item.Contains("filename=\""))
									{
										var posttag = item.Substring(0, item.IndexOf(";"));
										var data = item;
										start = data.IndexOf("filename=\"") + "filename=\"".Length;
										data = data.Remove(0, start);
										end = data.IndexOf("\"");

										var filename = data.Substring(0, end);
										if (string.IsNullOrEmpty(filename))
											continue;

										if (filename.Contains("\\") || filename.Contains("/"))
										{
											var parts = filename.Split(filename.Contains("\\") ? '\\' : '/');
											filename = parts[parts.Length - 1];
										}

										start = data.IndexOf("Content-Type: ");
										data = data.Remove(0, start);
										end = data.IndexOf("\r\n");
										data = data.Remove(0, end + 2);

										var filedata = context.Request.ContentEncoding
											.GetBytes(data.Substring(2, data.IndexOf("\r\n--")));

										var uploadpath = Filesystem.Combine(tmp, filename);
										Task.Run(() => File.WriteAllBytes(uploadpath, filedata));

										if (!formdata.ContainsKey(posttag))
											formdata.Add(posttag, uploadpath);
									}
									else
									{
										var x = item.Replace("\r\n--", string.Empty).Replace("name=\"",
											string.Empty).Replace("\"", string.Empty).Replace("\r\n\r\n", "|").Split('|');
										x[0] = x[0].Replace(" file", string.Empty);

										if (!formdata.ContainsKey(x[0]))
											formdata.Add(x[0], x[1]);
									}

								formparts.Clear();
							}

							formparts = null;
						}
						else
						{
							var xtmp = line.Split('&');
							for (var i = 0; i < xtmp.Length; i++)
								if (xtmp[i].Contains("="))
								{
									var p = xtmp[i].Split('=');
									if (!formdata.ContainsKey(p[0]) && p.Length != 0)
										formdata.Add(p[0], HttpUtility.UrlDecode(p[1]).ToString());
								}
						}
					}
					break;
				case "GET":
					if (tmp.Contains("?") && tmp.Contains("="))
					{
						var get_params = tmp.Contains("&") ?
							tmp.Split('?')[1].Split('&') : tmp.Split('?');

						for (var i = 0; i < get_params.Length; i++)
							if (get_params[i].Contains("="))
							{
								var p = get_params[i].Split('=');
								if (!formdata.ContainsKey(p[0]))
									formdata.Add(p[0], p[1]);
							}
					}

					if (tmp.Contains("|"))
					{
						var get_params = tmp.Split('|');

						for (var i = 0; i < get_params.Length; i++)
							if (get_params[i].Contains("="))
							{
								var p = get_params[i].Split('=');
								if (!formdata.ContainsKey(p[0]))
									formdata.Add(p[0], p[1]);
							}
					}
					break;
				default:
					break;
			}

			return formdata;
		}