boost.helpers.inc 2.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php
// $Id$

/**
 * @file
 * Various helper functions for the Boost module, to make life a bit easier.
 */

//////////////////////////////////////////////////////////////////////////////

/**
 * Recursive version of mkdir(), compatible with PHP4.
 */
function _boost_mkdir_p($pathname, $mode = 0775, $recursive = TRUE) {
  if (is_dir($pathname)) return TRUE;
  if ($recursive && !_boost_mkdir_p(dirname($pathname), $mode)) return FALSE;
  if ($result = @mkdir($pathname, $mode))
    @chmod($pathname, $mode);
  return $result;
}

/**
 * Recursive version of rmdir(); use with extreme caution.
24 25 26 27 28
 *
 * @param $dirname
 *   the top-level directory that will be recursively removed
 * @param $callback
 *   optional predicate function for determining if a file should be removed
29 30
 */
function _boost_rmdir_rf($dirname, $callback = NULL) {
31 32
  $empty = TRUE; // Start with an optimistic mindset

33 34
  foreach (glob($dirname . '/*', GLOB_NOSORT) as $file) {
    if (is_dir($file)) {
35 36
      if (!_boost_rmdir_rf($file, $callback))
        $empty = FALSE;
37 38
    }
    else if (is_file($file)) {
39 40 41 42 43 44 45
      if (function_exists($callback)) {
        if (!$callback($file)) {
          $empty = FALSE;
          continue;
        }
      }
      @unlink($file);
46 47
    }
  }
48 49 50 51 52

  // The reason for this elaborate safeguard is that Drupal will log even
  // warnings that should have been suppressed with the @ sign. Otherwise,
  // we'd just rely on the FALSE return value from rmdir().
  return ($empty && @rmdir($dirname));
53 54 55 56 57 58 59 60 61
}

/**
 * Creates a symbolic link using a computed relative path where possible.
 */
function _boost_symlink($target, $link) {
  if (!file_exists($target) || !file_exists(dirname($link)))
    return FALSE;

62 63
  $target = explode('/', $target);
  $link = explode('/', $link);
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

  // Only bother creating a relative link if the paths are in the same
  // top-level directory; otherwise just symlink to the absolute path.
  if ($target[1] == $link[1]) {
    // Remove the common path prefix
    $cwd = array();
    while (count($target) > 0 && count($link) > 0 && reset($target) == reset($link)) {
      $cwd[] = array_shift($target);
      array_shift($link);
    }
    // Compute the required relative path
    if (count($link) > 1)
      $target = array_merge(array_fill(0, count($link) - 1, '..'), $target);
    $link = array_merge($cwd, $link);
  }

  return symlink(implode('/', $target), implode('/', $link));
}

//////////////////////////////////////////////////////////////////////////////
84 85 86 87 88 89 90 91 92 93 94 95 96 97
// PHP4 COMPATIBILITY

if (!function_exists('file_put_contents')) {
  function file_put_contents($filename, $data) {
    if ($fp = fopen($filename, 'wb')) {
      fwrite($fp, $data);
      fclose($fp);
      return filesize($filename);
    }
    return FALSE;
  }
}

//////////////////////////////////////////////////////////////////////////////