bootstrapping joomla 2.5 from an external script - php

This used to work in 1.5... I could set up the following code:
function startJoomla() {
define('_JEXEC', true);
define( 'DS', DIRECTORY_SEPARATOR );
$dir= dirname(__FILE__);
define('JPATH_BASE', $dir);
// load joomla libraries
require_once JPATH_BASE . DS . 'includes' . DS . 'defines.php';
require_once JPATH_LIBRARIES . DS . 'loader.php';
jimport('joomla.base.object');
jimport('joomla.factory');
jimport('joomla.filter.filterinput');
jimport('joomla.error.error');
jimport('joomla.event.dispatcher');
jimport('joomla.event.plugin');
jimport('joomla.plugin.helper');
jimport( 'joomla.utilities.utility' );
jimport('joomla.utilities.arrayhelper');
jimport('joomla.environment.uri');
jimport('joomla.environment.request');
jimport('joomla.user.user');
// JText cannot be loaded with jimport since it's not in a file called text.php but in methods
JLoader::register('JText', JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'methods.php');
JLoader::register('JRoute', JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'methods.php');
$mainframe = & JFactory::getApplication('site');
$GLOBALS['mainframe'] = & $mainframe;
return $mainframe;
}
$mainframe = startJoomla();
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
And then, when I wanted to call something up from Joomla, I could do the following:
$custom = new CustomController(); // assuming there's a com_custom somewhere
This worked for 1.5. But in 2.5, the above doesn't work at all. I found a revised version of the load script:
function startJoomla() {
define('_JEXEC', 1);
define( 'DS', DIRECTORY_SEPARATOR );
$dir = dirname(__FILE__);
define('JPATH_BASE', $dir);
// load joomla libraries
require_once JPATH_BASE.'/includes/defines.php';
require_once JPATH_BASE.'/includes/framework.php';
require_once JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php';
// Instantiate the application.
$app = JFactory::getApplication('site');
// Initialise the application.
return $app->initialise();
}
$mainframe = startJoomla();
This enables things like 'JFactory::getUser();' but will no longer allow me to utilize commands like 'new MycustomcomponentController();' Gives me 'core' Joomla functionality, but skips everything I installed. Is there an appropriate new revised compilation of jimport instructions or perhaps a different function of JComponent that allows me to call up my custom functions along with other components and models for use in external php files? Assume this will be run from a CRON or similar.

Related

Unable to trace require_once error in PHP

i have a file structure like this in my PHP 7.1
Model/Abstract.php
Model/NTLStm.php
Model/SoapCl.php
Controller.php
i Called Model/Abstract.php in Controller like this:
use Model/Abstract as modelAbstract;
$abstract = new modelAbstract();
i tried to include the Model/NTLStm.php & Model/SoapCl.php in Model/Abstract.php like this:
defined('DS') OR define('DS', DIRECTORY_SEPARATOR);
require_once dirname(__FILE__) . DS . 'SoapCl.php';
require_once dirname(__FILE__) . DS . 'NTLStm.php';
echo 'success';
but it seems it always terminate the process in require_once , i already tried to put try catch like this:
try{
defined('DS') OR define('DS', DIRECTORY_SEPARATOR);
require_once dirname(__FILE__) . DS . 'SoapCl.php';
require_once dirname(__FILE__) . DS . 'NTLStm.php';
} catch(\Exception $e){
echo $e->getMessage();
}
but it won't print anything
Try using parentheses with require_once... like this:
require_once (dirname(__FILE__) . DS . 'SoapCl.php');
require_once (dirname(__FILE__) . DS . 'NTLStm.php');

Check user logged in Joomla 2.5 from external script (reiterated)

