############################################################################
Function to Convert any HTML file into PDF
Can save into a pdf file on server, or stream to the user's browser
If you want to stream to the browser: $objClass->fnURL2PDF("http://www.yahoo.com", "pdf.pdf");
If you want to save on the server: $objClass->fnURL2PDF("http://www.yahoo.com", "", "../folder/filename.pdf");
Usage: $objClass->fnURL2PDF("http://www.yahoo.com", "pdf.pdf"); Default is USA
############################################################################
function fnURL2PDF($varURL, $varFileName = "pdf.pdf", $varSavePath = "")
{
$varSocket = fsockopen("www.easysw.com", 80, $errno, $errstr, 1000);
if (!$varSocket) die("$errstr ($errno)\n");
fwrite($varSocket, "GET /htmldoc/pdf-o-matic.php?URL=" . $varURL . "&FORMAT=.pdf HTTP/1.0\r\n");
fwrite($varSocket, "Host: www.easysw.com\r\n");
fwrite($varSocket, "Referer: http://www.easysw.com/htmldoc/pdf-o-matic.php\r\n");
fwrite($varSocket, "\r\n");
$varHeaders = "";
while ($varStr = trim(fgets($varSocket, 4096)))
$varHeaders .= "$varStr\n";
$varBody = "";
while (!feof($varSocket))
$varBody .= fgets($varSocket, 4096);
if ($varSavePath != '')
{
// Save the File
$varFileHandle = @fopen($varSavePath,'w');
$varBytes = @fwrite($varFileHandle, $varBody);
}
else
{
//Download file
if(isset($HTTP_SERVER_VARS['HTTP_USER_AGENT']) and strpos($HTTP_SERVER_VARS['HTTP_USER_AGENT'],'MSIE'))
Header('Content-Type: application/force-download');
else
Header('Content-Type: application/octet-stream');
if(headers_sent())
die('Some data has already been output to browser, can\'t send PDF file');
Header('Content-Length: '.strlen($varBody));
Header('Content-disposition: attachment; filename='.$varFileName);
echo $varBody;
}
return(true);
}
Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.
Thursday, June 28, 2007
PHP-Get a Value from XML Settings file
############################################################################
Function to Get a Value from XML Settings file provided as a second argument
Default is ../main/settings.xml: standard for all of our current projects
Usage: $objClass->fnGetProperty("SiteURL", "settings.xml");
############################################################################
function fnGetProperty($varPropName, $varXMLFile = '../main/settings.xml')
{
$objXML = new clsXML($varXMLFile);
$varValue = $objXML->evaluate("//" . $varPropName);
$varRetValue = $objXML->get_content($varValue[0]);
$objXML = NULL;
return($varRetValue);
}
Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.
Function to Get a Value from XML Settings file provided as a second argument
Default is ../main/settings.xml: standard for all of our current projects
Usage: $objClass->fnGetProperty("SiteURL", "settings.xml");
############################################################################
function fnGetProperty($varPropName, $varXMLFile = '../main/settings.xml')
{
$objXML = new clsXML($varXMLFile);
$varValue = $objXML->evaluate("//" . $varPropName);
$varRetValue = $objXML->get_content($varValue[0]);
$objXML = NULL;
return($varRetValue);
}
Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.
PHP-Generate Random Password
############################################################
Function to Generate Random Password of length n, Default = 6 characters
Usage: $objClass->fnRandomPasswordGenerator(16);
###########################################################
function fnRandomPasswordGenerator($length = 6)
{
$pass = '';
$new = '';
// all the chars we want to use
$all = explode(" ",
"a b c d e f g h i j k l m n o p q r s t u v w x y z "
."A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
."0 1 2 3 4 5 6 7 8 9");
for($i=0; $i < $length; $i++)
{
srand((double)microtime()*1000000);
$pass .= $all[rand(0,61)];
$arr[] = $all[rand(0,61)];
$new .= $arr[$i];
}
return $new;
}
Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.
Function to Generate Random Password of length n, Default = 6 characters
Usage: $objClass->fnRandomPasswordGenerator(16);
###########################################################
function fnRandomPasswordGenerator($length = 6)
{
$pass = '';
$new = '';
// all the chars we want to use
$all = explode(" ",
"a b c d e f g h i j k l m n o p q r s t u v w x y z "
."A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
."0 1 2 3 4 5 6 7 8 9");
for($i=0; $i < $length; $i++)
{
srand((double)microtime()*1000000);
$pass .= $all[rand(0,61)];
$arr[] = $all[rand(0,61)];
$new .= $arr[$i];
}
return $new;
}
Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.
PHP- File Attachment
$fileatt = "file.txt"; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = "Report"; // Filename that will be used for the file as the attachment
$email_from = "test@hotmail.com"; // Who the email is from
$email_subject = "attachment"; // The Subject of the email
$email_txt = "attachment"; // Message that the email has in it
$email_to = "test@hotmail.com"; // Who the email is too
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
$ok = @mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
echo "The file was successfully sent!";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
?>
Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = "Report"; // Filename that will be used for the file as the attachment
$email_from = "test@hotmail.com"; // Who the email is from
$email_subject = "attachment"; // The Subject of the email
$email_txt = "attachment"; // Message that the email has in it
$email_to = "test@hotmail.com"; // Who the email is too
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
$ok = @mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
echo "The file was successfully sent!";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
?>
Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.
PHP- Calculate Elapsed time (in seconds!)
function calcElapsedTime($time)
{
$diff = time()-$time;
$yearsDiff = floor($diff/60/60/24/365);
$diff -= $yearsDiff*60*60*24*365;
$monthsDiff = floor($diff/60/60/24/30);
$diff -= $monthsDiff*60*60*24*30;
$weeksDiff = floor($diff/60/60/24/7);
$diff -= $weeksDiff*60*60*24*7;
$daysDiff = floor($diff/60/60/24);
$diff -= $daysDiff*60*60*24;
$hrsDiff = floor($diff/60/60);
$diff -= $hrsDiff*60*60;
$minsDiff = floor($diff/60);
$diff -= $minsDiff*60;
$secsDiff = $diff;
return (''.$yearsDiff.' year'.(($yearsDiff <> 1) ? "s" : "").', '.$monthsDiff.' month'.(($monthsDiff <> 1) ? "s" : "").', '.$weeksDiff.' week'.(($weeksDiff <> 1) ? "s" : "").', '.$daysDiff.' day'.(($daysDiff <> 1) ? "s" : "").', '.$hrsDiff.' hour'.(($hrsDiff <> 1) ? "s" : "").', '.$minsDiff.' minute'.(($minsDiff <> 1) ? "s" : "").', '.$secsDiff.' second'.(($secsDiff <> 1) ? "s" : "").'');
}
Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.
{
$diff = time()-$time;
$yearsDiff = floor($diff/60/60/24/365);
$diff -= $yearsDiff*60*60*24*365;
$monthsDiff = floor($diff/60/60/24/30);
$diff -= $monthsDiff*60*60*24*30;
$weeksDiff = floor($diff/60/60/24/7);
$diff -= $weeksDiff*60*60*24*7;
$daysDiff = floor($diff/60/60/24);
$diff -= $daysDiff*60*60*24;
$hrsDiff = floor($diff/60/60);
$diff -= $hrsDiff*60*60;
$minsDiff = floor($diff/60);
$diff -= $minsDiff*60;
$secsDiff = $diff;
return (''.$yearsDiff.' year'.(($yearsDiff <> 1) ? "s" : "").', '.$monthsDiff.' month'.(($monthsDiff <> 1) ? "s" : "").', '.$weeksDiff.' week'.(($weeksDiff <> 1) ? "s" : "").', '.$daysDiff.' day'.(($daysDiff <> 1) ? "s" : "").', '.$hrsDiff.' hour'.(($hrsDiff <> 1) ? "s" : "").', '.$minsDiff.' minute'.(($minsDiff <> 1) ? "s" : "").', '.$secsDiff.' second'.(($secsDiff <> 1) ? "s" : "").'');
}
Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.
PHP- Read contents of a text file
#############################################################################
# Function to Read contents of a text file
# Usage: $objClass->fnReadFromFile('filename.txt');# ############################################################################
function fnReadFromFile($sFileName)
{
$sReturn = '';
$fHandle = fopen($sFileName, "r");
while (!feof($fHandle))
$sReturn .= fread($fHandle, 4096);
fclose($fHandle);
return($sReturn);
}
Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.
# Function to Read contents of a text file
# Usage: $objClass->fnReadFromFile('filename.txt');# ############################################################################
function fnReadFromFile($sFileName)
{
$sReturn = '';
$fHandle = fopen($sFileName, "r");
while (!feof($fHandle))
$sReturn .= fread($fHandle, 4096);
fclose($fHandle);
return($sReturn);
}
Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.
PHP-A Unique session id
function MakeSessionId() {
$day = date('d', time());
$month = date('m', time());
$year = date('Y', time());
$hour = date('H', time());
$min = date('i', time());
$sec = date('s', time());
return sprintf("%02d%04d%02d-%02d%02d%04d-%04d-%02d%04d", $sec, rand(0, 9999),
$hour, $month, $min, rand(0, 9999), rand(0, 9999), $day, $year);
}
?>
Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.
Subscribe to:
Posts (Atom)