Friday, 6 November 2009

PDF and PHP, Generate PDFs with PHP

There is a lot of PHP code available to generate PDF but most of it requires downloading and installing php extensions. In a shared hosting environment you don't always have access to php configuration, which is why ezPDF is nice. ezPDF is open source PDF generation PHP. See below for an example of 'on the fly' pdf generation, using ezPDF.

include_once ('class.ezpdf.php');
//ezpdf: from http://www.ros.co.nz/pdf/?
//docs: http://www.ros.co.nz/pdf/readme.pdf
//note: xy origin is at the bottom left

//data
$colw = array( 80 , 40, 220, 80, 40 );//column widths
$rows = array(
array('company','size','desc','cost','instock'),
array("WD", "80GB","WD800AAJS SATA2 7200rpm 8mb" ,"$36.90","Y"),
array("WD","160GB","WD1600AAJS SATA300 8mb 7200rpm" ,"$39.87","Y"),
array("WD", "80GB","800jd SATA2 7200rpm 8mb" ,"$41.90","Y"),
array("WD","250GB","WD2500AAKS SATA300 16mb 7200rpm" ,"$49.88","Y"),
array("WD","320GB","WD3200AAKS SATA300 16mb 7200rpm" ,"$49.90","Y"),
array("WD","160GB","1600YS SATA raid 16mb 7200rpm" ,"$59.90","Y"),
array("WD","500GB","500gb WD5000AAKS SATA2 16mb 7200rpm","$64.90","Y"),
array("WD","250GB","2500ys SATA raid 7200rpm 16mb" ,"$69.90","Y"),
);

//x is 0-600, y is 0-780 (origin is at bottom left corner)
$pdf =& new Cezpdf('LETTER');

$image = imagecreatefrompng("background.png");
$pdf->addImage($image,0,0,611);

$pdf->selectFont("fonts/Helvetica.afm");
$pdf->setColor(0/255,0/255,0/255);
$pdf->addText(80,620,10,"List of Hard Drives");

$pdf->setLineStyle(0.5);
$pdf->line(80,615,540,615);
$pdf->setStrokeColor(0,0,0);

$pdf->setColor(0/255,0/255,0/255);
$pdf->addText(30,16,8,"Created ".date("m/d/Y"));

$total=0;
$curr_x=80;
$curr_y=600;
foreach($rows as $r)
{
$xoffset = $curr_x;
foreach($r as $i=>$data)
{
$pdf->setColor(0/255,0/255,0/255);
$pdf->addText( $xoffset, $curr_y , 10, $data );
$xoffset+=$colw[$i];
}
$curr_y-=20;
}

$pdf->ezStream();
?>


Demo:
Download Demo PDF (dynamically generated with php)
Download all-in-one example.zip 349KB (includes ezPDF)

Notes:
* The above code generates a 1 page pdf, but a multi-page PDF is possible using the $pdf->ezNewPage() function. Any $pdf->addText() or $pdf->addImage() calls after that point are added to the new page.
* The size of a LETTER page in ezPDF is 612x792 (w by h in pixels)
* The (x,y) origin (0,0) is at the bottom left of a PDF page

Further Reading
* ezPDF and
* ezPDF API Docs[pdf]
* pdf-php at sourceforge

Thursday, 5 November 2009

Nepali Date, Get Nepali Date, Today's Nepali Date

You can Get Nepali Date to your site from us;
Nepali Date Sample
(no need to update daily, it will be auto update)

For that Please Contact at :
Email: info@grabhost.net
visit: www.date.grabhost.net

Thank You !!!

Friday, 30 October 2009

Regular Expression (Validation) Examples in PHP

The Regular Expressions quickly covers the basics of regular-expression syntax, then delves into the mechanics of expression-processing, common pitfalls, performance issues, and implementation-specific differences. Written in an engaging style and sprinkled with solutions to complex real-world problems, MRE offers a wealth information that you use. I will start with some simple usage examples of the regular expressions and continue with a huge list of cases for various situations where we would normally need a regex to operate. We will use simple functions which return TRUE or FALSE. $regex will serve as our regular expression to match against and $text will be our text (pretty obvious):
function do_reg($text, $regex)
{
 if (preg_match($regex, $text)) {
  return TRUE;
 }
 else {
  return FALSE;
 }
}
The next function will get the part of a given string ($text) matched by the regex ($regex) using a group srorage ($regs). By changing the $regs[0] to $regs[1] we can use a capturing group (in this case griup 1) to match against. The capturing group can also have a name ($regs['groupname']):
function do_reg($text, $regex, $regs)
{
 if (preg_match($regex, $text, $regs)) {
  $result = $regs[0];
 }
 else {
  $result = "";
 }
 return $result;
}  

