Friday, November 29, 2013

How to use TpFIBDataSet, TpFIBQuery and TpFIBTransaction FIBPlus components to connect with Firebird / Interebase database in Delphi XE4?

How to use TpFIBDataSet, TpFIBQuery and TpFIBTransaction FIBPlus components to connect with Firebird / Interebase database in Delphi XE4?

Following is the basic article on Firebird / Interbase database connectivity in Delphi XE4 using FIBPlus database components like TpFIBDataSet, TpFIBQuery and TpFIBTransaction. I will explain all these FIBPlus database components in detail. I have written a small article on TpFIBDatabase before this article. Please go through that before reading this one. Read FIBPlus TpFIBDatabase...

FIBPlus TpFIBQuery Component

An application works with a database by issuing SQL instructions. They are used to get and modify data\metadata. FIBPlus has a special TpFIBQuery component responsible for SQL operator execution. This robust, light and powerful component can perform any actions with the database. 

TpFIBQuery is very easy-to-use: just set the TpFIBDatabase component, fill in the SQL property and call any ExecQuery method (ExecQueryWP, ExecQueryWPS). 

NOTE: The tpFIBQuery is not a TDataset descendant, so it does not act in exactly the same way or exhibit the same methods / properties as you would expect to find in a dataset. 

The example below will show how to create TpFIBQuery dynamically at run-time and thus get data about clients.

 var sql: TpFIBQuery;
 sql := TpFIBQuery.Create(nil);
 with sql do
 try
 Database := db;
 Transaction := db.DefaultTransaction;
 SQL.Text := 'select first_name, last_name from customer';
 ExecQuery;
 while not Eof do begin
 Memo1.Lines.Add(
 FldByName['FIRST_NAME'].AsString+' '+
 FldByName['LASTST_NAME'].AsString);
 Next; end;
 sql.Close;
 finally
 sql.Free;
 end;

FIBPlus TpFIBDataSet component

The TpFIBDataSet component is responsible for work with datasets. It is based on the TpFIBQuery component and helps to cache selection results. TpFIBDataSet is a TDataSet descendant so it supports all TDataSet properties, events and methods.

TpFIBDataSet enables you to select, insert, update and delete data. All these operations are executed by TpFIBQuery components in TpFIBDataSet. 

To select data you set the SelectSQL property. It’s similar to setting the SQL property of the QSelect component (TpFIBQuery type). Define the InsertSQL.Text property to insert data, UpdateSQL.Text to update, DeleteSQL.Text to delete and RefreshSQL.Text to refresh the data. 

Here is a demo database employee.gdb (or .fdb for Firebird) to show how to write Select SQL and get a list of all employees. I will write all queries in InsertSQL, UpdateSQL, etc.

with pFIBDataSet1 do begin
 if Active then Close;

 SelectSQL.Text := 'select CUST_NO, CUSTOMER, CONTACT_FIRST, CONTACT_LAST from CUSTOMER';

 InsertSQL.Text := 'insert into CUSTOMER(CUST_NO, CUSTOMER, CONTACT_FIRST,                                           CONTACT_LAST )' + 
                              ' values (:CUST_NO, :CUSTOMER, :CONTACT_FIRST, :CONTACT_LAST)';

 UpdateSQL.Text := 'update CUSTOMER set CUSTOMER = :CUSTOMER, '+
                   'CONTACT_FIRST = :CONTACT_FIRST, CONTACT_LAST = :CONTACT_LAST '+
                   'where CUST_NO = :CUST_NO';

 DeleteSQL.Text := 'delete from CUSTOMER where CUST_NO = :CUST_NO';

 RefreshSQL.Text := 'select CUST_NO, CUSTOMER, CONTACT_FIRST, CONTACT_LAST '                                       + 'from CUSTOMER where CUST_NO = :CUST_NO';

 Open;
end;

To open TpFIBDataSet either execute Open/OpenWP methods or set the Active property to True. To close TpFIBDataSet call the Close method

FIBPlus TpFIBTransaction component

A transaction is an operation of database transfer from one consistent state to another. All operations with the dataset (data/metadata changes) are done in the context of a transaction. To understand special FIBPlus features completely you need to know about InterBase / FIBPlus transactions. 

All the changes done in the transaction can be either committed (in case there are no errors) by Commit or rolled back (Rollback). Besides these basic methods TpFIBTransaction has their context saving analogues: CommitRetaining and RollbackRetaining, i.e. on the client side, these will not close a TpFibQuery or TpFibDataset.

