Berner Fachhochschule - HTI
PHP Miscellaneous
- Dr. E. Benoist
Winter Term 2005-2006
PHP Miscellaneous 1
PHP Miscellaneous Dr. E. Benoist Winter Term 2005-2006 PHP - - PowerPoint PPT Presentation
Berner Fachhochschule - HTI PHP Miscellaneous Dr. E. Benoist Winter Term 2005-2006 PHP Miscellaneous 1 PHP Miscellaneous Generating PDF Documents FPDF Library Image Generation GD Library Example: Barcode Generator Authorization
PHP Miscellaneous 1
PHP Miscellaneous 2
◮ Two Libraries ◮ PDFLib: A C library that must be included at compilation
◮ fpdf: A free, PHP written, PDF library.
◮ Use HTTP authentification scheme (which open a small
◮ Create your own authentification scheme using forms.
◮ Use the GD library, a C written lib included durring the
◮ Creation of jpeg and png files (no gif support for rights
PHP Miscellaneous 3
◮ http://www.fpdf.org/ ◮ In PHP written ◮ Contains a PHP class: FPDF ◮ Needs to be copied in the directory where PHP can find it ◮ Contains also font metrics (should be in a fonts subdirectory
◮ Creates a PDF document and makes the output ◮ Insert text, images, headers and footers ◮ Defines page size (A4, letter, landscape or portrait) ◮ Change the margins ◮ . . .
PHP Miscellaneous Generating PDF Documents: FPDF Library 4
◮ Load the class ◮ Create an instance of this class $pdf=new FPDF();
◮ Create a new page $pdf->AddPage(); ◮ Write a message
◮ Change the header and write the content of the document
PHP Miscellaneous Generating PDF Documents: FPDF Library 5
PHP Miscellaneous Generating PDF Documents: FPDF Library 6
PHP Miscellaneous Generating PDF Documents: FPDF Library 7
15 //Page footer 16 function Footer() 17 { 18 //Position at 1.5 cm from bottom 19 $this−>SetY(−15); 20 $this−>SetFont(’Arial’,’I’,8); 21 $this−>Cell(0,10,’Page ’.$this−>PageNo().’/{nb}’,0,0,’C’); 22 } 23 } 24 $pdf=new PDF(); 25 $pdf−>AliasNbPages(); 26 $pdf−>AddPage(); 27 $pdf−>SetFont(’Times’,’’,12); 28 for($i=1;$i<=40;$i++) 29 $pdf−>Cell(0,10,’Printing line number ’.$i,0,1); 30 $pdf−>Output(); 31 ?>
PHP Miscellaneous Generating PDF Documents: FPDF Library 8
◮ Overwrite the Header() function, ◮ Insert an image $this->Image(’logo_pb.png’10,8,33); , ◮ Move to the right $this->Cell(80); ◮ Line break of 20mm $this->Ln(20);
◮ Overwrite the Footer() function, ◮ Position at 1.5 cm from bottom $this->SetY(-15); ◮ Includes the page number:
PHP Miscellaneous Generating PDF Documents: FPDF Library 9
◮ Draw color for the rectangle (is set if we define a width) ◮ Fill color for the inside ◮ Text color for text
PHP Miscellaneous Generating PDF Documents: FPDF Library 10
◮ width ◮ height ◮ text ◮ if borders should be drawn ◮ where the current position moves after it
◮ If the text is centered (C) or aligned ◮ Fill or not fill (boolean)
PHP Miscellaneous Generating PDF Documents: FPDF Library 11
1 //Current column 2 var $col=0; 3 //Ordinate of column start 4 var $y0; 5 function Header() 6 { 7 ... 8 $this−>y0=$this−>GetY(); 9 } 10 11 function SetCol($col) 12 { 13 //Set position at a given column 14 $this−>col=$col; 15 $x=10+$col∗65; 16 $this−>SetLeftMargin($x); 17 $this−>SetX($x); 18 }
PHP Miscellaneous Generating PDF Documents: FPDF Library 12
19 function AcceptPageBreak() { 20 //Method accepting or not automatic page break 21 if($this−>col<2) { 22 //Go to next column 23 $this−>SetCol($this−>col+1); 24 //Set ordinate to top 25 $this−>SetY($this−>y0); 26 //Keep on page 27 return false; 28 } 29 else{ 30 //Go back to first column 31 $this−>SetCol(0); 32 //Page break 33 return true; 34 } 35 } 36 //Output text in a 6 cm width column 37 $this−>MultiCell(60,5,$txt);
PHP Miscellaneous Generating PDF Documents: FPDF Library 13
PHP Miscellaneous Generating PDF Documents: FPDF Library 14
◮ Each column has its own width, ◮ Titles are centered ◮ Figures right aligned. ◮ Horizontal lines have been removed. (Left and Right LR are
PHP Miscellaneous Generating PDF Documents: FPDF Library 15
PHP Miscellaneous Generating PDF Documents: FPDF Library 16
◮ $pdf->Write(5,’Hello World’); ◮ Goes to the end of the line ◮ Current position moves at the end of the text ◮ We can mix up text styles
PHP Miscellaneous Generating PDF Documents: FPDF Library 17
◮ Creates a link and write it
◮ Where does it go:
PHP Miscellaneous Generating PDF Documents: FPDF Library 18
◮ HTTP allows programmers to generate anything as response. ◮ It is possible to generate images for Maps, Menu items, ...
◮ Use the GD Library. GD library is a C library that allows
◮ Compile the PHP module with the GD library, ◮ Write scripts that create images.
PHP Miscellaneous Image Generation: GD Library 19
PHP Miscellaneous Image Generation: GD Library 20
Content type must contain a valid mime type if (function exists(”imagegif”)) { Header(”Content−type: image/gif”); ImageGIF($im); } elseif (function exists(”imagejpeg”)) { Header(”Content−type: image/jpeg”); ImageJPEG($im, ””, 0.5); } elseif (function exists(”imagepng”)) { Header(”Content−type: image/png”); ImagePNG($im); } elseif (function exists(”imagewbmp”)) { Header(”Content−type: image/vnd.wap.wbmp”); ImageWBMP($im); } else{ die(”No image support in this PHP server”); }
PHP Miscellaneous Image Generation: GD Library 21
PHP Miscellaneous Image Generation: GD Library 22
PHP Miscellaneous Image Generation: GD Library 23
PHP Miscellaneous Image Generation: GD Library 24
PHP Miscellaneous Image Generation: GD Library 25
PHP Miscellaneous Image Generation: GD Library 26
PHP Miscellaneous Image Generation: GD Library 27
PHP Miscellaneous Image Generation: GD Library 28
◮ Must be enabled durring the compilation ◮ Requires FreeTypes fonts
PHP Miscellaneous Image Generation: GD Library 29
<?php function UPCAbarcode($code) { $lw = 2; $hi = 100; $Lencode = array(’0001101’,’0011001’,’0010011’,’0111101’,’0100011’, ’0110001’,’0101111’,’0111011’,’0110111’,’0001011’); $Rencode = array(’1110010’,’1100110’,’1101100’,’1000010’,’1011100’, ’1001110’,’1010000’,’1000100’,’1001000’,’1110100’); $ends = ’101’; $center = ’01010’; /∗ UPC−A Must be 11 digits, we compute the checksum. ∗/ if ( strlen($code) != 11 ) { die(”UPC−A Must be 11 digits.”); } /∗ Compute the EAN−13 Checksum digit ∗/ $ncode = ’0’.$code; $even = 0; $odd = 0; for ($x=0;$x<12;$x++) { if ($x % 2) { $odd += $ncode[$x]; } else { $even += $ncode[$x]; } } $code.=(10 − (($odd ∗ 3 + $even) % 10)) % 10;
PHP Miscellaneous Image Generation: Example: Barcode Generator 30
/∗ Create the bar encoding using a binary string ∗/ $bars=$ends; $bars.=$Lencode[$code[0]]; for($x=1;$x<6;$x++) { $bars.=$Lencode[$code[$x]]; } $bars.=$center; for($x=6;$x<12;$x++) { $bars.=$Rencode[$code[$x]]; } $bars.=$ends; /∗ Generate the Barcode Image ∗/ $img = ImageCreate($lw∗95+30,$hi+30); $fg = ImageColorAllocate($img, 0, 0, 0); $bg = ImageColorAllocate($img, 255, 255, 255); ImageFilledRectangle($img, 0, 0, $lw∗95+30, $hi+30, $bg); $shift=10;
PHP Miscellaneous Image Generation: Example: Barcode Generator 31
for ($x=0;$x<strlen($bars);$x++) { if (($x<10) || ($x>=45 && $x<50) || ($x >=85)) { $sh=10; } else { $sh=0; } if ($bars[$x] == ’1’) { $color = $fg; } else { $color = $bg; } ImageFilledRectangle($img, ($x∗$lw)+15,5,($x+1)∗$lw+14,$hi+5+$sh,$color); } /∗ Add the Human Readable Label ∗/ ImageString($img,4,5,$hi−5,$code[0],$fg); for ($x=0;$x<5;$x++) { ImageString($img,5,$lw∗(13+$x∗6)+15,$hi+5,$code[$x+1],$fg); ImageString($img,5,$lw∗(53+$x∗6)+15,$hi+5,$code[$x+6],$fg); } ImageString($img,4,$lw∗95+17,$hi−5,$code[11],$fg); /∗ Output the Header and Content. ∗/ header(”Content−Type: image/png”); ImagePNG($img); } UPCAbarcode(’12342678901’); ?>
PHP Miscellaneous Image Generation: Example: Barcode Generator 32
Using HTTP Authorization scheme
◮ Scripts asks for a login (401 error) ◮ Browser answers a username password pair ◮ If it is OK: login ◮ If not: 401 Error (another login prompt is sent)
<?php if(!isset($ SERVER[’PHP AUTH USER’]) || $ SERVER[’PHP AUTH USER’] !=’bie’){ header(”HTTP/1.0 401 Unauthorized”); header(’WWW−AUthenticate: Basic realm=”My Realm”’); $msg= ”You are not authorized to access to this page”; } else{ $msg = <<<MSG Hello {$ SERVER[’PHP AUTH USER’]}<br> Your password is:{$ SERVER[’PHP AUTH PW’]} MSG; } ?>
PHP Miscellaneous Authorization: Using HTTP 33
◮ Send a form ◮ if the form has been filled, test the value ◮ if the value is OK, insert username as a session value ◮ Only test session value username to check if user is loged in.
PHP Miscellaneous Authorization: Using HTML form 34
<?php session start(); $login form = <<<END <form method=”POST”> Username <input type=”text” name=”username”><br> Password <input type=”password” name=”password”> <br> <input type=”submit” value=”Logg−in”> </form><br> END; if(isset($ POST[’username’])&&isset($ POST[’password’])){ if($ POST[’username’] ==’bie’ && $ POST[’password’] == ’bie’){ $ SESSION[’username’]=$ POST[’username’]; } else{ $login form .= ”Wrong username/password couple<br>\n”; } }
PHP Miscellaneous Authorization: Using HTML form 35
if(isset($ SESSION[’username’])){ $content = ”Hello {$ SESSION[’username’]} <br>\n”; $content .= ”<a href=\”logout.php\”>logout</a><br>”; } else{ $content = $login form; } ?> <html><header><title>Login form</title></header> <body> <?php echo $content; ?> </body> </html>
PHP Miscellaneous Authorization: Using HTML form 36
PHP Miscellaneous Authorization: Using HTML form 37
◮ Separate design from programs ◮ Let two teams work independently ◮ needs to be physically separated
◮ Gratis on the net:
◮ Easy to understand ◮ Not too powerfull but easy to understand
PHP Miscellaneous Template 38
PHP Miscellaneous Template 39
◮ We define blocks
◮ We define fields to be replaced
◮ We visit the blocks ◮ And affect variables
PHP Miscellaneous Template 40
In PHP, we affect the variables <?php // include class require (’HTML/Template/IT.php’); // create object // set template directory $template = new HTML Template IT(”./”); // load template $template−>loadTemplateFile(”quiz.tpl”); // set block $template−>setCurrentBlock(”main”); // assign values $template−>setVariable(”QUESTION”, ”Knock, knock!”); $template−>setVariable(”ANSWER”, ”Who’s there?”); // parse block $template−>parseCurrentBlock(); // render page $template−>show(); ?>
PHP Miscellaneous Template 41
◮ You can use templates for recurring items (Footer, Header, Menu) ◮ You can load templates one after the other
$template = new HTML Template IT(”.”); // load template $template−>loadTemplateFile(”header.tpl”); ... $template−>parseCurrentBlock(); $template−>show(); // parse main block $template−>loadTemplateFile(”mainTemplate.tpl”); .. $template−>parseCurrentBlock(); $template−>show(); // parse footer $template−>loadTemplateFile(”footer.tpl”); ... $template−>parseCurrentBlock(); $template−>show();
PHP Miscellaneous Template 42
Define a block inside another block
◮ For tables (TD’s inside TR’s) ◮ For lists
<!−− BEGIN main −−> <html> <head><basefont face=”{FONT}”></head> <body> <!−− BEGIN content −−> <h2>{TITLE}</h2> <img src=”{IMG}” align=”center”> <br /> {DESC} <!−− END content −−> </body> </html> <!−− END main −−>
PHP Miscellaneous Template 43
◮ Visit the inner block, affect variables ◮ Then do the same for the outer block
// set values for inner block $template−>setCurrentBlock(”content”); $template−>setVariable(”TITLE”, ”Images >> Singapore”); $template−>setVariable(”IMG”, ”/images/galleries/singapore/01.jpg”); $template−>setVariable(”DESC”, ”Shopping at the biggest mall in Singapore...”); $template−>parseCurrentBlock(); // set values for outer block $template−>setCurrentBlock(”main”); $template−>setVariable(”FONT”, ”Arial”); $template−>parseCurrentBlock(); // show page $template−>show();
PHP Miscellaneous Template 44
◮ Use a nested template
PHP Miscellaneous Template 45
The corresponging PHP file $template−>loadTemplateFile(”list.tpl”); // set values for inner block $template−>setCurrentBlock(”list item”); $template−>setVariable(”ITEM”, ”Item A”); $template−>parseCurrentBlock(); $template−>setVariable(”ITEM”, ”Item B”); $template−>parseCurrentBlock(); $template−>setVariable(”ITEM”, ”Item C”); $template−>parseCurrentBlock(); $template−>setVariable(”ITEM”, ”Item D”); $template−>parseCurrentBlock(); // parse outer block $template−>parse(”list”); // render page $template−>show();
PHP Miscellaneous Template 46
PHP Miscellaneous Template 47
require once ”HTML/Template/IT.php”; $data = array ( ”0” => array(”Stig”, ”Bakken”), ”1” => array(”Martin”, ”Jansen”), ”2” => array(”Alexander”, ”Merz”) ); $tpl = new HTML Template IT(”.”); $tpl−>loadTemplatefile(”main.tpl”, true, true); foreach($data as $name) { foreach($name as $cell) { // Assign data to the inner block $tpl−>setCurrentBlock(”cell”) ; $tpl−>setVariable(”DATA”, $cell) ; $tpl−>parseCurrentBlock(”cell”) ; } // Assign data and the inner block to the // outer block $tpl−>setCurrentBlock(”row”) ; $tpl−>parseCurrentBlock(”row”) ; } $tpl−>show();
PHP Miscellaneous Template 48
◮ SOAP is a Web protocol ◮ It is used to transfer information ◮ Crossplatform and Cross-language
◮ Compatible with most of the firewalls ◮ XML over HTTP
PHP Miscellaneous Web Services 49
◮ Web Service Description Language (WSDL)
XML file for describing the service (all methods parameters and return value)
◮ SOAP Request
<soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:tns=”http://www.webservicex.net” xmlns:xs=”http://www.w3.org/2001/XMLSchema”> − <soap:Body> − <tns:Translate> − <tns:LanguageMode>EnglishTOFrench</tns:LanguageMode> − <tns:Text>This is a phrase</tns:Text> </tns:Translate> </soap:Body> </soap:Envelope>
PHP Miscellaneous Web Services 50
◮ SOAP Response
PHP Miscellaneous Web Services 51
◮ Create an object of class SoapClient ◮ Loads WSDL and create methods for each SOAP method
PHP Miscellaneous Web Services 52
PHP Miscellaneous Web Services 53
PHP Miscellaneous Web Services 54
◮ Debug Methods
◮ require to create the SoapClient with the trace option
PHP Miscellaneous Web Services 55
◮ Handle HTML / XML / WML / XHTML ◮ Acts like a real programming language (OO design, reg-exp,...) ◮ But handles web functionalities perfectly
◮ Can be used for small to medium sized projects ◮ Code can not be used for a GUI
PHP Miscellaneous Conclusion 56