2018-03-10 08:05:56 +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
{
2015-08-07 19:09:13 +00:00
const string PASSWORD_REPLACEMENT = "|||%%PREVJACKPASSWD%%|||" ;
2016-10-27 07:30:03 +00:00
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 ,
2017-05-28 16:12:41 +00:00
InputSelect ,
2015-04-13 06:25:21 +00:00
DisplayImage ,
DisplayInfo ,
2015-08-07 23:15:42 +00:00
HiddenData ,
Recaptcha
2015-04-13 06:25:21 +00:00
}
2015-08-03 21:38:45 +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" } ;
2017-01-02 20:39:28 +00:00
public StringItem SiteLink { get ; private set ; } = new StringItem { Name = "Site Link" } ;
2015-08-03 21:38:45 +00:00
2015-07-27 00:03:51 +00:00
public ConfigurationData ( )
{
}
2015-08-07 19:09:13 +00:00
public ConfigurationData ( JToken json , IProtectionService ps )
2015-07-27 00:03:51 +00:00
{
2015-08-07 19:09:13 +00:00
LoadValuesFromJson ( json , ps ) ;
2015-07-27 00:03:51 +00:00
}
2015-08-07 19:09:13 +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 ;
2017-01-02 20:39:28 +00:00
// transistion from alternatelink to sitelink
var alternatelinkItem = arr . FirstOrDefault ( f = > f . Value < string > ( "id" ) = = "alternatelink" ) ;
2017-04-15 08:45:10 +00:00
if ( alternatelinkItem ! = null & & ! string . IsNullOrEmpty ( alternatelinkItem . Value < string > ( "value" ) ) )
{
//SiteLink.Value = alternatelinkItem.Value<string>("value");
2017-01-02 20:39:28 +00:00
}
2015-08-03 21:38:45 +00:00
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 ;
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 ) ;
}
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 ;
}
2015-04-13 06:25:21 +00:00
break ;
2015-08-02 17:28:59 +00:00
case ItemType . HiddenData :
2015-08-03 21:38:45 +00:00
( ( HiddenItem ) item ) . Value = arrItem . Value < string > ( "value" ) ;
break ;
case ItemType . InputBool :
( ( BoolItem ) item ) . Value = arrItem . Value < bool > ( "value" ) ;
2015-08-02 17:28:59 +00:00
break ;
2017-05-28 16:12:41 +00:00
case ItemType . InputSelect :
( ( SelectItem ) item ) . Value = arrItem . Value < string > ( "value" ) ;
break ;
2015-08-07 23:15:42 +00:00
case ItemType . Recaptcha :
( ( RecaptchaItem ) item ) . Value = arrItem . Value < string > ( "value" ) ;
2015-08-08 09:26:29 +00:00
( ( RecaptchaItem ) item ) . Cookie = arrItem . Value < string > ( "cookie" ) ;
2016-09-13 12:15:51 +00:00
( ( RecaptchaItem ) item ) . Version = arrItem . Value < string > ( "version" ) ;
( ( RecaptchaItem ) item ) . Challenge = arrItem . Value < string > ( "challenge" ) ;
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 )
{
var jObject = new JObject ( ) ;
jObject [ "id" ] = item . ID ;
jObject [ "type" ] = item . ItemType . ToString ( ) . ToLower ( ) ;
jObject [ "name" ] = item . Name ;
switch ( item . ItemType )
{
2015-08-07 23:15:42 +00:00
case ItemType . Recaptcha :
jObject [ "sitekey" ] = ( ( RecaptchaItem ) item ) . SiteKey ;
2016-09-13 12:15:51 +00:00
jObject [ "version" ] = ( ( RecaptchaItem ) item ) . Version ;
2015-08-07 23:15:42 +00:00
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 ;
2017-05-28 16:12:41 +00:00
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 ;
}
2015-08-03 21:38:45 +00:00
Item [ ] GetItems ( bool forDisplay )
{
2017-01-02 20:39:28 +00:00
List < Item > properties = GetType ( )
2015-08-03 21:38:45 +00:00
. GetProperties ( )
. Where ( p = > p . CanRead )
. Where ( p = > p . PropertyType . IsSubclassOf ( typeof ( Item ) ) )
2017-04-15 08:45:10 +00:00
. Select ( p = > ( Item ) p . GetValue ( this ) ) . ToList ( ) ;
// remove/insert Site Link manualy to make sure it shows up first
2017-01-02 20:39:28 +00:00
properties . Remove ( SiteLink ) ;
properties . Insert ( 0 , SiteLink ) ;
2015-08-03 21:38:45 +00:00
2017-01-02 20:39:28 +00:00
properties . AddRange ( dynamics . Values ) ;
2016-10-27 07:30:03 +00:00
2015-08-03 21:38:45 +00:00
if ( ! forDisplay )
{
properties = properties
2017-05-28 16:12:41 +00:00
. 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 )
2017-01-02 20:39:28 +00:00
. ToList ( ) ;
2015-08-03 21:38:45 +00:00
}
return properties . ToArray ( ) ;
}
2017-04-15 08:45:10 +00:00
public void AddDynamic ( string ID , Item item )
{
dynamics [ ID ] = item ;
2016-10-27 07:30:03 +00:00
}
2017-04-15 08:45:10 +00:00
public Item GetDynamic ( string ID )
{
try
{
return dynamics [ ID ] ;
}
catch ( KeyNotFoundException )
{
return null ;
}
2016-10-27 07:30:03 +00:00
}
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 ( ) ; } }
}
2015-08-02 17:28:59 +00:00
public class HiddenItem : StringItem
{
2015-08-03 21:38:45 +00:00
public HiddenItem ( string value = "" )
2015-08-02 17:28:59 +00:00
{
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
{
2015-08-07 23:15:42 +00:00
public string SiteKey { get ; set ; }
2015-04-13 06:25:21 +00:00
public string Value { get ; set ; }
2015-08-08 09:26:29 +00:00
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
}
2015-08-07 23:15:42 +00:00
public class RecaptchaItem : StringItem
{
2016-09-13 12:15:51 +00:00
public string Version { get ; set ; }
public string Challenge { get ; set ; }
2015-08-07 23:15:42 +00:00
public RecaptchaItem ( )
{
2016-09-13 12:15:51 +00:00
this . Version = "2" ;
2015-08-07 23:15:42 +00:00
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
}
2017-05-28 16:12:41 +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 ;
}
}
2015-08-03 21:38:45 +00:00
//public abstract Item[] GetItems();
2015-04-13 06:25:21 +00:00
}
}