Get current article id Joomla! 2.5 - php

I'm trying to retrieve the "current" article id from an external script in Joomla! 2.5
Firstly, I included Joomla core files:
define( '_JEXEC', 1 );
defined('_JEXEC') or die('Restricted access');
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' );
Then, initialized the session:
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
Now, I know that for Joomla 2.5 I have to use Jinput instead of JRequest (deprecated), so:
$jAp = JFactory::getApplication();
$jInput = $jAp->input;
As already told, I'm trying to retrieve the article id.
I tried many instances but nothing seems to work.
$id = $jInput->get('id', 0); // doesn't work
$idInt = $jInput->getInt('id',0); //doesn't work
Also with JRequest... I can't get current article id.
This works only if I would request data for logged in users, example:
$user = JFactory::getUser();
echo $user->username; // this works...
What I am missing?
Where is my fault?
Thanks a lot to all!

Debugging AJAX can be complex. I often find adding some logging in my handler helps a lot, for example adding the following to the start of your code...
# logging of all hits
$log_file = realpath(dirname(__FILE__)) . '/ajax_debug.log';
$fh = fopen($log_file, 'a') or die();
$log_string = "Backend Hit \n" . date("Y-m-d H:i:s") . "\n";
$log_string .= "POST: " . print_r($_POST, true) . "\n";
$log_string .= "GET: " . print_r($_GET, true) . "\n";
$log_string .= "Hit by: " . $_SERVER['REMOTE_ADDR'] . "\n";
$log_string .= "\n\n\n";
fwrite($fh, $log_string);
fclose($fh);
You didn't mention how/where your AJAX code was implemented. It sounds like it might be an implementation outwith Joomla. I would tend to put this in a plug-in if it is quite stand-alone, though module or component AJAX handlers also work well in Joomla. There are some good docs and code at https://docs.joomla.org/Using_Joomla_Ajax_Interface

Related

PHP __DIR__ working locally but not on production

I have a script in a Wordpress plugin and I am trying to incorporate logging using fwrite().
My code is like this:
$log_url = __DIR__ . '/logs/update-email-publish-tasks/' . date("n.j.Y") . '.txt';
$logfile = fopen($log_url, "w");
fwrite($logfile, 'Starting tasks at ' . date("H.i.s") . "\n");
//A bunch of code that logs stuff;
fwrite($logfile, "\n" . 'Finished at !' . date("H.i.s") . "\n");
fclose($logfile);
This is working fine locally but does not work on production. I am wondering if I need to use something besides __DIR__? I also tried WP_CONTENT_DIR which did not work.
FWIW I am hosting production on Pantheon.

Joomla 3.x set User to a specific group with PHP

I just want to set a user to a specific group. I have tried many things but not helped. One solutiuon is worked for me, but I think it is deprecated because in the Joomla's admin page I see other groups.
My code:
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );
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' );
$user =JFactory::getUser(joe); //joe is the username
$GroupJoomla = array('14'=>14); //I want to change it's group to 14
$user->set('groups',$GroupJoomla); //Method one, which I have found on documentations
print_r ( $user->get('groups')); //Succesfully sets, BUT if I check on the Joomla's admin page, the user is still on other group. Maybe this is a deprecated function and the Joomla didnt use it now.
$user->groups = $GroupJoomla;
$user->save(); //Method two. Unfortunatelly this is always returns to false for me, dont know why. There is no error message. (Apache error log also empty)
print_r ($user->getAuthorisedGroups()); //This is the good to get the user's groups. This is correct.
if ($user->save() === false)
{
throw new Exception($user->getError(), 500); // This gives me this error: Error displaying the error page: Application Instantiation Error: Application Instantiation Error
}
What am I doing wrong? :/
I havent got Joomfish, so it isnt why the $user->save() returns false.
Update:
This code is also returns false on $user->save().
$uid = 1748;
$user =JFactory::getUser($uid); //joe is the username
$GroupJoomla = array(14); //I want to change it's group to 14
$user->set('groups',$GroupJoomla);
$user->groups = $GroupJoomla;
$user->save();
jimport( 'joomla.access.access' );//Call the Access Class
/*If the below code is not there it wont save.*/
if ($user->save() === false){ //This is gives me false somehow
throw new Exception($user->getError(), 500);
}
$groups = JAccess::getGroupsByUser($uid, false);
print_r($groups); exit; //Receives Group id 15 (Not 14)
With both code samples I have got the good user object. $user->get('username'); This gives me the good username. (Joomla 3.6.5)
As #itoctopus said you need to enter id instead of username but also there are some other mistakes that needs to be taken care of like initiating the group variable. It should be
$GroupJoomla = array(14);
if more than one group
$GroupJoomla = array(13,14);
then to get the groups you can use
$groups = JAccess::getGroupsByUser($id, false);
So finally your code will be
<?php
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', 'C:\xampp\htdocs\joomla' );
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' );
$uid = 404;
$user =JFactory::getUser($uid); //joe is the username
$GroupJoomla = array(4,6); //I want to change it's group to 14
$user->set('groups',$GroupJoomla);
$user->groups = $GroupJoomla;
$user->save();
jimport( 'joomla.access.access' );//Call the Access Class
/*If the below code is not there it wont save.*/
if ($user->save() === false){
throw new Exception($user->getError(), 500);
}
$groups = JAccess::getGroupsByUser($uid, false);
print_r($groups); exit;
A quick glimpse at your code, and I think this line:
$user =JFactory::getUser(joe);
Should be:
$user =JFactory::getUser(5);
Where 5 is the id of the user with username "joe". You cannot get a user by username by using the function JFactory::getUser.
Alternatively, if you really want to get a user by username, then you should use:
$userId = JUserHelper::getUserId('joe'); //note that there are quotes around joe (in your code you omitted the quote)
$user =JFactory::getUser($userId);
Hope this helps.

