CRM-13750

Forward-port from 4.2. Please do a quick test before accepting the PR since I do not have a working 4.4 install.
Please also merge in 4.4 (the PR is issued to your master branch by default, so not know how to change that)