I use the php code for pages to include content from php. Usually I do it using the following syntax:
require_once('mypage.php');
This worked in 7.0. I upgraded to 7.4. When I use the page editor, it now tries to escape the quotes, which puts them in the drupal database with escape characters. This causes an error when I attempt to view the page saying unknown character. The code above is then converted the following whenever I try to edit the page:
require_once(\'mypage.php'\);
(note when viewing this in non-edit mode, this doesn't show the problem, there should be a backslash before the two single-quote characters.)
It looks like it is being escaped twice when putting it into the database, then unescaping it only once when coming out. I am able to work around this issue by clearing the cache in the database and manually editing the fields for the node to not be escaped improperly.
Comments
Comment #1
marcingy commentedNeed more information - eg an example etc.
Comment #2
tomcollins commentedThe example is provided above.
Create a "Basic Page". Set it to be php for the input. Create mypage.php in the same directory as your drupal install. Type the above code in:
Try to view the page. It will give the error saying unknown character referring to the ASCII character \.
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /modules/php/php.module(74) : eval()'d code on line 1
Warning: Unexpected character in input: ''' (ASCII=39) state=1 in /modules/php/php.module(74) : eval()'d code on line 1
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /modules/php/php.module(74) : eval()'d code on line 1
Warning: Unexpected character in input: ''' (ASCII=39) state=1 in /modules/php/php.module(74) : eval()'d code on line 1
Fatal error: require_once() [function.require]: Failed opening required 'php' (include_path='".:/usr/lib/php:/usr/local/lib/php') in /modules/php/php.module(74) : eval()'d code on line 1
If there is something more specific you need, please specify what would be more helpful.
Comment #3
tomcollins commentedComment #4
marcingy commentedHave tired this locally and can't recreate so it isn't a general issue with drupal and is a problem with your set up. Moving to support request.
Comment #5
tomcollins commentedI just installed clean yesterday. Installed locally, installed on my hosting. Same problem. Must be a problem with your setup. I was using 7.4, not sure how the version changed to 8.0. This problem did not exist in 7.0. The only thing I changed was the version of Drupal. Please try using 7.4.
Comment #6
tomcollins commentedFixing category to bug report rather than support request since previous user was using the wrong version of Drupal.
Comment #7
marcingy commentedBugs are fixed in head and at the moment d8 and d7.4 are exactly the same. Please provide more information about the environments you are using because I can not recreate at all. The fact my environment works == there is no problem with my environment as it is working correctly. A wider issue is why are you approaching the addition of content in this way rather than utilising the drupal hook system.
Comment #8
marcingy commentedThis is a support request no bug has been has actually be able to be recreated as of yet.
Comment #9
tomcollins commentedWhat information do you want? I am using a shared host that I installed Drupal on. What else might help you? Should I post the installation (which is identical to what I downloaded from this site)? Is there anyone else who can try to reproduce this?
I have changed nothing from just copying files. I can look inside the database tables, and the code has been escaped. I go to edit the page, and it shows up escaped there. This didn't use to happen, I can go to my installation of 7.0, and this problem does not exist. There cannot be that many places where things are being escaped and stored in the database for creating a node.
Why am I doing it this way? Because I can write PHP code, Drupal is documented extremely poorly for new users, and it works. Why should I need to learn the hook system when all I want to do is display dynamic content? I like the menus and navigation tools, and can add non-dynamic code as well with revision history, comments, etc... If you can provide documentation on how I might accomplish this with the hook system, I'm all ears. I do not want to rewrite all of my code for something that has been working properly for quite some time, though. Are you suggesting I create my own module to do this? I'd rather focus at the issue at hand, which seems to me to be a perfectly valid use. I cannot even use:
echo "hello world";without getting the same error.
Comment #10
tomcollins commentedComment #11
marcingy commentedLook taking this attitude is not going help. Please provide information about your environment and the settings in php.ini. There is simply no way to help you without more information. This is not normal behaviour for drupal as drupal does not escape when saving to the database, all filtering is done on output.
Comment #12
pingers commentedAlso tested using Drupal 7 HEAD. Could not reproduce.
Perhaps you're using a wysiwyg? If so, please try switching to plain text mode.
Comment #13
tomcollins commentedDrupal not escaping when writing strings to the database would be a serious security problem making it vulnerable to SQL injection attacks. So I highly doubt that the actual queries to the database are unescaped.
If there is specific information that can help, please let me know what would be helpful. If you cannot help, please let someone else who is willing to do so help.
Comment #14
tomcollins commentedThis worked in 7.0. I don't know if what you have is identical to what I was using, I was using 7.4 release. If that's the same as Drupal 7 HEAD, then it certainly is strange. I can try with a different version if you would like to make sure I am using the same version as you. Please direct me on how to get the exact same version you are using so I can test it. I want to make sure we are using the same code. If the problem goes away, we can at least trace it to the version I was using.
I'm not sure how you could use a WYSIWYG for editing pages. I select "Add Content" or "Edit Content" on a node and use the default editor:
!!myroot!!/#overlay=node/!!mynode!!/edit%3Fdestination%3Dadmin/content
Thank you for looking into this.
Comment #15
marcingy commentedDrupal core has a test for php eval code PHPFilterTestCase which does a simple print statement which you report doesn't work in #9, this test does not fail on the drupal test bots. Therefore there is something wrong with your environment, it maybe that you have some how corrupted your database. Please provide information with regards your php and mysql environments. This issue is simply going round in circles because no one else can reproduce and you can not provide clear steps on how to reproduce it on any environment but the ones you are using.
Comment #16
pingers commentedIf you haven't disabled all non-core modules and retested, I suggest doing this too.
Comment #17
pingers commentedAlso tested fresh 7.4 with Standard profile + php input format. Apache2 + PHP 5.3.5 + MySQL 5.1.54 (Ubuntu 11.04).
Still cannot reproduce :(
Comment #18
tomcollins commentedIs there a chance you are doing something different than what I am? If I am not using the proper terminology, there's a chance we are doing something different. If I am not being clear on what steps I am doing to create the problem, let me know and my apologizes for not using the proper terminology. Your concern about a WYSIWYG editor made me think we aren't doing the same thing.
Kernel 2.6.18-238.5.1.el5
Apache version 2.0.63
PHP version 5.2.14
MySQL version 5.0.92-community
I didn't have any modules added when I first notice the problem, although there are standard modules enabled that are not core, I can try removing those (I need to obviously add the php module to get it to work).
The system has not changed between 7.0 and 7.4, same versions of everything. I have an install of 7.0 still there and it is working properly on that same system. However, we have some differences on our system, although not sure what would cause the different behavior.
I'll try another clean install with a brand new database and see if that makes any difference.
I definitely can trace the problem to the form for editing/creating nodes (I did Basic Page for type) -> Database. If I fix the database to what it used to be (no escapes), everything works. If you know where that code is, I'll even look at it and try to debug it, but I am not familiar enough with the code to know where to start. There are some API functions to interact with the database that passes parameters, perhaps the problem might be with the database code.
I can try again tonight on the same server with the clean install and another box and see if it happens. If it happens on both machines with much different environments, I'll be a bit concerned. I can attach a debugger on the form editing code and try to step through it to see anything fishy on that machine.
Comment #19
pingers commentedFirst step when testing a core bug is always disable contributed & custom modules if they're not essential. Please try this first.
I've looked in the database and nothing is escaped for me... it looks exactly as I entered it.
Steps I followed:
Install Drupal 7.4 on empty database using standard install profile.
Enable php module.
node/add/page - select php input format,
<?php print 'hello world'; ?>, save.I also resaved the node.
I urge you to try these steps as they will confirm whether or not this is an installation specific issue.
Comment #20
tomcollins commentedWhat are you defining as core? I added no custom modules, I did not enable or disable anything other than the php module. I can start disabling other parts too to see if it goes away.
I've seen the same problem on two installs which was odd. I will also try re-downloading and installing clean to see what's going on, clean database, etc...
Hopefully it magically goes away.
Comment #21
pingers commentedCore is everything included within the drupal-7.4.tar.gz.
Contributed modules are modules like http://drupal.org/project/views.
Can you please list your php version, mysql version and operating system?
If running linux, commands to find these things are:
Comment #22
tomcollins commentedKernel 2.6.18-238.5.1.el5
Apache version 2.0.63
PHP version 5.2.14
MySQL version 5.0.92-community
I should have time to test some things out in a few hours, gonna start with another clean install/new database, and then with a new machine with a different configuration to see if that makes any difference.
Comment #23
pingers commentedI tried a 7.0 upgrade to 7.4, just to see if not running database updates would cause this. i think you'll have no problems with a clean install.
Best of luck.
Comment #24
tomcollins commentedResults are somewhat interesting:
Clean machine on windows install: works perfectly
Reinstall clean with a new directory, new database on the system described in post 22: same problem as described.
This at least explains why other people are having a hard time reproducing this.
One major difference is I have php 5.3 on the windows machine. I also have a different version of mysql ( 5.5.12 on Windows). I believe everyone who has tried to reproduce has done so with php 5.2.
Since this used to work on this system, I'm starting to think that there's some incompatibility with Drupal 7.4 and either php 5.2 or mysql 5.0. More likely php 5.2, but that's a guess.
The bad news is that I can only reproduce this on the shared host, so I don't have as much access to debug or screw around with the system too much. I'd like to just start digging in the source to figure out what could be wrong, I could diff key files to see if there's anything out of the ordinary. I don't think there is anything inherently wrong with the configuration since it used to work in 7.0. I'd be able to test any other versions of drupal as well, perhaps that could help narrowing things down.
Any advice would be appreciated on narrowing this down. I still think there's likely a bug in Drupal somewhere, but until we find the offending code, it's going to be hard to confirm. If someone knows the code base well enough to tell me where to narrow things down, I'll try digging it up myself, although not being super familiar with all the Drupal API being called, it might be tougher for me to debug. Thanks for your patience pingers.
Comment #25
pingers commentedIf you look at php module, there's only a handful of functions.
php_eval() - evaluates code.
php_filter_info() - is the filter callback to filter the field on output. Output is the key thing here.
If you're seeing slashes in the database, then something very weird indeed is going on... which has nothing to do with the input format... because input formats actually filter the output.
Comment #26
tomcollins commentedI'm more concerned about what is sending the info to the database, I'm not sure that module does it.
That gave me an idea, I'm trying it without the php module, just filtered HTML, same thing happens, it has the quotes on it. \"hello world\" came from "hello world" (hoping this doesn't screw up when I submit the reply).
So it can't be specific to the php module, but whenever I press "Save", whatever is going out to the database. Do you know what that code is?
Comment #27
marcingy commentedWhen you click submit it calls node_save, which allows modules to response to the node_save(http://api.drupal.org/api/drupal/modules--node--node.module/function/nod...) event and then drupal_write_record does the actual call to populate the database (http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_wr...) where basically does a db_insert (http://api.drupal.org/api/drupal/includes--database--database.inc/functi...).
Comment #28
pingers commentedI would check the contents of the node body just before it's written to the database. I.e. as Marc said in node_save() (modules/node/node.module). If there's no slashes there, you're probably looking at a database configuration issue.
Comment #29
tomcollins commentedThe weird part of it is that the title of the node comes out unscathed, but just the body is screwed up.
I am not aware how I can debug on my host's server, although I may try modifying the drupal code and have it try to write to a log. It won't be an easy process. I'm hoping looking at how it works on my other system will give me some insight on what to look for.
Comment #30
pingers commentedYou could do something like the following...
watchdog('debug', print_r($node, 1));which would log the node object to the log @ admin/reports/dblogStick this in node_save(), resave the node and check the log.
This isn't particularly secure, so please don't leave this debug code in there.
See http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/watchd... for more info on watchdog().
Comment #31
tomcollins commentedI was able to add the watchdog to both my working system and non working system. There were some key differences:
Working: [title] => "hello world"
Not Working: [title] => \"hello world\"
So the problem definitely is happening before it even tries to save it in the database and on the creation of the node object. Where is this done? For some reason my debugger isn't stopping at my breakpoints for me to examine the call stack.
I was wrong before that the title came out ok, the title does get screwed up.
I'm still tracing things, but my best guess now is it's coming from the form -> node conversion code. I'm trying to follow the code, trying to find a good candidate to put another watchdog in on the server to trace the problem. So making progress, hopefully I can narrow this down.
Comment #32
marcingy commentedThere might be an issue with using watchdog as it writes to db - a drupal_set_message might actually be a better option as that removes the involvement of the database.
so drupal_set_message(print_r($node, 1)) this will then display in the alter area on the page.
Comment #33
tomcollins commentedNarrowed it down more, inside of node.pages.inc in the node_form_submit_build_node function, I had a watchdog on form_state, and saw that it's escaped even that early. Looking more and more like form code. More debugging later.
Comment #34
tomcollins commentedGood point. I used your method, and it still comes out as escaped, so that means it's not related to the database. It's happening as early as processing the form values.
Thanks to everyone for their help so far, hopefully I can narrow it down. If anyone has a PHP 5.2 system set up, it would be really nice to test this case there.
Comment #35
marcingy commentedI'm wondering if some how magic quotes are enabled, can you do a phpinfo() and see if that is the case, because that could explain what you are seeing. They should be being disabled in the .htaccess. I don't have a php 5.2.x install to hand to test.
Comment #36
tomcollins commentedI narrowed it down as much as possible for now. I put an error message on $_POST["title"], and it shows up escaped.
So there are a few possibilities, but I don't understand the code well enough to know where to look.
What happens on form submission? Is it some javascript that tries to validate things or runs before submitting? Does it run through some other layers before the code I was looking at _POST to pre-process it?
It's quite odd that it doesn't work on the one machine but does on the other.
Comment #37
marcingy commentedDrupal does not do any client side validation. The $_POST data is not manipulated by drupal directly - drupal_build_form grabs the data out of $_POST so this seems to indicate a PHP issue which was why I was thinking about magic quotes.
Comment #38
tomcollins commentedI think you likely are finding the problem. I'll give it a shot. Since magic_quotes is deprecated in php 5.3, that could explain why it works for so many people. PHP has a default .htaccess file that got installed, I wonder if either has it disabled, or had it disabled in previous versions and no longer does (which wouldn't affect 5.3 systems, but would affect 5.2).
I'm going to take a look at let you know, but that very well could be it. If that's it, the most likely culprit would be a change between the .htaccess file in 7.0 and 7.4. But better to test and be sure before jumping to conclusions.
Configure Command: './configure' '--enable-bcmath' '--enable-calendar' '--enable-exif' '--enable-ftp' '--enable-gd-native-ttf' '--enable-libxml' '--enable-magic-quotes' '--enable-mbstring' '--enable-pdo=shared' '--enable-soap' '--enable-sockets' '--enable-wddx' '--prefix=/usr' '--with-curl=/opt/curlssl/' '--with-curlwrappers' '--with-freetype-dir=/usr' '--with-gd' '--with-gettext' '--with-imap=/opt/php_with_imap_client/' '--with-imap-ssl=/usr' '--with-jpeg-dir=/usr' '--with-kerberos' '--with-libdir=lib64' '--with-libexpat-dir=/usr' '--with-libxml-dir=/opt/xml2' '--with-libxml-dir=/opt/xml2/' '--with-mcrypt=/opt/libmcrypt/' '--with-mhash=/opt/mhash/' '--with-mysql=/usr' '--with-mysql-sock=/var/lib/mysql/mysql.sock' '--with-mysqli=/usr/bin/mysql_config' '--with-openssl=/usr' '--with-openssl-dir=/usr' '--with-pcre-regex=/opt/pcre' '--with-pdo-mysql=shared' '--with-pdo-sqlite=shared' '--with-pic' '--with-png-dir=/usr' '--with-pspell' '--with-sqlite=shared' '--with-ttf' '--with-xmlrpc' '--with-xpm-dir=/usr' '--with-xsl=/opt/xslt/' '--with-zlib' '--with-zlib-dir=/usr'
I noticed that --enable-magic-quotes is present.
magic_quotes_gpc Off On
magic_quotes_runtime Off Off
magic_quotes_sybase Off Off
Time to change the .htaccess file to see if that was it.
Comment #39
tomcollins commentedmarcingy-
I think you found the source of the problem. I checked phpinfo on the working system, magic_quotes is set to "on". The .htaccess file is considerably different. I downloaded the 7.0 revision and it looked similar to the 7.4 .htaccess file. I did install from the fantasticodeluxe system, so it may have had some customizations to the .htaccess that was initially installed.
The weird thing is, it appears all is well if magic_quotes is enabled, but screws up if magic_quotes is disabled. The other weird thing is, it appears to have the magic_quotes behavior only when it is disabled, and not when it is enabled.
Perhaps I am understanding this wrong. Any thoughts?
Comment #40
damien tournoud commentedWe cannot support hosting environments that have hacked PHP or offer an inconsistent PHP environments.
The problem you have here is that PHP is reporting to Drupal that magic quotes are Off, while they are actually On. This is a classic bug of htscanner.
The "fix" in your case would be to remove all the
php_flag magic_quotes_gpc offfrom your.htaccess. Drupal will fix the situation if it properly knows that magic quotes are actually on.Comment #41
damien tournoud commentedI doubt any of this changed between 7.0 and 7.4. The most probable explanation that the .htaccess was actually missing for you before.
Comment #42
tomcollins commentedThat solved the problem. I had it under a subdirectory that also had the "correct" 7.4 .htaccess file, so it was passing results down the line. Fixing it in both places makes drupal work.
Thanks for all your help. I don't know why the default .htaccess file coming from my host is so much different.
Is there some other component that my host needs to update to not have this problem?
Comment #43
GlitchFreak commentedJust a word of thanks for this thread. Turns out some hosts have magic-quotes on but a php_info() doesn't register it.
This issue of escaping characters has been plaguing me for months now and finally I have fixed it.
Thanks to all who were involved.
Comment #44
lesperras commentedYou guys are drupal geniuses! my site ws working great and then today... Bang! I got just this exact error! I was lost fora while till I stumbled upon this thread. (Thanks to google too). Problem solved.
I would have NEVER found this problem on my own. I really want to say my great big THANKS!!
Comment #45
skandalfish commentedI must say, I greatly appreciate everybody's time and imput on this. Couldn't this also be fixed by simple setting Magic Quotes "On" in the respective .htaccess?
Comment #46
GlitchFreak commentedMy .htaccess had magic quotes set to off but my host overwrote the setting somehow. The fact that magic quotes was on was not even shown in phpinfo(). Very odd behaviour.
Comment #46.0
GlitchFreak commentedThis was revised since the problem was being masked in the display.