Exemplo n.º 1
0
 /**
  * imports a user, or owncloud instance
  * @param string $path path to zip
  * @param string $type type of import (user or instance)
  * @param string|null|int $uid userid of new user
  * @return string
  */
 public static function import($path, $type = 'user', $uid = null)
 {
     $datadir = OC_Config::getValue('datadirectory');
     // Extract the zip
     if (!($extractpath = self::extractZip($path))) {
         return json_encode(array('success' => false));
     }
     // Get export_info.json
     $scan = scandir($extractpath);
     // Check for export_info.json
     if (!in_array('export_info.json', $scan)) {
         OC_Log::write('migration', 'Invalid import file, export_info.json not found', OC_Log::ERROR);
         return json_encode(array('success' => false));
     }
     $json = json_decode(file_get_contents($extractpath . 'export_info.json'));
     if ($json->exporttype != $type) {
         OC_Log::write('migration', 'Invalid import file', OC_Log::ERROR);
         return json_encode(array('success' => false));
     }
     self::$exporttype = $type;
     $currentuser = OC_User::getUser();
     // Have we got a user if type is user
     if (self::$exporttype == 'user') {
         self::$uid = !is_null($uid) ? $uid : $currentuser;
     }
     // We need to be an admin if we are not importing our own data
     if ($type == 'user' && self::$uid != $currentuser || $type != 'user') {
         if (!OC_User::isAdminUser($currentuser)) {
             // Naughty.
             OC_Log::write('migration', 'Import not permitted.', OC_Log::ERROR);
             return json_encode(array('success' => false));
         }
     }
     // Handle export types
     switch (self::$exporttype) {
         case 'user':
             // Check user availability
             if (!OC_User::userExists(self::$uid)) {
                 OC_Log::write('migration', 'User doesn\'t exist', OC_Log::ERROR);
                 return json_encode(array('success' => false));
             }
             // Check if the username is valid
             if (preg_match('/[^a-zA-Z0-9 _\\.@\\-]/', $json->exporteduser)) {
                 OC_Log::write('migration', 'Username is not valid', OC_Log::ERROR);
                 return json_encode(array('success' => false));
             }
             // Copy data
             $userfolder = $extractpath . $json->exporteduser;
             $newuserfolder = $datadir . '/' . self::$uid;
             foreach (scandir($userfolder) as $file) {
                 if ($file !== '.' && $file !== '..' && is_dir($userfolder . '/' . $file)) {
                     $file = str_replace(array('/', '\\'), '', $file);
                     // Then copy the folder over
                     OC_Helper::copyr($userfolder . '/' . $file, $newuserfolder . '/' . $file);
                 }
             }
             // Import user app data
             if (file_exists($extractpath . $json->exporteduser . '/migration.db')) {
                 if (!($appsimported = self::importAppData($extractpath . $json->exporteduser . '/migration.db', $json, self::$uid))) {
                     return json_encode(array('success' => false));
                 }
             }
             // All done!
             if (!self::unlink_r($extractpath)) {
                 OC_Log::write('migration', 'Failed to delete the extracted zip', OC_Log::ERROR);
             }
             return json_encode(array('success' => true, 'data' => $appsimported));
             break;
         case 'instance':
             /*
              * EXPERIMENTAL
             // Check for new data dir and dbexport before doing anything
             // TODO
             
             // Delete current data folder.
             OC_Log::write( 'migration', "Deleting current data dir", OC_Log::INFO );
             if( !self::unlink_r( $datadir, false ) ) {
             	OC_Log::write( 'migration', 'Failed to delete the current data dir', OC_Log::ERROR );
             	return json_encode( array( 'success' => false ) );
             }
             
             // Copy over data
             if( !self::copy_r( $extractpath . 'userdata', $datadir ) ) {
             	OC_Log::write( 'migration', 'Failed to copy over data directory', OC_Log::ERROR );
             	return json_encode( array( 'success' => false ) );
             }
             
             // Import the db
             if( !OC_DB::replaceDB( $extractpath . 'dbexport.xml' ) ) {
             	return json_encode( array( 'success' => false ) );
             }
             // Done
             return json_encode( array( 'success' => true ) );
             */
             break;
     }
 }
