throw new Exception("Overly broad deletion");
}
- if ($sourcedir = @opendir($target)) {
- while (FALSE !== ($sibling = readdir($sourcedir))) {
+ if ($dh = @opendir($target)) {
+ while (FALSE !== ($sibling = readdir($dh))) {
if (!in_array($sibling, $exceptions)) {
$object = $target . DIRECTORY_SEPARATOR . $sibling;
}
}
}
- closedir($sourcedir);
+ closedir($dh);
if ($rmdir) {
if (rmdir($target)) {
* @param $destination
*/
static function copyDir($source, $destination) {
- $dir = opendir($source);
- @mkdir($destination);
- while (FALSE !== ($file = readdir($dir))) {
- if (($file != '.') && ($file != '..')) {
- if (is_dir($source . DIRECTORY_SEPARATOR . $file)) {
- CRM_Utils_File::copyDir($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
- }
- else {
- copy($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
+ if ($dh = opendir($source)) {
+ @mkdir($destination);
+ while (FALSE !== ($file = readdir($dh))) {
+ if (($file != '.') && ($file != '..')) {
+ if (is_dir($source . DIRECTORY_SEPARATOR . $file)) {
+ CRM_Utils_File::copyDir($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
+ }
+ else {
+ copy($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
+ }
}
}
+ closedir($dh);
}
- closedir($dir);
}
/**
}
/**
- * Appends trailing slashed to paths
+ * Appends a slash to the end of a string if it doesn't already end with one
*
- * @param $name
- * @param null $separator
+ * @param string $path
+ * @param string $slash
*
* @return string
* @access public
* @static
*/
- static function addTrailingSlash($name, $separator = NULL) {
- if (!$separator) {
- $separator = DIRECTORY_SEPARATOR;
+ static function addTrailingSlash($path, $slash = NULL) {
+ if (!$slash) {
+ // FIXME: Defaulting to backslash on windows systems can produce unexpected results, esp for URL strings which should always use forward-slashes.
+ // I think this fn should default to forward-slash instead.
+ $slash = DIRECTORY_SEPARATOR;
}
-
- if (substr($name, -1, 1) != $separator) {
- $name .= $separator;
+ if (!in_array(substr($path, -1, 1), array('/', '\\'))) {
+ $path .= $slash;
}
- return $name;
+ return $path;
}
/**
*/
static function getFilesByExtension($path, $ext) {
$path = self::addTrailingSlash($path);
- $dh = opendir($path);
$files = array();
- while (FALSE !== ($elem = readdir($dh))) {
- if (substr($elem, -(strlen($ext) + 1)) == '.' . $ext) {
- $files[] .= $path . $elem;
+ if ($dh = opendir($path)) {
+ while (FALSE !== ($elem = readdir($dh))) {
+ if (substr($elem, -(strlen($ext) + 1)) == '.' . $ext) {
+ $files[] .= $path . $elem;
+ }
}
+ closedir($dh);
}
- closedir($dh);
return $files;
}
}
}
}
- $dh = opendir($subdir);
- if ($dh) {
+ if ($dh = opendir($subdir)) {
while (FALSE !== ($entry = readdir($dh))) {
$path = $subdir . DIRECTORY_SEPARATOR . $entry;
if ($entry{0} == '.') {
}
return TRUE;
}
-
- /**
- * Create a static file (e.g. css or js) in the dynamic resource directory
- * Note: if the file already exists it will be overwritten
- * @param string $fileName
- * @param string $contents
- */
- static function addDynamicResource($fileName, $contents) {
- // First ensure the directory exists
- $path = self::dynamicResourcePath();
- if (!is_dir($path)) {
- self::createDir($path);
- self::restrictBrowsing($path);
- }
- file_put_contents("$path/$fileName", $contents);
- }
-
- /**
- * Get the path of a dynamic resource file
- * With no fileName supplied, returns the path of the directory
- * @param string $fileName
- * @return string
- */
- static function dynamicResourcePath($fileName = NULL) {
- $config = CRM_Core_Config::singleton();
- // FIXME: Use self::baseFilePath once url issue has been resolved
- // Windows PHP accepts any mix of "/" or "\"; simpler if we only deal with one of those
- $imageUploadDir = str_replace('\\', '/', $config->imageUploadDir);
- $path = self::addTrailingSlash(str_replace('/persist/contribute', '', $imageUploadDir)) . 'dynamic';
- if ($fileName !== NULL) {
- $path .= "/$fileName";
- }
- return $path;
- }
-
- /**
- * Get the URL of a dynamic resource file
- * @param string $fileName
- * @return string
- */
- static function dynamicResourceUrl($fileName, $addCacheCode = TRUE) {
- $config = CRM_Core_Config::singleton();
- // FIXME: Need a better way of getting the url of the baseFilePath
- $url = self::addTrailingSlash(str_replace('/persist/contribute', '', $config->imageUploadURL), '/') . 'dynamic/' . $fileName;
- if ($addCacheCode) {
- return $url . '?r=' . CRM_Core_Resources::singleton()->getCacheCode();
- }
- return $url;
- }
-
- /**
- * Delete all files from the dynamic resource directory
- * Change the cache code to force browsers to reload new resources
- */
- static function flushDynamicResources() {
- $files = glob(self::dynamicResourcePath('*'));
- foreach ($files ? $files : array() as $file) {
- if (is_file($file)) {
- unlink($file);
- }
- }
- CRM_Core_Resources::singleton()->resetCacheCode();
- }
}