Implementating hooks in node bodies

hefox's picture

enable and configure php module

Create a node, set to php format.

Reset the module implements cache

Implement a hook for a module in node body that the module doesn't implement and that hasn't been invoked yet.

Example:

<?php
module_implements
('nodeapi'FALSE, TRUE);
function
php_nodeapi(&$node, $op) {
   if (
$node->nid == arg(1) && $op == 'alter') {
    
$node->body = "I change the content via php_nodeapi op = alter defined in the node body. >:O";
   }
}
?>

http://demo.foxinbox.org/node/7

I think that just killed a few kitties...

Comments

I like it, but

LittleViking's picture

If PHP module ever implements hook_nodeapi, then we'll get fatal errors. And any site awesome enough to implement hooks inside of node bodies, on behalf of other modules, should never be in danger of going down.

If we lie to Drupal about an imaginary module implementing the hook, we'll be a little bit safer. (As long as we never download a module that shares a name with our evil hook.)

<?php
$modules
= module_list();
$modules['my_kitten_killing_node'] = 'my_kitten_killing_node';
module_list(TRUE, FALSE, TRUE, $modules);

module_implements('nodeapi'FALSE, TRUE);
function
my_kitten_killing_node_nodeapi(&$node, $op) {
   if (
$node->nid == arg(1) && $op == 'alter') {
    
$node->body = "I change the content via my_kitten_killing_node_nodeapi op = alter defined in the node body. >:O";
   }
}
?>

And if you want to be even safer, you can generate a module name that definitely doesn't exist and then use eval() to call the hook!

<?php
$modules
= module_list();

$i = 0;
do {
 
$i++;
 
$fake_module = 'kitten_killing_node_' . $i;
}
while (
in_array($fake_module, $modules));

$modules[$fake_module] = $fake_module;
module_list(TRUE, FALSE, TRUE, $modules);

module_implements('nodeapi'FALSE, TRUE);
eval(
'
function '
. $fake_module . '_nodeapi(&$node, $op) {
   if ($node->nid == arg(1) && $op == "alter") {
     $node->body = "I change the content via '
. $fake_module . '_nodeapi op = alter defined in the node body. >:O";
   }
}
'
);
?>

A few months ago, I recall

Garrett Albright's picture

A few months ago, I recall having a conversation in #drupal about how Drupal could be used as a combination development platform, live application framework and VCS by using nodes using the PHP filter along with Drupal's revision tracking and the Diff module. It's nice to see that others have embraced the idea and more progress is being made along these lines.

Drupal for Evil

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: