Bonfire provides a number of resources that are loaded globally and are intended to make life a little easier for you. Most of these are loaded from the application/helpers/application_helper.php file.
$current_user
If a user is logged in, you can access the current user's detail within any of your controllers that extend Bonfire's provided controllers, like Front_Controller, Admin_Controller, etc. This is accessed by $this->current_user
.
This is an object with the user's details:
stdclass Object
{
[id] => 1
[role_id] => 1
[email] => darth@empire.com
[username] => Darth Vader
[password_hash] => ...
[reset_hash] => ...
[last_login] =>
[last_ip] =>
[created_on] =>
[deleted] => 0
[banned] => 0
[ban_message] =>
[reset_by] =>
[display_name] => Darth
[display_name_changed] =>
[timezone] => UM6
[language] =>
[active] => 1
[activate_hash] =>
[password_iterations] => 8
[force_password_reset] => 0
[role_name] => Administrator
[user_img] => (url for their avatar)
}
The exact details may change over the versions, but the entire users
table record is provided to you.
This same data is made available within all of your views as $current_user
.
<?php echo $current_user->display_name ?>
e()
A convenience function that can (and probably should) be used to replace echo()
in any places that you output text that a user might have entered. This function helps to defeat XSS attacks by running the text through htmlspecialchars()
.
<?php e($user->display_name); ?>
js_escape()
Performs some simple escaping of strings that will be inserted into javascript functions, like confirm()
or alert()
. Not suitable for use by itself within document.write. The only parameter is the string to escape.
dump()
Outputs the given variables with formatting and location. Any number of parameters can be passed to this function and they will all be printed out in a helpful manner, but script execution will continue.
gravatar_link()
Returns a URL for the user from Gravatar and provides a default image (the identicon) when the user hasn't provided one. This is intended to be used within your views for displaying the user's avatar.
<?php echo gravatar_image($current_user->email, 48, $current_user->display_name, $current_user->display_name, $class, $id); ?>
// Returns:
<img src="http://gravatar.com/avatar/XXX?s=48&r=pg&d=identicon" width="48" height="48" alt="" title="" />
The first parameter is the email address for the user. This is how they are referenced at Gravatar. The second parameter is the size of the image in pixels. The third parameter is the alt tag and the fourth parameter is the title for the tag. The fifth and sixth parameters are the class and id to be given to the img, respectively.
logit()
Logs an error to the Console in the profiler (if loaded) and to the log files.
logit('Some debugging message', 'debug');
The first parameter is the message to log. The second parameter is the log level.
If the profiler is enabled and the Console class is loaded, which it is by default in development mode, the string will show up in the Console section of the profiler.
If logging is enabled in your application/config/config.php
file, then it will also be logged to your default log file.
is_https()
This function was imported from CI v3. Returns true if a secure (HTTPS) connection is used. Otherwise, it returns false (including in the case of non-HTTP requests).
This is commonly used to ensure links generated by the site use the same protocol as is currently being used to access the site.
$httpProtocol = is_https() ? 'https://secure.' : 'http://www.';
$gravatarUrl = "{$httpProtocol}gravatar.com/";