docs/hook_system.html
author indvdum (gotoindvdum[at]gmail[dot]com)
Fri, 09 Mar 2012 16:11:37 +0400
changeset 12 898199d067e7
parent 0 ceef6d64c21d
permissions -rw-r--r--
Ignoring cache
indvdum@0
     1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
indvdum@0
     2
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en" xml:lang="en">
indvdum@0
     3
<head>
indvdum@0
     4
indvdum@0
     5
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
indvdum@0
     6
<meta http-equiv="content-style-type" content="text/css" />
indvdum@0
     7
<meta http-equiv="content-language" content="en" />
indvdum@0
     8
<meta http-equiv="imagetoolbar" content="no" />
indvdum@0
     9
<meta name="resource-type" content="document" />
indvdum@0
    10
<meta name="distribution" content="global" />
indvdum@0
    11
<meta name="copyright" content="phpBB Group" />
indvdum@0
    12
<meta name="keywords" content="" />
indvdum@0
    13
<meta name="description" content="Hook System explanation" />
indvdum@0
    14
<title>phpBB3 &bull; Hook System</title>
indvdum@0
    15
indvdum@0
    16
<style type="text/css">
indvdum@0
    17
/* <![CDATA[ */
indvdum@0
    18
indvdum@0
    19
/*
indvdum@0
    20
	The original "prosilver" theme for phpBB3
indvdum@0
    21
	Created by subBlue design :: http://www.subBlue.com
indvdum@0
    22
*/
indvdum@0
    23
indvdum@0
    24
* { margin: 0; padding: 0; }
indvdum@0
    25
indvdum@0
    26
html { font-size: 100%; height: 100%; margin-bottom: 1px; }
indvdum@0
    27
indvdum@0
    28
body {
indvdum@0
    29
	font-family: Verdana, Helvetica, Arial, sans-serif;
indvdum@0
    30
	color: #828282;
indvdum@0
    31
	background-color: #FFFFFF;
indvdum@0
    32
	font-size: 12px;
indvdum@0
    33
	margin: 0;
indvdum@0
    34
	padding: 12px 0;
indvdum@0
    35
}
indvdum@0
    36
indvdum@0
    37
img { border-width: 0; }
indvdum@0
    38
indvdum@0
    39
p {
indvdum@0
    40
	line-height: 1.3em;
indvdum@0
    41
	font-size: 1.1em;
indvdum@0
    42
	margin-bottom: 1.5em;
indvdum@0
    43
}
indvdum@0
    44
indvdum@0
    45
hr {
indvdum@0
    46
	border: 0 none #FFFFFF;
indvdum@0
    47
	border-top: 1px solid #CCCCCC;
indvdum@0
    48
	height: 1px;
indvdum@0
    49
	margin: 5px 0;
indvdum@0
    50
	display: block;
indvdum@0
    51
	clear: both;
indvdum@0
    52
}
indvdum@0
    53
indvdum@0
    54
html, body {
indvdum@0
    55
	color: #536482;
indvdum@0
    56
	background-color: #FFFFFF;
indvdum@0
    57
}
indvdum@0
    58
indvdum@0
    59
#doc-description h1 {
indvdum@0
    60
	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
indvdum@0
    61
	margin-right: 200px;
indvdum@0
    62
	color: #FFFFFF;
indvdum@0
    63
	margin-top: 15px;
indvdum@0
    64
	font-weight: bold;
indvdum@0
    65
	font-size: 2em;
indvdum@0
    66
	color: #fff;
indvdum@0
    67
}
indvdum@0
    68
indvdum@0
    69
h1 {
indvdum@0
    70
	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
indvdum@0
    71
	font-weight: normal;
indvdum@0
    72
	color: #000;
indvdum@0
    73
	font-size: 2em;
indvdum@0
    74
	margin: 0.8em 0 0.2em 0;
indvdum@0
    75
}
indvdum@0
    76
indvdum@0
    77
h2 {
indvdum@0
    78
	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
indvdum@0
    79
	font-weight: normal;
indvdum@0
    80
	color: #28313F;
indvdum@0
    81
	font-size: 1.5em;
indvdum@0
    82
	margin: 0.8em 0 0.2em 0;
indvdum@0
    83
}
indvdum@0
    84
indvdum@0
    85
h3 {
indvdum@0
    86
	font-family: Arial, Helvetica, sans-serif;
indvdum@0
    87
	font-weight: bold;
indvdum@0
    88
	border-bottom: 1px solid #CCCCCC;
indvdum@0
    89
	margin-bottom: 3px;
indvdum@0
    90
	padding-bottom: 2px;
indvdum@0
    91
	font-size: 1.05em;
indvdum@0
    92
	color: #115098;
indvdum@0
    93
	margin-top: 20px;
indvdum@0
    94
}
indvdum@0
    95
indvdum@0
    96
.good { color: green; }
indvdum@0
    97
.bad { color: red; }
indvdum@0
    98
indvdum@0
    99
.version {
indvdum@0
   100
	margin-top: 20px;
indvdum@0
   101
	text-align: left;
indvdum@0
   102
	font-size: 70%;
indvdum@0
   103
	color: #006600;
indvdum@0
   104
	border-top: 1px solid #ccc;
indvdum@0
   105
}
indvdum@0
   106
indvdum@0
   107
code {
indvdum@0
   108
	color: #006600;
indvdum@0
   109
	font-weight: normal;
indvdum@0
   110
	font-family: 'Courier New', monospace;
indvdum@0
   111
	border-color: #D1D7DC;
indvdum@0
   112
	border-width: 1px;
indvdum@0
   113
	border-style: solid;
indvdum@0
   114
	background-color: #FAFAFA;
indvdum@0
   115
}
indvdum@0
   116
indvdum@0
   117
