MyBB Community Forums

Full Version: Steam Account Status
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Introduction
This modification will allow you to have users input their Steam ID as a profile field. The upgrade to that, and the modification, is that you will have a cron job automatically update the user's steam account status which will then be displayed on the user's postbit and profile information. 

I would have made this as a plugin, but this is really my first time coming back into the forum suite in about 2 years, and even back then, I hardly knew how to make a plugin. If someone could help with that, or help me learn, that would be great.

Step One
You need to make a couple of new profile fields in MyBB for this to work. So, go to the Admin Panel -> Configuration -> Custom Profile Fields -> Add New Profile Field
[attachment=34621]

You will want to make two profile fields that match the images below. One is for the user to enter a Steam ID, the other the program will write to. The Steam ID is the editable and non-visible field while the Steam Account field is non-editable but visible. Regex is used to only allow numbers to be entered which creates less problems in securing the script. You can name these fields however you want with whatever descriptions and lengths you want. Just be mindful if you choose minimum / maximum lengths.
[attachment=34622]
[attachment=34623]

Step Two
Create a PHP script that will find information based on the user's Steam ID.
You want to save the following code as a PHP file, with whatever name you want. The easiest way to secure the file is to have it put outside your public directory and run it with cron jobs. I created a folder named "cron" under /home/USER where I placed my PHP file.
<?php
define('IN_MYBB', 1);
require '../public_html/forum/global.php';
global $db;

$steamStat = array(
	'0' => 'Offline',
	'1' => 'Online',
	'2' => 'Busy',
	'3' => 'Away',
	'4' => 'Snooze',
	'5' => 'Looking to Trade',
	'6' => 'Looking to Play'
);

$steamCSS = array(
	'0' => 'steamOffline',
	'1' => 'steamOnline',
	'2' => 'steamBusy',
	'3' => 'steamAway',
	'4' => 'steamSnooze',
	'5' => 'steamTrade',
	'6' => 'steamPlay'
);
	
$select = $db->simple_select('userfields', 'ufid, fid4');
while ($result = $db->fetch_array($select)) {
	$steamID = $result['fid4'];
	if ($steamID != '') {
		$uid = $result['ufid'];
		$json_url = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=YOURSTEAMAPIKEY&steamids=' . $steamID;
		$json = file_get_contents($json_url);
		$data = json_decode($json, TRUE);
		$steamStatus = $data['response']['players'][0]['personastate'];
		$steamAcc = '<a href="http://steamcommunity.com/profiles/' . $steamID . '/" title="' . $data['response']['players'][0]['personaname'] . '" class="' . $steamCSS[$steamStatus] . '">' . $steamStat[$steamStatus] . '</a>';
		$update = $db->update_query('userfields', array('fid5' => $steamAcc), 'ufid=' . $uid . '');
	}
}
?>
[attachment=34624]

Step Three
Now that you've uploaded the PHP file to your server, you need to run it via a cron job to indicate what the user's status is. The time period you choose is entirely up to you. Navigate to your cPanel or other software used to run your website. You'll want to find "Cron Jobs" and enter it to add a new one. For most, you simply choose a time period. The command line you will need is
/usr/bin/php -q /home/USER/cron/cron.php >/dev/null
This will run the script at every time period you tell the job to run at. For me, it's every 15 minutes. You're also limited to only 100,000 requests a day unless you get approved for more.

Step Four
The last thing you will need to do is add just a little bit of CSS to make the field look good, and different based on what the user's status is. You will need to navigate to the Themes & Templates -> Default -> Global.css -> Advanced Mode
[attachment=34626]
You will then want to add this code anywhere you want (I placed mine at the bottom). Of course, the colors don't have to be the ones I used, but they look decent on the default forum theme. I'll be changing them once my custom theme is done.
.steamOffline:hover, .steamOffline:active,
.steamOnline:hover, .steamOnline:active,
.steamBusy:hover, .steamBusy:active,
.steamAway:hover, .steamAway:active,
.steamSnooze:hover, .steamSnooze:active,
.steamTrade:hover, .steamTrade:active,
.steamPlay:hover, .steamPlay:active {
	text-decoration: none;
}

.steamOffline,
.steamOnline,
.steamBusy,
.steamAway,
.steamSnooze,
.steamTrade,
.steamPlay {
	text-decoration: none;
	font-weight: bold;
}

a.steamOffline {
	color: rgba(118, 143, 137, 0.95);	
}

a.steamOnline {
	color: rgba(16, 79, 24, 0.95);
}

a.steamBusy {
	color: rgba(130, 19, 16, 0.95);	
}

a.steamAway, a.steamSnooze {
	color: rgba(207, 92, 27, 0.95);	
}

a.steamTrade {
	color: rgba(71, 52, 143, 0.95);	
}

a.steamPlay {
	color: rgba(36, 93, 143, 0.95);	
}

That's it! You don't have to use the same colors, or place the CSS in the same spot, or even put the PHP folder in the same spot as long as you secure your cron jobs and link to everything properly. But, this should have been a pretty easy tutorial.

End Result
[attachment=34625]
Very, very nice tut.

Congrats ^^.
Thanks!

It's a good start to getting back into MyBB, realizing how much I've missed and information I have forgotten. It's also a good start into new areas of playing around as well.
I can't figure out what to run for cron in zpanel or were to place the cron folder.
Pming you my directory, thanks!!
I have a question.
Can I run this through the mybb task system?
If you put the cron.php file in the /inc/tasks folder, then you should be able to with the task manager. The only reason I hold back from that is that I generally put task (cron) files outside the public directory so that they are protected and are only executed when they are told to.
This is a little late but what would be the code I would put into the postbit template for this to work, because with almost every plugin I install on this theme I have to manually code things in.
(2015-08-13, 11:58 PM)Almighty™ Wrote: [ -> ]This is a little late but what would be the code I would put into the postbit template for this to work, because with almost every plugin I install on this theme I have to manually code things in.

Have you figured this out by chance?
You should have a copy of the default theme; look to see in the default templates to see where the code is added/what is the variable.
Doesn't work for me Sad
Pages: 1 2