Thursday, December 22, 2005

Simulate register_globals = on in PHP

Add the following lines on the top of your PHP code. This will get a result similar to switching register_globals=on

foreach($_POST AS $key => $value) { ${$key} = $value; }
foreach($_GET AS $key => $value) { ${$key} = $value; }

Friday, November 25, 2005

Use multiple submit buttons on one form using PHP

<form>

<input type="submit" name="submit" value="NEW" />

<input type="submit" name="submit" value="SAVE" />

<input type="submit" name="submit" value="EDIT" />

</form>

<?php


switch ($_GET['submit']) {


case 'NEW':

print 'This is what happens when new button is clicked.';

break;


case 'SAVE':

print 'This is what happens when save button is clicked.';

break;


case 'EDIT':

print 'This is what happens when edit button is clicked.';

break;


default:

print 'You can put the html form here.';

break;


} // End: switch ($_GET['submit'])


?>


Multiple submit buttons on a single form using JavaScript

<SCRIPT>

function submitFunction(i) {

if (i==1) document.form1.action=

"http://www.company.com/cgi-bin/cgi1.cgi";

if (i==2) document.form1.action=

"http://www.company.com/cgi-bin/cgi2.cgi";

if (i==3) document.form1.action=

"http://www.company.com/cgi-bin/cgi3.cgi";

document.form1.submit()

}

</SCRIPT>

<FORM NAME="form1">

<INPUT TYPE="button" VALUE="Submit 1" onClick="submitFunction(1)">

<INPUT TYPE="button" VALUE="Submit 2" onClick="submitFunction(2)">

<INPUT TYPE="button" VALUE="Submit 3" onClick="submitFunction(3)">

</FORM>

Tuesday, July 12, 2005

C# Genrerate Thumbnail Source Code

protected void genThumbnailPhoto(string fileName, string path)
{
System.IO.FileInfo info = new System.IO.FileInfo(fileName);

using(Bitmap bitmap1 = new Bitmap(fileName, false))
{
int i= 0;
int TWidth;
int THeight;
if(bitmap1.Width >= 90 && bitmap1.Height >=90 )
{
if(bitmap1.Width > bitmap1.Height )
{
TWidth = 90;
i = bitmap1.Width/90;
THeight = bitmap1.Height / i;
}
else
{
THeight = 90;
i = bitmap1.Height/90;
TWidth = bitmap1.Width / i;
}
}
else
{
TWidth = bitmap1.Width;
THeight = bitmap1.Height;

}

using(System.Drawing.Image image =
System.Drawing.Image.FromFile(fileName))
using(Bitmap bitmap = new Bitmap(image, TWidth, THeight))
{
bitmap.Save(path + "/t__" + info.Name , image.RawFormat);
}

}


}

C# Return a web page in a string

public static string HttpConnection(string strUrl)
{
string strResponse = "";
try
{
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("big5");
System.IO.Stream stream = webResponse.GetResponseStream();
System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, encode);
strResponse = streamReader.ReadToEnd();

//-----release the reader and stream resource-----
streamReader.Close();
stream.Close();
}
catch(Exception exp)
{
throw exp;
}
return strResponse;
}

C# Get result of a web page with POST variables

public static string HttpPOSTConnection(string strUrl,string Param)
{
string strResponse = "";
try
{
string postData = Param;
byte[] byte1=System.Text.Encoding.Default.GetBytes(postData);
// Set the content type of the data being posted.
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
webRequest.Method = "POST";
webRequest.ContentType="application/x-www-form-urlencoded";
// Set the content length of the string being posted.
webRequest.ContentLength=byte1.Length;
System.IO.Stream newStream=webRequest.GetRequestStream();
newStream.Write(byte1,0,byte1.Length);

System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("big5");
System.IO.Stream stream = webResponse.GetResponseStream();
System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.Default);
strResponse = streamReader.ReadToEnd();

//-----release the reader and stream resource-----
newStream.Close();
streamReader.Close();
stream.Close();
}
catch(Exception exp)
{
throw exp;
}
return strResponse;
}

C# Send mail, send web page by email

using System;
using System.Web.Mail;

namespace NEWS_RANK.util.net
{

public class Mail
{
private static string smtpserver = "127.0.0.1";


public static void sendMail(string To, string From, string Subject, string Body)
{
try
{
//-----Mail properties----
MailMessage mMessage = new MailMessage();
mMessage.From = From;
mMessage.To = To;
mMessage.Subject = Subject;
mMessage.Body = Body;
mMessage.BodyFormat = MailFormat.Html;
//-----Mail Send-----
SmtpMail.SmtpServer = smtpserver;
SmtpMail.Send(mMessage);
}
catch(Exception exp)
{
throw exp;
}
}


public static void sendMailHttpUrl(string To, string From, string Subject, string HttpUrl,string Query,string HttpMethod)
{
try
{
//-----Mail properties------
MailMessage mMessage = new MailMessage();
mMessage.From = From;
mMessage.To = To;
mMessage.Subject = Subject;
string mailContent = "";
if( HttpMethod != null && HttpMethod.ToLower().Equals("post") )
{
Uri baseUri = new Uri(HttpUrl);
string path = baseUri.GetLeftPart(System.UriPartial.Path);
string param = "";
if( Query != null &&amp;amp; Query.Length > 1 )
param = Query;
mailContent = HttpUtil.HttpPOSTConnection(path,param);
}
else mailContent = HttpUtil.HttpConnection(HttpUrl);
mMessage.Body = mailContent;
mMessage.BodyFormat = MailFormat.Html;
//-----Mail Send-----
SmtpMail.SmtpServer = smtpserver;
SmtpMail.Send(mMessage);
}
catch(Exception exp)
{
throw exp;
}
}
}
}

