/[drupal]/contributions/modules/cdn/patches/d5_file_url_rewrite.patch
ViewVC logotype

Contents of /contributions/modules/cdn/patches/d5_file_url_rewrite.patch

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Thu May 21 16:22:26 2009 UTC (6 months ago) by wimleers
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +0 -0 lines
File MIME type: text/x-patch
FILE REMOVED
Cleaned up HEAD for the upcoming, completely rewritten version of the CDN integration module. This rewrite was part of my bachelor thesis.
1 Index: includes/common.inc
2 ===================================================================
3 RCS file: /cvs/drupal/drupal/includes/common.inc,v
4 retrieving revision 1.611.2.15
5 diff -u -F^f -r1.611.2.15 common.inc
6 --- includes/common.inc 22 Jan 2008 09:36:49 -0000 1.611.2.15
7 +++ includes/common.inc 26 Jan 2008 22:08:15 -0000
8 @@ -1379,6 +1379,56 @@ function base_path() {
9 }
10
11 /**
12 + * Returns a rewritten file URL. Allows you to serve files from different
13 + * servers than the server on which Drupal is hosted.
14 + *
15 + * @param $file_path
16 + * Path to a file, relative to the root directory. E.g.: "misc/jquery.js".
17 + * @param $absolute_url
18 + * Whether to generate an absolute URL or not.
19 + * @return
20 + * A valid file URL.
21 + */
22 +function file_url($file_path, $absolute_url = FALSE) {
23 + static $modules;
24 + $file_url = FALSE;
25 +
26 + if (!isset($modules)) {
27 + $modules = array();
28 + foreach (module_implements('file_server') as $module) {
29 + $modules[] = array(
30 + 'module' => $module,
31 + 'weight' => variable_get("file_server_{$module}_weight", 0),
32 + );
33 + }
34 +
35 + // Sort file server types by weight.
36 + uasort($modules, create_function('$a, $b', 'return $a["weight"] > $b["weight"];'));
37 + }
38 +
39 + // Try the first preferred file server to check if it can serve the file.
40 + // Then try the second, and so on.
41 + foreach ($modules as $module) {
42 + $file_url = module_invoke($module['module'], 'file_server', 'url', $file_path, $absolute_url);
43 +
44 + // If the URL rewriting function sucesfully generated a URL, we can stop
45 + // trying to find a server that can serve the file.
46 + if ($file_url) {
47 + break;
48 + }
49 + }
50 +
51 + // Always fall back to Drupal's default file server, to guarantee a working
52 + // file URL.
53 + if (!$file_url) {
54 + $prefix = ($absolute_url) ? $GLOBALS['base_url'] .'/' : base_path();
55 + $file_url = $prefix . $file_path;
56 + }
57 +
58 + return $file_url;
59 +}
60 +
61 +/**
62 * Provide a substitute clone() function for PHP4.
63 */
64 function drupal_clone($object) {
65 @@ -1474,15 +1524,15 @@ function drupal_get_css($css = NULL) {
66 // If a CSS file is not to be preprocessed and it's a module CSS file, it needs to *always* appear at the *top*,
67 // regardless of whether preprocessing is on or off.
68 if (!$preprocess && $type == 'module') {
69 - $no_module_preprocess .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $file .'";</style>' ."\n";
70 + $no_module_preprocess .= '<style type="text/css" media="'. $media .'">@import "'. file_url($file) .'";</style>' ."\n";
71 }
72 // If a CSS file is not to be preprocessed and it's a theme CSS file, it needs to *always* appear at the *bottom*,
73 // regardless of whether preprocessing is on or off.
74 else if (!$preprocess && $type == 'theme') {
75 - $no_theme_preprocess .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $file .'";</style>' ."\n";
76 + $no_theme_preprocess .= '<style type="text/css" media="'. $media .'">@import "'. file_url($file) .'";</style>' ."\n";
77 }
78 else {
79 - $output .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $file .'";</style>' ."\n";
80 + $output .= '<style type="text/css" media="'. $media .'">@import "'. file_url($file) .'";</style>' ."\n";
81 }
82 }
83 }
84 @@ -1491,7 +1541,7 @@ function drupal_get_css($css = NULL) {
85 if ($is_writable && $preprocess_css) {
86 $filename = md5(serialize($types)) .'.css';
87 $preprocess_file = drupal_build_css_cache($types, $filename);
88 - $output .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $preprocess_file .'";</style>'. "\n";
89 + $output .= '<style type="text/css" media="'. $media .'">@import "'. file_url($preprocess_file) .'";</style>'. "\n";
90 }
91 }
92
93 @@ -1526,7 +1576,7 @@ function drupal_build_css_cache($types,
94 // Return the path to where this CSS file originated from, stripping off the name of the file at the end of the path.
95 $path = base_path() . substr($file, 0, strrpos($file, '/')) .'/';
96 // Wraps all @import arguments in url().
97 - $contents = preg_replace('/@import\s+(?!url)[\'"]?(\S*)\b[\'"]?/i', '@import url("\1")', $contents);
98 + $contents = preg_replace('/@import\s+(?!url)[\'"]?(\S*)\b[\'"]?/i', '@import url("\1")', $contents);
99 // Fix all paths within this CSS file, ignoring absolute paths.
100 $data .= preg_replace('/url\(([\'"]?)(?![a-z]+:)/i', 'url(\1'. $path . '\2', $contents);
101 }
102 @@ -1685,7 +1735,7 @@ function drupal_get_js($scope = 'header'
103 break;
104 default:
105 foreach ($data as $path => $info) {
106 - $output .= '<script type="text/javascript"'. ($info['defer'] ? ' defer="defer"' : '') .' src="'. check_url(base_path() . $path) . ($info['cache'] ? '' : '?'. time()) ."\"></script>\n";
107 + $output .= '<script type="text/javascript"'. ($info['defer'] ? ' defer="defer"' : '') .' src="'. check_url(file_url($path)) . ($info['cache'] ? '' : '?'. time()) ."\"></script>\n";
108 }
109 }
110 }
111 Index: includes/file.inc
112 ===================================================================
113 RCS file: /cvs/drupal/drupal/includes/file.inc,v
114 retrieving revision 1.90.2.3
115 diff -u -F^f -r1.90.2.3 file.inc
116 --- includes/file.inc 7 Jan 2008 01:00:22 -0000 1.90.2.3
117 +++ includes/file.inc 26 Jan 2008 22:08:16 -0000
118 @@ -33,7 +33,7 @@ function file_create_url($path) {
119 }
120 switch (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)) {
121 case FILE_DOWNLOADS_PUBLIC:
122 - return $GLOBALS['base_url'] .'/'. file_directory_path() .'/'. str_replace('\\', '/', $path);
123 + return file_url(file_directory_path() .'/'. str_replace('\\', '/', $path), TRUE);
124 case FILE_DOWNLOADS_PRIVATE:
125 return url('system/files/'. $path, NULL, NULL, TRUE);
126 }
127 Index: includes/theme.inc
128 ===================================================================
129 RCS file: /cvs/drupal/drupal/includes/theme.inc,v
130 retrieving revision 1.337.2.2
131 diff -u -F^f -r1.337.2.2 theme.inc
132 --- includes/theme.inc 31 May 2007 05:52:42 -0000 1.337.2.2
133 +++ includes/theme.inc 26 Jan 2008 22:08:16 -0000
134 @@ -322,24 +322,24 @@ function theme_get_setting($setting_name
135
136 if ($settings['toggle_logo']) {
137 if ($settings['default_logo']) {
138 - $settings['logo'] = base_path() . dirname($theme_object->filename) .'/logo.png';
139 + $settings['logo'] = file_url(dirname($theme_object->filename) .'/logo.png');
140 }
141 elseif ($settings['logo_path']) {
142 - $settings['logo'] = base_path() . $settings['logo_path'];
143 + $settings['logo'] = file_url($settings['logo_path']);
144 }
145 }
146
147 if ($settings['toggle_favicon']) {
148 if ($settings['default_favicon']) {
149 if (file_exists($favicon = dirname($theme_object->filename) .'/favicon.ico')) {
150 - $settings['favicon'] = base_path() . $favicon;
151 + $settings['favicon'] = file_url($favicon);
152 }
153 else {
154 - $settings['favicon'] = base_path() . 'misc/favicon.ico';
155 + $settings['favicon'] = file_url('misc/favicon.ico');
156 }
157 }
158 elseif ($settings['favicon_path']) {
159 - $settings['favicon'] = base_path() . $settings['favicon_path'];
160 + $settings['favicon'] = file_url($settings['favicon_path']);
161 }
162 else {
163 $settings['toggle_favicon'] = FALSE;
164 @@ -613,8 +613,7 @@ function theme_links($links, $attributes
165 function theme_image($path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
166 if (!$getsize || (is_file($path) && (list($width, $height, $type, $image_attributes) = @getimagesize($path)))) {
167 $attributes = drupal_attributes($attributes);
168 - $url = (url($path) == $path) ? $path : (base_path() . $path);
169 - return '<img src="'. check_url($url) .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $image_attributes . $attributes .' />';
170 + return '<img src="'. check_url(file_url($path)) .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $image_attributes . $attributes .' />';
171 }
172 }
173
174 Index: modules/system/system.module
175 ===================================================================
176 RCS file: /cvs/drupal/drupal/modules/system/system.module,v
177 retrieving revision 1.440.2.28
178 diff -u -F^f -r1.440.2.28 system.module
179 --- modules/system/system.module 11 Jan 2008 00:47:17 -0000 1.440.2.28
180 +++ modules/system/system.module 26 Jan 2008 22:08:18 -0000
181 @@ -738,9 +738,69 @@ function system_file_system_settings() {
182 '#description' => t('If you want any sort of access control on the downloading of files, this needs to be set to <em>private</em>. You can change this at any time, however all download URLs will change and there may be unexpected problems so it is not recommended.')
183 );
184
185 + $form['file_servers'] = array(
186 + '#tree' => TRUE,
187 + '#type' => 'fieldset',
188 + '#title' => t('File servers'),
189 + '#description' => t(
190 + '<strong>This setting is for advanced users</strong>.<br />
191 + Enables Drupal to serve files from any kind of file server. Drupal will
192 + automatically fall back to file servers that are lower in the list.
193 + <br />The web server Drupal is running on will always be used as the
194 + default file server.'),
195 + '#collapsible' => FALSE,
196 + );
197 +
198 + $modules = array();
199 + foreach (module_implements('file_server', TRUE) as $module) {
200 + $info = _module_parse_info_file(drupal_get_path('module', $module) ."/$module.info");
201 + $file_server_info = module_invoke($module, 'file_server', 'info');
202 +
203 + $modules[$module] = array(
204 + 'type' => $file_server_info['type'],
205 + 'module' => (!isset($file_server_info['configuration_page'])) ? $info['name'] : l($info['name'], $file_server_info['configuration_page']),
206 + 'weight' => variable_get("file_server_{$module}_weight", 0),
207 + );
208 + }
209 +
210 + // Sort file server types by weight.
211 + uasort($modules, create_function('$a, $b', 'return $a["weight"] > $b["weight"];'));
212 +
213 + foreach ($modules as $module => $settings) {
214 + $form['file_servers']['types'][$module] = array('#value' => $settings['type']);
215 + $form['file_servers']['modules'][$module] = array('#value' => $settings['module']);
216 + $form['file_servers']['weights']["file_server_{$module}_weight"] = array('#type' => 'weight', '#default_value' => $settings['weight']);
217 + }
218 +
219 + // The web server Drupal is running on is always available, and it's the
220 + // default server, i.e. a server we can always fall back to. So it must
221 + // always be at the bottom of the list and don't have a configurable weight.
222 + $form['file_servers']['types']['drupal'] = array('#value' => t('Web server'));
223 + $form['file_servers']['modules']['drupal'] = array('#value' => 'Drupal');
224 + $form['file_servers']['weights']['file_server_drupal_weight'] = array('#value' => t('Default server'));
225 +
226 return system_settings_form($form);
227 }
228
229 +function theme_system_file_system_settings($form) {
230 + $header = array(t('Type'), t('Module'), t('Weight'));
231 + $rows = array();
232 + foreach (element_children($form['file_servers']['modules']) as $module) {
233 + // Don't take form control structures
234 + if (is_array($form['file_servers']['modules'][$module])) {
235 + $rows[] = array(
236 + drupal_render($form['file_servers']['types'][$module]),
237 + drupal_render($form['file_servers']['modules'][$module]),
238 + drupal_render($form['file_servers']['weights']["file_server_{$module}_weight"]),
239 + );
240 + }
241 + }
242 +
243 + $form['file_servers']['#value'] = theme('table', $header, $rows);
244 +
245 + return drupal_render($form);
246 +}
247 +
248 function system_image_toolkit_settings() {
249 $toolkits_available = image_get_available_toolkits();
250 if (count($toolkits_available) > 1) {

  ViewVC Help
Powered by ViewVC 1.1.2