Joomla! Discussion Forums



It is currently Fri Nov 27, 2009 12:47 pm (All times are UTC )

 




Post new topic Reply to topic  [ 6 posts ] 
Author Message
Posted: Mon Nov 02, 2009 8:56 pm 
Joomla! Fledgling
Joomla! Fledgling
Offline

Joined: Mon Nov 02, 2009 10:02 am
Posts: 3
How do I check if a user is logged in either on 'site' or 'administrator' in an "external" php file?

After a lot of searching and reading I found the core of my current solution here http://forum.joomla.org/viewtopic.php?f=304&t=263491 (the 1.5 solution)

However it doesn't work as I want because I want to check if the user is logged in either in the frontend or the backend not only the front end. The check must be done in the same php file. Why doesn't it work? I think it's because of the singletons, the app/user and many other objects are loaded once and then cached. But maybe I'm wrong.
The code below works as expected if I'm logged in to the frontend ('site') if I'm logged in to the backend ('administrator') then it fails (access denied). If I'm logged in to the backend ('administrator') and I change the order in the if statement so the check_login('administrator') is first then it works as expected for the backend too (but not for the front end anymore...). So the parts of the solution works if used independently, but not in the same request.

Is there another way to achieve this without using the singeltons, but using regular joomla code?

(I have tried to extract code from within the singletons in the joomla code and use that directly, but not successfully. It's quite hard to follow the code deep down into the framework and there might be a number of singletons conspiring against me. )

Is it possible to instruct php in the middle of executing a php file to forget and start fresh again, at least forget static variables inside of functions?


1) I use joomla 1.5 (1.5.14), please state the version your solution works on. (You may post solutions for 1.0, others may be interested)
2) I would like the solution to work without changing anything in joomla. Not even through configuration. Remember it must work if the user is logged in into the front end or the back end.


Code:
<?php
// filehandler.php place the file in the joomla root directory
// http://yourhost+joomla_base_dir/filehandler.php   test by accessing the url.

// With the help of mod_rewrite a call to a url like this
// http://yourhost+joomla_base_dir/images/files/private/test.pdf
// will go through handler.php first, but it will be transparent to the user.

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' );
   
function check_login($app){
   $mainframe =& JFactory::getApplication($app);
   $mainframe->initialise();
   $user =& JFactory::getUser();
   return ( (int)$user->id==0 )? FALSE:TRUE;
}

//if (check_login('administrator') || check_login('site') ) { // admin first
if (check_login('site') || check_login('administrator') ) {   // site first
   // load and output file here
   echo "Access granted";// for testing purposes
}
else {
   // access denied message
   echo "Access denied";
}
?>


Reason: The reason I want to do this is because I want a simple transparent file access check. I use mod_rewrite together with a handler.php file. Basically I create a folder in the filesystem called 'private' and files put in there are checked on access.
I know there are extensions that will check access on files but I want something that works with the standard joomla filemanagers. I have tried several extensions, some doesn't do real access check on the files only on the associated fileinfo. Some extensions are to big and complex (but would really be useful for some projects). Some have usability issues. Some require replacement components/modules for existing functionality and so on. Most require retraining of users.

Any help is appreciated.


Top
  E-mail  
 
Posted: Tue Nov 03, 2009 3:02 am 
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Wed Feb 11, 2009 3:32 am
Posts: 169
Location: Malaysia
Hi,

You can try to create Plugin - User. I just tried at my end, it can be work at frontend and also backend. I am using Joomla 1.5.14.

Code:
<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport('joomla.plugin.plugin');

class plgUserUserAccess extends JPlugin
{
   function plgUserUserAccess(& $subject, $config) {
      parent::__construct($subject, $config);
   }

   function onLoginUser($user, $options = array())
   {
      jimport('joomla.user.helper');

      // Register the needed session variables
      $session =& JFactory::getSession();

      // Get the session object
      $table = & JTable::getInstance('session');
      $table->load( $session->getId() );

      //If login to backend client_id will be 1
      //If login to frontend client_id will be 0
      if($table->client_id == 1) { //backend
         //enter your code here
      } else {  //frontend
         //enter your code here
      }
   }
}

?>

_________________
Regards,
YB


Top
  E-mail  
 
Posted: Tue Nov 03, 2009 6:28 pm 
Joomla! Fledgling
Joomla! Fledgling
Offline

Joined: Mon Nov 02, 2009 10:02 am
Posts: 3
Reply to post by ybong
Thank you for the time spent replying to my post.

I created the plugin. I have confirmed that the plugin is active and working.
In the plugin I tried to set a session variable. I'm not sure what you intended I should set there? Something must be set there so I can retrieve it in the filehandler.php

in the onLoginUser function in the plugin I added the following
Code:
$session->set('jlogged_in',1);

