* delete a directory given a path name, delete children directories
* and files if needed
*
- * @param string $path the path name
+ * @param $target
+ * @param bool $rmdir
+ * @param bool $verbose
+ *
+ * @throws Exception
+ * @internal param string $path the path name
*
* @return void
* @access public
}
}
+ /**
+ * @param $source
+ * @param $destination
+ */
static function copyDir($source, $destination) {
$dir = opendir($source);
@mkdir($destination);
/**
* Appends trailing slashed to paths
*
+ * @param $name
+ * @param null $separator
+ *
* @return string
* @access public
* @static
return $name;
}
+ /**
+ * @param $dsn
+ * @param $fileName
+ * @param null $prefix
+ * @param bool $isQueryString
+ * @param bool $dieOnErrors
+ */
static function sourceSQLFile($dsn, $fileName, $prefix = NULL, $isQueryString = FALSE, $dieOnErrors = TRUE) {
require_once 'DB.php';
}
}
+ /**
+ * @param $ext
+ *
+ * @return bool
+ */
static function isExtensionSafe($ext) {
static $extensions = NULL;
if (!$extensions) {
return $name;
}
+ /**
+ * @param $name
+ *
+ * @return string
+ */
static function makeFileName($name) {
$uniqID = md5(uniqid(rand(), TRUE));
$info = pathinfo($name);
}
}
+ /**
+ * @param $path
+ * @param $ext
+ *
+ * @return array
+ */
static function getFilesByExtension($path, $ext) {
$path = self::addTrailingSlash($path);
$dh = opendir($path);
/**
* Restrict access to a given directory (by planting there a restrictive .htaccess file)
*
- * @param string $dir the directory to be secured
+ * @param string $dir the directory to be secured
+ * @param bool $overwrite
*/
static function restrictAccess($dir, $overwrite = FALSE) {
// note: empty value for $dir can play havoc, since that might result in putting '.htaccess' to root dir
return $_path;
}
+ /**
+ * @param $directory
+ *
+ * @return string
+ */
static function relativeDirectory($directory) {
// Do nothing on windows
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
return $directory;
}
+ /**
+ * @param $directory
+ *
+ * @return string
+ */
static function absoluteDirectory($directory) {
- // Do nothing on windows - config will need to specify absolute path
- if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
+ // check if directory is already absolute, if so return immediately
+ // Note: Windows PHP accepts any mix of "/" or "\", so "C:\htdocs" or "C:/htdocs" would be a valid absolute path
+ if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && preg_match(';^[a-zA-Z]:[/\\\\];', $directory)) {
return $directory;
}
/**
* Make a file path relative to some base dir
*
+ * @param $directory
+ * @param $basePath
+ *
* @return string
*/
static function relativize($directory, $basePath) {
*
* @param string $parent
* @param string $child
+ * @param bool $checkRealPath
+ *
* @return bool
*/
static function isChildPath($parent, $child, $checkRealPath = TRUE) {
*
* @param string $fromDir the directory which should be moved
* @param string $toDir the new location of the directory
+ * @param bool $verbose
+ *
* @return bool TRUE on success
*/
static function replaceDir($fromDir, $toDir, $verbose = FALSE) {
}
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) {
+ $config = CRM_Core_Config::singleton();
+ // FIXME: Need a better way of getting the url of the baseFilePath
+ return self::addTrailingSlash(str_replace('/persist/contribute', '', $config->imageUploadURL), '/') . 'dynamic/' . $fileName;
+ }
+
+ /**
+ * Delete all files from the dynamic resource directory
+ */
+ static function flushDynamicResources() {
+ $files = glob(self::dynamicResourcePath('*'));
+ foreach ($files ? $files : array() as $file) {
+ if (is_file($file)) {
+ unlink($file);
+ }
+ }
+ }
}