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-21 06:45:21 +00:00
private 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
2020-04-21 06:45:21 +00:00
public enum ItemType
2015-04-13 06:25:21 +00:00
{
2020-04-21 06:45:21 +00:00
InputString ,
InputBool ,
InputCheckbox ,
InputSelect ,
DisplayImage ,
DisplayInfo ,
2020-11-05 02:19:09 +00:00
HiddenData
2015-04-13 06:25:21 +00:00
}
2020-04-21 06:45:21 +00:00
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" } ;
2015-08-03 21:38:45 +00:00
2020-04-21 06:45:21 +00:00
public ConfigurationData ( )
2015-07-27 00:03:51 +00:00
{
}
2020-04-21 06:45:21 +00:00
public ConfigurationData ( JToken json , IProtectionService ps ) = > LoadValuesFromJson ( json , ps ) ;
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 ;
2020-04-21 06:45:21 +00:00
2015-08-03 21:38:45 +00:00
var arr = ( JArray ) json ;
2020-04-21 06:45:21 +00:00
// 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
{
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-21 06:45:21 +00:00
switch ( item . ItemType )
2015-04-13 06:25:21 +00:00
{
2020-04-21 06:45:21 +00:00
case ItemType . InputString :
var sItem = ( StringItem ) item ;
2015-08-07 19:09:13 +00:00
var newValue = arrItem . Value < string > ( "value" ) ;
2020-04-21 06:45:21 +00:00
2015-08-07 19:09:13 +00:00
if ( string . Equals ( item . Name , "password" , StringComparison . InvariantCultureIgnoreCase ) )
{
2020-04-21 06:45:21 +00:00
if ( newValue ! = PASSWORD_REPLACEMENT )
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
2020-04-21 06:45:21 +00:00
{
2015-08-07 19:09:13 +00:00
sItem . Value = newValue ;
2020-04-21 06:45: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 . 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 ;
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 ,
2020-04-21 06:45:21 +00:00
["type"] = item . ItemType . ToString ( ) . ToLower ( ) ,
2020-03-26 22:15:28 +00:00
["name"] = item . Name
} ;
2020-04-21 06:45:21 +00:00
switch ( item . ItemType )
2015-04-13 06:25:21 +00:00
{
2020-04-21 06:45:21 +00:00
case ItemType . InputString :
case ItemType . HiddenData :
case ItemType . DisplayInfo :
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
2015-08-07 19:09:13 +00:00
{
if ( string . IsNullOrEmpty ( value ) )
value = string . Empty ;
else if ( forDisplay )
2020-04-21 06:45:21 +00:00
value = PASSWORD_REPLACEMENT ;
2015-08-07 19:09:13 +00:00
else if ( ps ! = null )
value = ps . Protect ( value ) ;
}
jObject [ "value" ] = value ;
2015-04-13 06:25:21 +00:00
break ;
2020-04-21 06:45:21 +00:00
case ItemType . InputBool :
jObject [ "value" ] = ( ( BoolItem ) item ) . Value ;
2015-04-13 06:25:21 +00:00
break ;
2020-04-21 06:45:21 +00:00
case ItemType . InputCheckbox :
jObject [ "values" ] = new JArray ( ( ( CheckboxItem ) item ) . Values ) ;
2020-02-19 20:23:55 +00:00
jObject [ "options" ] = new JObject ( ) ;
2020-04-21 06:45:21 +00:00
foreach ( var option in ( ( CheckboxItem ) item ) . Options )
{
2020-02-19 20:23:55 +00:00
jObject [ "options" ] [ option . Key ] = option . Value ;
2020-04-21 06:45:21 +00:00
}
2020-02-19 20:23:55 +00:00
break ;
2020-04-21 06:45:21 +00:00
case ItemType . InputSelect :
jObject [ "value" ] = ( ( SelectItem ) item ) . Value ;
2017-05-28 16:12:41 +00:00
jObject [ "options" ] = new JObject ( ) ;
2020-04-21 06:45:21 +00:00
foreach ( var option in ( ( SelectItem ) item ) . Options )
{
2017-05-28 16:12:41 +00:00
jObject [ "options" ] [ option . Key ] = option . Value ;
2020-04-21 06:45:21 +00:00
}
2017-05-28 16:12:41 +00:00
break ;
2020-04-21 06:45:21 +00:00
case ItemType . DisplayImage :
var 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 ;
}
2020-04-21 06:45:21 +00:00
private Item [ ] GetItems ( bool forDisplay )
2015-08-03 21:38:45 +00:00
{
2020-04-21 06:45:21 +00:00
var properties = GetType ( )
. GetProperties ( )
. Where ( p = > p . CanRead )
. Where ( p = > p . PropertyType . IsSubclassOf ( typeof ( Item ) ) )
2020-10-08 19:16:22 +00:00
. Where ( p = > p . GetValue ( this ) ! = null )
2020-04-21 06:45:21 +00:00
. 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
2020-11-05 02:19:09 +00:00
. 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 . DisplayInfo )
2020-04-21 06:45:21 +00:00
. ToList ( ) ;
}
return properties . ToArray ( ) ;
2015-08-03 21:38:45 +00:00
}
2020-04-21 06:45:21 +00:00
public void AddDynamic ( string ID , Item item ) = > dynamics [ ID ] = item ;
2016-10-27 07:30:03 +00:00
2020-04-21 06:45:21 +00:00
// TODO Convert to TryGetValue to avoid throwing exception
public Item GetDynamic ( string ID )
{
try
{
return dynamics [ ID ] ;
}
catch ( KeyNotFoundException )
{
return null ;
}
2020-04-20 22:52:42 +00:00
}
2018-06-26 15:47:19 +00:00
2020-04-21 06:45:21 +00:00
public Item GetDynamicByName ( string Name )
= > dynamics . Values . FirstOrDefault ( i = > string . Equals ( i . Name , Name , StringComparison . InvariantCultureIgnoreCase ) ) ;
public class Item
2015-04-13 06:25:21 +00:00
{
2020-04-21 06:45:21 +00:00
public ItemType ItemType { get ; set ; }
public string Name { get ; set ; }
public string ID = > Name . Replace ( " " , "" ) . ToLower ( ) ;
2015-04-13 06:25:21 +00:00
}
2015-08-02 17:28:59 +00:00
public class HiddenItem : StringItem
{
2020-04-21 06:45:21 +00:00
public HiddenItem ( string value = "" )
{
Value = value ;
ItemType = ItemType . HiddenData ;
}
2015-08-02 17:28:59 +00:00
}
2020-04-21 06:45:21 +00:00
public class DisplayItem : StringItem
2015-04-18 06:05:30 +00:00
{
2020-04-21 06:45:21 +00:00
public DisplayItem ( string value )
{
Value = value ;
ItemType = ItemType . DisplayInfo ;
}
2015-04-18 06:05:30 +00:00
}
2020-04-21 06:45:21 +00:00
public class StringItem : Item
2015-04-13 06:25:21 +00:00
{
2020-04-21 06:45:21 +00:00
public string SiteKey { get ; set ; }
public string Value { get ; set ; }
public string Cookie { get ; set ; }
public StringItem ( ) = > ItemType = ItemType . InputString ;
2015-04-13 06:25:21 +00:00
}
2020-04-21 06:45:21 +00:00
public class BoolItem : Item
2020-02-19 20:23:55 +00:00
{
2020-04-21 06:45:21 +00:00
public bool Value { get ; set ; }
public BoolItem ( ) = > ItemType = ItemType . InputBool ;
}
public class ImageItem : Item
{
public byte [ ] Value { get ; set ; }
public ImageItem ( ) = > ItemType = ItemType . DisplayImage ;
}
public class CheckboxItem : Item
{
public string [ ] Values { get ; set ; }
2020-02-19 20:23:55 +00:00
public Dictionary < string , string > Options { get ; }
2020-04-21 06:45:21 +00:00
public CheckboxItem ( Dictionary < string , string > options )
{
ItemType = ItemType . InputCheckbox ;
Options = options ;
}
2020-02-19 20:23:55 +00:00
}
2020-04-21 06:45:21 +00:00
public class SelectItem : Item
2017-05-28 16:12:41 +00:00
{
public string Value { get ; set ; }
2020-04-21 06:45:21 +00:00
public Dictionary < string , string > Options { get ; }
public SelectItem ( Dictionary < string , string > options )
{
ItemType = ItemType . InputSelect ;
Options = options ;
}
2017-05-28 16:12:41 +00:00
}
2020-04-21 06:45:21 +00:00
//public abstract Item[] GetItems();
2015-04-13 06:25:21 +00:00
}
}