Post: [TUT] Understanding The Basics of Hookers
05-10-2011, 01:32 AM #1
(adsbygoogle = window.adsbygoogle || []).push({});
Originally posted by 039

Not literally speaking of hookers, but file hooking to a specific file in PHP. You may be familiar to the plugin system of MyBB. It's somewhat hard to understand and code. Once you know the logic behind it, you can do it very easily. First of all we need to create a function that will run a specific hook. This tutorial is somewhat OOP based.

Now, this is tutorial is a plugin system. We're going to name the class plugins. With an array variable named 'hook'

    
<?php
class plugins
{
var $hooks = array();
}
?>


First we're going to create a function that will add a hook to specific hook name.

    
<?php
class plugins
{
var $hooks = array();

public function addHook($hookName, $hookFile, $hookFunction)
{
// Check if the file is already attached to the hook
if (isset($this->hooks[$hookName][$hookFile])) {
return true;
}
// Add the hook
$this->hooks[$hookName][$hookFunction] = array('file' => $hookFile, 'function' =>
$hookFunction);

return true;
}
}
?>


After setting up the function, we can now use it.

    
require_once "plugins.php"

$p = new plugins;
$p->addHook('this', 'helloWorld', 'helloWorld'Winky Winky;


After setting up the hook, the only thing left is to run the hook

    
<?php
class plugins
{
var $hooks = array();

public function addHook($hookName, $hookFile, $hookFunction)
{
// Check if the file is already attached to the hook
if (isset($this->hooks[$hookName][$hookFile])) {
return true;
}
// Add the hook
$this->hooks[$hookName][$hookFunction] = array('file' => $hookFile, 'function' =>
$hookFunction);

return true;
}

public function runHook($hookName)
{
if (!is_array($this->hooks[$hookName])) {
return false;
}

foreach ($this->hooks[$hookName] as $function) {
if (!is_array($function)) {
return true;
}

if(file_exists($function->file . ".php")) {
return true;
} else {
require_once $function[$file] . ".php";
eval("{$function[$function]}"); // Run the function
}

}

}
}
?>


Now, in helloWorld.php we should have a function called helloWorld

    
function helloWorld() {
echo "Hello World";
}


The plugin class and the helloWorld must be in the same folder. Everything else is self explanatory.

Cheers!,
-n1tr0b


source You must login or register to view this content.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo