migrate.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Tom Needham
  6. * @copyright 2012 Tom Needham tom@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * provides an interface to migrate users and whole ownclouds
  24. */
  25. class OC_Migrate{
  26. // Array of OC_Migration_Provider objects
  27. static private $providers=array();
  28. // User id of the user to import/export
  29. static private $uid=false;
  30. // Holds the ZipArchive object
  31. static private $zip=false;
  32. // Stores the type of export
  33. static private $exporttype=false;
  34. // Array of temp files to be deleted after zip creation
  35. static private $tmpfiles=array();
  36. // Holds the db object
  37. static private $MDB2=false;
  38. // Schema db object
  39. static private $schema=false;
  40. // Path to the sqlite db
  41. static private $dbpath=false;
  42. // Holds the path to the zip file
  43. static private $zippath=false;
  44. // Holds the OC_Migration_Content object
  45. static private $content=false;
  46. /**
  47. * register a new migration provider
  48. * @param OC_Migrate_Provider $provider
  49. */
  50. public static function registerProvider($provider) {
  51. self::$providers[]=$provider;
  52. }
  53. /**
  54. * @brief finds and loads the providers
  55. */
  56. static private function findProviders() {
  57. // Find the providers
  58. $apps = OC_App::getAllApps();
  59. foreach($apps as $app) {
  60. $path = OC_App::getAppPath($app) . '/appinfo/migrate.php';
  61. if( file_exists( $path ) ) {
  62. include( $path );
  63. }
  64. }
  65. }
  66. /**
  67. * @brief exports a user, or owncloud instance
  68. * @param optional $uid string user id of user to export if export type is user, defaults to current
  69. * @param ootional $type string type of export, defualts to user
  70. * @param otional $path string path to zip output folder
  71. * @return false on error, path to zip on success
  72. */
  73. public static function export( $uid=null, $type='user', $path=null ) {
  74. $datadir = OC_Config::getValue( 'datadirectory' );
  75. // Validate export type
  76. $types = array( 'user', 'instance', 'system', 'userfiles' );
  77. if( !in_array( $type, $types ) ) {
  78. OC_Log::write( 'migration', 'Invalid export type', OC_Log::ERROR );
  79. return json_encode( array( 'success' => false ) );
  80. }
  81. self::$exporttype = $type;
  82. // Userid?
  83. if( self::$exporttype == 'user' ) {
  84. // Check user exists
  85. self::$uid = is_null($uid) ? OC_User::getUser() : $uid;
  86. if(!OC_User::userExists(self::$uid)){
  87. return json_encode( array( 'success' => false) );
  88. }
  89. }
  90. // Calculate zipname
  91. if( self::$exporttype == 'user' ) {
  92. $zipname = 'oc_export_' . self::$uid . '_' . date("y-m-d_H-i-s") . '.zip';
  93. } else {
  94. $zipname = 'oc_export_' . self::$exporttype . '_' . date("y-m-d_H-i-s") . '.zip';
  95. }
  96. // Calculate path
  97. if( self::$exporttype == 'user' ) {
  98. self::$zippath = $datadir . '/' . self::$uid . '/' . $zipname;
  99. } else {
  100. if( !is_null( $path ) ) {
  101. // Validate custom path
  102. if( !file_exists( $path ) || !is_writeable( $path ) ) {
  103. OC_Log::write( 'migration', 'Path supplied is invalid.', OC_Log::ERROR );
  104. return json_encode( array( 'success' => false ) );
  105. }
  106. self::$zippath = $path . $zipname;
  107. } else {
  108. // Default path
  109. self::$zippath = get_temp_dir() . '/' . $zipname;
  110. }
  111. }
  112. // Create the zip object
  113. if( !self::createZip() ) {
  114. return json_encode( array( 'success' => false ) );
  115. }
  116. // Do the export
  117. self::findProviders();
  118. $exportdata = array();
  119. switch( self::$exporttype ) {
  120. case 'user':
  121. // Connect to the db
  122. self::$dbpath = $datadir . '/' . self::$uid . '/migration.db';
  123. if( !self::connectDB() ) {
  124. return json_encode( array( 'success' => false ) );
  125. }
  126. self::$content = new OC_Migration_Content( self::$zip, self::$MDB2 );
  127. // Export the app info
  128. $exportdata = self::exportAppData();
  129. // Add the data dir to the zip
  130. self::$content->addDir(OC_User::getHome(self::$uid), true, '/' );
  131. break;
  132. case 'instance':
  133. self::$content = new OC_Migration_Content( self::$zip );
  134. // Creates a zip that is compatable with the import function
  135. $dbfile = tempnam( get_temp_dir(), "owncloud_export_data_" );
  136. OC_DB::getDbStructure( $dbfile, 'MDB2_SCHEMA_DUMP_ALL');
  137. // Now add in *dbname* and *dbprefix*
  138. $dbexport = file_get_contents( $dbfile );
  139. $dbnamestring = "<database>\n\n <name>" . OC_Config::getValue( "dbname", "owncloud" );
  140. $dbtableprefixstring = "<table>\n\n <name>" . OC_Config::getValue( "dbtableprefix", "oc_" );
  141. $dbexport = str_replace( $dbnamestring, "<database>\n\n <name>*dbname*", $dbexport );
  142. $dbexport = str_replace( $dbtableprefixstring, "<table>\n\n <name>*dbprefix*", $dbexport );
  143. // Add the export to the zip
  144. self::$content->addFromString( $dbexport, "dbexport.xml" );
  145. // Add user data
  146. foreach(OC_User::getUsers() as $user) {
  147. self::$content->addDir(OC_User::getHome($user), true, "/userdata/" );
  148. }
  149. break;
  150. case 'userfiles':
  151. self::$content = new OC_Migration_Content( self::$zip );
  152. // Creates a zip with all of the users files
  153. foreach(OC_User::getUsers() as $user) {
  154. self::$content->addDir(OC_User::getHome($user), true, "/" );
  155. }
  156. break;
  157. case 'system':
  158. self::$content = new OC_Migration_Content( self::$zip );
  159. // Creates a zip with the owncloud system files
  160. self::$content->addDir( OC::$SERVERROOT . '/', false, '/');
  161. foreach (array(".git", "3rdparty", "apps", "core", "files", "l10n", "lib", "ocs", "search", "settings", "tests") as $dir) {
  162. self::$content->addDir( OC::$SERVERROOT . '/' . $dir, true, "/");
  163. }
  164. break;
  165. }
  166. if( !$info = self::getExportInfo( $exportdata ) ) {
  167. return json_encode( array( 'success' => false ) );
  168. }
  169. // Add the export info json to the export zip
  170. self::$content->addFromString( $info, 'export_info.json' );
  171. if( !self::$content->finish() ) {
  172. return json_encode( array( 'success' => false ) );
  173. }
  174. return json_encode( array( 'success' => true, 'data' => self::$zippath ) );
  175. }
  176. /**
  177. * @brief imports a user, or owncloud instance
  178. * @param $path string path to zip
  179. * @param optional $type type of import (user or instance)
  180. * @param optional $uid userid of new user
  181. */
  182. public static function import( $path, $type='user', $uid=null ) {
  183. $datadir = OC_Config::getValue( 'datadirectory' );
  184. // Extract the zip
  185. if( !$extractpath = self::extractZip( $path ) ) {
  186. return json_encode( array( 'success' => false ) );
  187. }
  188. // Get export_info.json
  189. $scan = scandir( $extractpath );
  190. // Check for export_info.json
  191. if( !in_array( 'export_info.json', $scan ) ) {
  192. OC_Log::write( 'migration', 'Invalid import file, export_info.json note found', OC_Log::ERROR );
  193. return json_encode( array( 'success' => false ) );
  194. }
  195. $json = json_decode( file_get_contents( $extractpath . 'export_info.json' ) );
  196. if( $json->exporttype != $type ) {
  197. OC_Log::write( 'migration', 'Invalid import file', OC_Log::ERROR );
  198. return json_encode( array( 'success' => false ) );
  199. }
  200. self::$exporttype = $type;
  201. $currentuser = OC_User::getUser();
  202. // Have we got a user if type is user
  203. if( self::$exporttype == 'user' ) {
  204. self::$uid = !is_null($uid) ? $uid : $currentuser;
  205. }
  206. // We need to be an admin if we are not importing our own data
  207. if(($type == 'user' && self::$uid != $currentuser) || $type != 'user' ) {
  208. if( !OC_Group::inGroup( OC_User::getUser(), 'admin' )) {
  209. // Naughty.
  210. OC_Log::write( 'migration', 'Import not permitted.', OC_Log::ERROR );
  211. return json_encode( array( 'success' => false ) );
  212. }
  213. }
  214. // Handle export types
  215. switch( self::$exporttype ) {
  216. case 'user':
  217. // Check user availability
  218. if( !OC_User::userExists( self::$uid ) ) {
  219. OC_Log::write( 'migration', 'User doesn\'t exist', OC_Log::ERROR );
  220. return json_encode( array( 'success' => false ) );
  221. }
  222. // Copy data
  223. if( !self::copy_r( $extractpath . $json->exporteduser, $datadir . '/' . self::$uid ) ) {
  224. return json_encode( array( 'success' => false ) );
  225. }
  226. // Import user app data
  227. if( !$appsimported = self::importAppData( $extractpath . $json->exporteduser . '/migration.db', $json, self::$uid ) ) {
  228. return json_encode( array( 'success' => false ) );
  229. }
  230. // All done!
  231. if( !self::unlink_r( $extractpath ) ) {
  232. OC_Log::write( 'migration', 'Failed to delete the extracted zip', OC_Log::ERROR );
  233. }
  234. return json_encode( array( 'success' => true, 'data' => $appsimported ) );
  235. break;
  236. case 'instance':
  237. /*
  238. * EXPERIMENTAL
  239. // Check for new data dir and dbexport before doing anything
  240. // TODO
  241. // Delete current data folder.
  242. OC_Log::write( 'migration', "Deleting current data dir", OC_Log::INFO );
  243. if( !self::unlink_r( $datadir, false ) ) {
  244. OC_Log::write( 'migration', 'Failed to delete the current data dir', OC_Log::ERROR );
  245. return json_encode( array( 'success' => false ) );
  246. }
  247. // Copy over data
  248. if( !self::copy_r( $extractpath . 'userdata', $datadir ) ) {
  249. OC_Log::write( 'migration', 'Failed to copy over data directory', OC_Log::ERROR );
  250. return json_encode( array( 'success' => false ) );
  251. }
  252. // Import the db
  253. if( !OC_DB::replaceDB( $extractpath . 'dbexport.xml' ) ) {
  254. return json_encode( array( 'success' => false ) );
  255. }
  256. // Done
  257. return json_encode( array( 'success' => true ) );
  258. */
  259. break;
  260. }
  261. }
  262. /**
  263. * @brief recursively deletes a directory
  264. * @param $dir string path of dir to delete
  265. * $param optional $deleteRootToo bool delete the root directory
  266. * @return bool
  267. */
  268. private static function unlink_r( $dir, $deleteRootToo=true ) {
  269. if( !$dh = @opendir( $dir ) ) {
  270. return false;
  271. }
  272. while (false !== ($obj = readdir($dh))) {
  273. if($obj == '.' || $obj == '..') {
  274. continue;
  275. }
  276. if (!@unlink($dir . '/' . $obj)) {
  277. self::unlink_r($dir.'/'.$obj, true);
  278. }
  279. }
  280. closedir($dh);
  281. if ( $deleteRootToo ) {
  282. @rmdir($dir);
  283. }
  284. return true;
  285. }
  286. /**
  287. * @brief copies recursively
  288. * @param $path string path to source folder
  289. * @param $dest string path to destination
  290. * @return bool
  291. */
  292. private static function copy_r( $path, $dest ) {
  293. if( is_dir($path) ) {
  294. @mkdir( $dest );
  295. $objects = scandir( $path );
  296. if( sizeof( $objects ) > 0 ) {
  297. foreach( $objects as $file ) {
  298. if( $file == "." || $file == ".." || $file == ".htaccess")
  299. continue;
  300. // go on
  301. if( is_dir( $path . '/' . $file ) ) {
  302. self::copy_r( $path .'/' . $file, $dest . '/' . $file );
  303. } else {
  304. copy( $path . '/' . $file, $dest . '/' . $file );
  305. }
  306. }
  307. }
  308. return true;
  309. }
  310. elseif( is_file( $path ) ) {
  311. return copy( $path, $dest );
  312. } else {
  313. return false;
  314. }
  315. }
  316. /**
  317. * @brief tries to extract the import zip
  318. * @param $path string path to the zip
  319. * @return string path to extract location (with a trailing slash) or false on failure
  320. */
  321. static private function extractZip( $path ) {
  322. self::$zip = new ZipArchive;
  323. // Validate path
  324. if( !file_exists( $path ) ) {
  325. OC_Log::write( 'migration', 'Zip not found', OC_Log::ERROR );
  326. return false;
  327. }
  328. if ( self::$zip->open( $path ) != TRUE ) {
  329. OC_Log::write( 'migration', "Failed to open zip file", OC_Log::ERROR );
  330. return false;
  331. }
  332. $to = get_temp_dir() . '/oc_import_' . self::$exporttype . '_' . date("y-m-d_H-i-s") . '/';
  333. if( !self::$zip->extractTo( $to ) ) {
  334. return false;
  335. }
  336. self::$zip->close();
  337. return $to;
  338. }
  339. /**
  340. * @brief connects to a MDB2 database scheme
  341. * @returns bool
  342. */
  343. static private function connectScheme() {
  344. // We need a mdb2 database connection
  345. self::$MDB2->loadModule( 'Manager' );
  346. self::$MDB2->loadModule( 'Reverse' );
  347. // Connect if this did not happen before
  348. if( !self::$schema ) {
  349. require_once 'MDB2/Schema.php';
  350. self::$schema=MDB2_Schema::factory( self::$MDB2 );
  351. }
  352. return true;
  353. }
  354. /**
  355. * @brief creates a migration.db in the users data dir with their app data in
  356. * @return bool whether operation was successfull
  357. */
  358. private static function exportAppData( ) {
  359. $success = true;
  360. $return = array();
  361. // Foreach provider
  362. foreach( self::$providers as $provider ) {
  363. // Check if the app is enabled
  364. if( OC_App::isEnabled( $provider->getID() ) ) {
  365. $success = true;
  366. // Does this app use the database?
  367. if( file_exists( OC_App::getAppPath($provider->getID()).'/appinfo/database.xml' ) ) {
  368. // Create some app tables
  369. $tables = self::createAppTables( $provider->getID() );
  370. if( is_array( $tables ) ) {
  371. // Save the table names
  372. foreach($tables as $table) {
  373. $return['apps'][$provider->getID()]['tables'][] = $table;
  374. }
  375. } else {
  376. // It failed to create the tables
  377. $success = false;
  378. }
  379. }
  380. // Run the export function?
  381. if( $success ) {
  382. // Set the provider properties
  383. $provider->setData( self::$uid, self::$content );
  384. $return['apps'][$provider->getID()]['success'] = $provider->export();
  385. } else {
  386. $return['apps'][$provider->getID()]['success'] = false;
  387. $return['apps'][$provider->getID()]['message'] = 'failed to create the app tables';
  388. }
  389. // Now add some app info the the return array
  390. $appinfo = OC_App::getAppInfo( $provider->getID() );
  391. $return['apps'][$provider->getID()]['version'] = OC_App::getAppVersion($provider->getID());
  392. }
  393. }
  394. return $return;
  395. }
  396. /**
  397. * @brief generates json containing export info, and merges any data supplied
  398. * @param optional $array array of data to include in the returned json
  399. * @return bool
  400. */
  401. static private function getExportInfo( $array=array() ) {
  402. $info = array(
  403. 'ocversion' => OC_Util::getVersion(),
  404. 'exporttime' => time(),
  405. 'exportedby' => OC_User::getUser(),
  406. 'exporttype' => self::$exporttype,
  407. 'exporteduser' => self::$uid
  408. );
  409. if( !is_array( $array ) ) {
  410. OC_Log::write( 'migration', 'Supplied $array was not an array in getExportInfo()', OC_Log::ERROR );
  411. }
  412. // Merge in other data
  413. $info = array_merge( $info, (array)$array );
  414. // Create json
  415. $json = json_encode( $info );
  416. return $json;
  417. }
  418. /**
  419. * @brief connects to migration.db, or creates if not found
  420. * @param $db optional path to migration.db, defaults to user data dir
  421. * @return bool whether the operation was successful
  422. */
  423. static private function connectDB( $path=null ) {
  424. // Has the dbpath been set?
  425. self::$dbpath = !is_null( $path ) ? $path : self::$dbpath;
  426. if( !self::$dbpath ) {
  427. OC_Log::write( 'migration', 'connectDB() was called without dbpath being set', OC_Log::ERROR );
  428. return false;
  429. }
  430. // Already connected
  431. if(!self::$MDB2) {
  432. require_once 'MDB2.php';
  433. $datadir = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" );
  434. // DB type
  435. if( class_exists( 'SQLite3' ) ) {
  436. $dbtype = 'sqlite3';
  437. } else if( is_callable( 'sqlite_open' ) ) {
  438. $dbtype = 'sqlite';
  439. } else {
  440. OC_Log::write( 'migration', 'SQLite not found', OC_Log::ERROR );
  441. return false;
  442. }
  443. // Prepare options array
  444. $options = array(
  445. 'portability' => MDB2_PORTABILITY_ALL & (!MDB2_PORTABILITY_FIX_CASE),
  446. 'log_line_break' => '<br>',
  447. 'idxname_format' => '%s',
  448. 'debug' => true,
  449. 'quote_identifier' => true
  450. );
  451. $dsn = array(
  452. 'phptype' => $dbtype,
  453. 'database' => self::$dbpath,
  454. 'mode' => '0644'
  455. );
  456. // Try to establish connection
  457. self::$MDB2 = MDB2::factory( $dsn, $options );
  458. // Die if we could not connect
  459. if( PEAR::isError( self::$MDB2 ) ) {
  460. die( self::$MDB2->getMessage() );
  461. OC_Log::write( 'migration', 'Failed to create/connect to migration.db', OC_Log::FATAL );
  462. OC_Log::write( 'migration', self::$MDB2->getUserInfo(), OC_Log::FATAL );
  463. OC_Log::write( 'migration', self::$MDB2->getMessage(), OC_Log::FATAL );
  464. return false;
  465. }
  466. // We always, really always want associative arrays
  467. self::$MDB2->setFetchMode(MDB2_FETCHMODE_ASSOC);
  468. }
  469. return true;
  470. }
  471. /**
  472. * @brief creates the tables in migration.db from an apps database.xml
  473. * @param $appid string id of the app
  474. * @return bool whether the operation was successful
  475. */
  476. static private function createAppTables( $appid ) {
  477. if( !self::connectScheme() ) {
  478. return false;
  479. }
  480. // There is a database.xml file
  481. $content = file_get_contents(OC_App::getAppPath($appid) . '/appinfo/database.xml' );
  482. $file2 = 'static://db_scheme';
  483. // TODO get the relative path to migration.db from the data dir
  484. // For now just cheat
  485. $path = pathinfo( self::$dbpath );
  486. $content = str_replace( '*dbname*', self::$uid.'/migration', $content );
  487. $content = str_replace( '*dbprefix*', '', $content );
  488. $xml = new SimpleXMLElement($content);
  489. foreach($xml->table as $table) {
  490. $tables[] = (string)$table->name;
  491. }
  492. file_put_contents( $file2, $content );
  493. // Try to create tables
  494. $definition = self::$schema->parseDatabaseDefinitionFile( $file2 );
  495. unlink( $file2 );
  496. // Die in case something went wrong
  497. if( $definition instanceof MDB2_Schema_Error ) {
  498. OC_Log::write( 'migration', 'Failed to parse database.xml for: '.$appid, OC_Log::FATAL );
  499. OC_Log::write( 'migration', $definition->getMessage().': '.$definition->getUserInfo(), OC_Log::FATAL );
  500. return false;
  501. }
  502. $definition['overwrite'] = true;
  503. $ret = self::$schema->createDatabase( $definition );
  504. // Die in case something went wrong
  505. if( $ret instanceof MDB2_Error ) {
  506. OC_Log::write( 'migration', 'Failed to create tables for: '.$appid, OC_Log::FATAL );
  507. OC_Log::write( 'migration', $ret->getMessage().': '.$ret->getUserInfo(), OC_Log::FATAL );
  508. return false;
  509. }
  510. return $tables;
  511. }
  512. /**
  513. * @brief tries to create the zip
  514. * @param $path string path to zip destination
  515. * @return bool
  516. */
  517. static private function createZip() {
  518. self::$zip = new ZipArchive;
  519. // Check if properties are set
  520. if( !self::$zippath ) {
  521. OC_Log::write('migration', 'createZip() called but $zip and/or $zippath have not been set', OC_Log::ERROR);
  522. return false;
  523. }
  524. if ( self::$zip->open( self::$zippath, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE ) !== TRUE ) {
  525. OC_Log::write('migration', 'Failed to create the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR);
  526. return false;
  527. } else {
  528. return true;
  529. }
  530. }
  531. /**
  532. * @brief returns an array of apps that support migration
  533. * @return array
  534. */
  535. static public function getApps() {
  536. $allapps = OC_App::getAllApps();
  537. foreach($allapps as $app) {
  538. $path = self::getAppPath($app) . '/lib/migrate.php';
  539. if( file_exists( $path ) ) {
  540. $supportsmigration[] = $app;
  541. }
  542. }
  543. return $supportsmigration;
  544. }
  545. /**
  546. * @brief imports a new user
  547. * @param $db string path to migration.db
  548. * @param $info object of migration info
  549. * @param $uid optional uid to use
  550. * @return array of apps with import statuses, or false on failure.
  551. */
  552. public static function importAppData( $db, $info, $uid=null ) {
  553. // Check if the db exists
  554. if( file_exists( $db ) ) {
  555. // Connect to the db
  556. if(!self::connectDB( $db )) {
  557. OC_Log::write('migration','Failed to connect to migration.db',OC_Log::ERROR);
  558. return false;
  559. }
  560. } else {
  561. OC_Log::write('migration','Migration.db not found at: '.$db, OC_Log::FATAL );
  562. return false;
  563. }
  564. // Find providers
  565. self::findProviders();
  566. // Generate importinfo array
  567. $importinfo = array(
  568. 'olduid' => $info->exporteduser,
  569. 'newuid' => self::$uid
  570. );
  571. foreach( self::$providers as $provider) {
  572. // Is the app in the export?
  573. $id = $provider->getID();
  574. if( isset( $info->apps->$id ) ) {
  575. // Is the app installed
  576. if( !OC_App::isEnabled( $id ) ) {
  577. OC_Log::write( 'migration', 'App: ' . $id . ' is not installed, can\'t import data.', OC_Log::INFO );
  578. $appsstatus[$id] = 'notsupported';
  579. } else {
  580. // Did it succeed on export?
  581. if( $info->apps->$id->success ) {
  582. // Give the provider the content object
  583. if( !self::connectDB( $db ) ) {
  584. return false;
  585. }
  586. $content = new OC_Migration_Content( self::$zip, self::$MDB2 );
  587. $provider->setData( self::$uid, $content, $info );
  588. // Then do the import
  589. if( !$appsstatus[$id] = $provider->import( $info->apps->$id, $importinfo ) ) {
  590. // Failed to import app
  591. OC_Log::write( 'migration', 'Failed to import app data for user: ' . self::$uid . ' for app: ' . $id, OC_Log::ERROR );
  592. }
  593. } else {
  594. // Add to failed list
  595. $appsstatus[$id] = false;
  596. }
  597. }
  598. }
  599. }
  600. return $appsstatus;
  601. }
  602. /*
  603. * @brief creates a new user in the database
  604. * @param $uid string user_id of the user to be created
  605. * @param $hash string hash of the user to be created
  606. * @return bool result of user creation
  607. */
  608. public static function createUser( $uid, $hash ) {
  609. // Check if userid exists
  610. if(OC_User::userExists( $uid )) {
  611. return false;
  612. }
  613. // Create the user
  614. $query = OC_DB::prepare( "INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )" );
  615. $result = $query->execute( array( $uid, $hash));
  616. if( !$result ) {
  617. OC_Log::write('migration', 'Failed to create the new user "'.$uid."");
  618. }
  619. return $result ? true : false;
  620. }
  621. }