Hooks

From MemHT Wiki

Jump to: navigation, search
Image:Search.png‎ Available starting from the 4.0.0 version

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:

  1. You need to modify the MemHT core files
  2. 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
EventDefined in...
Arguments
ActivateUserpages/users/index.php
$user [0], $pass [1], $email [2]
ActivateUserEndpages/users/index.php
$user [0], $pass [1], $email [2]
ActivateUserMd5pages/users/index.php
$user [0], $pass [1], $email [2]
ActivateUserMd5Endpages/users/index.php
$user [0], $pass [1], $email [2]
AddEmailToNewsletteradmin/pages/users/index.php, pages/newsletter/index.php, pages/users/index.php
$email [0]
AddEmailToNewsletterEndadmin/pages/users/index.php, pages/newsletter/index.php, pages/users/index.php
$email [0]
AdminPostLoginCheckadmin/inc_login.php
&$privs [0]
AdminPostLoginCheckEndadmin/inc_login.php
&$privs [0]
BBCodeinc/inc_bbcode.php
&$string [0]
BBCodeEndinc/inc_bbcode.php
&$string [0]
BBCodeMiniinc/inc_bbcode.php
&$string [0]
BBCodeMiniEndinc/inc_bbcode.php
&$string [0]
DefaultHomeindex.php
-
DefaultHomeEndindex.php
-
HighlightCodeinc/inc_functions.php
&$string [0]
HighlightCodeEndinc/inc_functions.php
&$string [0]
IsAuth Starting from 4.0.1inc/inc_functions.php
$userid [0], $force [1] = 0default value, &$privs [2] - Function must return true or false
IsUserinc/inc_functions.php, cron.php
$userid [0], $force [1] = 0default value, &$privs [2] - Function must return true or false
JavaScriptinc/inc_javascript.php
-
JavaScriptEndinc/inc_javascript.php
-
Maintenanceinc/inc_functions.php, cron.php
$forcedexec [0] = falsedefault value
MaintenanceEndinc/inc_functions.php, cron.php
$forcedexec [0] = falsedefault value
MetaTagsinc/inc_header.php
-
MetaTagsEndinc/inc_header.php
-
ModRewriteReplaceinc/inc_modrewrite.php
&$string [0]
ModRewriteReplaceEndinc/inc_modrewrite.php
&$string [0]
PvtMsgNotificationinc/inc_header.php
-
PvtMsgNotificationEndinc/inc_header.php
-
RemoveEmailFromNewsletteradmin/pages/newsletter/index.php, pages/newsletter/index.php
$email [0]
RemoveEmailFromNewsletterEndadmin/pages/newsletter/index.php, pages/newsletter/index.php
$email [0]
RssSyndicationinc/inc_header.php
-
RssSyndicationEndinc/inc_header.php
-
SiteHtmlCleaninc/inc_htmlclean.php
&$string [0]
SiteHtmlCleanEndinc/inc_htmlclean.php
&$string [0]
SiteTitleinc/inc_header.php
&$sitetitle [0]
SiteTitleEndinc/inc_header.php
&$sitetitle [0]
ShowPathinc/inc_functions.php
&$path [0]
ShowPathEndinc/inc_functions.php
&$path [0]
StyleSheets Starting from 4.0.1inc/inc_header.php
-
UserLogoutinc/inc_login.php
&$privs [0]
UserLogoutEndinc/inc_login.php
&$privs [0]
UserPostLoginCheckinc/inc_login.php
&$privs [0]
UserPostLoginCheckEndinc/inc_login.php
&$privs [0]
ViewArticlepages/articles/index.php
$id [0], $argument [1], $title [2], $description [3], $text [4], $author [5], $date [6], $hits [7], $rank [8]
ViewArticleEndpages/articles/index.php
$id [0], $argument [1], $title [2], $description [3], $text [4], $author [5], $date [6], $hits [7], $rank [8]
ViewFilepages/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]
ViewFileEndpages/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]
ViewForumPostpages/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]
ViewForumPostEndpages/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]
ViewGuidepages/guide/index.php
$id [0], $argument [1], $title [2], $description [3], $text [4], $author [5], $date [6], $hits [7], $rank [8]
ViewGuideEndpages/guide/index.php
$id [0], $argument [1], $title [2], $description [3], $text [4], $author [5], $date [6], $hits [7], $rank [8]
 