The following function will return an array of all regex matches in a given string ($text):
function do_reg($text, $regex)
{
 preg_match_all($regex, $text, $result, PREG_PATTERN_ORDER);
 return $result = $result[0];
}

Next we can iterate (loop) over all matches in a string ($text) and output the results:
function do_reg($text, $regex, $regs)
{
 preg_match_all($regex, $text, $result, PREG_PATTERN_ORDER);
  for ($i = 0; $i < count($result[0]); $i++) {
  $result[0][$i];
 }
}  

Extending the above one we can iterate over all matches ($text) and capture groups in a string ($text):
function do_reg($text, $regex)
{
 preg_match_all($regex, $text, $result, PREG_SET_ORDER);
 for ($matchi = 0; $matchi < count($result); $matchi++) {
  for ($backrefi = 0; $backrefi < count($result[$matchi]); $backrefi++) {
   $result[$matchi][$backrefi];
  }
 }
} 

EXAMPLES:
For Date

$date_regex = "/^(0[1-9]|[12][0-9]|3[01])[\-\/\.](0[1-9]|1[012])[\-\/\.](19|20)[\d][\d]$/";
$date_regex = "/^(0[1-9]|[12][0-9]|3[01])[\-\/.](0[1-9]|1[012])[\-\/.](19|20)[\d][\d]$/";


//Date d/m/yy and dd/mm/yyyy
//1/1/00 through 31/12/99 and 01/01/1900 through 31/12/2099
//Matches invalid dates such as February 31st
'\b(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)?[0-9]{2}\b'

//Date dd/mm/yyyy
//01/01/1900 through 31/12/2099
//Matches invalid dates such as February 31st
'(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)[0-9]{2}'

//Date m/d/y and mm/dd/yyyy
//1/1/99 through 12/31/99 and 01/01/1900 through 12/31/2099
//Matches invalid dates such as February 31st
//Accepts dashes, spaces, forward slashes and dots as date separators
'\b(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2}\b'

//Date mm/dd/yyyy
//01/01/1900 through 12/31/2099
//Matches invalid dates such as February 31st
'(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2}'

//Date yy-m-d or yyyy-mm-dd
//00-1-1 through 99-12-31 and 1900-01-01 through 2099-12-31
//Matches invalid dates such as February 31st
'\b(19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])\b'

//Date yyyy-mm-dd
//1900-01-01 through 2099-12-31
//Matches invalid dates such as February 31st
'(19|20)[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])' 
/-------------------------------------------------------------------------------/ For Address /-------------------------------------------------------------------------------/
//Address: State code (US)
'/\\b(?:A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])\\b/'

//Address: ZIP code (US)
'\b[0-9]{5}(?:-[0-9]{4})?\b' 
/-------------------------------------------------------------------------------/ For Email /-------------------------------------------------------------------------------/
//Email address
//Use this version to seek out email addresses in random documents and texts.
//Does not match email addresses using an IP address instead of a domain name.
//Does not match email addresses on new-fangled top-level domains with more than 4 letters such as .museum.
//Including these increases the risk of false positives when applying the regex to random documents.
'\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b'

//Email address (anchored)
//Use this anchored version to check if a valid email address was entered.
//Does not match email addresses using an IP address instead of a domain name.
//Does not match email addresses on new-fangled top-level domains with more than 4 letters such as .museum.
//Requires the "case insensitive" option to be ON.
'^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$'

//Email address (anchored; no consecutive dots)
//Use this anchored version to check if a valid email address was entered.
//Improves on the original email address regex by excluding addresses with consecutive dots such as john@aol...com
//Does not match email addresses using an IP address instead of a domain name.
//Does not match email addresses on new-fangled top-level domains with more than 4 letters such as .museum.
//Including these increases the risk of false positives when applying the regex to random documents.
'^[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$'