#wrap {
indvdum@0
   118
	padding: 0 20px;
indvdum@0
   119
	min-width: 650px;
indvdum@0
   120
}
indvdum@0
   121
indvdum@0
   122
#simple-wrap {
indvdum@0
   123
	padding: 6px 10px;
indvdum@0
   124
}
indvdum@0
   125
indvdum@0
   126
#page-body {
indvdum@0
   127
	margin: 4px 0;
indvdum@0
   128
	clear: both;
indvdum@0
   129
}
indvdum@0
   130
indvdum@0
   131
#page-footer {
indvdum@0
   132
	clear: both;
indvdum@0
   133
}
indvdum@0
   134
indvdum@0
   135
#logo {
indvdum@0
   136
	float: left;
indvdum@0
   137
	width: auto;
indvdum@0
   138
	padding: 10px 13px 0 10px;
indvdum@0
   139
}
indvdum@0
   140
indvdum@0
   141
a#logo:hover {
indvdum@0
   142
	text-decoration: none;
indvdum@0
   143
}
indvdum@0
   144
indvdum@0
   145
#doc-description {
indvdum@0
   146
	float: left;
indvdum@0
   147
	width: 70%;
indvdum@0
   148
}
indvdum@0
   149
indvdum@0
   150
#doc-description h1 {
indvdum@0
   151
	margin-right: 0;
indvdum@0
   152
}
indvdum@0
   153
indvdum@0
   154
.headerbar {
indvdum@0
   155
	background: #ebebeb none repeat-x 0 0;
indvdum@0
   156
	color: #FFFFFF;
indvdum@0
   157
	margin-bottom: 4px;
indvdum@0
   158
	padding: 0 5px;
indvdum@0
   159
}
indvdum@0
   160
indvdum@0
   161
span.corners-top, span.corners-bottom, span.corners-top span, span.corners-bottom span {
indvdum@0
   162
	font-size: 1px;
indvdum@0
   163
	line-height: 1px;
indvdum@0
   164
	display: block;
indvdum@0
   165
	height: 5px;
indvdum@0
   166
	background-repeat: no-repeat;
indvdum@0
   167
}
indvdum@0
   168
indvdum@0
   169
span.corners-top {
indvdum@0
   170
	background-image: none;
indvdum@0
   171
	background-position: 0 0;
indvdum@0
   172
	margin: 0 -5px;
indvdum@0
   173
}
indvdum@0
   174
indvdum@0
   175
span.corners-top span {
indvdum@0
   176
	background-image: none;
indvdum@0
   177
	background-position: 100% 0;
indvdum@0
   178
}
indvdum@0
   179
indvdum@0
   180
span.corners-bottom {
indvdum@0
   181
	background-image: none;
indvdum@0
   182
	background-position: 0 100%;
indvdum@0
   183
	margin: 0 -5px;
indvdum@0
   184
	clear: both;
indvdum@0
   185
}
indvdum@0
   186
indvdum@0
   187
span.corners-bottom span {
indvdum@0
   188
	background-image: none;
indvdum@0
   189
	background-position: 100% 100%;
indvdum@0
   190
}
indvdum@0
   191
indvdum@0
   192
.paragraph {
indvdum@0
   193
	padding: 0 10px;
indvdum@0
   194
	margin-bottom: 4px;
indvdum@0
   195
	background-repeat: no-repeat;
indvdum@0
   196
	background-position: 100% 0;
indvdum@0
   197
	background-color: #ECF3F7;
indvdum@0
   198
}
indvdum@0
   199
indvdum@0
   200
.paragraph:target .content {
indvdum@0
   201
	color: #000000;
indvdum@0
   202
}
indvdum@0
   203
indvdum@0
   204
.paragraph:target h3 a {
indvdum@0
   205
	color: #000000;
indvdum@0
   206
}
indvdum@0
   207
indvdum@0
   208
.content {
indvdum@0
   209
	color: #333333;
indvdum@0
   210
}
indvdum@0
   211
indvdum@0
   212
.content h2, .panel h2 {
indvdum@0
   213
	color: #115098;
indvdum@0
   214
	border-bottom-color:  #CCCCCC;
indvdum@0
   215
}
indvdum@0
   216
indvdum@0
   217