I'm well familiar with coding in PHP and other languages, but I'm totally unfamiliar with Joomla.
What I want to accomplish is to know when a user has logged in on the Joomla site (under /web) and differentiate this script's behaviour accordingly.
I have a Joomla 2.5 site under /web/ and I have a script under /web/test/index.php, where the following code lies (collected from other replies in here and elsewhere):
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
echo dirname(dirname(__FILE__)) . "\n";
if (file_exists(dirname(dirname(__FILE__)) . '/defines.php'))
include_once dirname(dirname(__FILE__)) . '/defines.php';
if (!defined('_JDEFINES')) {
define('JPATH_BASE', dirname(dirname(__FILE__)));
require_once JPATH_BASE.'/includes/defines.php';
}
require_once JPATH_BASE.'/includes/framework.php';
$app = JFactory::getApplication('site');
$app->initialise();
print_r(JFactory::getUser());
What I get back is
/web
JUser Object
(
[isRoot:protected] =>
[id] => 0
[name] =>
...
Thank you,
Use this code it's working:
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/' ));
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$mainframe = JFactory::getApplication('site');
var_dump(JFactory::getUser());

How to modify template params via database in joomla

I want to modify the current template params via database outside joomla framework. I managed to set the 'preset' param, but I can't write it back to the database. Here is my code.
Thanks a lot
// Initialize The Joomla Framework
// -----------------------------------------------------------------------------------
define('_JEXEC', 1);
// this is relative to the current script, so change this according to your environment
define('JPATH_BASE', '/home/kristof/public_html/joomla1');
define('DS', DIRECTORY_SEPARATOR);
// Require Joomla libraries
require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require_once(JPATH_CONFIGURATION . DS . 'configuration.php');
require_once(JPATH_LIBRARIES . DS . 'joomla' . DS . 'database' . DS . 'database.php');
require_once(JPATH_LIBRARIES . DS . 'import.php');
// -----------------------------------------------------------------------------------
$app = JFactory::getApplication('site');
$template = $app->getTemplate(true);
$param=$template->params->set('preset','preset3');
$template = JFactory::getApplication('site')->getTemplate();
$db = JFactory::getDBO();
$sql = "select params from #__template_styles where template = ".$db->quote($template);
$db->setQuery($sql);
$params = json_decode($db->loadResult());
$params->PARAM_NAME= PARAM_VALUE;
$sql = "update #__template_styles set params = ".$db->quote(json_encode($params))." where template = ".$db->quote($template);
$db->setQuery($sql);
return $db->query();

use joomla JRoute in external php file

i want use joomla methods in external php file and i use this code and it's work:
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../..' ));
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$mainframe =& JFactory::getApplication('site');
now i want use joomla JRoute method but it's not work correct in external file. how can use JRoute ?
thank you.

How can I access login user data using external files in joomla 2.5?

I am having trouble in accessing the data of the login user in joomla site.
Can someone help me on it?
I've tried this code:
define( '_JEXEC', 1 );
define( 'DS', '/' );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );
require_once( JPATH_BASE . DS .'xampp' . DS .'UPOnlineLeave '. DS .'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS .'xampp' . DS .'UPOnlineLeave '. DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS .'xampp' . DS .'UPOnlineLeave '. DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
$user =& JFactory::getUser();
$session =& JFactory::getSession();
$user->username;
but it produces JOSerror:application instantation error.
Can someone help me?
Fisrt you need to load the entire Joomla Framework:
// Get Joomla! framework
define( '_JEXEC', 1 );
define( '_VALID_MOS', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__)));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
Only then you can try to get the user
$user =& JFactory::getUser();
$session =& JFactory::getSession();
You have to include the following lines to include the joomla default libraries.
You can also refer the code on the root path of the project index.php file
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
if (file_exists(dirname(__FILE__) . '/defines.php')) {
include_once dirname(__FILE__) . '/defines.php';
}
if (!defined('_JDEFINES')) {
define('JPATH_BASE', dirname(__FILE__));
require_once JPATH_BASE.'/includes/defines.php';
}
require_once JPATH_BASE.'/includes/framework.php';

Categories