Files Ending in .php.html Unexpectedly Being Processed by the PHP Engine

Posted by Chief on Jul 29, 2010 in Gotchas
No Comments

Apache/2.2.3 (Red Hat), PHP 5.1.6 and phpDocumentor v1.4.3 weren’t getting along. phpDocumentor was generating HTML with filenames of the form script.php.html. Apache’s mod_php wanted to interpret script.php.html as a PHP script and was throwing errors about improper formatting:

PHP Parse error: syntax error, unexpected T_STRING in /var/www/default/docs/out/lib/lib/_php---obj---FFT.php.html on line 1, referer:...

I wanted script.php.html to be treated as an HTML file and not be processed by the php engine.  This double-extension was causing me grief.  The docs claimed that only files ending in the extension will be handled by the AddHandler command.  I was incredulous of this claim, especially in light of Apache’s ability to do content negotiation with those .var language files.

Q: Why was this happening? I first guessed it may have been due to mod_mime, but quickly ruled that out.  I then guessed it was the MultiView option… Maybe, this is one mechanism that governs content negotiation… The documentation claims:

# The index.html.var file (a type-map) is used to deliver content-
# negotiated documents.  The MultiViews Option can be used for the
# same purpose, but it is much slower.

This statement lead me to suspect that my problem may actually be caused by an issue with the AddHandler option, since an AddHandler command must be executed to associate type-map with that .var extension.

Ah me, time is money, and in the end, I actually didn’t diagnose the problem, but I did discover (by trial and error) a workaround.

In file /etc/httpd/conf.d/php.conf, I surrounded

AddHandler php5-script .php
AddType text/html .php

with

<Files ~ "\.php$">
  AddHandler php5-script .php
  AddType text/html .php
</Files>

Now, only files explicitly ending in “.php” will be treated as a php5-script.  Take that!

Tags: , , , , , ,

PHP 5.1+ Date vs Strftime Timezone Annoyance

Posted by Chief on Feb 21, 2010 in Gotchas
No Comments

In the old PHP4 days, you could change the default timezone by: putenv("TZ=America/Los_Angeles");. In php5, you can still do this and it’ll work for strtotime, strftime, gmstftime (maybe more). It will not work for methods date and gmdate. For those, starting in version 5.1, you’ll need to call date_default_timezone_set("America/Los_Angeles");. This is just another one of those annoying things that make PHP a dying language.

Together, to be cross-version compliant:

putenv("TZ=America/Los_Angeles");
if( function_exists("date_default_timezone_set") ) { 
 date_default_timezone_set("America/Los_Angeles");
}

We need both to be reverse-compatible with PHP4.

Tags: , , , ,

The little known PHP htmlspecialchars

Posted by Chief on Feb 20, 2010 in Reference
No Comments

Want to replace only xml entities <, >, &? Don’t use htmlentites or str_replace; use htmlspecialchars.

