mirror of
https://github.com/Jackett/Jackett.git
synced 2025-09-17 17:34:09 +02:00
This reverts commit e83ed96194
.
This commit is contained in:
@@ -9,79 +9,61 @@ namespace Jackett.Common.Models.IndexerConfig
|
|||||||
{
|
{
|
||||||
public class ConfigurationData
|
public class ConfigurationData
|
||||||
{
|
{
|
||||||
private const string _PasswordReplacement = "|||%%PREVJACKPASSWD%%|||";
|
private const string PASSWORD_REPLACEMENT = "|||%%PREVJACKPASSWD%%|||";
|
||||||
protected readonly Dictionary<string, Item> _dynamics = new Dictionary<string, Item>(); // list for dynamic items
|
protected Dictionary<string, Item> dynamics = new Dictionary<string, Item>(); // list for dynamic items
|
||||||
public ConfigurationData() {}
|
|
||||||
|
public enum ItemType
|
||||||
|
{
|
||||||
|
InputString,
|
||||||
|
InputBool,
|
||||||
|
InputCheckbox,
|
||||||
|
InputSelect,
|
||||||
|
DisplayImage,
|
||||||
|
DisplayInfo,
|
||||||
|
HiddenData,
|
||||||
|
Recaptcha
|
||||||
|
}
|
||||||
|
|
||||||
|
public HiddenItem CookieHeader { get; private set; } = new HiddenItem { Name = "CookieHeader" };
|
||||||
|
public HiddenItem LastError { get; private set; } = new HiddenItem { Name = "LastError" };
|
||||||
|
public StringItem SiteLink { get; private set; } = new StringItem { Name = "Site Link" };
|
||||||
|
|
||||||
|
public ConfigurationData()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public ConfigurationData(JToken json, IProtectionService ps) => LoadValuesFromJson(json, ps);
|
public ConfigurationData(JToken json, IProtectionService ps) => LoadValuesFromJson(json, ps);
|
||||||
|
|
||||||
public HiddenItem CookieHeader { get; } = new HiddenItem {Name = "CookieHeader"};
|
|
||||||
public HiddenItem LastError { get; } = new HiddenItem {Name = "LastError"};
|
|
||||||
public StringItem SiteLink { get; } = new StringItem {Name = "Site Link"};
|
|
||||||
|
|
||||||
public void AddDynamic(string id, Item item) => _dynamics[id] = item;
|
|
||||||
|
|
||||||
public Item GetDynamic(string id)
|
|
||||||
{
|
|
||||||
_dynamics.TryGetValue(id, out var item);
|
|
||||||
return item;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Item GetDynamicByName(string name) =>
|
|
||||||
_dynamics.Values.FirstOrDefault(i => string.Equals(i.Name, name, StringComparison.InvariantCultureIgnoreCase));
|
|
||||||
|
|
||||||
private Item[] GetItems(bool forDisplay)
|
|
||||||
{
|
|
||||||
var 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.RemoveAll(property => property is ImageItem);
|
|
||||||
return properties.ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void LoadValuesFromJson(JToken json, IProtectionService ps = null)
|
public void LoadValuesFromJson(JToken json, IProtectionService ps = null)
|
||||||
{
|
{
|
||||||
if (json == null)
|
if (json == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var arr = (JArray)json;
|
var arr = (JArray)json;
|
||||||
foreach (var item in GetItems(false))
|
|
||||||
|
// 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))
|
||||||
{
|
{
|
||||||
var arrItem = arr.FirstOrDefault(f => f.Value<string>("id") == item.ID);
|
var arrItem = arr.FirstOrDefault(f => f.Value<string>("id") == item.ID);
|
||||||
if (arrItem == null)
|
if (arrItem == null)
|
||||||
continue;
|
continue;
|
||||||
switch (item)
|
|
||||||
|
switch (item.ItemType)
|
||||||
{
|
{
|
||||||
case HiddenItem hiddenItem:
|
case ItemType.InputString:
|
||||||
hiddenItem.Value = arrItem.Value<string>("value");
|
var sItem = (StringItem)item;
|
||||||
break;
|
|
||||||
case BoolItem boolItem:
|
|
||||||
boolItem.Value = arrItem.Value<bool>("value");
|
|
||||||
break;
|
|
||||||
case CheckboxItem checkboxItem:
|
|
||||||
var values = arrItem.Value<JArray>("values");
|
|
||||||
if (values != null)
|
|
||||||
checkboxItem.Values = values.Values<string>().ToArray();
|
|
||||||
break;
|
|
||||||
case SelectItem selectItem:
|
|
||||||
selectItem.Value = arrItem.Value<string>("value");
|
|
||||||
break;
|
|
||||||
case RecaptchaItem recaptcha:
|
|
||||||
recaptcha.Value = arrItem.Value<string>("value");
|
|
||||||
recaptcha.Cookie = arrItem.Value<string>("cookie");
|
|
||||||
recaptcha.Version = arrItem.Value<string>("version");
|
|
||||||
recaptcha.Challenge = arrItem.Value<string>("challenge");
|
|
||||||
break;
|
|
||||||
case StringItem sItem:
|
|
||||||
var newValue = arrItem.Value<string>("value");
|
var newValue = arrItem.Value<string>("value");
|
||||||
|
|
||||||
if (string.Equals(item.Name, "password", StringComparison.InvariantCultureIgnoreCase))
|
if (string.Equals(item.Name, "password", StringComparison.InvariantCultureIgnoreCase))
|
||||||
{
|
{
|
||||||
if (newValue != _PasswordReplacement)
|
if (newValue != PASSWORD_REPLACEMENT)
|
||||||
{
|
{
|
||||||
sItem.Value = newValue;
|
sItem.Value = newValue;
|
||||||
if (ps != null)
|
if (ps != null)
|
||||||
@@ -89,8 +71,29 @@ namespace Jackett.Common.Models.IndexerConfig
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
sItem.Value = newValue;
|
sItem.Value = newValue;
|
||||||
|
}
|
||||||
|
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.InputCheckbox:
|
||||||
|
var values = arrItem.Value<JArray>("values");
|
||||||
|
if (values != null)
|
||||||
|
((CheckboxItem)item).Values = values.Values<string>().ToArray();
|
||||||
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -105,110 +108,185 @@ namespace Jackett.Common.Models.IndexerConfig
|
|||||||
var jObject = new JObject
|
var jObject = new JObject
|
||||||
{
|
{
|
||||||
["id"] = item.ID,
|
["id"] = item.ID,
|
||||||
|
["type"] = item.ItemType.ToString().ToLower(),
|
||||||
["name"] = item.Name
|
["name"] = item.Name
|
||||||
};
|
};
|
||||||
switch (item)
|
switch (item.ItemType)
|
||||||
{
|
{
|
||||||
case RecaptchaItem recaptcha:
|
case ItemType.Recaptcha:
|
||||||
jObject["sitekey"] = recaptcha.SiteKey;
|
jObject["sitekey"] = ((RecaptchaItem)item).SiteKey;
|
||||||
jObject["version"] = recaptcha.Version;
|
jObject["version"] = ((RecaptchaItem)item).Version;
|
||||||
break;
|
break;
|
||||||
case StringItem stringItem:
|
case ItemType.InputString:
|
||||||
var value = stringItem.Value;
|
case ItemType.HiddenData:
|
||||||
// if we change this logic we've to change the MigratedFromDPAPI() logic too, #2114 is realted
|
case ItemType.DisplayInfo:
|
||||||
if (string.Equals(stringItem.Name, "password", StringComparison.InvariantCultureIgnoreCase))
|
var value = ((StringItem)item).Value;
|
||||||
|
if (string.Equals(item.Name, "password", StringComparison.InvariantCultureIgnoreCase)) // if we chagne this logic we've to change the MigratedFromDPAPI() logic too, #2114 is realted
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(value))
|
if (string.IsNullOrEmpty(value))
|
||||||
value = string.Empty;
|
value = string.Empty;
|
||||||
else if (forDisplay)
|
else if (forDisplay)
|
||||||
value = _PasswordReplacement;
|
value = PASSWORD_REPLACEMENT;
|
||||||
else if (ps != null)
|
else if (ps != null)
|
||||||
value = ps.Protect(value);
|
value = ps.Protect(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
jObject["value"] = value;
|
jObject["value"] = value;
|
||||||
break;
|
break;
|
||||||
case BoolItem boolItem:
|
case ItemType.InputBool:
|
||||||
jObject["value"] = boolItem.Value;
|
jObject["value"] = ((BoolItem)item).Value;
|
||||||
break;
|
break;
|
||||||
case CheckboxItem checkboxItem:
|
case ItemType.InputCheckbox:
|
||||||
jObject["values"] = new JArray(checkboxItem.Values);
|
jObject["values"] = new JArray(((CheckboxItem)item).Values);
|
||||||
jObject["options"] = new JObject();
|
jObject["options"] = new JObject();
|
||||||
foreach (var option in checkboxItem.Options)
|
|
||||||
|
foreach (var option in ((CheckboxItem)item).Options)
|
||||||
|
{
|
||||||
jObject["options"][option.Key] = option.Value;
|
jObject["options"][option.Key] = option.Value;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case SelectItem selectItem:
|
case ItemType.InputSelect:
|
||||||
jObject["value"] = selectItem.Value;
|
jObject["value"] = ((SelectItem)item).Value;
|
||||||
jObject["options"] = new JObject();
|
jObject["options"] = new JObject();
|
||||||
foreach (var option in selectItem.Options)
|
|
||||||
|
foreach (var option in ((SelectItem)item).Options)
|
||||||
|
{
|
||||||
jObject["options"][option.Key] = option.Value;
|
jObject["options"][option.Key] = option.Value;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case ImageItem imageItem:
|
case ItemType.DisplayImage:
|
||||||
var dataUri = DataUrlUtils.BytesToDataUrl(imageItem.Value, "image/jpeg");
|
var dataUri = DataUrlUtils.BytesToDataUrl(((ImageItem)item).Value, "image/jpeg");
|
||||||
jObject["value"] = dataUri;
|
jObject["value"] = dataUri;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
jArray.Add(jObject);
|
jArray.Add(jObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
return jArray;
|
return jArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Item[] GetItems(bool forDisplay)
|
||||||
|
{
|
||||||
|
var 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.InputCheckbox || 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;
|
||||||
|
|
||||||
|
// TODO Convert to TryGetValue to avoid throwing exception
|
||||||
|
public Item GetDynamic(string ID)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return dynamics[ID];
|
||||||
|
}
|
||||||
|
catch (KeyNotFoundException)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Item GetDynamicByName(string Name)
|
||||||
|
=> dynamics.Values.FirstOrDefault(i => string.Equals(i.Name, Name, StringComparison.InvariantCultureIgnoreCase));
|
||||||
|
|
||||||
|
public class Item
|
||||||
|
{
|
||||||
|
public ItemType ItemType { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string ID => Name.Replace(" ", "").ToLower();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class HiddenItem : StringItem
|
||||||
|
{
|
||||||
|
public HiddenItem(string value = "")
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
ItemType = ItemType.HiddenData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DisplayItem : StringItem
|
||||||
|
{
|
||||||
|
public DisplayItem(string value)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
ItemType = ItemType.DisplayInfo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StringItem : Item
|
||||||
|
{
|
||||||
|
public string SiteKey { get; set; }
|
||||||
|
public string Value { get; set; }
|
||||||
|
public string Cookie { get; set; }
|
||||||
|
public StringItem() => ItemType = ItemType.InputString;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RecaptchaItem : StringItem
|
||||||
|
{
|
||||||
|
public string Version { get; set; }
|
||||||
|
public string Challenge { get; set; }
|
||||||
|
public RecaptchaItem()
|
||||||
|
{
|
||||||
|
Version = "2";
|
||||||
|
ItemType = ItemType.Recaptcha;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class BoolItem : Item
|
public class BoolItem : Item
|
||||||
{
|
{
|
||||||
public bool Value { get; set; }
|
public bool Value { get; set; }
|
||||||
}
|
public BoolItem() => ItemType = ItemType.InputBool;
|
||||||
|
|
||||||
public class CheckboxItem : Item
|
|
||||||
{
|
|
||||||
public CheckboxItem(Dictionary<string, string> options) => Options = options;
|
|
||||||
|
|
||||||
public Dictionary<string, string> Options { get; }
|
|
||||||
public string[] Values { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class DisplayItem : StringItem
|
|
||||||
{
|
|
||||||
public DisplayItem(string value) => Value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public class HiddenItem : StringItem
|
|
||||||
{
|
|
||||||
public HiddenItem(string value = "") => Value = value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ImageItem : Item
|
public class ImageItem : Item
|
||||||
{
|
{
|
||||||
public byte[] Value { get; set; }
|
public byte[] Value { get; set; }
|
||||||
|
public ImageItem() => ItemType = ItemType.DisplayImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Item
|
public class CheckboxItem : Item
|
||||||
{
|
{
|
||||||
public string ID => Name.Replace(" ", "").ToLower();
|
public string[] Values { get; set; }
|
||||||
public string Name { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class RecaptchaItem : StringItem
|
public Dictionary<string, string> Options { get; }
|
||||||
|
|
||||||
|
public CheckboxItem(Dictionary<string, string> options)
|
||||||
{
|
{
|
||||||
public RecaptchaItem() => Version = "2";
|
ItemType = ItemType.InputCheckbox;
|
||||||
public string Challenge { get; set; }
|
Options = options;
|
||||||
public string Version { get; set; }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class SelectItem : Item
|
public class SelectItem : Item
|
||||||
{
|
{
|
||||||
public SelectItem(Dictionary<string, string> options) => Options = options;
|
public string Value { get; set; }
|
||||||
|
|
||||||
public Dictionary<string, string> Options { get; }
|
public Dictionary<string, string> Options { get; }
|
||||||
public string Value { get; set; }
|
|
||||||
|
public SelectItem(Dictionary<string, string> options)
|
||||||
|
{
|
||||||
|
ItemType = ItemType.InputSelect;
|
||||||
|
Options = options;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class StringItem : Item
|
//public abstract Item[] GetItems();
|
||||||
{
|
|
||||||
public string Cookie { get; set; }
|
|
||||||
public string SiteKey { get; set; }
|
|
||||||
public string Value { get; set; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user