To start the transaction you should call the StartTransaction method or set the Active property to True. To commit the transaction call Commit/CommitRetaing, to roll it back - Rollback/RollbackRetaining. 

TpFIBQuery and TpFIBDataSet components have some properties which help to control transactions automatically. In particular they are: the TpFIBDataSet.AutoCommit property; the poStartTransaction parameter in TpFIBDataSet.Options; qoStartTransaction and qoCommitTransaction in TpFIBQuery.Options.

TpFIBTransaction has three basic transaction types: 
tpbDefault, 
tpbReadCommited, 
tpbRepeatableRead. 

At design time you can also create special types of your own in the TpFIBTransaction editor and use them as internal ones. Set the transaction type to set its 
parameters:

TpbDefault – parameters must be set in TRParams
tbpReadCommited – shows the ReadCommited isolation level
tbpRepeatableRead – shows the RepeatableRead isolation level 

How to use TpFIBDatabase FIBPlus Component to connect with Firebird database in Delphi XE4?

How to use TpFIBDatabase FIBPlus Component to connect with Firebird database in Delphi XE4?

TpFIBDatabase component is used to make database connectivity with Firebird database in Delphi. For using TpFIBDatabase component, you should have FIBPlus and Firebird installed on your system. I am using Delphi XE4, Firebird 2.5.2 and FIBPlus 7.5 to make database connection.

Connection parameters are typical for InterBase/Firebird server:

1) path to a database file;
2) user name and password;
3) user role;
4) charset;
5) dialect;
6) client library (gds32.dll for InterBase and fbclient.dll for Firebird).

To connect to a database you should call the Open method or set the Connected property to True. It’s also possible to use this code to connect to a database:

function Login(DataBase: TpFIBDatabase; dbpath, uname, upass, urole: string): Boolean;
begin
 if DataBase.Connected then DataBase.Connected := False; 
 with FDataBase.ConnectParams do begin
   UserName := uname;
   Password := upass;
   RoleName := urole;
 end;
 DataBase.DBName := dbpath;
 try DataBase.Connected := True;
 except
   on e: Exception do
   ShowMessage(e.Message);
 end;
 Result := DataBase.Connected;
end;

To close the connection either call the Close method or set the Connected property to False. 

You can also close all datasets and connected transactions at once:

procedure Logout(DataBase: TpFIBDatabase);
var i: Integer;
begin
  if not DataBase.Connected then
  Exit;

  for i := 0 to DataBase.TransactionCount - 1 do
    if TpFIBTransaction(DataBase.Transactions[i]).InTransaction then 
      TpFIBTransaction(DataBase.Transactions[i]).Rollback
 DataBase.CloseDataSets;
 DataBase.Close;
end;

Wednesday, November 27, 2013

HTML5 MENU Tag Redefined

HTML5 <menu> Tag Redefined

The <menu> tag provides an easy way to create menus on a web page. The HTML <menu> element was deprecated in HTML 4.01 but is redefined in HTML5. The HTML <menu> tag defines a list/menu of commands. The <menu> tag is used for context menus, toolbars and for listing form controls and commands.

<li> tag is used within the <menu> tag. For example:

Which pet do you own?
<menu>
 <li>Dog</li>
 <li>Cat</li>
</menu>

You can also place radio buttons and check boxes inside <menu> tags. Have a look at following simple example of <menu> tag

radiobuttons inside <menu> tag

<menu>
   <li><input type="radio" id="radDog" class="radPets" name="radDog"/>Dog</li>
   <li><input type="radio" id="radCat" class="radPets" name="radCat"/>Cat</li>
</menu>

checkboxes inside <menu> tag

<menu>
   <li><input type="checkbox" id="cbDog" class="cbPets" name="cbDog"/>Dog</li>
   <li><input type="checkbox" id="cbCat" class="cbPets" name="cbCat"/>Cat</li>
</menu>

<menu> tag is used instead of <ul> tag

Earlier we used to make an unordered list by using <ul> tag like this:

<ul class="toolbar">
  <li>New</li>
  <li>Open</li>
  <li>Save</li>
  <li>Quit</li>
</ul>

But as I earlier said, <menu> is redefined in HTML5. The <menu> element represents an unordered list of commands. It has a type attribute, which can be set to popup or toolbar.

<menu type="toolbar">
  <li>New</li>
  <li>Open</li>
  <li>Save</li>
  <li>Quit</li>
</menu>

Please note: The <menu> tag is valid within blockquote, body, button, center, dd, div, fieldset, form, iframe, li, noframes, noscript, object, td and th tags.

Saturday, November 23, 2013

How to secure jQuery AJAX calls in PHP from hackers?

