diff --git a/filefield_paths.module b/filefield_paths.module index e641ab0..ce1746d 100644 --- a/filefield_paths.module +++ b/filefield_paths.module @@ -282,10 +282,20 @@ function filefield_paths_file_presave($file) { function _filefield_paths_replace_path($old, $new, $entity) { // Build regular expression. $info = parse_url(file_stream_wrapper_uri_normalize($old)); - $info['path'] = !empty($info['path']) ? $info['path'] : ''; + // we urlencode the path and host in case they contain special characters + $info['host'] = !empty($info['host']) ? urlencode($info['host']) : ''; + // for the path we need to first split the path in parts, so that we don't urlencode the forward slashes + $path_parts = !empty($info['path']) ? explode('/', $info['path']) : array(); + foreach($path_parts as $delta => $part) + { + $path_parts[$delta] = urlencode($part); + } + $info['path'] = implode('/', $path_parts); + $absolute = str_replace("{$info['host']}{$info['path']}", '', file_create_url($old)); $relative = parse_url($absolute, PHP_URL_PATH); - $regex = str_replace('/', '\/', "({$absolute}|{$relative}|{$info['scheme']}://)(styles/.*?/{$info['scheme']}/|)({$info['host']}{$info['path']})"); + + $regex = str_replace('/', '\/', "({$absolute}|{$relative}|{$info['scheme']}://)(styles/[^/]+?/{$info['scheme']}/|)({$info['host']}{$info['path']})"); // Build replacement. $info = parse_url(file_stream_wrapper_uri_normalize($new)); @@ -297,7 +307,9 @@ function _filefield_paths_replace_path($old, $new, $entity) { if ($field['module'] == 'text' && isset($entity->{$field['field_name']}) && is_array($entity->{$field['field_name']})) { foreach ($entity->{$field['field_name']} as &$language) { foreach ($language as &$item) { - $item['value'] = preg_replace("/$regex/e", $replacement, $item['value']); + $result = preg_replace("/$regex/e", $replacement, $item['value']); + if($result) + $item['value'] = $result; } } }