tags:

views:

291

answers:

4

I've got a helper script for a Joomla 1.0 module I'm developing. I'm using it to perform AJAX tasks (reading and writing to the database, returning JSON), so I need to stop Joomla outputting its headers and whatnot.

I've got around this so far by hacking together some code to do the necessary includes to set up the database connection, etc:

define( '_VALID_MOS', 1 );
require_once '../globals.php';
require_once '../configuration.php';
require_once '../includes/joomla.php';

The only problem is that the $my variable (the mos_user object for the current user) is not being created.

How do I create this user? What file do I need to include?

A: 

I found that it's a function on the $mainframe object, but you then have to construct that too:

$mainframe = new mosMainFrame( $database, '', '.' );
$mainframe->initSession();
$my = $mainframe->getUser();
nickf
A: 

You can also call index2.php?no_html=1 (followed by any other parameters you need) to get raw output on a standard component.

jlleblanc
ah i was wondering how to avoid the header being printed. What about if it's a module though?
nickf
You can't call a module directly, but every request in Joomla! will load exactly one component. If you have a module that performs AJAX requests, you will need to build a component to go with it.
jlleblanc
A: 

I've just faced exactly this problem when migrating a 1.5 module to 1.0, but none of the above answers seemed to give the complete story. nickf's answer got me most of the way there, but I then fell down a rabbit hole trying to construct the mainframe.

I then assumed that perhaps $my was already created, and although it didn't work within a function (I'm new to PHP so I assumed it would be accessible) I found that using it at the module scope was fine, as shown below:

<?php
/**
 * Demo package
 *
 * @package    Demo package
 * @subpackage Modules
 */

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );


class modDcsgSimulatorHelper
{

    /**
     * security by obscurity
     */
    function getAppToken( $username )
    {
      return urlencode($username);
    }
}

$apptoken = modDcsgSimulatorHelper::getAppToken( $my->username );
?>
Run the <A href="http://somesecurewebsite.com?apptoken=&lt;?php echo $apptoken; ?>">A Secure Link</A>.

And before anyone flames me for this wonderfully secure mechanism, rest assured I've elided details from the real implementation as it was unneccessarily verbose for my answer.

Ubiguchi
to access global variables (such as $my) from inside functions, add this line to the start of the function: "global $my;"
nickf
A: 

People, what is a $mainframe object, and what is a mosMainFrame class? I can not to find them here - http://api.joomla.org/li%5FJoomla-Framework.html .

Israel Rider
Hi Israel, if you want answers to a question, the best way to get them is to Ask a Question (button in the top right of every page). In Joomla 1.5, they removed the $mainframe object, which is why it's not on that page.
nickf