Wednesday, April 30, 2008

PHP Error: function return in write context

I was presented with a strange error in PHP today,

Fatal error: Can't use function return value in write context in /var/www/script.php on line 31


This is the code I was using on line 31.

$email = empty(ini_get('sendmail_from')) ? $default : ini_get('sendmail_from');


The problem was with the call to ini_get being within a call to empty.

To get rid of the error, I needed to check for empty values returned from ini_get differently.

$email = ini_get('sendmail_from') == '' ? $default : ini_get('sendmail_from');

Sunday, April 13, 2008

gedit External Tools indent-- & indent++

UPDATE: The same thing can be accomplished by simply pressing the tab key when you have a block of code selected, hold shift to decrease indent instead of increase. Thanks Ben !

program conditional block example There's times when I have to remove an outter conditional block from a script & in order to keep everything orderly this means I also need to decrease the indent in the code within that conditional block by one.

To the right is an example of this situation. I want to remove the else outter conditional since the inner code needs to be evaluated whether the preceeding code succeeds or not.

Normally in something like Notepad this would mean alot of fancy finger work on the down arrow and the delete key on my keyboard.
In Notepad2 I was able to utilize the find & replace support for Regular Expressions and remove the tabs from the beginning of each line in the selection.

Now that I have gedit as my primary editor since I'm on Ubuntu I don't have acces to Notepad2, however I do have access to the External Tools plugin for gedit.

Through the external tools plugin I can filter selected text through sed, which is basically a RegEx filter designed to be used with pipes.

So I setup two new commands through that plugin, one for decreasing the indent by one which I call indent-- and another which I use to do the exact oppisite which I call indent++

If you're new to gedit like I am, first you'll need to enable the Enternal Tools plugin.
You do this by going through
Edit -> Preferences -> Plugins Tab -> Check "External Tools"


This will enable the following menu option where you can manage commands to filter selected text and documents through.
Tools -> External Tools


The dialog for managing tools is pretty self explainitory, here's what I entered for the command to be used for my indent-- tool.
#!/bin/sh

sed 's/^\t//g'



Here's the command I'm using for my indent++ tool.
#!/bin/sh

sed 's/^/\t/g'


I actually got the idea after seeing the existing command to remove whitespace from the end of lines.

Sunday, April 6, 2008

UID of script is smaller than min_uid

I just spent almost two hours trying to figure out what this error meant for suPHP.
I couldn't figure out whether it applied to the UID of the file itself, or the user the process was running as when it started just going by what I found through searching.

So I ended up installing suPHP on my Ubuntu box just to find out for myself.
I went ahead and installed the Apache2 module, to make things easy.
sudo apt-get install libapache2-mod-suphp


Once that was installed it restarted Apache by itself & the first PHP script I accessed in my browser worked like a charm. Reason being, the PHP script was already owned by the user I usually use to test stuff out with, a non-root user with a UID of 100.

Now when I chowned the file to be owned by root,
sudo chown root /var/www/test.php


I got the following error showing up in /var/log/suphp/suphp.log.
[Sun Apr 06 07:45:22 2008] [warn] UID of script "/var/www/test.php" is smaller than min_uid


As soon as I gave the file back to myself it worked fine however.
sudo chown joebert /var/www/test.php



Update: Now that I read this post a few months later, I'm not sure what the actual problem was is clear enough. It's not soo much who owns the file as much as it is what their system UID is.

When a user account is created on the system it's generally given an ID number. Usually when you create a user account which will actually get logged into by a human, such as "bob" or "arlene", rather than a system/service account such as "www-data" or "postfix", the UID will tend to be 100+. However with a system/service account the UID will tend to be below 100.

So, if you chown the problematic file to "root", "www-data", or similar, you're likely going to get the min_uid error since those accounts tend to be system/service accounts.

The point of suPHP is to make sure scripts are running as the person who owns them. If you need to assign ownership of scripts to services, you'll have to modify the suPHP configuration file so that min_uid is as low as the UID of the account you wish to assign ownership of the file to.