November 22, 2009

The web and the evil microsoft spider

Microsoft is being such a spoilt sport in getting its products into the market that it really irritates me. Initially I was so excited with silverlight that I thought I should learn it. Once I stepped into the lapping waters of silverlight, I realized how badly you need to depend ONLY on microsoft technologies to take a swim to the deep end. Here's why:

XPath is almost a W3C standard. Its almost necessary for all programs to have an XPath parser. Microsoft can be so uptight in packaging silverlight that they left XPath out and have only provided support for LINQ to XML.

Im going to learn more on LINQ to XML and write a post on how to actually use it.

August 27, 2009

Silverlight Error - The specified valued cannot be assigned to the collection. The following type was expected UIElement

I got this error when I was fiddling around with the XAML in Expression Blend.

Fix: Check the XAML tags. If a closing tag is missing or there is a parse error with the XML syntax, this error could arise.

June 24, 2009

XML Transformer

This is my post about XML transformer. A PEAR extension that could work just great (apparently) in creating custom PHP tags. Here in this post I'll discuss my greviances in trying to deploy this.

Firstly, I wanted to install this. There was a catch here. I didn't want to do a normal PEAR installation, because it looked rather cumbersome and I wasn't in a mood to try out all the commands. So I copied the files and placed it in a new folder called XML. (This is only for XML_Transfermer though). The heirarchy was something like:

XML
- transformer.php
- util.php
/ transformer
/ and all other XML folders.

Now all I had to do was:

set_include_path('.'.PATH_SEPARATOR.'../'.PATH_SEPARATOR.'/AppServ/www/tmp/lib');
?>

This would set an additional include path to /lib which contained /XML. So far so god. I took nearly an hour to figure this out.

Now I was stuck with a mega huge problem of 'how the hell do I get this to work'. There is so little documentation available about XML_Transformer on the internet that its really bad. So I have decided to create a documentation of XML_transformer once I actually understand how to get it to work...

June 08, 2009

.NET and PHP

So I have experienced a lot of PHP and a lot more of .NET lately. And what do you know... they are now being packaged together by Microsoft. Those microsoft bastards are one of the best minds in the software market!

PHP is like untouchable by .NET, Ruby on rails (which is like the coolest MVC architecture by the way), JSP, CGI (grubby old technology)... none of these can shake PHP. With new CMS tools like Mambo, Joomla, WordPress, Drupal, and many many more, PHP is everybody's favorite when it comes to developing a quick and sexy sites. The new object oriented model in PHP 5.5 is just awesome.

Microsoft knowing that their .NET bomb cannot shake PHP, have decided to make it a part of the package they are shipping out with Visual Studio 2010. I think some hardcore PHP guys may not be very comfortable with this. It'll be like an Indian guy eating Upma (Uppit) with chopsticks. But for me, this is a great thing. I think they are going to include PHP hosting support with the new release of IIS. That just one server for both my areas of expertise. (Maybe expertise is a strong word... for now). No more switching between services to free my port 80.

Microsoft has actually done a good thing with this. PHP afterall is an opensource engine, and combining it with .NET would just be great. I would really look forward to a .NET CLR level code compatibility between PHP and ASP.NET someday. Or maybe that will never happen, as PHP is way too easy compared to ASP.NET with C#/VB. What the hell is J# i sometimes wonder!

Anyway... That's my update. Im really looking forward to user Visual Studio .NET with .NET 4.0. The SuperPreview is another sexed out application they are releasing. Oh by the way... all these are available as free downloads also. Microsoft is definitely going to get big. Silverlight is still a baby in a fish market.

November 01, 2008

Low level encryption in PHP

I was creating this site ih PHP where in there are lots of configuration details. Details like DB username and password, site theme information, etc. The site would be installed and used by an unknown person who would become the site admin. Now, if the admin is not so computer savvy and wanted to change some of the configurations, he could easily edit the configurations which could screw up several things. To correct the data, the admin will have to reinstall the whole site. In order to prevent this from happening, I wanted to encrypt the config data.

I used a simple conbination of hex conversion and binary packing to do this. Here is the code I used for this:


function hex2bin($h)
{
if (!is_string($h)) return null;
$r='';
for ($a=0; $a<strlen($h); $a+=2)
{
$r.=chr(hexdec($h{$a}.$h{($a+1)}));
}
return $r;

}

$config_data = file ("normal.txt");
$CFGSTR = "DO NOT EDIT THIS FILE";
foreach ($config_data as $n_str)
{
$hex_str = bin2hex ( $n_str );
$bin_str = pack ('h*', $hex_str);
$CFGSTR = $CFGSTR."\n".$bin_str;
}

$fp = fopen ("club.cfg", "w+");
fwrite ($fp, $CFGSTR, strlen ($CFGSTR));
fclose ($fp);

