Jackett/src/Jackett.Common/Models/IndexerConfig/ConfigurationData.cs

279 lines
9.7 KiB
C#
Raw Normal View History

using System;
2015-04-13 06:25:21 +00:00
using System.Collections.Generic;
using System.Linq;
using Jackett.Common.Services.Interfaces;
using Jackett.Common.Utils;
using Newtonsoft.Json.Linq;
2015-04-13 06:25:21 +00:00
namespace Jackett.Common.Models.IndexerConfig
2015-04-13 06:25:21 +00:00
{
public class ConfigurationData
2015-04-13 06:25:21 +00:00
{
2015-08-07 19:09:13 +00:00
const string PASSWORD_REPLACEMENT = "|||%%PREVJACKPASSWD%%|||";
protected Dictionary<string, Item> dynamics = new Dictionary<string, Item>(); // list for dynamic items
2015-08-07 19:09:13 +00:00
2015-04-13 06:25:21 +00:00
public enum ItemType
{
InputString,
InputBool,
InputSelect,
2015-04-13 06:25:21 +00:00
DisplayImage,
DisplayInfo,
HiddenData,
Recaptcha
2015-04-13 06:25:21 +00:00
}
public HiddenItem CookieHeader { get; private set; } = new HiddenItem { Name = "CookieHeader" };
2016-12-05 10:43:07 +00:00
public HiddenItem LastError { get; private set; } = new HiddenItem { Name = "LastError" };
public StringItem SiteLink { get; private set; } = new StringItem { Name = "Site Link" };
public ConfigurationData()
{
}
2015-08-07 19:09:13 +00:00
public ConfigurationData(JToken json, IProtectionService ps)
{
2015-08-07 19:09:13 +00:00
LoadValuesFromJson(json, ps);
}
2015-08-07 19:09:13 +00:00
public void LoadValuesFromJson(JToken json, IProtectionService ps= null)
2015-04-13 06:25:21 +00:00
{
if (json == null)
return;
var arr = (JArray)json;
// transistion from alternatelink to sitelink
var alternatelinkItem = arr.FirstOrDefault(f => f.Value<string>("id") == "alternatelink");
if (alternatelinkItem != null && !string.IsNullOrEmpty(alternatelinkItem.Value<string>("value")))
{
//SiteLink.Value = alternatelinkItem.Value<string>("value");
}
foreach (var item in GetItems(forDisplay: false))
2015-04-13 06:25:21 +00:00
{
var arrItem = arr.FirstOrDefault(f => f.Value<string>("id") == item.ID);
if (arrItem == null)
continue;
2015-04-13 06:25:21 +00:00
switch (item.ItemType)
{
case ItemType.InputString:
2015-08-07 19:09:13 +00:00
var sItem = (StringItem)item;
var newValue = arrItem.Value<string>("value");
if (string.Equals(item.Name, "password", StringComparison.InvariantCultureIgnoreCase))
{
if (newValue != PASSWORD_REPLACEMENT)
{
sItem.Value = newValue;
if (ps != null)
sItem.Value = ps.UnProtect(newValue);
}
}
else
2015-08-07 19:09:13 +00:00
{
sItem.Value = newValue;
}
2015-04-13 06:25:21 +00:00
break;
case ItemType.HiddenData:
((HiddenItem)item).Value = arrItem.Value<string>("value");
break;
case ItemType.InputBool:
((BoolItem)item).Value = arrItem.Value<bool>("value");
break;
case ItemType.InputSelect:
((SelectItem)item).Value = arrItem.Value<string>("value");
break;
case ItemType.Recaptcha:
((RecaptchaItem)item).Value = arrItem.Value<string>("value");
((RecaptchaItem)item).Cookie = arrItem.Value<string>("cookie");
((RecaptchaItem)item).Version = arrItem.Value<string>("version");
((RecaptchaItem)item).Challenge = arrItem.Value<string>("challenge");
break;
2015-04-13 06:25:21 +00:00
}
}
}
2015-08-07 19:09:13 +00:00
public JToken ToJson(IProtectionService ps, bool forDisplay = true)
2015-04-13 06:25:21 +00:00
{
var items = GetItems(forDisplay);
2015-04-13 06:25:21 +00:00
var jArray = new JArray();
foreach (var item in items)
{
var jObject = new JObject();
jObject["id"] = item.ID;
jObject["type"] = item.ItemType.ToString().ToLower();
jObject["name"] = item.Name;
switch (item.ItemType)
{
case ItemType.Recaptcha:
jObject["sitekey"] = ((RecaptchaItem)item).SiteKey;
jObject["version"] = ((RecaptchaItem)item).Version;
break;
2015-04-13 06:25:21 +00:00
case ItemType.InputString:
case ItemType.HiddenData:
case ItemType.DisplayInfo:
2015-08-07 19:09:13 +00:00
var value = ((StringItem)item).Value;
2018-06-26 15:47:19 +00:00
if (string.Equals(item.Name, "password", StringComparison.InvariantCultureIgnoreCase)) // if we chagne this logic we've to change the MigratedFromDPAPI() logic too, #2114 is realted
2015-08-07 19:09:13 +00:00
{
if (string.IsNullOrEmpty(value))
value = string.Empty;
else if (forDisplay)
value = PASSWORD_REPLACEMENT;
else if (ps != null)
value = ps.Protect(value);
}
jObject["value"] = value;
2015-04-13 06:25:21 +00:00
break;
case ItemType.InputBool:
jObject["value"] = ((BoolItem)item).Value;
break;
case ItemType.InputSelect:
jObject["value"] = ((SelectItem)item).Value;
jObject["options"] = new JObject();
foreach (var option in ((SelectItem)item).Options)
{
jObject["options"][option.Key] = option.Value;
}
break;
2015-04-13 06:25:21 +00:00
case ItemType.DisplayImage:
2015-07-19 17:18:54 +00:00
string dataUri = DataUrlUtils.BytesToDataUrl(((ImageItem)item).Value, "image/jpeg");
2015-04-13 06:25:21 +00:00
jObject["value"] = dataUri;
break;
}
jArray.Add(jObject);
}
return jArray;
}
Item[] GetItems(bool forDisplay)
{
List<Item> properties = GetType()
.GetProperties()
.Where(p => p.CanRead)
.Where(p => p.PropertyType.IsSubclassOf(typeof(Item)))
.Select(p => (Item)p.GetValue(this)).ToList();
// remove/insert Site Link manualy to make sure it shows up first
properties.Remove(SiteLink);
properties.Insert(0, SiteLink);
properties.AddRange(dynamics.Values);
if (!forDisplay)
{
properties = properties
.Where(p => p.ItemType == ItemType.HiddenData || p.ItemType == ItemType.InputBool || p.ItemType == ItemType.InputString || p.ItemType == ItemType.InputSelect || p.ItemType == ItemType.Recaptcha || p.ItemType == ItemType.DisplayInfo)
.ToList();
}
return properties.ToArray();
}
public void AddDynamic(string ID, Item item)
{
dynamics[ID] = item;
}
public Item GetDynamic(string ID)
{
try
{
return dynamics[ID];
}
catch(KeyNotFoundException)
{
return null;
}
}
2018-06-26 15:47:19 +00:00
public Item GetDynamicByName(string Name)
{
return dynamics.Values.Where(i => string.Equals(i.Name, Name, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
}
2015-04-13 06:25:21 +00:00
public class Item
{
public ItemType ItemType { get; set; }
public string Name { get; set; }
public string ID { get { return Name.Replace(" ", "").ToLower(); } }
}
public class HiddenItem : StringItem
{
public HiddenItem(string value = "")
{
Value = value;
ItemType = ItemType.HiddenData;
}
}
2015-04-18 06:05:30 +00:00
public class DisplayItem : StringItem
{
public DisplayItem(string value)
{
Value = value;
ItemType = ItemType.DisplayInfo;
}
}
2015-04-13 06:25:21 +00:00
public class StringItem : Item
{
public string SiteKey { get; set; }
2015-04-13 06:25:21 +00:00
public string Value { get; set; }
public string Cookie { get; set; }
2015-04-18 06:05:30 +00:00
public StringItem()
{
ItemType = ConfigurationData.ItemType.InputString;
}
2015-04-13 06:25:21 +00:00
}
public class RecaptchaItem : StringItem
{
public string Version { get; set; }
public string Challenge { get; set; }
public RecaptchaItem()
{
this.Version = "2";
ItemType = ConfigurationData.ItemType.Recaptcha;
}
}
2015-04-13 06:25:21 +00:00
public class BoolItem : Item
{
public bool Value { get; set; }
2015-04-18 06:05:30 +00:00
public BoolItem()
{
ItemType = ConfigurationData.ItemType.InputBool;
}
2015-04-13 06:25:21 +00:00
}
public class ImageItem : Item
{
public byte[] Value { get; set; }
2015-04-18 06:05:30 +00:00
public ImageItem()
{
ItemType = ConfigurationData.ItemType.DisplayImage;
}
2015-04-13 06:25:21 +00:00
}
public class SelectItem : Item
{
public string Value { get; set; }
public Dictionary<string, string> Options { get; }
public SelectItem(Dictionary<string, string> options)
{
ItemType = ItemType.InputSelect;
Options = options;
}
}
//public abstract Item[] GetItems();
2015-04-13 06:25:21 +00:00
}
}