Why? because I need information in my external file that the user is logged in.

Code:
<?php
// filehandler.php place the file in the joomla root directory
// http://yourhost+joomla_base_dir/filehandler.php   test by accessing the url.
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' );

// I must initialise and call getApplication or else I get no result, but getApplication requires an argument.
// if I only knew if the user was a frontend user or backend user the I could initalise the correct application.
$app1 =& JFactory::getApplication('administrator');
$app1->initialise();
$admin_session = &JFactory::getSession();

// same problem as in my first post. 'site' isn't loaded it's still 'administrator'...
$app2 =& JFactory::getApplication('site');
$app2->initialise();
$site_session = &JFactory::getSession();

if ($admin_session->get('jlogged_in') == 1) {
   echo "Access granted";// for testing purposes
} else if ($site_session->get('jlogged_in') == 1) {
   echo "Access granted";// for testing purposes
} else {
   echo "Access denied"; ;// for testing purposes
}
?>

The session doesn't seem to be stored in the same 'place' if the user logged into the frontend or the backend. So I can't make it work with session.

You said It worked on your site? What did you add in the plugin? What did you add in the "filehandler.php" file (or what your external file is called)? Please explain in greater detail exactly what you did. I probably missunderstand you.

Currently this proposed solution is a step back, because I get the same result as with my solution in my first post but now I have added more code (a plugin) too.

a) I'm not sure I explained my issue enough.
b) I didn't understand what you proposed I should do.
c) You didn't understand my problem.

To reiterate the reason to my post:
I configured apache with mod_rewrite in my .htaccess file. This part works. If a request comes in to: http://myjoomlasite/images/files/private/top_secret.pdf the request will be rewritten and transparently sent to the filehandler.php like this: filehandler.php?file?=/images/files/private/top_secret.pdf
the filehandler.php writes the top_secret.pdf as a response if the request comes from a logged in user. Any user as long they are logged in. And regardless if they logged in from the front end or the back end.

mod_rewrite works
output of the file as a response works.
The only part that doesn't work is the OR part in the if statement in my first post.

An other way around the problem would be if I in my filehandler.php could peek somewhere and find out if the user logged in via the frontend or the backend. But how and where do I peek to find out which application the user is using?

added:
I found this regarding http://docs.joomla.org/JFactory/getApplication
the example on that page but I can't get that to work. When I try to run it I get

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 71 bytes) in /path_to_joomla/libraries/joomla/error/exception.php on line 117

I must add a argument to getApplication to avoid an error...


Top
  E-mail  
 
Posted: Wed Nov 04, 2009 4:29 am 
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Wed Feb 11, 2009 3:32 am
Posts: 169
Location: Malaysia
Hi kalmaritm,

Sorry if I misunderstand you. This is what I did,

In the plugin function onLoginUser()

Code:
function onLoginUser($user, $options = array())
   {
      jimport('joomla.user.helper');

      // Register the needed session variables
      $session =& JFactory::getSession();

      // Get the session object
      $table = & JTable::getInstance('session');
      $table->load( $session->getId() );

      if( $table->client_id == 1) {
         $session->set('admin_user', 1);
      } else {
         $session->set('admin_user', 0);
      }

      require_once(JPATH_ROOT.DS.'filehandler.php');
   }


In filehandler.php
Code:
<?php
$session = JFactory::getSession();

if ($session->get('admin_user') == 1) {
   echo "Access granted admin";// for testing purposes
} else if ($session->get('admin_user') == 0) {
   echo "Access granted site";// for testing purposes
} else {
   echo "Access denied"; ;// for testing purposes
}
?>


If you call directly to the filehandler.php file (localhost/filehandler.php), I am afraid that this might cause conflict if there are many user login to your website. Correct me if I am wrong.

_________________
Regards,
YB


Top
  E-mail  
 
Posted: Thu Nov 05, 2009 10:01 pm 
Joomla! Fledgling
Joomla! Fledgling
Offline

Joined: Mon Nov 02, 2009 10:02 am
Posts: 3
Ybong Thank you again for you effort! Now I get what you did. Your solution may be the answer to someone elses problem just not my problem. One positive thing is that I learned about how to write a user plugin. That could come in handy one day.

I don't think that many users logged in to the site would make it more problematic if accessing the filehandler.php. A session should be unique for each user.

I still hope I can find a simple solution to my problem.


Top
  E-mail  
 
Posted: Fri Nov 06, 2009 3:25 am 
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Wed Feb 11, 2009 3:32 am
Posts: 169
Location: Malaysia
Welcome. Hope that other expert could solve your problem.

_________________
Regards,
YB


Top
  E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

Quick reply

 



Who is online

Users browsing this forum: dark_element, nailson_imgn, profc, renuka1 and 36 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group