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

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