Time has passed, and since original article How to use Joomla security in own php code my own coding has switched to Joomla native using Component Creator so this trick has really not been needed.
Now however I have found some smart php apps which I would like to integrate into Joomla security - so had to refer to this again - and upgrade it to work with Joomla 6.
A litlle background info
How to do it!
- passing the current session-ID from Joomla to the target-web-page in the Joomla Wrapper (iFrame)
- in the target-page use the session-ID to retrieve user info from Joomla user file
- using Joomla user info to decide on acces to programs
- remember to keep passing session-ID!
Passing session-ID to target web page
| .. to pass the specific session-variable of Joomla to the iframe ....
The new concept of "Template Overrides" in Joomla 1.5.x makes it easy and there is no hack in the Joomla-core necessary. I assume knowledge about this concept, otherwise there is lot of information on the joomla-websites. You make a file in your joomla-templatey directory which should be:
your_joomla_template/html/com_wrapper/wrapper/default.php to override the core-wrapper-component. At the beginning of the source-code - right after the default "no direct access"- lines copy this: ################# $session =& JFactory::getSession(); $sid = $session->getId(); ################### This passes the current joomla-sessionvariable to the variable $id through joomlaspecific functions. $id must now be added to the url, which calls the iframe in Joomla-framework - to be found a litte bit below: src="/joomla/wrapper->url; ?>" is modified to: src="/joomla/wrapper->url."?sessioncookie=$sid"; ?>" Passing a sessionvariable to the iframe doesn't work in the Joomla-Framework - I don't know why. I get the error "Notice: Undefined index: sessioncookie in ..... " - sessioncookie as sessionvariable is not recognized by the iframe. url works! |
Joomla 6 - modifications
#################$session = Joomla\CMS\Factory::getApplication()->getSession();$sid = $session->getId();###################
and modify the url genration like this:
src="<?php echo $this->escape($this->wrapper->url)."?sessioncookie=$sid"; ?>"
That was not so bad - and easy to test that the ID is actually passed!
Retrieve user info from Joomla user file
<?PHP// this script access Joomla databases session-table to verify sessionid, and retrieve user info// fields: username, time, session_id, guest, userid, usertype, gid, client_id, data
// Set flag that in parent file// define( '_JosSession', 1 );
// no direct accessdefined( '_JosSession' ) or die( 'Restricted access' );
// DB login information$jos_username = 'testreader'; // restrict user to read just this table if possible$jos_password = 'password';$jos_database = 'database';$jos_table = 'table';$jos_host = 'host';/****** Connect to MySQL ******/if(!extension_loaded('mysqlnd')){ die("ERROR: PHP is not configured to connect to MySQL.");}
// Create connection$conn = new mysqli($jos_host, $jos_username, $jos_password, $jos_database);
// Check connectionif ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);}
// Check table name to make sure.... $sql = "select * from ".$jos_table." where session_id='".sessioncookie."'";
// Execute the SQL query$result = $conn->query($sql);
// Process the result setif ($result->num_rows > 0) { // check if name i filled $row = $result->fetch_assoc(); if(!$row["username"]) { die("ERROR: Login to access content."); }} else { die("ERROR: can not find session in database.");}
?>
Using Joomla user info
define('sessioncookie', htmlspecialchars($_GET["sessioncookie"]));
if (!sessioncookie) { die("ERROR: no session!"); }define('_JosSession', 1 ); // define for later test if module called directlyinclude("./jos_info.php");
Modifying original script
2) modify following parameters added - doing search & replace: phpSelf+'? ==> phpSelf+'&