How to secure jQuery AJAX calls in PHP from hackers?

If you are making jQuery AJAX calls in your PHP website, please ensure that those jQuery AJAX calls are secure from website hackers. Your code should not be vulnerable to hackers. Below are some methods and steps which need to be taken to secure your jQuery AJAX calls to PHP files. I am writing this post because I had written a simple post "How to call PHP function from JavaScript function? Always use AJAX." without mentioning any security code. I got following comment on that post:

"Your code is very vulnerable. You're not filtering the $_POST variable at all. This opens yourself to HTML injection. A hacker could pwn your web site very quickly if you used this code. Careless examples like yours is exactly why so many web sites are hacked."

That's why this is my small attempt to make your jQuery AJAX calls secure. 

1. Use $_SERVER['HTTP_X_REQUESTED_WITH']

This is a basic check to see if the request is an Ajax request or not?

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&       strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') 
{
    //Request identified as ajax request
}

However you should never base your security on this check. It will eliminate direct accesses to the page if that is what you need.

2. Use $_SERVER['HTTP_REFERER']

if(@isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']=="http://yourdomain/ajaxurl")
{
 //Request identified as ajax request
}

But not all browsers set it. So don't properly rely on it but yes, to some extent it can secure your webpage.

Nobody can AJAX your site from other domain, but always can connect and drieclty send http request, for example by cURL.

JavaScript running on another domain cannot access any page on your domain because this is a violation of the Same-Origin Policy. The attacker would need to exploit an XSS vulnerability in order to pull this off. In short you don't need to worry about this specific attack, just the same old attacks that affect every web application.

3. Generate Access Tokens

$token = md5(rand(1000,9999)); //you can use any encryption
$_SESSION['token'] = $token; //store it as session variable

You can create some token in cookies, that will be also seen from jquery request, but that solution can also be hacked.

4. Always check $_POST variables in your PHP file whether those are set or not? Whether there is valid value in $_POST or not before executing the actual PHP code.

Basic code snippet for securing your jQuery AJAX calls in PHP

Step-1 : Generate Token System For All Web-Service:

Generating Token :

<?php
  session_start();
  $token = md5(rand(1000,9999)); //you can use any encryption
  $_SESSION['token'] = $token; //store it as session variable
?>

Step-2 : Use it while sending ajax call:

var form_data = 
{
  data: $("#data").val(), //your data being sent with ajax
  token:'<?php echo $token; ?>', //used token here.
  is_ajax: 1
};

$.ajax({
  type: "POST",
  url: 'yourajax_url_here',
  data: form_data,
  success: function(response)
  {
    //do further
  }
});

Step-3 : NOW, Let's secure ajax handler PHP file with,

session_start(); 
if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') 
{
  //Request identified as ajax request

  if(@isset($_SERVER['HTTP_REFERER']) &&    $_SERVER['HTTP_REFERER']=="http://yourdomain/ajaxurl")
  {
   //HTTP_REFERER verification
    if($_POST['token'] == $_SESSION['token']) {
      //do your ajax task
      //don't forget to use sql injection prevention here.
    }
    else
   {
      header('Location: http://yourdomain.com');
    }
  }
  else 
  {
    header('Location: http://yourdomain.com');
  }
}
else 
{
  header('Location: http://yourdomain.com');
}

Tuesday, October 30, 2012

Why Professional Email Marketing Software? Need and Importance

Why Professional Email Marketing Software? Need and Importance

Whether you want to create and send newsletters, marketing materials, customer correspondences or other documents, email marketing software will help you develop a professional and effective campaign—and, it's easy.

This software has a number of creation features that will help you build an attractive email. You can import HTML pages that you've already developed or use one of the email templates to create a new design. You can add pictures, attachments, background images and even sound files to create an impressive email.

Some of the programs will automatically include an unsubscribe/subscribe button and track the number of subscribers and those who choose to unsubscribe. Additionally, the best bulk email software offers other reports that contain information on the number of successful emails, the number of emails that bounce back, the number of opened emails and more.

What to Look for in Email Marketing Software

A good email marketing program makes it easy to setup email lists and create and send marketing emails, newsletters and other bulk emails and track the results of your email campaign. Below are the criteria TopTenREVIEWS used to evaluate email marketing software.

1. Feature Set

Newsletter software should provide a number of practical features that will help you develop and send professional email marketing campaigns.
 

2. Ease of Installation/Setup

The software should come with clear installation instructions so anyone, regardless of their knowledge of computers, can install and setup the program without errors.
 

3. Ease of Use

Bulk email software should be easy to navigate with a comprehensive interface so even the computer novice will feel comfortable and confident using the program.
 

4. Email Creation

It should be easy to create an attractive, professional and effective marketing email with the bulk email software.
 

5. Reporting

The software should produce a number of valuable reports that will provide statistics pertaining to unsubscribers, the number of emails that are opened, how many “click throughs” the emails generate and more.

Professional Bulk Email Marketing Services Software Solution

Professional Bulk Email Marketing Services Software Solution

Professional Bulk Email Marketing Services Software Solution is best for small business, medium business and large size business. It is one of the best email marketing software and ranked top in the email marketing software market. It provides best software programs for email marketing. Review of Bulk Email Marketing Software is very nice in the market.

Bulk email software is included in almost all modern bulk mail marketing strategies. Powerful, reliable and fast email marketing solution is always required by direct list managers and mail marketers. Live Software offers you free bulk mailing software. This email marketing software allows performing permission based email marketing campaigns.

The email marketing software offers you:

1. Free bulk email sending software
2. Easy install and use the email software
3. No monthly fees like email services
4. Send bulk email from your PC
5. Bulk email advertising proven technology

Email Marketing Studio

Email Marketing Studio contains free email marketing software for targeted bulk email advertising and bulk email marketing campaigns. Moreover, the software is efficient when build mailing lists at the desktop.

To be successful and effective, each email marketing campaign requires a suite of email marketing software.

Bulk Email Sender is the software that assists to perform targeted email campaigns. The program allows sending email announcements and newsletters.

1. Email messages templates
2. Email message personalization
3. Built-in database to manage lists
4. Easy editing of  HTML messages
5. Supports embedded images and flash

Email Marketing Studio includes the following software:

1. Bulk Mailer - is the best bulk email software on the market. It allows the user to send mass email just in few minutes.
2. Email Verifier - ais the email verification program that allows the user to verify mailing list with just one click.
3. Email Autoresponder - The installation of the software is simple. It allows the user to follow up autoresponders.

"Powerful features and excellent designs of our mass email software will help you to achieve best results. Beat your competitors with reliable email marketing software!"

1. Bulk Mailer

Bulk Mailer is free bulk e-mail software. It is efficiently used for targeted bulk email marketing campaigns as well as to build email lists at your desktop. Bulk Mailer is simple but all-inclusive bulk email program. It is leading bulk emailing software on the market.

2. Email Verifier

Email Verifier  is free program that allows the user to verify email addresses from Text Files, Windows Address Book, External Databases, Excel and Microsoft Outlook Contacts. Email Verifier is a powerful verification tool. The advanced software is designed for powerful users, marketers and administrators to keep their email mailing lists clean.  If you require email software to verify email addresses to determine if they are valid, use Email Verifier

3. Email Verifier Lite

Email Verifier Lite is easy to use, fast and free email program. It is intended to verify email addresses if they are valid and make mailing lists clean. It is a powerful email verification tool for those who often send email messages.

Free bulk email software for effective mass mailing! Get highly comprehensive software to manage your email marketing campaigns.

Professional Comm100 Email Marketing Services Software Solution

Professional Comm100 Email Marketing Services Software Solution

Professional Comm100 Email Marketing Services Software Solution is best for small business, medium business and large size business. It is one of the best email marketing software and ranked top in the email marketing software market. It provides best software programs for email marketing. Review of  Comm100 Email Marketing Software is very nice in the market.

Comm100 provides you with the best email marketing software that offers all the tools needed to implement successful opt-in email marketing programs. Comm100's email marketing software is the best email marketing solution for you to develop and maintain good customer relationships and increase your sales revenue at a very low cost.

Featues and Advantages of Comm100 Email Marketing Software

1. High Inbox Delivery

We maintain a superb sender reputation with all major ISPs to ensure that your emails get the best inbox delivery when sent with our email mailing software. 

2. Easy Email Creation

Many free email templates come with our opt-in email marketing solution & you can customize the free email templates to best match your own email marketing program.

3. Robust List Management

Grow mailing lists with our email mailing software by importing existing contacts & collecting new leads from your website. Easily manage & prune your mailing lists. 

4. Insightful Report & Analysis

See who opens your emails & who clicks on which links with the best email marketing software. Track your opt-in email marketing program with insightful reports.

5. Guaranteed Privacy & Security

As a licensee of TRUSTe, we are committed to protecting your information privacy in our email mailing software & will not view, share or trade your mailing lists. 

6. Personal Coaching & Service

Besides the email marketing tool, free resources are offered to help you create great opt-in email marketing programs. Dedicated customer service available.