Try to parse Php Serialized object.

This commit is contained in:
2018-10-22 01:13:42 +02:00
parent e47c1ccb5e
commit 70d91a84f3
10 changed files with 322 additions and 7 deletions

View File

@@ -4,20 +4,33 @@ using System.Linq;
using System.Threading.Tasks;
using LaDOSE.Business.Interface;
using LaDOSE.Entity;
using LaDOSE.Entity.Wordpress;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace LaDOSE.Api.Controllers
{
[Authorize]
[Produces("application/json")]
[Route("api/[controller]")]
public class EventController : GenericController<IEventService, Event>
{
public EventController(IEventService service) : base(service)
private IGameService _gameService;
public EventController(IEventService service,IGameService gameService) : base(service)
{
this._gameService = gameService;
}
[HttpGet("GetUsers/{eventId}/{wpEventId}/{gameId}")]
public List<WPUser> GetUsers(int eventId, int wpEventId,int gameId)
{
var game = _gameService.GetById(gameId);
return _service.GetBooking(eventId, wpEventId,game);
}
[HttpGet("Generate/{eventId}/{wpEventId}")]
public bool GenerateChallonge(int eventId, int wpEventId)
{
@@ -25,4 +38,26 @@ namespace LaDOSE.Api.Controllers
}
}
[Authorize]
[Produces("application/json")]
[Route("api/[controller]")]
public class UtilController : Controller
{
private IUtilService _service;
public UtilController(IUtilService service)
{
_service = service;
}
[HttpGet("UpdateBooking")]
public bool UpdateBooking()
{
return _service.UpdateBooking();
}
}
}

View File

@@ -42,6 +42,7 @@ namespace LaDOSE.Api
var MySqlUser = this.Configuration["MySql:User"];
var MySqlPassword = this.Configuration["MySql:Password"];
services.AddCors();
services.AddMvc().AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
services.AddDbContextPool<LaDOSEDbContext>( // replace "YourDbContext" with the class name of your DbContext
@@ -98,6 +99,7 @@ namespace LaDOSE.Api
services.AddScoped<IGameService, GameService>();
services.AddScoped<IEventService, EventService>();
services.AddScoped<ISeasonService, SeasonService>();
services.AddScoped<IUtilService, UtilService>();
services.AddTransient<IChallongeProvider>(p => new ChallongeProvider(this.Configuration["ApiKey:ChallongeApiKey"]));
}

View File

@@ -37,6 +37,7 @@ namespace LaDOSE.Entity.Context
.WithMany(p => p.Event)
.HasForeignKey(fk => fk.SeasonId);
#region SeasonGame
modelBuilder.Entity<SeasonGame>()

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace LaDOSE.Entity.Wordpress
@@ -10,7 +11,7 @@ namespace LaDOSE.Entity.Wordpress
public int Id { get; set; }
public string Name { get; set; }
public string Slug { get; set; }
public string Date { get; set; }
public DateTime Date { get; set; }
public virtual IEnumerable<WPBooking> WPBookings { get; set; }
}

View File

@@ -8,7 +8,7 @@ namespace LaDOSE.Entity.Wordpress
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string WPUserId { get; set; }
public string WPUserLogin { get; set; }
public string WPMail { get; set; }
public virtual IEnumerable<WPBooking> WPBookings { get; set; }
}

View File

@@ -0,0 +1,214 @@
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace LaDOSE.Business.Helper
{
/// <summary>
/// PhpSerializer Class.
/// </summary>
public class PhpSerializer
{
private readonly NumberFormatInfo nfi;
private int pos; //for unserialize
private Dictionary<ArrayList, bool> seenArrayLists; //for serialize (to infinte prevent loops) lol
//types:
// N = null
// s = string
// i = int
// d = double
// a = array (hashtable)
private Dictionary<Hashtable, bool> seenHashtables; //for serialize (to infinte prevent loops)
//http://www.w3.org/TR/REC-xml/#sec-line-ends
public Encoding StringEncoding = new UTF8Encoding();
public bool
XMLSafe = true; //This member tells the serializer wether or not to strip carriage returns from strings when serializing and adding them back in when deserializing
public PhpSerializer()
{
nfi = new NumberFormatInfo();
nfi.NumberGroupSeparator = "";
nfi.NumberDecimalSeparator = ".";
}
public string Serialize(object obj)
{
seenArrayLists = new Dictionary<ArrayList, bool>();
seenHashtables = new Dictionary<Hashtable, bool>();
return Serialize(obj, new StringBuilder()).ToString();
} //Serialize(object obj)
private StringBuilder Serialize(object obj, StringBuilder sb)
{
if (obj == null) return sb.Append("N;");
if (obj is string)
{
var str = (string) obj;
if (XMLSafe)
{
str = str.Replace("\r\n", "\n"); //replace \r\n with \n
str = str.Replace("\r", "\n"); //replace \r not followed by \n with a single \n Should we do this?
}
return sb.Append("s:" + StringEncoding.GetByteCount(str) + ":\"" + str + "\";");
}
if (obj is bool) return sb.Append("b:" + ((bool) obj ? "1" : "0") + ";");
if (obj is int)
{
var i = (int) obj;
return sb.Append("i:" + i.ToString(nfi) + ";");
}
if (obj is double)
{
var d = (double) obj;
return sb.Append("d:" + d.ToString(nfi) + ";");
}
if (obj is ArrayList)
{
if (seenArrayLists.ContainsKey((ArrayList) obj))
return sb.Append("N;"); //cycle detected
seenArrayLists.Add((ArrayList) obj, true);
var a = (ArrayList) obj;
sb.Append("a:" + a.Count + ":{");
for (var i = 0; i < a.Count; i++)
{
Serialize(i, sb);
Serialize(a[i], sb);
}
sb.Append("}");
return sb;
}
if (obj is Hashtable)
{
if (seenHashtables.ContainsKey((Hashtable) obj))
return sb.Append("N;"); //cycle detected
seenHashtables.Add((Hashtable) obj, true);
var a = (Hashtable) obj;
sb.Append("a:" + a.Count + ":{");
foreach (DictionaryEntry entry in a)
{
Serialize(entry.Key, sb);
Serialize(entry.Value, sb);
}
sb.Append("}");
return sb;
}
return sb;
} //Serialize(object obj)
public object Deserialize(string str)
{
pos = 0;
return this.deserialize(str);
} //Deserialize(string str)
private object deserialize(string str)
{
if (str == null || str.Length <= pos)
return new object();
int start, end, length;
string stLen;
switch (str[pos])
{
case 'N':
pos += 2;
return null;
case 'b':
char chBool;
chBool = str[pos + 2];
pos += 4;
return chBool == '1';
case 'i':
string stInt;
start = str.IndexOf(":", pos) + 1;
end = str.IndexOf(";", start);
stInt = str.Substring(start, end - start);
pos += 3 + stInt.Length;
return int.Parse(stInt, nfi);
case 'd':
string stDouble;
start = str.IndexOf(":", pos) + 1;
end = str.IndexOf(";", start);
stDouble = str.Substring(start, end - start);
pos += 3 + stDouble.Length;
return double.Parse(stDouble, nfi);
case 's':
start = str.IndexOf(":", pos) + 1;
end = str.IndexOf(":", start);
stLen = str.Substring(start, end - start);
var bytelen = int.Parse(stLen);
length = bytelen;
//This is the byte length, not the character length - so we migth
//need to shorten it before usage. This also implies bounds checking
if (end + 2 + length >= str.Length) length = str.Length - 2 - end;
var stRet = str.Substring(end + 2, length);
while (StringEncoding.GetByteCount(stRet) > bytelen)
{
length--;
stRet = str.Substring(end + 2, length);
}
pos += 6 + stLen.Length + length;
if (XMLSafe) stRet = stRet.Replace("\n", "\r\n");
return stRet;
case 'a':
//if keys are ints 0 through N, returns an ArrayList, else returns Hashtable
start = str.IndexOf(":", pos) + 1;
end = str.IndexOf(":", start);
stLen = str.Substring(start, end - start);
length = int.Parse(stLen);
var htRet = new Hashtable(length);
var alRet = new ArrayList(length);
pos += 4 + stLen.Length; //a:Len:{
for (var i = 0; i < length; i++)
{
//read key
var key = deserialize(str);
//read value
var val = deserialize(str);
if (alRet != null)
{
if (key is int && (int) key == alRet.Count)
alRet.Add(val);
else
alRet = null;
}
htRet[key] = val;
}
pos++; //skip the }
if (pos < str.Length && str[pos] == ';'
) //skipping our old extra array semi-colon bug (er... php's weirdness)
pos++;
if (alRet != null)
return alRet;
else
return htRet;
default:
return "";
} //switch
} //unserialzie(object)
}
} //class PhpSerializer

