| 1 |
<?php
|
| 2 |
// $Id: boost.helpers.inc,v 1.4 2006/12/02 12:53:26 arto Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Various helper functions for the Boost module, to make life a bit easier.
|
| 7 |
*/
|
| 8 |
|
| 9 |
//////////////////////////////////////////////////////////////////////////////
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Recursive version of mkdir(), compatible with PHP4.
|
| 13 |
*/
|
| 14 |
function _boost_mkdir_p($pathname, $mode = 0775, $recursive = TRUE) {
|
| 15 |
if (is_dir($pathname)) return TRUE;
|
| 16 |
if ($recursive && !_boost_mkdir_p(dirname($pathname), $mode)) return FALSE;
|
| 17 |
if ($result = @mkdir($pathname, $mode))
|
| 18 |
@chmod($pathname, $mode);
|
| 19 |
return $result;
|
| 20 |
}
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Recursive version of rmdir(); use with extreme caution.
|
| 24 |
*
|
| 25 |
* @param $dirname
|
| 26 |
* the top-level directory that will be recursively removed
|
| 27 |
* @param $callback
|
| 28 |
* optional predicate function for determining if a file should be removed
|
| 29 |
*/
|
| 30 |
function _boost_rmdir_rf($dirname, $callback = NULL) {
|
| 31 |
$empty = TRUE; // Start with an optimistic mindset
|
| 32 |
|
| 33 |
foreach (glob($dirname . '/*', GLOB_NOSORT) as $file) {
|
| 34 |
if (is_dir($file)) {
|
| 35 |
if (!_boost_rmdir_rf($file, $callback))
|
| 36 |
$empty = FALSE;
|
| 37 |
}
|
| 38 |
else if (is_file($file)) {
|
| 39 |
if (function_exists($callback)) {
|
| 40 |
if (!$callback($file)) {
|
| 41 |
$empty = FALSE;
|
| 42 |
continue;
|
| 43 |
}
|
| 44 |
}
|
| 45 |
@unlink($file);
|
| 46 |
}
|
| 47 |
else {
|
| 48 |
$empty = FALSE; // it's probably a symbolic link
|
| 49 |
}
|
| 50 |
}
|
| 51 |
|
| 52 |
// The reason for this elaborate safeguard is that Drupal will log even
|
| 53 |
// warnings that should have been suppressed with the @ sign. Otherwise,
|
| 54 |
// we'd just rely on the FALSE return value from rmdir().
|
| 55 |
return ($empty && @rmdir($dirname));
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 59 |
* Creates a symbolic link using a computed relative path where possible.
|
| 60 |
*/
|
| 61 |
function _boost_symlink($target, $link) {
|
| 62 |
if (!file_exists($target) || !file_exists(dirname($link)))
|
| 63 |
return FALSE;
|
| 64 |
|
| 65 |
$target = explode('/', $target);
|
| 66 |
$link = explode('/', $link);
|
| 67 |
|
| 68 |
// Only bother creating a relative link if the paths are in the same
|
| 69 |
// top-level directory; otherwise just symlink to the absolute path.
|
| 70 |
if ($target[1] == $link[1]) {
|
| 71 |
// Remove the common path prefix
|
| 72 |
$cwd = array();
|
| 73 |
while (count($target) > 0 && count($link) > 0 && reset($target) == reset($link)) {
|
| 74 |
$cwd[] = array_shift($target);
|
| 75 |
array_shift($link);
|
| 76 |
}
|
| 77 |
// Compute the required relative path
|
| 78 |
if (count($link) > 1)
|
| 79 |
$target = array_merge(array_fill(0, count($link) - 1, '..'), $target);
|
| 80 |
$link = array_merge($cwd, $link);
|
| 81 |
}
|
| 82 |
|
| 83 |
return symlink(implode('/', $target), implode('/', $link));
|
| 84 |
}
|
| 85 |
|
| 86 |
//////////////////////////////////////////////////////////////////////////////
|
| 87 |
// PHP4 COMPATIBILITY
|
| 88 |
|
| 89 |
if (!function_exists('file_put_contents')) {
|
| 90 |
function file_put_contents($filename, $data) {
|
| 91 |
if ($fp = fopen($filename, 'wb')) {
|
| 92 |
fwrite($fp, $data);
|
| 93 |
fclose($fp);
|
| 94 |
return filesize($filename);
|
| 95 |
}
|
| 96 |
return FALSE;
|
| 97 |
}
|
| 98 |
}
|
| 99 |
|
| 100 |
//////////////////////////////////////////////////////////////////////////////
|