You are here

function _advagg_url_to_filename_filter in Advanced CSS/JS Aggregation 7.2

Given a URL output a filtered filename.

Parameters

string $url: The url.

string $strict: If FALSE then slashes will be kept.

Return value

string The filename.

1 call to _advagg_url_to_filename_filter()
advagg_url_to_filename in ./advagg.module
Given a URL output a filename.

File

./advagg.module, line 6227
Advanced CSS/JS aggregation module.

Code

function _advagg_url_to_filename_filter($url, $strict = TRUE) {

  // URL Decode if needed.
  $decoded1 = $url;
  $decoded2 = rawurldecode($decoded1);
  while ($decoded1 != $decoded2) {
    $decoded1 = rawurldecode($decoded2);
    $decoded2 = rawurldecode($decoded1);
  }
  $url = $decoded1;

  // Replace url spaces with a dash.
  $filename = str_replace(array(
    '%20',
    '+',
  ), '-', $url);

  // Remove file system reserved characters
  // https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
  // Remove control charters
  // http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
  // Remove non-printing characters DEL, NO-BREAK SPACE, SOFT HYPHEN
  // Remove URI reserved characters
  // https://tools.ietf.org/html/rfc3986#section-2.2
  // Remove URL unsafe characters
  // https://www.ietf.org/rfc/rfc1738.txt
  if ($strict) {
    $filename = preg_replace('~[<>:"/\\|?*]|[\\x00-\\x1F]|[\\x7F\\xA0\\xAD]|[#\\[\\]@!$&\'()+,;=%]|[{}^\\~`]~x', '-', $filename);
  }
  else {
    $filename = preg_replace('~[<>:"\\|?*]|[\\x00-\\x1F]|[\\x7F\\xA0\\xAD]|[#\\[\\]@!$&\'()+,;=%]|[{}^\\~`]~x', '-', $filename);
  }

  // Replce all white spaces with a dash.
  $filename = preg_replace('/[\\r\\n\\t -]+/', '-', $filename);

  // Avoid ".", ".." or ".hiddenFiles".
  $filename = ltrim($filename, '.-');

  // Compress spaces in a file name and replace with a dash.
  // Compress underscores in a file name and replace with a dash.
  // Compress dashes in a file name and replace with a dash.
  $filename = preg_replace(array(
    '/ +/',
    '/_+/',
    '/-+/',
  ), '-', $filename);

  // Compress dashes and dots in a file name and replace with a dot.
  $filename = preg_replace(array(
    '/-*\\.-*/',
    '/\\.{2,}/',
  ), '.', $filename);

  // Lowercase for windows/unix interoperability
  // http://support.microsoft.com/kb/100625.
  $filename = mb_strtolower($filename, 'UTF-8');

  // Remove ? \ ..
  $filename = str_replace(array(
    '?',
    '\\',
    '..',
  ), '', $filename);

  // ".file-name.-" becomes "file-name".
  $filename = trim($filename, '.-');
  return $filename;
}