| 1 |
<?php
|
| 2 |
|
| 3 |
// $Id: hooker.module,v 1.1 2008/05/07 02:42:36 eaton Exp $
|
| 4 |
|
| 5 |
/**
|
| 6 |
* Hooker 1.0
|
| 7 |
*
|
| 8 |
* Normally, implementations of Drupal hooks must be defined in modules explicitly.
|
| 9 |
* Hooker allows administrators to paste arbitrary chunks of PHP in via the admin
|
| 10 |
* interface, to be executed when specific hooks are run.
|
| 11 |
*
|
| 12 |
* Please note that this module is NOT capable of exposing install or schema
|
| 13 |
* related hooks. It's probably not compatible with any kinds of best practices, or
|
| 14 |
* good security practices either. Let's be honest: if you're running a production
|
| 15 |
* site, you probably shouldn't be running this module.
|
| 16 |
*/
|
| 17 |
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Implementation of hook_menu.
|
| 21 |
* Defines menu callbacks for Hooker's configuration pages.
|
| 22 |
*/
|
| 23 |
function hooker_menu() {
|
| 24 |
$items = array(
|
| 25 |
'admin/settings/hooker' => array(
|
| 26 |
'title' => 'Hooker',
|
| 27 |
'description' => 'Define arbitrary code to execute when Drupal hooks are called.',
|
| 28 |
'page callback' => 'drupal_get_form',
|
| 29 |
'page arguments' => array('hooker_admin_form'),
|
| 30 |
'access callback' => 'user_access',
|
| 31 |
'access arguments' => array('administer hooker'),
|
| 32 |
'file' => 'hooker.admin.inc',
|
| 33 |
),
|
| 34 |
'admin/settings/hooker/edit' => array(
|
| 35 |
'title' => 'Edit hook',
|
| 36 |
'page callback' => 'drupal_get_form',
|
| 37 |
'page arguments' => array('hooker_edit'),
|
| 38 |
'access callback' => 'user_access',
|
| 39 |
'access arguments' => array('administer hooker'),
|
| 40 |
'file' => 'hooker.admin.inc',
|
| 41 |
'type' => MENU_CALLBACK,
|
| 42 |
),
|
| 43 |
'admin/settings/hooker/add' => array(
|
| 44 |
'title' => 'Add hook',
|
| 45 |
'page callback' => 'drupal_get_form',
|
| 46 |
'page arguments' => array('hooker_edit'),
|
| 47 |
'access callback' => 'user_access',
|
| 48 |
'access arguments' => array('administer hooker'),
|
| 49 |
'file' => 'hooker.admin.inc',
|
| 50 |
'type' => MENU_CALLBACK,
|
| 51 |
),
|
| 52 |
'admin/settings/hooker/delete' => array(
|
| 53 |
'title' => 'Delete hook',
|
| 54 |
'page callback' => 'drupal_get_form',
|
| 55 |
'page arguments' => array('hooker_delete_confirm'),
|
| 56 |
'access callback' => 'user_access',
|
| 57 |
'access arguments' => array('administer hooker'),
|
| 58 |
'file' => 'hooker.admin.inc',
|
| 59 |
'type' => MENU_CALLBACK,
|
| 60 |
),
|
| 61 |
);
|
| 62 |
return $items;
|
| 63 |
}
|
| 64 |
|
| 65 |
|
| 66 |
/**
|
| 67 |
* Implementation of hook_theme.
|
| 68 |
* Registers the theme function to handle the hooker_admin_form.
|
| 69 |
*/
|
| 70 |
function hooker_theme() {
|
| 71 |
return array(
|
| 72 |
'hooker_admin_form' => array(
|
| 73 |
'arguments' => array('form' => array()),
|
| 74 |
'file' => 'hooker.admin.inc',
|
| 75 |
),
|
| 76 |
);
|
| 77 |
}
|
| 78 |
|
| 79 |
|
| 80 |
/**
|
| 81 |
* Implementation of hook_init.
|
| 82 |
* Inserts arbitrary hooks into the global PHP namespace, as early in the bootstrap
|
| 83 |
* process as possible.
|
| 84 |
*/
|
| 85 |
function hooker_init() {
|
| 86 |
if (arg(0) != 'admin') {
|
| 87 |
foreach (_hooker_hooks() as $key => $hook) {
|
| 88 |
if (!function_exists('hooker_'. $hook->hook)) {
|
| 89 |
$function = "function hooker_{$hook->hook}({$hook->params}) {\n$hook->code\n}";
|
| 90 |
eval($function);
|
| 91 |
}
|
| 92 |
}
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
|
| 97 |
/**
|
| 98 |
* Internal helper function to load defined hooks.
|
| 99 |
*/
|
| 100 |
function _hooker_hooks($reset = FALSE) {
|
| 101 |
static $hooker_data;
|
| 102 |
if (!isset($hooker_data) || $reset) {
|
| 103 |
if (!$reset && ($cache = cache_get('hooker:cached_hooks')) && !empty($cache->data)) {
|
| 104 |
$hooker_data = $cache->data;
|
| 105 |
}
|
| 106 |
else {
|
| 107 |
$hooker_data = array();
|
| 108 |
// Retrieve the actual hooks from the database.
|
| 109 |
$results = db_query("SELECT * FROM {hooker}");
|
| 110 |
while ($hook = db_fetch_object($results)) {
|
| 111 |
$hooker_data[$hook->hook] = $hook;
|
| 112 |
}
|
| 113 |
cache_set('hooker:cached_hooks', $hooker_data);
|
| 114 |
}
|
| 115 |
}
|
| 116 |
return $hooker_data;
|
| 117 |
}
|