Skip to content

Commit

Permalink
Merge pull request phpbb#962 from imkingdavid/feature/add_events
Browse files Browse the repository at this point in the history
Feature/add events
  • Loading branch information
naderman committed Sep 1, 2012
2 parents c539c2b + 02644c0 commit 43190eb
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 5 deletions.
34 changes: 34 additions & 0 deletions phpBB/includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2231,13 +2231,47 @@ function phpbb_on_page($template, $user, $base_url, $num_items, $per_page, $star
function append_sid($url, $params = false, $is_amp = true, $session_id = false)
{
global $_SID, $_EXTRA_URL, $phpbb_hook;
global $phpbb_dispatcher;

if ($params === '' || (is_array($params) && empty($params)))
{
// Do not append the ? if the param-list is empty anyway.
$params = false;
}

$append_sid_overwrite = false;

/**
* This event can either supplement or override the append_sid() function
*
* To override this function, the event must set $append_sid_overwrite to
* the new URL value, which will be returned following the event
*
* @event core.append_sid
* @var string url The url the session id needs
* to be appended to (can have
* params)
* @var mixed params String or array of additional
* url parameters
* @var bool is_amp Is url using & (true) or
* & (false)
* @var bool|string session_id Possibility to use a custom
* session id (string) instead of
* the global one (false)
* @var bool|string append_sid_overwrite Overwrite function (string
* URL) or not (false)
* @since 3.1-A1
*/
$vars = array('url', 'params', 'is_amp', 'session_id', 'append_sid_overwrite');
extract($phpbb_dispatcher->trigger_event('core.append_sid', compact($vars)));

if ($append_sid_overwrite)
{
return $append_sid_overwrite;
}

// The following hook remains for backwards compatibility, though use of
// the event above is preferred.
// Developers using the hook function need to globalise the $_SID and $_EXTRA_URL on their own and also handle it appropriately.
// They could mimic most of what is within this function
if (!empty($phpbb_hook) && $phpbb_hook->call_hook(__FUNCTION__, $url, $params, $is_amp, $session_id))
Expand Down
120 changes: 115 additions & 5 deletions phpBB/includes/functions_content.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,33 @@ function strip_bbcode(&$text, $uid = '')
function generate_text_for_display($text, $uid, $bitfield, $flags)
{
static $bbcode;
global $phpbb_dispatcher;

if (!$text)
{
return '';
}

$text = censor_text($text);
$censor_text = true;

/**
* Use this event to modify the text before it is parsed
*
* @event core.modify_text_for_display_before
* @var string text The text to parse
* @var string uid The BBCode UID
* @var string bitfield The BBCode Bitfield
* @var int flags The BBCode Flags
* @var bool censor_text Whether or not to apply word censors
* @since 3.1-A1
*/
$vars = array('text', 'uid', 'bitfield', 'flags', 'censor_text');
extract($phpbb_dispatcher->trigger_event('core.modify_text_for_display_before', compact($vars)));

if ($censor_text)
{
$text = censor_text($text);
}

// Parse bbcode if bbcode uid stored and bbcode enabled
if ($uid && ($flags & OPTION_FLAG_BBCODE))
Expand All @@ -443,6 +463,19 @@ function generate_text_for_display($text, $uid, $bitfield, $flags)
$text = bbcode_nl2br($text);
$text = smiley_text($text, !($flags & OPTION_FLAG_SMILIES));

/**
* Use this event to modify the text after it is parsed
*
* @event core.modify_text_for_display_after
* @var string text The text to parse
* @var string uid The BBCode UID
* @var string bitfield The BBCode Bitfield
* @var int flags The BBCode Flags
* @since 3.1-A1
*/
$vars = array('text', 'uid', 'bitfield', 'flags');
extract($phpbb_dispatcher->trigger_event('core.modify_text_for_display_after', compact($vars)));

return $text;
}

Expand All @@ -453,7 +486,23 @@ function generate_text_for_display($text, $uid, $bitfield, $flags)
*/
function generate_text_for_storage(&$text, &$uid, &$bitfield, &$flags, $allow_bbcode = false, $allow_urls = false, $allow_smilies = false)
{
global $phpbb_root_path, $phpEx;
global $phpbb_root_path, $phpEx, $phpbb_dispatcher;

/**
* Use this event to modify the text before it is prepared for storage
*
* @event core.modify_text_for_storage_before
* @var string text The text to parse
* @var string uid The BBCode UID
* @var string bitfield The BBCode Bitfield
* @var int flags The BBCode Flags
* @var bool allow_bbcode Whether or not to parse BBCode
* @var bool allow_urls Whether or not to parse URLs
* @var bool allow_smilies Whether or not to parse Smilies
* @since 3.1-A1
*/
$vars = array('text', 'uid', 'bitfield', 'flags', 'allow_bbcode', 'allow_urls', 'allow_smilies');
extract($phpbb_dispatcher->trigger_event('core.modify_text_for_storage_before', compact($vars)));

$uid = $bitfield = '';
$flags = (($allow_bbcode) ? OPTION_FLAG_BBCODE : 0) + (($allow_smilies) ? OPTION_FLAG_SMILIES : 0) + (($allow_urls) ? OPTION_FLAG_LINKS : 0);
Expand Down Expand Up @@ -482,6 +531,19 @@ function generate_text_for_storage(&$text, &$uid, &$bitfield, &$flags, $allow_bb

$bitfield = $message_parser->bbcode_bitfield;

/**
* Use this event to modify the text after it is prepared for storage
*
* @event core.modify_text_for_storage_after
* @var string text The text to parse
* @var string uid The BBCode UID
* @var string bitfield The BBCode Bitfield
* @var int flags The BBCode Flags
* @since 3.1-A1
*/
$vars = array('text', 'uid', 'bitfield', 'flags');
extract($phpbb_dispatcher->trigger_event('core.modify_text_for_storage_after', compact($vars)));

return;
}

Expand All @@ -491,10 +553,33 @@ function generate_text_for_storage(&$text, &$uid, &$bitfield, &$flags, $allow_bb
*/
function generate_text_for_edit($text, $uid, $flags)
{
global $phpbb_root_path, $phpEx;
global $phpbb_root_path, $phpEx, $phpbb_dispatcher;

/**
* Use this event to modify the text before it is decoded for editing
*
* @event core.modify_text_for_edit_before
* @var string text The text to parse
* @var string uid The BBCode UID
* @var int flags The BBCode Flags
* @since 3.1-A1
*/
$vars = array('text', 'uid', 'flags');
extract($phpbb_dispatcher->trigger_event('core.modify_text_for_edit_before', compact($vars)));

decode_message($text, $uid);

/**
* Use this event to modify the text after it is decoded for editing
*
* @event core.modify_text_for_edit_after
* @var string text The text to parse
* @var int flags The BBCode Flags
* @since 3.1-A1
*/
$vars = array('text', 'flags');
extract($phpbb_dispatcher->trigger_event('core.modify_text_for_edit_after', compact($vars)));

return array(
'allow_bbcode' => ($flags & OPTION_FLAG_BBCODE) ? 1 : 0,
'allow_smilies' => ($flags & OPTION_FLAG_SMILIES) ? 1 : 0,
Expand Down Expand Up @@ -1175,6 +1260,7 @@ function truncate_string($string, $max_length = 60, $max_store_length = 255, $al
function get_username_string($mode, $user_id, $username, $username_colour = '', $guest_username = false, $custom_profile_url = false)
{
static $_profile_cache;
global $phpbb_dispatcher;

// We cache some common variables we need within this function
if (empty($_profile_cache))
Expand Down Expand Up @@ -1252,10 +1338,34 @@ function get_username_string($mode, $user_id, $username, $username_colour = '',

if (($mode == 'full' && !$profile_url) || $mode == 'no_profile')
{
return str_replace(array('{USERNAME_COLOUR}', '{USERNAME}'), array($username_colour, $username), (!$username_colour) ? $_profile_cache['tpl_noprofile'] : $_profile_cache['tpl_noprofile_colour']);
$username_string = str_replace(array('{USERNAME_COLOUR}', '{USERNAME}'), array($username_colour, $username), (!$username_colour) ? $_profile_cache['tpl_noprofile'] : $_profile_cache['tpl_noprofile_colour']);
}
else
{
$username_string = str_replace(array('{PROFILE_URL}', '{USERNAME_COLOUR}', '{USERNAME}'), array($profile_url, $username_colour, $username), (!$username_colour) ? $_profile_cache['tpl_profile'] : $_profile_cache['tpl_profile_colour']);
}

/**
* Use this event to change the output of get_username_string()
*
* @event core.modify_username_string
* @var string mode profile|username|colour|full|no_profile
* @var int user_id String or array of additional url
* parameters
* @var string username The user's username
* @var string username_colour The user's colour
* @var string guest_username Optional parameter to specify the
* guest username.
* @var string custom_profile_url Optional parameter to specify a
* profile url.
* @var string username_string The string that has been generated
* @var array _profile_cache Array of original return templates
* @since 3.1-A1
*/
$vars = array('mode', 'user_id', 'username', 'username_colour', 'guest_username', 'custom_profile_url', 'username_string', '_profile_cache');
extract($phpbb_dispatcher->trigger_event('core.modify_username_string', compact($vars)));

return str_replace(array('{PROFILE_URL}', '{USERNAME_COLOUR}', '{USERNAME}'), array($profile_url, $username_colour, $username), (!$username_colour) ? $_profile_cache['tpl_profile'] : $_profile_cache['tpl_profile_colour']);
return $username_string;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions phpBB/includes/functions_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ function user_update_name($old_name, $new_name)
function user_add($user_row, $cp_data = false)
{
global $db, $user, $auth, $config, $phpbb_root_path, $phpEx;
global $phpbb_dispatcher;

if (empty($user_row['username']) || !isset($user_row['group_id']) || !isset($user_row['user_email']) || !isset($user_row['user_type']))
{
Expand Down Expand Up @@ -255,6 +256,16 @@ function user_add($user_row, $cp_data = false)
}
}

/**
* Use this event to modify the values to be inserted when a user is added
*
* @event core.user_add_modify_data
* @var array sql_ary Array of data to be inserted when a user is added
* @since 3.1-A1
*/
$vars = array('sql_ary');
extract($phpbb_dispatcher->trigger_event('core.user_add_modify_data', compact($vars)));

$sql = 'INSERT INTO ' . USERS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
$db->sql_query($sql);

Expand Down
43 changes: 43 additions & 0 deletions phpBB/posting.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,41 @@
$error = $post_data = array();
$current_time = time();

/**
* This event allows you to alter the above parameters, such as submit and mode
*
* Note: $refresh must be true to retain previously submitted form data.
*
* Note: The template class will not work properly until $user->setup() is
* called, and it has not been called yet. Extensions requiring template
* assignments should use an event that comes later in this file.
*
* @event core.modify_posting_parameters
* @var int post_id ID of the post
* @var int topic_id ID of the topic
* @var int forum_id ID of the forum
* @var int draft_id ID of the draft
* @var int lastclick Timestamp of when the form was last loaded
* @var bool submit Whether or not the form has been submitted
* @var bool preview Whether or not the post is being previewed
* @var bool save Whether or not a draft is being saved
* @var bool load Whether or not a draft is being loaded
* @var bool delete Whether or not the post is being deleted
* @var bool cancel Whether or not to cancel the form (returns to
* viewtopic or viewforum depending on if the user
* is posting a new topic or editing a post)
* @var bool refresh Whether or not to retain previously submitted data
* @var string mode What action to take if the form has been sumitted
* post|reply|quote|edit|delete|bump|smilies|popup
* @var array error Any error strings; a non-empty array aborts
* form submission.
* NOTE: Should be actual language strings, NOT
* language keys.
* @since 3.1-A1
*/
$vars = array('post_id', 'topic_id', 'forum_id', 'draft_id', 'lastclick', 'submit', 'preview', 'save', 'load', 'delete', 'cancel', 'refresh', 'mode', 'error');
extract($phpbb_dispatcher->trigger_event('core.modify_posting_parameters', compact($vars)));

// Was cancel pressed? If so then redirect to the appropriate page
if ($cancel || ($current_time - $lastclick < 2 && $submit))
{
Expand Down Expand Up @@ -1417,6 +1452,14 @@
'S_HIDDEN_FIELDS' => $s_hidden_fields)
);

/**
* This event allows you to modify template variables for the posting screen
*
* @event core.posting_modify_template_vars
* @since 3.1-A1
*/
$phpbb_dispatcher->trigger_event('core.posting_modify_template_vars');

// Build custom bbcodes array
display_custom_bbcodes();

Expand Down
3 changes: 3 additions & 0 deletions tests/session/append_sid_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public function append_sid_data()
*/
public function test_append_sid($url, $params, $is_amp, $session_id, $expected, $description)
{
global $phpbb_dispatcher;

$phpbb_dispatcher = new phpbb_mock_event_dispatcher;
$this->assertEquals($expected, append_sid($url, $params, $is_amp, $session_id));
}
}
Expand Down

0 comments on commit 43190eb

Please sign in to comment.