Source for file authenticator.php

Documentation is available at authenticator.php

  1. <?php
  2. /**
  3. * Authenticator class file for Mambo
  4. @package Mambo
  5. @author Mambo Foundation Inc see README.php
  6. @copyright Mambo Foundation Inc.
  7. *  See COPYRIGHT.php for copyright notices and details.
  8. @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see
  9. *  LICENSE.php
  10. *  Mambo is free software; you can redistribute it and/or
  11. *  modify it under the terms of the GNU General Public License
  12. *  as published by the Free Software Foundation; version 2 of the
  13. *  License.
  14. */
  15.  
  16.     
  17.     function &getInstance ({
  18.         static $instance;
  19.         if (!is_object($instance)) {
  20.             $instance =new mamboAuthenticator();
  21.         }
  22.         return $instance;
  23.     }
  24.  
  25.     /**
  26.     * Login management function
  27.     *
  28.     * The current session is passed.
  29.     * Username and encoded password is authenticated.
  30.     * A successful authentication updates the current session record with
  31.     * the users details.
  32.     */
  33.     function loginUser ($username=null$passwd=null$remember=null{
  34.         $mambothandler =mosMambotHandler::getInstance();
  35.         $mambothandler->loadBotGroup('authenticator');
  36.         $session =mosSession::getCurrent();
  37.         $database =mamboDatabase::getInstance();
  38.         if (!$username OR !$passwd{
  39.             $username mosGetParam($_REQUEST'username''');
  40.             $passwd mosGetParam($_REQUEST'passwd''' );
  41.             $bypost 1;
  42.         }
  43.         else $bypost 0;
  44.         if ($remember === null$remember mosGetParam($_REQUEST'remember''');
  45.  
  46.         if (!$username OR !$passwd{
  47.             echo "<script> alert(\"".T_('Please complete the username and password fields.')."\"); window.history.go(-1); </script>\n";
  48.             exit();
  49.         else {
  50.             $username $database->getEscaped($username);
  51.             $passwd $database->getEscaped($passwd);
  52.             $loginfo =new mosLoginDetails($username$passwd$remember);
  53.             $checkuser true;
  54.             $logresults $mambothandler->trigger('requiredLogin',array($loginfo));
  55.             if (count($logresults== 0$logresults[T_('Logins are not permitted.  There is no authentication check active.');
  56.             foreach ($logresults as $message{
  57.                 if ($message$checkuser false;
  58.                 break;
  59.             }
  60.             if ($checkuser{
  61.                 $mambothandler->trigger('goodLogin'array($loginfo));
  62.                 return true;
  63.             }
  64.             $mambothandler->trigger('badLogin'array($loginfo));
  65.             if (isset($bypost)) echo "<script>alert(\"".$message."\"); window.history.go(-1); </script>\n";
  66.             @session_destroy();
  67.         }
  68.     }
  69.     
  70.     /**
  71.     * User authentication function
  72.     *
  73.     * Username and encoded password are checked against the database.
  74.     */
  75.     function authenticateUser (&$message$username$passwd$remember=null$session=null{
  76.         $message '';
  77.         if ($session === null$session =mosSession::getCurrent();
  78.         $database =mamboDatabase::getInstance();
  79.         $database->setQuery"SELECT id, gid, block, usertype"
  80.         . "\nFROM #__users"
  81.         . "\nWHERE username='$username' AND password='$passwd'"
  82.         );
  83.         if ($database->loadObject($row)) {
  84.             if ($row->block{
  85.                 $message T_('Your login has been blocked. Please contact the administrator.');
  86.                 return false;
  87.             }
  88.             // fudge the group stuff
  89. //            $grp = $acl->getAroGroup( $row->id );
  90. //            if ($acl->is_group_child_of( $grp->name, 'Registered', 'ARO' ) ||
  91. //            $acl->is_group_child_of( $grp->name, 'Public Backend', 'ARO' )) {
  92.             // fudge Authors, Editors, Publishers and Super Administrators into the Special Group
  93. //            $row->usertype = $grp->name;
  94.             $session->guest 0;
  95.             $session->username $username;
  96.             $session->userid $row->id;
  97.             $session->usertype $row->usertype;
  98.             if ($row->usertype == 'Registered'$session->gid 1;
  99.             else $session->gid 2;
  100.             $session->gid intval$row->gid )# what is going on here???
  101.             $session->update();
  102.             $currentDate date("Y-m-d\TH:i:s");
  103.             $query "UPDATE #__users SET lastvisitDate='$currentDate' where id='$session->userid'";
  104.             $database->setQuery($query);
  105.             if (!$database->query()) {
  106.                 die($database->stderr(true));
  107.             }
  108.             if ($remember=="yes"{
  109.                 $lifetime time(365*24*60*60;
  110.                 setcookie("usercookie[username]"$username$lifetime"/");
  111.                 setcookie("usercookie[password]"$passwd$lifetime"/");
  112.             }
  113.             //mosCache::cleanCache('com_content');
  114.             mosCache::cleanCache();
  115.         else {
  116.             $message T_('Incorrect username or password. Please try again.');
  117.             $this->clearSession($session);
  118.             return false;
  119.         }
  120.         return true;
  121.     }
  122.  
  123.     function clearSession ($session=null{
  124.         if ($session === null$session =mosSession::getCurrent();
  125.         //mosCache::cleanCache('com_content');
  126.         mosCache::cleanCache();
  127.         $session->guest 1;
  128.         $session->username '';
  129.         $session->userid '';
  130.         $session->usertype '';
  131.         $session->gid 0;
  132.         $session->update();
  133.         // this is daggy??
  134.         $lifetime time(1800;
  135.         setcookie"usercookie[username]"" "$lifetime"/" );
  136.         setcookie"usercookie[password]"" "$lifetime"/" );
  137.         setcookie"usercookie"" "$lifetime"/" );
  138.         @session_destroy();
  139.     }
  140.  
  141.     /**
  142.     * User logout
  143.     *
  144.     * Reverts the current session record back to 'anonymous' parameters
  145.     */
  146.     function logoutUser ({
  147.         $session =mosSession::getCurrent();
  148.         if ($session{
  149.             $mambothandler =mosMambotHandler::getInstance();
  150.             $mambothandler->loadBotGroup('authenticator');
  151.             $loginfo new mosLoginDetails($session->username);
  152.             $mambothandler->trigger('beforeLogout'array($loginfo));
  153.             $this->clearSession($session);
  154.         }
  155.     }
  156.  
  157.     function &loginAdmin ($acl{
  158.         $database =mamboDatabase::getInstance();
  159.         /** escape and trim to minimise injection of malicious sql */
  160.         $usrname     $database->getEscaped(mosGetParam($_POST'usrname'''));
  161.         $pass         $database->getEscaped(mosGetParam($_POST'pass'''));
  162.  
  163.         $my null;
  164.         if (!$passecho "<script>alert('".T_('Please enter a password')."'); document.location.href='index.php';</script>\n";
  165.         else $pass md5$pass );
  166.  
  167.         $admintypes array ('administrator''superadministrator''super administrator');
  168.         $admins 0;
  169.         $query "SELECT u.*, a.name as usertype, a.lft as grp FROM #__users AS u, #__core_acl_aro_groups AS a"
  170.         . "\n WHERE ( LOWER( usertype ) = 'administrator'"
  171.         . "\n OR LOWER( usertype ) = 'superadministrator'"
  172.         . "\n OR LOWER( usertype ) = 'super administrator'"
  173.         . "\n OR (username='$usrname' AND block=0)) AND a.group_id = u.gid"
  174.         ;
  175.         $users $database->doSQLget$query'mosUser' );
  176.         foreach ($users as $key=>$oneuser{
  177.             if (in_array(strtolower($oneuser->usertype),$admintypes)) $admins++;
  178.             if ($oneuser->username == $usrname$my =$users[$key];
  179.         }
  180.         if ($admins == 0echo "<script>alert(\"".T_('You cannot login. There are no administrators set up.')."\"); window.history.go(-1); </script>\n";
  181.         /** find the user group (or groups in the future) */
  182.         elseif (isset($my)) {
  183.             if (strcmp$my->password$pass )
  184.             OR !$acl->acl_check'administration''login''users'$my->usertype )) {
  185.                 echo "<script>alert('".T_('Incorrect Username, Password, or Access Level.  Please try again')."'); document.location.href='index.php';</script>\n";
  186.                 return;
  187.             }
  188.             $logintime     time();
  189.             $session_id md5"$my->id$my->username$my->usertype$logintime);
  190.             $query "INSERT INTO #__session"
  191.             . "\nSET time='$logintime', session_id='$session_id', "
  192.             . "userid='$my->id', usertype='$my->usertype', username='$my->username', guest=-1"
  193.             ;
  194.             $database->setQuery$query );
  195.             if (!$database->query()) {
  196.                 echo $database->stderr();
  197.             }
  198.             $_SESSION['session_id']         $session_id;
  199.             $_SESSION['session_user_id']     $my->id;
  200.             $_SESSION['session_username']     $my->username;
  201.             $_SESSION['session_usertype']     $my->usertype;
  202.             $_SESSION['session_gid']         $my->gid;
  203.             $_SESSION['session_grp']        $my->grp;
  204.             $_SESSION['session_logintime']     $logintime;
  205.             $_SESSION['session_userstate']     array();
  206.         }
  207.         return $my;
  208.     }
  209.     
  210.     /**
  211.     * Random password generator
  212.     * @return password 
  213.     */
  214.     function mosMakePassword({
  215.         $salt "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  216.         $len strlen($salt);
  217.         $makepass="";
  218.         mt_srand(10000000*(double)microtime());
  219.         for ($i 0$i 8$i++)
  220.         $makepass .= $salt[mt_rand(0,$len 1)];
  221.         return $makepass;
  222.     }
  223. }

Documentation generated on Mon, 05 May 2008 16:16:44 +0400 by phpDocumentor 1.4.0