$cfg_data = file ("club.cfg");
echo "<hr>CONFIG DATA: <br>";
for ($i=1; $i<23; $i++)
{
$cfg_arr = unpack ('h*',$cfg_data[$i]);
$cfg = "";
foreach ($cfg_arr as $c)
$cfg = $cfg.$c;
$cfg = hex2bin ($cfg);

echo "$cfg <br>";
}


A brief explanation on the above code: The normal.txt file contains the ACSII data. This would be converted to a non-human readable format and stored in club.cfg file. My site is a club of sorts, so the name "club". Anyway, the first part of encryption is to convert the data to hex. Then pack it to a binary format. The reason why im converting it to hex first is to make the code platform independent. I am storing each configuration detail in a seperate line. So I can reference the data by the line number. The second foreach loop reads thils encrypted file and displays the data. The hex2bin function was created by someone else. (Ref + credits: http://us.php.net/bin2hex).

So a simple conbination of hex conversion and binary packing is good enough to create and easy low level data encryption in php.

Peace ..V,

October 18, 2008

Color chooser with PHP

I made this color chooser in PHP which is a rather simple code. It displays a table of known colors with RGB values ranging from 0 to 255. The table cell onclick property is used to redirect back to the same page by passing the color value through GET.

Here is the code:


<table cellspacing="0" cellpadding="0">

<?php
$inc = 255/5;
for ($r=0; $r<=255; $r+=$inc)
{
echo "<tr>";
for ($g=0; $g<=255; $g+=$inc)
{
for ($b=0; $b<=255; $b+=$inc)
{
$cstr = sprintf ("%02X%02X%02X", $r, $g, $b);
echo "<td width="5px" height="5px" bgcolor="#$cstr"
onclick="window.location='hello.php?g_cstr=$cstr'">n";
echo "</td>n";
}
}
$cstr = sprintf ("%02X%02X%02X", $r, $r, $r);
echo "<td width="5px" height="5px" bgcolor="#$cstr"
onclick="window.location='hello.php?g_cstr=$cstr'">n";
echo "</td>n";
echo "</tr>";
}
?>
</table>

The color you picked was:
<table> <tr>
<?php
$sel_col = $_GET['g_cstr'];
echo "<td width="25px" bgcolor="$sel_col"></td>n";
echo "<td>$sel_col</td>";
?>
</tr> </table>



The output will be something like this:


Peace ..V,

September 16, 2008

Simple bar graph in java

Here's a simple program in Java that reads integer values from a file and graphs it. This program is written in three files. The code is shown below.

File: drawGraph.java


import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
public class drawGraph
{
public static void main( String args[] )
{
JFrame frame = new JFrame( "Graph" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

CreateGraph createGraph = new CreateGraph();
createGraph.setBackground( Color.WHITE );
frame.add( createGraph );
frame.setSize( 640, 480 );
frame.setVisible( true );
}
}


File: Create Graph.java


import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class CreateGraph extends JPanel
{
public int x (int tx)
{
return (tx + 5);
}

public int y (int ty)
{
return (400 - ty + 5);
}

public void paintComponent( Graphics g )
{
super.paintComponent( g ); // call superclass's paint method
this.setBackground( Color.WHITE );

GraphComponents graphComponents = new GraphComponents();
graphComponents.openFile();
graphComponents.readRecords();
graphComponents.closeFile();

g.setColor( Color.BLACK );
g.drawLine( x(0), y(0), x(620), y(0) );
g.drawLine( x(0), y(0), x(0), y(460) );

g.setColor( Color.BLUE );
for (int i = 0; i < graphComponents.getMaxYCount(); i ++)
{
g.fillRect( x(i*50+10), y(graphComponents.getYValues(i)*20),
40, graphComponents.getYValues(i)*20 );
}


File: GraphComponents.java


import java.io.File;
import java.io.FileNotFoundException;
import java.lang.IllegalStateException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class GraphComponents
{
private Scanner input;
private int yValues [];
private int yCounter;

public GraphComponents ()
{
yValues = new int [10];
yCounter = 0;
}

public void storeYValues (int val)
{
yValues [yCounter++] = val;
}

public int getYValues (int index)
{
return yValues[index];
}

public int getMaxYCount ()
{
return yCounter;
}

public void openFile()
{
try
{
input = new Scanner( new File( ".\\graphvalues.txt" ) );
yCounter = 0;
}
catch ( FileNotFoundException fileNotFoundException )
{
System.err.println( "Error opening file." );
System.exit( 1 );
}
}

public void readRecords()
{
try
{
while ( input.hasNext() )
{
storeYValues (input.nextInt());
System.out.println (yValues[yCounter - 1]);
}
}
catch ( NoSuchElementException elementException )
{
System.err.println( "ERROR: Unable to read file (No such element)" );
input.close();
System.exit( 1 );
}
catch ( IllegalStateException stateException )
{
System.err.println( "ERROR: Unable to read file (State exception)" );
System.exit( 1 );
}
}

public void closeFile()
{
if ( input != null )
input.close();
}
}


Peace ..V,