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.4 2004/12/18 21:53:17 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. $GLOBALS['obs''';
  36.  
  37. /**
  38.  * LiveUser observer example.
  39.  *
  40.  * This class uses the facilities provided by PEAR::Log
  41.  * to create an observer for LiveUser.
  42.  *
  43.  * There are two ways to define an observer:
  44.  * - The class should contain methods
  45.  *   with the same names ar the events that can be triggered.
  46.  * - when the observer is attached to LiveUser you must define
  47.  *   the event => methodName mapping.
  48.  *
  49.  * @access public
  50.  * @param  object receives the main LiveUser object, fetch it by reference
  51.  *                 to avoid object copies
  52.  * @param  array  an array of optional paramaters the triggered event can send
  53.  * @return void  return is discarded
  54.  */
  55. class Log_LU extends Log
  56. {
  57.     function Log_LU($name ''$ident ''$conf = array(),
  58.                          $level = PEAR_LOG_DEBUG)
  59.     {
  60.         $this->_id md5(microtime());
  61.         $this->_ident $ident;
  62.         $this->_mask = Log::UPTO($level);
  63.  
  64.         if (!empty($conf['error_prepend'])) {
  65.             $this->_error_prepend $conf['error_prepend'];
  66.         else {
  67.             $this->_error_prepend ini_get('error_prepend_string');
  68.         }
  69.  
  70.         if (!empty($conf['error_append'])) {
  71.             $this->_error_append $conf['error_append'];
  72.         else {
  73.             $this->_error_append ini_get('error_append_string');
  74.         }
  75.     }
  76.     function onLogin(&$obj$params = array())
  77.     {
  78.         $GLOBALS['obs'.= '<p>Observer called with the Liveuser object';
  79.         if (count($params> 0{
  80.             $GLOBALS['obs'.= ' and optional parameters</p>';
  81.             ob_start();
  82.             var_dump($params);
  83.             $GLOBALS['obs'.= ob_get_contents();
  84.             ob_clean()
  85.         }
  86.     }
  87.     function onLogout()
  88.     {
  89.         print 'You have been logged out';
  90.         exit();
  91.     }
  92.     function onIdled()
  93.     {
  94.         print 'You have been idle for too long !<br />';
  95.         print 'Please login again';
  96.     }
  97. }
  98.  
  99. $log &Log::factory('LU');
  100.  
  101. // Create new LiveUser (LiveUser) object.
  102. $LU =LiveUser::factory($liveuserConfig);
  103. $LU->attachObserver($log);
  104.  
  105. $username (isset($_REQUEST['handle'])) $_REQUEST['handle': NULL;
  106. $password (isset($_REQUEST['passwd'])) $_REQUEST['passwd': NULL;
  107. $logout (isset($_REQUEST['logout'])) $_REQUEST['logout': false;
  108. $LU->init($username$password$logout);
  109. ?>
  110. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  111. <html>
  112. <head>
  113.     <title>Example 1</title>
  114.     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  115.     <style type="text/css">
  116.     <!--
  117.     body {
  118.         font-family: Verdana, Arial, Helvetica, sans-serif;
  119.         font-size: 12px;
  120.         color: #000000;
  121.         background-color: #FFFFFF
  122.     }
  123.  
  124.     table {
  125.         border: 1px solid #000;
  126.         border-top: 0px;
  127.         border-right: 0px;
  128.         border-spacing: 0px;
  129.         border-collapse: collapse;
  130.     }
  131.  
  132.     table td {
  133.         width: 100px;
  134.         border-top: 1px solid #000;
  135.         border-right: 1px solid #000;
  136.         padding: 5px;
  137.     }
  138.  
  139.         .center {
  140.            text-align: center;
  141.     }
  142.     .center table {
  143.            margin: auto;
  144.     }
  145.     -->
  146.     </style>
  147. </head>
  148.  
  149. <body>
  150. <?php
  151. // Check if the user has logged in successfully
  152. if (!$LU->isLoggedIn()) {
  153.     if (!$username{
  154. ?>
  155.     <form name="loginform" method="post" action="example.php">
  156.     <div class="center">
  157.         <table width="300" border="0" cellspacing="0" cellpadding="5">
  158.             <tr>
  159.                 <td colspan="2"><b>Example login</b></td>
  160.             </tr>
  161.             <tr>
  162.                 <td>Handle:</td>
  163.                 <td>
  164.                     <input type="text" name="handle" maxlength="80" value="" />
  165.                 </td>
  166.             </tr>
  167.              <tr>
  168.                 <td>Password:</td>
  169.                 <td>
  170.                     <input type="password" name="passwd" maxlength="80" value="" />
  171.                 </td>
  172.             </tr>
  173.             <tr>
  174.                 <td colspan="2">
  175.                     <div class="center">
  176.                         <input type="submit" value="Login" />
  177.                     </div>
  178.                 </td>
  179.             </tr>
  180.         </table>
  181.     </div>
  182.     </form>
  183. <?php
  184.     // The user couldn't login, so let's check if the reason was that
  185.     // he's not yet been declared "valid" by an administrator.
  186.     else if ($LU->isInactive()) {
  187. ?>
  188.         <h3>Sorry kid, but one of our admins has yet approved
  189.        your user status. Please be patient. Don't call us -
  190.        we'll call you.</h3>
  191.       <p align="center"><a href="example.php?logout=1">Logout</a></p>
  192. <?php
  193.     else {
  194. ?>
  195.       <h3>Sorry, we can't let you in. Check if the spelling of
  196.       your handle and password is correct.</h3>
  197.       <p align="center"><a href="example.php?logout=1">Logout</a></p>
  198. <?php
  199.     }
  200. ?>
  201.         <p>&nbsp;</p>
  202.         <p><i>Login Data for this Example:</i></p>
  203.         <table>
  204.             <tr>
  205.                 <td style="text-align: center; font-weight: bold;">Handle</th>
  206.                 <td style="text-align: center; font-weight: bold;">Password</th>
  207.             </tr>
  208.             <tr>
  209.             <td>father</td>
  210.                 <td>father</td>
  211.             </tr>
  212.             <tr>
  213.             <td>mother</td>
  214.                 <td>mother</td>
  215.             </tr>
  216.             <tr>
  217.                 <td>child</td>
  218.                 <td>child</td>
  219.     </tr>
  220.     </table>
  221. <?php
  222. else {
  223. ?>
  224.     <h2 align="center">User logged in: <?php echo $LU->getProperty('handle')?></h2>
  225.     <h3>Congrats, you're in</h3>
  226.     <p align="center"><a href="example.php?logout=1">Logout</a></p>
  227. <?php
  228. }
  229. // Just some more debug output with no further relevance
  230. echo '<hr />Handle:';
  231. print_r($LU->getProperty('handle'));
  232. echo '<br />User Type:';
  233. print_r($LU->getProperty('userType'));
  234.  
  235. echo '<hr /><pre>';
  236. print_r($LU);
  237. echo '<hr />';
  238. print_r($_SESSION);
  239. echo '<hr />';
  240. print_r($_REQUEST);
  241. echo '<hr />';
  242. echo 'Observer<br />';
  243. var_dump($GLOBALS['obs']);
  244. echo '</pre>';
  245. ?>
  246. </body>
  247. </html>

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