//Email address (no consecutive dots)
//Use this version to seek out email addresses in random documents and texts.
//Improves on the original email address regex by excluding addresses with consecutive dots such as john@aol...com
//Does not match email addresses using an IP address instead of a domain name.
//Does not match email addresses on new-fangled top-level domains with more than 4 letters such as .museum.
//Including these increases the risk of false positives when applying the regex to random documents.
'\b[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b'

//Email address (specific TLDs)
//Does not match email addresses using an IP address instead of a domain name.
//Matches all country code top level domains, and specific common top level domains.
'^[A-Z0-9._%-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|biz|info|name|aero|biz|info|jobs|museum|name)$'

//Email address: Replace with HTML link
'\b(?:mailto:)?([A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4})\b' 
/-------------------------------------------------------------------------------/ For Credit Cards /-------------------------------------------------------------------------------/
//Credit card: All major cards
'^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$'

//Credit card: American Express
'^3[47][0-9]{13}$'

//Credit card: Diners Club
'^3(?:0[0-5]|[68][0-9])[0-9]{11}$'

//Credit card: Discover
'^6011[0-9]{12}$'

//Credit card: MasterCard
'^5[1-5][0-9]{14}$'

//Credit card: Visa
'^4[0-9]{12}(?:[0-9]{3})?$'

//Credit card: remove non-digits
'/[^0-9]+/'

//Delimiters: Replace commas with tabs
//Replaces commas with tabs, except for commas inside double-quoted strings
'((?:"[^",]*+")|[^,]++)*+,'

/-------------------------------------------------------------------------------/ For HTML /-------------------------------------------------------------------------------/
//HTML comment
''

//HTML file
//Matches a complete HTML file. Place round brackets around the .*? parts you want to extract from the file.
//Performance will be terrible on HTML files that miss some of the tags
//(and thus won't be matched by this regular expression). Use the atomic version instead when your search
//includes such files (the atomic version will also fail invalid files, but much faster).
'.*?.*?.*?.*?]*>.*?.*?'

//HTML file (atomic)
//Matches a complete HTML file. Place round brackets around the .*? parts you want to extract from the file.
//Atomic grouping maintains the regular expression's performance on invalid HTML files.
'(?>.*?)(?>.*?)(?>.*?)(?>.*?]*>)(?>.*?).*?'

//HTML tag
//Matches the opening and closing pair of whichever HTML tag comes next.
//The name of the tag is stored into the first capturing group.
//The text between the tags is stored into the second capturing group.
'<([A-Z][A-Z0-9]*)[^>]*>(.*?)'

//HTML tag
//Matches the opening and closing pair of a specific HTML tag.
//Anything between the tags is stored into the first capturing group.
//Does NOT properly match tags nested inside themselves.
'<%TAG%[^>]*>(.*?)'

//HTML tag
//Matches any opening or closing HTML tag, without its contents.
']*>' 

Thursday, 16 July 2009

Brute Force Protection

While trying to browse the site, if you get this
--------------------------------------------------------------------------------------------
The requested URL /suspended.page/ was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
------------------------------------------------------------------------------------------
It could be due to due to Brute force attempt was detected.

Brute Force Protection
This account is currently locked out because a brute force attempt was detected.
Please wait 10 minutes and try again. Attempting to login again will only increase this delay.
If you frequently experience this problem, we recommend having your username changed to something less generic.

Solution / Fix

Account Locks Out Due to Brute Force Protection in cPanel WebHost Manager (WHM)


Occasionally, when user or website administrator attempts to login to cPanel’s WebHost Manager (WHM), or remote or local log in via Telnet or SSH to Linux console to the web server, the login is denied and not allowed. The following error message may appear.

This account is currently locked out because a brute force attempt was detected. Please wait 10 minutes and try again. Attempting to login again will only increase this delay. If you frequently experience this problem, we recommend having your username changed to something less generic.


The brute force protection on cPanel-powerd web host is provided by cPHulk, which prevents malicious forces from trying to access the server’s services by guessing the login password for that service. When an account on the system has experienced too many failed login attempts, the particular account will automatically been “protected” by forbidding further login attempts, including all-important root account. cPHulk Brute Force Protection will also block out an IP address which has been detected to send too many unauthorized logon attempts.

