+/**
+ * Get an absolute URL from a complete one, and another absolute/relative URL.
+ *
+ * @param string $originalUrl The original complete URL.
+ * @param string $newUrl The new one, absolute or relative.
+ *
+ * @return string Final URL:
+ * - $newUrl if it was already an absolute URL.
+ * - if it was relative, absolute URL from $originalUrl path.
+ */
+function getAbsoluteUrl($originalUrl, $newUrl)
+{
+ $newScheme = parse_url($newUrl, PHP_URL_SCHEME);
+ // Already an absolute URL.
+ if (!empty($newScheme)) {
+ return $newUrl;
+ }
+
+ $parts = parse_url($originalUrl);
+ $final = $parts['scheme'] .'://'. $parts['host'];
+ $final .= (!empty($parts['port'])) ? $parts['port'] : '';
+ $final .= '/';
+ if ($newUrl[0] != '/') {
+ $final .= substr(ltrim($parts['path'], '/'), 0, strrpos($parts['path'], '/'));
+ }
+ $final .= ltrim($newUrl, '/');
+ return $final;
+}
+