An odd bug. If you use functions within PHP code on a content type created with the content construction kit the page itself will fail and all processing seems to stop and only the output of the page is output.

Try it:
create a new form of content with a title and body

Use the content to create a simple page with PHP code in it (don't forget the input format).

add a simple function ex:
function hello-world() {
return "hello";
}

CommentFileSizeAuthor
#6 drupal_eval.png46.18 KBjohnalbin

Comments

jonathan_hunt’s picture

Version: 6.x-1.x-dev » 4.7.x-1.x-dev
Component: General » content.module

Using // $Id: content.module,v 1.56.2.5 2006/08/04 13:47:50 JonBob Exp $

I just tested this by adding the hello_world function to a CCK content type but I get a fatal error:

Fatal error: Cannot redeclare hello_world() (previously declared in /home/jonathan/public_html/drupal47/includes/common.inc(1150) : eval()'d code:4) in /home/jonathan/public_html/drupal47/includes/common.inc(1150) : eval()'d code on line 6

jonathan_hunt’s picture

@jredding: Note that '-' is not a valid character for PHP function names (refer http://nz.php.net/manual/en/language.functions.php ). But I still get a fatal error when the function is named correctly... (see previous comment).

killes@www.drop.org’s picture

can confirm this. Probably the code gets evaluated twice.

jredding’s picture

Title: Bug when using PHP functions in content body » Sorry about my sloppy example

Sorry about my sloppy example I was just typing up something quick. Like you said even if named correctly the bug is still present.

Any ideas? When I get some spare time I'll try to track this down..

Any suggestions on where to look?

jredding’s picture

Title: Sorry about my sloppy example » Bug when using PHP functions in content body

whoops, didn't mean to change the title.. my fault.

johnalbin’s picture

Title: Bug when using PHP functions in content body » Core bug?
StatusFileSize
new46.18 KB

This may be a core bug. I don't have CCK installed, but when I enable nodewords and put a function in a "page" content, I get the same error as you are describing.

I don't know what API is causing drupal_eval() to being called twice, but here's screenshots of the PHP stack during the two calls. Hopefully it will help someone.

RayZ’s picture

Title: Core bug? » Bug when using PHP functions in content body

Changing title back.

nevets’s picture

This is likely do to the fact that the node gets processed twice, once for the teaser and once for the body. I get around it by setting the teaser to a text message.

karens’s picture

I ran into this and reported it somewhere else, but I've lost track of where. It's because each field is evaluated at least twice, once by the field module and once by the content module. This is because the content module has to have a crack at the field info, too, so I don't think the fact that the field is massaged twice is a bug exactly. And it's possible that it gets massaged more than twice (the widget may have a shot at it too, maybe others).

My immediate workaround was to wrap any functions with if (!function_exists('myfunction'). The only other way to fix it is somehow to figure out exactly which pass through the field should trigger the eval, instead of evaling on every pass, but I'm not sure which would be the right time to do that. It might be easier just to check for function_exists.

joeguru’s picture

This isn't just happening with the CCK, and it's taking recently-upgraded sites pretty much down if they embed php code. I'm following up on the teaser re-eval theory.

dopry’s picture

Status: Active » Closed (won't fix)

I'm setting this to won't fix.... You have to be careful when declaring functions in nodes... I would even go so far as to say you should never do that... You should also be careful that you paste bug free code into nodes.

dman’s picture

this is not a bug, just a result of the PHP language scoping.
PHP would always do that if you included the same library more than once.

It's been a side-effect of all versions of Drupal that allow multiple evaluations of embedded code that are odd enough to try and declare functions inside an eval() context.

try not to do that, or if you must, protect yourself with

if(! function_exists('myfunc')){
  function myfunc(){
    ...
  }
}

But really, seriously, don't try making code that way.