function copyRecursive($source, $dest, $copyCallback = null) { if (is_file($source)) { $c = copy($source, $dest); chmod($dest, 0777); if ($copyCallback) { call_user_func($copyCallback, $dest); } return $c; } if (!is_dir($dest)) { $oldumask = umask(0); mkdir($dest, 0777); umask($oldumask); } $dir = dir($source); while (false !== ($entry = $dir->read())) { if ($entry == "." || $entry == "..") { continue; } if ($dest !== $source . '/' . $entry) { copyRecursive($source . '/' . $entry, $dest . '/' . $entry, $copyCallback); } } $dir->close(); return true; }
private function doCopyTheme() { ClassLoader::importNow('application.helper.CopyRecursive'); $request = $this->getRequest(); $this->fromTheme = $request->get('id'); $this->toTheme = $request->get('name'); $files = $this->getThemeFiles($this->fromTheme); $copyFiles = $this->getThemeFiles($this->toTheme, false); $baseDir = ClassLoader::getBaseDir(); foreach ($files as $key => $orginalFileName) { if (array_key_exists($key, $copyFiles)) { $copyToFileName = $copyFiles[$key]; } else { if (preg_match('/public.?upload.?css.?delete/', $orginalFileName)) { // orginal theme files matching glob('public/upload/css/delete/<theme>-*.php') // get copyTo file name by replacing $copyToFileName = str_replace('public/upload/css/delete/' . $this->fromTheme . '-', 'public/upload/css/delete/' . $this->toTheme . '-', $orginalFileName); $copyFiles[] = $copyToFileName; } else { continue; // only if new type of files added in themes } } copyRecursive($baseDir . DIRECTORY_SEPARATOR . $orginalFileName, $baseDir . DIRECTORY_SEPARATOR . $copyToFileName, array($this, 'onThemeFileCopied')); } return array('status' => 'success', 'id' => $this->toTheme, 'message' => $this->maketext('_theme_copied', array($this->fromTheme, $this->toTheme))); }
function copyRecursive($from, $to, $mode = 0666, $dir_mode = 0777) { $orig_dir_mode = $dir_mode; if ($dir_mode == 0777) { $dir_mode = get_filesystem_permissions(0777); } elseif ($dir_mode == 0755) { $dir_mode = get_filesystem_permissions(0755); } $orig_mode = $mode; if ($mode == 0666) { $mode = get_filesystem_permissions(0666, $from); } elseif ($mode == 0644) { $mode = get_filesystem_permissions(0644, $from); } if (@is_file($from)) { @copy($from, $to); @umask(00); @chmod($to, $mode); } elseif (@is_dir($from)) { if (!@file_exists($to)) { @umask(00); $attempts = 5; while (!@mkdir($to, $dir_mode)) { \Includes\Utils\FileManager::unlinkRecursive($to); $attempts--; if ($attempts < 0) { echo "Can't create directory {$to}: permission denied"; die; } } } if ($handle = @opendir($from)) { while (false !== ($file = @readdir($handle))) { if (!($file == "." || $file == "..")) { copyRecursive($from . '/' . $file, $to . '/' . $file, $orig_mode, $orig_dir_mode); } } @closedir($handle); } } else { return 1; } }