2020-02-09 02:35:16 +00:00
|
|
|
using System;
|
2015-04-13 06:25:21 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2018-03-10 08:05:56 +00:00
|
|
|
using Jackett.Common.Services.Interfaces;
|
|
|
|
using Jackett.Common.Utils;
|
|
|
|
using Newtonsoft.Json.Linq;
|
2015-04-13 06:25:21 +00:00
|
|
|
|
2018-03-10 08:05:56 +00:00
|
|
|
namespace Jackett.Common.Models.IndexerConfig
|
2015-04-13 06:25:21 +00:00
|
|
|
{
|
2016-10-27 07:30:03 +00:00
|
|
|
public class ConfigurationData
|
2015-04-13 06:25:21 +00:00
|
|
|
{
|
2020-04-20 22:52:42 +00:00
|
|
|
private const string _PasswordReplacement = "|||%%PREVJACKPASSWD%%|||";
|
|
|
|
protected readonly Dictionary<string, Item> _dynamics = new Dictionary<string, Item>(); // list for dynamic items
|
|
|
|
public ConfigurationData() {}
|
2015-08-07 19:09:13 +00:00
|
|
|
|
2020-04-20 22:52:42 +00:00
|
|
|
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)
|
2015-04-13 06:25:21 +00:00
|
|
|
{
|
2020-04-20 22:52:42 +00:00
|
|
|
_dynamics.TryGetValue(id, out var item);
|
|
|
|
return item;
|
2015-04-13 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 22:52:42 +00:00
|
|
|
public Item GetDynamicByName(string name) =>
|
|
|
|
_dynamics.Values.FirstOrDefault(i => string.Equals(i.Name, name, StringComparison.InvariantCultureIgnoreCase));
|
2015-08-03 21:38:45 +00:00
|
|
|
|
2020-04-20 22:52:42 +00:00
|
|
|
private Item[] GetItems(bool forDisplay)
|
2015-07-27 00:03:51 +00:00
|
|
|
{
|
2020-04-20 22:52:42 +00:00
|
|
|
var properties = GetType().GetProperties().Where(p => p.CanRead)
|
|
|
|
.Where(p => p.PropertyType.IsSubclassOf(typeof(Item)))
|
|
|
|
.Select(p => (Item)p.GetValue(this)).ToList();
|
2015-07-27 00:03:51 +00:00
|
|
|
|
2020-04-20 22:52:42 +00:00
|
|
|
// 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();
|
2015-07-27 00:03:51 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 02:35:16 +00:00
|
|
|
public void LoadValuesFromJson(JToken json, IProtectionService ps = null)
|
2015-04-13 06:25:21 +00:00
|
|
|
{
|
2016-12-10 16:53:41 +00:00
|
|
|
if (json == null)
|
|
|
|
return;
|
2015-08-03 21:38:45 +00:00
|
|
|
var arr = (JArray)json;
|
2020-04-20 22:52:42 +00:00
|
|
|
foreach (var item in GetItems(false))
|
2015-04-13 06:25:21 +00:00
|
|
|
{
|
2015-08-03 21:38:45 +00:00
|
|
|
var arrItem = arr.FirstOrDefault(f => f.Value<string>("id") == item.ID);
|
|
|
|
if (arrItem == null)
|
|
|
|
continue;
|
2020-04-20 22:52:42 +00:00
|
|
|
switch (item)
|
2015-04-13 06:25:21 +00:00
|
|
|
{
|
2020-04-20 22:52:42 +00:00
|
|
|
case HiddenItem hiddenItem:
|
|
|
|
hiddenItem.Value = arrItem.Value<string>("value");
|
|
|
|
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:
|
2015-08-07 19:09:13 +00:00
|
|
|
var newValue = arrItem.Value<string>("value");
|
|
|
|
if (string.Equals(item.Name, "password", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
{
|
2020-04-20 22:52:42 +00:00
|
|
|
if (newValue != _PasswordReplacement)
|
2015-08-07 19:09:13 +00:00
|
|
|
{
|
|
|
|
sItem.Value = newValue;
|
|
|
|
if (ps != null)
|
|
|
|
sItem.Value = ps.UnProtect(newValue);
|
|
|
|
}
|
2017-04-15 08:45:10 +00:00
|
|
|
}
|
2017-01-02 20:39:28 +00:00
|
|
|
else
|
2015-08-07 19:09:13 +00:00
|
|
|
sItem.Value = newValue;
|
2020-04-20 22:52:42 +00:00
|
|
|
|
2015-08-07 23:15:42 +00:00
|
|
|
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
|
|
|
{
|
2015-08-03 21:38:45 +00:00
|
|
|
var items = GetItems(forDisplay);
|
2015-04-13 06:25:21 +00:00
|
|
|
var jArray = new JArray();
|
|
|
|
foreach (var item in items)
|
|
|
|
{
|
2020-03-26 22:15:28 +00:00
|
|
|
var jObject = new JObject
|
|
|
|
{
|
|
|
|
["id"] = item.ID,
|
|
|
|
["name"] = item.Name
|
|
|
|
};
|
2020-04-20 22:52:42 +00:00
|
|
|
switch (item)
|
2015-04-13 06:25:21 +00:00
|
|
|
{
|
2020-04-20 22:52:42 +00:00
|
|
|
case RecaptchaItem recaptcha:
|
|
|
|
jObject["sitekey"] = recaptcha.SiteKey;
|
|
|
|
jObject["version"] = recaptcha.Version;
|
2015-08-07 23:15:42 +00:00
|
|
|
break;
|
2020-04-20 22:52:42 +00:00
|
|
|
case StringItem stringItem:
|
|
|
|
var value = stringItem.Value;
|
|
|
|
// if we change this logic we've to change the MigratedFromDPAPI() logic too, #2114 is realted
|
|
|
|
if (string.Equals(stringItem.Name, "password", StringComparison.InvariantCultureIgnoreCase))
|
2015-08-07 19:09:13 +00:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(value))
|
|
|
|
value = string.Empty;
|
|
|
|
else if (forDisplay)
|
2020-04-20 22:52:42 +00:00
|
|
|
value = _PasswordReplacement;
|
2015-08-07 19:09:13 +00:00
|
|
|
else if (ps != null)
|
|
|
|
value = ps.Protect(value);
|
|
|
|
}
|
2020-04-20 22:52:42 +00:00
|
|
|
|
2015-08-07 19:09:13 +00:00
|
|
|
jObject["value"] = value;
|
2015-04-13 06:25:21 +00:00
|
|
|
break;
|
2020-04-20 22:52:42 +00:00
|
|
|
case BoolItem boolItem:
|
|
|
|
jObject["value"] = boolItem.Value;
|
2015-04-13 06:25:21 +00:00
|
|
|
break;
|
2020-04-20 22:52:42 +00:00
|
|
|
case CheckboxItem checkboxItem:
|
|
|
|
jObject["values"] = new JArray(checkboxItem.Values);
|
2020-02-19 20:23:55 +00:00
|
|
|
jObject["options"] = new JObject();
|
2020-04-20 22:52:42 +00:00
|
|
|
foreach (var option in checkboxItem.Options)
|
2020-02-19 20:23:55 +00:00
|
|
|
jObject["options"][option.Key] = option.Value;
|
|
|
|
break;
|
2020-04-20 22:52:42 +00:00
|
|
|
case SelectItem selectItem:
|
|
|
|
jObject["value"] = selectItem.Value;
|
2017-05-28 16:12:41 +00:00
|
|
|
jObject["options"] = new JObject();
|
2020-04-20 22:52:42 +00:00
|
|
|
foreach (var option in selectItem.Options)
|
2017-05-28 16:12:41 +00:00
|
|
|
jObject["options"][option.Key] = option.Value;
|
|
|
|
break;
|
2020-04-20 22:52:42 +00:00
|
|
|
case ImageItem imageItem:
|
|
|
|
var dataUri = DataUrlUtils.BytesToDataUrl(imageItem.Value, "image/jpeg");
|
2015-04-13 06:25:21 +00:00
|
|
|
jObject["value"] = dataUri;
|
|
|
|
break;
|
|
|
|
}
|
2020-04-20 22:52:42 +00:00
|
|
|
|
2015-04-13 06:25:21 +00:00
|
|
|
jArray.Add(jObject);
|
|
|
|
}
|
2020-04-20 22:52:42 +00:00
|
|
|
|
2015-04-13 06:25:21 +00:00
|
|
|
return jArray;
|
|
|
|
}
|
|
|
|
|
2020-04-20 22:52:42 +00:00
|
|
|
public class BoolItem : Item
|
2015-08-03 21:38:45 +00:00
|
|
|
{
|
2020-04-20 22:52:42 +00:00
|
|
|
public bool Value { get; set; }
|
2015-08-03 21:38:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 22:52:42 +00:00
|
|
|
public class CheckboxItem : Item
|
2017-04-15 08:45:10 +00:00
|
|
|
{
|
2020-04-20 22:52:42 +00:00
|
|
|
public CheckboxItem(Dictionary<string, string> options) => Options = options;
|
2016-10-27 07:30:03 +00:00
|
|
|
|
2020-04-20 22:52:42 +00:00
|
|
|
public Dictionary<string, string> Options { get; }
|
|
|
|
public string[] Values { get; set; }
|
|
|
|
}
|
2018-06-26 15:47:19 +00:00
|
|
|
|
2020-04-20 22:52:42 +00:00
|
|
|
public class DisplayItem : StringItem
|
2015-04-13 06:25:21 +00:00
|
|
|
{
|
2020-04-20 22:52:42 +00:00
|
|
|
public DisplayItem(string value) => Value = value;
|
2015-04-13 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2015-08-02 17:28:59 +00:00
|
|
|
public class HiddenItem : StringItem
|
|
|
|
{
|
2020-04-20 22:52:42 +00:00
|
|
|
public HiddenItem(string value = "") => Value = value;
|
2015-08-02 17:28:59 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 22:52:42 +00:00
|
|
|
public class ImageItem : Item
|
2015-04-18 06:05:30 +00:00
|
|
|
{
|
2020-04-20 22:52:42 +00:00
|
|
|
public byte[] Value { get; set; }
|
2015-04-18 06:05:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 22:52:42 +00:00
|
|
|
public class Item
|
2015-04-13 06:25:21 +00:00
|
|
|
{
|
2020-04-20 22:52:42 +00:00
|
|
|
public string ID => Name.Replace(" ", "").ToLower();
|
|
|
|
public string Name { get; set; }
|
2015-04-13 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 23:15:42 +00:00
|
|
|
public class RecaptchaItem : StringItem
|
|
|
|
{
|
2020-04-20 22:52:42 +00:00
|
|
|
public RecaptchaItem() => Version = "2";
|
2016-09-13 12:15:51 +00:00
|
|
|
public string Challenge { get; set; }
|
2020-04-20 22:52:42 +00:00
|
|
|
public string Version { get; set; }
|
2015-04-13 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 22:52:42 +00:00
|
|
|
public class SelectItem : Item
|
2020-02-19 20:23:55 +00:00
|
|
|
{
|
2020-04-20 22:52:42 +00:00
|
|
|
public SelectItem(Dictionary<string, string> options) => Options = options;
|
2020-02-19 20:23:55 +00:00
|
|
|
|
|
|
|
public Dictionary<string, string> Options { get; }
|
2020-04-20 22:52:42 +00:00
|
|
|
public string Value { get; set; }
|
2020-02-19 20:23:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 22:52:42 +00:00
|
|
|
public class StringItem : Item
|
2017-05-28 16:12:41 +00:00
|
|
|
{
|
2020-04-20 22:52:42 +00:00
|
|
|
public string Cookie { get; set; }
|
|
|
|
public string SiteKey { get; set; }
|
2017-05-28 16:12:41 +00:00
|
|
|
public string Value { get; set; }
|
|
|
|
}
|
2015-04-13 06:25:21 +00:00
|
|
|
}
|
|
|
|
}
|