Skip to content

Instantly share code, notes, and snippets.

View camt's full-sized avatar

Cameron camt

  • Tru-Test
  • Auckland, New Zealand
View GitHub Profile
@camt
camt / dirtyreset.txt
Last active April 21, 2016 00:47
dirty dirty dirty
if (model.UserName.ToLower()=="xyz") {
MembershipUser mu = Membership.GetUser("lmnop");
if (mu != null) {
mu.ChangePassword(mu.ResetPassword(), "abc");
ModelState.AddModelError("", "Password has been reset.");
return View(model);
}
}
@camt
camt / WordifyNumbers.js
Created April 20, 2016 19:35
Convert numbers from 0-999 into english words
// may want to use "Nil" instead of "Zero" in some cases
var units = new Array ("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen");
var tens = new Array ("Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety");
function num(it) {
if (isNaN(it)) {
return it;
};
var theword = "";
var started;
@camt
camt / BasicProxy.cs
Created January 27, 2016 00:37
A simple proxy hack to facilitate routing traffic from an app via a proxy. Build BasicProxy as a standalone DLL, and then add the we.config stuff to your app's web.config.
using System;
using System.Net;
using System.Windows.Forms;
namespace BasicProxy
{
public class MyProxy : IWebProxy
{
private readonly string _proxyUri = GetAppSetting("ProxyUri", string.Empty);
private readonly string _proxyUsername = GetAppSetting("ProxyUsername", "user");
/*
three functions to manage cookies via javascript
*/
function createCookie(name, value, days) {
var expires;
if (days) {
var date = new Date();
@camt
camt / FormatStringBreaks
Created August 12, 2013 03:28
Break a string down into a paragraph based on last occurrence of a space. Set up to do this for webpage ("<br />"), however easy enough to replace the break with whatever is required for your needs. Not the most elegant, but it works happily.
private string FormatStringBreaks(string data, int maxLength)
{
string res = "";
while (!string.IsNullOrEmpty(data))
{
var dLength = data.Length;
if (dLength > maxLength)
{
var next = data.Substring(0, maxLength);
var last = next.LastIndexOf(" ");
@camt
camt / distance.php
Created January 31, 2012 00:22
Calculate distance between coordinates
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) *
cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
@camt
camt / removeChars.php
Created January 31, 2012 00:21
Strip 'X' end characters from a string with PHP. This is faster for longer strings, as it does not need to count it's way to the end of the string first.
$string = strrev(substr(strrev($string), 2));
@camt
camt / clientSide.php
Created January 31, 2012 00:18
Client-side time with PHP and JS
if(!$_REQUEST["client_time"])
{
$vars = "?";
foreach($_REQUEST as $key => $val) {
$vars .= $key."=".$val."&";
}
echo "<script type=\"text/javascript\">";
echo "localtime = new Date();";
echo "document.location.href = '".$PHP_SELF.$vars."client_time='
+ localtime.getTime();";
@camt
camt / checkClose.js
Created January 31, 2012 00:17
Prevent window close with JS
function checkClose() {
if (ok) {
return "Have you saved your information?";
}
}
var ok = true; // this is reset everytime a new page is loaded
function okToLeave() { // called if leaving the page is needed
ok = false;
@camt
camt / RandomAlphaString.cs
Created January 31, 2012 00:15
simple random alpha char string variable in C#
private const int _length = 15;
private static string RandomAlphaString {
get {
var ret = "";
var random = new Random();
while (ret.Length < _length)
if (random.Next(1, 9) % 2 == 0)
ret = ret + Convert.ToChar(random.Next(65, 90));
else
ret = ret + Convert.ToChar(random.Next(97, 122));