Index: filefield_paths.module =================================================================== --- filefield_paths.module (revision 233) +++ filefield_paths.module (working copy) @@ -281,24 +281,30 @@ */ 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'] : ''; - $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']})"); + $info = parse_url(file_create_url(file_stream_wrapper_uri_normalize($old))); + $path = empty($info['path']) ? '' : $info['path']; + + // Build list of possible prefixes for matching old links and choosing like replacements. + $pre = array(); + $pre['abs'] = str_replace($path, '', file_create_url($old)); + $pre['rel'] = parse_url($absolute, PHP_URL_PATH); + $pre['wrap'] = $info['scheme'] . '://'; + $regex = "({$pre['abs']}|{$pre['rel']}|{$pre['wrap']})(styles\/.*?\/|)$path"; - // Build replacement. - $info = parse_url(file_stream_wrapper_uri_normalize($new)); - $info['path'] = !empty($info['path']) ? $info['path'] : ''; - $replacement = "_filefield_paths_replace_path_uri_scheme('\\1', '{$old}', '{$new}') . '\\2{$info['host']}{$info['path']}'"; + // Build replacement handler. + $info = parse_url(file_create_url(file_stream_wrapper_uri_normalize($new))); + $path = empty($info['path']) ? '' : $info['path']; + // lets use double quotes because they're not a valid filename character in any known filesystem + $replacer = "_filefield_paths_replace_path_uri_scheme('\\1', \"$pre\", \"$new\") . \"\\2$path\""; $fields = field_info_fields(); foreach ($fields as $name => $field) { - 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']); - } + if ($field['module'] != 'text' || !isset($entity->{$field['field_name']}) || !is_array($entity->{$field['field_name']})) { + continue; + } + foreach ($entity->{$field['field_name']} as &$language) { + foreach ($language as &$item) { + $item['value'] = preg_replace("/$regex/e", $replacer, $item['value']); } } } @@ -310,16 +316,21 @@ * Determines what format the old URI prefix was and returns the new URI prefix * in the same format. */ -function _filefield_paths_replace_path_uri_scheme($prefix, $old, $new) { +function _filefield_paths_replace_path_uri_scheme($prefix, $options, $new) { + $scheme = file_uri_scheme($new) . '://'; + switch (TRUE) { - case $prefix == file_uri_scheme($old) . '://': - return file_uri_scheme($new) . '://'; + // using stream wrapper + case $prefix == $options['wrap']: + return $scheme; - case $prefix == file_create_url(file_uri_scheme($old) . '://'): - return file_create_url(file_uri_scheme($new) . '://'); + // using absolute link + case $prefix == $options['abs']: + return file_create_url($scheme); - case $prefix == parse_url(file_create_url(file_uri_scheme($old) . '://'), PHP_URL_PATH): - return parse_url(file_create_url(file_uri_scheme($new) . '://'), PHP_URL_PATH); + // using relative link + case $prefix == $options['rel']: + return parse_url(file_create_url($scheme), PHP_URL_PATH); } return $prefix;