After wordpress upgrade I'm getting wp_user not found in pluggable.php

I am calling a php script from an app that checks the credentials of a wp user. This worked fine until I upgraded wordpress from 4.3 to 4.4 (and 4.5). It's really time I got this sorted but I can't think why wp-user is no longer available as it is in the include list.
error: wp_user not found in pluggable.php
Please see the code below..
define( 'SHORTINIT', TRUE );
require_once $abspath . '/wp-load.php';
require_once $abspath . '/wp-includes/user.php';
require_once $abspath . '/wp-includes/pluggable.php';
require_once $abspath . '/wp-includes/formatting.php';
require_once $abspath . '/wp-includes/capabilities.php';
require_once $abspath . '/wp-includes/kses.php';
require_once $abspath . '/wp-includes/meta.php';
require_once $abspath . '/wp-includes/l10n.php';
require_once $abspath . '/wp-includes/class-wp-error.php';
require_once $abspath . '/wp-includes/general-template.php';
require_once $abspath . '/wp-includes/link-template.php';
$the_authenticate = wp_authenticate_username_password('null',$user_name,$user_password);
if( is_wp_error( $the_authenticate ) ) {
echo '{"error":"The username was not recognised"}';
}
else
{
$the_user_authenticate_id = $the_authenticate->ID;
$the_user = get_user_by('login', $user_name);
$the_user_id = $the_user->ID;
if ( !$the_user )
{
//echo "{'error':'The username was not recognised'}";
}
I have found the solution is that I now need to include the following..
require_once $abspath . '/wp-includes/class-wp-roles.php';
require_once $abspath . '/wp-includes/class-wp-user.php';
require_once $abspath . '/wp-includes/class-wp-role.php';
Please can anyone explain why these extra includes are suddenly necessary in wordpress 4.4 and whether my solution makes sense?
Download the updated wordpress version from wordpress.org and replace your current wp-admin and wp-includes folders with updated wordpress folders.
Hope it will work.

Joomla 3 load template in external file

I have created an external file. I want to load joomla default template in that file. I used below code, and it caused an error:
Fatal error: Call to protected method JApplicationSite::render() from
context ' '
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' );
$mainframe = JFactory::getApplication('site');
JPluginHelper::importPlugin('system');
$mainframe->initialise();
$myContent = "Hello World!!";
$document = JFactory::getDocument();
$document->setBuffer($myContent, 'component');
$mainframe->render();
echo $mainframe;
At the least you need to let the Joomla app know which template you'd like to render.
//normal external script initialisation
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath('/home/somepath/public_html/somedirectory/'));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php');
require_once( JPATH_LIBRARIES .DS.'joomla'.DS.'factory.php');
$app = JFactory::getApplication('site');
$app->initialise();
//jimport( 'joomla.document.html.html' ); required in certain cases
//initialise the doc to use the Html renderer
$doc = new JDocumentHtml();
//minimal info required
$options['directory'] = "/my/template/directory";
$options["template"] = $app->getTemplate();//get the template name
$options["file"] = "index.php";//usually
//Optional: Manually set params
//$params = new JRegistry();
//$params->colorVariation = "blue";
//$params->widthStyle = "fluid";
//$params->showComponent = "1";
//$options["params"] = $params;
//you may initialise the document object here and set the buffers
//$document = JFactory::getDocument();
//$document->setBuffer($contents, 'module', $name);
// $document->setHeadData($data['head']);etc
echo $doc->render(false, $options);
$app->close();
Of course dont expect it to display the same way esp if you use relative urls. Can be used for other purposes though like examining the scripts in a document template. tested on Joomla 3.3 PHP 5.5 . It will not work on lower versions like 1.5.

bootstrapping joomla 2.5 from an external script

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.

Categories