Replace $_SERVER['SERVER_NAME'] with $GLOBALS['base_url'].

Fixes issue where $_SERVER['SERVER_NAME'] returns a value that doesn't match $GLOBALS['base_url'], thus generating invalid public-facing URLs.

$GLOBALS['base_url'] is derived from $_SERVER['HTTP_HOST'], which is the contents of the Host: HTTP header from the current page request. $_SERVER['SERVER_NAME'] is the name of the server host under which the current script is executing. For VirtualHost environments, $_SERVER['HTTP_HOST'] is going to be a more reliable source to generate public-facing URLs.
Simplify $loader_url via url() function.

My previous commit didn't actually work because $GLOBALS['base_url'] includes the protocol, which we are also prefixing here.

However, if we reduce this whole block of code down, we're essentially reproducing the output of url(), a core function.
Using $GLOBALS['base_url'] instead of $_SERVER['SERVER_NAME'].

Two additional uses of $_SERVER['SERVER_NAME'] that might break when SERVER_NAME != HTTP_HOST. Better to rely on the Drupal $base_url global.
Use $_SERVER['SERVER_NAME'] when checking whether we're running from within Drush.

Drush sets $base_url to `http://default` if it can't determine the URL. However, PHP does not populate the $_SERVER['SERVER_NAME'] value in this case, so it's a reliable way to determine whether $base_url is going to be valid.
Merging pull request #547.