www: Re-work the setting of custom field names

Previously we were passing a fields structure around and setting and
freeing the field names in each function that wanted them.

The passing of the fields structure around wasn't really correct and not
really needed. It should be treated like the env_vars and user_session
structures which are global, set one and free'd once.

So this makes that happen for the fields structure. It gets set once in
url_handlers() and free'd once in the same.

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
www: config: Allow to use '#' for comment lines in the config file

Lines starting with a # will be ignored.

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
www: receiptomatic-www.c: Some minor comment fix ups

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
www: environment: Store the name of the requested host

This is the first part of the multi-tenancy work. We will be using the
url/hostname that the user uses to access the site to tell what
organisation they are with. e.g if a user accessed the site by
otl.ri.opentechlabs.net, then we need to get the left most part of the
host, in this case, otl.

To get this, we need to look in some CGI environment variables. We first
look in HTTP_X_FORWARDED_HOST. This will be the case if the web server
in front of receiptomatic is itself behind another web server. Otherwise
if will be in HTTP_HOST.

Once we have the host we store it in env_vars.host, this will still be
the FQDN, a function will be introduced the get only the left most part.

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
multi-tenancy: config: Add a MULTI_TENANT config option

This is optional. When set to 1, this enables multi-tenancy mode. If
it's set to 0 or not present, multi-tenancy mode is disabled.

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
multi-tenancy: Introduce a get_tenant() function

This function, given a hostname of say, otl.ri.opentechlabs.net will
return otl, the tenant.

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
multi-tenancy: Store the users tenancy in the user session

Using the previously introduced get_tenant() function, store the users
tenancy in the users session as user_session.tenant.

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
multi-tenancy: Make the email receiver multi-tenancy aware

The main difference here is where it will store the images if running in
multi-tenancy mode. Rather than just IMAGE_PATH/UID/ it will now be
IMAGE_PATH/TENANT/UID/.

In order for it to work out who the tenant is, it uses the To: header
from the email and looks at the left most part of the hostname. Thus if
an email was sent to, receipts@otl.ri.opentechlabs.net, this will tell
it that the tenant is otl.

We also make a copy of the db_conn() function from www/db.c here as the
sharing of code was becomming messy and no longer worth it for a few
lines of code.

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
multi-tenancy: Log the tenant in the log files

e.g, multi-tenancy mode

    1351519556.911099 29223 otl handle_request: Got request from
        192.168.1.20 for /receipts/ (GET), 3ms

vs, non multi-tenancy mode

    1351519396.920219 29145  handle_request: Got request from
        192.168.1.20 for /receipts/ (GET), 3ms

In multi-tenancy mode we also set the tenant to "" when logging is
happening due to a signal handler and there is no host.

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
multi-tenancy: db: Select the right database

We use the tenant name to determine which database to use.

Tenant databases are prefixed with rm_, so a tenant called otl will
have a database called rm_otl

In the case of non multi-tenancy mode, the database is as defined by the
DB_NAME config option.

Also seeing as a copy of db_conn() is now in the mail receiver and db.c
isn't being shared, we can get rid of the conditional compilation on
_RECEIPTOMATIC_WWW_ being defined.

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
multi-tenancy: Make delete_image() multi-tenancy aware

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
multi-tenancy: Make image_access_allowed() multi-tenancy aware

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
multi-tenancy: utils: Fix send_activation_mail()

In multi-tenancy mode, the hostname in the link in the account
activation email will be different for each organisation. So instead of
using the value in BASE_URL, we just hardcode the use of https and use
the value in env_vars.host for the hostname to use in the activation
link.

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
multi-tenancy: db: Handle incorrect hostnames / db's

Seeing as the application uses the hostname to determine what database
to use, if someone mistypes their url, the app may then be looking for
a database that doesn't exist.

Catch this and put them to a page that suggests they have mistyped the
url and that they should fix it and try again.

Note: This requires that the receiptomatic database user has the GRANT
privilege, so that it gets the 'unknown database' error message and not
an access denied message. Which is used in the above to send the user to
a helpful url.

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
www: db: Re-work database connection handling

Previously a database connection would be made and destroyed in each
function that it was needed. Some actions would mean multiple of these
events occuring during a client connection.

Instead, open the connection once at client connection time and destroy
it at the end of the connection.

This results in a nice reduction of code and _should_ be slightly
faster with doing just one database connection.

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
config: Remove BASE_URL

This is no longer used by anything.

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
docs: INSTALL: Add text covering multi-tenancy

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>
www: templates: Fix duplicated html tags in receipts.tmpl

If there were no images, then we'd get duplicate </body></html> tags as
they fell outside the </tmpl_if> for the no receipts case.

Signed-off-by: Andrew Clayton <andrew@opentechlabs.co.uk>