Administration CP events
EventDefined in...
Arguments
AddAdmin Starting from 4.0.1admin/pages/administratoris/index.php
$userid [0]
AddAdminEnd Starting from 4.0.1admin/pages/administratoris/index.php
$userid [0]
AddArticleadmin/pages/articles/index.php
$title [0], $argument [1], $description [2], $text [3], $author [4], $email [5]
AddArticleEndadmin/pages/articles/index.php
$title [0], $argument [1], $description [2], $text [3], $author [4], $email [5]
AddBlogPostadmin/pages/blog/index.php
$title [0], $category [1], $home_text [2], $full_text [3], $author [4]
AddBlogPostEndadmin/pages/blog/index.php
$title [0], $category [1], $home_text [2], $full_text [3], $author [4]
AddFileadmin/pages/download/index.php
$category [0], $title [1], $url [2], $description [3], $author [4], $demolink [5], $version [6], $dimension [7], $permission [8], $group [9]
AddFileEndadmin/pages/download/index.php
$category [0], $title [1], $url [2], $description [3], $author [4], $demolink [5], $version [6], $dimension [7], $permission [8], $group [9]
AddGuideadmin/pages/guide/index.php
$title [0], $argument [1], $description [2], $text [3], $author [4], $email [5]
AddGuideEndadmin/pages/guide/index.php
$title [0], $argument [1], $description [2], $text [3], $author [4], $email [5]
AddLinkadmin/pages/mylinks/index.php
$category [0], $title [1], $url [2], $email [3], $description [4]
AddLinkEndadmin/pages/mylinks/index.php
$category [0], $title [1], $url [2], $email [3], $description [4]
AddNewsadmin/pages/news/index.php
$title [0], $argument [1], $hometext [2], $fulltext [3], $author [4], $email [5]
AddNewsEndadmin/pages/news/index.php
$title [0], $argument [1], $hometext [2], $fulltext [3], $author [4], $email [5]
AddUserByAdminadmin/pages/users/index.php
$user [0], $pass [1], $email [2]
AddUserByAdminEndadmin/pages/users/index.php
$user [0], $pass [1], $email [2]
ApproveArticleadmin/pages/submission/index.php
$title [0], $argument [1], $description [2], $text [3], $author [4], $email [5]
ApproveArticleEndadmin/pages/submission/index.php
$title [0], $argument [1], $description [2], $text [3], $author [4], $email [5]
ApproveBlogPostadmin/pages/submission/index.php
$title [0], $category [1], $home_text [2], $full_text [3], $author [4]
ApproveBlogPostEndadmin/pages/submission/index.php
$title [0], $category [1], $home_text [2], $full_text [3], $author [4]
ApproveFileadmin/pages/submission/index.php
$category [0], $title [1], $url [2], $description [3], $author [4], $demolink [5], $version [6], $dimension [7], $permission [8], $group [9]
ApproveFileEndadmin/pages/submission/index.php
$category [0], $title [1], $url [2], $description [3], $author [4], $demolink [5], $version [6], $dimension [7], $permission [8], $group [9]
ApproveGuideadmin/pages/submission/index.php
$title [0], $argument [1], $description [2], $text [3], $author [4], $email [5]
ApproveGuideEndadmin/pages/submission/index.php
$title [0], $argument [1], $description [2], $text [3], $author [4], $email [5]
ApproveLinkadmin/pages/submission/index.php
$category [0], $title [1], $url [2], $email [3], $description [4]
ApproveLinkEndadmin/pages/submission/index.php
$category [0], $title [1], $url [2], $email [3], $description [4]
ApproveNewsadmin/pages/submission/index.php
$title [0], $argument [1], $hometext [2], $fulltext [3], $author [4], $email [5]
ApproveNewsEndadmin/pages/submission/index.php
$title [0], $argument [1], $hometext [2], $fulltext [3], $author [4], $email [5]
ApproveUseradmin/pages/users/index.php
$user [0], $pass [1], $email [2]
ApproveUserEndadmin/pages/users/index.php
$user [0], $pass [1], $email [2]
EditAdmin Starting from 4.0.1admin/pages/administratoris/index.php
$userid [0]
EditAdminEnd Starting from 4.0.1admin/pages/administratoris/index.php
$userid [0]
DeleteAdminadmin/pages/administratoris/index.php
$userid [0]
DeleteAdminEndadmin/pages/administratoris/index.php
$userid [0]


Image:Globe.png Languages العربيةBahasa IndonesiaBosanskiБългарскиDanskDeutschEnglishEspañolفارسیFrançaisGalegoעבריתItalianoMagyarМакедонскиNederlandsPortuguêsРусскийСрпски/SrpskiSvenskaTürkçeУкраїнськаTiếng Việt
Retrieved from "http://memht.com/wiki/Hooks"
Personal tools