Skip to content

Instantly share code, notes, and snippets.

View Martyr2's full-sized avatar
🪄
Looking for company to work magic for

Tim Hurd Martyr2

🪄
Looking for company to work magic for
View GitHub Profile
@Martyr2
Martyr2 / Webhook.cs
Created November 27, 2016 22:53
A C# .NET Web API class for handling webhooks from Chargify. Supply the class with request headers and the request body from a incoming webhook event to have it work its magic.
/// <summary>
/// Represents a webhook from Chargify.
/// </summary>
public class Webhook
{
private Dictionary<string, string> headerPayload = new Dictionary<string,string>();
private Dictionary<string, string> bodyPayload = new Dictionary<string, string>();
private string signatureHmacKey = string.Empty;
private string sharedSiteKey = <Chargify Store Site Key Here>;
@Martyr2
Martyr2 / GetVisitorIPAddress.cs
Created June 1, 2017 22:07
Gets the visitors IP address for an ASP.NET address. Will take a proxy address if real visitor IP is not forwarded along by the proxy.
/// <summary>
/// Get the IP address of the current client visiting the website.
/// </summary>
/// <returns>String representing the IP address</returns>
public static string GetVisitorIPAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress)) {
@Martyr2
Martyr2 / GetRandomString.cs
Created June 1, 2017 22:09
Generates a random string of a specified length. Defaults to 25 characters.
/// <summary>
/// Gets a random crypto string
/// </summary>
/// <param name="length">The length of the string to generate</param>
/// <remarks>Typically the length is 25</remarks>
/// <returns>Returns a random string of characters of length size.</returns>
public static string GetRandomString(int length = 25)
{
// Set the character space
string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
@Martyr2
Martyr2 / EncryptDecrypt.cs
Created June 1, 2017 22:11
Simple encryption and decryption functions that make the process easy to use.
/// <summary>
/// Decrypts a string that was encrypted using the encrypt method in this library.
/// </summary>
/// <param name="input">Encrypted string in base 64</param>
/// <param name="key">Salt key used in encryption</param>
/// <returns>Decrypted string data</returns>
public static string Decrypt(string input, string key)
{
byte[] inputArray = Convert.FromBase64String(input);
@Martyr2
Martyr2 / ValidIP.cs
Created June 1, 2017 22:13
A few utility functions for checking if an IP address is a IPv4 or IPv6 address.
/// <summary>
/// Checks if the supplied address string is an IPv4 version IP address.
/// </summary>
/// <param name="address">IP address to check for being IPv4 (minus any port)</param>
/// <returns>True if the address conforms to IPv4. False otherwise.</returns>
public static bool IsIPv4(string address)
{
string octetRegex = "([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])";
System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(String.Format(@"^{0}\.{0}\.{0}\.{0}$", octetRegex));
@Martyr2
Martyr2 / LeftRight.cs
Created June 1, 2017 22:15
Left and Right string extension methods in C# that mimic the old VB6 functions of the same name.
/// <summary>
/// Takes the left "length" characters of a string. Pretty much "Left" from the old VB library.
/// </summary>
/// <param name="str">String to take characters from</param>
/// <param name="length">Length of the string to take</param>
/// <returns>Substring of original string of the left most characters as specified by length</returns>
public static string Left(this string str, int length)
{
return str.Substring(0, Math.Min(length, str.Length));
}
@Martyr2
Martyr2 / flatten_array.php
Last active June 21, 2017 17:26
Iterative approach to flatten a multi-dimensional array of values.
<?php
namespace FlattenArray;
/**
* Iterative approach to flatten a multi-dimensional array of values.
* Notes: Non-array values are untouched so this could be an array of integers, strings, objects etc. An iterative approach is
* taken here over a recursive one as to not overload the call stack on deeply nested arrays.
*/
function flatten($ar) {
@Martyr2
Martyr2 / cryptor.php
Created September 23, 2017 17:23
Simple encryption / decryption utility class which uses openssl. Be sure to supply a strong cryptographic key. Change method as you see fit.
<?php
/**
* Static class for simple encryption and decryption utilities.
*/
class Cryptor {
// This key is used if one is not supplied during encryption / decryption.
private const UNIQUE_KEY = '<some default key here>';
private const METHOD = 'AES-256-CTR';
@Martyr2
Martyr2 / logger.php
Created September 25, 2017 05:03
Simple static logger class for logging errors. Provides convenience logging methods, date stamping the log file, getting call source and specifying log location.
<?php
class Logger {
private static $timestamp = 'Y-m-d H:i:s';
private static $logFileNameFormat = 'Y-m-d';
private static $logDirLocation = __DIR__;
private static $logLevels = ['DEBUG', 'INFO', 'WARN', 'ERROR'];
private static $minLogLevel = 'INFO';
@Martyr2
Martyr2 / user.php
Created October 1, 2017 16:46
Simple User class with supporting Address class. This serves as a basic starting point for developing a quick user class for projects and can be expanded on further.
<?php
/**
* Basic user class to hold some standard data and designed to be expanded and altered as necessary.
*/
class User {
private $userID = 0;
private $firstName = '';
private $lastName = '';
private $address = null;