Hooks
From MemHT Wiki
Contents |
Hooks allow to users to inject and run customized code into certain points of the MemHT engine (or even replace parts of it) without editing any core file. Imagine how would be nice to replace the MemHT login system with your own, change the downloads page layout, add new BBCode tags or interface the accounts creation with a third party script, just uploading a file on your site. All this (and a lot more) is possible with hooks, permitting to developers to create easy-installable extensions.
How do hooks work?
Let's see a simple and practical example:
function foo($id) { global $dblink; $row = $dblink->get_row("SELECT text FROM memht_foo WHERE id=$id"); $text = outCode($row['text']); return $text }
This function returns a text string.
What happen when you want to modify it?
The easiest solution would be something like this:
function foo($id) { global $dblink; $row = $dblink->get_row("SELECT text FROM memht_foo WHERE id=$id"); $text = outCode($row['text']); $text = myModFunction($text); return $text }
This works of course, but there are at least 2 problems:
- You need to modify the MemHT core files
- After every update of your portal, you would need to modify all files again!
The solution? Hooks obviously:
function foo($text) { global $dblink; if (memRunHooks('EventName',array($id))) { $row = $dblink->get_row("SELECT text FROM memht_foo WHERE id=$id"); $text = outCode($row['text']); memRunHooks('EventNameEnd',array($id,&$text)); } return $text; }
In order to be used, hooks have to be associated to the events defined in MemHT. In the previous example we have 2 of them: EventName and EventNameEnd:
EventName permits to place your code at the beginning of the function (or to completely hijack it, executing the custom code only) while EventNameEnd permit to append the code at the end of the function.
Simple extensions and mods with hooks
Note: Extensions and mods using hooks, in order to be recognized by the MemHT engine, must be placed in the hooks folder with the following name format: name.hook.php
Let's see some example:
<?php $memHooks['ViewArticleEnd'][] = "putBannerInArticles"; //The $memHooks array content is the NAME of the function to run function putBannerInArticles($args) { //This banner will be shown in the articles page, after the main text showBanner(N); } ?>
<?php $memHooks['ViewArticle'][] = "putBannerInArticles"; //The $memHooks array content is the NAME of the function to run function putBannerInArticles($args) { //This banner will be shown in the articles page, before the main text showBanner(N); } ?>
<?php $memHooks['ViewArticle'][] = "showMyCustomArtPage"; function showMyCustomArtPage($args) { echo "<div>Article title: ".$args[2]."</div>"; echo "<div>Author: ".$args[5]."</div>"; return false; //Returning false, the original function content will not be executed, showing the injected code only } ?>
<?php $memHooks['SiteTitle'][] = "hooksTest"; function hooksTest($args) { $args[0] .= "HI, MY NAME IS HOOK - "; } ?>
This code will add the text "HI, MY NAME IS HOOK - " in your site title. Very simple right?
Hooks list
| Generic events | |
| Event | Defined in... Arguments |
| ActivateUser | pages/users/index.php |
| $user [0], $pass [1], $email [2] | |
| ActivateUserEnd | pages/users/index.php |
| $user [0], $pass [1], $email [2] | |
| ActivateUserMd5 | pages/users/index.php |
| $user [0], $pass [1], $email [2] | |
| ActivateUserMd5End | pages/users/index.php |
| $user [0], $pass [1], $email [2] | |
| AddEmailToNewsletter | admin/pages/users/index.php, pages/newsletter/index.php, pages/users/index.php |
| $email [0] | |
| AddEmailToNewsletterEnd | admin/pages/users/index.php, pages/newsletter/index.php, pages/users/index.php |
| $email [0] | |
| AdminPostLoginCheck | admin/inc_login.php |
| &$privs [0] | |
| AdminPostLoginCheckEnd | admin/inc_login.php |
| &$privs [0] | |
| BBCode | inc/inc_bbcode.php |
| &$string [0] | |
| BBCodeEnd | inc/inc_bbcode.php |
| &$string [0] | |
| BBCodeMini | inc/inc_bbcode.php |
| &$string [0] | |
| BBCodeMiniEnd | inc/inc_bbcode.php |
| &$string [0] | |
| DefaultHome | index.php |
| - | |
| DefaultHomeEnd | index.php |
| - | |
| HighlightCode | inc/inc_functions.php |
| &$string [0] | |
| HighlightCodeEnd | inc/inc_functions.php |
| &$string [0] | |
| IsAuth Starting from 4.0.1 | inc/inc_functions.php |
| $userid [0], $force [1] = 0default value, &$privs [2] - Function must return true or false | |
| IsUser | inc/inc_functions.php, cron.php |
| $userid [0], $force [1] = 0default value, &$privs [2] - Function must return true or false | |
| JavaScript | inc/inc_javascript.php |
| - | |
| JavaScriptEnd | inc/inc_javascript.php |
| - | |
| Maintenance | inc/inc_functions.php, cron.php |
| $forcedexec [0] = falsedefault value | |
| MaintenanceEnd | inc/inc_functions.php, cron.php |
| $forcedexec [0] = falsedefault value | |
| MetaTags | inc/inc_header.php |
| - | |
| MetaTagsEnd | inc/inc_header.php |
| - | |
| ModRewriteReplace | inc/inc_modrewrite.php |
| &$string [0] | |
| ModRewriteReplaceEnd | inc/inc_modrewrite.php |
| &$string [0] | |
| PvtMsgNotification | inc/inc_header.php |
| - | |
| PvtMsgNotificationEnd | inc/inc_header.php |
| - | |
| RemoveEmailFromNewsletter | admin/pages/newsletter/index.php, pages/newsletter/index.php |
| $email [0] | |
| RemoveEmailFromNewsletterEnd | admin/pages/newsletter/index.php, pages/newsletter/index.php |
| $email [0] | |
| RssSyndication | inc/inc_header.php |
| - | |
| RssSyndicationEnd | inc/inc_header.php |
| - | |
| SiteHtmlClean | inc/inc_htmlclean.php |
| &$string [0] | |
| SiteHtmlCleanEnd | inc/inc_htmlclean.php |
| &$string [0] | |
| SiteTitle | inc/inc_header.php |
| &$sitetitle [0] | |
| SiteTitleEnd | inc/inc_header.php |
| &$sitetitle [0] | |
| ShowPath | inc/inc_functions.php |
| &$path [0] | |
| ShowPathEnd | inc/inc_functions.php |
| &$path [0] | |
| StyleSheets Starting from 4.0.1 | inc/inc_header.php |
| - | |
| UserLogout | inc/inc_login.php |
| &$privs [0] | |
| UserLogoutEnd | inc/inc_login.php |
| &$privs [0] | |
| UserPostLoginCheck | inc/inc_login.php |
| &$privs [0] | |
| UserPostLoginCheckEnd | inc/inc_login.php |
| &$privs [0] | |
| ViewArticle | pages/articles/index.php |
| $id [0], $argument [1], $title [2], $description [3], $text [4], $author [5], $date [6], $hits [7], $rank [8] | |
| ViewArticleEnd | pages/articles/index.php |
| $id [0], $argument [1], $title [2], $description [3], $text [4], $author [5], $date [6], $hits [7], $rank [8] | |
| ViewFile | pages/download/index.php |
| $id [0], $title [1], $url [2], $description [3], $demolink [4], $version [5], $author [6],$date [7], $dimension [8], $hits [9], $download [10], $permission [11], $group [12], $rank [13] | |
| ViewFileEnd | pages/download/index.php |
| $id [0], $title [1], $url [2], $description [3], $demolink [4], $version [5], $author [6],$date [7], $dimension [8], $hits [9], $download [10], $permission [11], $group [12], $rank [13] | |
| ViewForumPost | pages/forum/index.php |
| &$postStructure [0], $id [1], $title [2], $text [3], $author [4], $date [5], $edited [6], $reason [7], $ip [8], $attachment [9], $status [10] | |
| ViewForumPostEnd | pages/forum/index.php |
| &$postStructure [0], $id [1], $title [2], $text [3], $author [4], $date [5], $edited [6], $reason [7], $ip [8], $attachment [9], $status [10] | |
| ViewGuide | pages/guide/index.php |
| $id [0], $argument [1], $title [2], $description [3], $text [4], $author [5], $date [6], $hits [7], $rank [8] | |
| ViewGuideEnd | pages/guide/index.php |
| $id [0], $argument [1], $title [2], $description [3], $text [4], $author [5], $date [6], $hits [7], $rank [8] | |
| Administration CP events | |
| Event | Defined in... Arguments |
| AddAdmin Starting from 4.0.1 | admin/pages/administratoris/index.php |
| $userid [0] | |
| AddAdminEnd Starting from 4.0.1 | admin/pages/administratoris/index.php |
| $userid [0] | |
| AddArticle | admin/pages/articles/index.php |
| $title [0], $argument [1], $description [2], $text [3], $author [4], $email [5] | |
| AddArticleEnd | admin/pages/articles/index.php |
| $title [0], $argument [1], $description [2], $text [3], $author [4], $email [5] | |
| AddBlogPost | admin/pages/blog/index.php |
| $title [0], $category [1], $home_text [2], $full_text [3], $author [4] | |
| AddBlogPostEnd | admin/pages/blog/index.php |
| $title [0], $category [1], $home_text [2], $full_text [3], $author [4] | |
| AddFile | admin/pages/download/index.php |
| $category [0], $title [1], $url [2], $description [3], $author [4], $demolink [5], $version [6], $dimension [7], $permission [8], $group [9] | |
| AddFileEnd | admin/pages/download/index.php |
| $category [0], $title [1], $url [2], $description [3], $author [4], $demolink [5], $version [6], $dimension [7], $permission [8], $group [9] | |
| AddGuide | admin/pages/guide/index.php |
| $title [0], $argument [1], $description [2], $text [3], $author [4], $email [5] | |
| AddGuideEnd | admin/pages/guide/index.php |
| $title [0], $argument [1], $description [2], $text [3], $author [4], $email [5] | |
| AddLink | admin/pages/mylinks/index.php |
| $category [0], $title [1], $url [2], $email [3], $description [4] | |
| AddLinkEnd | admin/pages/mylinks/index.php |
| $category [0], $title [1], $url [2], $email [3], $description [4] | |
| AddNews | admin/pages/news/index.php |
| $title [0], $argument [1], $hometext [2], $fulltext [3], $author [4], $email [5] | |
| AddNewsEnd | admin/pages/news/index.php |
| $title [0], $argument [1], $hometext [2], $fulltext [3], $author [4], $email [5] | |
| AddUserByAdmin | admin/pages/users/index.php |
| $user [0], $pass [1], $email [2] | |
| AddUserByAdminEnd | admin/pages/users/index.php |
| $user [0], $pass [1], $email [2] | |
| ApproveArticle | admin/pages/submission/index.php |
| $title [0], $argument [1], $description [2], $text [3], $author [4], $email [5] | |
| ApproveArticleEnd | admin/pages/submission/index.php |
| $title [0], $argument [1], $description [2], $text [3], $author [4], $email [5] | |
| ApproveBlogPost | admin/pages/submission/index.php |
| $title [0], $category [1], $home_text [2], $full_text [3], $author [4] | |
| ApproveBlogPostEnd | admin/pages/submission/index.php |
| $title [0], $category [1], $home_text [2], $full_text [3], $author [4] | |
| ApproveFile | admin/pages/submission/index.php |
| $category [0], $title [1], $url [2], $description [3], $author [4], $demolink [5], $version [6], $dimension [7], $permission [8], $group [9] | |
| ApproveFileEnd | admin/pages/submission/index.php |
| $category [0], $title [1], $url [2], $description [3], $author [4], $demolink [5], $version [6], $dimension [7], $permission [8], $group [9] | |
| ApproveGuide | admin/pages/submission/index.php |
| $title [0], $argument [1], $description [2], $text [3], $author [4], $email [5] | |
| ApproveGuideEnd | admin/pages/submission/index.php |
| $title [0], $argument [1], $description [2], $text [3], $author [4], $email [5] | |
| ApproveLink | admin/pages/submission/index.php |
| $category [0], $title [1], $url [2], $email [3], $description [4] | |
| ApproveLinkEnd | admin/pages/submission/index.php |
| $category [0], $title [1], $url [2], $email [3], $description [4] | |
| ApproveNews | admin/pages/submission/index.php |
| $title [0], $argument [1], $hometext [2], $fulltext [3], $author [4], $email [5] | |
| ApproveNewsEnd | admin/pages/submission/index.php |
| $title [0], $argument [1], $hometext [2], $fulltext [3], $author [4], $email [5] | |
| ApproveUser | admin/pages/users/index.php |
| $user [0], $pass [1], $email [2] | |
| ApproveUserEnd | admin/pages/users/index.php |
| $user [0], $pass [1], $email [2] | |
| EditAdmin Starting from 4.0.1 | admin/pages/administratoris/index.php |
| $userid [0] | |
| EditAdminEnd Starting from 4.0.1 | admin/pages/administratoris/index.php |
| $userid [0] | |
| DeleteAdmin | admin/pages/administratoris/index.php |
| $userid [0] | |
| DeleteAdminEnd | admin/pages/administratoris/index.php |
| $userid [0] | |
| Languages | العربية • Bahasa Indonesia • Bosanski • Български • Dansk • Deutsch • English • Español • فارسی • Français • Galego • עברית • Italiano • Magyar • Македонски • Nederlands • Português • Русский • Српски/Srpski • Svenska • Türkçe • Українська • Tiếng Việt |