a:link	{ color: #898989; text-decoration: none; }
indvdum@0
   218
a:visited	{ color: #898989; text-decoration: none; }
indvdum@0
   219
a:hover	{ color: #d3d3d3; text-decoration: underline; }
indvdum@0
   220
a:active	{ color: #d2d2d2; text-decoration: none; }
indvdum@0
   221
indvdum@0
   222
hr {
indvdum@0
   223
	border-color: #FFFFFF;
indvdum@0
   224
	border-top-color: #CCCCCC;
indvdum@0
   225
}
indvdum@0
   226
indvdum@0
   227
.menu {
indvdum@0
   228
	background-color: #cadceb;
indvdum@0
   229
}
indvdum@0
   230
indvdum@0
   231
.headerbar {
indvdum@0
   232
	background-color: #12A3EB;
indvdum@0
   233
	background-image: url("bg_header.gif");
indvdum@0
   234
	color: #FFFFFF;
indvdum@0
   235
}
indvdum@0
   236
indvdum@0
   237
.panel {
indvdum@0
   238
	background-color: #ECF1F3;
indvdum@0
   239
	color: #28313F;
indvdum@0
   240
}
indvdum@0
   241
indvdum@0
   242
indvdum@0
   243
span.corners-top {
indvdum@0
   244
	background-image: url("corners_left.png");
indvdum@0
   245
}
indvdum@0
   246
indvdum@0
   247
span.corners-top span {
indvdum@0
   248
	background-image: url("corners_right.png");
indvdum@0
   249
}
indvdum@0
   250
indvdum@0
   251
span.corners-bottom {
indvdum@0
   252
	background-image: url("corners_left.png");
indvdum@0
   253
}
indvdum@0
   254
indvdum@0
   255
span.corners-bottom span {
indvdum@0
   256
	background-image: url("corners_right.png");
indvdum@0
   257
}
indvdum@0
   258
indvdum@0
   259
.error {
indvdum@0
   260
	color: #BC2A4D;
indvdum@0
   261
}
indvdum@0
   262
indvdum@0
   263
a:link	{ color: #105289; }
indvdum@0
   264
a:visited	{ color: #105289; }
indvdum@0
   265
a:hover	{ color: #D31141; }
indvdum@0
   266
a:active	{ color: #368AD2; }
indvdum@0
   267
indvdum@0
   268
.paragraph span.corners-top, .paragraph span.corners-bottom {
indvdum@0
   269
	margin: 0 -10px;
indvdum@0
   270
}
indvdum@0
   271
indvdum@0
   272
.content {
indvdum@0
   273
	padding: 0;
indvdum@0
   274
	line-height: 1.48em;
indvdum@0
   275
	color: #333333;
indvdum@0
   276
}
indvdum@0
   277
indvdum@0
   278
.content h2, .panel h2 {
indvdum@0
   279
	color: #115098;
indvdum@0
   280
	border-bottom-color:  #CCCCCC;
indvdum@0
   281
}
indvdum@0
   282
indvdum@0
   283
.notice {
indvdum@0
   284
	border-top-color:  #CCCCCC;
indvdum@0
   285
}
indvdum@0
   286
indvdum@0
   287
.codebox {
indvdum@0
   288
	padding: 3px;
indvdum@0
   289
	background-color: #FFFFFF;
indvdum@0
   290
	border: 1px solid #C9D2D8;
indvdum@0
   291
	font-size: 1em;
indvdum@0
   292
	margin-bottom: 10px;
indvdum@0
   293
	display: block;
indvdum@0
   294
	font: 0.9em Monaco, "Andale Mono","Courier New", Courier, mono;
indvdum@0
   295
	line-height: 1.3em;
indvdum@0
   296
}
indvdum@0
   297
indvdum@0
   298
* html hr { margin: 0; }
indvdum@0
   299
* html span.corners-top, * html span.corners-bottom { background-image: url("corners_left.gif"); }
indvdum@0
   300
* html span.corners-top span, * html span.corners-bottom span { background-image: url("corners_right.gif"); }
indvdum@0
   301
indvdum@0
   302
.back2top {
indvdum@0
   303
	clear: both;
indvdum@0
   304
	height: 11px;
indvdum@0
   305
	text-align: right;
indvdum@0
   306
}
indvdum@0
   307
indvdum@0
   308
.content ol {
indvdum@0
   309
	margin-left: 25px;
indvdum@0
   310
}
indvdum@0
   311
indvdum@0
   312
/* ]]> */
indvdum@0
   313
</style>
indvdum@0
   314
indvdum@0
   315
</head>
indvdum@0
   316
indvdum@0
   317
<body id="phpbb" class="section-docs">
indvdum@0
   318
indvdum@0
   319
<div id="wrap">
indvdum@0
   320
	<a id="top" name="top" accesskey="t"></a>
indvdum@0
   321
	<div id="page-header">
indvdum@0
   322
		<div class="headerbar">
indvdum@0
   323
			<div class="inner"><span class="corners-top"><span></span></span>
indvdum@0
   324
indvdum@0
   325
			<div id="doc-description">
indvdum@0
   326
				<a href="../index.php" id="logo"><img src="site_logo.gif" alt="" /></a>
indvdum@0
   327
				<h1>Hook System</h1>
indvdum@0
   328
				<p>This is an explanation of how to use the phpBB3 hook system.</p>
indvdum@0
   329
				<p style="display: none;"><a href="#start_here">Skip</a></p>
indvdum@0
   330
			</div>
indvdum@0
   331
indvdum@0
   332
			<span class="corners-bottom"><span></span></span></div>
indvdum@0
   333
		</div>
indvdum@0
   334
	</div>
indvdum@0
   335
indvdum@0
   336
	<a name="start_here"></a>
indvdum@0
   337
indvdum@0
   338
	<div id="page-body">
indvdum@0
   339
indvdum@0
   340
	<h1>Hook System</h1>
indvdum@0
   341
indvdum@0
   342
	<div class="paragraph menu">
indvdum@0
   343
		<div class="inner"><span class="corners-top"><span></span></span>
indvdum@0
   344
indvdum@0
   345
		<div class="content">
indvdum@0
   346
indvdum@0
   347
			<ol>
indvdum@0
   348
				<li><a href="#intro">Introduction</a></li>
indvdum@0
   349
				<li><a href="#use">Allow hooks in functions/methods</a></li>
indvdum@0
   350
				<li><a href="#register">Registering hooks</a></li>
indvdum@0
   351
				<li><a href="#return">Result returning</a></li>
indvdum@0
   352
				<li><a href="#embed">Embedding your hook files/classes/methods</a></li>
indvdum@0
   353
				<li><a href="#disclaimer">Copyright and disclaimer</a></li>
indvdum@0
   354
			</ol>
indvdum@0
   355
indvdum@0
   356
		</div>
indvdum@0
   357
indvdum@0
   358
		<span class="corners-bottom"><span></span></span></div>
indvdum@0
   359
	</div>
indvdum@0
   360
indvdum@0
   361
	<hr />
indvdum@0
   362
indvdum@0
   363
	<a name="intro"></a><h2>1. Introduction</h2>
indvdum@0
   364
indvdum@0
   365
	<div class="paragraph">
indvdum@0
   366
		<div class="inner"><span class="corners-top"><span></span></span>
indvdum@0
   367
indvdum@0
   368
		<div class="content">
indvdum@0
   369
indvdum@0
   370
<h3>What is it?</h3>
indvdum@0
   371
indvdum@0
   372
<p>The hook system allows applicaton and mod developers to hook into phpBB's or their own functions.</p>
indvdum@0
   373
indvdum@0
   374
<h3>Pre-defined hookable phpBB3 functions</h3>
indvdum@0
   375
indvdum@0
   376
<p>In phpBB3 there are four functions you are able to hook into with your custom functions:</p>
indvdum@0
   377
indvdum@0
   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 />
indvdum@0
   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 />
indvdum@0
   380
<code>$template-&gt;display($handle, $include_once = true);</code> which is called directly before outputting the (not-yet-compiled) template.<br />
indvdum@0
   381
<code>exit_handler();</code> which is called at the very end of phpBB3's execution.</p>
indvdum@0
   382
indvdum@0
   383
<p>Please note: The <code>$template-&gt;display</code> hook takes a third <code>$template</code> argument, which is the template instance being used, which should be used instead of the global.</p>
indvdum@0
   384
indvdum@0
   385
<p>There are also valid external constants you may want to use if you embed phpBB3 into your application:</p>
indvdum@0
   386
indvdum@0
   387
<div class="codebox"><pre>
indvdum@0
   388
PHPBB_MSG_HANDLER (overwrite message handler)
indvdum@0
   389
PHPBB_DB_NEW_LINK (overwrite new_link parameter for sql_connect)
indvdum@0
   390
PHPBB_ROOT_PATH   (overwrite $phpbb_root_path)
indvdum@0
   391
PHPBB_ADMIN_PATH  (overwrite $phpbb_admin_path)
indvdum@0
   392
PHPBB_USE_BOARD_URL_PATH (use generate_board_url() for image paths instead of $phpbb_root_path)
indvdum@0
   393
</pre></div>
indvdum@0
   394
indvdum@0
   395
<p>If the <code>PHPBB_USE_BOARD_URL_PATH</code> constant is set to true, phpBB uses generate_board_url() (this will return the boards url with the script path included) on all instances where web-accessible images are loaded. The exact locations are:</p>
indvdum@0
   396
indvdum@0
   397
<ul>
indvdum@0
   398
	<li>/includes/session.php - user::img()</li>
indvdum@0
   399
	<li>/includes/functions_content.php - smiley_text()</li>
indvdum@0
   400
</ul>
indvdum@0
   401
indvdum@0
   402
<p>Path locations for the following template variables are affected by this too:</p>
indvdum@0
   403
indvdum@0
   404
<ul>
indvdum@0
   405
	<li>{T_THEME_PATH} - styles/xxx/theme</li>
indvdum@0
   406
	<li>{T_TEMPLATE_PATH} - styles/xxx/template</li>
indvdum@0
   407
	<li>{T_SUPER_TEMPLATE_PATH} - styles/xxx/template</li>
indvdum@0
   408
	<li>{T_IMAGESET_PATH} - styles/xxx/imageset</li>
indvdum@0
   409
	<li>{T_IMAGESET_LANG_PATH} - styles/xxx/imageset/yy</li>
indvdum@0
   410
	<li>{T_IMAGES_PATH} - images/</li>
indvdum@0
   411
	<li>{T_SMILIES_PATH} - $config['smilies_path']/</li>
indvdum@0
   412
	<li>{T_AVATAR_PATH} - $config['avatar_path']/</li>
indvdum@0
   413
	<li>{T_AVATAR_GALLERY_PATH} - $config['avatar_gallery_path']/</li>
indvdum@0
   414
	<li>{T_ICONS_PATH} - $config['icons_path']/</li>
indvdum@0
   415
	<li>{T_RANKS_PATH} - $config['ranks_path']/</li>
indvdum@0
   416
	<li>{T_UPLOAD_PATH} - $config['upload_path']/</li>
indvdum@0
   417
	<li>{T_STYLESHEET_LINK} - styles/xxx/theme/stylesheet.css (or link to style.php if css is parsed dynamically)</li>
indvdum@0
   418
	<li>New template variable {BOARD_URL} for the board url + script path.</li>
indvdum@0
   419
</ul>
indvdum@0
   420
indvdum@0
   421
indvdum@0
   422
		</div>
indvdum@0
   423
indvdum@0
   424
		<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>
indvdum@0
   425
indvdum@0
   426
		<span class="corners-bottom"><span></span></span></div>
indvdum@0
   427
	</div>
indvdum@0
   428
indvdum@0
   429
	<a name="use"></a><h2>2. Allow hooks in functions/methods</h2>
indvdum@0
   430
indvdum@0
   431
	<div class="paragraph">
indvdum@0
   432
		<div class="inner"><span class="corners-top"><span></span></span>
indvdum@0
   433
indvdum@0
   434
		<div class="content">
indvdum@0
   435
indvdum@0
   436
<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>
indvdum@0
   437
indvdum@0
   438
<p>First of all, this is how a function need to be layed out if you want to allow it to be hookable...</p>
indvdum@0
   439
indvdum@0
   440
<div class="codebox"><pre>
indvdum@0
   441
function my_own_function($my_first_parameter, $my_second_parameter)
indvdum@0
   442
{
indvdum@0
   443
	global $phpbb_hook;
indvdum@0
   444
indvdum@0
   445
	if ($phpbb_hook-&gt;call_hook(__FUNCTION__, $my_first_parameter, $my_second_parameter))
indvdum@0
   446
	{
indvdum@0
   447
		if ($phpbb_hook-&gt;hook_return(__FUNCTION__))
indvdum@0
   448
		{
indvdum@0
   449
			return $phpbb_hook-&gt;hook_return_result(__FUNCTION__);
indvdum@0
   450
		}
indvdum@0
   451
	}
indvdum@0
   452
indvdum@0
   453
	[YOUR CODE HERE]
indvdum@0
   454
}
indvdum@0
   455
</pre></div>
indvdum@0
   456
indvdum@0
   457
<p>Above, the call_hook function should always be mapping your function call... in regard to the number of parameters passed.</p>
indvdum@0
   458
indvdum@0
   459
<p>This is how you could make a method being hookable...</p>
indvdum@0
   460
indvdum@0
   461
<div class="codebox"><pre>
indvdum@0
   462
class my_hookable_object
indvdum@0
   463
{
indvdum@0
   464
	function hook_me($my_first_parameter, $my_second_parameter)
indvdum@0
   465
	{
indvdum@0
   466
		global $phpbb_hook;
indvdum@0
   467
indvdum@0
   468
		if ($phpbb_hook-&gt;call_hook(array(__CLASS__, __FUNCTION__), $my_first_parameter, $my_second_parameter))
indvdum@0
   469
		{
indvdum@0
   470
			if ($phpbb_hook-&gt;hook_return(array(__CLASS__, __FUNCTION__)))
indvdum@0
   471
			{
indvdum@0
   472
				return $phpbb_hook-&gt;hook_return_result(array(__CLASS__, __FUNCTION__));
indvdum@0
   473
			}
indvdum@0
   474
		}
indvdum@0
   475
indvdum@0
   476
		[YOUR CODE HERE]
indvdum@0
   477
	}
indvdum@0
   478
}
indvdum@0
   479
</pre></div>
indvdum@0
   480
indvdum@0
   481
<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>
indvdum@0
   482
indvdum@0
   483
<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>
indvdum@0
   484
indvdum@0
   485
<div class="codebox"><pre>
indvdum@0
   486
// Adding your own hookable function:
indvdum@0
   487
$phpbb_hook-&gt;add_hook('my_own_function');
indvdum@0
   488
indvdum@0
   489
// Adding your own hookable method:
indvdum@0
   490
$phpbb_hook-&gt;add_hook(array('my_hookable_object', 'hook_me'));
indvdum@0
   491
</pre></div>
indvdum@0
   492
indvdum@0
   493
<p>You are also able to remove the possibility of hooking a function/method by calling <code>$phpbb_hook-&gt;remove_hook()</code> with the same parameters as add_hook().<br />
indvdum@0
   494
This comes in handy if you want to force some hooks not to be called - at all.</p>
indvdum@0
   495
indvdum@0
   496
		</div>
indvdum@0
   497
indvdum@0
   498
		<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>
indvdum@0
   499
indvdum@0
   500
		<span class="corners-bottom"><span></span></span></div>
indvdum@0
   501
	</div>
indvdum@0
   502
indvdum@0
   503
	<a name="register"></a><h2>3. Registering hooks</h2>
indvdum@0
   504
indvdum@0
   505
	<div class="paragraph">
indvdum@0
   506
		<div class="inner"><span class="corners-top"><span></span></span>
indvdum@0
   507
indvdum@0
   508
		<div class="content">
indvdum@0
   509
indvdum@0
   510
		<h3>Registering hooks</h3>
indvdum@0
   511
indvdum@0
   512
<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>
indvdum@0
   513
indvdum@0
   514
<div class="codebox"><pre>
indvdum@0
   515
class my_append_sid_class
indvdum@0
   516
{
indvdum@0
   517
	// Our functions
indvdum@0
   518
	function my_append_sid(&amp;$hook, $url, $params = false, $is_amp = true, $session_id = false)
indvdum@0
   519
	{
indvdum@0
   520
		// Get possible previous results
indvdum@0
   521
		$result = $hook-&gt;previous_hook_result('append_sid');
indvdum@0
   522
indvdum@0
   523
		return $result['result'] . '&lt;br /&gt;And i was the second one.';
indvdum@0
   524
	}
indvdum@0
   525
}
indvdum@0
   526
indvdum@0
   527
// Yet another class :o
indvdum@0
   528
class my_second_append_sid_class
indvdum@0
   529
{
indvdum@0
   530
	function my_append_sid(&amp;$hook, $url, $params = false, $is_amp = true, $session_id = false)
indvdum@0
   531
	{
indvdum@0
   532
		// Get possible previous results
indvdum@0
   533
		$result = $hook-&gt;previous_hook_result('append_sid');
indvdum@0
   534
indvdum@0
   535
		echo $result['result'] . '&lt;br /&gt;I was called as the third one.';
indvdum@0
   536
	}
indvdum@0
   537
}
indvdum@0
   538
indvdum@0
   539
// And a normal function
indvdum@0
   540
function my_append_sid(&amp;$hook, $url, $params = false, $is_amp = true, $session_id = false)
indvdum@0
   541
{
indvdum@0
   542
	// Get possible previous results
indvdum@0
   543
	$result = $hook-&gt;previous_hook_result('append_sid');
indvdum@0
   544
indvdum@0
   545
	return 'I was called as the first one';
indvdum@0
   546
}
indvdum@0
   547
indvdum@0
   548
// Initializing the second class
indvdum@0
   549
$my_second_append_sid_class = new my_second_append_sid_class();
indvdum@0
   550
</pre></div>
indvdum@0
   551
indvdum@0
   552
<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>&amp;$hook</code>... this is the hook object itself you are able to operate on.</p>
indvdum@0
   553
indvdum@0
   554
<p>Now we register the hooks one by one with the <code>$phpbb_hook-&gt;register()</code> method:</p>
indvdum@0
   555
indvdum@0
   556
<div class="codebox"><pre>
indvdum@0
   557
// Now, we register our append_sid &quot;replacements&quot; in a stacked way...
indvdum@0
   558
// Registering the function (this is called first)
indvdum@0
   559
$phpbb_hook-&gt;register('append_sid', 'my_append_sid');
indvdum@0
   560
indvdum@0
   561
// Registering the first class
indvdum@0
   562
$phpbb_hook-&gt;register('append_sid', array('my_append_sid_class', 'my_append_sid'));
indvdum@0
   563
$phpbb_hook-&gt;register('append_sid', array(&amp;$my_second_append_sid_class, 'my_append_sid'));
indvdum@0
   564
</pre></div>
indvdum@0
   565
indvdum@0
   566
<p>With this you are even able to make your own functions that are already hooked itself being hooked again...</p>
indvdum@0
   567
indvdum@0
   568
<div class="codebox"><pre>
indvdum@0
   569
// Registering hook, which will be called
indvdum@0
   570
$phpbb_hook-&gt;register('append_sid', 'my_own_append_sid');
indvdum@0
   571
indvdum@0
   572
// Add hook to our called hook function
indvdum@0
   573
$phpbb_hook-&gt;add_hook('my_own_append_sid');
indvdum@0
   574
indvdum@0
   575
// Register added hook
indvdum@0
   576
$phpbb_hook-&gt;register('my_own_append_sid', 'also_my_own_append_sid');
indvdum@0
   577
</pre></div>
indvdum@0
   578
indvdum@0
   579
		<h3>Special treatment/chains</h3>
indvdum@0
   580
indvdum@0
   581
		<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>
indvdum@0
   582
indvdum@0
   583
		<p><code>$phpbb_hook-&gt;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>
indvdum@0
   584
indvdum@0
   585
		<p><code>$phpbb_hook-&gt;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>
indvdum@0
   586
indvdum@0
   587
		<p><code>$phpbb_hook-&gt;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>
indvdum@0
   588
indvdum@0
   589
		<h3>Only allowing hooks for some objects</h3>
indvdum@0
   590
indvdum@0
   591
		<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>
indvdum@0
   592
indvdum@0
   593
		<p>One possibility would be to use a property:</p>
indvdum@0
   594
indvdum@0
   595
		<div class="codebox"><pre>
indvdum@0
   596
class my_hookable_object
indvdum@0
   597
{
indvdum@0
   598
	function blabla()
indvdum@0
   599
	{
indvdum@0
   600
	}
indvdum@0
   601
}
indvdum@0
   602
indvdum@0
   603
class my_hookable_object2 extends my_hookable_object
indvdum@0
   604
{
indvdum@0
   605
	var $call_hook = true;
indvdum@0
   606
indvdum@0
   607
	function hook_me($my_first_parameter, $my_second_parameter)
indvdum@0
   608
	{
indvdum@0
   609
		if ($this-&gt;call_hook)
indvdum@0
   610
		{
indvdum@0
   611
			global $phpbb_hook;
indvdum@0
   612
indvdum@0
   613
			if ($phpbb_hook-&gt;call_hook(array(__CLASS__, __FUNCTION__), $my_first_parameter, $my_second_parameter))
indvdum@0
   614
			{
indvdum@0
   615
				if ($phpbb_hook-&gt;hook_return(array(__CLASS__, __FUNCTION__)))
indvdum@0
   616
				{
indvdum@0
   617
					return $phpbb_hook-&gt;hook_return_result(array(__CLASS__, __FUNCTION__));
indvdum@0
   618
				}
indvdum@0
   619
			}
indvdum@0
   620
		}
indvdum@0
   621
indvdum@0
   622
		return 'not hooked';
indvdum@0
   623
	}
indvdum@0
   624
}
indvdum@0
   625
indvdum@0
   626
function hooking(&amp;$hook, $first, $second)
indvdum@0
   627
{
indvdum@0
   628
	return 'hooked';
indvdum@0
   629
}
indvdum@0
   630
indvdum@0
   631
$first_object = new my_hookable_object2();
indvdum@0
   632
$second_object = new my_hookable_object2();
indvdum@0
   633
indvdum@0
   634
$phpbb_hook-&gt;add_hook(array('my_hookable_object2', 'hook_me'));
indvdum@0
   635
indvdum@0
   636
$phpbb_hook-&gt;register(array('my_hookable_object2', 'hook_me'), 'hooking');
indvdum@0
   637
indvdum@0
   638
// Do not call the hook for $first_object
indvdum@0
   639
$first_object-&gt;call_hook = false;
indvdum@0
   640
indvdum@0
   641
echo $first_object-&gt;hook_me('first', 'second') . '&lt;br /&gt;';
indvdum@0
   642
echo $second_object-&gt;hook_me('first', 'second') . '&lt;br /&gt;';
indvdum@0
   643
</pre></div>
indvdum@0
   644
indvdum@0
   645
<p>OUTPUT:</p>
indvdum@0
   646
indvdum@0
   647
<div class="codebox"><pre>
indvdum@0
   648
not hooked
indvdum@0
   649
hooked
indvdum@0
   650
</pre></div>
indvdum@0
   651
indvdum@0
   652
		<p>A different possibility would be using a function variable (which could be left out on passing the function variables to the hook):</p>
indvdum@0
   653
indvdum@0
   654
		<div class="codebox"><pre>
indvdum@0
   655
class my_hookable_object
indvdum@0
   656
{
indvdum@0
   657
	function blabla()
indvdum@0
   658
	{
indvdum@0
   659
	}
indvdum@0
   660
}
indvdum@0
   661
indvdum@0
   662
class my_hookable_object2 extends my_hookable_object
indvdum@0
   663
{
indvdum@0
   664
	function hook_me($my_first_parameter, $my_second_parameter, $hook_me = true)
indvdum@0
   665
	{
indvdum@0
   666
		if ($hook_me)
indvdum@0
   667
		{
indvdum@0
   668
			global $phpbb_hook;
indvdum@0
   669
indvdum@0
   670
			if ($phpbb_hook-&gt;call_hook(array(__CLASS__, __FUNCTION__), $my_first_parameter, $my_second_parameter))
indvdum@0
   671
			{
indvdum@0
   672
				if ($phpbb_hook-&gt;hook_return(array(__CLASS__, __FUNCTION__)))
indvdum@0
   673
				{
indvdum@0
   674
					return $phpbb_hook-&gt;hook_return_result(array(__CLASS__, __FUNCTION__));
indvdum@0
   675
				}
indvdum@0
   676
			}
indvdum@0
   677
		}
indvdum@0
   678
indvdum@0
   679
		return 'not hooked';
indvdum@0
   680
	}
indvdum@0
   681
}
indvdum@0
   682
indvdum@0
   683
function hooking(&amp;$hook, $first, $second)
indvdum@0
   684
{
indvdum@0
   685
	return 'hooked';
indvdum@0
   686
}
indvdum@0
   687
indvdum@0
   688
$first_object = new my_hookable_object2();
indvdum@0
   689
$second_object = new my_hookable_object2();
indvdum@0
   690
indvdum@0
   691
$phpbb_hook-&gt;add_hook(array('my_hookable_object2', 'hook_me'));
indvdum@0
   692
indvdum@0
   693
$phpbb_hook-&gt;register(array('my_hookable_object2', 'hook_me'), 'hooking');
indvdum@0
   694
indvdum@0
   695
echo $first_object-&gt;hook_me('first', 'second', false) . '&lt;br /&gt;';
indvdum@0
   696
echo $second_object-&gt;hook_me('first', 'second') . '&lt;br /&gt;';
indvdum@0
   697
		</pre></div>
indvdum@0
   698
indvdum@0
   699
		<p>OUTPUT:</p>
indvdum@0
   700
indvdum@0
   701
		<div class="codebox"><pre>
indvdum@0
   702
not hooked
indvdum@0
   703
hooked
indvdum@0
   704
		</pre></div>
indvdum@0
   705
indvdum@0
   706
		</div>
indvdum@0
   707
indvdum@0
   708
		<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>
indvdum@0
   709
indvdum@0
   710
		<span class="corners-bottom"><span></span></span></div>
indvdum@0
   711
	</div>
indvdum@0
   712
indvdum@0
   713
	<a name="return"></a><h2>4. Result returning</h2>
indvdum@0
   714
indvdum@0
   715
	<div class="paragraph">
indvdum@0
   716
		<div class="inner"><span class="corners-top"><span></span></span>
indvdum@0
   717
indvdum@0
   718
		<div class="content">
indvdum@0
   719
indvdum@0
   720
<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>
indvdum@0
   721
indvdum@0
   722
<h3>Case 1 - Returning the result</h3>
indvdum@0
   723
indvdum@0
   724
<p>Imagine the following function supporting hooks:</p>
indvdum@0
   725
indvdum@0
   726
<div class="codebox"><pre>
indvdum@0
   727
function append_sid($url, $params = false, $is_amp = true, $session_id = false)
indvdum@0
   728
{
indvdum@0
   729
	global $_SID, $_EXTRA_URL, $phpbb_hook;
indvdum@0
   730
indvdum@0
   731
	// Developers using the hook function need to globalise the $_SID and $_EXTRA_URL on their own and also handle it appropiatly.
indvdum@0
   732
	// They could mimick most of what is within this function
indvdum@0
   733
	if ($phpbb_hook-&gt;call_hook(__FUNCTION__, $url, $params, $is_amp, $session_id))
indvdum@0
   734
	{
indvdum@0
   735
		if ($phpbb_hook-&gt;hook_return(__FUNCTION__))
indvdum@0
   736
		{
indvdum@0
   737
			return $phpbb_hook-&gt;hook_return_result(__FUNCTION__);
indvdum@0
   738
		}
indvdum@0
   739
	}
indvdum@0
   740
indvdum@0
   741
	[...]
indvdum@0
   742
}
indvdum@0
   743
</pre></div>
indvdum@0
   744
indvdum@0
   745
<p>Now, the following function is yours. Since you return a value, the append_sid() function itself is returning it as is:</p>
indvdum@0
   746
indvdum@0
   747
<div class="codebox"><pre>
indvdum@0
   748
// The function called
indvdum@0
   749
function my_append_sid(&amp;$hook, $url, $params = false, $is_amp = true, $session_id = false)
indvdum@0
   750
{
indvdum@0
   751
	// Get possible previous results
indvdum@0
   752
	$result = $hook-&gt;previous_hook_result('append_sid');
indvdum@0
   753
indvdum@0
   754
	return 'Since i return something the append_sid() function will return my result.';
indvdum@0
   755
}
indvdum@0
   756
</pre></div>
indvdum@0
   757
indvdum@0
   758
<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>
indvdum@0
   759
indvdum@0
   760
<h3>Case 2 - Not Returning any result</h3>
indvdum@0
   761
indvdum@0
   762
<p>Sometimes applications want to return nothing and therefore force the underlying function to continue it's execution:</p>
indvdum@0
   763
indvdum@0
   764
<div class="codebox"><pre>
indvdum@0
   765
function append_sid($url, $params = false, $is_amp = true, $session_id = false)
indvdum@0
   766
{
indvdum@0
   767
	global $_SID, $_EXTRA_URL, $phpbb_hook;
indvdum@0
   768
indvdum@0
   769
	// Developers using the hook function need to globalise the $_SID and $_EXTRA_URL on their own and also handle it appropiatly.
indvdum@0
   770
	// They could mimick most of what is within this function
indvdum@0
   771
	if ($phpbb_hook-&gt;call_hook(__FUNCTION__, $url, $params, $is_amp, $session_id))
indvdum@0
   772
	{
indvdum@0
   773
		if ($phpbb_hook-&gt;hook_return(__FUNCTION__))
indvdum@0
   774
		{
indvdum@0
   775
			return $phpbb_hook-&gt;hook_return_result(__FUNCTION__);
indvdum@0
   776
		}
indvdum@0
   777
	}
indvdum@0
   778
indvdum@0
   779
	[...]
indvdum@0
   780
}
indvdum@0
   781
indvdum@0
   782
// The function called
indvdum@0
   783
function my_append_sid(&amp;$hook, $url, $params = false, $is_amp = true, $session_id = false)
indvdum@0
   784
{
indvdum@0
   785
	// Get possible previous results
indvdum@0
   786
	$result = $hook-&gt;previous_hook_result('append_sid');
indvdum@0
   787
indvdum@0
   788
	[...]
indvdum@0
   789
indvdum@0
   790
	// I only rewrite some variables, but return nothing. Therefore, the append_sid() function will not return my (non)result.
indvdum@0
   791
}
indvdum@0
   792
</pre></div>
indvdum@0
   793
indvdum@0
   794
<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>
indvdum@0
   795
indvdum@0
   796
<div class="codebox"><pre>
indvdum@0
   797
// The function called
indvdum@0
   798
function my_append_sid(&amp;$hook, $url, $params = false, $is_amp = true, $session_id = false)
indvdum@0
   799
{
indvdum@0
   800
	// Get possible previous results
indvdum@0
   801
	$result = $hook-&gt;previous_hook_result('append_sid');
indvdum@0
   802
indvdum@0
   803
	// $result is not filled
indvdum@0
   804
indvdum@0
   805
	return 'FILLED';
indvdum@0
   806
}
indvdum@0
   807
indvdum@0
   808
// This function is registered too and gets executed after my_append_sid()
indvdum@0
   809
function my_own_append_sid(&amp;$hook, $url, $params = false, $is_amp = true, $session_id = false)
indvdum@0
   810
{
indvdum@0
   811
	$result = $hook->previous_hook_result('append_sid');
indvdum@0
   812
indvdum@0
   813
	// $result is actually filled with $result['result'] = 'FILLED'
indvdum@0
   814
	// But i return nothing, therefore append_sid() continues it's execution.
indvdum@0
   815
}
indvdum@0
   816
indvdum@0
   817
// The way both functions are registered.
indvdum@0
   818
$phpbb_hook->register('append_sid', 'my_append_sid');
indvdum@0
   819
$phpbb_hook->register('append_sid', 'my_own_append_sid');
indvdum@0
   820
</pre></div>
indvdum@0
   821
indvdum@0
   822
		</div>
indvdum@0
   823
indvdum@0
   824
		<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>
indvdum@0
   825
indvdum@0
   826
		<span class="corners-bottom"><span></span></span></div>
indvdum@0
   827
	</div>
indvdum@0
   828
indvdum@0
   829
	<a name="embed"></a><h2>5. Embedding your hook files/classes/methods</h2>
indvdum@0
   830
indvdum@0
   831
	<div class="paragraph">
indvdum@0
   832
		<div class="inner"><span class="corners-top"><span></span></span>
indvdum@0
   833
indvdum@0
   834
		<div class="content">
indvdum@0
   835
indvdum@0
   836
<p>There are basically two methods you are able to choose from:</p>
indvdum@0
   837
indvdum@0
   838
<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>
indvdum@0
   839
indvdum@0
   840
<p>Please be aware that you need to purge your cache within the ACP to make your newly placed file available to phpBB3.</p>
indvdum@0
   841
indvdum@0
   842
<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>
indvdum@0
   843
indvdum@0
   844
<div class="codebox"><pre>
indvdum@0
   845
// My function which gets executed within the hooks constuctor
indvdum@0
   846
function phpbb_hook_register(&amp;$hook)
indvdum@0
   847
{
indvdum@0
   848
	$hook-&gt;register('append_sid', 'my_append_sid');
indvdum@0
   849
}
indvdum@0
   850
indvdum@0
   851
[...]
indvdum@0
   852
</pre></div>
indvdum@0
   853
indvdum@0
   854
<p>You should get the idea. ;)</p>
indvdum@0
   855
indvdum@0
   856
		</div>
indvdum@0
   857
indvdum@0
   858
		<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>
indvdum@0
   859
indvdum@0
   860
		<span class="corners-bottom"><span></span></span></div>
indvdum@0
   861
	</div>
indvdum@0
   862
indvdum@0
   863
	<a name="disclaimer"></a><h2>6. Copyright and disclaimer</h2>
indvdum@0
   864
indvdum@0
   865
	<div class="paragraph">
indvdum@0
   866
		<div class="inner"><span class="corners-top"><span></span></span>
indvdum@0
   867
indvdum@0
   868
		<div class="content">
indvdum@0
   869
indvdum@0
   870
	<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) <a href="http://www.phpbb.com/">phpBB Group</a>, All Rights Reserved.</p>
indvdum@0
   871
indvdum@0
   872
		</div>
indvdum@0
   873
indvdum@0
   874
		<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>
indvdum@0
   875
indvdum@0
   876
		<span class="corners-bottom"><span></span></span></div>
indvdum@0
   877
	</div>
indvdum@0
   878
indvdum@0
   879
	<div id="page-footer">
indvdum@0
   880
		<div class="version">&nbsp;</div>
indvdum@0
   881
	</div>
indvdum@0
   882
</div></div>
indvdum@0
   883
indvdum@0
   884
<div>
indvdum@0
   885
	<a id="bottom" name="bottom" accesskey="z"></a>
indvdum@0
   886
</div>
indvdum@0
   887
indvdum@0
   888
</body>
indvdum@0
   889
</html>