1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en" xml:lang="en">
5
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
6
<meta http-equiv="content-style-type" content="text/css" />
7
<meta http-equiv="content-language" content="en" />
8
<meta http-equiv="imagetoolbar" content="no" />
9
<meta name="resource-type" content="document" />
10
<meta name="distribution" content="global" />
11
<meta name="copyright" content="2007 phpBB Group" />
12
<meta name="keywords" content="" />
13
<meta name="description" content="Hook System explanation" />
14
<title>phpBB3 • Hook System</title>
16
<style type="text/css">
20
The original "prosilver" theme for phpBB3
21
Created by subBlue design :: http://www.subBlue.com
24
* { margin: 0; padding: 0; }
26
html { font-size: 100%; height: 100%; margin-bottom: 1px; }
29
font-family: Verdana, Helvetica, Arial, sans-serif;
31
background-color: #FFFFFF;
37
img { border-width: 0; }
46
border: 0 none #FFFFFF;
47
border-top: 1px solid #CCCCCC;
56
background-color: #FFFFFF;
60
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
70
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
74
margin: 0.8em 0 0.2em 0;
78
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
82
margin: 0.8em 0 0.2em 0;
86
font-family: Arial, Helvetica, sans-serif;
88
border-bottom: 1px solid #CCCCCC;
96
.good { color: green; }
104
border-top: 1px solid #ccc;
110
font-family: 'Courier New', monospace;
111
border-color: #D1D7DC;
114
background-color: #FAFAFA;
138
padding: 10px 13px 0 10px;
142
text-decoration: none;
150
#doc-description h1 {
155
background: #ebebeb none repeat-x 0 0;
161
span.corners-top, span.corners-bottom, span.corners-top span, span.corners-bottom span {
166
background-repeat: no-repeat;
170
background-image: none;
171
background-position: 0 0;
175
span.corners-top span {
176
background-image: none;
177
background-position: 100% 0;
180
span.corners-bottom {
181
background-image: none;
182
background-position: 0 100%;
187
span.corners-bottom span {
188
background-image: none;
189
background-position: 100% 100%;
195
background-repeat: no-repeat;
196
background-position: 100% 0;
197
background-color: #ECF3F7;
200
.paragraph:target .content {
204
.paragraph:target h3 a {
212
.content h2, .panel h2 {
214
border-bottom-color: #CCCCCC;
217
a:link { color: #898989; text-decoration: none; }
218
a:visited { color: #898989; text-decoration: none; }
219
a:hover { color: #d3d3d3; text-decoration: underline; }
220
a:active { color: #d2d2d2; text-decoration: none; }
223
border-color: #FFFFFF;
224
border-top-color: #CCCCCC;
228
background-color: #cadceb;
232
background-color: #12A3EB;
233
background-image: url("bg_header.gif");
238
background-color: #ECF1F3;
244
background-image: url("corners_left.png");
247
span.corners-top span {
248
background-image: url("corners_right.png");
251
span.corners-bottom {
252
background-image: url("corners_left.png");
255
span.corners-bottom span {
256
background-image: url("corners_right.png");
263
a:link { color: #105289; }
264
a:visited { color: #105289; }
265
a:hover { color: #D31141; }
266
a:active { color: #368AD2; }
268
.paragraph span.corners-top, .paragraph span.corners-bottom {
278
.content h2, .panel h2 {
280
border-bottom-color: #CCCCCC;
284
border-top-color: #CCCCCC;
289
background-color: #FFFFFF;
290
border: 1px solid #C9D2D8;
294
font: 0.9em Monaco, "Andale Mono","Courier New", Courier, mono;
298
* html hr { margin: 0; }
299
* html span.corners-top, * html span.corners-bottom { background-image: url("corners_left.gif"); }
300
* html span.corners-top span, * html span.corners-bottom span { background-image: url("corners_right.gif"); }
317
<body id="phpbb" class="section-docs">
320
<a id="top" name="top" accesskey="t"></a>
321
<div id="page-header">
322
<div class="headerbar">
323
<div class="inner"><span class="corners-top"><span></span></span>
325
<div id="doc-description">
326
<a href="../index.php" id="logo"><img src="site_logo.gif" alt="" /></a>
328
<p>This is an explanation of how to use the phpBB3 hook system.</p>
329
<p style="display: none;"><a href="#start_here">Skip</a></p>
332
<span class="corners-bottom"><span></span></span></div>
336
<a name="start_here"></a>
342
<div class="paragraph menu">
343
<div class="inner"><span class="corners-top"><span></span></span>
345
<div class="content">
348
<li><a href="#intro">Introduction</a></li>
349
<li><a href="#use">Allow hooks in functions/methods</a></li>
350
<li><a href="#register">Registering hooks</a></li>
351
<li><a href="#return">Result returning</a></li>
352
<li><a href="#embed">Embedding your hook files/classes/methods</a></li>
353
<li><a href="#disclaimer">Copyright and disclaimer</a></li>
358
<span class="corners-bottom"><span></span></span></div>
363
<a name="intro"></a><h2>1. Introduction</h2>
365
<div class="paragraph">
366
<div class="inner"><span class="corners-top"><span></span></span>
368
<div class="content">
372
<p>The hook system allows applicaton and mod developers to hook into phpBB's or their own functions.</p>
374
<h3>Pre-defined hookable phpBB3 functions</h3>
376
<p>In phpBB3 there are four functions you are able to hook into with your custom functions:</p>
378
<p><code>phpbb_user_session_handler();</code> which is called within user::setup after the session and the user object is correctly initialized.<br />
379
<code>append_sid($url, $params = false, $is_amp = true, $session_id = false);</code> which is called for building urls (appending the session id)<br />
380
<code>$template->display($handle, $include_once = true);</code> which is called directly before outputting the (not-yet-compiled) template.<br />
381
<code>exit_handler();</code> which is called at the very end of phpBB3's execution.</p>
383
<p>There are also valid external constants you may want to use if you embed phpBB3 into your application:</p>
385
<div class="codebox"><pre>
386
PHPBB_MSG_HANDLER (overwrite message handler)
387
PHPBB_DB_NEW_LINK (overwrite new_link parameter for sql_connect)
388
PHPBB_ROOT_PATH (overwrite $phpbb_root_path)
389
PHPBB_ADMIN_PATH (overwrite $phpbb_admin_path)
394
<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>
396
<span class="corners-bottom"><span></span></span></div>
399
<a name="use"></a><h2>2. Allow hooks in functions/methods</h2>
401
<div class="paragraph">
402
<div class="inner"><span class="corners-top"><span></span></span>
404
<div class="content">
406
<p>The following examples explain how phpBB3 utilize the in-build hook system. You will be more interested in registering your hooks, but showing you this may help you understand the system better along the way.</p>
408
<p>First of all, this is how a function need to be layed out if you want to allow it to be hookable...</p>
410
<div class="codebox"><pre>
411
function my_own_function($my_first_parameter, $my_second_parameter)
415
if ($phpbb_hook->call_hook(__FUNCTION__, $my_first_parameter, $my_second_parameter))
417
if ($phpbb_hook->hook_return(__FUNCTION__))
419
return $phpbb_hook->hook_return_result(__FUNCTION__);
427
<p>Above, the call_hook function should always be mapping your function call... in regard to the number of parameters passed.</p>
429
<p>This is how you could make a method being hookable...</p>
431
<div class="codebox"><pre>
432
class my_hookable_object
434
function hook_me($my_first_parameter, $my_second_parameter)
438
if ($phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $my_first_parameter, $my_second_parameter))
440
if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__)))
442
return $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__));
451
<p>The only difference about calling it is the way you define the first parameter. For a function it is only <code>__FUNCTION__</code>, for a method it is <code>array(__CLASS__, __FUNCTION__)</code>. In PHP4 __CLASS__ is always returning the class in lowercase.</p>
453
<p>Now, in phpBB there are some pre-defined hooks available, but how do you make your own hookable function available (and therefore allowing others to hook into it)? For this, there is the add_hook() method:</p>
455
<div class="codebox"><pre>
456
// Adding your own hookable function:
457
$phpbb_hook->add_hook('my_own_function');
459
// Adding your own hookable method:
460
$phpbb_hook->add_hook(array('my_hookable_object', 'hook_me'));
463
<p>You are also able to remove the possibility of hooking a function/method by calling <code>$phpbb_hook->remove_hook()</code> with the same parameters as add_hook().<br />
464
This comes in handy if you want to force some hooks not to be called - at all.</p>
468
<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>
470
<span class="corners-bottom"><span></span></span></div>
473
<a name="register"></a><h2>3. Registering hooks</h2>
475
<div class="paragraph">
476
<div class="inner"><span class="corners-top"><span></span></span>
478
<div class="content">
480
<h3>Registering hooks</h3>
482
<p>Now to actually defining your functions which should be called. For this we take the append_sid() function as an example (this function is able to be hooked by default). We create two classes, one being static and a function:</p>
484
<div class="codebox"><pre>
485
class my_append_sid_class
488
function my_append_sid(&$hook, $url, $params = false, $is_amp = true, $session_id = false)
490
// Get possible previous results
491
$result = $hook->previous_hook_result('append_sid');
493
return $result['result'] . '<br />And i was the second one.';
497
// Yet another class :o
498
class my_second_append_sid_class
500
function my_append_sid(&$hook, $url, $params = false, $is_amp = true, $session_id = false)
502
// Get possible previous results
503
$result = $hook->previous_hook_result('append_sid');
505
echo $result['result'] . '<br />I was called as the third one.';
509
// And a normal function
510
function my_append_sid(&$hook, $url, $params = false, $is_amp = true, $session_id = false)
512
// Get possible previous results
513
$result = $hook->previous_hook_result('append_sid');
515
return 'I was called as the first one';
518
// Initializing the second class
519
$my_second_append_sid_class = new my_second_append_sid_class();
522
<p>Make sure you add the same parameters to your function as is defined for the hookable function with one exception: The first variable is always <code>&$hook</code>... this is the hook object itself you are able to operate on.</p>
524
<p>Now we register the hooks one by one with the <code>$phpbb_hook->register()</code> method:</p>
526
<div class="codebox"><pre>
527
// Now, we register our append_sid "replacements" in a stacked way...
528
// Registering the function (this is called first)
529
$phpbb_hook->register('append_sid', 'my_append_sid');
531
// Registering the first class
532
$phpbb_hook->register('append_sid', array('my_append_sid_class', 'my_append_sid'));
533
$phpbb_hook->register('append_sid', array(&$my_second_append_sid_class, 'my_append_sid'));
536
<p>With this you are even able to make your own functions that are already hooked itself being hooked again...</p>
538
<div class="codebox"><pre>
539
// Registering hook, which will be called
540
$phpbb_hook->register('append_sid', 'my_own_append_sid');
542
// Add hook to our called hook function
543
$phpbb_hook->add_hook('my_own_append_sid');
545
// Register added hook
546
$phpbb_hook->register('my_own_append_sid', 'also_my_own_append_sid');
549
<h3>Special treatment/chains</h3>
551
<p>The <code>register</code> method is able to take a third argument to specify a special 'chain' mode. The valid modes are <code>first</code>, <code>last</code> and <code>standalone</code></p>
553
<p><code>$phpbb_hook->register('append_sid', 'my_own_append_sid', 'first')</code> would make sure that the function is called in the beginning of the chain. It is possible that more than one function is called within the first block - here the FIFO principle is used.</p>
555
<p><code>$phpbb_hook->register('append_sid', 'my_own_append_sid', 'last')</code> would make sure that the function is called at the very end of the chain. It is possible that more than one function is called within the last block - here the FIFO principle is used.</p>
557
<p><code>$phpbb_hook->register('append_sid', 'my_own_append_sid', 'standalone')</code> makes sure only the defined function is called. All other functions are removed from the chain and no other functions are added to it later on. If two applications try to trigger the standalone mode a PHP notice will be printed and the second function being discarded.</p>
559
<h3>Only allowing hooks for some objects</h3>
561
<p>Because the hook system is not able to differate between initialized objects and only operate on the class, you need to solve this on the code level.</p>
563
<p>One possibility would be to use a property:</p>
565
<div class="codebox"><pre>
566
class my_hookable_object
573
class my_hookable_object2 extends my_hookable_object
575
var $call_hook = true;
577
function hook_me($my_first_parameter, $my_second_parameter)
579
if ($this->call_hook)
583
if ($phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $my_first_parameter, $my_second_parameter))
585
if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__)))
587
return $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__));
596
function hooking(&$hook, $first, $second)
601
$first_object = new my_hookable_object2();
602
$second_object = new my_hookable_object2();
604
$phpbb_hook->add_hook(array('my_hookable_object2', 'hook_me'));
606
$phpbb_hook->register(array('my_hookable_object2', 'hook_me'), 'hooking');
608
// Do not call the hook for $first_object
609
$first_object->call_hook = false;
611
echo $first_object->hook_me('first', 'second') . '<br />';
612
echo $second_object->hook_me('first', 'second') . '<br />';
617
<div class="codebox"><pre>
622
<p>A different possibility would be using a function variable (which could be left out on passing the function variables to the hook):</p>
624
<div class="codebox"><pre>
625
class my_hookable_object
632
class my_hookable_object2 extends my_hookable_object
634
function hook_me($my_first_parameter, $my_second_parameter, $hook_me = true)
640
if ($phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $my_first_parameter, $my_second_parameter))
642
if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__)))
644
return $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__));
653
function hooking(&$hook, $first, $second)
658
$first_object = new my_hookable_object2();
659
$second_object = new my_hookable_object2();
661
$phpbb_hook->add_hook(array('my_hookable_object2', 'hook_me'));
663
$phpbb_hook->register(array('my_hookable_object2', 'hook_me'), 'hooking');
665
echo $first_object->hook_me('first', 'second', false) . '<br />';
666
echo $second_object->hook_me('first', 'second') . '<br />';
671
<div class="codebox"><pre>
678
<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>
680
<span class="corners-bottom"><span></span></span></div>
683
<a name="return"></a><h2>4. Result returning</h2>
685
<div class="paragraph">
686
<div class="inner"><span class="corners-top"><span></span></span>
688
<div class="content">
690
<p>Generally, the distinction has to be made if a function returns the result obtained from the called function or continue the execution. Based on the needs of the application this may differ. Therefore, the function returns the results only if the called hook function is returning a result.</p>
692
<h3>Case 1 - Returning the result</h3>
694
<p>Imagine the following function supporting hooks:</p>
696
<div class="codebox"><pre>
697
function append_sid($url, $params = false, $is_amp = true, $session_id = false)
699
global $_SID, $_EXTRA_URL, $phpbb_hook;
701
// Developers using the hook function need to globalise the $_SID and $_EXTRA_URL on their own and also handle it appropiatly.
702
// They could mimick most of what is within this function
703
if ($phpbb_hook->call_hook(__FUNCTION__, $url, $params, $is_amp, $session_id))
705
if ($phpbb_hook->hook_return(__FUNCTION__))
707
return $phpbb_hook->hook_return_result(__FUNCTION__);
715
<p>Now, the following function is yours. Since you return a value, the append_sid() function itself is returning it as is:</p>
717
<div class="codebox"><pre>
718
// The function called
719
function my_append_sid(&$hook, $url, $params = false, $is_amp = true, $session_id = false)
721
// Get possible previous results
722
$result = $hook->previous_hook_result('append_sid');
724
return 'Since i return something the append_sid() function will return my result.';
728
<p>To be able to get the results returned from functions higher in the change the <code>previous_hook_result()</code> method should always be used, it returns an <code>array('result' => [your result])</code> construct.</p>
730
<h3>Case 2 - Not Returning any result</h3>
732
<p>Sometimes applications want to return nothing and therefore force the underlying function to continue it's execution:</p>
734
<div class="codebox"><pre>
735
function append_sid($url, $params = false, $is_amp = true, $session_id = false)
737
global $_SID, $_EXTRA_URL, $phpbb_hook;
739
// Developers using the hook function need to globalise the $_SID and $_EXTRA_URL on their own and also handle it appropiatly.
740
// They could mimick most of what is within this function
741
if ($phpbb_hook->call_hook(__FUNCTION__, $url, $params, $is_amp, $session_id))
743
if ($phpbb_hook->hook_return(__FUNCTION__))
745
return $phpbb_hook->hook_return_result(__FUNCTION__);
752
// The function called
753
function my_append_sid(&$hook, $url, $params = false, $is_amp = true, $session_id = false)
755
// Get possible previous results
756
$result = $hook->previous_hook_result('append_sid');
760
// I only rewrite some variables, but return nothing. Therefore, the append_sid() function will not return my (non)result.
764
<p>Please Note: The decision to return or not return is solely made of the very last function call within the hook chain. An example:</p>
766
<div class="codebox"><pre>
767
// The function called
768
function my_append_sid(&$hook, $url, $params = false, $is_amp = true, $session_id = false)
770
// Get possible previous results
771
$result = $hook->previous_hook_result('append_sid');
773
// $result is not filled
778
// This function is registered too and gets executed after my_append_sid()
779
function my_own_append_sid(&$hook, $url, $params = false, $is_amp = true, $session_id = false)
781
$result = $hook->previous_hook_result('append_sid');
783
// $result is actually filled with $result['result'] = 'FILLED'
784
// But i return nothing, therefore append_sid() continues it's execution.
787
// The way both functions are registered.
788
$phpbb_hook->register('append_sid', 'my_append_sid');
789
$phpbb_hook->register('append_sid', 'my_own_append_sid');
794
<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>
796
<span class="corners-bottom"><span></span></span></div>
799
<a name="embed"></a><h2>5. Embedding your hook files/classes/methods</h2>
801
<div class="paragraph">
802
<div class="inner"><span class="corners-top"><span></span></span>
804
<div class="content">
806
<p>There are basically two methods you are able to choose from:</p>
808
<p>1) Add a file to includes/hooks/. The file need to be prefixed by <code>hook_</code>. This file is included within common.php, you are able to register your hooks, include other files or functions, etc. It is advised to only include other files if needed (within a function call for example).</p>
810
<p>Please be aware that you need to purge your cache within the ACP to make your newly placed file available to phpBB3.</p>
812
<p>2) The second method is meant for those wanting to wrap phpBB3 without placing a custom file to the hooks directory. This is mostly done by including phpBB's files within the application file. To be able to register your hooks you need to create a function within your application:</p>
814
<div class="codebox"><pre>
815
// My function which gets executed within the hooks constuctor
816
function phpbb_hook_register(&$hook)
818
$hook->register('append_sid', 'my_append_sid');
824
<p>You should get the idea. ;)</p>
828
<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>
830
<span class="corners-bottom"><span></span></span></div>
833
<a name="disclaimer"></a><h2>6. Copyright and disclaimer</h2>
835
<div class="paragraph">
836
<div class="inner"><span class="corners-top"><span></span></span>
838
<div class="content">
840
<p>This application is opensource software released under the <a href="http://opensource.org/licenses/gpl-license.php">GPL</a>. Please see source code and the docs directory for more details. This package and its contents are Copyright (c) 2000, 2002, 2005, 2007 <a href="http://www.phpbb.com/">phpBB Group</a>, All Rights Reserved.</p>
844
<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>
846
<span class="corners-bottom"><span></span></span></div>
849
<div id="page-footer">
850
<div class="version">$Id: hook_system.html,v 1.4 2007/11/18 15:37:17 naderman Exp $</div>
855
<a id="bottom" name="bottom" accesskey="z"></a>