summaryrefslogtreecommitdiff
path: root/modules/system/system.api.php
diff options
context:
space:
mode:
Diffstat (limited to 'modules/system/system.api.php')
-rw-r--r--modules/system/system.api.php61
1 files changed, 61 insertions, 0 deletions
diff --git a/modules/system/system.api.php b/modules/system/system.api.php
index ea8bcc115..5f50c30d2 100644
--- a/modules/system/system.api.php
+++ b/modules/system/system.api.php
@@ -1449,6 +1449,67 @@ function hook_file_download($filepath) {
}
/**
+ * Alter the URL to a file.
+ *
+ * This hook is called from file_create_url(), and is called fairly
+ * frequently (10+ times per page), depending on how many files there are in a
+ * given page.
+ * If CSS and JS aggregation are disabled, this can become very frequently
+ * (50+ times per page) so performance is critical.
+ *
+ * This function should alter the URI, if it wants to rewrite the file URL.
+ * If it does so, no other hook_file_url_alter() implementation will be
+ * allowed to further alter the path.
+ *
+ * @param $uri
+ * The URI to a file for which we need an external URL, or the path to a
+ * shipped file.
+ */
+function hook_file_url_alter(&$uri) {
+ global $user;
+
+ // User 1 will always see the local file in this example.
+ if ($user->uid == 1) {
+ return;
+ }
+
+ $cdn1 = 'http://cdn1.example.com';
+ $cdn2 = 'http://cdn2.example.com';
+ $cdn_extensions = array('css', 'js', 'gif', 'jpg', 'jpeg', 'png');
+
+ // Most CDNs don't support private file transfers without a lot of hassle,
+ // so don't support this in the common case.
+ $schemes = array('public');
+
+ $scheme = file_uri_scheme($uri);
+
+ // Only serve shipped files and public created files from the CDN.
+ if (!$scheme || in_array($scheme, $schemes)) {
+ // Shipped files.
+ if (!$scheme) {
+ $path = $uri;
+ }
+ // Public created files.
+ else {
+ $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
+ $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
+ }
+
+ // Clean up Windows paths.
+ $path = str_replace('\\', '/', $path);
+
+ // Serve files with one of the CDN extensions from CDN 1, all others from
+ // CDN 2.
+ $pathinfo = pathinfo($path);
+ if (array_key_exists('extension', $pathinfo) && in_array($pathinfo['extension'], $cdn_extensions)) {
+ $uri = $cdn1 . '/' . $path;
+ }
+ else {
+ $uri = $cdn2 . '/' . $path;
+ }
+ }
+}
+ /**
* Check installation requirements and do status reporting.
*
* This hook has two closely related uses, determined by the $phase argument: