C# XML Reader, CSV Reader and vCard Reader

Import.XMLCSV is a basic simple, and usable XML, CSV and vCard reader

CSV Usage:

CSVImport m_csv = new CSVImport();
m_csv.Load("thecsvfile.txt");
CSVRow m_row = m_csv.GetRow(1); //get the first row

XML Usage:

XMLImport m_xml = new XMLImport();
XMLNode m_root_node = m_xml.LoadXML("xmlfile.xml");
/* you can now use m_root_node and traverse your way down to all the child nodes */

Includes Visual Studio 2008 project file, but works all the way back to .NET 2.0 and all the way up to .NET 4.0

Download Import.XMLCSV

The Advantages of Secure Passwords

Secure passwords are random strings of letters (both upper case and lower case), numbers, and special symbols. Rather than simply using a dictionary word.

All passwords should be stored as a hash, so that nobody with access to the database they are stored in can simply see all the plain text passwords.

The advantage is that a totally random password can only be broken by a brute force method, which means a program has to generate random strings and create the hash for that string, and compare it to what is in the database. Once it finds something that matches, that is the password.

On a powerful computer, a well written program can run through around 17 billion different generations, hashes and checks per hour. (2 * (2^33))

The longer the password is, the longer it takes to crack the password via the brute force method. See below for examples.

Upper or Lower Case Only

No. Characters 1 Compter 100 Computers 1,000 Computers (small botnet)
8 6.08 hours 0.06 hours 0.006 hours
10 4,108.5 hours 41.085 hours 4.1084 hours
12 2,777,348.18 hours 27,773.48 hours 2,777.35 hours

Mixed Case and Numbers

No. Characters 1 Compter 100 Computers 1,000 Computers (small botnet)
8 6,354.53 hours 63.55 hours 6.35 hours
10 24,426,826.45 hours 244,268.26 hours 24,426.83 hours
12 93,896,720,861.02 hours 938,967,208.61 hours 93,896,720.86 hours

Mixed Case, Numbers and Special Symbols

No. Characters 1 Compter 1,000 Computers 100,000 Computers
8 177,407.91 hours 177.41 hours 1.77 hours
10 1,567,576,296.21 hours 1,567,576.30 hours 15,675.76 hours
12 13,851,104,153,269.40 hours 13,851,104,153.27 hours 138,511,041.53 hours

As you can see, not only is the character space (upper case/lower
case/numbers/symbols) important, but also string length. e.g. an 8 character,
totally random string using mixed case, numbers and symbols could be cracked in
less than two hours on a larger botnet, and it is likely that the person
stealing this information has access to something like this, but for a 12
character string, this would take over 15,800 years on a 100,000 computer
botnet.

p.s. A 32 character mixed case, numbers and sybol password would take upto
45,839,513,591,436,800,000,000,000,000,000,000,000,000 years on 100,000,000 (100 million) computers,
to brute force which is one-million-trillion-trillion times longer than the universe has existed.

Generic htaccess mod_rewrite SEO rules

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [L,R=301]

Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://%{HTTP_HOST}/ [R=301,L]

RewriteCond %{THE_REQUEST} ^.*/index.htm
RewriteRule ^(.*)index.htm$ http://%{HTTP_HOST}/ [R=301,L]

RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://%{HTTP_HOST}/ [R=301,L]

this little strip of code will make sure you always have a WWW and will make sure that you dont have / and index as two seperate pages.

Fix broken image links with javascript

When you have large scale websites that span many servers, sometimes servers go down, and you get a missing image, even if the server is up, you can get corruption loading to a broken image that does not display correctly. This piece of javascript aims to fix broken images on each page load, giving an overall more profesional look. Quite simply, the javascript adds a hook into each image and checks to see if the image has loaded properly or not, and if not, a replacment images is put there instead to make sure that the missing image is auto-fixed for you and the site does not have any missing images.

step 1. add this piece of code as the very last thing before the body close tag within a <script> tag

  var i = 0;
  var img = new Image();
  for (i = 0; i < document.images.length; i++) {
    img = document.images[i];
    img.onerror = function (evt) {
      this.src = "soon.gif";
    }
  };

step 2. “soon.gif” image and drop it within the same directory as the file that is using the script (i.e. the root web directory)

now you will notice that when you have missing images within the site, they will now be replaced by “soon.gif”.

Notes. you can put the replacement image within a directory and use any filename you wish as long as you reflect the changes within the javascript.

Automated Search Engine Submission via Sitemap Ping

If you want to automate the way in which you submit your new websites to the major search engines, try using the sitemap.xml pinging service. You just replace [FULL SITEMAP URL] with a URL encoded version of the full URL of your sitemap and then GET request each ping service.

http://www.google.com/webmasters/tools/ping?sitemap=[FULL SITEMAP URL]
http://search.yahooapis.com/SiteExplorerService/V1/ping?sitemap=[FULL SITEMAP URL]
http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=SitemapWriter&url=[FULL SITEMAP URL]
http://submissions.ask.com/ping?sitemap=[FULL SITEMAP URL]
http://www.bing.com/webmaster/ping.aspx?siteMap=[FULL SITEMAP URL]
http://api.moreover.com/ping?u=[FULL SITEMAP URL]

Load and Parse Large XML Files in PHP

Usually, PHP is limited to using somewhere between 16mb and 128mb of RAM. So what happens if you want to parse a 1.1gb file of exported product data (over 500,000 products) and not hit the RAM limiter?

