PHP include() halts when the included file has certain size - php

There is a compiler.assign.php plugin in Smarty 2.6.19. Its contents can be looked here: http://smarty-php.googlecode.com/svn/branches/Smarty2Dev/libs/plugins/compiler.assign.php
Sometimes PHP halts on the line where this file is included. No errors thrown. But if I make any change in the file (i.e. add a character in a commented line), it starts working. If I remove added text, it halts again.
Halting can be reproduced only when file contents are original. If I add one character in one place and remove one in another, the script will work, despite its size is the same as original.
Restarting Apache helps. So this is an Apache+PHP problem.
I was adding and removing characters in the comments only, so they couldn't affect PHP. I am confused very much by this weird problem.
The problem occurs under Apache 2.2.24, PHP 5.3.23 and Linux 2.6.18. Didn't check another environments.

Related

How do remove unwanted linefeed charachter from file-download in Laravel Nova?

In a Laravel Nova application i've got a strange issue: Whenever i try to download a file i have a linefeed char on the first line (0A, an empty line). This corrupts images, zip-files and so on. But what is causing this?
I've tried/checked:
Check all php files on closing tags (there are none)
Original file on hdd does not have this extra linefeed when it's created
Checked every php file since last release for correct UTF-8 encoding
Copied composer.lock from live server and installed everything again
Ran composer dump, php artisan cache:clear, php artisan clear-compiled
Checked all code changes since the last release (where there was no issue)
Tried Chrome and Firefox
Here's some Laravel Nova code that produced this error:
return Fields\Image::make(__('Image')), 'imageStorage')
->rules('image', "dimensions:max_width={$image_max_width},max_height={$image_max_height}")
->squared()
->disk(Enums\StorageDisk::APP_PUBLIC_DISTRIBUTORS)
->path(DistributorPath::products($distributorId))
->deletable(false)
->prunable()
->onlyOnForms();
First charachter is always 0A.
So, how do i find the cause? Any tips or strategy?
Short answer: the linebreak was hiding in a routes file.
This mistake was made almost a year ago and never surfaced until now, because it was no issue in any of the output the application created (html / xml). Started to be a problem when I tried to output binary files (zip / images).
So how did i find this bug? die('find bug!');
For anyone in the future:
Start at index.php
Place die('find bug!'); at one place and test.
Don't be afraid to place it in vendor or library code.
No linebreak? Remove die('find bug!'); and place it in the next call.
Found the linebreak? So now you now the linebreak is somewhere inbetween.
Zoom in and find it!

PHP Files Throw Parse Errors (ex: T_STRING) After Migrating

