pearweb_index
[ class tree: pearweb_index ] [ index: pearweb_index ] [ all elements ]

Source for file effectivepearuse.php

Documentation is available at effectivepearuse.php

  1. <?php response_header('PEAR Support :: Developing Effectively with PEAR packages')?>
  2. <h1>Developing Effectively with PEAR Packages</h1>
  3. <h5>by Justin Patrin, edited by Gregory Beaver</h5>
  4. <h2>Introduction</h2>
  5. <p>
  6.  This document is the result of years of collected helpful hints that everyone
  7.  using PEAR packages should know about.  Users who come on IRC often need to know
  8.  this information, and so now this knowledge is available for all to learn from.
  9. </p>
  10.  
  11. <h2>Error Handling</h2>
  12.  
  13. <p>
  14. Most PEAR packages will return errors from a function call. These errors take the form of <a href="http://pear.php.net/manual/en/core.pear.pear-error.php">PEAR_Error</a> objects. The correct way to handle these is:
  15. </p>
  16.  
  17. <div class="explain">
  18. <?php
  19. <?php
  20. require_once \'PEAR/DB.php\';
  21. $db = DB::connect($dsn);
  22. if (PEAR::isError($db)) {
  23.     //This is an example of what you can do when an error happens. You could also log the error or try to recover from it.
  24.     die($db->getMessage() . \' \' . print_r($db->getUserInfo(), true));
  25. }
  26. ?>')
  27. ?>
  28. </div>
  29.  
  30. <p>
  31. This should be done for all calls which are documented to return an error. If you don't check for an error return you will get error messages such as <code>Fatal error: Call to undefined function: PEAR_Error::fetchRow(). in /usr/share/php5/MDB2.php on line 1921.</code>
  32. </p>
  33.  
  34. <p>
  35. Another way to handle errors in PEAR is to use a global error handler. A simple example is below. This will die on all PEAR_Errors and show the reason for the error:
  36. </p>
  37.  
  38. <div class="explain">
  39. <?php
  40. <?php
  41. function handle_pear_error($e) {
  42.     die($e->getMessage() . \' \' . print_r($e->getUserInfo(), true));
  43. }
  44. require_once \'PEAR.php\';
  45. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, \'handle_pear_error\');
  46. ?>'?>
  47. </div>
  48.  
  49. <p>
  50. You can also get a backtrace from the error object so that you can see where the error came from. This is especially helpful when you're using a global error handler and you can't tell where the error is coming from.
  51. </p>
  52.  
  53. <?php
  54. <?php
  55. function handle_pear_error($e) {
  56.     echo \'Backtrace:
  57. \';
  58.     foreach ($e->getBacktrace() as $l) {
  59.         echo \'File: \' . $l[\'file\'] . \' Line: \' . $l[\'line\'] .
  60.              \' Class: \' . $l[\'class\'] . \' Function: \' . $l[\'function\'] . \'
  61. \';
  62.     }
  63.     die($e->getMessage() . \' \' . print_r($e->getUserInfo(), true));
  64. }
  65. require_once \'PEAR.php\';
  66. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, \'handle_pear_error\');
  67. ?>'?>
  68.  
  69. <p>
  70. The first line output will be from PEAR_Error and can usually be ignored. It is the next line that tells you where the error was raised.
  71. </p>
  72.  
  73. <div class="explain">
  74. <pre>
  75. Backtrace:
  76. File: /usr/lib/php/PEAR.php Line: 572 Class: PEAR_Error Function: PEAR_Error
  77. File: /home/papercrane/public_html/test.php Line: 13 Class: PEAR Function: raiseError
  78. Some Error
  79. </pre>
  80. </div>
  81.  
  82. <p>
  83. Newer PEAR packages which are written for PHP5 use <a href="http://pear.php.net/manual/en/core.pear.pear-exception.php">PEAR_Exception</a> instead of PEAR_Error. See the <a href="http://pear.php.net/pepr/pepr-proposal-show.php?id=132">Error Handling Guidelines for PHP5 packages RFC</a>, <a href="http://wiki.ciaweb.net/yawiki/index.php?area=PEAR_Dev&page=RfcExceptionUse">the wiki page it is based on</a>, and the <a href="http://www.php.net/exceptions">PHP documentation</a> for how to handle these. Be careful with your input and output
  84. </p>
  85.  
  86. <h2>Security Concerns</h2>
  87.  
  88. <p>
  89. Handling the superglobals such as <code>$_POST</code>, <code>$_GET</code>, and <code>$_REQUEST</code> can be tricky and leave you open to Injection and XSS attacks as well as cause annoying problems such as multiplying backslashes.
  90. </p>
  91.  
  92. <p>
  93. It is always best to let a well tested package, such as <a href="http://pear.php.net/HTML_QuickForm">HTML_QuickForm</a>, <a href="http://pear.php.net/MDB2">MDB2</a>, or <a href="http://pear.php.net/DB">DB</a> handle these values for you.
  94. </p>
  95.  
  96. <p>
  97. For input and output of form values, use HTML_QuickForm. It will automatically quote your values so as to stop XSS and will also make sure that magic_quotes_gpc isn't corrupting your values.
  98. </p>
  99.  
  100. <?php
  101. <?php
  102. $value = \'inject">XX<input name="password" type="hidden" value="h4cked\';
  103. require_once \'HTML/QuickForm.php\';
  104. $form = new HTML_QuickForm();
  105. $form->addElement(\'password\', \'password\', \'Enter your password\');
  106. $form->setDefaults($value);
  107. if ($form->validate()) {
  108.     echo \'Password entered: \' . htmlentities($form->exportValue(\'password\'));
  109. }
  110. $form->display();'?>
  111.  
  112. <p>
  113. If you had simply output <code>$value</code> without passing it through HTML_QuickForm you would have had injected HTML in your form. If you happened to have <pre>magic_quotes_gpc</pre> turned on (you should never have this on) then the value output would have had extra backslashes before any quotes passed in. If htmlentities() hadn't been run before outputting the value then any HTML entered would have been injected into your page.
  114. </p>
  115.  
  116. <p>
  117. Then when you want to insert into your database: With MDB2:
  118. </p>
  119.  
  120. <?php
  121. <?php
  122. $mdb2 = MDB2::connect($dsn);
  123. $sth = $mdb2->query(\'SELECT * FROM table WHERE col = \' .
  124.     $mdb2->quote($value, \'string\'));
  125.  
  126. // With DB:
  127.  
  128. $db = DB::connect($dsn);
  129. $sth = $db->query(\'SELECT * FROM table WHERE col = \' .
  130.     $db->quoteSmart($value));
  131. ?>'?>

Documentation generated on Mon, 11 Mar 2019 15:14:35 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.