Exemplo n.º 2
0
 /**
  * @brief imports a user, or owncloud instance
  * @param $path string path to zip
  * @param optional $type type of import (user or instance)
  * @param optional $uid userid of new user
  */
 public static function import($path, $type = 'user', $uid = null)
 {
     OC_Util::checkAdminUser();
     $datadir = OC_Config::getValue('datadirectory');
     // Extract the zip
     if (!($extractpath = self::extractZip($path))) {
         return json_encode(array('success' => false));
     }
     // Get export_info.json
     $scan = scandir($extractpath);
     // Check for export_info.json
     if (!in_array('export_info.json', $scan)) {
         OC_Log::write('migration', 'Invalid import file, export_info.json note found', OC_Log::ERROR);
         return json_encode(array('success' => false));
     }
     $json = json_decode(file_get_contents($extractpath . 'export_info.json'));
     if ($json->exporttype != $type) {
         OC_Log::write('migration', 'Invalid import file', OC_Log::ERROR);
         return json_encode(array('success' => false));
     }
     self::$exporttype = $type;
     // Have we got a user if type is user
     if (self::$exporttype == 'user') {
         if (!$uid) {
             self::$uid = $json->exporteduser;
         } else {
             self::$uid = $uid;
         }
     }
     // Handle export types
     switch (self::$exporttype) {
         case 'user':
             // Check user availability
             if (OC_User::userExists(self::$uid)) {
                 OC_Log::write('migration', 'User already exists', OC_Log::ERROR);
                 return json_encode(array('success' => false));
             }
             $run = true;
             OC_Hook::emit("OC_User", "pre_createUser", array("run" => &$run, "uid" => self::$uid, "password" => $json->hash));
             if (!$run) {
                 // Something stopped the user creation
                 OC_Log::write('migration', 'User creation failed', OC_Log::ERROR);
                 return json_encode(array('success' => false));
             }
             // Create the user
             if (!self::createUser(self::$uid, $json->hash)) {
                 return json_encode(array('success' => false));
             }
             // Emit the post_createUser hook (password is already hashed, will cause problems
             OC_Hook::emit("OC_User", "post_createUser", array("uid" => self::$uid, "password" => $json->hash));
             // Make the new users data dir
             $path = $datadir . '/' . self::$uid;
             if (!mkdir($path, 0755, true)) {
                 OC_Log::write('migration', 'Failed to create users data dir: ' . $path, OC_Log::ERROR);
                 return json_encode(array('success' => false));
             }
             // Copy data
             if (!self::copy_r($extractpath . $json->exporteduser, $datadir . '/' . self::$uid)) {
                 return json_encode(array('success' => false));
             }
             // Import user app data
             if (!($appsimported = self::importAppData($extractpath . $json->exporteduser . '/migration.db', $json, self::$uid))) {
                 return json_encode(array('success' => false));
             }
             // All done!
             if (!self::unlink_r($extractpath)) {
                 OC_Log::write('migration', 'Failed to delete the extracted zip', OC_Log::ERROR);
             }
             return json_encode(array('success' => true, 'data' => $appsimported));
             break;
         case 'instance':
             /*
              * EXPERIMENTAL
             // Check for new data dir and dbexport before doing anything
             // TODO
             
             // Delete current data folder.
             OC_Log::write( 'migration', "Deleting current data dir", OC_Log::INFO );
             if( !self::unlink_r( $datadir, false ) ){
             	OC_Log::write( 'migration', 'Failed to delete the current data dir', OC_Log::ERROR );
             	return json_encode( array( 'success' => false ) );
             }
             
             // Copy over data
             if( !self::copy_r( $extractpath . 'userdata', $datadir ) ){
             	OC_Log::write( 'migration', 'Failed to copy over data directory', OC_Log::ERROR );
             	return json_encode( array( 'success' => false ) );
             }
             
             // Import the db
             if( !OC_DB::replaceDB( $extractpath . 'dbexport.xml' ) ){
             	return json_encode( array( 'success' => false ) );
             }
             // Done
             return json_encode( 'success' => true );
             */
             break;
     }
 }