This is a weird one, and something I've never run into before.
I'm deploying to a vvv (Varying Vagrant Vagrants) box from PHPStorm, and the project is a wordpress site.
Frequently, when files are moved over, after reloading the site, I get a PHP parse error, always at the last line of the file. The file isn't necessarily the one I opened and edited, and adding a ?> to the end fixes it. I can then immediately remove the ?> at the end of the file, and all remains well.
This occurs intermittently, making it very difficult to isolate and fix.
An example parse error would be something like:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting end of file in /srv/www/cpr/public_html/wp-content/plugins/jetpack/3rd-party/class.jetpack-amp-support.php on line 359
The specific parse error changes; it's not always the same thing, but it is always fixed by simply adding a ?>, refreshing, etc.
I keep thinking this has to be something to do with either line endings or encoding, but both seem to be ok. (PHPStorm using LF and UTF-8.
The only part of this workflow that is new to me, is the vvv box, as I've previously used other vendors' vagrant boxes, but I'm not sure how that would cause this.
Is something being appended to these files that's breaking when PHP goes to parse them? Is git or vagrant or PHPStorm's deployment overriding line ending rules and changing them?
I'm at a total loss.
Any help is greatly appreciated. As I roll out larger plugin changes, I'm unable to use the WP api, or do anything else without manually opening all the affected files and adding PHP closing tags, then removing them.
I was able to finally track down the cause of this by isolating the various components.
I ruled out phpStorm and git by migrating some files manually and getting the same error to occur above the app's root, on the vagrant box. This isolated the issue to the vvv box itself, and not the project, PHPStorm, or the deploy settings.
I then setup git to force linux line endings via a .gitattributes file in the repo root, with settings based on things I read in this thread.
Here's what I'm using now:
# Force provisioning script to use LF, even on Windows
* text eol=lf
# Avoid binary files to be corrupted by CRLF conversion
*.png binary
*.gif binary
*.jpg binary
*.jpeg binary
I then used dos2unix and find to recursively fix all the line endings in the project, like so:
find . -name '*.php' |xargs dos2unix
So far - all is well, and persisting. I'll update if this pops back up and needs further revision.

PHP-Files magically serverd with a leading TAB

I got a weired behavior on my apache, php setup on debian system.
I tried to create an image with php and output it directly. It failed permanent and the browser said sth. like corrupt image file. After a lot of try and error arroun 3 hours I figured out that there is a TAB character added right at the beginning ob my image content, that does not belong there.
To proove that its part of the apache-php setup I created a test.php file, that just contains the letter 'a' and coppied this file to test.html. So Both files just contain the letter a.
You can see the resulting tab here:
http://www.activeroom.net/test.php
http://www.activeroom.net/test.html
and even on the base url itself - its a php-file, too.
Hope anybody can point me in the correct direction. Btw. on the console everything is fine: php test.php just returns an a. Maybe it is something about the Apache MimeTypes or headers?!
How stupid - there was a php-file prepended through the php.ini. That file had a tab at the end. Sometimes things are really messed up.

New line before header()

I'm working with a custom made Joomla script that generates an image from a blob in a database. However, when we moved it from an Apache server to an IIS server, the script is breaking.
Upon investigation, it seems that now, there is a line break that is getting appended before the header function is being run (when I comment out the image portion, and do an echo "test";, test gets printed on line 2 of the source code.
I've gone through the script over and over again, and through the many files in the component, and can't seem to find out where this new line is coming from. At all.
I've tried using ob_start() and many similar functions, but nothing seems to work.
I'm completely out of ideas, and don't know where to turn.
Is there a way to trace what files are being called in the page, perhaps? Any thoughts on further steps?
Look out for additional whitespace at the end of PHP files. If a PHP file ends in more than a single return after the last ?>, that stuff gets printed regardless of where the file is/was included. That is a common error source in larger PHP projects and would meet your symptoms.
(To circumvent this, some have started with a coding standard that prohibits the last ?> in their source files, which is perfectly legal PHP.)
Be careful, maybe it's an encoding problem. Open the file with vim under linux to see if there an ^M or something like that.

Including different types of files in PHP

I took over a PHP project and I'm trying to get it running on my dev box. I'm a developer and not a sysadmin, so I'm having trouble getting it working.
The previous developer used .h files to include PHP. My current configuration is taking that .h file and including it without executing it. What do I need to look for in my apache config (or is it my php.ini)?
EDIT:
Something clicked when I read one of the comments below. The code is using ASP style tags "<?". I've turned the option on in php.ini and according to phpinfo(), it's enabled, but apache is still just including the code as text.
I just checked it and running the code with the full opening PHP tag "<?php" fixes the problem. Having said that, I'd still like it to work the other way.
I'm running the code on a Macbook with the latest versions available. PHP 5.2.6, postgresql 8.3 and apache 2.
The code works on the staging server, but I can't figure out what the difference it.
EDIT
Durrr...I didn't have short_open_tags enabled in php.ini.
Could the problem be that the .h file you are including just starts into php code without the opening "<?php" tag?
From include documentation:
When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.
PHP is an interpreted language, so you can't 'include' a file without executing it. Remember that '.h' is just a file extension, so although the previous coder may have put PHP in a '.h' file, it's still just PHP.
More details on the problems you're running into would be helpful.
you can use a .htaccess file and add:
php_value auto_prepend_file /path/to/include-file.h
Edit
I've just read that .htaccess only works with the module version of php.
You should change them all to .php extensions. But, if you are going to leave them as .h, you change the mapping in Apache. It's Apache that runs the file through the proper interpreter, not PHP. The PHP engine will process anything passed to it.
include and require will execute the included file. If you are using readfile, it simply echoes the file without treating it as PHP code.
If the .h files are "missing" the <?php directive, then you may have to switch from include to something like:
eval( file_get_contents("file.h") );
Personally I try to avoid eval whenever I can, but sometimes there is just no choice.

Categories