Saving a web page to a file using C#

public static void HttpConnectionSaveToFile(string strUrl,string SavedPath)
{
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
System.IO.Stream stream = webResponse.GetResponseStream();
System.IO.BinaryReader streamReader = new System.IO.BinaryReader(stream);
byte[] b = streamReader.ReadBytes((int)webResponse.ContentLength);
//-----release the reader and stream resource-----
if (System.IO.File.Exists(SavedPath))
{
//Console.WriteLine("{0} already exists!", SavedPath);
System.IO.File.Delete(SavedPath);
}
System.IO.FileStream fs = new System.IO.FileStream(SavedPath, System.IO.FileMode.CreateNew);
// Create the writer for data.
System.IO.BinaryWriter w = new System.IO.BinaryWriter(fs);
// Write data to Test.data.
w.Write( b );
w.Close();
fs.Close();

streamReader.Close();
stream.Close();
}

Thursday, March 10, 2005

SpAcDaemon Virus

Today I was kind of doing some house keeping job on my computer.
To my supprise, when I went to My Computer -> System Properties,
I was looking at a picture of Bart Simpson and some weird info
like this:

Manufactured and supported by:
SPAC
thespacdude@yahoo.com

and when I clicked on Support Information, it shows:

SPAC
ERROR: Water detected in the BIOS.

Something wonderful has happened,
your computer is alive......

I googled it, I also tried McFee, Symantec, MS AntiSpyware, Adware, SpyBot, F-Secure, etc.
But none would detect any virus or trojans in my computer.
There're 2 strange behavior on my computer recently.
1. At Windows Startup, it plays some weird sound... you hear someone laughing.
2. It changed my OEM information, so I saw "Manufactured and supported by: SPAC"

After about 3 hours of digging thru my registry and system files, I found it.
The file is c:\windows\mssreprc.exe
also it creates a registry key

Name:SpAcDaemon Value: C:\WINDOWS\mssreprc.exe
under HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
It changed my C:\WINDOWS\system32\OEMINFO.INI to the following:

[general]
Manufacturer=SPAC
Model=thespacdude@yahoo.com
[Support Information]
Line1= ERROR: Water detected in the BIOS.
Line2=
Line3= Something wonderful has happened,
Line4= your computer is alive......

and created a Bart Simpson Logo as a bmp file
C:\WINDOWS\system32\Oemlogo.bmp

Saturday, February 26, 2005

Import mails from Thunderbird to Microsoft Outlook step by step

1. Download a program called IMAPSize

2. After installing IMAPSize, run it. You dont have to create an account, just click "no".

3. Click on "tools" -> mbox2eml

4. Locate C:\Documents and Settings\USERNAME\Application Data\Thunderbird\Profiles\xtsb8for.default\Mail\Local Folders\Inbox file, "Inbox" is the mbox file that Thunderbird stores all your inbox mails.

5. Create a folder such as C:\eml and save the eml files there

6. Click on convert, this will convert each of your mail in your Thunderbird mailbox into an eml file.

7. Now you can open Outlook Express.

8. Go to C:\eml, select all eml files, drag and drop them to the Inbox or whatever mail folder you wish in Outlook Express

9. Open Outlook, click on File - Import/export, then import from Outlook Express

Friday, January 07, 2005

How do I add install/uninstall option to a C# windows service program?


1. Add this to your service project


/*****************************

*

* install.cs

* Install the service

*

*/


using System;

using System.Collections;

using System.ComponentModel;

using System.Configuration.Install;

using System.ServiceProcess;

namespace MyService

{

[RunInstaller(true)]

public class MyInstaller : Installer

{

public MyInstaller()

{

ServiceProcessInstaller spi=new ServiceProcessInstaller();

spi.Account = System.ServiceProcess.ServiceAccount.LocalSystem;

spi.Password = null;

spi.Username = null;

ServiceInstaller si = new ServiceInstaller();

si.StartType =ServiceStartMode.Automatic;
si.ServiceName = new MyService().ServiceName;

this.Installers.Add(spi);

this.Installers.Add(si);

}

}

}


2. Add System.Configuration.Install reference to the project


3. The main function shuold look like this


// The main entry point for the process

static void Main(string[] args)

{

string opt=null;


// install the service if user entered "ServiceMonitor /install"

if(args.Length >0 )

{

opt=args[0];

}

if(opt!=null && opt.ToLower()=="/install")

{

try

{


TransactedInstaller ti= new TransactedInstaller();

MyInstaller mi = new MyInstaller();

ti.Installers.Add(mi);

String path=String.Format("/assemblypath={0}",

System.Reflection.Assembly.GetExecutingAssembly().Location);

String[] cmdline={path};

InstallContext ctx = new InstallContext("",cmdline);

ti.Context =ctx;

ti.Install(new Hashtable());

}

catch(System.Exception exp)

{

Console.WriteLine(exp);

}

}



// uninstall the service if user entered "ServiceMonitor /uninstall"

else if (opt !=null && opt.ToLower()=="/uninstall")

{

try

{

TransactedInstaller ti=new TransactedInstaller();

MyInstaller mi=new MyInstaller();

ti.Installers.Add(mi);

String path = String.Format("/assemblypath={0}",

System.Reflection.Assembly.GetExecutingAssembly().Location);

String[] cmdline={path};

InstallContext ctx = new InstallContext("",cmdline);

ti.Context=ctx;

ti.Uninstall(null);

}

catch(System.Exception exp)

{

Console.WriteLine(exp);

}



}


if(opt==null) // e.g. ,nothing on the command line

{

System.ServiceProcess.ServiceBase[] ServicesToRun;



ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyService() };


System.ServiceProcess.ServiceBase.Run(ServicesToRun);

}


}