The only named entities for XML are &amp;, &gt; and &lt;. For all others you need to use the Unicode character code (eg. &#160;). Webkit: Entity ‘nbsp’ not defined – Convert HTML entities to XML

Tags: , , , ,

PHP 5.3.1 Error Logging and Display of Said Errors

Posted by Chief on Feb 18, 2010 in Reference
No Comments

I just installed the PHP 5.3.1 Windows installer bundle [VC9 x86 Non Thread Safe (2009-Nov-19 09:53:39)] on my Windows 7 [Ultimate] machine. I want the PHP error output to get sent to stderr when run from the command line. I don’t want the errors logged to a file because I’m developing cli tools. Since we’re dealing with the cli, I also don’t want html errors. Should be pretty easy… if you know how to edit the settings.

These worked for me:

error_reporting = E_ALL
display_errors = stderr
display_startup_errors = On
log_errors = Off
html_errors = Off
;error_log = php-errors.log

Ensure that error_log is not set, otherwise error will not be displayed. This problem may be related, but is obviously not, the date bug mentioned in the PHP bug reports. It may be fixed in the latest snapshot. As a note, the php.ini date.timezone setting is date.timezone = "Pacific/Honolulu", and I still had this problem.

Other things I noticed:
When error_log = syslog, you can find the PHP events in the windows Event Viewer -> Windows Logs -> Application. I mention it in case you want that type of thing… or think error that logging may not be working.

Tags: , , , , , ,

PHP/GD Bug: imageFilledEllipse Centered on Y=0

Posted by Chief on Nov 23, 2009 in Scripts, Tricks and Hacks
No Comments

I was drawing circles using PHP’s GD function imageFilledEllipse. I noticed that when I went to draw a circle having its center at $y=0,there would be a line missing at $y=1.

Here’s the work-around:

if( -1 <= $py && $py <= 1 ) {
  // Undocumented PHP bug when Y coordinate is zero.
  imageFilledEllipse($img,$px,$py,$X,$X,$color);
  imageFilledRectangle($img,$px-$halfX,$py-1,$px+$halfX,$py+1,$color);
} else {
  imageFilledEllipse($img,$px,$py,$X,$X,$color);
}

PHP Version:

PHP 4.3.9 (cgi) (built: Apr  1 2009 10:41:42)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

$ yum search php-gd

php-gd.i386                              4.3.9-3.26             installed
Matched from:
php-gd
The php-gd package contains a dynamic shared object that will add
support for using the gd graphics library to PHP.

Tags: , , , , ,

Using Log Math to Solve Iterative Problems

Posted by Chief on Nov 20, 2009 in Reference
No Comments

Premise: You have a map of infinite resolution, but finite viewing space. You have points on that map that have a fixed foot print.

Question: How much do you need to zoom before the points are clearly distinguishable?

Let us arbitrarily define the criteria for distinguishability as an icon of 25 pixels in size. This icon, it can be said, has a radius of 13 pixels. In reality, icon size is a function of your viewport’s size. I leave that up to you to calculate.

Let us define the scale of the map as the absolute width of the map (in map units) divided by the width of the view port. As an example, let’s use the Earth’s longitude range: -180 to +180 degrees, but in a global Mercator projection. The coordinate range of longitude for this map is [-20037508,+20037508) meters. Our viewport has a range of [0,255] pixels (viewport units). The scale of this map in this viewport is (2*20037508)/256. Be careful not to double-count +20037508, as -20037508 = +20037508 and would yield an improper scale. The viewport size is an integer and does not wrap; there are no fractions of a pixel in a viewport.

We shall now define a target scale. The target scale is the desired scale of the map that allows our icons to become viewable. Our example will use buildings having a foot print of 300 meters (map units) in width and depth. Our target scale, therefore, is 300/25 = 12 meters per pixel.

Question: How much do we need to zoom in before our buildings are distinguishable?

Implementation 1 (naive, iterative):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function zoomLevel( $maprange, $viewport, $distFactor=0.10, $zoomFactor=2 ) {
  // $viewport = 256;
  // $maprange = 2*M_PI*6378137;
  $scale = $maprange / $viewport;
 
  // 10% of viewport required to distinguish an icon.
  $target = $distFactor * $viewport;
 
  $zoom = 0;
  while ( $scale > $target ) {
    $zoom += 1;
    $scale /= $zoomFactor;
  }
  return $zoom;
}

Implementations 2, 3 and 4:

1
2
3
4
5
6
7
8
9
10
11
12
13
function zoomLevel( $maprange, $viewport, $distFactor=0.10, $zoomFactor=2 ) {
  $scale = $maprange / $viewport;
  $target = $distFactor * $viewport;
 
  // naive log
  return ceil( (log($scale)/log($zoomFactor) - log($target)/log($zoomFactor)) );
 
  // commutative log
  return ceil( (log($scale) - log($target)) / log($zoomFactor) );
 
  // subtraction property of logs (best implementation)
  return ceil( log($scale/$target)/log($zoomFactor) );
}

Tags: , , , , , , , , ,

Copyright © 2010 cat brain.log | less All rights reserved.
Shades v1.2 theme from BuyNowShop.com.