Warning: file_get_contents(): Filename cannot be empty in _locale_parse_js_file() includes/locale.inc)

backtrace:

file_get_contents('') locale.inc:1502
_locale_parse_js_file('https://maps.googleapis.com/maps/api/js?sensor=true') locale.module:917
locale_js_alter(Array, NULL, NULL, NULL) module.inc:1109
drupal_alter('js', Array) common.inc:4271
drupal_get_js('footer') theme.inc:2631
template_process_html(Array, 'html') theme.inc:1122
theme('html', Array) common.inc:5930
drupal_render(Array) common.inc:5777

Looking at the code I am not sure what is done with external js files like this.

the locale.inc file is:

function _locale_parse_js_file($filepath) {
  global $language;

  // The file path might contain a query string, so make sure we only use the
  // actual file.
  $parsed_url = drupal_parse_url($filepath);
  $filepath = drupal_realpath($parsed_url['path']);
  // Load the JavaScript file.

  $file = file_get_contents($filepath);

At https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_... api.drupal.org on drupal.parse.url states:
This function should only be used for URLs that have been generated by the system, such as via url(). It should not be used for URLs that come from external sources, or URLs that link to external resources.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

SocialNicheGuru’s picture

just wonder if the code checks for the external flag on urls. if so many urls don't set it. google just in case.

SocialNicheGuru’s picture

Am I thinking about this in the right way or would the strpos add to much of a lag? is there a better way to exclude external scripts from the drupal_parse_url function?

//make sure that it is not an external file
//http://www.php.net/file_get_contents

//should we add external as another option since some scripts specify it?
if ((strpos($filepath, 'http') === TRUE)||(strpos($filepath, 'https') === TRUE)){
   $filepath = urlencode($filepath);
}
else{
  // The file path might contain a query string, so make sure we only use the
  // actual file.
  $parsed_url = drupal_parse_url($filepath);
  $filepath = drupal_realpath($parsed_url['path']);
  // Load the JavaScript file.
}

  $file = file_get_contents($filepath);

SocialNicheGuru’s picture

Version: 7.24 » 7.27
nicobot’s picture

I'm not sure how you get this error, since file_get_contents() should receive at least a string with the path.

Even though, drupal_parse_url() is able to handle external script files, it's true that GET parameters might change the content of a script file (as you pointed with gmaps and its libraries); so the parsing is not being done correctly.

But, I also think that some external files (again, as pointed out, like gmaps) should not be processed by locale since they might not be localizable.
So it would be nice to have a way of selecting JS files to be excluded of this processing, as it incurs on a waste of time and resources in the backend affecting the page load time.

As a proposal, all external libraries could be excluded and just if needed, the programmer can make a drupal_add_js() for an external file also indicating it as "localizable". Or a less aggressive change could be to mark external libraries as "not_localizable".

If we have an agreement, I don't mind to make a patch for this.

pinoniq’s picture

We currently have a similar problem. The following error is occurring:
failed to open stream: No such file or directory in _locale_parse_js_file

This because the file being added is an external one that needs the query parameters to be added.
Having an option in drupal_add_js 'localize' => 'false' that defaults to true seems to be the way forward here.

Has there been any work done so far?

pinoniq’s picture

Version: 7.27 » 7.42
jmdorian’s picture

I have same problem too,when using query-parameters in url of external JS library.
+1 to an option like "localize".

jmdorian’s picture

Version: 7.42 » 7.58
berramou’s picture

I fix this by pathing includes/locale.inc :


   -  $filepath = $parsed_url['path'];
   +  $filepath = drupal_realpath($parsed_url['path']);

    -  $file = file_get_contents($filepath);
   +  $file = (is_file($filepath)) ? file_get_contents($filepath) : '';

Mike Lewis’s picture

Mike Lewis’s picture

Status: Active » Needs review

Status: Needs review » Needs work
DamienMcKenna’s picture

Patch #13 is exactly the same as #10.