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

Source for file example.php

Documentation is available at example.php

  1. <?php
  2. /**
  3.  * Test for the LiveUser class
  4.  * ===============================
  5.  *
  6.  * This example sets up an authorization system using the LiveUser
  7.  * class. You don't have to use this to use the LiveUser class(es), but
  8.  * this way you don't need to take care of the login-process, storing
  9.  * the user object in a session and more...
  10.  *
  11.  * This example is intended to be used with the auth XML driver.
  12.  * No permission management is supported.
  13.  *
  14.  * @author Björn Kraus <krausbn@php.net>
  15.  * @version $Id: example.php,v 1.12 2005/12/18 12:41:10 lsmith Exp $
  16.  ***/
  17.  
  18. // Get LiveUser configuration array
  19. require_once 'conf.php';
  20.  
  21. if (!isset($liveuserConfig)) {
  22.     die('<p style="color: red; text-align: center;">The XML file isn\'t readable/writable. Add the right permissions to it and then try again.</p>');
  23. }
  24.  
  25. // The error handling stuff is not needed and used only for debugging
  26. // while LiveUser is not yet mature
  27. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK'eHandler');
  28.  
  29. function eHandler($errObj)
  30. {
  31.     echo('<hr /><span style="color: red">' $errObj->getMessage(':<br />'$errObj->getUserInfo('</span><hr />');
  32. }
  33.  
  34.  
  35. class Log_liveuserlog extends Log
  36. {
  37.     function Log_liveuserlog($name ''$ident ''$conf = array(),
  38.                             $level = PEAR_LOG_DEBUG)
  39.     {
  40.         $this->_id md5(microtime());
  41.         $this->_ident $ident;
  42.         $this->_mask = Log::UPTO($level);
  43.     }
  44.  
  45.     function log($msg$level)
  46.     {
  47.         echo "message: $msg\n";
  48.     }
  49. }
  50.  
  51. class LU_Default_observer
  52. {
  53.     function notify(&$notification)
  54.     {
  55.         echo "observer called on event: " $notification->getNotificationName(" \n";
  56.     }
  57. }
  58.  
  59. // Create new LiveUser (LiveUser) object.
  60. // We´ll only use the auth container, permissions are not used.
  61. $LU =LiveUser::factory($liveuserConfig);
  62.  
  63. $obs = new LU_Default_observer();
  64.  
  65. $LU->dispatcher->addObserver(array(&$obs'notify'));
  66.  
  67. if (!$LU->init()) {
  68.     var_dump($LU->getErrors());
  69.     die();
  70. }
  71.  
  72. $username (array_key_exists('handle'$_REQUEST)) $_REQUEST['handle': null;
  73. $password (array_key_exists('passwd'$_REQUEST)) $_REQUEST['passwd': null;
  74. $logout (array_key_exists('logout'$_REQUEST)) $_REQUEST['logout': false;
  75.  
  76. if ($logout{
  77.     $LU->logout(true);
  78. elseif($username && (!$LU->isLoggedIn(|| $LU->getProperty('handle'!= $username)) {
  79.     $LU->login($username$password);
  80. }
  81.  
  82. ?>
  83. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  84. <html>
  85. <head>
  86.     <title>Example 1</title>
  87.     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  88.     <style type="text/css">
  89.     <!--
  90.     body {
  91.         font-family: Verdana, Arial, Helvetica, sans-serif;
  92.         font-size: 12px;
  93.         color: #000000;
  94.         background-color: #FFFFFF
  95.     }
  96.  
  97.     table {
  98.         border: 1px solid #000;
  99.         border-top: 0px;
  100.         border-right: 0px;
  101.         border-spacing: 0px;
  102.         border-collapse: collapse;
  103.     }
  104.  
  105.     table td {
  106.         width: 100px;
  107.         border-top: 1px solid #000;
  108.         border-right: 1px solid #000;
  109.         padding: 5px;
  110.     }
  111.  
  112.         .center {
  113.            text-align: center;
  114.     }
  115.     .center table {
  116.            margin: auto;
  117.     }
  118.     -->
  119.     </style>
  120. </head>
  121.  
  122. <body>
  123. <?php
  124. // Check if the user has logged in successfully
  125. if (!$LU->isLoggedIn()) {
  126.     if (!$username{
  127. ?>
  128.     <form name="loginform" method="post" action="example.php">
  129.     <div class="center">
  130.         <table width="300" border="0" cellspacing="0" cellpadding="5">
  131.             <tr>
  132.                 <td colspan="2"><b>Example login</b></td>
  133.             </tr>
  134.             <tr>
  135.                 <td>Handle:</td>
  136.                 <td>
  137.                     <input type="text" name="handle" maxlength="80" value="" />
  138.                 </td>
  139.             </tr>
  140.              <tr>
  141.                 <td>Password:</td>
  142.                 <td>
  143.                     <input type="password" name="passwd" maxlength="80" value="" />
  144.                 </td>
  145.             </tr>
  146.             <tr>
  147.                 <td colspan="2">
  148.                     <div class="center">
  149.                         <input type="submit" value="Login" />
  150.                     </div>
  151.                 </td>
  152.             </tr>
  153.         </table>
  154.     </div>
  155.     </form>
  156. <?php
  157.     // The user couldn't login, so let's check if the reason was that
  158.     // he's not yet been declared "valid" by an administrator.
  159.     else if ($LU->isInactive()) {
  160. ?>
  161.         <h3>Sorry kid, but one of our admins has yet approved
  162.        your user status. Please be patient. Don't call us -
  163.        we'll call you.</h3>
  164.       <p align="center"><a href="example.php?logout=1">Logout</a></p>
  165. <?php
  166.     else {
  167. ?>
  168.       <h3>Sorry, we can't let you in. Check if the spelling of
  169.       your handle and password is correct.</h3>
  170.       <p align="center"><a href="example.php?logout=1">Logout</a></p>
  171. <?php
  172.     }
  173. ?>
  174.         <p>&nbsp;</p>
  175.         <p><i>Login Data for this Example:</i></p>
  176.         <table>
  177.             <tr>
  178.                 <td style="text-align: center; font-weight: bold;">Handle</th>
  179.                 <td style="text-align: center; font-weight: bold;">Password</th>
  180.             </tr>
  181.             <tr>
  182.             <td>father</td>
  183.                 <td>father</td>
  184.             </tr>
  185.             <tr>
  186.             <td>mother</td>
  187.                 <td>mother</td>
  188.             </tr>
  189.             <tr>
  190.                 <td>child</td>
  191.                 <td>child</td>
  192.     </tr>
  193.     </table>
  194. <?php
  195. else {
  196. ?>
  197.     <h2 align="center">User logged in: <?php echo $LU->getProperty('handle')?></h2>
  198.     <h3>Congrats, you're in</h3>
  199.     <p align="center"><a href="example.php?logout=1">Logout</a></p>
  200. <?php
  201. }
  202. // Just some more debug output with no further relevance
  203. echo '<hr />Handle:';
  204. print_r($LU->getProperty('handle'));
  205. echo '<hr />Name:';
  206. print_r($LU->getProperty('name'));
  207.  
  208. echo '<hr /><pre>';
  209. print_r($LU);
  210. echo '<hr />';
  211. print_r($_SESSION);
  212. echo '<hr />';
  213. print_r($_REQUEST);
  214. echo '<hr />';
  215. echo 'Observer<br />';
  216. var_dump($GLOBALS['obs']);
  217. echo '</pre>';
  218. ?>
  219. </body>
  220. </html>

Documentation generated on Mon, 28 Jan 2008 03:30:14 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.