At first this seemed to be a pretty impossible task, as to parse the file you require the entire XML to parse it to a tree.

Usually you would run something such as file_get_contents() and then parse the contents returned, but this would load in the entire 1.1gb of XML and put you well beyond most PHP ram limiters.

What you need to do is parse the XML in small chunks (example below uses 128kb chunks) and parse those bit by bit, this way, you get to speedily parse through your XML file, while at the same time, steer clear of the PHP RAM limiter.

set_time_limit(0);
define('__BUFFER_SIZE__', 131072);
define('__XML_FILE__', 'pf_1360591.xml');

function elementStart($p, $n, $a) {
  //handle opening of elements
}

function elementEnd($p, $n) {
  //handle closing of elements
}

function elementData($p, $d) {
  //handle cdata in elements
}

$xml = xml_parser_create();

xml_parser_set_option($xml, XML_OPTION_TARGET_ENCODING, 'UTF-8');
xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);

xml_set_element_handler($xml, 'elementStart', 'elementEnd');
xml_set_character_data_handler($xml, 'elementData');

$f = fopen(__XML_FILE__, 'r');
if($f) {
  while(!feof($f)) {
    $content = fread($f, __BUFFER_SIZE__);

    xml_parse($xml, $content, feof($f));

    unset($content);
  }
  fclose($f);
}

Paranoid about cookies

A lot of people are paranoid about cookies, and not without reason, but the simple fact is that this is how you create persistence in a stateless protocol. I’ve heard all the arguments and all the debates on the subject, and this is how we’re doing it. The only information that can be stored in a cookie is information you have already handed over and its impossible for more information to be gathered other than what you have already supplied.
If you don’t want to use cookies, you don’t have to. You can still use most websites but you will not be able to use some of the advanced features since they require logging in and having your login information stored on your pc. If your paranoia requires you not to use cookies, this is the sacrifice you’ll have to make.
A lot of people are paranoid about cookies, and not without reason, but the simple fact is that this is how you create persistence in a stateless protocol. I’ve heard all the arguments and all the debates on the subject, and this is how we’re doing it. The only information that can be stored in a cookie is information you have already handed over and its impossible for more information to be gathered other than what you have already supplied.
If you don’t want to use cookies, you don’t have to. You can still use most websites but you will not be able to use some of the advanced features since they require logging in and having your login information stored on your pc. If your paranoia requires you not to use cookies, this is the sacrifice you’ll have to make.

Imitate target=_blank with jQuery

You can replace target=_blank with jquery, or prototype or with raw javascript if you like. Here is the code i came up with to do just this.

jQuery

$(function() {
	$('a[rel*=external]').click(function() {
		var w = window.open(this.href);
		if(!w) alert("Boo! A popup blocker stopped our window from opening");
		return false;
	});
});

PHP Installer for Web Apps

We all know uploading things via ftp like Squirrel Mail or phpMyAdmin, or any application that has 100′s of small files takes a long time. This is mainly due to the overhead in the commands that have to be preformed to upload each file individually.

The first thing we need to do is to package all of our files into a single large file, this will allow us to massively reduce overhead, since there is only ever one send file command sent. How we do this, is simply add all the files (and the root directory if required) into a zip, so that when they are extracted to the directory on the server, they will appear on the server correctly.

Once we have zipped all of our files up, how do we then expect to be able to unzip them? We need a small bootstrap script (example below) to extract them to the server, which yes, is a second file that is small, but beats having to upload those over few 100 files.

$zip = new ZipArchive();
$r = $zip->open("myzip.zip");
if($r == TRUE) {
  $zip->extractTo("./");
}
$zip->close();

If you now upload and run your bootstrap script, you will notice (if you have the zip extension loaded in PHP) that your zip file has been extracted on the server, and was done a vast amount quicker than it would have taken to upload the individual files.

$package = "myadmin.zip";

$c = file_get_contents($package);

$content = base64_encode($c);
$bcontent = "";

for($i=0; $i<strlen($content); $i+=1024) {
  $c = substr($content, $i, 1024);
  $bcontent .= "\$content .= \"{$c}\";\r\n";
}

$f = fopen("installer.php", "w+");
if($f) {
  fwrite($f, "<?php\r\n");
  fwrite($f, "\$content = \"\";\r\n");
  fwrite($f, $bcontent);
  fwrite($f, "file_put_contents('{$package}', base64_decode(\$content));\r\n");
  fwrite($f, "\$zip = new ZipArchive();\r\n");
  fwrite($f, "\$zip->open('{$package}');\r\n");
  fwrite($f, "\$zip->extractTo('./');\r\n");
  fwrite($f, "\$zip->close();\r\n");
  fwrite($f, "?" . ">");
  fclose($f);
}

Set font size in Win32 API EDIT Window

i have recently been attempting to write a small text editor application, and the main problem i found was migrating notepad settings over to my application, the font for 10pt was stored as 100.

obviously, i needed to divide by 10, but when i did this, the font came out so small it was unreadable.

after some digging, i found the following works perfectly.

int m_height = -MulDiv(g_point_size/10, GetDeviceCaps(m_dc, LOGPIXELSY), 72);

once you have done this, just SendMessage to the EDIT you want to set the font size of, and bingo, we have a correct font size

<< Latest posts

Older posts >>