MyBB Community Forums

Full Version: Who's Online hooks
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I was just making the who's online function for a plugin which hooks into the misc.php file. So I have to look with the $parameters variable. But, the problem is that you can't request that variable. It's made in the function called fetch_wol_activity(), and this means it isn't available in an other plugin, except with the follow file-change:

Open inc/functions_online.php and find:
function fetch_wol_activity($location)
{
	global $uid_list, $aid_list, $pid_list, $tid_list, $fid_list, $eid_list, $plugins, $user;

Replace with:
function fetch_wol_activity($location)
{
	global $uid_list, $aid_list, $pid_list, $tid_list, $fid_list, $eid_list, $plugins, $user, $parameters;

Possible to include this by default?
Found a second problem:
In the build_friendly_wol_location() function, the needed threads are loaded, and that works perfect. You have chosen for static variables, so the threads are only loaded once.

But, a static variable isn't available in an other function/class. This means you can't use it in an other plugin.

To fix it, open inc/functions_online.php and find:
function build_friendly_wol_location($user_activity, $return=false)
{
	global $db, $lang, $uid_list, $aid_list, $pid_list, $tid_list, $fid_list, $eid_list, $plugins, $parser, $mybb;
	static $threads, $forums, $forums_linkto, $posts, $events, $users, $attachments;

Replace with:
function build_friendly_wol_location($user_activity, $return=false)
{
	global $db, $lang, $uid_list, $aid_list, $pid_list, $tid_list, $fid_list, $eid_list, $plugins, $parser, $mybb;
	global $threads, $forums, $forums_linkto, $posts, $events, $users, $attachments;

Or, you can add them in the array passed to the hook (L 865).
For the first one, you could either regenerate the $parameters, or just check the $user_activity['location'] variable.

For the second one, is there really any need? If you want to load an extra thread, add it to $tid_list.
ZiNgA BuRgA, the first one could be done in fact, true.

The Second one: the $threads value contains the loaded threads, produced by the $tid_list, which means it had to be every time reloaded.
I'm sorry, but I have to say a similar thing, again.

Using one of the refreshes, I've made the Who's online compatibility of the Game Section, and I found then some problems, which I fixed and reported.

Yesterday (and now again), I've mentioned some changes in the code, and now, I was updating the code for this. But the problem was that I could get access to the $filename variable, but I didn't want to report again such a thing, and so I tested it by making that var in my code, too. But the problem is then that the $location variable isn't accessible, too.

So I would ask if it's possible to do this code for MyBB 1.4.1:
Open inc/functions_online.php and find:
function fetch_wol_activity($location)
{
	global $uid_list, $aid_list, $pid_list, $tid_list, $fid_list, $eid_list, $plugins, $user, $parameters;

Replace with:
function fetch_wol_activity($location)
{
	global $uid_list, $aid_list, $pid_list, $tid_list, $fid_list, $eid_list, $plugins, $user, $parameters, $filename, $location;

EDIT: Maybe this thread would be better placed in Suggestions and Feedback?
The above gives some errors, just do this:
Open inc/functions_online.php and find:
function fetch_wol_activity($location)
{
	global $uid_list, $aid_list, $pid_list, $tid_list, $fid_list, $eid_list, $plugins, $user, $parameters;

Replace with:
function fetch_wol_activity($location)
{
	global $uid_list, $aid_list, $pid_list, $tid_list, $fid_list, $eid_list, $plugins, $user, $parameters, $filename;

Otherwise the whole system goes wrong.

But, I see the $location is given, and so it's usable.
	$user_activity['location'] = $location;

And to do like with the location value, you could do this, too:
	$user_activity['location'] = $location;
	$user_activity['filename'] = $filename;
(2008-08-03, 06:51 PM)Paretje Wrote: [ -> ]The Second one: the $threads value contains the loaded threads, produced by the $tid_list, which means it had to be every time reloaded.
How so? It's cached into static variables.

(2008-08-03, 06:51 PM)Paretje Wrote: [ -> ]But, I see the $location is given, and so it's usable.
	$user_activity['location'] = $location;

And to do like with the location value, you could do this, too:
	$user_activity['location'] = $location;
	$user_activity['filename'] = $filename;
You can regenerate the filename from the supplied location, and the global'd $user variable.
The filename may be sent through the parameter at a later stage, but it's not really a priority (or exactly necessary).

Hope that helps.
(2008-08-04, 07:14 AM)ZiNgA BuRgA Wrote: [ -> ]
(2008-08-03, 06:51 PM)Paretje Wrote: [ -> ]The Second one: the $threads value contains the loaded threads, produced by the $tid_list, which means it had to be every time reloaded.
How so? It's cached into static variables.

Static variables are only accessible in the same function/class. That means the fucntion doesn't have to rebuild it, but out of the function/class, it isn't accessible.

(2008-08-03, 06:51 PM)Paretje Wrote: [ -> ]But, I see the $location is given, and so it's usable.
	$user_activity['location'] = $location;

And to do like with the location value, you could do this, too:
	$user_activity['location'] = $location;
	$user_activity['filename'] = $filename;
You can regenerate the filename from the supplied location, and the global'd $user variable.
The filename may be sent through the parameter at a later stage, but it's not really a priority (or exactly necessary).

Hope that helps.
[/quote]

That is what I meant with the first line you quoted: I updated my file-change, and then I said I saw it is accessible, and showed the code which does that. After that, I said you could do it with that principle, too.

So, it's possible, indeed, but for an unclear reason I hadn't seen that piece of code, it's strange, but true.
(2008-08-04, 08:52 AM)Paretje Wrote: [ -> ]
(2008-08-04, 07:14 AM)ZiNgA BuRgA Wrote: [ -> ]
(2008-08-03, 06:51 PM)Paretje Wrote: [ -> ]The Second one: the $threads value contains the loaded threads, produced by the $tid_list, which means it had to be every time reloaded.
How so? It's cached into static variables.

Static variables are only accessible in the same function/class. That means the fucntion doesn't have to rebuild it, but out of the function/class, it isn't accessible.
My question was, do you really need to access them outside of the function? If so, could you provide an example?
I don't want to sound anal, but I somewhat can't think of a purpose for such a thing...
(2008-08-05, 06:53 AM)ZiNgA BuRgA Wrote: [ -> ]
(2008-08-04, 08:52 AM)Paretje Wrote: [ -> ]
(2008-08-04, 07:14 AM)ZiNgA BuRgA Wrote: [ -> ]
(2008-08-03, 06:51 PM)Paretje Wrote: [ -> ]The Second one: the $threads value contains the loaded threads, produced by the $tid_list, which means it had to be every time reloaded.
How so? It's cached into static variables.

Static variables are only accessible in the same function/class. That means the fucntion doesn't have to rebuild it, but out of the function/class, it isn't accessible.
My question was, do you really need to access them outside of the function? If so, could you provide an example?
I don't want to sound anal, but I somewhat can't think of a purpose for such a thing...

Yes, I have. This is the Whos online code of my attachmentspopup plugin:
function attachmentspopup_online_activity($user_activity)
{
	global $parameters, $tid_list;
	
	if($user_activity['activity'] == "misc" && $parameters['action'] == "attachmentspopup")
	{
		if(is_numeric($parameters['tid']))

		{

			$tid_list[] = $parameters['tid'];

		}
		
		$user_activity['activity'] = "misc_attachmentspopup";

		$user_activity['tid'] = $parameters['tid'];
		
		return $user_activity;
	}
}

function attachmentspopup_online_location($plugin_array)
{
	global $lang, $threads;
	
	if($plugin_array['user_activity']['activity']['activity'] == "misc_attachmentspopup")
	{
		//Load language
		$lang->load("attachmentspopup");
		$plugin_array['location_name'] = $lang->sprintf($lang->viewing_attachmentspopup, $plugin_array['user_activity']['tid'], $threads[1]);		
		
		return $plugin_array;
	}
}

Then you get (for example):
"Viewing Attachments of Test Thread 1"

But that is included in MyBB 1.4 final. The code of the filename isn't, but that isn't necessary, you can recount that in your plugin.
Hmm, I see what you're trying to do there. There's a number of solutions, but exposing the cached threadslist is really ugly, which is why I'm trying to avoid it.
1) Use your own caching system - ie, don't use tid, name it something like attachtid. This will require an extra query if there are people viewing a thread and the attachment popup (not really an issue IMO)
2) Forge the location to make MyBB think the user is viewing a thread, then fix up everything in pre_output_page; this may be a little tricky to code
3) Gain control of various objects and inject code into the function - this is also a little tricky (and not too sure if this is possible)

If we do expose those variables to plugins, it'll most likely be through the array, and passed by reference.