Changeset:
        9e92af237808
        
https://sourceforge.net/p/mrbs/hg-code/ci/9e92af237808e1f437f949d1a12a71e07d3e81f0
Author:
        Campbell Morrison <[email protected]>
Date:
        Thu Oct 27 16:49:49 2016 +0100
Log message:

First attempt at Joomla! authentication

diffstat:

 web/auth/auth_joomla.inc       |   75 ++++++++++++++
 web/internalconfig.inc.php     |    7 +-
 web/joomla.inc                 |   10 +
 web/lib/MRBS/JFactory.php      |   57 +++++++++++
 web/lib/MRBS/JUser.php         |   26 +++++
 web/session/session_joomla.inc |  210 +++++++++++++++++++++++++++++++++++++++++
 web/systemdefaults.inc.php     |   23 ++++-
 7 files changed, 403 insertions(+), 5 deletions(-)

diffs (truncated from 458 to 300 lines):

diff -r 41dd1349fc0f -r 9e92af237808 web/auth/auth_joomla.inc
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/web/auth/auth_joomla.inc  Thu Oct 27 16:49:49 2016 +0100
@@ -0,0 +1,75 @@
+<?php
+namespace MRBS;
+
+require_once MRBS_ROOT . '/joomla.inc';
+
+/* authGetUserLevel($user)
+ * 
+ * Determines the users access level
+ * 
+ * $user - The user name
+ *
+ * Returns:
+ *   The users access level
+ */
+function authGetUserLevel($username)
+{
+  global $auth;
+  
+  // User not logged in, user level '0'
+  if (!isset($username))
+  {
+    return 0;
+  }
+  
+  // Otherwise get the user's access levels
+  $user = JFactory::getUser($username);
+  $authorised_levels = $user->getAuthorisedViewLevels();
+  
+  // Check if they have manager access
+  if (is_array($auth['joomla']['admin_access_levels']))
+  {
+    $admin_levels = $auth['joomla']['admin_access_levels'];
+  }
+  else
+  {
+    $admin_levels = array($auth['joomla']['admin_access_levels']);
+  }
+  if (count(array_intersect($authorised_levels, $admin_levels)) > 0)
+  {
+    return 2;
+  }
+  
+  // Check if they have user access
+  if (is_array($auth['joomla']['user_access_levels']))
+  {
+    $user_levels = $auth['joomla']['user_access_levels'];
+  }
+  else
+  {
+    $user_levels = array($auth['joomla']['user_access_levels']);
+  }
+  if (count(array_intersect($authorised_levels, $user_levels)) > 0)
+  {
+    return 1;
+  }
+
+  // Everybody else is access level '0'
+  return 0;
+}
+
+
+// Gets the user's email address.   Returns an empty
+// string if one can't be found
+function authGetUserEmail($username)
+{
+  if (!isset($username) || $username === '')
+  {
+    return '';
+  }
+  
+  $user = JFactory::getUser($username);
+  $email = '"' . $user->name . '"<' . $user->email . '>';
+  return $email;
+}
+
diff -r 41dd1349fc0f -r 9e92af237808 web/internalconfig.inc.php
--- a/web/internalconfig.inc.php        Thu Oct 27 10:01:41 2016 +0100
+++ b/web/internalconfig.inc.php        Thu Oct 27 16:49:49 2016 +0100
@@ -306,9 +306,10 @@
  * DIRECTORIES - internal use, do not change
  *************************************************/
 