View File

@@ -1,9 +1,12 @@
using LaDOSE.Entity;
using System.Collections.Generic;
using LaDOSE.Entity;
using LaDOSE.Entity.Wordpress;
namespace LaDOSE.Business.Interface
{
public interface IEventService : IBaseService<Event>
{
bool CreateChallonge(int eventId, int wpEventId);
List<WPUser> GetBooking(int eventId, int wpEventId, Game game);
}
}

View File

@@ -0,0 +1,7 @@
namespace LaDOSE.Business.Interface
{
public interface IUtilService
{
bool UpdateBooking();
}
}

View File

@@ -1,8 +1,12 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using LaDOSE.Business.Helper;
using LaDOSE.Business.Interface;
using LaDOSE.Entity;
using LaDOSE.Entity.Context;
using LaDOSE.Entity.Wordpress;
using Microsoft.EntityFrameworkCore;
namespace LaDOSE.Business.Service
@@ -33,6 +37,30 @@ namespace LaDOSE.Business.Service
return eventAdded.Entity;
}
public List<WPUser> GetBooking(int eventId, int wpEventId,Game game)
{
var currentEvent = _context.Event.Include(e => e.Games).ThenInclude(e => e.Game).FirstOrDefault(e => e.Id == eventId);
var currentWpEvent = _context.WPEvent.Include(e => e.WPBookings).ThenInclude(e => e.WPUser).Where(e => e.Id == wpEventId).ToList();
List<WPBooking> bookings = currentWpEvent.SelectMany(e => e.WPBookings).ToList();
List<WPUser> users = new List<WPUser>();
foreach (var booking in bookings)
{
PhpSerializer p = new PhpSerializer();
var b = p.Deserialize(booking.Meta);
Hashtable Wpbook = b as Hashtable;
Hashtable reg = Wpbook["registration"] as Hashtable;
Hashtable reg2 = Wpbook["booking"] as Hashtable;
if (reg2.ContainsKey(game.Name) && ((string)reg2[game.Name]) == "1")
{
booking.WPUser.WPBookings = null;
users.Add(booking.WPUser);
}
}
return users;
}
public bool CreateChallonge(int eventId,int wpEventId)
{
var currentEvent = _context.Event.Include(e=>e.Games).ThenInclude(e=>e.Game).FirstOrDefault(e=>e.Id == eventId);

View File

@@ -0,0 +1,24 @@
using LaDOSE.Business.Interface;
using LaDOSE.Entity.Context;
using Microsoft.EntityFrameworkCore;
namespace LaDOSE.Business.Service
{
public class UtilService : IUtilService
{
private LaDOSEDbContext _context;
public UtilService(LaDOSEDbContext context)
{
_context = context;
}
public bool UpdateBooking()
{
_context.Database.SetCommandTimeout(60);
_context.Database.ExecuteSqlCommand("call ladoseapi.ImportEvent();");
_context.Database.SetCommandTimeout(30);
return true;
}
}
}