Exemple #1
0
 /**
  * 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
  * 
  * @author Sina Salek - http://sina.salek.ws/en/contact 
  * @param $source //file or folder
  * @param $dest ///file or folder
  * @param $options //folderPermission,filePermission
  * @return boolean
  */
 public static function Copy($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;
                 }
                 $result = Gdn_FileSystem::Copy($source . "/" . $file, $__dest, $options);
             }
         }
         closedir($dirHandle);
     } else {
         $result = false;
     }
     return $result;
 }
 /**
  * Overwrites Yaga configurations, dumps Yaga db tables, inserts data via the 
  * model, and copies uploaded files to the server
  * 
  * @param stdClass The info object read in from the archive
  * @param array Which tables should be overwritten
  * @return bool Pass/Fail on the import being executed. Errors can exist on the
  * form with a passing return value.
  */
 protected function _ImportData($Info, $Include)
 {
     if (!$Info) {
         return FALSE;
     }
     // Import Configs
     $Configs = unserialize(file_get_contents(PATH_UPLOADS . DS . 'import' . DS . 'yaga' . DS . $Info->Config));
     $Configurations = $this->_NestedToDotNotation($Configs, 'Yaga');
     foreach ($Configurations as $Name => $Value) {
         SaveToConfig($Name, $Value);
     }
     // Import model data
     foreach ($Include as $Key => $Value) {
         if ($Value) {
             $Data = unserialize(file_get_contents(PATH_UPLOADS . DS . 'import' . DS . 'yaga' . DS . $Info->{$Key}));
             Gdn::SQL()->EmptyTable($Key);
             $ModelName = $Key . 'Model';
             $Model = Yaga::$ModelName();
             foreach ($Data as $Datum) {
                 $Model->Insert((array) $Datum);
             }
             $this->SetData($Key . 'Count', $Model->GetCount());
         }
     }
     // Import uploaded files
     if (Gdn_FileSystem::Copy(PATH_UPLOADS . DS . 'import' . DS . 'yaga' . DS . 'images' . DS . 'uploads' . DS, PATH_UPLOADS . DS) === FALSE) {
         $this->Form->AddError(T('Yaga.Error.TransportCopy'));
     }
     return TRUE;
 }