-define('MRBS_ROOT',       __DIR__);                   // Root of MRBS 
installation
-define('TZDIR',           'tzurl/zoneinfo');          // Directory containing 
TZURL definitions
-define('TZDIR_OUTLOOK',   'tzurl/zoneinfo-outlook');  // Outlook compatible 
TZURL definitions
+define('DS',            DIRECTORY_SEPARATOR );
+define('MRBS_ROOT',     __DIR__);                   // Root of MRBS 
installation
+define('TZDIR',         'tzurl/zoneinfo');          // Directory containing 
TZURL definitions
+define('TZDIR_OUTLOOK', 'tzurl/zoneinfo-outlook');  // Outlook compatible 
TZURL definitions
 
 
 /*****************************************
diff -r 41dd1349fc0f -r 9e92af237808 web/joomla.inc
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/web/joomla.inc    Thu Oct 27 16:49:49 2016 +0100
@@ -0,0 +1,10 @@
+<?php
+
+global $auth;
+
+define('_JEXEC', 1);
+define('JPATH_BASE', realpath(MRBS_ROOT . DS . $auth['joomla']['rel_path']));
+ 
+require_once JPATH_BASE . DS . 'includes' . DS . 'defines.php';
+require_once JPATH_BASE . DS . 'includes' . DS . 'framework.php';
+
diff -r 41dd1349fc0f -r 9e92af237808 web/lib/MRBS/JFactory.php
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/web/lib/MRBS/JFactory.php Thu Oct 27 16:49:49 2016 +0100
@@ -0,0 +1,57 @@
+<?php
+
+namespace MRBS;
+
+class JFactory extends \JFactory {
+ 
+  // NOTE:  JFactory::getUser() sems to reset the timezone to the user's
+  // Joomla timezone, which may be different from the MRBS timezone, so we
+  // have to get the timezone before calling it and restore it afterwards.
+  public static function getUser($id = NULL)
+  {
+    $tz = date_default_timezone_get();
+    // need to cast the object to MRBS\JUser to avoid more
+    // Joomla timezone problems
+    $result = self::cast('MRBS\JUser', parent::getUser($id));
+    date_default_timezone_set($tz);
+    return $result;
+  }
+  
+  /**
+   * Class casting
+   *
+   * @param string|object $destination
+   * @param object $sourceObject
+   * @return object
+   */
+  private static function cast($destination, $sourceObject)
+  {
+    if (is_string($destination))
+    {
+        $destination = new $destination();
+    }
+    
+    $sourceReflection = new \ReflectionObject($sourceObject);
+    $destinationReflection = new \ReflectionObject($destination);
+    $sourceProperties = $sourceReflection->getProperties();
+    
+    foreach ($sourceProperties as $sourceProperty)
+    {
+      $sourceProperty->setAccessible(true);
+      $name = $sourceProperty->getName();
+      $value = $sourceProperty->getValue($sourceObject);
+      if ($destinationReflection->hasProperty($name))
+      {
+        $propDest = $destinationReflection->getProperty($name);
+        $propDest->setAccessible(true);
+        $propDest->setValue($destination,$value);
+      }
+      else
+      {
+        $destination->$name = $value;
+      }
+    }
+    
+    return $destination;
+  }
+}
diff -r 41dd1349fc0f -r 9e92af237808 web/lib/MRBS/JUser.php
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/web/lib/MRBS/JUser.php    Thu Oct 27 16:49:49 2016 +0100
@@ -0,0 +1,26 @@
+<?php
+
+namespace MRBS;
+
+class JUser extends \JUser {
+ 
+  // NOTE:  In some versions of Joomla!, JUser::getAuthorisedGroups() seems to 
reset the
+  // timezone to the user's Joomla timezone, which may be different from the 
MRBS timezone, so
+  // we have to get the timezone before calling it and restore it afterwards.
+  public function getAuthorisedGroups()
+  {
+    $tz = date_default_timezone_get();
+    $result = parent::getAuthorisedGroups();
+    date_default_timezone_set($tz);
+    return $result;
+  }
+  
+  // Not sure whether getAuthorisedViewLevels() has the same problem, but just 
in case ...
+  public function getAuthorisedViewLevels()
+  {
+    $tz = date_default_timezone_get();
+    $result = parent::getAuthorisedViewLevels();
+    date_default_timezone_set($tz);
+    return $result;
+  }
+}
diff -r 41dd1349fc0f -r 9e92af237808 web/session/session_joomla.inc
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/web/session/session_joomla.inc    Thu Oct 27 16:49:49 2016 +0100
@@ -0,0 +1,210 @@
+<?php
+namespace MRBS;
+
+require_once MRBS_ROOT . '/joomla.inc';
+
+$mainframe = JFactory::getApplication('site');
+$mainframe->initialise();
+
+global $PHP_SELF, $auth;
+
+
+// Get non-standard form variables
+$Action = get_form_var('Action', 'string');
+$NewUserName = get_form_var('NewUserName', 'string');
+$NewUserPassword = get_form_var('NewUserPassword', 'string');
+$TargetURL = get_form_var('TargetURL', 'string');
+$returl = get_form_var('returl', 'string');
+
+/*
+  Target of the form with sets the URL argument "Action=SetName".
+  Will eventually return to URL argument "TargetURL=whatever".
+*/
+if (isset($Action) && ($Action == "SetName"))
+{
+  /* First make sure the password is valid */
+  if ($NewUserName == "")
+  {
+    $mainframe->logout();
+  }
+  else
+  {
+    $credentials = array();
+    $credentials['username'] = $NewUserName;
+    $credentials['password'] = $NewUserPassword;
+ 
+    if (!$mainframe->login($credentials))
+    {
+      print_header(0, 0, 0, 0, "");
+      echo "<p>".get_vocab('unknown_user')."</p>\n";
+      printLoginForm($TargetURL);
+      exit();
+    }
+  }
+  // preserve the original $HTTP_REFERER by sending it as a GET parameter
+  if (!empty($returl))
+  {
+    // check to see whether there's a query string already
+    if (strpos($TargetURL, '?') === false)
+    {
+      $TargetURL .= "?returl=" . urlencode($returl);
+    }
+    else
+    {
+      $TargetURL .= "&returl=" . urlencode($returl);
+    }
+  }
+
+  header ("Location: $TargetURL"); /* Redirect browser to initial page */
+  /* Note HTTP 1.1 mandates an absolute URL. Most modern browsers support 
relative URLs,
+    which allows to work around problems with DNS inconsistencies in the 
server name.
+    Anyway, if the browser cannot redirect automatically, the manual link 
below will work. */
+  print_header(0, 0, 0, 0, "");
+  echo "<br>\n";
+  echo "<p>Please click <a href=\"".htmlspecialchars($TargetURL)."\">here</a> 
if you're not redirected automatically to the page you requested.</p>\n";
+
+  // Print footer and exit
+  print_footer(TRUE);
+}
+
+/*
+  Display the login form. Used by two routines below.
+  Will eventually return to $TargetURL.
+*/
+function printLoginForm($TargetURL)
+{
+  global $PHP_SELF, $HTTP_REFERER;
+  global $returl;
+?>
+<form class="form_general" id="logon" method="post" action="<?php echo 
htmlspecialchars(basename($PHP_SELF)) ?>">
+  <fieldset>
+  <legend><?php echo get_vocab("please_login") ?></legend>
+    <div>
+      <label for="NewUserName"><?php echo get_vocab("users.name") ?>:</label>
+      <input type="text" id="NewUserName" name="NewUserName">
+    </div>
+    <div>
+      <label for="NewUserPassword"><?php echo get_vocab("users.password") 
?>:</label>
+      <input type="password" id="NewUserPassword" name="NewUserPassword">
+    </div>
+    <?php
+    // We need to preserve the original calling page, so that it's there when 
we eventually get
+    // to the TargetURL (especially if that's edit_entry.php).  If this is the 
first time through then $HTTP_REFERER holds
+    // the original caller.    If this is the second time through we will have 
stored it in $returl.
+    if (!isset($returl))
+    {

------------------------------------------------------------------------------
The Command Line: Reinvented for Modern Developers
Did the resurgence of CLI tooling catch you by surprise?
Reconnect with the command line and become more productive. 
Learn the new .NET and ASP.NET CLI. Get your free copy!
http://sdm.link/telerik
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to