As a result, server’s owner are potentially been locked out of the server if the cPHulkd is enabled, even the wild-guessing brute force hacking is done by hackers in another corner of the world.

When WHM locks out an user account, especially “root”, the best way is to wait for 10 minutes to see if the account will be unlocked. If the locks persists, webmaster and administrator who still can remote login via SSH to the server as root can manually remove the lockouts via following steps:

  1. Type mysql at console to access MySQL client.
  2. At MySQL client prompt, enter the following commands (preceding with mysql>)one after one, pressing Enter each time:

    mysql> use cphulkd;

    Expected result: Database changed.

    mysql> BACKUP TABLE `brutes` TO ‘/path/to/backup/directory’;
    mysql> BACKUP TABLE `logins` TO ‘/path/to/backup/directory’;

    Above command will backup the brutes table, the main table used by cPHulk to record locked accounts and denied IP addresses.

    mysql> DELETE FROM `brutes`;
    mysql> DELETE FROM `logins`;

    Above commands will remove all blocked IP addresses and locked accounts from the system, enabling full access again. If you’re familiar with SQL statements, it’s possible to use WHERE clause to specify logins or IP address that you want to remove only.

    mysql> quit;

    Exit MySQL client.

If you can’t login to the server due to brute force protection, you probably have to contact web hosting service provider support to physically access the server to remove the Brute Force Protection. To avoid future blockage or lock out, it’s recommended to add own IP address as Trusted Hosts List whitelist in cPHulk Brute Force Protection. To do so, go to WHM -> Security -> Security Center -> cPHulk Brute Force Protection. Inside “Configure cPHulk”, click Trusted Hosts List link.

Sunday, 28 June 2009

School Leaving Certificate (SLC) examination result 2065/2066 (2009 AD) has been just published by today...

School Leaving Certificate (SLC) examination result 2065/2066 (2009 AD) has been just published by today...

For details information, please do LogOn to http://www.nepalipathshala.com,

Best of luck for better result to you !!!

Thanks !!!

Tuesday, 9 June 2009

JQuery Fancy Box AutoClose, Auto Close Fancy Box, Auto Closing of Fancy Box iframe

Sample Code for Auto Close of Fancy Box ( JQuery Fancy Box AutoClose )

1. The parent page(file) from where the Fancy Box is called as;

-----parent.html---------
===================================
< type="text/javascript">
$().ready(function(){
'hideOnContentClick': true,'callbackOnShow': autoClose });
$("a.uploadVideo").fancybox({'frameWidth': 400, 'frameHeight':160});
});

< /script>

< type="text/javascript" src="http://localhost/test/jscripts/jquery.js">< /script>
< type="text/javascript" src="http://localhost/test/jscripts/jquery.fancybox/jquery.fancybox-1.2.1.pack.js">< /script>

<>
function triggerClose(){
var el = $("#fancyCloseId");
el.bind("click", $.fn.fancybox.close);
el.trigger('click');
}

function autoClose(){
setTimeout("triggerClose()",1000);
}
< /script>


<>
< class="iframe uploadVideo" href="iframepage.html?ie=UTF-8&aid=video">Video< /a>

< id="fancyCloseId">< /div < /body >


2. The Child ( or Light box) page included the following code.

===========================================
iframepage.html page
-----------------------

< type="text/javascript" src="http://localhost/test/jscripts/jquery.js">< /script>
< type="text/javascript" src="http://localhost/test/jscripts/jquery.fancybox/jquery.fancybox-1.2.1.pack.js">< /script>

<>
...........message here........
< /body>

$(function(){
if(typeof(parent.autoClose)=="function"){
parent.autoClose();
}
else {alert("Function not found")};
});
< / script >

Use the following code for JQuery Fancy Box Auto Close(Auto Close Fancy Box).

Thursday, 4 June 2009

Email Validation in PHP using Regular Experession


if((!ereg("[a-zA-Z0-9]+[\w{0}|\.|\-|_]+@[a-zA-Z0-9]+\.[a-zA-z]{2,4}$", $ email))
{
$email_error = 'set error message';
}