/** * Copy file or folder from source to destination, it can do * recursive copy as well and is very smart * It recursively creates the dest file or directory path if there weren't exists * Situtaions : * - Src:/home/test/file.txt ,Dst:/home/test/b ,Result:/home/test/b -> If source was file copy file.txt name with b as name to destination * - Src:/home/test/file.txt ,Dst:/home/test/b/ ,Result:/home/test/b/file.txt -> If source was file Creates b directory if does not exsits and copy file.txt into it * - Src:/home/test ,Dst:/home/ ,Result:/home/test/** -> If source was directory copy test directory and all of its content into dest * - Src:/home/test/ ,Dst:/home/ ,Result:/home/**-> if source was direcotry copy its content to dest * - Src:/home/test ,Dst:/home/test2 ,Result:/home/test2/** -> if source was directoy copy it and its content to dest with test2 as name * - Src:/home/test/ ,Dst:/home/test2 ,Result:->/home/test2/** if source was directoy copy it and its content to dest with test2 as name * @todo * - Should have rollback technique so it can undo the copy when it wasn't successful * - Auto destination technique should be possible to turn off * - Supporting callback function * - May prevent some issues on shared enviroments : http://us3.php.net/umask * @param $source //file or folder * @param $dest ///file or folder * @param $options //folderPermission,filePermission * @return boolean */ private static function smartCopy($source, $dest, $options = array('folderPermission' => 0755, 'filePermission' => 0755)) { $result = false; if (is_file($source)) { if ($dest[strlen($dest) - 1] == '/') { if (!file_exists($dest)) { cmfcDirectory::makeAll($dest, $options['folderPermission'], true); } $__dest = $dest . "/" . basename($source); } else { $__dest = $dest; } $result = copy($source, $__dest); chmod($__dest, $options['filePermission']); } elseif (is_dir($source)) { if ($dest[strlen($dest) - 1] == '/') { if ($source[strlen($source) - 1] == '/') { //Copy only contents } else { //Change parent itself and its contents $dest = $dest . basename($source); @mkdir($dest); chmod($dest, $options['filePermission']); } } else { if ($source[strlen($source) - 1] == '/') { //Copy parent directory with new name and all its content @mkdir($dest, $options['folderPermission']); chmod($dest, $options['filePermission']); } else { //Copy parent directory with new name and all its content @mkdir($dest, $options['folderPermission']); chmod($dest, $options['filePermission']); } } $dirHandle = opendir($source); while ($file = readdir($dirHandle)) { if ($file != "." && $file != "..") { if (!is_dir($source . "/" . $file)) { $__dest = $dest . "/" . $file; } else { $__dest = $dest . "/" . $file; } //echo "$source/$file ||| $__dest<br />"; $result = self::smartCopy($source . "/" . $file, $__dest, $options); } } closedir($dirHandle); } else { $result = false; } return $result; }
/** * Copy files recursively. * * @param String source-- the source directory. * @param String dest -- the destination directory. * @param $options //folderPermission,filePermission * @return boolean */ function copy_recursively($source, $dest, $options = array('folderPermission' => 0755, 'filePermission' => 0755)) { if (is_file($source)) { if ($dest[strlen($dest) - 1] == '/') { if (!file_exists($dest)) { cmfcDirectory::makeAll($dest, $options['folderPermission'], true); } $__dest = $dest . "/" . basename($source); } else { $__dest = $dest; } // Turn off error reporting temporarily error_reporting(0); $result = copy($source, $__dest); if ($result) { chmod($__dest, $options['filePermission']); $this->success = true; $this->logger("Copied to " . $__dest); //Turn on error reporting again error_reporting($this->error_level); } else { $this->success = false; $this->logger("** Failed writing " . $__dest); //Turn on error reporting again error_reporting($this->error_level); return false; } } elseif (is_dir($source)) { if ($dest[strlen($dest) - 1] == '/') { if ($source[strlen($source) - 1] == '/') { //Copy only contents } else { //Change parent itself and its contents $dest = $dest . basename($source); if (!is_writable($dest)) { $this->success = false; $this->logger("** Can't write to " . $dest); return false; } @mkdir($dest); chmod($dest, $options['filePermission']); } } else { if (!is_writable($dest)) { $this->success = false; $this->logger("** Can't write to " . $dest); return false; } if ($source[strlen($source) - 1] == '/') { //Copy parent directory with new name and all its content @mkdir($dest, $options['folderPermission']); chmod($dest, $options['filePermission']); } else { //Copy parent directory with new name and all its content @mkdir($dest, $options['folderPermission']); chmod($dest, $options['filePermission']); } } $dirHandle = opendir($source); while ($file = readdir($dirHandle)) { if ($file != "." and $file != ".." and substr($file, 0, 1) != '.') { if (!is_dir($source . "/" . $file)) { $__dest = $dest . "/" . $file; } else { $__dest = $dest . "/" . $file; } //echo "$source/$file ||| $__dest<br />"; if (!is_writable($__dest)) { $this->success = false; $this->logger("** Can't write to - " . $__dest); return false; } $result = $this->copy_recursively($source . "/" . $file, $__dest, $options); } } closedir($dirHandle); } }
/** * This function calculates the base location of package in filesystem and in browser * This is a critical function for other packages to work properly * By default it points to the real path when symlinks are used * * Site with subdomain should set siteFolderPath manually and also pass siteFolderPathBrowser (http://domain.com) */ function getCalculatedLocations($options, $parameters) { $server = $_SERVER; if (!isset($server['DOCUMENT_ROOT'])) { $server['DOCUMENT_ROOT'] = str_replace('\\', '/', substr($server['SCRIPT_FILENAME'], 0, 0 - strlen($server['PHP_SELF']))); } if (isset($server['DOCUMENT_ROOT'])) { if (strpos('ASD@#$@#$#23423' . dirname(__FILE__), 'ASD@#$@#$#23423' . $server['DOCUMENT_ROOT']) === false) { $server['DOCUMENT_ROOT'] = '/chroot' . $server['DOCUMENT_ROOT']; } } if (!isset($options['pageFolderPath'])) { $options['pageFolderPath'] = dirname($server['SCRIPT_FILENAME']); $options['pageFolderPath'] = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $options['pageFolderPath']); } if (!isset($options['pageFolderPathBrowser'])) { $options['pageFolderPathBrowser'] = dirname($server['SCRIPT_NAME']); } if (!isset($options['siteFolderPath'])) { $options['siteFolderPath'] = $server['DOCUMENT_ROOT']; if (is_link($options['siteFolderPath'])) { $options['siteFolderPath'] = readlink($options['siteFolderPath']); } } if (!isset($options['siteFolderPathBrowser'])) { $options['siteFolderPathBrowser'] = ''; } if (!isset($options['packageFolderPath'])) { $options['packageFolderPath'] = dirname($parameters['filePath']); $options['siteFolderPath'] = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $options['siteFolderPath']); } if (!isset($options['packageFolderPathBrowser'])) { $fileRelativePath = $options['siteFolderPathBrowser'] . '/' . str_replace($options['siteFolderPath'], '', $options['packageFolderPath']); $options['packageFolderPathBrowser'] = cmfcDirectory::normalizePath($fileRelativePath); } if (!isset($options['cmfFolderPath'])) { $options['cmfFolderPath'] = realpath($options['packageFolderPath'] . '/../../..'); $options['cmfFolderPath'] = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $options['cmfFolderPath']); } if (!isset($options['cmfFolderPathBrowser'])) { $fileRelativePath = $options['siteFolderPathBrowser'] . '/' . str_replace($options['siteFolderPath'], '', $options['cmfFolderPath']); $options['cmfFolderPathBrowser'] = cmfcDirectory::normalizePath($fileRelativePath); } return $options; }
public function smartCopy($source, $dest, $options = array('folderPermission' => 0755, 'filePermission' => 0755)) { $result = false; if (is_file($source)) { if ($dest[strlen($dest) - 1] == '/') { if (!file_exists($dest)) { cmfcDirectory::makeAll($dest, $options['folderPermission'], true); } $__dest = $dest . "/" . basename($source); } else { $__dest = $dest; } $result = copy($source, $__dest); chmod($__dest, $options['filePermission']); } elseif (is_dir($source)) { if ($dest[strlen($dest) - 1] == '/') { if ($source[strlen($source) - 1] == '/') { //Copy only contents } else { $dest = $dest . basename($source); @mkdir($dest); chmod($dest, $options['filePermission']); } } else { if ($source[strlen($source) - 1] == '/') { @mkdir($dest, $options['folderPermission']); chmod($dest, $options['filePermission']); } else { @mkdir($dest, $options['folderPermission']); chmod($dest, $options['filePermission']); } } $dirHandle = opendir($source); while ($file = readdir($dirHandle)) { if ($file != "." && $file != "..") { if (!is_dir($source . "/" . $file)) { $__dest = $dest . "/" . $file; } else { $__dest = $dest . "/" . $file; } $result = smartCopy($source . "/" . $file, $__dest, $options); } } closedir($dirHandle); } else { $result = false; } return $result; }
/** * Copy a directory */ function copyDirectoryRecursive($source, $dest) { $result = false; if (is_file($source)) { if ($dest[strlen($dest)-1] == '/') { if (!file_exists($dest)) { cmfcDirectory::makeAll($dest, "0755", true); } $__dest = $dest."/".basename($source); } else { $__dest = $dest; } $result = JFile::copy($source, $__dest); @JPath::setPermissions($__dest, "0644", "0755"); } elseif(is_dir($source)) { if ($dest[strlen($dest)-1] == '/') { if ($source[strlen($source)-1] == '/') { //Copy only contents } else { //Change parent itself and its contents $dest=$dest.basename($source); JFolder::create($dest); @JPath::setPermissions($dest, "0644", "0755"); } } else { if ($source[strlen($source)-1]=='/') { //Copy parent directory with new name and all its content JFolder::create($dest, "0755"); @JPath::setPermissions($dest, "0644", "0755"); } else { //Copy parent directory with new name and all its content JFolder::create($dest, "0755"); @JPath::setPermissions($dest, "0644", "0755"); } } $dirHandle = opendir($source); while($file = readdir($dirHandle)) { if($file != "." && $file != "..") { if(!is_dir($source."/".$file)) { $__dest = $dest."/".$file; } else { $__dest = $dest."/".$file; } //echo "$source/$file ||| $__dest<br />"; $result=copyDirectoryRecursive($source."/".$file, $__dest); } } closedir($dirHandle); } else { $result=false; } return $result; }
private function CopyMailFolder($source, $dest, $options = array('folderPermission' => 0744, 'filePermission' => 0744)) { /** * Copy file or folder from source to destination, it can do * recursive copy as well and is very smart * It recursively creates the dest file or directory path if there weren't exists * Situtaions : * - Src:/home/test/file.txt ,Dst:/home/test/b ,Result:/home/test/b -> If source was file copy file.txt name with b as name to destination * - Src:/home/test/file.txt ,Dst:/home/test/b/ ,Result:/home/test/b/file.txt -> If source was file Creates b directory if does not exsits and copy file.txt into it * - Src:/home/test ,Dst:/home/ ,Result:/home/test/** -> If source was directory copy test directory and all of its content into dest * - Src:/home/test/ ,Dst:/home/ ,Result:/home/**-> if source was direcotry copy its content to dest * - Src:/home/test ,Dst:/home/test2 ,Result:/home/test2/** -> if source was directoy copy it and its content to dest with test2 as name * - Src:/home/test/ ,Dst:/home/test2 ,Result:->/home/test2/** if source was directoy copy it and its content to dest with test2 as name * @todo * - Should have rollback technique so it can undo the copy when it wasn't successful * - Auto destination technique should be possible to turn off * - Supporting callback function * - May prevent some issues on shared enviroments : http://us3.php.net/umask * @param $source //file or folder * @param $dest ///file or folder * @param $options //folderPermission,filePermission * @return boolean */ $result = false; if (is_file($source)) { if ($dest[strlen($dest) - 1] == '/') { if (!file_exists($dest)) { cmfcDirectory::makeAll($dest, $options['folderPermission'], true); } $__dest = $dest . "/" . basename($source); } else { $__dest = $dest; } $result = copy($source, $__dest); chmod($__dest, $options['filePermission']); } elseif (is_dir($source)) { if ($dest[strlen($dest) - 1] == '/') { if ($source[strlen($source) - 1] == '/') { //Copy only contents } else { //Change parent itself and its contents $dest = $dest . basename($source); mkdir($dest); chmod($dest, $options['filePermission']); } } else { if ($source[strlen($source) - 1] == '/') { //Copy parent directory with new name and all its content mkdir($dest, $options['folderPermission']); chmod($dest, $options['filePermission']); } else { //Copy parent directory with new name and all its content mkdir($dest, $options['folderPermission']); chmod($dest, $options['filePermission']); } } $dirHandle = opendir($source); while ($file = readdir($dirHandle)) { if ($file != "." && $file != "..") { if (!is_dir($source . "/" . $file)) { $__dest = $dest . "/" . $file; } else { $__dest = $dest . "/" . $file; } //echo "$source/$file ||| $__dest<br />"; $result = $this->CopyMailFolder($source . "/" . $file, $__dest, $options); } } closedir($dirHandle); } else { $result = false; } return $result; }