Merge lp:~wimleers/pressflow/hook_file_url_alter_bugfix into lp:pressflow

Proposed by Wim Leers
Status: Rejected
Rejected by: David Strauss
Proposed branch: lp:~wimleers/pressflow/hook_file_url_alter_bugfix
Merge into: lp:pressflow
Diff against target: 73 lines (+8/-30)
1 file modified
includes/file.inc (+8/-30)
To merge this branch: bzr merge lp:~wimleers/pressflow/hook_file_url_alter_bugfix
Reviewer Review Type Date Requested Status
Wim Leers (community) Needs Resubmitting
David Strauss Needs Fixing
Review via email: mp+39054@code.launchpad.net

Description of the change

Includes the bugfixes for the bugs reported in https://bugs.launchpad.net/pressflow/+bug/597718. Only the file_create_url() function in includes/file.inc is affected.

Big thanks go to jcmarco, whose insight has uncovered flaws in my initial patch.

Note: the exact same code for file_create_url() is used in the CDN module's (http://drupal.org/project/cdn) core patch, as of version 2.0 beta 6 (http://drupal.org/node/933908). No more problems have been reported.

To post a comment you must log in.
Revision history for this message
David Strauss (davidstrauss) wrote :

This is a little silly:

> drupal_substr($path, 0, 1) == '/' || drupal_substr($path, 0, 2) == '//'

Anything that satisfies the latter condition inherently satisfies the former.

This confuses me:

> if (strpos($path, file_directory_path() . '/') === 0) {
> $path = trim(substr($path, strlen(file_directory_path())), '\\/');
> }

If we know $path begins with file_directory_path() plus a slash, why would I set $path to the first N characters of itself, where N is the length of file_directory_path()? It seems like that would set $path = trim(file_directory_path(), '\\/').

Finally, I don't see why the preg_match() should be case-insensitive, while other code (like the match I'm confused about above) is case-sensitive.

review: Needs Fixing
Revision history for this message
David Strauss (davidstrauss) wrote :

Never mind about the file_directory_path() thing I was confused about. I mis-remembered the parameters to substr() to be substr(<string>, <characters-to-take>) where the second parameter is actually where to begin. My other two concerns remain.

Revision history for this message
Wim Leers (wimleers) wrote :

> This is a little silly:
>
>> drupal_substr($path, 0, 1) == '/' || drupal_substr($path, 0, 2) == '//'
>
> Anything that satisfies the latter condition inherently satisfies the former.

It is. But it's for clarity: the code now corresponds to the comment:
> // Return path if it was altered by any module, or if it already is a
> // root-relative or a protocol-relative URL.

It's not a bug, it's just to make the code more legible/understandable/maintainable.

> Finally, I don't see why the preg_match() should be case-insensitive, while other > code (like the match I'm confused about above) is case-sensitive.

You're right, this is not necessary. But it can't do anything wrong either. It's a tiny performance hit, I suppose. Fixed: http://bazaar.launchpad.net/~wimleers/pressflow/hook_file_url_alter_bugfix/revision/97.

review: Needs Resubmitting
Revision history for this message
Wim Leers (wimleers) wrote :

Is there anything else I can do to move this forward?

Revision history for this message
David Strauss (davidstrauss) wrote :

> Is there anything else I can do to move this forward?

I just need to find the time to review it. I think you've done everything you can. Thanks.

Revision history for this message
Aaron Moore (mcaden) wrote :

This patch resolved this issue for me: http://drupal.org/node/997364

Revision history for this message
David Strauss (davidstrauss) wrote :

Pressflow 6 is now maintained on GitHub, so I'm marking this merge as rejected.

Revision history for this message
goofrider (goofrider) wrote :

@mcaden:

I have the same problem with pressflow 6 (some file url return as href="//sites/...." on my staging sites). This patch didn't work for me though. :(

Unmerged revisions

97. By Wim Leers <email address hidden>

Fixed: the regular expression to detect shipped files was case-insensitive, while this was not necessary.

96. By Wim Leers <email address hidden>

Fixed #597718: file.inc function file_create_url() not correct

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'includes/file.inc'
2--- includes/file.inc 2010-08-11 21:05:18 +0000
3+++ includes/file.inc 2010-10-22 15:57:50 +0000
4@@ -48,7 +48,7 @@
5 * A string containing the Drupal path (i.e. path relative to the Drupal
6 * root directory) of the file to generate the URL for.
7 * @return
8- * A string containing a URL that can be used to download the file.
9+ * A string containing a URL that can be used to access the file.
10 */
11 function file_create_url($path) {
12 // Clean up Windows paths.
13@@ -56,21 +56,14 @@
14
15 drupal_alter('file_url', $path);
16
17- // If any module has altered the path, then return the alteration.
18- if ($path != $old_path) {
19+ // Return path if it was altered by any module, or if it already is a
20+ // root-relative or a protocol-relative URL.
21+ if ($path != $old_path || drupal_substr($path, 0, 1) == '/' || drupal_substr($path, 0, 2) == '//') {
22 return $path;
23 }
24
25- // Otherwise serve the file from Drupal's web server. This point will only
26- // be reached when either no custom_file_url_rewrite() function has been
27- // defined, or when that function returned FALSE, thereby indicating that it
28- // cannot (or doesn't wish to) rewrite the URL. This is typically because
29- // the file doesn't match some conditions to be served from a CDN or static
30- // file server, or because the file has not yet been synced to the CDN or
31- // static file server.
32-
33 // Shipped files.
34- if (strpos($path, file_directory_path() . '/') !== 0) {
35+ if (preg_match("#^(/?)(misc|modules|sites|themes)/#", $path) && (strpos($path, file_directory_path() . '/') !== 0)) {
36 return base_path() . $path;
37 }
38 // Created files.
39@@ -83,7 +76,9 @@
40 // rewritten to be served relatively to system/files (which is a menu
41 // callback that streams the file) instead of relatively to the file
42 // directory path.
43- $path = file_directory_strip($path);
44+ if (strpos($path, file_directory_path() . '/') === 0) {
45+ $path = trim(substr($path, strlen(file_directory_path())), '\\/');
46+ }
47 return url('system/files/' . $path, array('absolute' => TRUE));
48 }
49 }
50@@ -1046,23 +1041,6 @@
51 }
52
53 /**
54- * Remove a possible leading file directory path from the given path.
55- *
56- * @param $path
57- * Path to a file that may be in Drupal's files directory.
58- * @return
59- * String with Drupal's files directory removed from it.
60- */
61-function file_directory_strip($path) {
62- // Strip file_directory_path from $path. We only include relative paths in
63- // URLs.
64- if (strpos($path, file_directory_path() . '/') === 0) {
65- $path = trim(substr($path, strlen(file_directory_path())), '\\/');
66- }
67- return $path;
68-}
69-
70-/**
71 * Determine the maximum file upload size by querying the PHP settings.
72 *
73 * @return

Subscribers

People subscribed via source and target branches

to status/vote changes: